[Haskell] Type problem
Emil Axelsson
emax at cs.chalmers.se
Tue Dec 13 03:46:31 EST 2005
Hello all,
Could I please get some guidance with this?
I'm working on implementing a simple relational language in Haskell.
I'm trying to construct a data type that can represent values and patterns for a
small set of supported types. See code below.
HasX is a class of types that have an unconstrained value (xVal).
Number is a typical member of that class.
Val is my value/pattern data type.
P represents a primitive value and T2 is used to make structure.
X represents the unconstrained value or a wildcard pattern. It can only be used
for types in HasX.
The problem is the commented line in the value function. I want to use the xVal
method to get the value for X. This is only allowed if I add the constraint
(HasX a => ). But I don't want value to have that constraint, since then I
cannot run it on pairs.
Furthermore, it should be safe without the constraint. ex2 shows that we cannot
use X to construct values that are not in HasX.
Is this just a limitation of the current GATDs, or is it unreasonable of me to
expect this to work?
Is there any workaround, such as coercing the type of the value function?
--
/ Emil
********************************
{-# OPTIONS -fglasgow-exts #-}
class HasX a where
xVal :: a
data Number = XN -- Unconstrained
| N Int -- Constrained
instance HasX Number where
xVal = XN
data Val a where
P :: a -> Val a -- Primitive
T2 :: (Val a1, Val a2) -> Val (a1,a2)
X :: HasX a => Val a -- Unconstrained
value :: Val a -> a
-- value X = xVal
value (P a) = a
value (T2 (a1,a2)) = (value a1, value a2)
ex1 :: Val (Number,(Number,Number))
ex1 = T2 (P (N 3), T2 (X, P (N 5)))
-- ex2 :: Val (Number,Number)
-- ex2 = X
More information about the Haskell
mailing list