Reputation: 181
Say I have the following code:
data LiftItOut f a = LiftItOut (f a)
deriving Show
instance (Functor fct) => Functor (LiftItOut fct) where
fmap f (LiftItOut fctor) = LiftItOut (fmap f fctor)
If I try calling fmap (\x-> 3 x) (LiftItOut (+3))
it doesn't work. Though, wouldn't it make sense to work? The result would be LiftItOut (fmap (\x ->3 x) (+3))
and, as I see it, the 3 would be fed to (+3) and the result would be wrapped in LiftItOut. I read about (->r) functors on learnyouahaskell though it didn't click with me. Appreciate any and every bit of help
Upvotes: 1
Views: 99
Reputation: 153342
You're right in that you'd get to LiftItOut (fmap (\x -> 3 x) (+3))
, and you correctly identified that the wrapped functor of interest is the function functor (N.B. the (r ->)
functor, not the (-> r)
functor), but your reasoning from there got a bit sloppy. Recall that for functions, we have fmap = (.)
. So:
LiftItOut (fmap (\x -> 3 x) (+3))
= { fmap = (.) }
LiftItOut ((\x -> 3 x) . (+3))
= { f . g = \y -> f (g y) }
LiftItOut (\y -> (\x -> 3 x) ((+3) y))
= { section application }
LiftItOut (\y -> (\x -> 3 x) (y + 3))
= { lambda application }
LiftItOut (\y -> 3 (y + 3))
...not exactly the result you were thinking you'd get, I wager! And hopefully in this simplified form it's more clear why this isn't really sensible: y + 3
is probably sensible enough, but you are attempting to provide that as an argument to 3
, which isn't really a function.
Upvotes: 2