[Haskell] Type problem

Emil Axelsson emax at cs.chalmers.se
Tue Dec 13 04:20:46 EST 2005


I found an acceptable, but not too nice workaround:

* Add another class without methods:

     class HasX a => HasX' a

* Make all types that may be unconstrained an instance of this class:

     instance HasX' Number

* Make pairs an instance of HasX (this feels wrong):

     instance HasX (a1,a2) where
       xVal = undefined

* Add appropriate constraints to the GADT types (X has constraint HasX'):

     data Val a where
       P :: a -> Val a        -- Primitive

       T2 :: (HasX a1, HasX a2) => (Val a1, Val a2) -> Val (a1,a2)

       X :: HasX' a => Val a  -- Unconstrained

* Add (HasX a => ) to the value type.


At least this is safe. The undefined xVal will never be run.

I still wonder if the original idea couldn't work somehow...

Thank you,

/ Emil



Emil Axelsson skrev:
> 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?
> 


More information about the Haskell mailing list