Easier alternatives to existential types?

Well, yes, I know that almost certanly that was not what you were thinking when you wrote the question.
Sorry, I should include my code snippet indeed. data Chunk a = Chunk { cHeader :: Header, cData :: a } data Header = Header { hID :: Word32, hSize :: Word32 } data Format = Format { ... } newtype Data = Data ByteString h :: Header ... formatChunk :: Chunk Format ... dataChunk :: Chunk Data ... data Container a = Container { ctHeader :: Header, ctType :: Word32, ctContent :: [Chunk a] } -- Very naive. containerChunk :: Container a containerChunk = Container h 42 [formatChunk, dataChunk] I'm interested in common pattern used in Haskell for this kind of problems.
If you really want existentials, though, I'd recommend using GADT syntax
OK, I'll try it out. I'm already know about GADT's, but can't catch the difference between regular Algebraic Data Types and Generalised ones (in terms of disjoint union etc.). Can you recommend some paper where GADT's are described, but without very deep mathematical entities?

On Thu, Nov 26, 2009 at 10:01:32PM +0400, Emile Melnicov wrote:
Well, yes, I know that almost certanly that was not what you were thinking when you wrote the question.
Sorry, I should include my code snippet indeed.
data Chunk a = Chunk { cHeader :: Header, cData :: a } data Header = Header { hID :: Word32, hSize :: Word32 } data Format = Format { ... } newtype Data = Data ByteString
h :: Header ... formatChunk :: Chunk Format ... dataChunk :: Chunk Data ...
data Container a = Container { ctHeader :: Header, ctType :: Word32, ctContent :: [Chunk a] }
Do you *really* need 'Chunk' to be able to hold *any* type of data? Or are there just a few alternatives? If there are only a few alternatives, you can put the alternatives together in a data type, like this: data Content = ConH Header | ConD Data | ... other alternatives ... -- Chunk is no longer polymorphic data Chunk = Chunk { cHeader :: Header, cData :: Content } Then you can pattern-match on things of type Content to see which sort of content it is. Existential types are not required unless you really want to be able to put any type of content at all inside a Chunk. -Brent

2009/11/26 Brent Yorgey
Do you *really* need 'Chunk' to be able to hold *any* type of data? Or are there just a few alternatives? If there are only a few alternatives, you can put the alternatives together in a data type, like this:
Horribly enough, RIFF files do seem to support any type of data. A processor of RIFF files would presumably parse chunk types that it knows to expect and discard the rest. Data.Dynamic from the Hierarchical libs shipped with GHC is another option. Or a home brewed union type: data Chunk = WaveChunk { ... } | AuChunk { ... } | UnintrepretedChunk ByteString Best wishes Stephen
participants (3)
-
Brent Yorgey
-
Emile Melnicov
-
Stephen Tetley