Why doesn't Functor provide a standard fmap implementation?

In class type definition Functor:

class Functor f where
    fmap :: (a -> b) -> f a -> f b

Why fmapdoesn't it have a default implementation? Something like that:

class Functor f where
    fmap :: (a -> b) -> f a -> f b
    fmap fn (f a) = (f $ (fn) a)

When I write instances Functor, I write the same code for each instance manually. Is there a way to specify a default implementation?

+4
source share
1 answer

You probably want:

{-# LANGUAGE DeriveFunctor #-}

data T1 a = T1 a
  deriving Functor
data T2 a = T2 a
  deriving Functor

Regarding why there is no default implementation for a functor: your sentence only works if it fis an identical functor (up to isomorphism). That is, it works on

data F a = F a

but he will not work on

data F a = F a a

or

data F a = F (Int -> a) [a] (Maybe a)

fmap s.

fmap, , , , , , fmap. , derive Functor, .

+14

Source: https://habr.com/ru/post/1570453/


All Articles