MyNixOS website logo
Description

Help Haskellers derive class instances for composited data types.

This package will make it easier to derive class instance for complex composited data types by using Template Haskell.

derive-topdown

Hackage version Stackage version

This is a Haskell project which will derive type class instances from top for a composite data type

1. Standalone deriving

There are functions named deriving_, derivings, derivingss. Please see the API for their types.

{-# LANGUAGE StandaloneDeriving,
	ConstraintKinds,
	UndecidableInstances,
	GADTs,
	TemplateHaskell,
	DeriveGeneric #-}
{-# OPTIONS_GHC -ddump-splices #-}
	
import Data.Derive.TopDown
import GHC.Generics
import Data.Binary
imoprt Data.Aeson
import Data.Aeson.TH

data Gender = Male | Female
type Age = Int
data Person a = P {name :: String , age :: Int, gender :: Gender}
data Department a = D {dname :: String , 
					   head :: Person a, 
					   staff :: [Person a]}
data Company a = C {cname :: String, 
                    departments :: [Department a]}
	
derivings [''Eq, ''Ord, ''Generic] ''Company

You will get:

	derivings [''Eq, ''Ord, ''Generic] ''Company
  ======>
    deriving instance Eq Gender
    deriving instance Eq (Person a_acKV)
    deriving instance Eq a_acKU => Eq (Department a_acKU)
    deriving instance Eq a_acKT => Eq (Company a_acKT)
    deriving instance Ord Gender
    deriving instance Ord (Person a_acKV)
    deriving instance Ord a_acKU => Ord (Department a_acKU)
    deriving instance Ord a_acKT => Ord (Company a_acKT)
    deriving instance Generic Gender
    deriving instance Generic (Person a_acKV)
    deriving instance Generic (Department a_acKU)
    deriving instance Generic (Company a_acKT)

2. Empty Instances generation

For empty class instances deriving, instance_, instances, instancess are provided. We can use it in this way.

    instances [''Binary] ''Company
  ======>
    instance Binary Gender
    instance Binary (Person a_af50)
    instance Binary a_af4Z => Binary (Department a_af4Z)
    instance Binary a_af4Y => Binary (Company a_af4Y)

3. Usage with Template Haskell

For generating instances with a template Haskell function, derivingTH, derivingTHs and derivingTHss can be used:

	
   derivingTHs
      [(''ToJSON, deriveToJSON defaultOptions),
       (''FromJSON, deriveFromJSON defaultOptions)]
      ''Company
  ======>
    instance ToJSON Gender where
      toJSON
        = \ value_amQG
            -> case value_amQG of {
                 Male -> String (text-1.2.2.2:Data.Text.pack "Male")
                 Female -> String (text-1.2.2.2:Data.Text.pack "Female") }
      toEncoding
        = \ value_amQH
            -> case value_amQH of {
                 Male
                   -> Data.Aeson.Encoding.Internal.text
                        (text-1.2.2.2:Data.Text.pack "Male")
                 Female
                   -> Data.Aeson.Encoding.Internal.text
                        (text-1.2.2.2:Data.Text.pack "Female") }
    instance ToJSON a_amqg => ToJSON (Person a_amqg) where
      toJSON
        = \ value_amQy
        ...
        ...

You can use this this function with derive package. However, it seems that this package is broken with GHC >= 802.

4. Deriving the superclasses

Data.Derive.Superclass provides deriving_superclasses, strategy_deriving_superclasses and newtype_deriving_superclasses, gnds can be used to derive class instance and its superclass instances.

For example:

    data A = A
    deriving_superclasses ''Ord ''A

You wil get:

    deriving_superclasses ''Ord ''A
  ======>
    deriving instance Ord A
    deriving instance Eq A

5. Deriving with strategies in GHC 8.2

If you want to specify the strategy for deriving mechanism then strategy_deriving, strategy_derivings and strategy_derivingss can be used. The 3 strategies for deriving StockStrategy,AnyclassStrategy,NewtypeStrategy are exposed when you import TopDown. They can be written as stock, anyclass as the default grammar. For newtype, you can write it as newtype_ since there is a clison with newtype for data declaration. Please see DerivingStrategies

NOTE: About deriving instances of Typeable

There is a bug with isInstance function when working with Typeable class. See ticket #11251. So there might be problems if you really want to derive Typeable class. However, this bug should not affect you too much here since GHC now has AutoDeriveTypeable extension, which means you should never derive Typeable manually.

NOTE: You cannot derive a type synonym.

derive-topdown will not work with -XTypeSynonymInstances language extension. The top node in the data declaration tree has to be a data or newtype.

More discussion about derive-topdown, please see ticket #10607

Metadata

Version

0.0.3.0

Platforms (77)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows