-- Hoogle documentation, generated by Haddock
-- See Hoogle, http://www.haskell.org/hoogle/
-- | Generic programming library for regular datatypes.
--
-- This package provides generic functionality for regular datatypes.
-- Regular datatypes are recursive datatypes such as lists, binary trees,
-- etc. This library cannot be used with mutually recursive datatypes or
-- with nested datatypes. The multirec library [1] can deal with mutually
-- recursive datatypes.
--
-- This library has been described in the paper:
--
--
-- - A Lightweight Approach to Datatype-Generic Rewriting.
-- Thomas van Noort, Alexey Rodriguez, Stefan Holdermans, Johan Jeuring,
-- Bastiaan Heeren. ACM SIGPLAN Workshop on Generic Programming
-- 2008.
--
--
-- More information about this library can be found at
-- http://www.cs.uu.nl/wiki/GenericProgramming/Regular.
--
-- [1] http://hackage.haskell.org/package/multirec
@package regular
@version 0.3.1
-- | Summary: Representation for record selectors.
module Generics.Regular.Selector
class Selector s
selName :: Selector s => t s (f :: * -> *) r -> String
-- | Summary: Representation for constructors.
module Generics.Regular.Constructor
-- | Class for datatypes that represent data constructors. For non-symbolic
-- constructors, only conName has to be defined. The weird
-- argument is supposed to be instantiated with C from base,
-- hence the complex kind.
class Constructor c
conName :: Constructor c => t c (f :: * -> *) r -> String
conFixity :: Constructor c => t c (f :: * -> *) r -> Fixity
conIsRecord :: Constructor c => t c (f :: * -> *) r -> Bool
-- | Datatype to represent the fixity of a constructor. An infix
-- declaration directly corresponds to an application of Infix.
data Fixity
Prefix :: Fixity
Infix :: Associativity -> Int -> Fixity
data Associativity
LeftAssociative :: Associativity
RightAssociative :: Associativity
NotAssociative :: Associativity
instance Eq Associativity
instance Show Associativity
instance Ord Associativity
instance Read Associativity
instance Eq Fixity
instance Show Fixity
instance Ord Fixity
instance Read Fixity
-- | Summary: Types for structural representation.
module Generics.Regular.Base
-- | Structure type for constant values.
newtype K a r
K :: a -> K a r
unK :: K a r -> a
-- | Structure type for recursive values.
newtype I r
I :: r -> I r
unI :: I r -> r
-- | Structure type for empty constructors.
data U r
U :: U r
-- | Structure type for alternatives in a type.
data (:+:) f g r
L :: (f r) -> :+: f g r
R :: (g r) -> :+: f g r
-- | Structure type for fields of a constructor.
data (:*:) f g r
(:*:) :: f r -> g r -> :*: f g r
-- | Structure type to store the name of a constructor.
data C c f r
C :: f r -> C c f r
unC :: C c f r -> f r
-- | Structure type to store the name of a record selector.
data S l f r
S :: f r -> S l f r
unS :: S l f r -> f r
-- | Class for datatypes that represent data constructors. For non-symbolic
-- constructors, only conName has to be defined. The weird
-- argument is supposed to be instantiated with C from base,
-- hence the complex kind.
class Constructor c
conName :: Constructor c => t c (f :: * -> *) r -> String
conFixity :: Constructor c => t c (f :: * -> *) r -> Fixity
conIsRecord :: Constructor c => t c (f :: * -> *) r -> Bool
-- | Datatype to represent the fixity of a constructor. An infix
-- declaration directly corresponds to an application of Infix.
data Fixity
Prefix :: Fixity
Infix :: Associativity -> Int -> Fixity
data Associativity
LeftAssociative :: Associativity
RightAssociative :: Associativity
NotAssociative :: Associativity
class Selector s
selName :: Selector s => t s (f :: * -> *) r -> String
-- | The well-known fixed-point type.
newtype Fix f
In :: f (Fix f) -> Fix f
out :: Fix f -> f (Fix f)
-- | The type class Regular captures the structural representation
-- of a type and the corresponding embedding-projection pairs.
--
-- To be able to use the generic functions, the user is required to
-- provide an instance of this type class.
class Regular a
from :: Regular a => a -> PF a a
to :: Regular a => PF a a -> a
-- | The type family PF represents the pattern functor of a
-- datatype.
--
-- To be able to use the generic functions, the user is required to
-- provide an instance of this type family.
instance Functor f => Functor (S c f)
instance Functor f => Functor (C c f)
instance (Functor f, Functor g) => Functor (f :*: g)
instance (Functor f, Functor g) => Functor (f :+: g)
instance Functor U
instance Functor (K a)
instance Functor I
-- | This module contains Template Haskell code that can be used to
-- automatically generate the boilerplate code for the regular library.
module Generics.Regular.TH
-- | Given the type and the name (as string) for the pattern functor to
-- derive, generate the Constructor' instances, the Selector' instances
-- and the Regular instance.
deriveAll :: Name -> String -> Q [Dec]
-- | Given a datatype name, derive datatypes and instances of class
-- Constructor.
deriveConstructors :: Name -> Q [Dec]
-- | Given a datatype name, derive datatypes and instances of class
-- Selector.
deriveSelectors :: Name -> Q [Dec]
-- | Given the type and the name (as string) for the pattern functor to
-- derive, generate the Regular instance.
deriveRegular :: Name -> String -> Q [Dec]
-- | Derive only the PF instance. Not needed if deriveRegular
-- is used.
derivePF :: String -> Name -> Q [Dec]
instance Lift Associativity
instance Lift Fixity
-- | Summary: Return the name of all the constructors of a type.
module Generics.Regular.Functions.ConNames
class ConNames f
hconNames :: ConNames f => f a -> [String]
-- | Return the name of all the constructors of the type of the given term.
conNames :: (Regular a, ConNames (PF a)) => a -> [String]
instance ConNames (K a)
instance ConNames U
instance ConNames I
instance (ConNames f, ConNames g) => ConNames (f :*: g)
instance (ConNames f, Constructor c) => ConNames (C c f)
instance (ConNames f, ConNames g) => ConNames (f :+: g)
-- | Summary: Generic crush.
module Generics.Regular.Functions.Crush
-- | The Crush class defines a right-associative crush on
-- functorial values.
class Crush f
crush :: Crush f => Assoc -> (a -> b -> b) -> b -> f a -> b
-- | Associativity of the binary operator used for crush
data Assoc
-- | Left-associative
AssocLeft :: Assoc
-- | Right-associative
AssocRight :: Assoc
-- | Flatten a structure by collecting all the elements present.
flattenl :: Crush f => f a -> [a]
flattenr :: Crush f => f a -> [a]
crushr :: Crush f => (a -> b -> b) -> b -> f a -> b
crushl :: Crush f => (a -> b -> b) -> b -> f a -> b
instance Crush f => Crush (S s f)
instance Crush f => Crush (C c f)
instance (Crush f, Crush g) => Crush (f :*: g)
instance (Crush f, Crush g) => Crush (f :+: g)
instance Crush U
instance Crush (K a)
instance Crush I
-- | Summary: Generic folding and unfolding.
module Generics.Regular.Functions.Fold
type Algebra a r = Alg (PF a) r
-- | The class fold explains how to convert an algebra Alg into a
-- function from functor to result.
class Fold f :: (* -> *)
alg :: Fold f => Alg f r -> f r -> r
-- | Fold with convenient algebras.
fold :: (Regular a, Fold (PF a), Functor (PF a)) => Algebra a r -> a -> r
type CoAlgebra a s = s -> CoAlg (PF a) s
-- | The class unfold explains how to convert a coalgebra CoAlg and
-- a seed into a representation.
class Unfold f :: (* -> *)
coalg :: Unfold f => (s -> a) -> CoAlg f s -> f a
unfold :: (Unfold (PF a), Regular a) => CoAlgebra a s -> s -> a
-- | For constructing algebras it is helpful to use this pairing
-- combinator.
(&) :: a -> b -> (a, b)
instance Unfold f => Unfold (S s f)
instance Unfold f => Unfold (C c f)
instance (Unfold f, Unfold g) => Unfold (f :*: g)
instance (Unfold f, Unfold g) => Unfold (f :+: g)
instance Unfold U
instance Unfold I
instance Unfold (K a)
instance Fold f => Fold (S s f)
instance Fold f => Fold (C c f)
instance Fold g => Fold (I :*: g)
instance Fold g => Fold (K a :*: g)
instance (Fold f, Fold g) => Fold (f :+: g)
instance Fold I
instance Fold U
instance Fold (K a)
-- | Summary: Monadic generic map.
module Generics.Regular.Functions.GMap
-- | The Functor class is used for types that can be mapped over.
-- Instances of Functor should satisfy the following laws:
--
--
-- fmap id == id
-- fmap (f . g) == fmap f . fmap g
--
--
-- The instances of Functor for lists, Data.Maybe.Maybe
-- and System.IO.IO satisfy these laws.
class Functor f :: (* -> *)
fmap :: Functor f => (a -> b) -> f a -> f b
-- | The GMap class defines a monadic functorial map.
class GMap f
fmapM :: (GMap f, Monad m) => (a -> m b) -> f a -> m (f b)
instance GMap f => GMap (S s f)
instance GMap f => GMap (C c f)
instance (GMap f, GMap g) => GMap (f :*: g)
instance (GMap f, GMap g) => GMap (f :+: g)
instance GMap U
instance GMap (K a)
instance GMap I
-- | Summary: Generic functionality for regular dataypes: mapM, flatten,
-- zip, equality, show, value generation and fold.
module Generics.Regular.Functions.LR
-- | The LRBase class defines two functions, leftb and
-- rightb, which should produce different values.
class LRBase a
leftb :: LRBase a => a
rightb :: LRBase a => a
-- | The LR class defines two functions, leftf and
-- rightf, which should produce different functorial values.
class LR f
leftf :: LR f => a -> f a
rightf :: LR f => a -> f a
-- | Produces a value which should be different from the value returned by
-- right.
left :: (Regular a, LR (PF a)) => a
-- | Produces a value which should be different from the value returned by
-- left.
right :: (Regular a, LR (PF a)) => a
instance LR f => LR (S s f)
instance LR f => LR (C c f)
instance (LR f, LR g) => LR (f :*: g)
instance (LR f, LR g) => LR (f :+: g)
instance LR U
instance LRBase a => LR (K a)
instance LR I
instance LRBase a => LRBase [a]
instance LRBase Char
instance LRBase Integer
instance LRBase Int
-- | Summary: Generic zip.
module Generics.Regular.Functions.Zip
-- | The Zip class defines a monadic zip on functorial values.
class Zip f
fzipM :: (Zip f, Monad m) => (a -> b -> m c) -> f a -> f b -> m (f c)
-- | Functorial zip with a non-monadic function, resulting in a monadic
-- value.
fzip :: (Zip f, Monad m) => (a -> b -> c) -> f a -> f b -> m (f c)
-- | Partial functorial zip with a non-monadic function.
fzip' :: Zip f => (a -> b -> c) -> f a -> f b -> f c
instance Zip f => Zip (S s f)
instance Zip f => Zip (C c f)
instance (Zip f, Zip g) => Zip (f :*: g)
instance (Zip f, Zip g) => Zip (f :+: g)
instance Zip U
instance Eq a => Zip (K a)
instance Zip I
-- | Summary: All of the generic functionality for regular dataypes: mapM,
-- flatten, zip, equality, value generation, fold and unfold. Generic
-- show (Generics.Regular.Functions.Show), generic read
-- (Generics.Regular.Functions.Read) and generic equality
-- (Generics.Regular.Functions.Eq) are not exported to prevent
-- clashes with Prelude.
module Generics.Regular.Functions
-- | Summary: Generic equality.
module Generics.Regular.Functions.Eq
class Eq f
eqf :: Eq f => (a -> a -> Bool) -> f a -> f a -> Bool
eq :: (Regular a, Eq (PF a)) => a -> a -> Bool
instance Eq f => Eq (C c f)
instance (Eq f, Eq g) => Eq (f :*: g)
instance (Eq f, Eq g) => Eq (f :+: g)
instance Eq U
instance Eq a => Eq (K a)
instance Eq I
-- | Summary: Generic read. This module is not exported by
-- Generics.Regular.Functions to avoid clashes with
-- Prelude.
module Generics.Regular.Functions.Read
class Read f
hreader :: Read f => ReadPrec a -> Bool -> ReadPrec (f a)
read :: (Regular a, Read (PF a)) => String -> a
readPrec :: (Regular a, Read (PF a)) => ReadPrec a
readsPrec :: (Regular a, Read (PF a)) => Int -> ReadS a
instance (Selector s, Read f) => Read (S s f)
instance (Constructor c, CountAtoms f, CountAtoms g, Read f, Read g) => Read (C c (f :*: g))
instance (Constructor c, Read (S s f)) => Read (C c (S s f))
instance (Constructor c, Read (K a)) => Read (C c (K a))
instance (Constructor c, Read I) => Read (C c I)
instance Constructor c => Read (C c U)
instance (Read f, Read g) => Read (f :*: g)
instance (Read f, Read g) => Read (f :+: g)
instance Read I
instance Read a => Read (K a)
instance Read U
instance CountAtoms f => CountAtoms (S s f)
instance (CountAtoms f, CountAtoms g) => CountAtoms (f :*: g)
instance CountAtoms I
instance CountAtoms (K a)
-- | Summary: Generic show. This module is not exported by
-- Generics.Regular.Functions to avoid clashes with
-- Prelude.
module Generics.Regular.Functions.Show
-- | The Show class defines a show on values.
class Show f
hshowsPrec :: Show f => (Int -> a -> ShowS) -> Bool -> Int -> f a -> ShowS
show :: (Regular a, Show (PF a)) => a -> String
shows :: (Regular a, Show (PF a)) => a -> ShowS
instance (Selector s, Show f) => Show (S s f)
instance (Constructor c, Show f) => Show (C c f)
instance (Show f, Show g) => Show (f :*: g)
instance (Show f, Show g) => Show (f :+: g)
instance Show U
instance Show a => Show (K a)
instance Show I
-- | Summary: Top-level module for this library. By importing this module,
-- the user is able to use all the generic functionality. The user is
-- only required to provide an instance of Regular for the
-- datatype.
--
-- Consider a datatype representing logical expressions:
--
--
-- data Logic = Var String
-- | Logic :->: Logic -- implication
-- | Logic :<->: Logic -- equivalence
-- | Logic :&&: Logic -- and (conjunction)
-- | Logic :||: Logic -- or (disjunction)
-- | Not Logic -- not
-- | T -- true
-- | F -- false
--
--
-- First we import the relevant modules:
--
--
-- import Generics.Regular
-- import Generics.Regular.Functions
-- import qualified Generics.Regular.Functions.Show as G
-- import qualified Generics.Regular.Functions.Read as G
--
--
-- An instance of Regular can be derived automatically with TH
-- by invoking:
--
--
-- $(deriveAll ''Logic "PFLogic")
-- type instance PF Logic = PFLogic
--
--
-- We define some logic expressions:
--
--
-- l1, l2, l3 :: Logic
-- l1 = Var "p"
-- l2 = Not l1
-- l3 = l1 :->: l2
--
--
-- And now we can use all of the generic functions. Flattening:
--
--
-- ex0 :: [Logic]
-- ex0 = flattenr (from l3)
--
-- > [Var "p",Not (Var "p")]
--
--
-- Generic equality:
--
--
-- ex1, ex2 :: Bool
-- ex1 = eq l3 l3
--
-- > True
--
--
-- ex2 = eq l3 l2
--
-- > False
--
--
-- Generic show:
--
--
-- ex3 :: String
-- ex3 = G.show l3
--
-- > "((:->:) (Var \"p\") (Not (Var \"p\")))"
--
--
-- Generic read:
--
--
-- ex4 :: Logic
-- ex4 = G.read ex3
--
-- > Var "p" :->: Not (Var "p")
--
--
-- Value generation:
--
--
-- ex5, ex6 :: Logic
-- ex5 = left
--
-- > Var ""
--
--
-- ex6 = right
--
-- > F
--
--
-- Folding:
--
--
-- ex7 :: Bool
-- ex7 = fold (alg (\_ -> False)) l3 where
-- alg env = (env & impl & (==) & (&&) & (||) & not & True & False)
-- impl p q = not p || q
--
-- > True
--
--
-- Unfolding:
--
--
-- ex8 :: Logic
-- ex8 = unfold alg 8 where
-- alg :: CoAlgebra Logic Int
-- alg n | odd n || n <= 0 = Left ""
-- | even n = Right (Left (n-1,n-2))
--
-- > Var "" :->: (Var "" :->: (Var "" :->: (Var "" :->: Var "")))
--
--
-- Constructor names:
--
--
-- ex9 = conNames (undefined :: Logic)
--
-- > ["Var",":->:",":<->:",":&&:",":||:","Not","T","F"]
--
--
-- Deep seq:
--
--
-- ex10 = gdseq (Not (T :->: (error "deep seq works"))) ()
--
-- > *** Exception: deep seq works
--
module Generics.Regular