tags:

views:

111

answers:

3

I'm trying to run the BinaryDerive.hs script as per the instructions in the Data.Binary doc, which states:

To derive the instance for a type, load this script into GHCi, and bring your type into scope. Your type can then have its Binary instances derived as follows:

$ ghci -fglasgow-exts BinaryDerive.hs

*BinaryDerive> :l Example.hs
*Main> deriveM (undefined :: Drinks)

However when I try to follow those instructions I get:

c:\Scripts\Haskell>$ ghci -fglasgow-exts BinaryDerive.hs

*BinaryDerive> :l TemperatureRecord.hs
[1 of 1] Compiling TemperatureRecord (TemperatureRecord.hs, interpreted )

Ok, modules loaded:TemperatureRecord.

*TemperatureRecord> deriveM (undefined :: TemperatureRecord)

(interactive):1:0: Not in scope: 'deriveM'

I am assuming that there is an additional step that was not specified that a beginner, like myself would not be aware of. It seems the root of the problem is that loading the TemperatureRecord takes BinaryDerive out of scope. Anyone have any ideas?

+1  A: 

try

$ ghci -fglasgow-exts

Prelude> :l BinaryDerive TemperatureRecord

the .hs's are not necessary

Then you can access TemperatureRecord in a similar manner to this example (JSON Doc type taken from Real World Haskell, with some added Data and Typeable derives)

$ ghci -fglasgow-exts -XDeriveDataTypeable
Prelude> :l Binaryderive JSON
[1 of 2] Compiling JSON             ( JSON.hs, interpreted )
[2 of 2] Compiling BinaryDerive     ( Binaryderive.hs, interpreted )
Ok, modules loaded: BinaryDerive, JSON.
*BinaryDerive> deriveM (undefined::JSON.Doc)
instance Binary JSON.Doc where
  put Empty = putWord8 0
  put (Char a) = putWord8 1 >> put a
  put (Text a) = putWord8 2 >> put a
  put Line = putWord8 3
  put (Concat a b) = putWord8 4 >> put a >> put b
  put (Union a b) = putWord8 5 >> put a >> put b
  get = do
    tag_ <- getWord8
    case tag_ of
      0 -> return Empty
      1 -> get >>= \a -> return (Char a)
      2 -> get >>= \a -> return (Text a)
      3 -> return Line
      4 -> get >>= \a -> get >>= \b -> return (Concat a b)
      5 -> get >>= \a -> get >>= \b -> return (Union a b)
      _ -> fail "no parse"
barkmadley
+1  A: 

My understanding of ghci's loading/namespacing mechanisms is full of holes, but since nobody else has answered, here are some guessses:

  1. Try :m + BinaryDerive after :l Example.hs
  2. Add import BinaryDerive at the top of Example.hs
  3. Don't add module TemperatureRecord where at the top of Example.hs
Nathan Sanders
A: 

Thanks everyone for the answers. I tried both of them and neither worked, however, the answers I saw did lead me too a solution.

$ ghci -fglasgow-exts -XDeriveDataTypeable
Prelude> :l Binaryderive TemperatureRecord
Prelude> :m Binaryderive TemperatureRecord
Prelude BinaryDerive TemperatureRecord>deriveM (undefined :: TemperatureRecord)

The above worked for me. However, I am not using that solution in practice. I am working on getting a more automatic solution to work. Anyway thanks for helping me get something that works.

Jonathan Fischoff
You're actually looking for a `derive` tool. Two exist to my knowledge. [Derive](http://community.haskell.org/~ndm/derive/) and [DriFT](http://repetae.net/computer/haskell/DrIFT/). Using these tools you can [just add a pseudo-comment](http://community.haskell.org/~ndm/darcs/derive/derive.htm) to the `deriving` clause in your data declaration then run `derive` on the .hs file to generate the requested instances.
TomMD