[Haskell-beginners] Making a generic interpreter

Patrick LeBoutillier patrick.leboutillier at gmail.com
Fri May 6 18:45:43 CEST 2011


Erik,

On Fri, May 6, 2011 at 3:01 AM, Erik Helin <erik.helin at gmail.com> wrote:
>
> Now, I would like to change StackElement to be able to work with any
> types, that is:
>
> data StackElement a b = StackInteger a
>                                   | StackInteger b
>                                   deriving (Show)
>
> How would I make this change?

I don't see how you can make the StackBool something else than Bool.
The CMP and BRANCH operators seem to me very Boolean by definition.
Can you provide an example of how it would work?

Patrick

>
> I thought about using typeclasses, but since type a and type b in
> StackElement a b clearly are related (the == operation on type a must
> result in type b), I need to use multi-parameter type classes (and
> maybe also functional dependecies?).
>
> I also thought about introducing a new type Operation:
>
> data Operation a b = Operation { cmp :: a -> a -> b, add :: a -> a ->
> a, (more required functions) }
>
> Is any of these solutions the preferred one, or is there another
> solution that I don't know of (this is very likely, I'm completely new
> to Haskell)?
>
> Thanks for taking your time and reading this lengthy post,
> Erik
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>



-- 
=====================
Patrick LeBoutillier
Rosemère, Québec, Canada



More information about the Beginners mailing list