Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arrow operator in functor instance declarations?

I'm going through these Haskell 'intermediate' exercises, I've made the following instances:

class Fluffy f where
  furry :: (a -> b) -> f a -> f b

instance Fluffy [] where
  furry f [] = []

instance Fluffy Maybe where
  furry f (Just e) = Just (f e)
  furry f (Nothing) = Nothing

However, the third question syntax has stumped me:

instance Fluffy ((->) t) where
  ...

I've read up on the arrow operator and also read the answer to this which explains the role of (->) in a Monad instance. However I don't quite understand how (->) works in the context of Functors?

like image 657
Babra Cunningham Avatar asked Feb 04 '26 00:02

Babra Cunningham


1 Answers

We have:

class Fluffy f where
  furry :: (a -> b) -> f a -> f b

We want to define:

instance Fluffy ((->) t) where
  furry = ...

This means that in the above instance furry should have the type (a -> b) -> f a -> f b where f is ((->) t), or in other words:

furry :: (a -> b) -> ((->) t) a -> ((->) t) b

Just as ((+) 2) 3 is the same as 2 + 3, ((->) X) Y is the same as X -> Y (it's curried operator application and it even works at the type level):

furry :: (a -> b) -> (t -> a) -> (t -> b)

We can read the above signature as "given a function from a to b and a function from t to a, return a function from t to b".

Now you just have to implement it. :-)

like image 73
melpomene Avatar answered Feb 06 '26 01:02

melpomene



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!