How to make the constructor part of the type of a Function class if it has more than one type parameter?

I am trying to understand how Functor-Typeclass works in Haskell. If you have a function f :: a -> b -> c

, and you want to partially apply it to 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 this behavior when you do something from the Functor-Typeclass with something like this:

instance Functor (MyTypeconstructor _ argB) where
   fmap <implementation of fmap>

      

I know that you can partially apply a type constructor to its first type parameter (standard currying):

instance Functor (MyTypeconstructor argA) where
   fmap <implementation of fmap>

      

but how can you partially apply it to the type parameter second / third / all except one

if possible?

thank.

+3


source to share


2 answers


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 identify your copy for F _ X

.

+5


source


More generally, you can write

newtype FlippedF a b = FlippedF { unFlip :: F b a } 

      

and then

instance Functor (FlippedF argB)

      



Or, more importantly,

newtype Flip f a b = Flip { unFlip :: f b a }

instance Functor (Flip F argB)

      

Flip

and many other type-level combinators are defined, eg. 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 )

+2


source







All Articles