Existential quantification of config data types

Hi, I'm running into a weird problem i've never had before. I want to define a datatype like: data Network = forall a. Layer a => Network [a] for which I have things that implement Layer, like data TypicalLayer = ... instance Layer TypicalLayer where ... with this, I want to be able to load a Network from a config file where Network can consist of different data types all implementing Layer. Is there a nice way to do this without making a data type that combines all of my layer types, something like: data LayerType = TypicalLayerType TypicalLayer | AnotherLayerType AnotherLayer ... deriving (Show,Read) Thanks for the help! Charlie Durham

You can create a readLayer function that returns something like this: * data** OneLayer** =** forall** a. Layer a =>** OneLayer** a* Such that we get: * -- Read next layer* *readLayer** :: Handle -> IO* *OneLayer* Still, I feel that an ADT that combines all types is better as it fits the problem perfectly. Every time you add a new layer it will generate warnings for missing pattern matches. Regards, Sumit

Yeah, I'm starting to think the ADT makes the most sense. With the
readLayer call though, how do you know which layer to read? Or are you
still stuck with the failing to parse a list of potentials and then taking
the one that doesnt fail?
Thanks,
Charlie
On Mon, Apr 18, 2016 at 7:45 PM, Sumit Sahrawat, Maths & Computing, IIT
(BHU)
You can create a readLayer function that returns something like this:
* data** OneLayer** =** forall** a. Layer a =>** OneLayer** a*
Such that we get:
* -- Read next layer* *readLayer** :: Handle -> IO* *OneLayer*
Still, I feel that an ADT that combines all types is better as it fits the problem perfectly. Every time you add a new layer it will generate warnings for missing pattern matches.
Regards, Sumit

Yup, the only way would be to parse a possible list of alternatives. Should be simple with parsing libraries like parsec. try format1 <|> try format2 <|> ... Regards, Sumit
participants (2)
-
Charlie Durham
-
Sumit Sahrawat, Maths & Computing, IIT (BHU)