haskell - An example of a type with kind * -> * which cannot be an instance of Functor -


i'm haskell novice, apologies if answer obvious, i'm working through typeclassopedia in effort better understand categories. when doing exercises section on functors, came across problem:

give example of type of kind * -> * cannot made instance of functor (without using undefined).

my first thought define kind of infinitely recursing definition of fmap, wouldn't same if undefined used in definition?

if explain answer appreciated.

thanks!

source of original exercise here, section 3: http://www.haskell.org/haskellwiki/typeclassopedia#introduction

a simple example is

data k = k (a -> int) 

here's ghci tells try automatically derive functor instance k:

prelude> :set -xderivefunctor prelude> data k = k (a -> int) prelude> :k k k :: * -> * prelude> data k = k (a -> int) deriving functor  <interactive>:14:34:     can't make derived instance of `functor k':       constructor `k' must not use type variable in function argument     in data type declaration `k' 

the problem standard functor class represents covariant functors (fmap lifts argument f -> f b), there no way can compose a -> b , a -> int function of type b -> int (see ramon's answer). however, it's possible define type class contravariant functors:

class contravariant f     contramap :: (a -> b) -> f b -> f  

and make k instance of it:

instance contravariant k     contramap f (k g) = k (g . f) 

for more on covariance/contravariance in haskell, see here.

edit: here's a nice comment on topic chris smith on reddit.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -