Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Examples of multiple constraints? #46

Closed
MichaelXavier opened this issue Oct 12, 2023 · 3 comments
Closed

Examples of multiple constraints? #46

MichaelXavier opened this issue Oct 12, 2023 · 3 comments

Comments

@MichaelXavier
Copy link

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
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants