[Haskell-cafe] lawless instances of Functor

Steffen Schuldenzucker sschuldenzucker at uni-bonn.de
Mon Jan 4 17:49:33 EST 2010


Hi Paul,

Paul Brauner wrote:
> Hi,
>
> I'm trying to get a deep feeling of Functors (and then pointed Functors,
> Applicative Functors, etc.). To this end, I try to find lawless
> instances of Functor that satisfy one law but not the other.
>
> I've found one instance that satisfies fmap (f.g) = fmap f . fmap g
> but not fmap id = id:
> [...]
> But I can't come up with an example that satifies law 1 and not law 2.
> I'm beginning to think this isn't possible but I didn't read anything
> saying so, neither do I manage to prove it.
>
> I'm sure someone knows :)

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

-- Steffen




More information about the Haskell-Cafe mailing list