I am trying to implement this pair data type as an applicative functor but I am told that 'a' is not in scope. I thought already stated what 'a' was in the instance declaration.
data Pair a b = Pair a b deriving (Show)
instance Functor (Pair a) where
fmap f (Pair a b) = Pair a (f b)
instance Applicative (Pair a) where
pure x = Pair a x
Pair a f <*> Pair a' x = Pair (a a') (f x)
CodePudding user response:
a
is a type variable, not something you can use in the definition of pure
. pure
needs some way of getting a value of type a
to pair with x
. There are a few ways you could do that:
- A function of type
b -> a
that you could apply tox
. - A function of type
() -> a
that you could apply to()
. - Some predefined value of type
a
.
The Applicative
instance of (,) a
takes the third approach, by requiring that a
have a Monoid
instance so that you can define pure
in terms of mempty
.
instance Monoid a => Applicative (Pair a) where
pure x = Pair mempty x
Pair a f <*> Pair a' x = Pair (a <> a') (f x)
In your definition, you assumed that ( )
is defined for the a
values, which means you are missing a Num
constraint, but also that you could simply use 0
in your definition of pure
.
instance Num a => Applicative (Pair a) where
pure x = Pair 0 x
Pair a f <*> Pair a' x = Pair (a a') (f x)