Type class inference trouble

Ken Shan ken@digitas.harvard.edu
Thu, 15 Feb 2001 14:37:09 -0500

Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

Hello all,

I'm trying to implement some simple natural language semantics with
Haskell (Hugs), and I'm running into trouble with type classes.
Here's what I want to do:  Suppose that

    x :: a -> b
    y :: a

then I want to write

    apply x y =3D x y :: b

Moreover, if

    x :: a
    y :: a -> b

I also want to write

    apply x y =3D y x :: b

So far I was able to implement what I want, as follows:

    class Applicable a b c | a b -> c where apply :: a -> b -> c

    instance Applicable (a -> b) a b where apply =3D ($)
    instance Applicable a (a -> b) b where apply =3D flip ($)

The code above allows me to say

    int :: Int -> Int
    int =3D id
    test =3D apply (int 3) (apply ((+)::Int->Int->Int) (int 5))

which results in test =3D=3D 8.  Now, suppose that m is a Monad.  If

    x :: m a
    y :: m (a -> b)

I want to write

    apply x y =3D do x' <- x; y' <- y; return x y :: m b

Similarly, if

    x :: m (a -> b)
    y :: m a

I want to write

    apply x y =3D do x' <- x; y' <- y; return y x :: m b

In general, if

    apply :: a -> b -> c

works, I also want

    apply :: m a -> m b -> m c

to work, by

    apply =3D liftM2 apply

Thus I attempted:

    import Monad

    instance (Monad m, Applicable a b c) =3D> Applicable (m a) (m b) (m c)
        where apply =3D liftM2 apply

    test2 =3D apply [int 3] (apply [(+)::Int->Int->Int] [int 5])

But Hugs said:

    ERROR M4.hs:23 - Unresolved top-level overloading
    *** Binding             : test2
    *** Outstanding context : Applicable Int (Int -> Int) b

What's wrong?  Given that class Applicable a b c | a b -> c, shouldn't
Hugs be able to figure out automatically what type b would result in
Applicable Int (Int -> Int) b?

Thanks in advance...

Edit this signature at http://rodimus.digitas.harvard.edu/cgi-bin/ken/sig
"The day Microsoft makes something that doesn't suck is probably the day
                   they start making vacuum cleaners" - Ernst Jan Plugge

Content-Type: application/pgp-signature
Content-Disposition: inline

Version: GnuPG v1.0.4 (GNU/Linux)
Comment: For info see http://www.gnupg.org