[Haskell-cafe] Newcomers question

Henning Thielemann lemming at henning-thielemann.de
Sat Oct 31 19:37:44 EDT 2009


On Sat, 31 Oct 2009, b1g3ar5 wrote:

> I'm trying:
>
> instance Num b => Num (a -> b) where
> fromInteger = pure . Prelude.fromInteger
> negate = fmap Prelude.negate
> (+) = liftA2 (Prelude.+)
> (*) = liftA2 (Prelude.*)
> abs = fmap Prelude.abs
> signum = fmap Prelude.signum
>
> but the compiler rejects it with:
>
> src\Main.hs:24:9:
>    Could not deduce (Show (a -> b), Eq (a -> b))
>      from the context (Num b)
>      arising from the superclasses of an instance declaration
>                   at src\Main.hs:24:9-29
>    Possible fix:
>      add (Show (a -> b), Eq (a -> b)) to the context of
>        the instance declaration
>      or add an instance declaration for (Show (a -> b), Eq (a -> b))
>    In the instance declaration for `Num (a -> b)'
>
> Could someone please explain this to me?
>
> I thought that it might be that it couldn't work out the functions
> necessary for (a->b) to be in the classes Show and Eq - so I tried
> adding definitions for == ans show, but it made no difference.

You have to define instances for Show and Eq, that is methods 'show' and 
(==), because the Num class has these classes as superclasses. This has 
been criticised a lot and is e.g. not the case in NumericPrelude. However, 
I would not seriously define a Num instance for functions:
    http://www.haskell.org/haskellwiki/Num_instance_for_functions


More information about the Haskell-Cafe mailing list