[Haskell-cafe] Practise of hiding contsructors and serialization

Marc Weber marco-oweber at gmx.de
Sun Feb 22 14:55:29 EST 2009

While working on extending scion I got some trouble and wonder which is
the best way to do this?

I have to pass a compilation result from a scion process instance to the
scion daemon process. Of course I'd just like to use a simple Read Show
for that. However I can't because two datatypes are NominalDiffTime and
Bag ErrMsg: 

newtype NominalDiffTime = MkNominalDiffTime Pico deriving (Eq,Ord)
data ErrMsg = ErrMsg { 
        errMsgSpans     :: [SrcSpan],
        errMsgContext   :: PrintUnqualified,
        errMsgShortDoc  :: Message,
        errMsgExtraInfo :: Message

Both are fine and both don't export all contstructors so you can't just
let ghc derive instances for it?

So the only way is to duplicate those datatypes in order to derive some
Show, Read instances?

On the one hand I do understand that you don't want users to access
constructors directly. On the other hand it would be desirable to derive
Read, Show instances here.

Is there a way to get all?
a) hiding constructors so that you don't have to expose implementation
  details to the user.
b) still allow deriving serialization instances?

Is there a better way than using toRational to get a type which can be
serialzied quickly? I don't like Rational because the unit information
1e-12[s] is no longer included in the type..

Marc Weber

More information about the Haskell-Cafe mailing list