standalone-derive-topdown: This package will derive class instance along the data type declaration tree.

This is a package candidate release! Here you can preview how this package release will appear once published to the main package index (which can be accomplished via the 'maintain' link below). Please note that once a package has been published to the main package index it cannot be undone! Please consult the package uploading documentation for more information.

[maintain] [Publish]

For a very complex composited data type, you just need to write one deriving declaration instead of writing deriving for each of them. Please see the example in Readme file. You need to use GHC 7.10 or higher GHC version.


[Skip to Readme]

Properties

Versions 0.0.0.1, 0.0.0.1, 0.0.0.2
Change log None available
Dependencies base (>=4.8 && <4.9), mtl (>=2.2.1), template-haskell (>=2.10) [details]
License MIT
Author songzh
Maintainer Haskell.Zhang.Song@hotmail.com
Category Development
Home page https://github.com/HaskellZhangSong/TopdownDerive
Uploaded by songzh at 2015-10-04T04:02:46Z

Modules

[Index]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


Readme for standalone-derive-topdown-0.0.0.1

[back to package description]

TopdownDerive

This code will do standalone deriving along the data type declaration dependencies in Haskell. Please see https://ghc.haskell.org/trac/ghc/ticket/10607 For example

{-# LANGUAGE TemplateHaskell  #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
    
{-# OPTIONS_GHC -ddump-splices  #-}
    
import Data.Derive.TopDown.StandaloneDerive
import qualified GHC.Generics as G
import qualified Data.Binary as B
import qualified Data.Aeson as A

data C a b = A (B a)
data B a = B a | F (D a)
data D b = D b | E b
derivings instances [''Eq, ''G.Generic, ''Ord] ''C

derivings generic_instances [''B.Binary, ''A.FromJSON, ''A.ToJSON] ''C

will give:

derivings instances [''Eq, ''G.Generic, ''Ord] ''C
======>
deriving instance Eq b_acQA => Eq (D b_acQA)
deriving instance Eq a_acQB => Eq (B a_acQB)
deriving instance (Eq a_acQC, Eq b_acQD) => Eq (C a_acQC b_acQD)
deriving instance G.Generic b_acQA => G.Generic (D b_acQA)
deriving instance G.Generic a_acQB => G.Generic (B a_acQB)
deriving instance (G.Generic a_acQC, G.Generic b_acQD) =>
                  G.Generic (C a_acQC b_acQD)
deriving instance Ord b_acQA => Ord (D b_acQA)
deriving instance Ord a_acQB => Ord (B a_acQB)
deriving instance (Ord a_acQC, Ord b_acQD) => Ord (C a_acQC b_acQD)
D:\Haskell\TopDerive.hs:20:1-70: Splicing declarations
derivings
  generic_instances [''B.Binary, ''A.FromJSON, ''A.ToJSON] ''C
======>
deriving instance (B.Binary b_acQA, G.Generic b_acQA) =>
                  B.Binary (D b_acQA)
deriving instance (B.Binary a_acQB, G.Generic a_acQB) =>
                  B.Binary (B a_acQB)
deriving instance (B.Binary a_acQC,
                   B.Binary b_acQD,
                   G.Generic a_acQC,
                   G.Generic b_acQD) =>
                  B.Binary (C a_acQC b_acQD)
deriving instance (A.FromJSON b_acQA, G.Generic b_acQA) =>
                  A.FromJSON (D b_acQA)
deriving instance (A.FromJSON a_acQB, G.Generic a_acQB) =>
                  A.FromJSON (B a_acQB)
deriving instance (A.FromJSON a_acQC,
                   A.FromJSON b_acQD,
                   G.Generic a_acQC,
                   G.Generic b_acQD) =>
                  A.FromJSON (C a_acQC b_acQD)
deriving instance (A.ToJSON b_acQA, G.Generic b_acQA) =>
                  A.ToJSON (D b_acQA)
deriving instance (A.ToJSON a_acQB, G.Generic a_acQB) =>
                  A.ToJSON (B a_acQB)
deriving instance (A.ToJSON a_acQC,
                   A.ToJSON b_acQD,
                   G.Generic a_acQC,
                   G.Generic b_acQD) =>
                  A.ToJSON (C a_acQC b_acQD)

Note: if you want to derive a type class with a default implementation with Generic class, you need to write derivings generic_instances.

Hope this can save you some work.