[Haskell-cafe] Are GADTs what I need?
Chris Eidhof
chris at eidhof.nl
Mon Jul 13 11:54:52 EDT 2009
Then you could add a specific constructor for String. The main point
is: the case construct only works for values, not for types. There is
no typecase construct. If you want to have certain restrictions on the
'a', such as the Show class, you could also do something like this:
> data Value where
> VWrapper :: (Show a) => a -> Value
If you could elaborate a bit on what you're trying to accomplish (from
a higher viewpoint) then maybe we can help you some more.
-chris
On 13 jul 2009, at 17:42, Kev Mahoney wrote:
> Thanks, that helps.
>
> I was hoping to not have to parametrize Value as there is a fair bit
> of code to change, and it cascades down through the data structures
> (maybe a forall a . Value a will help here?)
>
> I will have a go at this approach. In case anyone is interested the
> code is at http://github.com/KMahoney
>
>
> 2009/7/13 Chris Eidhof <chris at eidhof.nl>:
>> 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