[Haskell-cafe] How to derive instance for type without exported constructor?

2009-09-04 Thread Grigory Sarnitskiy
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

Re: [Haskell-cafe] How to derive instance for type without exported constructor?

2009-09-04 Thread Miguel Mitrofanov
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 |}

Re: [Haskell-cafe] How to derive instance for type without exported constructor?

2009-09-04 Thread Grigory Sarnitskiy
{-# 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

Re: [Haskell-cafe] How to derive instance for type without exported constructor?

2009-09-04 Thread Miguel Mitrofanov
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

Re: [Haskell-cafe] How to derive instance for type without exported constructor?

2009-09-04 Thread kyra
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' ::

Re: [Haskell-cafe] How to derive instance for type without exported constructor?

2009-09-04 Thread Grigory Sarnitskiy
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'

Re: [Haskell-cafe] How to derive instance for type without exported constructor?

2009-09-04 Thread Grigory Sarnitskiy
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

Re: [Haskell-cafe] How to derive instance for type without exported constructor?

2009-09-04 Thread Miguel Mitrofanov
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