-- 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 datatype-generic programming. -- -- 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 (Alternative f) => Rep (Collect f T) T where
-- rep = Collect pure
--
--
-- (Note that overlapping instances are required.) This instance triggers
-- when the result type (the T in Collect f T) matches
-- the value type (the second T) contained within the argument
-- to collect. See the source of this module for more examples.
newtype Collect f b a
Collect :: (a -> f b) -> Collect f b a
selCollect :: Collect f b a -> a -> f b
-- | Collect values of type b from some value of type a.
-- An empty 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 :: (Alternative f, Rep (Collect f b) a) => a -> f b instance [overlap ok] Alternative f => Rep (Collect f Char) Char instance [overlap ok] Alternative f => Rep (Collect f Double) Double instance [overlap ok] Alternative f => Rep (Collect f Float) Float instance [overlap ok] Alternative f => Rep (Collect f Integer) Integer instance [overlap ok] Alternative f => Rep (Collect f Int) Int instance [overlap ok] Alternative f => Generic (Collect f b) -- | 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. fail if the container -- is empty. -- -- This is the most general form in which you must specify the -- associativity and the Monad instance. You may prefer to use the -- more convenient firstr or firstl. first :: (Monad m, FRep (Crush [a]) f) => Assoc -> f a -> m a -- | A left-associative Maybe 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 Maybe 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. -- --
-- -- 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)
-- | Summary: Generic functions that translate values of one type into
-- values of another.
--
-- map is a generic version of the Prelude map
-- function. It works on all supported container datatypes of kind *
-- -> *. The map function is equivalent to fmap
-- after deriving Functor if that were possible.
--
-- cast is a generic and configurable function for converting a
-- value of one type into a value of another using instances provided by
-- the programmer.
module Generics.EMGM.Functions.Map
-- | 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 f a with b. Defined
-- as: replace as b = 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 are -- found, so you must define instances in order to use cast. cast :: Rep (Map a) b => a -> b instance Generic2 Map -- | Summary: Functions for extracting meta-information about the -- representation. module Generics.EMGM.Functions.Meta -- | A class to reveal the embedding-projection pair for a given datatype -- and its isomorphic representation type. class HasEP a b | a -> b epOf :: HasEP a b => a -> EP a b -- | The type of a generic function that takes one value and returns an -- optional constructor description. newtype Con a Con :: (a -> Maybe ConDescr) -> Con a selConstructor :: Con a -> a -> Maybe ConDescr -- | Returns a constructor description if the value is not a primitive. The -- argument is not evaluated and may be undefined. conDescr :: Rep Con a => a -> Maybe ConDescr -- | The type of a generic function that takes a boolean to limit recursion -- and a value and returns a list of label descriptions for that -- constructor. newtype Lbls a Lbls :: (Bool -> a -> [LblDescr]) -> Lbls a selLabels :: Lbls a -> Bool -> a -> [LblDescr] -- | Returns a list of descriptions for all labels in the head constructor. -- Does not recurse into the children. The argument is not evaluated and -- may be undefined. lblDescrs :: Rep Lbls a => a -> [LblDescr] instance Generic Lbls instance Generic Con -- | 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 element in a value, splitting the element into two. The result -- is a pair of 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 ZipWith, though -- it has no direct Prelude counterpart. Only unzip has a -- Prelude analog. -- -- 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 m a b c UnzipWith :: (a -> m (b, c)) -> UnzipWith m a b c selUnzipWith :: UnzipWith m a b c -> a -> m (b, c) -- | Splits a container into two structurally equivalent containers by -- applying a function to every element, which splits it into two -- corresponding elements. Fails if the spliting function fails unzipWithM :: (Monad m, FRep3 (UnzipWith m) f) => (a -> m (b, c)) -> f a -> m (f b, f c) -- | A specialized version of unzipWithM using the identity monad -- and a splitting function that does not fail. unzipWith :: FRep3 (UnzipWith Id) f => (a -> (b, c)) -> f a -> (f b, f c) -- | A specialized version of unzipWith for pairs. Generic version -- of Prelude.unzip. unzip :: FRep3 (UnzipWith Id) f => f (b, c) -> (f b, f c) instance Monad Id instance Monad m => Generic3 (UnzipWith m) -- | 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. -- -- The important concepts for zipWithM are structural -- equivalence and corresponding elements. For zipWithM -- to be successful (and not fail), its two container arguments -- must have exactly the same 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, zipWithM will fail. -- -- 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 returns a value of a third type in a Monad. newtype ZipWith m a b c ZipWith :: (a -> b -> m c) -> ZipWith m a b c selZipWith :: ZipWith m a b c -> a -> b -> m c -- | Combine two structurally equivalent containers into one by applying a -- function to every corresponding pair of elements. Fails if (1) the -- binary operator fails or (2) f a and f b have -- different shapes. zipWithM :: (Monad m, FRep3 (ZipWith m) f) => (a -> b -> m c) -> f a -> f b -> m (f c) -- | A specialized version of zipWithM for the Maybe monad -- and a binary operator that does not fail. Generic version of -- Prelude.zipWith. zipWith :: FRep3 (ZipWith Maybe) f => (a -> b -> c) -> f a -> f b -> Maybe (f c) -- | A specialized version of zipWith for pairs. Generic version of -- Prelude.zip. zip :: FRep3 (ZipWith Maybe) f => f a -> f b -> Maybe (f (a, b)) instance Monad m => Generic3 (ZipWith m) -- | Summary: Generic function thats transposes a value f (g a) to -- g (f a). -- -- This is an interesting generic function since it uses multiple other -- generic functions: Crush, Enum, Map, and -- ZipWith. Notably, Map and ZipWith are required -- for definining the sum and product cases of the generic function. The -- others make the generic function easy to use. -- -- NOTE: Be aware of the special case for empty values noted in the -- documentation of tranpose. module Generics.EMGM.Functions.Transpose -- | The type of a generic function that takes a generic value and -- non-generic container and returns the container filled with other -- generic values. newtype Monad m => Transpose m f c b a Transpose :: (a -> f c -> m (f b)) -> Transpose m f c b a selTranspose :: Transpose m f c b a -> a -> f c -> m (f b) -- | Transposes the structure of nested containers (types f and -- g). fail if the outermost container is empty, because -- there is no generic way to guarantee that both have unit constructors -- or, if they do, decide which one to choose. See transposeE for -- an alternative approach. transpose :: (Monad m, FRep (Crush [g a]) f, FRep2 (Transpose m g a) f) => f (g a) -> m (g (f a)) -- | A convenient version of transpose that returns the empty -- value on failure. transposeE :: (Rep Enum (g (f a)), FRep (Crush [g a]) f, FRep2 (Transpose Maybe g a) f) => f (g a) -> g (f a) instance (Monad m, FRep2 Map f, FRep3 (ZipWith m) f) => Generic2 (Transpose m f c) -- | Summary: Generic representation and instances for Bool. module Generics.EMGM.Data.Bool type BoolS = 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] Alternative f => Rep (Collect f Bool) Bool instance [overlap ok] Generic g => Rep g Bool instance [overlap ok] HasEP Bool BoolS -- | Summary: Generic representation and instances for Either. module Generics.EMGM.Data.Either type EitherS 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] Alternative f => Rep (Collect f (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) instance [overlap ok] HasEP (Either a b) (EitherS a b) -- | Summary: Generic representation and instances for lists. module Generics.EMGM.Data.List type ListS 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, 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] Alternative f => Rep (Collect f [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] instance [overlap ok] HasEP [a] (ListS a) -- | Summary: Generic representation and instances for Maybe. module Generics.EMGM.Data.Maybe type MaybeS 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] Alternative f => Rep (Collect f (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) instance [overlap ok] HasEP (Maybe a) (MaybeS a) -- | Summary: Generic representation and instances for tuples of arity 0 -- (''unit'') and 2 to 7. module Generics.EMGM.Data.Tuple type Tuple0S = 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 () () type Tuple2S 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) type Tuple3S 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) type Tuple4S 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) type Tuple5S 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) type Tuple6S 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) type Tuple7S 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] Alternative f => Rep (Collect f (a, b, c, d, e, h, i)) (a, b, c, d, e, h, i) instance [overlap ok] Alternative f => Rep (Collect f (a, b, c, d, e, h)) (a, b, c, d, e, h) instance [overlap ok] Alternative f => Rep (Collect f (a, b, c, d, e)) (a, b, c, d, e) instance [overlap ok] Alternative f => Rep (Collect f (a, b, c, d)) (a, b, c, d) instance [overlap ok] Alternative f => Rep (Collect f (a, b, c)) (a, b, c) instance [overlap ok] Alternative f => Rep (Collect f (a, b)) (a, b) instance [overlap ok] Alternative f => Rep (Collect f ()) () 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 () instance [overlap ok] HasEP (a, b, c, d, e, f, h) (Tuple7S a b c d e f h) instance [overlap ok] HasEP (a, b, c, d, e, f) (Tuple6S a b c d e f) instance [overlap ok] HasEP (a, b, c, d, e) (Tuple5S a b c d e) instance [overlap ok] HasEP (a, b, c, d) (Tuple4S a b c d) instance [overlap ok] HasEP (a, b, c) (Tuple3S a b c) instance [overlap ok] HasEP (a, b) (Tuple2S a b) instance [overlap ok] HasEP () Tuple0S -- | Summary: Generic representation and instances for Ratio. module Generics.EMGM.Data.Ratio type RatioS a = a :*: a -- | Constructor description for %. conRatio :: ConDescr -- | Representation of Ratio for rep. repRatio :: (Integral a, Generic g, Rep g a) => g (Ratio a) -- | Representation of Ratio for frep. frepRatio :: (Integral a, Generic g) => g a -> g (Ratio a) -- | Representation of Ratio for frep2. frep2Ratio :: (Integral a1, Integral a2, Generic2 g) => g a1 a2 -> g (Ratio a1) (Ratio a2) -- | Representation of Ratio for frep3. frep3Ratio :: (Integral a1, Integral a2, Integral a3, Generic3 g) => g a1 a2 a3 -> g (Ratio a1) (Ratio a2) (Ratio a3) -- | Representation of Ratio for bifrep2. bifrep2Ratio :: (Integral a1, Integral a2, Generic2 g) => g a1 a2 -> g (Ratio a1) (Ratio a2) instance [overlap ok] Rep (Everywhere' (Ratio a)) (Ratio a) instance [overlap ok] (Integral a, Rep (Everywhere (Ratio a)) a) => Rep (Everywhere (Ratio a)) (Ratio a) instance [overlap ok] Alternative f => Rep (Collect f (Ratio a)) (Ratio a) instance [overlap ok] (Integral a, Generic g, Rep g a) => Rep g (Ratio a) instance [overlap ok] Integral a => HasEP (Ratio a) (RatioS a) -- | 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. module Generics.EMGM -- | Encodes a constructor with no arguments. An analogous standard Haskell -- type is (). data Unit -- | The only value of type Unit (ignoring _|_). Unit :: Unit -- | The "sum" encodes 2 constructor alternatives. An analogous standard -- Haskell type is Either a b. data (:+:) a b -- | Left alternative L :: a -> :+: a b -- | Right alternative R :: b -> :+: a b -- | The "product" encodes 2 constructor arguments. An analogous standard -- Haskell type is (a, b). data (:*:) a b -- | A pair of arguments (:*:) :: a -> b -> :*: a b -- | The embedding-projection pair contains two functions for converting -- between the datatype and its representation. An EP value -- preserves an isomorphism (ignoring _|_s) between a datatype -- and its structure representation. data EP d r EP :: (d -> r) -> (r -> d) -> EP d r -- | Embed a datatype into its representation. from :: EP d r -> (d -> r) -- | Project datatype from its representation. to :: EP d r -> (r -> d) -- | Contains useful meta-information about the syntax used in a -- constructor declaration. -- -- NOTE: It is important that the ConDescr value accurately -- describe the syntax in a constructor declaration. An incorrect -- description may lead to faulty Read or Show operation. data ConDescr ConDescr :: String -> Int -> Bool -> Fixity -> ConDescr -- | Name of the constructor (without parenthesese if infix). conName :: ConDescr -> String -- | Number of fields. conArity :: ConDescr -> Int -- | Uses labeled fields (a.k.a. record syntax). conRecord :: ConDescr -> Bool -- | Fixity, associativity, precedence. conFixity :: ConDescr -> Fixity -- | Encodes the string label for a field in a constructor defined with -- labeled fields (a.k.a. record syntax). newtype LblDescr LblDescr :: String -> LblDescr -- | A constructor's fixity, associativity, and precedence. data Fixity -- | Associativity and precedence are the same as function application. Prefix :: Fixity Infix :: Associativity -> Prec -> Fixity -- | A constructor's associativity. data Associativity -- | Declared with infixl LeftAssoc :: Associativity -- | Declared with infixr RightAssoc :: Associativity -- | Declared with infix NonAssoc :: Associativity type Prec = Int -- | Get the precedence of a fixity value. prec :: Fixity -> Prec -- | This class forms the foundation for defining generic functions with a -- single generic argument. Each method represents a type case. There are -- cases for primitive types, structural representation types, and for -- user-defined datatypes. -- -- The included modules using Generic are: -- --
-- 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 :: (Alternative f, Rep (Collect f b) a) => a -> f b -- | 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 -- | 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. fail if the container -- is empty. -- -- This is the most general form in which you must specify the -- associativity and the Monad instance. You may prefer to use the -- more convenient firstr or firstl. first :: (Monad m, FRep (Crush [a]) f) => Assoc -> f a -> m a -- | A left-associative Maybe 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 Maybe 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 -- | 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 -- | 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 -- | 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
-- | 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 f a with b. Defined
-- as: replace as b = 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 are -- found, so you must define instances in order to use cast. cast :: Rep (Map a) b => a -> b -- | A class to reveal the embedding-projection pair for a given datatype -- and its isomorphic representation type. class HasEP a b | a -> b epOf :: HasEP a b => a -> EP a b -- | Returns a constructor description if the value is not a primitive. The -- argument is not evaluated and may be undefined. conDescr :: Rep Con a => a -> Maybe ConDescr -- | Returns a list of descriptions for all labels in the head constructor. -- Does not recurse into the children. The argument is not evaluated and -- may be undefined. lblDescrs :: Rep Lbls a => a -> [LblDescr] -- | 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 -- | 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 -- | Transposes the structure of nested containers (types f and -- g). fail if the outermost container is empty, because -- there is no generic way to guarantee that both have unit constructors -- or, if they do, decide which one to choose. See transposeE for -- an alternative approach. transpose :: (Monad m, FRep (Crush [g a]) f, FRep2 (Transpose m g a) f) => f (g a) -> m (g (f a)) -- | A convenient version of transpose that returns the empty -- value on failure. transposeE :: (Rep Enum (g (f a)), FRep (Crush [g a]) f, FRep2 (Transpose Maybe g a) f) => f (g a) -> g (f a) -- | Splits a container into two structurally equivalent containers by -- applying a function to every element, which splits it into two -- corresponding elements. Fails if the spliting function fails unzipWithM :: (Monad m, FRep3 (UnzipWith m) f) => (a -> m (b, c)) -> f a -> m (f b, f c) -- | A specialized version of unzipWithM using the identity monad -- and a splitting function that does not fail. unzipWith :: FRep3 (UnzipWith Id) f => (a -> (b, c)) -> f a -> (f b, f c) -- | A specialized version of unzipWith for pairs. Generic version -- of Prelude.unzip. unzip :: FRep3 (UnzipWith Id) f => f (b, c) -> (f b, f c) -- | Combine two structurally equivalent containers into one by applying a -- function to every corresponding pair of elements. Fails if (1) the -- binary operator fails or (2) f a and f b have -- different shapes. zipWithM :: (Monad m, FRep3 (ZipWith m) f) => (a -> b -> m c) -> f a -> f b -> m (f c) -- | A specialized version of zipWithM for the Maybe monad -- and a binary operator that does not fail. Generic version of -- Prelude.zipWith. zipWith :: FRep3 (ZipWith Maybe) f => (a -> b -> c) -> f a -> f b -> Maybe (f c) -- | A specialized version of zipWith for pairs. Generic version of -- Prelude.zip. zip :: FRep3 (ZipWith Maybe) f => f a -> f b -> Maybe (f (a, b))