Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using functors/applicatives on custom data types with multiple type classes?

Consider a Users type with two parameters, a and b. Clearly, this enables User to be composed of two different types:

data Users a b = User a b deriving (Show, Eq, Ord)

How can we declare functors and applicative for this instance?

I've tried these approaches won't compile:

instance Functor Users where
   fmap f(User a b) = User (f a) (f b) 

instance Applicative Users where
   pure a b = User a b
   (<*>) User a b = (fmap a) (fmap b)

What's the reason these won't compile?

like image 856
Babra Cunningham Avatar asked Apr 01 '26 21:04

Babra Cunningham


1 Answers

Have a look at Data.Bifunctor for a type class for ADTs which are functorial in both arguments. User is then just a fancy name for tuple, and this already supports such an instance. Deriving a bifunctor instance is possible in Haskell.

@Bakuriu suggests defining User as a newtype and the using the extension GeneralizedNewtypeDeriving.

For the second one, see Biapplicative. As to be expected, its instance for (,) is:

instance Biapplicative (,) where
  pure = (,)
  ap (f, g) (a, b) = (f a, g b)
like image 117
ThreeFx Avatar answered Apr 04 '26 12:04

ThreeFx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!