Adding output (data) to standard types

I would like to add deriving (Data) to the standard types. After enabling the extensions StandaloneDeriving , FlexibleContexts , DeriveDataTypeable and UndecidableInstances ghc accepts

 deriving instance Data Day => Data (Day) 

However, if I do the same for DiffTime , I get

  TemperatureRecord.hs: 30: 0:
     The data constructors of `DiffTime 'are not all in scope
       so you cannot derive an instance for it
     In the stand-alone deriving instance for
       `(Data DiffTime) => Data (DiffTime) '

I am doing all this to help auto-generate binary instances of standard types. Therefore, I have two questions:

  • How to solve the error that I get with DiffTime , and
  • What is the correct way to create standard libraries in binary serialization in Haskell?
+4
source share
2 answers

The time package does not provide the MkDiffTime constructor, DiffTime .

To create an instance of Data , you usually work with type constructors. Even if you automatically extract it, then what the derived instance will do.

Fortunately, it can work to some degree. You can extract data from DiffTime using toRational , and you can wrap a number in DiffTime using fromRational . Therefore, it should be possible to "fake" a Data instance using a "fake" constructor. This will be done "manually" (not displayed).

As for why the DiffTime constructor DiffTime not displayed, it will be similar to the reasoning behind private in C ++ et al. This gives DiffTime creators the freedom to change their implementation without breaking anything. Indeed, if they change the structure, it will not break your "fake" instance of Data and will not violate your serialization format.

+4
source

I havekell newb, but I think you can do this to expose the constructor (and therefore get the data automatically)

unpacking time

change

newtype DiffTime = MkDiffTime Pico derivative (Eq, Ord

to

DiffTime data = MkDiffTime Pico-derivation (Eq, Ord

and cabal installation works

-one
source

All Articles