In System.Random StdGen is defined as
data StdGen = StdGen Int32 Int32
but its constructor StdGen is not exported. How to make StdGen to be an
instance of Binary? The following won't work:
instance Data.Binary.Binary StdGen where
put (StdGen aa ab) = do
Data.Binary.put aa
Well, normally - you can't (unless there is some equivalent to the
constructor exported).
But there is a trick. You can use generic classes:
{-# OPTIONS_GHC -fglasgow-exts -XGenerics -package lang #-}
import Generics
class Binary' a where
put' :: a - Put
get' :: Get a
put' {| Unit |}
{-# OPTIONS_GHC -fglasgow-exts -XGenerics -package lang #-}
Got some problems:
Could not find module `Generics':
it is a member of package ghc-6.8.2, which is hidden
Failed, modules loaded: none.
and for ghci test.hs -fglasgow-exts -XGenerics -package lang
ghc-6.8.2: unknown
You're right.
The issue you've mentioned can be fixed easily - import Data.Generics
instead of Generics and get rid of -package lang (I've copied them from
the documentation without checking, seems like it's a bit outdated).
The real problem is that you can't use Get a in generics! And you
Miguel Mitrofanov wrote:
Well, normally - you can't (unless there is some equivalent to the
constructor exported).
But there is a trick. You can use generic classes:
{-# OPTIONS_GHC -fglasgow-exts -XGenerics -package lang #-}
import Generics
class Binary' a where
put' :: a - Put
get' ::
This time I've checked that it really compiles. Pretty much sure it works.
But how?! I'can't compile it:
test.hs:11:2:
Conflicting definitions for `put''
Bound at: test.hs:11:2-5
test.hs:13:2-5
test.hs:20:2-5
In the default-methods for class Binary'
Well, I've managed to produce a solution, quite ugly and unefficient. Still it
works (and I really need it). StdGen serialization occurs only once during
computation that lasts several hours, so the speed is not vital for me. Here is
my solution:
module Main where
import System.Random
import
Just copypasted it from my own email. Works fine.
On 4 Sep 2009, at 20:05, Grigory Sarnitskiy wrote:
This time I've checked that it really compiles. Pretty much sure it
works.
But how?! I'can't compile it:
test.hs:11:2:
Conflicting definitions for `put''
Bound at: test.hs:11:2-5