[Haskell-cafe] reading existential types
Andrea Rossato
mailing_list at istitutocolli.org
Mon Jul 9 12:36:13 EDT 2007
On Mon, Jul 09, 2007 at 04:28:43PM +0100, Claus Reinke wrote:
> which is the important hint! the parser used for 'read' depends on
> the return type, but the existential type _hides_ the internal type
> which would be needed to select a read parser.
I think that this is precisely what I wasn't getting. If I understand
it correctly, this also means that what I supposed to be my smart
trick is actually dumbly useless - the "Read e" here:
forall e . (MyClass e, Show e, Read e) => MT (e,Int)
> if your hidden types have distinguishable 'show'-representations,
> you could write your own typecase like this (making use of the
> fact that 'read' parsers with incorrect type will fail, and that the
> internal type can be hidden after parsing)
>
> readMT :: ReadPrec MyType
> readMT = prec 10 $ do
> Ident "MT" <- lexP
> parens $ (do { m <- readPrec; return (MT (m::(TipoA,Int))) })
> `mplus` (do { m <- readPrec; return (MT (m::(TipoB,Int))) })
>
> *Test> read (show [MT (T1a,1),MT (T1b,3)]) :: [MyType]
> [MT (T1a,1),MT (T1b,3)]
>
> (if necessary, you could have 'show' embed a type representation for the
> hidden type, and dispatch on that representation in 'read')
The problem is that I was trying to find a way to define the class
(MyClass) and not writing a parser for every possible type (or even
using their show-representation): I wanted a polymorphic list of types
over which I could use the method defined for their class, but, as far
as I can get it, this is not possible.
Thanks for your kind attention.
All the best,
Andrea
More information about the Haskell-Cafe
mailing list