Re: [Haskell] Type problem

2005-12-13 Thread Emil Axelsson

That's good enough until GHC 6.6.

Thank you very much!

/ Emil



Tomasz Zielonka skrev:

On Tue, Dec 13, 2005 at 09:46:31AM +0100, Emil Axelsson wrote:

Is this just a limitation of the current GATDs, or is it unreasonable of me 
to expect this to work?



AFAIK it is a current limitation of GADTs, which will be removed in GHC
6.6.



Is there any workaround, such as coercing the type of the value function?



I've had the same problem myself. The workaround is to replace some of
type-class constraints with "witness" GADTs. The code I attached
shows how you can do it. I chose to make HasX a GADT, and introduce
the HasX' type-class, but the latter is only for convenience. Note
the subtle change in Val's definition:

data Val a where
   ...
   X :: HasX a -> Val a  -- Unconstrained
   ^^

Best regards
Tomasz





{-# OPTIONS -fglasgow-exts #-}

data Number = XN -- Unconstrained
 | N Int  -- Constrained

data HasX a where
   HasX_Number :: HasX Number

xVal :: HasX a -> a
xVal HasX_Number = XN

class HasX' a where
hasX :: HasX a

instance HasX' Number where
hasX = HasX_Number

x :: HasX' a => Val a
x = X hasX

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 hx)   = xVal hx
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


___
Haskell mailing list
Haskell@haskell.org
http://www.haskell.org/mailman/listinfo/haskell


Re: [Haskell] Type problem

2005-12-13 Thread Tomasz Zielonka
On Tue, Dec 13, 2005 at 10:42:15AM +0100, Tomasz Zielonka wrote:
> On Tue, Dec 13, 2005 at 09:46:31AM +0100, Emil Axelsson wrote:
> > Is this just a limitation of the current GATDs, or is it unreasonable of me 
> > to expect this to work?
> 
> AFAIK it is a current limitation of GADTs, which will be removed in GHC
> 6.6.

Of course it is the limitation that will be removed, not GADTs ;-)

Best regards
Tomasz

-- 
I am searching for a programmer who is good at least in some of
[Haskell, ML, C++, Linux, FreeBSD, math] for work in Warsaw, Poland
___
Haskell mailing list
Haskell@haskell.org
http://www.haskell.org/mailman/listinfo/haskell


Re: [Haskell] Type problem

2005-12-13 Thread Tomasz Zielonka
On Tue, Dec 13, 2005 at 09:46:31AM +0100, Emil Axelsson wrote:
> Is this just a limitation of the current GATDs, or is it unreasonable of me 
> to expect this to work?

AFAIK it is a current limitation of GADTs, which will be removed in GHC
6.6.

> Is there any workaround, such as coercing the type of the value function?

I've had the same problem myself. The workaround is to replace some of
type-class constraints with "witness" GADTs. The code I attached
shows how you can do it. I chose to make HasX a GADT, and introduce
the HasX' type-class, but the latter is only for convenience. Note
the subtle change in Val's definition:

data Val a where
   ...
   X :: HasX a -> Val a  -- Unconstrained
   ^^

Best regards
Tomasz

-- 
I am searching for a programmer who is good at least in some of
[Haskell, ML, C++, Linux, FreeBSD, math] for work in Warsaw, Poland
{-# OPTIONS -fglasgow-exts #-}

data Number = XN -- Unconstrained
 | N Int  -- Constrained

data HasX a where
   HasX_Number :: HasX Number

xVal :: HasX a -> a
xVal HasX_Number = XN

class HasX' a where
hasX :: HasX a

instance HasX' Number where
hasX = HasX_Number

x :: HasX' a => Val a
x = X hasX

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 hx)   = xVal hx
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

___
Haskell mailing list
Haskell@haskell.org
http://www.haskell.org/mailman/listinfo/haskell


Re: [Haskell] Type problem

2005-12-13 Thread Emil Axelsson

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?


___
Haskell mailing list
Haskell@haskell.org
http://www.haskell.org/mailman/listinfo/haskell


[Haskell] Type problem

2005-12-13 Thread Emil Axelsson

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


___
Haskell mailing list
Haskell@haskell.org
http://www.haskell.org/mailman/listinfo/haskell