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 = x y :: b

Moreover, if

    x :: a
    y :: a -> b

I also want to write

    apply x y = 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 = ($)
    instance Applicable a (a -> b) b where apply = flip ($)

The code above allows me to say

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

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

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

I want to write

    apply x y = 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 = 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 = liftM2 apply

Thus I attempted:

    import Monad

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

    test2 = 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

PGP signature

Reply via email to