MyNixOS website logo
Description

Derive a model of a data type using Generics.

Join the chat at https://gitter.im/Quid2/Lobby Build Status Hackage version Stackage Nightly Stackage LTS

With model you can easily derive models of Haskell data types.

Let's see some code.

We need a couple of GHC extensions:

{-# LANGUAGE DeriveGeneric, DeriveAnyClass #-}

Import the library:

import Data.Model

To derive a model of a data type we need to make it an instance of the Generic and Model classes.

For data types without parameters, we can do it directly in the deriving clause of the definition:

data Direction = North | South | Center | East | West deriving (Show,Generic,Model)

For data types with parameters we currently need a separate instance declaration for Model:

data Couple a b = Couple a b deriving (Show,Generic)
instance (Model a,Model b) => Model (Couple a b)

Instances for a few common types (Bool,Maybe,Either..) are already predefined.

We use typeModel to get the model for the given type plus its full environment, that's to say the models of all the data types referred to, directly or indirectly by the data type.

We pass the type using a Proxy.

typeModel (Proxy:: Proxy (Couple Direction Bool))
-> TypeModel
->   { typeName =
->       TypeApp
->         (TypeApp
->            (TypeCon
->               QualName
->                 { pkgName = "main" , mdlName = "Main" , locName = "Couple" })
->            (TypeCon
->               QualName
->                 { pkgName = "main" , mdlName = "Main" , locName = "Direction" }))
->         (TypeCon
->            QualName
->              { pkgName = "ghc-prim"
->              , mdlName = "GHC.Types"
->              , locName = "Bool"
->              })
->   , typeEnv =
->       fromList
->         [ ( QualName
->               { pkgName = "ghc-prim" , mdlName = "GHC.Types" , locName = "Bool" }
->           , ADT
->               { declName = "Bool"
->               , declNumParameters = 0
->               , declCons =
->                   Just
->                     (ConTree
->                        Con { constrName = "False" , constrFields = Left [] }
->                        Con { constrName = "True" , constrFields = Left [] })
->               }
->           )
->         , ( QualName
->               { pkgName = "main" , mdlName = "Main" , locName = "Couple" }
->           , ADT
->               { declName = "Couple"
->               , declNumParameters = 2
->               , declCons =
->                   Just
->                     Con
->                       { constrName = "Couple"
->                       , constrFields = Left [ TypeCon (TypVar 0) , TypeCon (TypVar 1) ]
->                       }
->               }
->           )
->         , ( QualName
->               { pkgName = "main" , mdlName = "Main" , locName = "Direction" }
->           , ADT
->               { declName = "Direction"
->               , declNumParameters = 0
->               , declCons =
->                   Just
->                     (ConTree
->                        (ConTree
->                           Con { constrName = "North" , constrFields = Left [] }
->                           Con { constrName = "South" , constrFields = Left [] })
->                        (ConTree
->                           Con { constrName = "Center" , constrFields = Left [] }
->                           (ConTree
->                              Con { constrName = "East" , constrFields = Left [] }
->                              Con { constrName = "West" , constrFields = Left [] })))
->               }
->           )
->         ]
->   }

That's a lot of information, let's show it in a prettier and more compact way:

pPrint $ typeModel (Proxy:: Proxy (Couple Direction Bool))
-> Type:
-> main.Main.Couple main.Main.Direction
->                  ghc-prim.GHC.Types.Bool -> Couple Direction Bool
-> Environment:
-> ghc-prim.GHC.Types.Bool -> Bool ≡   False
->        | True
-> main.Main.Couple -> Couple a b ≡   Couple a b
-> main.Main.Direction -> Direction ≡   North
->             | South
->             | Center
->             | East
->             | West

Data types with symbolic names are also supported:

instance (Model a) => Model [a]
pPrint $ typeModel (Proxy:: Proxy [Bool])
-> Type:
-> ghc-prim.GHC.Types.[] ghc-prim.GHC.Types.Bool -> [] Bool
-> Environment:
-> ghc-prim.GHC.Types.Bool -> Bool ≡   False
->        | True
-> ghc-prim.GHC.Types.[] -> [] a ≡   []
->        | : a (ghc-prim.GHC.Types.[] a)

Installation

Get the latest stable version from hackage.

Compatibility

Tested with ghc 7.10.3, 8.0.2, 8.2.2, 8.4.4 and 8.6.5.

Known Bugs and Infelicities

  • No support for variables of higher kind.

    For example, we cannot define a Model instance for Higher:

    data Higher f a = Higher (f a) deriving Generic

    as f has kind *->*:

  • Parametric data types cannot derive Model in the deriving clause and need to define an instance separately

    For example:

    data Couple a b = Couple a b Bool deriving (Generic,Model)

    won't work, we need a separate instance:

    instance (Model a,Model b) => Model (Couple a b)

  • Works incorrectly with data types with more than 9 type variables.

Metadata

Version

0.5

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • 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