-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Extensible and Modular Generics for the Masses -- -- EMGM is a general-purpose library for generic programming with type -- classes. -- -- The design is based on the idea of modeling algebraic datatypes as -- sum-of-product structures. Many datatypes can be modeled this way, and -- because they all share a common structure, we can write generic -- functions that work on this structure. -- -- The primary features of the library are: -- --
-- {-# LANGUAGE OverlappingInstances #-}
--
-- data T = ...
--
-- instance Rep (Collect T) T where
-- rep = Collect (:[])
--
--
-- (Note the requirement of overlapping instances.) This instance
-- triggers when the result type (the first T) matches some
-- value type (the second T) contained within the argument to
-- collect. See the source of this module for more examples.
newtype Collect b a
Collect :: (a -> [b]) -> Collect b a
selCollect :: Collect b a -> a -> [b]
-- | Collect values of type b from some value of type a.
-- An empty list means no values were collected. If you expected
-- otherwise, be sure that you have an instance such as Rep
-- (Collect B) B for the type B that you are
-- collecting.
--
-- collect works by searching a datatype for values that are the
-- same type as the return type specified. Here are some examples using
-- the same value with different return types:
--
-- -- ghci> let x = [Left 1, Right 'a', Left 2] :: [Either Int Char] -- ghci> collect x :: [Int] -- [1,2] -- ghci> collect x :: [Char] -- "a" -- ghci> collect x == x -- True ---- -- Note that the numerical constants have been declared Int using -- the type annotation. Since these natively have the type Num -- a => a, you may need to give explicit types. By design, there -- is no connection that can be inferred between the return type and the -- argument type. -- -- collect only works if there is an instance for the return -- type as described in the newtype Collect. collect :: (Rep (Collect b) a) => a -> [b] instance [overlap ok] Rep (Collect Char) Char instance [overlap ok] Rep (Collect Double) Double instance [overlap ok] Rep (Collect Float) Float instance [overlap ok] Rep (Collect Integer) Integer instance [overlap ok] Rep (Collect Int) Int instance [overlap ok] Generic (Collect b) -- | Summary: Generic functions that apply a transformation at every -- location of one type in a value of a possibly different type. -- -- The functions everywhere and everywhere' have exactly -- the same type, but they apply the transformation in different -- fashions. everywhere uses bottom-up application while -- everywhere' uses a top-down approach. This may make a -- difference if you have recursive datatypes or use nested pattern -- matching in the higher-order function. -- -- These functions are very similar to others with the same names in the -- "Scrap Your Boilerplate" library (syb package). The SYB -- functions use rank-2 types, while the EMGM functions use a single -- class constraint. Compare the types of the following: -- --
-- -- SYB -- everywhere :: (forall a. Data a => a -> a) -> forall a. Data a => a -> a ---- --
-- -- EMGM -- everywhere :: (Rep (Everywhere a) b) => (a -> a) -> b -> b --module Generics.EMGM.Functions.Everywhere -- | The type of a generic function that takes a function of one type, a -- value of another type, and returns a value of the value type. -- -- For datatypes to work with Everywhere, a special instance must be -- given. This instance is trivial to write. For a non-recursive type, -- the instance is the same as described for Everywhere'. For a -- recursive type T, the Rep instance looks like this: -- --
-- {-# LANGUAGE OverlappingInstances #-}
--
--
-- -- data T a = Val a | Rec (T a) ---- --
-- instance (Rep (Everywhere (T a)) (T a), Rep (Everywhere (T a)) a) => Rep (Everywhere (T a)) (T a) where -- rep = Everywhere app -- where -- app f x = -- case x of -- Val v1 -> f (Val (selEverywhere rep f v1)) -- Rec v1 -> f (Rec (selEverywhere rep f v1)) ---- -- Note the requirement of overlapping instances. -- -- This instance is triggered when the function type (the first T -- a in Rep (Everywhere (T a)) (T a)) matches some -- value type (the second T a) contained within the argument to -- everywhere. newtype Everywhere a b Everywhere :: ((a -> a) -> b -> b) -> Everywhere a b selEverywhere :: Everywhere a b -> (a -> a) -> b -> b -- | Apply a transformation a -> a to values of type a -- within the argument of type b in a bottom-up manner. Values -- that do not have type a are passed through id. -- -- everywhere works by searching the datatype b for -- values that are the same type as the function argument type -- a. Here are some examples using the datatype declared in the -- documentation for Everywhere. -- --
-- ghci> let f t = case t of { Val i -> Val (i+(1::Int)); other -> other }
-- ghci> everywhere f (Val (1::Int))
-- Val 2
-- ghci> everywhere f (Rec (Rec (Val (1::Int))))
-- Rec (Rec (Val 2))
--
--
-- -- ghci> let x = [Left 1, Right 'a', Left 2] :: [Either Int Char] -- ghci> everywhere (*(3::Int)) x -- [Left 3,Right 'a',Left 6] -- ghci> everywhere (\x -> x :: Float) x == x -- True ---- -- Note the type annotations. Since numerical constants have the type -- Num a => a, you may need to give explicit types. -- Also, the function \x -> x has type a -> a, -- but we need to give it some non-polymorphic type here. By design, -- there is no connection that can be inferred between the value type and -- the function type. -- -- everywhere only works if there is an instance for the return -- type as described in the newtype Everywhere. everywhere :: (Rep (Everywhere a) b) => (a -> a) -> b -> b -- | This type servers the same purpose as Everywhere, except that -- Rep instances are designed to be top-down instead of bottom-up. -- That means, given any type U (recursive or not), the -- Rep instance looks like this: -- --
-- {-# LANGUAGE OverlappingInstances #-}
--
--
-- -- data U = ... ---- --
-- instance Rep (Everywhere' U) U where -- rep = Everywhere' ($) ---- -- Note the requirement of overlapping instances. -- -- This instance is triggered when the function type (the first -- U in Rep (Everywhere U) U) matches some value -- type (the second U) contained within the argument to -- everywhere'. newtype Everywhere' a b Everywhere' :: ((a -> a) -> b -> b) -> Everywhere' a b selEverywhere' :: Everywhere' a b -> (a -> a) -> b -> b -- | Apply a transformation a -> a to values of type a -- within the argument of type b in a top-down manner. Values -- that do not have type a are passed through id. -- -- everywhere' is the same as everywhere with the -- exception of recursive datatypes. For example, compare the example -- used in the documentation for everywhere with the following. -- --
-- ghci> let f t = case t of { Val i -> Val (i+(1::Int)); other -> other }
-- ghci> everywhere' f (Val (1::Int))
-- Val 2
-- ghci> everywhere' f (Rec (Rec (Val (1::Int))))
-- Rec (Rec (Val 1))
--
--
-- everywhere' only works if there is an instance for the return
-- type as described in the newtype Everywhere'.
everywhere' :: (Rep (Everywhere' a) b) => (a -> a) -> b -> b
instance [overlap ok] Rep (Everywhere' Char) Char
instance [overlap ok] Rep (Everywhere' Float) Float
instance [overlap ok] Rep (Everywhere' Double) Double
instance [overlap ok] Rep (Everywhere' Integer) Integer
instance [overlap ok] Rep (Everywhere' Int) Int
instance [overlap ok] Generic (Everywhere' a)
instance [overlap ok] Rep (Everywhere Char) Char
instance [overlap ok] Rep (Everywhere Float) Float
instance [overlap ok] Rep (Everywhere Double) Double
instance [overlap ok] Rep (Everywhere Integer) Integer
instance [overlap ok] Rep (Everywhere Int) Int
instance [overlap ok] Generic (Everywhere a)
-- | Exports all modules in the Generics.EMGM.Common.* hierarchy.
module Generics.EMGM.Common
-- | Summary: Generic functions for comparing two values in different ways.
--
-- The fundamental function here is compare, a function that
-- returns the Ordering of two values (less than, equal to, or
-- greater than). It uses the same lexicographical ordering as
-- deriving Ord (e.g. left alternative of a sum is less than the
-- right alternative, the first component of a product is compared first
-- while the second is only compared if the first is equal, etc.).
--
-- All of the remaining functions are simply derived (in the most obvious
-- way) from compare. All of these functions are equivalent to
-- methods in the Eq and Ord type classes. The difference
-- with using this approach vs. deriving (Eq, Ord) is that you
-- can write ad-hoc cases for certain datatypes while most of the
-- functionality is handled generically.
module Generics.EMGM.Functions.Compare
-- | The type of a generic function that takes two values of the same type
-- and returns an Ordering.
newtype Compare a
Compare :: (a -> a -> Ordering) -> Compare a
selCompare :: Compare a -> a -> a -> Ordering
-- | Compare two values and return an Ordering (i.e. LT,
-- GT, or EQ). This is implemented exactly as if the
-- datatype was deriving Ord.
compare :: (Rep Compare a) => a -> a -> Ordering
-- | Equal to. Returns x == y.
eq :: (Rep Compare a) => a -> a -> Bool
-- | Not equal to. Returns x /= y.
neq :: (Rep Compare a) => a -> a -> Bool
-- | Less than. Returns x < y.
lt :: (Rep Compare a) => a -> a -> Bool
-- | Less than or equal to. Returns x <= y.
lteq :: (Rep Compare a) => a -> a -> Bool
-- | Greater than. Returns x > y.
gt :: (Rep Compare a) => a -> a -> Bool
-- | Greater than or equal to. Returns x >= y.
gteq :: (Rep Compare a) => a -> a -> Bool
-- | The minimum of two values.
min :: (Rep Compare a) => a -> a -> a
-- | The maximum of two values.
max :: (Rep Compare a) => a -> a -> a
instance Generic Compare
-- | Summary: Generic functions that crush a container into an iteration
-- over its elements.
--
-- Crush is a datatype-generic operation on container types. It is a
-- generalization of folds, but it is not a catamorphism. To understand
-- how crush works, one can think of it as generating a list of all
-- elements and mapping an accumulating function over each one. With this
-- image in mind, it is evident that (unlike a catamorphism) very little
-- information can be determined about the structure of the container.
--
-- The EMGM implementation of crush can not inherently know the
-- associativity of the binary operator. Consequently, associativity is
-- left as an argument, but there are variants specific to left- and
-- right-associativity for convenience.
--
-- Many standard Haskell datatypes (e.g. [], Data.Tree)
-- are designed such that a constructor with more than one argument (i.e.
-- a product structurally represented by (:*:)) has the element
-- on the left and any recursive points towards the right. Due to this,
-- the right-associative functions would typically produce the expected
-- values. See examples in the comments for flattenr and
-- firstr.
module Generics.EMGM.Functions.Crush
-- | The type of a generic function that takes an associativity and two
-- arguments of different types and returns a value of the type of the
-- second.
newtype Crush b a
Crush :: (Assoc -> a -> b -> b) -> Crush b a
selCrush :: Crush b a -> Assoc -> a -> b -> b
-- | Associativity of the binary operator used for crush
data Assoc
-- | Left-associative
AssocLeft :: Assoc
-- | Right-associative
AssocRight :: Assoc
-- | Apply a function (a -> b -> b) to each element
-- (a) of a container (f a) and an accumulator value
-- (b) to produce an accumulated result (b).
--
-- This is the most general form in which you must specify the
-- associativity. You may prefer to use crushr or crushl.
crush :: (FRep (Crush b) f) => Assoc -> (a -> b -> b) -> b -> f a -> b
-- | A left-associative variant of crush.
crushl :: (FRep (Crush b) f) => (a -> b -> b) -> b -> f a -> b
-- | A right-associative variant of crush.
crushr :: (FRep (Crush b) f) => (a -> b -> b) -> b -> f a -> b
-- | Flatten the elements of a container into a list.
--
-- This is the most general form in which you must specify the
-- associativity. You may prefer to use flattenr or
-- flattenl.
flatten :: (FRep (Crush [a]) f) => Assoc -> f a -> [a]
-- | A left-associative variant of flatten.
--
-- Note that, for a list ls :: [a], flattenl ls == reverse
-- ls.
flattenl :: (FRep (Crush [a]) f) => f a -> [a]
-- | A right-associative variant of flatten.
--
-- Note that, for a list ls :: [a], flattenr ls == ls.
flattenr :: (FRep (Crush [a]) f) => f a -> [a]
-- | Extract the first element of a container. If the container is empty,
-- return Nothing.
--
-- This is the most general form in which you must specify the
-- associativity. You may prefer to use firstr or firstl.
first :: (FRep (Crush [a]) f) => Assoc -> f a -> Maybe a
-- | A left-associative variant of first.
--
-- Note that, for a list ls :: [a], fromJust (firstl ls) ==
-- last ls.
firstl :: (FRep (Crush [a]) f) => f a -> Maybe a
-- | A right-associative variant of first.
--
-- Note that, for a list ls :: [a], fromJust (firstr ls) ==
-- head ls.
firstr :: (FRep (Crush [a]) f) => f a -> Maybe a
-- | Compute the conjunction of all elements in a container. This is a
-- generalization of the Prelude function of the same name.
and :: (FRep (Crush Bool) f) => f Bool -> Bool
-- | Compute the disjunction of all elements in a container. This is a
-- generalization of the Prelude function of the same name.
or :: (FRep (Crush Bool) f) => f Bool -> Bool
-- | Determine if any element in a container satisfies the predicate
-- p. This is a generalization of the Prelude function of the
-- same name.
any :: (FRep (Crush Bool) f) => (a -> Bool) -> f a -> Bool
-- | Determine if all elements in a container satisfy the predicate
-- p. This is a generalization the Prelude function of the same
-- name.
all :: (FRep (Crush Bool) f) => (a -> Bool) -> f a -> Bool
-- | Compute the sum of all elements in a container. This is a
-- generalization of the Prelude function of the same name.
sum :: (Num a, FRep (Crush a) f) => f a -> a
-- | Compute the product of all elements in a container. This is a
-- generalization of the Prelude function of the same name.
product :: (Num a, FRep (Crush a) f) => f a -> a
-- | Determine the minimum element of a container. If the container is
-- empty, return Nothing. This is a generalization of the Prelude
-- function of the same name.
minimum :: (Rep Compare a, FRep (Crush (Maybe a)) f) => f a -> Maybe a
-- | Determine the maximum element of a container. If the container is
-- empty, return Nothing. This is a generalization of the Prelude
-- function of the same name.
maximum :: (Rep Compare a, FRep (Crush (Maybe a)) f) => f a -> Maybe a
-- | Determine if an element is a member of a container. This is a
-- generalization of the Prelude function of the same name.
elem :: (Rep Compare a, FRep (Crush Bool) f) => a -> f a -> Bool
-- | Determine if an element is not a member of a container. This is a
-- generalization of the Prelude function of the same name.
notElem :: (Rep Compare a, FRep (Crush Bool) f) => a -> f a -> Bool
instance Generic (Crush b)
-- | Summary: Generic function that enumerates the values of a datatype.
--
-- enum generates a list of the values of a datatypes. It will
-- produce all values of all supported datatypes (with only a few
-- exceptions [1]). For datatypes that have an infinite enumeration (e.g.
-- Integer and [a]), enum produces an infinite
-- list.
--
-- A number of the techniques used to write enum came from a talk
-- by Mark Jones at the 2008 Advanced Functional Programming Summer
-- School. The authors gratefully acknowledge his contribution.
--
--
-- {-# LANGUAGE MultiParamTypeClasses #-}
-- {-# LANGUAGE FlexibleContexts #-}
-- {-# LANGUAGE FlexibleInstances #-}
-- {-# OPTIONS_GHC -fno-warn-orphans #-}
--
--
-- -- instance Rep (Map Int) Char where -- rep = Map chr ---- --
-- instance Rep (Map Float) Double where -- rep = Map realToFrac ---- --
-- instance Rep (Map Integer) Integer where -- rep = Map (+42) ---- -- There are no pre-defined instances, and a call to cast will -- not compile if no instances for the input and output type pair is -- found, so you must define instances in order to use cast. cast :: (Rep (Map a) b) => a -> b instance Generic2 Map -- | Summary: Generic functions that parse strings to produce values. -- -- The functions in this module involve generically parsing a string and -- producing a value. They rely on the return type to determine the -- structure for parsing. Often, this can be determined by the type -- checker, but you will occasionally need to give an explicit type -- signature. -- -- The underlying parser is designed to be as similar to deriving -- Read (as implemented by GHC) as possible. Refer to documentation -- in Text.Read for details. -- -- Since this library does not have access to the syntax of a -- data declaration, it relies on ConDescr for -- information. It is important that ConDescr accurately describe, -- for each constructor, the name, record labels (in same order as -- declared) if present, and fixity. -- -- See also Generics.EMGM.Functions.Show. module Generics.EMGM.Functions.Read -- | The type of a generic function that takes a constructor-type argument -- and returns a parser combinator for some type. newtype Read a Read :: (ConType -> ReadPrec a) -> Read a selRead :: Read a -> ConType -> ReadPrec a -- | Generate a ReadPrec parser combinator for the datatype -- a that handles operator precedence. This uses the library in -- Text.ParserCombinators.ReadPrec and should be similar to a -- derived implementation of Text.Read.readPrec. readPrec :: (Rep Read a) => ReadPrec a -- | Generate a ReadP parser combinator for the datatype a. -- This can be used with Text.ParserCombinators.ReadP. readP :: (Rep Read a) => Int -> ReadP a -- | Attempt to parse a value from the front of the string using the given -- precedence. readsPrec returns a list of (parsed value, -- remaining string) pairs. If parsing fails, readsPrec returns an -- empty list. readsPrec :: (Rep Read a) => Int -> ReadS a -- | A variant of readsPrec with the minimum precedence (0). reads :: (Rep Read a) => ReadS a -- | A variant of reads that returns Just value on a -- successful parse. Otherwise, read returns Nothing. Note -- that a successful parse requires the input to be completely consumed. read :: (Rep Read a) => String -> Maybe a instance [overlap ok] (Rep Read a, Rep Read b, Rep Read c, Rep Read d, Rep Read e, Rep Read f, Rep Read h) => Rep Read (a, b, c, d, e, f, h) instance [overlap ok] (Rep Read a, Rep Read b, Rep Read c, Rep Read d, Rep Read e, Rep Read f) => Rep Read (a, b, c, d, e, f) instance [overlap ok] (Rep Read a, Rep Read b, Rep Read c, Rep Read d, Rep Read e) => Rep Read (a, b, c, d, e) instance [overlap ok] (Rep Read a, Rep Read b, Rep Read c, Rep Read d) => Rep Read (a, b, c, d) instance [overlap ok] (Rep Read a, Rep Read b, Rep Read c) => Rep Read (a, b, c) instance [overlap ok] (Rep Read a, Rep Read b) => Rep Read (a, b) instance [overlap ok] Rep Read () instance [overlap ok] Rep Read String instance [overlap ok] (Rep Read a) => Rep Read [a] instance [overlap ok] Generic Read -- | Summary: Generic functions that convert values to readable strings. -- -- The functions in this module involve generically producing a string -- from a value of a supported datatype. The functions showsPrec -- and show are modeled after those in the class Show, -- and shows after the related function of the same name. -- -- The underlying unparser is designed to be as similar to deriving -- Show as possible. Refer to documentation in Text.Show for -- details. -- -- Since this library does not have access to the syntax of a -- data declaration, it relies on ConDescr for -- information. It is important that ConDescr accurately describe, -- for each constructor, the name, arity, record labels (in same order as -- declared) if present, and fixity. -- -- See also Generics.EMGM.Functions.Read. module Generics.EMGM.Functions.Show -- | The type of a generic function that takes a constructor-type argument, -- a number (precedence), and a value and returns a ShowS -- function. newtype Show a Show :: (ConType -> Int -> a -> ShowS) -> Show a selShow :: Show a -> ConType -> Int -> a -> ShowS -- | Convert a value to a readable string starting with the operator -- precedence of the enclosing context. showsPrec :: (Rep Show a) => Int -> a -> ShowS -- | A variant of showsPrec with the minimum precedence (0). shows :: (Rep Show a) => a -> ShowS -- | A variant of shows that returns a String instead of -- ShowS. show :: (Rep Show a) => a -> String instance [overlap ok] (Rep Show a, Rep Show b, Rep Show c, Rep Show d, Rep Show e, Rep Show f, Rep Show h) => Rep Show (a, b, c, d, e, f, h) instance [overlap ok] (Rep Show a, Rep Show b, Rep Show c, Rep Show d, Rep Show e, Rep Show f) => Rep Show (a, b, c, d, e, f) instance [overlap ok] (Rep Show a, Rep Show b, Rep Show c, Rep Show d, Rep Show e) => Rep Show (a, b, c, d, e) instance [overlap ok] (Rep Show a, Rep Show b, Rep Show c, Rep Show d) => Rep Show (a, b, c, d) instance [overlap ok] (Rep Show a, Rep Show b, Rep Show c) => Rep Show (a, b, c) instance [overlap ok] (Rep Show a, Rep Show b) => Rep Show (a, b) instance [overlap ok] Rep Show () instance [overlap ok] Rep Show String instance [overlap ok] (Rep Show a) => Rep Show [a] instance [overlap ok] Generic Show -- | Summary: Generic function that applies a (non-generic) function to -- every pair of corresponding elements in two structurally equivalent -- polymorphic values to produce a third (also structurally equivalent) -- value with the result of each application in every element location. -- -- zipWith is a generic version of the Prelude -- zipWith function. It works on all supported container -- datatypes of kind * -> *. -- -- The important concepts for zipWith are structural -- equivalence and corresponding elements. A regular, -- algebraic datatype can be visualized as some sort of tree representing -- its structure. For zipWith to be successful (and not return -- Nothing), its two container arguments must have exactly the -- same tree shape. If the shapes of the arguments differ, then it is -- unclear what the shape of the result is supposed to be. As a result, -- zipWith safely returns Nothing. -- -- Corresponding elements are those elements that are located in the same -- place in the tree of each argument. If you were to traverse the tree -- to get to element x in one tree, then its corresponding element y in -- the other tree should require the exact same path to reach it. -- -- See also Generics.EMGM.Functions.UnzipWith. module Generics.EMGM.Functions.ZipWith -- | The type of a generic function that takes two arguments of two -- different types and optionally returns a value of a third type. newtype ZipWith a b c ZipWith :: (a -> b -> Maybe c) -> ZipWith a b c selZipWith :: ZipWith a b c -> a -> b -> Maybe c -- | Combine two structurally equivalent containers into one by applying a -- function to every corresponding pair of elements. Returns -- Nothing if f a and f b have different shapes. zipWith :: (FRep3 ZipWith f) => (a -> b -> c) -> f a -> f b -> Maybe (f c) -- | Combine two containers into a single container with pairs of the -- original elements. See zipWith for restrictions. This is a -- generic version of the Prelude function of the same name. zip :: (FRep3 ZipWith f) => f a -> f b -> Maybe (f (a, b)) instance Generic3 ZipWith -- | Summary: Generic function that applies a (non-generic) function to -- every element in a value, splitting the element into two. The result -- are two structurally equivalent values, one with the elements from the -- first component of the splitting function and the other with the -- elements from the second component. -- -- unzipWith can be seen as the dual of the zipWith -- function. It has no Prelude counterpart. -- -- See also Generics.EMGM.Functions.ZipWith. module Generics.EMGM.Functions.UnzipWith -- | The type of a generic function that takes an argument of one type and -- returns a pair of values with two different types. newtype UnzipWith a b c UnzipWith :: (a -> (b, c)) -> UnzipWith a b c selUnzipWith :: UnzipWith a b c -> a -> (b, c) -- | Splits a container into two structurally equivalent containers by -- applying a function to every element, which splits it into two -- corresponding elements. unzipWith :: (FRep3 UnzipWith f) => (a -> (b, c)) -> f a -> (f b, f c) -- | Transforms a container of pairs into a container of first components -- and a container of second components. This is a generic version of the -- Prelude function of the same name. unzip :: (FRep3 UnzipWith f) => f (a, b) -> (f a, f b) instance Generic3 UnzipWith -- | Exports all modules in the Generics.EMGM.Functions.* hierarchy. module Generics.EMGM.Functions -- | Summary: Generic representation and instances for Bool. module Generics.EMGM.Data.Bool -- | Embedding-projection pair for Bool. epBool :: EP Bool (Unit :+: Unit) -- | Constructor description for False. conFalse :: ConDescr -- | Constructor description for True. conTrue :: ConDescr -- | Representation of Bool for rep. repBool :: (Generic g) => g Bool -- | Representation of Bool for frep. frepBool :: (Generic g) => g Bool -- | Representation of Bool for frep2. frep2Bool :: (Generic2 g) => g Bool Bool -- | Representation of Bool for frep3. frep3Bool :: (Generic3 g) => g Bool Bool Bool -- | Representation of Bool for bifrep2. bifrep2Bool :: (Generic2 g) => g Bool Bool instance [overlap ok] Rep (Everywhere' Bool) Bool instance [overlap ok] Rep (Everywhere Bool) Bool instance [overlap ok] Rep (Collect Bool) Bool instance [overlap ok] (Generic g) => Rep g Bool -- | Summary: Generic representation and instances for Either. module Generics.EMGM.Data.Either -- | Embedding-projection pair for Either. epEither :: EP (Either a b) (a :+: b) -- | Constructor description for Left. conLeft :: ConDescr -- | Constructor description for Right. conRight :: ConDescr -- | Representation of Either for rep. repEither :: (Generic g, Rep g a, Rep g b) => g (Either a b) -- | Representation of Either for frep. frepEither :: (Generic g) => g a -> g b -> g (Either a b) -- | Representation of Either for frep2. frep2Either :: (Generic2 g) => g a1 a2 -> g b1 b2 -> g (Either a1 b1) (Either a2 b2) -- | Representation of Either for frep3. frep3Either :: (Generic3 g) => g a1 a2 a3 -> g b1 b2 b3 -> g (Either a1 b1) (Either a2 b2) (Either a3 b3) -- | Representation of Either for bifrep2. bifrep2Either :: (Generic2 g) => g a1 a2 -> g b1 b2 -> g (Either a1 b1) (Either a2 b2) instance [overlap ok] Rep (Everywhere' (Either a b)) (Either a b) instance [overlap ok] (Rep (Everywhere (Either a b)) a, Rep (Everywhere (Either a b)) b) => Rep (Everywhere (Either a b)) (Either a b) instance [overlap ok] Rep (Collect (Either a b)) (Either a b) instance [overlap ok] (Generic2 g) => BiFRep2 g Either instance [overlap ok] (Generic g, Rep g a, Rep g b) => Rep g (Either a b) -- | Summary: Generic representation and instances for lists. module Generics.EMGM.Data.List -- | Embedding-projection pair for lists. epList :: EP [a] (Unit :+: (a :*: [a])) -- | Constructor description for ''nil'': []. conNil :: ConDescr -- | Constructor description for ''cons'': (:). conCons :: ConDescr -- | Representation of lists for rep. repList :: (Generic g, Rep g a) => g [a] -- | Representation of lists for frep. frepList :: (Generic g) => g a -> g [a] -- | Representation of lists for frep2. frep2List :: (Generic2 g) => g a b -> g [a] [b] -- | Representation of lists for frep3. frep3List :: (Generic3 g) => g a b c -> g [a] [b] [c] -- | Representation of lists for bifrep2. bifrep2List :: (Generic2 g) => g a b -> g [a] [b] instance [overlap ok] Rep (Everywhere' [a]) [a] instance [overlap ok] (Rep (Everywhere [a]) a) => Rep (Everywhere [a]) [a] instance [overlap ok] Rep (Collect [a]) [a] instance [overlap ok] (Generic3 g) => FRep3 g [] instance [overlap ok] (Generic2 g) => FRep2 g [] instance [overlap ok] (Generic g) => FRep g [] instance [overlap ok] (Generic g, Rep g a) => Rep g [a] -- | Summary: Generic representation and instances for Maybe. module Generics.EMGM.Data.Maybe -- | Embedding-projection pair for Maybe. epMaybe :: EP (Maybe a) (Unit :+: a) -- | Constructor description for Nothing. conNothing :: ConDescr -- | Constructor description for Just. conJust :: ConDescr -- | Representation of Maybe for rep. repMaybe :: (Generic g, Rep g a) => g (Maybe a) -- | Representation of Maybe for frep. frepMaybe :: (Generic g) => g a -> g (Maybe a) -- | Representation of Maybe for frep2. frep2Maybe :: (Generic2 g) => g a b -> g (Maybe a) (Maybe b) -- | Representation of Maybe for frep3. frep3Maybe :: (Generic3 g) => g a b c -> g (Maybe a) (Maybe b) (Maybe c) -- | Representation of Maybe for bifrep2. bifrep2Maybe :: (Generic2 g) => g a b -> g (Maybe a) (Maybe b) instance [overlap ok] Rep (Everywhere' (Maybe a)) (Maybe a) instance [overlap ok] (Rep (Everywhere (Maybe a)) a) => Rep (Everywhere (Maybe a)) (Maybe a) instance [overlap ok] Rep (Collect (Maybe a)) (Maybe a) instance [overlap ok] (Generic3 g) => FRep3 g Maybe instance [overlap ok] (Generic2 g) => FRep2 g Maybe instance [overlap ok] (Generic g) => FRep g Maybe instance [overlap ok] (Generic g, Rep g a) => Rep g (Maybe a) -- | Summary: Generic representation and instances for tuples of arity 0 -- (''unit'') and 2 to 7. module Generics.EMGM.Data.Tuple -- | Embedding-projection pair for (). epTuple0 :: EP () Unit -- | Constructor description for (). conTuple0 :: ConDescr -- | Representation of () for rep. repTuple0 :: (Generic g) => g () -- | Representation of () for frep. frepTuple0 :: (Generic g) => g () -- | Representation of () for frep2. frep2Tuple0 :: (Generic2 g) => g () () -- | Representation of () for frep3. frep3Tuple0 :: (Generic3 g) => g () () () -- | Representation of () for bifrep2. bifrep2Tuple0 :: (Generic2 g) => g () () -- | Embedding-projection pair for (,). epTuple2 :: EP (a, b) (a :*: b) -- | Constructor description for (,). conTuple2 :: ConDescr -- | Representation of (,) for rep. repTuple2 :: (Generic g, Rep g a, Rep g b) => g (a, b) -- | Representation of (,) for frep. frepTuple2 :: (Generic g) => g a -> g b -> g (a, b) -- | Representation of (,) for frep2. frep2Tuple2 :: (Generic2 g) => g a1 a2 -> g b1 b2 -> g (a1, b1) (a2, b2) -- | Representation of (,) for frep3. frep3Tuple2 :: (Generic3 g) => g a1 a2 a3 -> g b1 b2 b3 -> g (a1, b1) (a2, b2) (a3, b3) -- | Representation of (,) for bifrep2. bifrep2Tuple2 :: (Generic2 g) => g a1 a2 -> g b1 b2 -> g (a1, b1) (a2, b2) -- | Embedding-projection pair for (,,). epTuple3 :: EP (a, b, c) (a :*: (b :*: c)) -- | Constructor description for (,,). conTuple3 :: ConDescr -- | Representation of (,,) for rep. repTuple3 :: (Generic g, Rep g a, Rep g b, Rep g c) => g (a, b, c) -- | Representation of (,,) for frep. frepTuple3 :: (Generic g) => g a -> g b -> g c -> g (a, b, c) -- | Representation of (,,) for frep2. frep2Tuple3 :: (Generic2 g) => g a1 a2 -> g b1 b2 -> g c1 c2 -> g (a1, b1, c1) (a2, b2, c2) -- | Representation of (,,) for frep3. frep3Tuple3 :: (Generic3 g) => g a1 a2 a3 -> g b1 b2 b3 -> g c1 c2 c3 -> g (a1, b1, c1) (a2, b2, c2) (a3, b3, c3) -- | Representation of (,,) for bifrep2. bifrep2Tuple3 :: (Generic2 g) => g a1 a2 -> g b1 b2 -> g c1 c2 -> g (a1, b1, c1) (a2, b2, c2) -- | Embedding-projection pair for (,,,). epTuple4 :: EP (a, b, c, d) (a :*: (b :*: (c :*: d))) -- | Constructor description for (,,,). conTuple4 :: ConDescr -- | Representation of (,,,) for rep. repTuple4 :: (Generic g, Rep g a, Rep g b, Rep g c, Rep g d) => g (a, b, c, d) -- | Representation of (,,,) for frep. frepTuple4 :: (Generic g) => g a -> g b -> g c -> g d -> g (a, b, c, d) -- | Representation of (,,,) for frep2. frep2Tuple4 :: (Generic2 g) => g a1 a2 -> g b1 b2 -> g c1 c2 -> g d1 d2 -> g (a1, b1, c1, d1) (a2, b2, c2, d2) -- | Representation of (,,,) for frep3. frep3Tuple4 :: (Generic3 g) => g a1 a2 a3 -> g b1 b2 b3 -> g c1 c2 c3 -> g d1 d2 d3 -> g (a1, b1, c1, d1) (a2, b2, c2, d2) (a3, b3, c3, d3) -- | Representation of (,,,) for bifrep2. bifrep2Tuple4 :: (Generic2 g) => g a1 a2 -> g b1 b2 -> g c1 c2 -> g d1 d2 -> g (a1, b1, c1, d1) (a2, b2, c2, d2) -- | Embedding-projection pair for (,,,,). epTuple5 :: EP (a, b, c, d, e) (a :*: (b :*: (c :*: (d :*: e)))) -- | Constructor description for (,,,,). conTuple5 :: ConDescr -- | Representation of (,,,,) for rep. repTuple5 :: (Generic g, Rep g a, Rep g b, Rep g c, Rep g d, Rep g e) => g (a, b, c, d, e) -- | Representation of (,,,,) for frep. frepTuple5 :: (Generic g) => g a -> g b -> g c -> g d -> g e -> g (a, b, c, d, e) -- | Representation of (,,,,) for frep2. frep2Tuple5 :: (Generic2 g) => g a1 a2 -> g b1 b2 -> g c1 c2 -> g d1 d2 -> g e1 e2 -> g (a1, b1, c1, d1, e1) (a2, b2, c2, d2, e2) -- | Representation of (,,,,) for frep3. frep3Tuple5 :: (Generic3 g) => g a1 a2 a3 -> g b1 b2 b3 -> g c1 c2 c3 -> g d1 d2 d3 -> g e1 e2 e3 -> g (a1, b1, c1, d1, e1) (a2, b2, c2, d2, e2) (a3, b3, c3, d3, e3) -- | Representation of (,,,,) for bfrep2. bifrep2Tuple5 :: (Generic2 g) => g a1 a2 -> g b1 b2 -> g c1 c2 -> g d1 d2 -> g e1 e2 -> g (a1, b1, c1, d1, e1) (a2, b2, c2, d2, e2) -- | Embedding-projection pair for (,,,,,). epTuple6 :: EP (a, b, c, d, e, f) (a :*: (b :*: (c :*: (d :*: (e :*: f))))) -- | Constructor description for (,,,,,). conTuple6 :: ConDescr -- | Representation of (,,,,,) for rep. repTuple6 :: (Generic g, Rep g a, Rep g b, Rep g c, Rep g d, Rep g e, Rep g f) => g (a, b, c, d, e, f) -- | Representation of (,,,,,) for frep. frepTuple6 :: (Generic g) => g a -> g b -> g c -> g d -> g e -> g f -> g (a, b, c, d, e, f) -- | Representation of (,,,,,) for frep2. frep2Tuple6 :: (Generic2 g) => g a1 a2 -> g b1 b2 -> g c1 c2 -> g d1 d2 -> g e1 e2 -> g f1 f2 -> g (a1, b1, c1, d1, e1, f1) (a2, b2, c2, d2, e2, f2) -- | Representation of (,,,,,) for frep3. frep3Tuple6 :: (Generic3 g) => g a1 a2 a3 -> g b1 b2 b3 -> g c1 c2 c3 -> g d1 d2 d3 -> g e1 e2 e3 -> g f1 f2 f3 -> g (a1, b1, c1, d1, e1, f1) (a2, b2, c2, d2, e2, f2) (a3, b3, c3, d3, e3, f3) -- | Representation of (,,,,,) for bifrep2. bifrep2Tuple6 :: (Generic2 g) => g a1 a2 -> g b1 b2 -> g c1 c2 -> g d1 d2 -> g e1 e2 -> g f1 f2 -> g (a1, b1, c1, d1, e1, f1) (a2, b2, c2, d2, e2, f2) -- | Embedding-projection pair for (,,,,,,). epTuple7 :: EP (a, b, c, d, e, f, h) (a :*: (b :*: (c :*: (d :*: (e :*: (f :*: h)))))) -- | Constructor description for (,,,,,,). conTuple7 :: ConDescr -- | Representation of (,,,,,,) for rep. repTuple7 :: (Generic g, Rep g a, Rep g b, Rep g c, Rep g d, Rep g e, Rep g f, Rep g h) => g (a, b, c, d, e, f, h) -- | Representation of (,,,,,,) for frep. frepTuple7 :: (Generic g) => g a -> g b -> g c -> g d -> g e -> g f -> g h -> g (a, b, c, d, e, f, h) -- | Representation of (,,,,,,) for frep2. frep2Tuple7 :: (Generic2 g) => g a1 a2 -> g b1 b2 -> g c1 c2 -> g d1 d2 -> g e1 e2 -> g f1 f2 -> g h1 h2 -> g (a1, b1, c1, d1, e1, f1, h1) (a2, b2, c2, d2, e2, f2, h2) -- | Representation of (,,,,,,) for frep3. frep3Tuple7 :: (Generic3 g) => g a1 a2 a3 -> g b1 b2 b3 -> g c1 c2 c3 -> g d1 d2 d3 -> g e1 e2 e3 -> g f1 f2 f3 -> g h1 h2 h3 -> g (a1, b1, c1, d1, e1, f1, h1) (a2, b2, c2, d2, e2, f2, h2) (a3, b3, c3, d3, e3, f3, h3) -- | Representation of (,,,,,,) for bifrep2. bifrep2Tuple7 :: (Generic2 g) => g a1 a2 -> g b1 b2 -> g c1 c2 -> g d1 d2 -> g e1 e2 -> g f1 f2 -> g h1 h2 -> g (a1, b1, c1, d1, e1, f1, h1) (a2, b2, c2, d2, e2, f2, h2) instance [overlap ok] (Rep (Everywhere (a, b, c, d, e, f, h)) a, Rep (Everywhere (a, b, c, d, e, f, h)) b, Rep (Everywhere (a, b, c, d, e, f, h)) c, Rep (Everywhere (a, b, c, d, e, f, h)) d, Rep (Everywhere (a, b, c, d, e, f, h)) e, Rep (Everywhere (a, b, c, d, e, f, h)) f, Rep (Everywhere (a, b, c, d, e, f, h)) h) => Rep (Everywhere (a, b, c, d, e, f, h)) (a, b, c, d, e, f, h) instance [overlap ok] (Rep (Everywhere (a, b, c, d, e, f)) a, Rep (Everywhere (a, b, c, d, e, f)) b, Rep (Everywhere (a, b, c, d, e, f)) c, Rep (Everywhere (a, b, c, d, e, f)) d, Rep (Everywhere (a, b, c, d, e, f)) e, Rep (Everywhere (a, b, c, d, e, f)) f) => Rep (Everywhere (a, b, c, d, e, f)) (a, b, c, d, e, f) instance [overlap ok] (Rep (Everywhere (a, b, c, d, e)) a, Rep (Everywhere (a, b, c, d, e)) b, Rep (Everywhere (a, b, c, d, e)) c, Rep (Everywhere (a, b, c, d, e)) d, Rep (Everywhere (a, b, c, d, e)) e) => Rep (Everywhere (a, b, c, d, e)) (a, b, c, d, e) instance [overlap ok] (Rep (Everywhere (a, b, c, d)) a, Rep (Everywhere (a, b, c, d)) b, Rep (Everywhere (a, b, c, d)) c, Rep (Everywhere (a, b, c, d)) d) => Rep (Everywhere (a, b, c, d)) (a, b, c, d) instance [overlap ok] (Rep (Everywhere (a, b, c)) a, Rep (Everywhere (a, b, c)) b, Rep (Everywhere (a, b, c)) c) => Rep (Everywhere (a, b, c)) (a, b, c) instance [overlap ok] (Rep (Everywhere (a, b)) a, Rep (Everywhere (a, b)) b) => Rep (Everywhere (a, b)) (a, b) instance [overlap ok] Rep (Everywhere ()) () instance [overlap ok] Rep (Everywhere' (a, b, c, d, e, f, h)) (a, b, c, d, e, f, h) instance [overlap ok] Rep (Everywhere' (a, b, c, d, e, f)) (a, b, c, d, e, f) instance [overlap ok] Rep (Everywhere' (a, b, c, d, e)) (a, b, c, d, e) instance [overlap ok] Rep (Everywhere' (a, b, c, d)) (a, b, c, d) instance [overlap ok] Rep (Everywhere' (a, b, c)) (a, b, c) instance [overlap ok] Rep (Everywhere' (a, b)) (a, b) instance [overlap ok] Rep (Everywhere' ()) () instance [overlap ok] Rep (Collect (a, b, c, d, e, f, h)) (a, b, c, d, e, f, h) instance [overlap ok] Rep (Collect (a, b, c, d, e, f)) (a, b, c, d, e, f) instance [overlap ok] Rep (Collect (a, b, c, d, e)) (a, b, c, d, e) instance [overlap ok] Rep (Collect (a, b, c, d)) (a, b, c, d) instance [overlap ok] Rep (Collect (a, b, c)) (a, b, c) instance [overlap ok] Rep (Collect (a, b)) (a, b) instance [overlap ok] Rep (Collect ()) () instance [overlap ok] (Generic2 g) => BiFRep2 g (,) instance [overlap ok] (Generic g, Rep g a, Rep g b, Rep g c, Rep g d, Rep g e, Rep g f, Rep g h) => Rep g (a, b, c, d, e, f, h) instance [overlap ok] (Generic g, Rep g a, Rep g b, Rep g c, Rep g d, Rep g e, Rep g f) => Rep g (a, b, c, d, e, f) instance [overlap ok] (Generic g, Rep g a, Rep g b, Rep g c, Rep g d, Rep g e) => Rep g (a, b, c, d, e) instance [overlap ok] (Generic g, Rep g a, Rep g b, Rep g c, Rep g d) => Rep g (a, b, c, d) instance [overlap ok] (Generic g, Rep g a, Rep g b, Rep g c) => Rep g (a, b, c) instance [overlap ok] (Generic g, Rep g a, Rep g b) => Rep g (a, b) instance [overlap ok] (Generic g) => Rep g () -- | Summary: Generic representation and instances for Template Haskell -- types. -- -- The main purpose of this module is to export the instances for the -- representation dispatcher Rep. For the rare cases in which it -- is needed, this module also exports the embedding-projection pair and -- constructor description. -- -- NOTE: The exported values are not explicitly documented, -- because there is a large number and they are all generated with -- Template Haskell expressions. For a detailed look, use the -- :browse command in GHCi. module Generics.EMGM.Data.TH -- | Summary: Functions for generating the representation for using a -- datatype with EMGM. -- -- The simplest way to get a representation for a datatype is using -- derive in a Template Haskell declaration, e.g. -- $(derive ''MyType). This generates all of the -- appropriate instances, e.g. Rep, FRep, etc., for the -- type MyType. -- -- Generating datatype support can be done in a fully automatic way using -- derive or deriveWith, or it can be done piecemeal using -- a number of other functions. For most needs, the automatic approach is -- fine. But if you find you need more control, use the manual deriving -- approach. -- -- Naming conventions: -- --
-- {-# LANGUAGE TemplateHaskell #-}
-- {-# LANGUAGE MultiParamTypeClasses #-}
-- {-# LANGUAGE FlexibleContexts #-}
-- {-# LANGUAGE FlexibleInstances #-}
-- {-# LANGUAGE OverlappingInstances #-}
-- {-# LANGUAGE UndecidableInstances #-}
--
--
-- -- module Example where -- import Generics.EMGM.Derive -- data T a = C a Int ---- --
-- $(derive ''T) ---- -- The Template Haskell derive declaration in the above example -- generates the following (annotated) code: -- --
-- -- (1) Constructor description declarations ---- --
-- conC :: ConDescr -- conC = ConDescr "C" 2 [] Nonfix ---- --
-- -- (2) Embedding-projection pair declaration ---- --
-- epT :: EP (T a) (a :*: Int) -- epT = EP fromT toT -- where fromT (C v1 v2) = v1 :*: v2 -- toT (v1 :*: v2) = C v1 v2 ---- --
-- -- (3) Representation values ---- --
-- repT :: (Generic g, Rep g a, Rep g Int) => g (T a) -- repT = rtype epT (rcon conC (rprod rep rep)) ---- --
-- frepT :: (Generic g) => g a1 -> g (T a1) -- frepT a = rtype epT (rcon conC (rprod a rint)) ---- --
-- frep2T :: (Generic2 g) => g a1 a2 -> g (T a1) (T a2) -- frep2T a = rtype2 epT epT (rcon2 conC (rprod2 a rint2)) ---- --
-- frep3T :: (Generic3 g) => g a1 a2 a3 -> g (T a1) (T a2) (T a3) -- frep3T a = rtype3 epT epT epT (rcon3 conC (rprod3 a rint3)) ---- --
-- bifrep2T :: (Generic2 g) => g a1 a2 -> g (T a1) (T a2) -- bifrep2T a = rtype2 epT epT (rcon2 conC (rprod2 a rint2)) ---- --
-- -- (4) Representation instances ---- --
-- instance (Generic g, Rep g a, Rep g Int) => Rep g (T a) where -- rep = repT ---- --
-- instance (Generic g) => FRep g T where -- frep = frepT ---- --
-- instance (Generic2 g) => FRep2 g T where -- frep2 = frep2T ---- --
-- instance (Generic3 g) => FRep3 g T where -- frep3 = frep3T ---- --
-- -- In this case, no instances for BiFRep2 is generated, because T is not -- -- a bifunctor type; however, the bifrep2T value is always generated in -- -- case T is used in a bifunctor type. ---- --
-- -- (5) Generic function-specific instances ---- --
-- instance Rep (Collect (T a)) (T a) where -- rep = Collect (\x -> [x]) ---- --
-- instance (Rep (Everywhere (T a)) a, Rep (Everywhere (T a)) Int) -- => Rep (Everywhere (T a)) (T a) where -- rep = Everywhere (\f x -> -- case x of -- C v1 v2 -> f (C (selEverywhere rep f v1) (selEverywhere rep f v2)) ---- --
-- instance Rep (Everywhere' (T a)) (T a) where -- rep = Everywhere' (\f x -> f x) ---- -- Note that all the values are top-level. This allows them to be shared -- between multiple instances. For example, if you have two mutually -- recursive functor datatypes, you may need to have each other's derived -- code in scope. derive :: Name -> Q [Dec] -- | Same as derive except that you can pass a list of name -- modifications to the deriving mechanism. -- -- Use deriveWith if: -- --
-- data U = Int :* Char
-- $(deriveWith [(":*", ChangeTo "Star")] ''U)
-- x = ... conStar ...
--
--
-- For option 2, use DefinedAs as in this example:
--
--
-- data V = (:=) { i :: Int, j :: Char }
-- $(deriveWith [(":=", DefinedAs "Equals")] ''V)
-- conEquals = ConDescr ":=" 2 [] (Infix 4)
--
--
-- Using the example for option 2 with
-- Generics.EMGM.Functions.Show will print values of V as
-- infix instead of the default record syntax.
--
-- Note that only the first pair with its first field matching the type
-- or constructor name in the Modifiers list will be used. Any
-- other matches will be ignored.
deriveWith :: Modifiers -> Name -> Q [Dec]
-- | Modify the action taken for a given name.
data Modifier
-- | Change the syntactic name (of a type or constructor) to the argument
-- in the generated EP or ConDescr value. This results in a
-- value named epX or conX if the argument is
-- "X".
ChangeTo :: String -> Modifier
-- | Use this for the name of a user-defined constructor description
-- instead of a generated one. The generated code assumes the existance
-- of conX :: ConDescr (in scope) if the argument is
-- "X".
DefinedAs :: String -> Modifier
-- | List of pairs mapping a (type or constructor) name to a modifier
-- action.
type Modifiers = [(String, Modifier)]
-- | Same as derive except that only the monomorphic Rep
-- representation value and instance are generated. This is a convenience
-- function that can be used instead of the following declarations:
--
-- -- $(declareConDescrs ''T) -- $(declareEP ''T) -- $(declareMonoRep ''T) -- $(deriveRep ''T) -- $(deriveFRep ''T) -- $(deriveCollect ''T) --deriveMono :: Name -> Q [Dec] -- | Same as deriveMono except that you can pass a list of name -- modifications to the deriving mechanism. See deriveWith for an -- example. deriveMonoWith :: Modifiers -> Name -> Q [Dec] -- | Generate declarations of ConDescr values for all constructors -- in a type. See derive for an example. declareConDescrs :: Name -> Q [Dec] -- | Same as declareConDescrs except that you can pass a list of -- name modifications to the deriving mechanism. See deriveWith -- for an example. declareConDescrsWith :: Modifiers -> Name -> Q [Dec] -- | Generate declarations of EP values for a type. See -- derive for an example. declareEP :: Name -> Q [Dec] -- | Same as declareEP except that you can pass a list of name -- modifications to the deriving mechanism. See deriveWith for an -- example. declareEPWith :: Modifiers -> Name -> Q [Dec] -- | Generate declarations of all representation values for a type. These -- functions are used in rep, frep, ..., bifrep2. declareRepValues :: Name -> Q [Dec] -- | Same as declareRepValues except that you can pass a list of -- name modifications to the deriving mechanism. See deriveWith -- for an example. declareRepValuesWith :: Modifiers -> Name -> Q [Dec] -- | Generate the declaration of a monomorphic representation value for a -- type. This is the value used for rep in an instance of -- Rep. The difference with declareRepValues is that -- declareRepValues generates generates all representation values -- (including frep, frep2, etc.). See derive for an -- example. declareMonoRep :: Name -> Q [Dec] -- | Same as declareMonoRep except that you can pass a list of name -- modifications to the deriving mechanism. See deriveWith for an -- example. declareMonoRepWith :: Modifiers -> Name -> Q [Dec] -- | Generate Rep instance declarations for a type. See -- derive for an example. deriveRep :: Name -> Q [Dec] -- | Same as deriveRep except that you can pass a list of name -- modifications to the deriving mechanism. See deriveWith for an -- example. deriveRepWith :: Modifiers -> Name -> Q [Dec] -- | Generate FRep, FRep2, and FRep3 instance -- declarations for a type. See derive for an example. deriveFRep :: Name -> Q [Dec] -- | Same as deriveFRep except that you can pass a list of name -- modifications to the deriving mechanism. See deriveWith for an -- example. deriveFRepWith :: Modifiers -> Name -> Q [Dec] -- | Generate BiFRep2 instance declarations for a type. See -- derive for an example. deriveBiFRep :: Name -> Q [Dec] -- | Same as deriveBiFRep except that you can pass a list of name -- modifications to the deriving mechanism. See deriveWith for an -- example. deriveBiFRepWith :: Modifiers -> Name -> Q [Dec] -- | Generate a Rep Collect T instance declaration -- for a type T. See derive for an example. deriveCollect :: Name -> Q [Dec] -- | Generate a Rep Everywhere T instance -- declaration for a type T. See derive for an example. deriveEverywhere :: Name -> Q [Dec] -- | Generate a Rep Everywhere' T instance -- declaration for a type T. See derive for an example. deriveEverywhere' :: Name -> Q [Dec] -- | Embedding-projection pair for Bool. epBool :: EP Bool (Unit :+: Unit) -- | Constructor description for False. conFalse :: ConDescr -- | Constructor description for True. conTrue :: ConDescr -- | Representation of Bool for rep. repBool :: (Generic g) => g Bool -- | Representation of Bool for frep. frepBool :: (Generic g) => g Bool -- | Representation of Bool for frep2. frep2Bool :: (Generic2 g) => g Bool Bool -- | Representation of Bool for frep3. frep3Bool :: (Generic3 g) => g Bool Bool Bool -- | Representation of Bool for bifrep2. bifrep2Bool :: (Generic2 g) => g Bool Bool -- | Embedding-projection pair for Either. epEither :: EP (Either a b) (a :+: b) -- | Constructor description for Left. conLeft :: ConDescr -- | Constructor description for Right. conRight :: ConDescr -- | Representation of Either for rep. repEither :: (Generic g, Rep g a, Rep g b) => g (Either a b) -- | Representation of Either for frep. frepEither :: (Generic g) => g a -> g b -> g (Either a b) -- | Representation of Either for frep2. frep2Either :: (Generic2 g) => g a1 a2 -> g b1 b2 -> g (Either a1 b1) (Either a2 b2) -- | Representation of Either for frep3. frep3Either :: (Generic3 g) => g a1 a2 a3 -> g b1 b2 b3 -> g (Either a1 b1) (Either a2 b2) (Either a3 b3) -- | Representation of Either for bifrep2. bifrep2Either :: (Generic2 g) => g a1 a2 -> g b1 b2 -> g (Either a1 b1) (Either a2 b2) -- | Embedding-projection pair for lists. epList :: EP [a] (Unit :+: (a :*: [a])) -- | Constructor description for ''nil'': []. conNil :: ConDescr -- | Constructor description for ''cons'': (:). conCons :: ConDescr -- | Representation of lists for rep. repList :: (Generic g, Rep g a) => g [a] -- | Representation of lists for frep. frepList :: (Generic g) => g a -> g [a] -- | Representation of lists for frep2. frep2List :: (Generic2 g) => g a b -> g [a] [b] -- | Representation of lists for frep3. frep3List :: (Generic3 g) => g a b c -> g [a] [b] [c] -- | Representation of lists for bifrep2. bifrep2List :: (Generic2 g) => g a b -> g [a] [b] -- | Embedding-projection pair for Maybe. epMaybe :: EP (Maybe a) (Unit :+: a) -- | Constructor description for Nothing. conNothing :: ConDescr -- | Constructor description for Just. conJust :: ConDescr -- | Representation of Maybe for rep. repMaybe :: (Generic g, Rep g a) => g (Maybe a) -- | Representation of Maybe for frep. frepMaybe :: (Generic g) => g a -> g (Maybe a) -- | Representation of Maybe for frep2. frep2Maybe :: (Generic2 g) => g a b -> g (Maybe a) (Maybe b) -- | Representation of Maybe for frep3. frep3Maybe :: (Generic3 g) => g a b c -> g (Maybe a) (Maybe b) (Maybe c) -- | Representation of Maybe for bifrep2. bifrep2Maybe :: (Generic2 g) => g a b -> g (Maybe a) (Maybe b) -- | Embedding-projection pair for (). epTuple0 :: EP () Unit -- | Constructor description for (). conTuple0 :: ConDescr -- | Representation of () for rep. repTuple0 :: (Generic g) => g () -- | Representation of () for frep. frepTuple0 :: (Generic g) => g () -- | Representation of () for frep2. frep2Tuple0 :: (Generic2 g) => g () () -- | Representation of () for frep3. frep3Tuple0 :: (Generic3 g) => g () () () -- | Representation of () for bifrep2. bifrep2Tuple0 :: (Generic2 g) => g () () -- | Embedding-projection pair for (,). epTuple2 :: EP (a, b) (a :*: b) -- | Constructor description for (,). conTuple2 :: ConDescr -- | Representation of (,) for rep. repTuple2 :: (Generic g, Rep g a, Rep g b) => g (a, b) -- | Representation of (,) for frep. frepTuple2 :: (Generic g) => g a -> g b -> g (a, b) -- | Representation of (,) for frep2. frep2Tuple2 :: (Generic2 g) => g a1 a2 -> g b1 b2 -> g (a1, b1) (a2, b2) -- | Representation of (,) for frep3. frep3Tuple2 :: (Generic3 g) => g a1 a2 a3 -> g b1 b2 b3 -> g (a1, b1) (a2, b2) (a3, b3) -- | Representation of (,) for bifrep2. bifrep2Tuple2 :: (Generic2 g) => g a1 a2 -> g b1 b2 -> g (a1, b1) (a2, b2) -- | Embedding-projection pair for (,,). epTuple3 :: EP (a, b, c) (a :*: (b :*: c)) -- | Constructor description for (,,). conTuple3 :: ConDescr -- | Representation of (,,) for rep. repTuple3 :: (Generic g, Rep g a, Rep g b, Rep g c) => g (a, b, c) -- | Representation of (,,) for frep. frepTuple3 :: (Generic g) => g a -> g b -> g c -> g (a, b, c) -- | Representation of (,,) for frep2. frep2Tuple3 :: (Generic2 g) => g a1 a2 -> g b1 b2 -> g c1 c2 -> g (a1, b1, c1) (a2, b2, c2) -- | Representation of (,,) for frep3. frep3Tuple3 :: (Generic3 g) => g a1 a2 a3 -> g b1 b2 b3 -> g c1 c2 c3 -> g (a1, b1, c1) (a2, b2, c2) (a3, b3, c3) -- | Representation of (,,) for bifrep2. bifrep2Tuple3 :: (Generic2 g) => g a1 a2 -> g b1 b2 -> g c1 c2 -> g (a1, b1, c1) (a2, b2, c2) -- | Embedding-projection pair for (,,,). epTuple4 :: EP (a, b, c, d) (a :*: (b :*: (c :*: d))) -- | Constructor description for (,,,). conTuple4 :: ConDescr -- | Representation of (,,,) for rep. repTuple4 :: (Generic g, Rep g a, Rep g b, Rep g c, Rep g d) => g (a, b, c, d) -- | Representation of (,,,) for frep. frepTuple4 :: (Generic g) => g a -> g b -> g c -> g d -> g (a, b, c, d) -- | Representation of (,,,) for frep2. frep2Tuple4 :: (Generic2 g) => g a1 a2 -> g b1 b2 -> g c1 c2 -> g d1 d2 -> g (a1, b1, c1, d1) (a2, b2, c2, d2) -- | Representation of (,,,) for frep3. frep3Tuple4 :: (Generic3 g) => g a1 a2 a3 -> g b1 b2 b3 -> g c1 c2 c3 -> g d1 d2 d3 -> g (a1, b1, c1, d1) (a2, b2, c2, d2) (a3, b3, c3, d3) -- | Representation of (,,,) for bifrep2. bifrep2Tuple4 :: (Generic2 g) => g a1 a2 -> g b1 b2 -> g c1 c2 -> g d1 d2 -> g (a1, b1, c1, d1) (a2, b2, c2, d2) -- | Embedding-projection pair for (,,,,). epTuple5 :: EP (a, b, c, d, e) (a :*: (b :*: (c :*: (d :*: e)))) -- | Constructor description for (,,,,). conTuple5 :: ConDescr -- | Representation of (,,,,) for rep. repTuple5 :: (Generic g, Rep g a, Rep g b, Rep g c, Rep g d, Rep g e) => g (a, b, c, d, e) -- | Representation of (,,,,) for frep. frepTuple5 :: (Generic g) => g a -> g b -> g c -> g d -> g e -> g (a, b, c, d, e) -- | Representation of (,,,,) for frep2. frep2Tuple5 :: (Generic2 g) => g a1 a2 -> g b1 b2 -> g c1 c2 -> g d1 d2 -> g e1 e2 -> g (a1, b1, c1, d1, e1) (a2, b2, c2, d2, e2) -- | Representation of (,,,,) for frep3. frep3Tuple5 :: (Generic3 g) => g a1 a2 a3 -> g b1 b2 b3 -> g c1 c2 c3 -> g d1 d2 d3 -> g e1 e2 e3 -> g (a1, b1, c1, d1, e1) (a2, b2, c2, d2, e2) (a3, b3, c3, d3, e3) -- | Representation of (,,,,) for bfrep2. bifrep2Tuple5 :: (Generic2 g) => g a1 a2 -> g b1 b2 -> g c1 c2 -> g d1 d2 -> g e1 e2 -> g (a1, b1, c1, d1, e1) (a2, b2, c2, d2, e2) -- | Embedding-projection pair for (,,,,,). epTuple6 :: EP (a, b, c, d, e, f) (a :*: (b :*: (c :*: (d :*: (e :*: f))))) -- | Constructor description for (,,,,,). conTuple6 :: ConDescr -- | Representation of (,,,,,) for rep. repTuple6 :: (Generic g, Rep g a, Rep g b, Rep g c, Rep g d, Rep g e, Rep g f) => g (a, b, c, d, e, f) -- | Representation of (,,,,,) for frep. frepTuple6 :: (Generic g) => g a -> g b -> g c -> g d -> g e -> g f -> g (a, b, c, d, e, f) -- | Representation of (,,,,,) for frep2. frep2Tuple6 :: (Generic2 g) => g a1 a2 -> g b1 b2 -> g c1 c2 -> g d1 d2 -> g e1 e2 -> g f1 f2 -> g (a1, b1, c1, d1, e1, f1) (a2, b2, c2, d2, e2, f2) -- | Representation of (,,,,,) for frep3. frep3Tuple6 :: (Generic3 g) => g a1 a2 a3 -> g b1 b2 b3 -> g c1 c2 c3 -> g d1 d2 d3 -> g e1 e2 e3 -> g f1 f2 f3 -> g (a1, b1, c1, d1, e1, f1) (a2, b2, c2, d2, e2, f2) (a3, b3, c3, d3, e3, f3) -- | Representation of (,,,,,) for bifrep2. bifrep2Tuple6 :: (Generic2 g) => g a1 a2 -> g b1 b2 -> g c1 c2 -> g d1 d2 -> g e1 e2 -> g f1 f2 -> g (a1, b1, c1, d1, e1, f1) (a2, b2, c2, d2, e2, f2) -- | Embedding-projection pair for (,,,,,,). epTuple7 :: EP (a, b, c, d, e, f, h) (a :*: (b :*: (c :*: (d :*: (e :*: (f :*: h)))))) -- | Constructor description for (,,,,,,). conTuple7 :: ConDescr -- | Representation of (,,,,,,) for rep. repTuple7 :: (Generic g, Rep g a, Rep g b, Rep g c, Rep g d, Rep g e, Rep g f, Rep g h) => g (a, b, c, d, e, f, h) -- | Representation of (,,,,,,) for frep. frepTuple7 :: (Generic g) => g a -> g b -> g c -> g d -> g e -> g f -> g h -> g (a, b, c, d, e, f, h) -- | Representation of (,,,,,,) for frep2. frep2Tuple7 :: (Generic2 g) => g a1 a2 -> g b1 b2 -> g c1 c2 -> g d1 d2 -> g e1 e2 -> g f1 f2 -> g h1 h2 -> g (a1, b1, c1, d1, e1, f1, h1) (a2, b2, c2, d2, e2, f2, h2) -- | Representation of (,,,,,,) for frep3. frep3Tuple7 :: (Generic3 g) => g a1 a2 a3 -> g b1 b2 b3 -> g c1 c2 c3 -> g d1 d2 d3 -> g e1 e2 e3 -> g f1 f2 f3 -> g h1 h2 h3 -> g (a1, b1, c1, d1, e1, f1, h1) (a2, b2, c2, d2, e2, f2, h2) (a3, b3, c3, d3, e3, f3, h3) -- | Representation of (,,,,,,) for bifrep2. bifrep2Tuple7 :: (Generic2 g) => g a1 a2 -> g b1 b2 -> g c1 c2 -> g d1 d2 -> g e1 e2 -> g f1 f2 -> g h1 h2 -> g (a1, b1, c1, d1, e1, f1, h1) (a2, b2, c2, d2, e2, f2, h2) -- | The type of a generic function that takes a value of one type and -- returns a list of values of another type. -- -- For datatypes to work with Collect, a special instance must be given. -- This instance is trivial to write. Given a type T, the -- Rep instance looks like this: -- --
-- {-# LANGUAGE OverlappingInstances #-}
--
-- data T = ...
--
-- instance Rep (Collect T) T where
-- rep = Collect (:[])
--
--
-- (Note the requirement of overlapping instances.) This instance
-- triggers when the result type (the first T) matches some
-- value type (the second T) contained within the argument to
-- collect. See the source of this module for more examples.
newtype Collect b a
Collect :: (a -> [b]) -> Collect b a
selCollect :: Collect b a -> a -> [b]
-- | The type of a generic function that takes a function of one type, a
-- value of another type, and returns a value of the value type.
--
-- For datatypes to work with Everywhere, a special instance must be
-- given. This instance is trivial to write. For a non-recursive type,
-- the instance is the same as described for Everywhere'. For a
-- recursive type T, the Rep instance looks like this:
--
--
-- {-# LANGUAGE OverlappingInstances #-}
--
--
-- -- data T a = Val a | Rec (T a) ---- --
-- instance (Rep (Everywhere (T a)) (T a), Rep (Everywhere (T a)) a) => Rep (Everywhere (T a)) (T a) where -- rep = Everywhere app -- where -- app f x = -- case x of -- Val v1 -> f (Val (selEverywhere rep f v1)) -- Rec v1 -> f (Rec (selEverywhere rep f v1)) ---- -- Note the requirement of overlapping instances. -- -- This instance is triggered when the function type (the first T -- a in Rep (Everywhere (T a)) (T a)) matches some -- value type (the second T a) contained within the argument to -- everywhere. newtype Everywhere a b Everywhere :: ((a -> a) -> b -> b) -> Everywhere a b selEverywhere :: Everywhere a b -> (a -> a) -> b -> b -- | This type servers the same purpose as Everywhere, except that -- Rep instances are designed to be top-down instead of bottom-up. -- That means, given any type U (recursive or not), the -- Rep instance looks like this: -- --
-- {-# LANGUAGE OverlappingInstances #-}
--
--
-- -- data U = ... ---- --
-- instance Rep (Everywhere' U) U where -- rep = Everywhere' ($) ---- -- Note the requirement of overlapping instances. -- -- This instance is triggered when the function type (the first -- U in Rep (Everywhere U) U) matches some value -- type (the second U) contained within the argument to -- everywhere'. newtype Everywhere' a b Everywhere' :: ((a -> a) -> b -> b) -> Everywhere' a b selEverywhere' :: Everywhere' a b -> (a -> a) -> b -> b -- | EMGM is "Extensible and Modular Generics for the Masses," a library -- for datatype-generic programming in Haskell. -- -- This module exports the most commonly used types, classes, and -- functions. The documentation is organized by topic for convenient -- access. -- -- For more in-depth documentation, refer to one of the modules in these -- hierarchies: -- --
-- {-# LANGUAGE OverlappingInstances #-}
--
-- data T = ...
--
-- instance Rep (Collect T) T where
-- rep = Collect (:[])
--
--
-- (Note the requirement of overlapping instances.) This instance
-- triggers when the result type (the first T) matches some
-- value type (the second T) contained within the argument to
-- collect. See the source of this module for more examples.
newtype Collect b a
Collect :: (a -> [b]) -> Collect b a
selCollect :: Collect b a -> a -> [b]
-- | Collect values of type b from some value of type a.
-- An empty list means no values were collected. If you expected
-- otherwise, be sure that you have an instance such as Rep
-- (Collect B) B for the type B that you are
-- collecting.
--
-- collect works by searching a datatype for values that are the
-- same type as the return type specified. Here are some examples using
-- the same value with different return types:
--
-- -- ghci> let x = [Left 1, Right 'a', Left 2] :: [Either Int Char] -- ghci> collect x :: [Int] -- [1,2] -- ghci> collect x :: [Char] -- "a" -- ghci> collect x == x -- True ---- -- Note that the numerical constants have been declared Int using -- the type annotation. Since these natively have the type Num -- a => a, you may need to give explicit types. By design, there -- is no connection that can be inferred between the return type and the -- argument type. -- -- collect only works if there is an instance for the return -- type as described in the newtype Collect. collect :: (Rep (Collect b) a) => a -> [b] -- | The type of a generic function that takes two values of the same type -- and returns an Ordering. newtype Compare a Compare :: (a -> a -> Ordering) -> Compare a selCompare :: Compare a -> a -> a -> Ordering -- | Compare two values and return an Ordering (i.e. LT, -- GT, or EQ). This is implemented exactly as if the -- datatype was deriving Ord. compare :: (Rep Compare a) => a -> a -> Ordering -- | Equal to. Returns x == y. eq :: (Rep Compare a) => a -> a -> Bool -- | Not equal to. Returns x /= y. neq :: (Rep Compare a) => a -> a -> Bool -- | Less than. Returns x < y. lt :: (Rep Compare a) => a -> a -> Bool -- | Less than or equal to. Returns x <= y. lteq :: (Rep Compare a) => a -> a -> Bool -- | Greater than. Returns x > y. gt :: (Rep Compare a) => a -> a -> Bool -- | Greater than or equal to. Returns x >= y. gteq :: (Rep Compare a) => a -> a -> Bool -- | The minimum of two values. min :: (Rep Compare a) => a -> a -> a -- | The maximum of two values. max :: (Rep Compare a) => a -> a -> a -- | The type of a generic function that takes an associativity and two -- arguments of different types and returns a value of the type of the -- second. newtype Crush b a Crush :: (Assoc -> a -> b -> b) -> Crush b a selCrush :: Crush b a -> Assoc -> a -> b -> b -- | Associativity of the binary operator used for crush data Assoc -- | Left-associative AssocLeft :: Assoc -- | Right-associative AssocRight :: Assoc -- | Apply a function (a -> b -> b) to each element -- (a) of a container (f a) and an accumulator value -- (b) to produce an accumulated result (b). -- -- This is the most general form in which you must specify the -- associativity. You may prefer to use crushr or crushl. crush :: (FRep (Crush b) f) => Assoc -> (a -> b -> b) -> b -> f a -> b -- | A left-associative variant of crush. crushl :: (FRep (Crush b) f) => (a -> b -> b) -> b -> f a -> b -- | A right-associative variant of crush. crushr :: (FRep (Crush b) f) => (a -> b -> b) -> b -> f a -> b -- | Flatten the elements of a container into a list. -- -- This is the most general form in which you must specify the -- associativity. You may prefer to use flattenr or -- flattenl. flatten :: (FRep (Crush [a]) f) => Assoc -> f a -> [a] -- | A left-associative variant of flatten. -- -- Note that, for a list ls :: [a], flattenl ls == reverse -- ls. flattenl :: (FRep (Crush [a]) f) => f a -> [a] -- | A right-associative variant of flatten. -- -- Note that, for a list ls :: [a], flattenr ls == ls. flattenr :: (FRep (Crush [a]) f) => f a -> [a] -- | Extract the first element of a container. If the container is empty, -- return Nothing. -- -- This is the most general form in which you must specify the -- associativity. You may prefer to use firstr or firstl. first :: (FRep (Crush [a]) f) => Assoc -> f a -> Maybe a -- | A left-associative variant of first. -- -- Note that, for a list ls :: [a], fromJust (firstl ls) == -- last ls. firstl :: (FRep (Crush [a]) f) => f a -> Maybe a -- | A right-associative variant of first. -- -- Note that, for a list ls :: [a], fromJust (firstr ls) == -- head ls. firstr :: (FRep (Crush [a]) f) => f a -> Maybe a -- | Compute the conjunction of all elements in a container. This is a -- generalization of the Prelude function of the same name. and :: (FRep (Crush Bool) f) => f Bool -> Bool -- | Compute the disjunction of all elements in a container. This is a -- generalization of the Prelude function of the same name. or :: (FRep (Crush Bool) f) => f Bool -> Bool -- | Determine if any element in a container satisfies the predicate -- p. This is a generalization of the Prelude function of the -- same name. any :: (FRep (Crush Bool) f) => (a -> Bool) -> f a -> Bool -- | Determine if all elements in a container satisfy the predicate -- p. This is a generalization the Prelude function of the same -- name. all :: (FRep (Crush Bool) f) => (a -> Bool) -> f a -> Bool -- | Compute the sum of all elements in a container. This is a -- generalization of the Prelude function of the same name. sum :: (Num a, FRep (Crush a) f) => f a -> a -- | Compute the product of all elements in a container. This is a -- generalization of the Prelude function of the same name. product :: (Num a, FRep (Crush a) f) => f a -> a -- | Determine the minimum element of a container. If the container is -- empty, return Nothing. This is a generalization of the Prelude -- function of the same name. minimum :: (Rep Compare a, FRep (Crush (Maybe a)) f) => f a -> Maybe a -- | Determine the maximum element of a container. If the container is -- empty, return Nothing. This is a generalization of the Prelude -- function of the same name. maximum :: (Rep Compare a, FRep (Crush (Maybe a)) f) => f a -> Maybe a -- | Determine if an element is a member of a container. This is a -- generalization of the Prelude function of the same name. elem :: (Rep Compare a, FRep (Crush Bool) f) => a -> f a -> Bool -- | Determine if an element is not a member of a container. This is a -- generalization of the Prelude function of the same name. notElem :: (Rep Compare a, FRep (Crush Bool) f) => a -> f a -> Bool -- | The type of a generic function that takes no arguments and returns a -- list of some type. newtype Enum a Enum :: [a] -> Enum a selEnum :: Enum a -> [a] -- | Enumerate the values of a datatype. If the number of values is -- infinite, the result will be an infinite list. The remaining functions -- are derived from enum. enum :: (Rep Enum a) => [a] -- | Enumerate the first n values of a datatype. This is a -- shortcut for genericTake n (enum). enumN :: (Integral n, Rep Enum a) => n -> [a] -- | Returns the first element of the enumeration from enum. This is -- often called the neutral or empty value. empty :: (Rep Enum a) => a -- | The type of a generic function that takes a function of one type, a -- value of another type, and returns a value of the value type. -- -- For datatypes to work with Everywhere, a special instance must be -- given. This instance is trivial to write. For a non-recursive type, -- the instance is the same as described for Everywhere'. For a -- recursive type T, the Rep instance looks like this: -- --
-- {-# LANGUAGE OverlappingInstances #-}
--
--
-- -- data T a = Val a | Rec (T a) ---- --
-- instance (Rep (Everywhere (T a)) (T a), Rep (Everywhere (T a)) a) => Rep (Everywhere (T a)) (T a) where -- rep = Everywhere app -- where -- app f x = -- case x of -- Val v1 -> f (Val (selEverywhere rep f v1)) -- Rec v1 -> f (Rec (selEverywhere rep f v1)) ---- -- Note the requirement of overlapping instances. -- -- This instance is triggered when the function type (the first T -- a in Rep (Everywhere (T a)) (T a)) matches some -- value type (the second T a) contained within the argument to -- everywhere. newtype Everywhere a b Everywhere :: ((a -> a) -> b -> b) -> Everywhere a b selEverywhere :: Everywhere a b -> (a -> a) -> b -> b -- | Apply a transformation a -> a to values of type a -- within the argument of type b in a bottom-up manner. Values -- that do not have type a are passed through id. -- -- everywhere works by searching the datatype b for -- values that are the same type as the function argument type -- a. Here are some examples using the datatype declared in the -- documentation for Everywhere. -- --
-- ghci> let f t = case t of { Val i -> Val (i+(1::Int)); other -> other }
-- ghci> everywhere f (Val (1::Int))
-- Val 2
-- ghci> everywhere f (Rec (Rec (Val (1::Int))))
-- Rec (Rec (Val 2))
--
--
-- -- ghci> let x = [Left 1, Right 'a', Left 2] :: [Either Int Char] -- ghci> everywhere (*(3::Int)) x -- [Left 3,Right 'a',Left 6] -- ghci> everywhere (\x -> x :: Float) x == x -- True ---- -- Note the type annotations. Since numerical constants have the type -- Num a => a, you may need to give explicit types. -- Also, the function \x -> x has type a -> a, -- but we need to give it some non-polymorphic type here. By design, -- there is no connection that can be inferred between the value type and -- the function type. -- -- everywhere only works if there is an instance for the return -- type as described in the newtype Everywhere. everywhere :: (Rep (Everywhere a) b) => (a -> a) -> b -> b -- | This type servers the same purpose as Everywhere, except that -- Rep instances are designed to be top-down instead of bottom-up. -- That means, given any type U (recursive or not), the -- Rep instance looks like this: -- --
-- {-# LANGUAGE OverlappingInstances #-}
--
--
-- -- data U = ... ---- --
-- instance Rep (Everywhere' U) U where -- rep = Everywhere' ($) ---- -- Note the requirement of overlapping instances. -- -- This instance is triggered when the function type (the first -- U in Rep (Everywhere U) U) matches some value -- type (the second U) contained within the argument to -- everywhere'. newtype Everywhere' a b Everywhere' :: ((a -> a) -> b -> b) -> Everywhere' a b selEverywhere' :: Everywhere' a b -> (a -> a) -> b -> b -- | Apply a transformation a -> a to values of type a -- within the argument of type b in a top-down manner. Values -- that do not have type a are passed through id. -- -- everywhere' is the same as everywhere with the -- exception of recursive datatypes. For example, compare the example -- used in the documentation for everywhere with the following. -- --
-- ghci> let f t = case t of { Val i -> Val (i+(1::Int)); other -> other }
-- ghci> everywhere' f (Val (1::Int))
-- Val 2
-- ghci> everywhere' f (Rec (Rec (Val (1::Int))))
-- Rec (Rec (Val 1))
--
--
-- everywhere' only works if there is an instance for the return
-- type as described in the newtype Everywhere'.
everywhere' :: (Rep (Everywhere' a) b) => (a -> a) -> b -> b
-- | The type of a generic function that takes a value of one type and
-- returns a value of a different type.
newtype Map a b
Map :: (a -> b) -> Map a b
selMap :: Map a b -> a -> b
-- | Apply a function to all elements of a container datatype (kind *
-- -> *).
map :: (FRep2 Map f) => (a -> b) -> f a -> f b
-- | Replace all a-values in as with b. This is
-- a convenience function for the implementation map
-- (const b) as.
replace :: (FRep2 Map f) => f a -> b -> f b
-- | Given a datatype F a b, bimap f g applies the
-- function f :: a -> c to every a-element and the
-- function g :: b -> d to every b-element. The
-- result is a value with transformed elements: F c d.
bimap :: (BiFRep2 Map f) => (a -> c) -> (b -> d) -> f a b -> f c d
-- | Cast a value of one type into a value of another. This is a
-- configurable function that allows you to define your own type-safe
-- conversions for a variety of types.
--
-- cast works with instances of Rep (Map i)
-- o in which you choose the input type i and the output
-- type o and implement the function of type i -> o.
--
-- Here are some examples of instances (and flags you will need or want):
--
--
-- {-# LANGUAGE MultiParamTypeClasses #-}
-- {-# LANGUAGE FlexibleContexts #-}
-- {-# LANGUAGE FlexibleInstances #-}
-- {-# OPTIONS_GHC -fno-warn-orphans #-}
--
--
-- -- instance Rep (Map Int) Char where -- rep = Map chr ---- --
-- instance Rep (Map Float) Double where -- rep = Map realToFrac ---- --
-- instance Rep (Map Integer) Integer where -- rep = Map (+42) ---- -- There are no pre-defined instances, and a call to cast will -- not compile if no instances for the input and output type pair is -- found, so you must define instances in order to use cast. cast :: (Rep (Map a) b) => a -> b -- | The type of a generic function that takes a constructor-type argument -- and returns a parser combinator for some type. newtype Read a Read :: (ConType -> ReadPrec a) -> Read a selRead :: Read a -> ConType -> ReadPrec a -- | Generate a ReadPrec parser combinator for the datatype -- a that handles operator precedence. This uses the library in -- Text.ParserCombinators.ReadPrec and should be similar to a -- derived implementation of Text.Read.readPrec. readPrec :: (Rep Read a) => ReadPrec a -- | Generate a ReadP parser combinator for the datatype a. -- This can be used with Text.ParserCombinators.ReadP. readP :: (Rep Read a) => Int -> ReadP a -- | Attempt to parse a value from the front of the string using the given -- precedence. readsPrec returns a list of (parsed value, -- remaining string) pairs. If parsing fails, readsPrec returns an -- empty list. readsPrec :: (Rep Read a) => Int -> ReadS a -- | A variant of readsPrec with the minimum precedence (0). reads :: (Rep Read a) => ReadS a -- | A variant of reads that returns Just value on a -- successful parse. Otherwise, read returns Nothing. Note -- that a successful parse requires the input to be completely consumed. read :: (Rep Read a) => String -> Maybe a -- | The type of a generic function that takes a constructor-type argument, -- a number (precedence), and a value and returns a ShowS -- function. newtype Show a Show :: (ConType -> Int -> a -> ShowS) -> Show a selShow :: Show a -> ConType -> Int -> a -> ShowS -- | Convert a value to a readable string starting with the operator -- precedence of the enclosing context. showsPrec :: (Rep Show a) => Int -> a -> ShowS -- | A variant of showsPrec with the minimum precedence (0). shows :: (Rep Show a) => a -> ShowS -- | A variant of shows that returns a String instead of -- ShowS. show :: (Rep Show a) => a -> String -- | The type of a generic function that takes an argument of one type and -- returns a pair of values with two different types. newtype UnzipWith a b c UnzipWith :: (a -> (b, c)) -> UnzipWith a b c selUnzipWith :: UnzipWith a b c -> a -> (b, c) -- | Transforms a container of pairs into a container of first components -- and a container of second components. This is a generic version of the -- Prelude function of the same name. unzip :: (FRep3 UnzipWith f) => f (a, b) -> (f a, f b) -- | Splits a container into two structurally equivalent containers by -- applying a function to every element, which splits it into two -- corresponding elements. unzipWith :: (FRep3 UnzipWith f) => (a -> (b, c)) -> f a -> (f b, f c) -- | The type of a generic function that takes two arguments of two -- different types and optionally returns a value of a third type. newtype ZipWith a b c ZipWith :: (a -> b -> Maybe c) -> ZipWith a b c selZipWith :: ZipWith a b c -> a -> b -> Maybe c -- | Combine two containers into a single container with pairs of the -- original elements. See zipWith for restrictions. This is a -- generic version of the Prelude function of the same name. zip :: (FRep3 ZipWith f) => f a -> f b -> Maybe (f (a, b)) -- | Combine two structurally equivalent containers into one by applying a -- function to every corresponding pair of elements. Returns -- Nothing if f a and f b have different shapes. zipWith :: (FRep3 ZipWith f) => (a -> b -> c) -> f a -> f b -> Maybe (f c)