
On Mon, Jul 09, 2007 at 09:41:32PM +0100, Claus Reinke wrote:
i'm not sure i understand the problem correctly, but note that the branches in 'readMT' have identical implementations, the only difficulty is instantiating them at different hidden types, so that they try the appropriate 'Read' instances for those types. there's no need for different parsers beyond the 'Read' instances for every possible type.
This is now clear.
hiding concrete types in existentials sometimes only defers problems instead of solving them, but exposing class interfaces instead of types is a useful way to mitigate that effect. it just so happens that this particular problem, reading an existential type, slightly exceeds that pattern, as 'read' needs to know the hidden type to do its job ('read' does not determine the type from the input form, but uses the type to determine what form.the input should have).
That's exactly what I had in mind. But I ignored the specific problem of reading: "'read' uses the type", and the type is hidden..
a workaround is to try to read all possible types, then hide the type again once a match is found. the main disadvantage of this method is that we need a list of all the types that could possibly be hidden in 'MyType' (or at least a list of all the types that we expect to find hidden in 'MyType' when we read it).
we can, however, abstract out that list of types, and write a general type-level recursion to try reading every type in such a list:
class ReadAsAnyOf ts ex -- read an existential as any of hidden types ts where readAsAnyOf :: ts -> ReadPrec ex
[...]
-- a list of hidden types hidden = undefined :: (TipoA,(TipoB,()))
readMT :: ReadPrec MyType readMT = prec 10 $ do Ident "MT" <- lexP parens $ readAsAnyOf hidden -- r T1a `mplus` r T1b
This is a nice work around indeed and could be suitable for my specific problem. Thank you very much: this thread perfectly clarified the issue I was facing. Most kind of you. BTW, I have the feeling that existential types should be used with extreme care since they open up great possibilities of really hidden bugs: in other word, it seems to me they could be use to just trick the type checker. Sort of scary. Am I right? Thanks Andrea