Reputation: 710
I am trying to understand the way the Functor-Typeclass works in Haskell. If you have a function f :: a -> b -> c
and you want to partially apply it to the argB
to get a function that takes one argument, you can simply do:
f' :: a -> c
f' x = f x argB
and use that. Is it possible to get such behavior when making something part of the Functor-Typeclass with something like this:
instance Functor (MyTypeconstructor _ argB) where
fmap <implementation of fmap>
I know you can partially apply a Type-constructor to its first type-parameter (standart currying):
instance Functor (MyTypeconstructor argA) where
fmap <implementation of fmap>
but how can you partially apply it to its second / third / all except one
type-parameter, if this is possible?
thank you.
Upvotes: 3
Views: 289
Reputation: 170805
More generally, you can write
newtype FlippedF a b = FlippedF { unFlip :: F b a }
and then
instance Functor (FlippedF argB)
Or even more generally,
newtype Flip f a b = Flip { unFlip :: f b a }
instance Functor (Flip F argB)
Flip
and many more type-level combinators are defined in e.g. http://hackage.haskell.org/package/TypeCompose-0.9.12/docs/Control-Compose.html#g:9 (and also http://hackage.haskell.org/package/bifunctors-5.4.1/docs/Data-Bifunctor-Flip.html#t:Flip)
Upvotes: 2
Reputation: 2223
Suppose you have data F a b = ...
, define
newtype Fx a = Fx { unFx :: F a X }
to partially apply F
to X
in the second argument. Now you can use
instance Functor Fx where
fmap f fa = ...
to define your instance for F _ X
.
Upvotes: 5