[Haskell-beginners] Making a generic interpreter

Erik Helin erik.helin at gmail.com
Fri May 6 21:32:51 CEST 2011


On Fri, May 6, 2011 at 20:01, Brent Yorgey <byorgey at seas.upenn.edu> wrote:
>
> Why not use
>
>> class (Show a) => AbsInteger a where
>>     (+)        :: a -> a -> a
>>     (==)       :: AbsBool b => a -> a -> b
>>     absInteger :: Integer -> a
>
> ?

Because I believe (I might be very wrong now, I am very new to
Haskell) that I would run into problem with the b in "AbsBool b => a
-> a -> b" being unbounded when it would be used in:

data StackElement a b = StackInteger a
                                   |  StackBool b
                                   deriving (Show)

eval :: (AbsInteger a, AbsNum b) => [Code] -> [StackElement a b] ->
[StackElement a b]
{- some cases omitted -}
eval (CMP:c) (StackInteger x:StackInteger y:t) = eval c (StackBool (x == y):t)

Now, GHC tells me that the type b returned by x == y does not need to
equal the b specified in the type definition of eval.

Did I do something wrong in my definiton of eval?

On Fri, May 6, 2011 at 21:12, Patrick LeBoutillier
<patrick.leboutillier at gmail.com> wrote:
> Did you try Brent's suggestion? For me it worked great and also allows
> you to drop the language extentions.

I tried, but I didn't manage to get it working, due to the what I
describe above. Could you post your code using Brent's suggestion?

Clearly I am doing something wrong here, I just don't know what it is...



More information about the Beginners mailing list