[Haskell-cafe] lawless instances of Functor

Derek Elkins derek.a.elkins at gmail.com
Mon Jan 4 18:29:03 EST 2010


On Tue, Jan 5, 2010 at 8:22 AM, Brent Yorgey <byorgey at seas.upenn.edu> wrote:
> On Mon, Jan 04, 2010 at 11:49:33PM +0100, Steffen Schuldenzucker wrote:
>>
>> data Foo a = Foo a
>>
>> instance Functor Foo where
>>     fmap f (Foo x) = Foo . f . f $ x
>>
>> Then:
>>
>> fmap id (Foo x) == Foo . id . id $ x == Foo x
>>
>> fmap (f . g) (Foo x)      == Foo . f . g . f . g $ x
>> fmap f . fmap g $ (Foo x) == Foo . f . f . g . g $ x
>>
>> Now consider Foo Int and
>>
>> fmap ((+1) . (*3)) (Foo x)      == Foo $ (x * 3 + 1) * 3 + 1
>>     == Foo $ x * 9 + 4
>> fmap (+1) . fmap (*3) $ (Foo x) == Foo $ x * 3 * 3 + 1 + 1
>>     == Foo $ x * 9 + 2
>
> As others have pointed out, this doesn't typecheck; but what it DOES
> show is that if we had a type class
>
>  class Endofunctor a where
>    efmap :: (a -> a) -> f a -> f a

As an aside, for clarity, this class does NOT correspond to the
categorical notion of "endofunctor."  I don't think any such
identification was Brent's intent, I just want to avoid potential
confusion.


More information about the Haskell-Cafe mailing list