[Haskell-cafe] Are GADTs what I need?

Chris Eidhof chris at eidhof.nl
Mon Jul 13 08:09:57 EDT 2009


Hey Kev,

The types are "thrown away" during compile time. Therefore, if you  
have a constructor "VWrapper :: a -> Value" nothing is known about  
that "a" when you scrutinize it.

What you could do, however, is something like this:

> data Value a where
>   VInt :: Integer -> Value Integer
>   ...
>   VWrapper :: a -> Value a

And then you can write a function doSomething:

> doSomething :: Value String -> String
> doSomething (VWrapper s) = s

HTH,

-chris

On 13 jul 2009, at 12:41, Kev Mahoney wrote:

> Hi there,
>
> I'm currently writing an interpreter that I would like to be able to
> use with other haskell programs. I would like to be able to pass along
> arbitrary types though the interpreter. I've seen hints that GADTs can
> do this, but I am having trouble understanding them.
>
> So far, I've learnt you can do this:
>
> data Value where
> VInt :: Integer -> Value
> ...
> VWrapper :: a -> Value
>
> which can let you encode arbitrary 'dynamic' types into Value. I was
> hoping to be able to pattern match to get the value out again e.g.
>
> doSomething :: Value -> ....
> doSomething (VWrapper String s) = .....
>
> Also, anything that can help me out with GADTs in general will be much
> appreciated.
>
> Thanks,
> Kevin.
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe



More information about the Haskell-Cafe mailing list