[Haskell-cafe] Re: Newcomers question
David Menendez
dave at zednenem.com
Sun Nov 1 12:06:05 EST 2009
On Sun, Nov 1, 2009 at 11:09 AM, b1g3ar5 <nick.straw at gmail.com> wrote:
> OK, I understand that now but I've got a supplimentary question.
>
> If I put:
>
> instance Eq b => Eq (a -> b) where
> (==) = liftA2 (Prelude.==)
You don't need the "Prelude." here.
> to do the Eq part I get another error:
>
> Couldn't match expected type `Bool'
> against inferred type `a -> Bool'
> In the expression: liftA2 (==)
> In the definition of `==': == = liftA2 (==)
> In the instance declaration for `Eq (a -> b)'
>
> Now can someone please explain this?
The type of liftA2 (==) is forall f b. Applicative f => f b -> f b ->
f Bool. In your case, f is (->) a, so liftA2 (==) :: (a -> b) -> (a ->
b) -> (a -> Bool), which can't be unified with the type for (==).
Personally, I recommend against trying to make (a -> b) an instance of
Num. If you really want to do arithmetic on functions, it's easier to
just do something along these lines:
(.+.),(.-.),(.*.) :: (Applicative f, Num a) => f a -> f a -> f a
(.+.) = liftA2 (+)
(.-.) = liftA2 (-)
(.*.) = liftA2 (*)
--
Dave Menendez <dave at zednenem.com>
<http://www.eyrie.org/~zednenem/>
More information about the Haskell-Cafe
mailing list