jcpetruzza / barbies

BSD 3-Clause "New" or "Revised" License
92 stars 15 forks source link

Examples of multiple constraints? #46

Closed MichaelXavier closed 1 year ago

MichaelXavier commented 1 year ago

I've noticed in the code that the convenience functions like bmapC seem to let you specify a constraint via type applications like bmapC @Show but I haven't found any examples of specifying multiple constraints such as Show and Eq. I haven't really found a good solution for this because type applications doesn't seem to like specifying multiple constraints with one type application. Combining multiple constraints into a type alias with ConstraintKinds doesn't seem to work easier because they don't get partially applied in the same way that individual constraints do. I'm very hesitant to define an entirely new class just for a specific combination of 2 or more classes. Am I missing a better way to do this?

jcpetruzza commented 1 year ago

Good question. I think one could use a higher-kinded version of the (&) class in the constraints package. Something like:

{-# LANGUAGE ConstraintKinds, UndecidableSuperClasses #-}
module Foo where

import GHC.Generics
import Barbies

class (c a, d a) => (c & d) a where
instance (c a, d a) => (c & d) a where

blah :: (Eq a, Show a, Enum a) => Maybe a -> [a]
blah = maybe [] pure

convert :: Foo Maybe -> Foo []
convert = bmapC @(Eq & Show & Enum) blah

data Foo f = Foo (f Int)
  deriving Generic
  deriving (FunctorB, ConstraintsB)

deriving instance AllBF Show f Foo => Show (Foo f)

Would that work for you?

MichaelXavier commented 1 year ago

That worked great, thank you! I had tried the one from the constraints package but it wasn't clicking with me that I needed a fully applied version. It may be worth adding this somewhere in the docs and mentioning that you can use it in the constraint-oriented parts of the barbies API. IMO it would also be completely reasonable to actually define and export it in barbies itself if the language extensions aren't too problematic.

jcpetruzza commented 1 year ago

IMO it would also be completely reasonable to actually define and export it in barbies itself if the language extensions aren't too problematic.

Done in 2.0.5.0!