Re: Re : escape from existential quantification

2003-02-27 Thread Hal Daume III
> It occasionally happens that I *know* what type is (or at least ought to be) inside
> an existential type, but unfortunately GHC doesn't, and I need to get the value out.
> This can be solved using dynamic types, for example you declare the datatype as

You can also use an unsafe cast operation if you know statically what the
type will be, but the compiler doesn't.  See for example
http://www.isi.edu/~hdaume/haskell/NewBinary/TestBits.hs for an example of
this.  It essentially has a function which writes items of arbitrary type
(in a list) to a binary memory location and then reads the back.  When
reading them back, the only way to know the type is to look at the type of
the corresponding item on the "write" list.  It relies on this to make the
casting really a safe operation.


___
Haskell mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell


Re : escape from existential quantification

2003-02-27 Thread George Russell
Wang Meng wrote
I understand that existentially bound types cannot escape.

For example, say we have
data Foo = forall a. Foo Int a
Then we cannot define a function
extract (Foo i a) = a
However,this limitation makes it extremly difficult to program with local
quantifications.Is there any way to by pass this?
It occasionally happens that I *know* what type is (or at least ought to be) inside
an existential type, but unfortunately GHC doesn't, and I need to get the value out.
This can be solved using dynamic types, for example you declare the datatype as
> data Foo = forall a. Typeable a => Foo Int a

(or something like that), then you can extract the a value by going to Dynamic and back.

___
Haskell mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell


Re: escape from existential quantification

2003-02-27 Thread Nick Name
On Thu, 27 Feb 2003 18:26:31 +
Keith Wansbrough <[EMAIL PROTECTED]> wrote:

> 
>  The idea is to use a type more like this:
> 
>  data Foo = forall a. Foo Int a (a -> (Int,Bool)) (a -> Int) (a ->
>  Foo)
> 
>  where the functions are the operations you want to use on the data

Or else one can use type classes:

-
data Foo = forall a. Show a => Foo a

instance Show Foo where 
show (Foo x) = show x

main = print [Foo 3,Foo "ciao"]
-

Vincenzo

-- 
Mai pensato a cosa vuol dire 10 anni di embargo?
Si può chiedere ad una popolazione ormai allo stremo di subire un'altra
guerra e magari un secondo embargo? Cosa c'entrano i bambini che muoiono
di fame, di radiazioni e di mancanza di medicine con i giochi di potere
di un dittatore? Che colpa ne portano?
Leggere per esempio: http://www.fulviopoglio.com/salvi1.htm

___
Haskell mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell


Re: escape from existential quantification

2003-02-27 Thread Keith Wansbrough
> I understand that existentially bound types cannot escape.
> 
> For example, say we have
> data Foo = forall a. Foo Int a
> 
> Then we cannot define a function
> extract (Foo i a) = a
> 
> However,this limitation makes it extremly difficult to program with local
> quantifications.Is there any way to by pass this?

The idea is to use a type more like this:

data Foo = forall a. Foo Int a (a -> (Int,Bool)) (a -> Int) (a -> Foo)

where the functions are the operations you want to use on the data.  So now a list of 
Foos can contain data of many different types, as long as it is paired with the 
appropriate accessor functions for those types.  You can use it like this:

case x of
  Foo n x f g h -> if snd (f x) then g x else 0

for example.

--KW 8-)
-- 
Keith Wansbrough <[EMAIL PROTECTED]>
http://www.cl.cam.ac.uk/users/kw217/
University of Cambridge Computer Laboratory.

___
Haskell mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell


escape from existential quantification

2003-02-27 Thread Wang Meng
I understand that existentially bound types cannot escape.

For example, say we have
data Foo = forall a. Foo Int a

Then we cannot define a function
extract (Foo i a) = a

However,this limitation makes it extremly difficult to program with local
quantifications.Is there any way to by pass this?

 -W-M-
  @ @  
   |
  \_/   






___
Haskell mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell