Skip to content

Commit

Permalink
Add label info to representation.
Browse files Browse the repository at this point in the history
* Intro LblDescr
* Change ConDescr to remove labels
* Change ConType to remove labels
* Change Read, Show to support labels
* Derive TTree example by hand for testing
* Modify associated tests
* Update datatype representations
* Test
  • Loading branch information
spl committed Apr 30, 2010
1 parent 10cb0fe commit 50b7fb8
Show file tree
Hide file tree
Showing 13 changed files with 455 additions and 380 deletions.
49 changes: 38 additions & 11 deletions src/Generics/EMGM/Base.hs
@@ -1,9 +1,3 @@
{-# OPTIONS_GHC -Wall #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE OverlappingInstances #-}

-----------------------------------------------------------------------------
-- |
-- Module : Generics.EMGM.Base
Expand All @@ -25,6 +19,13 @@
-- apply) for every datatype.
-----------------------------------------------------------------------------

{-# OPTIONS_GHC -Wall #-}

{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE OverlappingInstances #-}

module Generics.EMGM.Base (

module Generics.EMGM.Representation,
Expand Down Expand Up @@ -77,6 +78,7 @@ class Generic g where
--
-- The class context represents the intersection set of supported type
-- classes.

rconstant :: (Enum a, Eq a, Ord a, Read a, Show a) => g a

-- | Case for the primitive type 'Int'. (Default implementation:
Expand All @@ -85,48 +87,68 @@ class Generic g where

-- | Case for the primitive type 'Integer'. (Default implementation:
-- 'rconstant'.)

rinteger :: g Integer

-- | Case for the primitive type 'Float'. (Default implementation:
-- 'rconstant'.)

rfloat :: g Float

-- | Case for the primitive type 'Double'. (Default implementation:
-- 'rconstant'.)

rdouble :: g Double

-- | Case for the primitive type 'Char'. (Default implementation:
-- 'rconstant'.)

rchar :: g Char

-- | Case for the structural representation type 'Unit'. It is used to
-- represent a constructor with no arguments. (Default implementation:
-- 'rconstant'.)

runit :: g Unit

-- | Case for the structural representation type @:+:@ (sum). It
-- is used to represent alternative choices between constructors. (No
-- default implementation.)

rsum :: g a -> g b -> g (a :+: b)

-- | Case for the structural representation type @:*:@ (product).
-- It is used to represent multiple arguments to a constructor. (No
-- default implementation.)

rprod :: g a -> g b -> g (a :*: b)

-- | Case for constructors. While not necessary for every generic function,
-- this method is required for 'Read' and 'Show'. It is used to hold the
-- meta-information about a constructor ('ConDescr'), e.g. name, arity,
-- fixity, etc. (Since most generic functions do not use 'rcon' and simply pass
-- the value through, the default implementation is @const id@.)
-- | Case for constructors. It is used to hold the meta-information about a
-- constructor, e.g. name, arity, fixity, etc. This is not needed for many
-- generic functions, so the default implementation is:
--
-- @
-- rcon = const id
-- @

rcon :: ConDescr -> g a -> g a

-- | Case for labeled field. Contains the label string. This is not needed for
-- many generic functions, so the default implementation is:
--
-- @
-- rlabel = const id
-- @

rlabel :: LblDescr -> g a -> g a

-- | Case for datatypes. This method is used to define the structural
-- representation of an arbitrary Haskell datatype. The first argument is the
-- embedding-projection pair, necessary for establishing the isomorphism
-- between datatype and representation. The second argument is the
-- run-time representation using the methods of 'Generic'. (No default
-- implementation.)

rtype :: EP b a -> g a -> g b

rint = rconstant
Expand All @@ -137,6 +159,7 @@ class Generic g where
runit = rconstant

rcon = const id
rlabel = const id

infixr 5 `rsum`
infixr 6 `rprod`
Expand All @@ -160,6 +183,7 @@ class Generic2 g where
rsum2 :: g a1 a2 -> g b1 b2 -> g (a1 :+: b1) (a2 :+: b2)
rprod2 :: g a1 a2 -> g b1 b2 -> g (a1 :*: b1) (a2 :*: b2)
rcon2 :: ConDescr -> g a1 a2 -> g a1 a2
rlabel2 :: LblDescr -> g a1 a2 -> g a1 a2

-- | See 'rtype'. This case is the primary difference that separates
-- 'Generic2' from 'Generic'. Since we have two generic type parameters, we
Expand All @@ -175,6 +199,7 @@ class Generic2 g where
runit2 = rconstant2

rcon2 = const id
rlabel2 = const id

infixr 5 `rsum2`
infixr 6 `rprod2`
Expand All @@ -200,6 +225,7 @@ class Generic3 g where
rsum3 :: g a1 a2 a3 -> g b1 b2 b3 -> g (a1 :+: b1) (a2 :+: b2) (a3 :+: b3)
rprod3 :: g a1 a2 a3 -> g b1 b2 b3 -> g (a1 :*: b1) (a2 :*: b2) (a3 :*: b3)
rcon3 :: ConDescr -> g a1 a2 a3 -> g a1 a2 a3
rlabel3 :: LblDescr -> g a1 a2 a3 -> g a1 a2 a3

-- | See 'rtype'. This case is the primary difference that separates
-- 'Generic3' from 'Generic'. Since we have three generic type parameters, we
Expand All @@ -215,6 +241,7 @@ class Generic3 g where
runit3 = rconstant3

rcon3 = const id
rlabel3 = const id

infixr 5 `rsum3`
infixr 6 `rprod3`
Expand Down
22 changes: 12 additions & 10 deletions src/Generics/EMGM/Data/Bool.hs
@@ -1,11 +1,3 @@
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverlappingInstances #-}
{-# OPTIONS -fno-warn-orphans #-}
{- OPTIONS -ddump-splices -}

-----------------------------------------------------------------------------
-- |
-- Module : Generics.EMGM.Data.Bool
Expand All @@ -19,6 +11,16 @@
-- Summary: Generic representation and instances for 'Bool'.
-----------------------------------------------------------------------------

{-# OPTIONS_GHC -Wall #-}

{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverlappingInstances #-}
{-# OPTIONS -fno-warn-orphans #-}
{- OPTIONS -ddump-splices -}

module Generics.EMGM.Data.Bool (
epBool,
conFalse,
Expand Down Expand Up @@ -56,11 +58,11 @@ epBool = EP fromBool toBool

-- | Constructor description for 'False'.
conFalse :: ConDescr
conFalse = ConDescr "False" 0 [] Nonfix
conFalse = ConDescr "False" 0 False Prefix

-- | Constructor description for 'True'.
conTrue :: ConDescr
conTrue = ConDescr "True" 0 [] Nonfix
conTrue = ConDescr "True" 0 False Prefix

-- | Representation of 'Bool' for 'rep'.
repBool :: (Generic g) => g Bool
Expand Down
22 changes: 12 additions & 10 deletions src/Generics/EMGM/Data/Either.hs
@@ -1,11 +1,3 @@
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverlappingInstances #-}
{-# OPTIONS -fno-warn-orphans #-}
{- OPTIONS -ddump-splices -}

-----------------------------------------------------------------------------
-- |
-- Module : Generics.EMGM.Data.Either
Expand All @@ -19,6 +11,16 @@
-- Summary: Generic representation and instances for 'Either'.
-----------------------------------------------------------------------------

{-# OPTIONS_GHC -Wall #-}

{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverlappingInstances #-}
{-# OPTIONS -fno-warn-orphans #-}
{- OPTIONS -ddump-splices -}

module Generics.EMGM.Data.Either (
epEither,
conLeft,
Expand Down Expand Up @@ -56,11 +58,11 @@ epEither = EP fromEither toEither

-- | Constructor description for 'Left'.
conLeft :: ConDescr
conLeft = ConDescr "Left" 1 [] Nonfix
conLeft = ConDescr "Left" 1 False Prefix

-- | Constructor description for 'Right'.
conRight :: ConDescr
conRight = ConDescr "Right" 1 [] Nonfix
conRight = ConDescr "Right" 1 False Prefix

-- | Representation of 'Either' for 'frep'.
frepEither :: (Generic g) => g a -> g b -> g (Either a b)
Expand Down
20 changes: 11 additions & 9 deletions src/Generics/EMGM/Data/List.hs
@@ -1,10 +1,3 @@
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverlappingInstances #-}
{-# OPTIONS -fno-warn-orphans #-}

-----------------------------------------------------------------------------
-- |
-- Module : Generics.EMGM.Data.List
Expand All @@ -18,6 +11,15 @@
-- Summary: Generic representation and instances for lists.
-----------------------------------------------------------------------------

{-# OPTIONS_GHC -Wall #-}

{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverlappingInstances #-}
{-# OPTIONS -fno-warn-orphans #-}

module Generics.EMGM.Data.List (
epList,
conNil,
Expand Down Expand Up @@ -55,11 +57,11 @@ epList = EP fromList toList

-- | Constructor description for ''nil'': @[]@.
conNil :: ConDescr
conNil = ConDescr "[]" 0 [] Nonfix
conNil = ConDescr "[]" 0 False Prefix

-- | Constructor description for ''cons'': @(:)@.
conCons :: ConDescr
conCons = ConDescr ":" 2 [] (Infixr 5)
conCons = ConDescr ":" 2 False (Infix RightAssoc 5)

-- | Representation of lists for 'frep'.
frepList :: (Generic g) => g a -> g [a]
Expand Down
22 changes: 12 additions & 10 deletions src/Generics/EMGM/Data/Maybe.hs
@@ -1,11 +1,3 @@
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverlappingInstances #-}
{-# OPTIONS -fno-warn-orphans #-}
{- OPTIONS -ddump-splices -}

-----------------------------------------------------------------------------
-- |
-- Module : Generics.EMGM.Data.Maybe
Expand All @@ -19,6 +11,16 @@
-- Summary: Generic representation and instances for 'Maybe'.
-----------------------------------------------------------------------------

{-# OPTIONS_GHC -Wall #-}

{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverlappingInstances #-}
{-# OPTIONS -fno-warn-orphans #-}
{- OPTIONS -ddump-splices -}

module Generics.EMGM.Data.Maybe (
epMaybe,
conNothing,
Expand Down Expand Up @@ -56,11 +58,11 @@ epMaybe = EP fromMaybe toMaybe

-- | Constructor description for 'Nothing'.
conNothing :: ConDescr
conNothing = ConDescr "Nothing" 0 [] Nonfix
conNothing = ConDescr "Nothing" 0 False Prefix

-- | Constructor description for 'Just'.
conJust :: ConDescr
conJust = ConDescr "Just" 1 [] Nonfix
conJust = ConDescr "Just" 1 False Prefix

-- | Representation of 'Maybe' for 'frep'.
frepMaybe :: (Generic g) => g a -> g (Maybe a)
Expand Down

0 comments on commit 50b7fb8

Please sign in to comment.