[Haskell-cafe] handling multiple versions of a data structure

Daniel Peebles pumpkingod at gmail.com
Thu Dec 16 19:56:50 CET 2010


I haven't tested this idea, but it occurred to me that this might be a good
place for data families:

data Z = Z
newtype S n = S n

-- Some nastiness to avoid having to see into the future?
type family Pred n :: *
type instance Pred (S n) = n
type instance Pred Z = Z

class MyDataClass ver where
  data MyData ver :: *
  upgrade :: MyData (Pred ver) -> MyData ver
  -- other methods that won't change from version to version

class Less a b
instance Less Z a
instance Less a b => Less a (S b)

convert :: Less a b => MyData a -> MyData b
-- you may need a method in Less to witness the less-ness, but then you'd
iterate your upgrade until you reach the version you want.

-- Then you might want to abstract over this class with an existential, so
it doesn't infect other things:
data MyDataGeneral = forall ver. MyDataClass ver => MyDataGeneral ver

-- now make instances for versions you have, with data instances for your
current version of the structure.


This might not even compile as I just wrote it into my email client, but it
seems like it could work. Any comments?

Dan

On Thu, Dec 16, 2010 at 1:26 PM, Dmitry V'yal <akamaus at gmail.com> wrote:

> Greetings,
>
> while developing my neural net simulator I stumbled upon a problem.
>
> I have a data type NeuralNet and use Show and Read instances for saving and
> loading configurations. As time passed, I changed the data type, so the
> program can no longer load files saved in previous versions.
>
> I want fix it. My current idea looks as follows. I'm going to create a
> bunch of types NN1, NN2, NN3..NNn for different versions and write
> converters c12 :: N1 -> N2, c23 :: N2 -> N3 and so on.
>
> But how to organize the whole process of parsing String into NNn so it's
> easy to change formats?
> Something based on using a list of parsers
> [read, c43 . read, c43 . c23 . read, c43, c23 . c12 . read, c43 . c32 . c21
> . read]
>
> looks rather verbose and grows quadratically with N.
>
> I'm sure there must be a more elegant way. Any ideas?
>
> Dmitry
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20101216/79eaf467/attachment.htm>


More information about the Haskell-Cafe mailing list