Existential quantification of config data types

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Existential quantification of config data types

Charlie Durham
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

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Existential quantification of config data types

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


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Existential quantification of config data types

Charlie Durham
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) <[hidden email]> wrote:

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



_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Existential quantification of config data types

Sumit Sahrawat, Maths & Computing,
 IIT (BHU)

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


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe