-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Compositional Data Types -- -- Based on Wouter Swierstra's Functional Pearl Data types à la -- carte (Journal of Functional Programming, 18(4):423-436, 2008), -- this package provides a framework for defining recursive data types in -- a compositional manner. The fundamental idea of compositional data -- types is to separate the signature of a data type from the fixed point -- construction that produces its recursive structure. By allowing to -- compose and decompose signatures, compositional data types -- enable to combine data types in a flexible way. The key point of -- Wouter Swierstra's original work is to define functions on -- compositional data types in a compositional manner as well by -- leveraging Haskell's type class machinery. -- -- Building on that foundation, this library provides additional -- extensions and (run-time) optimisations which makes compositional data -- types usable for practical implementations. In particular, it provides -- an excellent framework for manipulating and analysing abstract syntax -- trees in a type-safe manner. Thus, it is perfectly suited for -- programming language implementations, especially, in an environment -- consisting of a family of tightly interwoven domain-specific -- languages. -- -- In concrete terms, this package provides the following features: -- --
-- data (f :&&: a) (g :: * -> *) e = f g e :&&: a e ---- -- This is too general, however, for example for -- productHTermHom. data (:&&:) f a g :: (* -> *) e (:&&:) :: f g e -> a -> :&&: f a e -- | This class defines how to distribute a product over a sum of -- signatures. class HDistProd s :: ((* -> *) -> * -> *) p s' | s' -> s, s' -> p hinjectP :: HDistProd s p s' => p -> s a :-> s' a hprojectP :: HDistProd s p s' => s' a :-> (s a :&: p) class HRemoveP s :: ((* -> *) -> * -> *) s' | s -> s' hremoveP :: HRemoveP s s' => s a :-> s' a instance [incoherent] HDistProd s p s' => HDistProd (f :++: s) p ((f :&&: p) :++: s') instance [incoherent] HDistProd f p (f :&&: p) instance [incoherent] HRemoveP (f :&&: p) f instance [incoherent] HRemoveP s s' => HRemoveP ((f :&&: p) :++: s) (f :++: s') instance [incoherent] HTraversable f => HTraversable (f :&&: a) instance [incoherent] HFoldable f => HFoldable (f :&&: a) instance [incoherent] HFunctor f => HFunctor (f :&&: a) instance [incoherent] f :<<: g => f :<<: (h :++: g) instance [incoherent] f :<<: (f :++: g) instance [incoherent] f :<<: f instance [incoherent] (HExpFunctor f, HExpFunctor g) => HExpFunctor (f :++: g) instance [incoherent] (HTraversable f, HTraversable g) => HTraversable (f :++: g) instance [incoherent] (HFoldable f, HFoldable g) => HFoldable (f :++: g) instance [incoherent] (HFunctor f, HFunctor g) => HFunctor (f :++: g) -- | This module defines sums on signatures. All definitions are -- generalised versions of those in Data.Comp.Sum. module Data.Comp.Multi.Sum -- | The subsumption relation. class :<<: sub :: ((* -> *) -> * -> *) sup hinj :: :<<: sub sup => sub a :-> sup a hproj :: :<<: sub sup => NatM Maybe (sup a) (sub a) -- | Data type defining coproducts. data (:++:) f g h :: (* -> *) e HInl :: (f h e) -> :++: f g e HInr :: (g h e) -> :++: f g e -- | A variant of hproj for binary sum signatures. hproj2 :: (:<<: g1 f, :<<: g2 f) => f a i -> Maybe (((g1 :++: g2) a) i) -- | A variant of hproj for ternary sum signatures. hproj3 :: (:<<: g1 f, :<<: g2 f, :<<: g3 f) => f a i -> Maybe (((g1 :++: (g2 :++: g3)) a) i) -- | Project the outermost layer of a term to a sub signature. hproject :: :<<: g f => NatM Maybe (HCxt h f a) (g (HCxt h f a)) -- | Project the outermost layer of a term to a binary sub signature. hproject2 :: (:<<: g1 f, :<<: g2 f) => NatM Maybe (HCxt h f a) ((g1 :++: g2) (HCxt h f a)) -- | Project the outermost layer of a term to a ternary sub signature. hproject3 :: (:<<: g1 f, :<<: g2 f, :<<: g3 f) => NatM Maybe (HCxt h f a) ((g1 :++: (g2 :++: g3)) (HCxt h f a)) -- | Project a term to a term over a sub signature. deepHProject :: (HTraversable f, HFunctor g, :<<: g f) => NatM Maybe (HCxt h f a) (HCxt h g a) -- | Project a term to a term over a binary sub signature. deepHProject2 :: (HTraversable f, HFunctor g1, HFunctor g2, :<<: g1 f, :<<: g2 f) => NatM Maybe (HCxt h f a) (HCxt h (g1 :++: g2) a) -- | Project a term to a term over a ternary sub signature. deepHProject3 :: (HTraversable f, HFunctor g1, HFunctor g2, HFunctor g3, :<<: g1 f, :<<: g2 f, :<<: g3 f) => NatM Maybe (HCxt h f a) (HCxt h (g1 :++: (g2 :++: g3)) a) -- | A variant of hinj for binary sum signatures. hinj2 :: (:<<: f1 g, :<<: f2 g) => (f1 :++: f2) a :-> g a -- | A variant of hinj for ternary sum signatures. hinj3 :: (:<<: f1 g, :<<: f2 g, :<<: f3 g) => (f1 :++: (f2 :++: f3)) a :-> g a -- | Inject a term where the outermost layer is a sub signature. hinject :: :<<: g f => g (HCxt h f a) :-> HCxt h f a -- | Inject a term where the outermost layer is a binary sub signature. hinject2 :: (:<<: f1 g, :<<: f2 g) => (f1 :++: f2) (HCxt h g a) :-> HCxt h g a -- | Inject a term where the outermost layer is a ternary sub signature. hinject3 :: (:<<: f1 g, :<<: f2 g, :<<: f3 g) => (f1 :++: (f2 :++: f3)) (HCxt h g a) :-> HCxt h g a -- | Inject a term over a sub signature to a term over larger signature. deepHInject :: (HFunctor g, HFunctor f, :<<: g f) => HCxt h g a :-> HCxt h f a -- | Inject a term over a binary sub signature to a term over larger -- signature. deepHInject2 :: (HFunctor f1, HFunctor f2, HFunctor g, :<<: f1 g, :<<: f2 g) => HCxt h (f1 :++: f2) a :-> HCxt h g a -- | Inject a term over a ternary sub signature to a term over larger -- signature. deepHInject3 :: (HFunctor f1, HFunctor f2, HFunctor f3, HFunctor g, :<<: f1 g, :<<: f2 g, :<<: f3 g) => HCxt h (f1 :++: (f2 :++: f3)) a :-> HCxt h g a -- | A variant of deepHInject for exponential signatures. deepHInjectE :: (HExpFunctor g, :<<: g f) => HTerm g :-> HTerm f -- | A variant of deepHInject2 for exponential signatures. deepHInjectE2 :: (HExpFunctor g1, HExpFunctor g2, :<<: g1 f, :<<: g2 f) => HTerm (g1 :++: g2) :-> HTerm f -- | A variant of deepHInject3 for exponential signatures. deepHInjectE3 :: (HExpFunctor g1, HExpFunctor g2, HExpFunctor g3, :<<: g1 f, :<<: g2 f, :<<: g3 f) => HTerm (g1 :++: (g2 :++: g3)) :-> HTerm f hinjectHConst :: (HFunctor g, :<<: g f) => HConst g :-> HCxt h f a hinjectHConst2 :: (HFunctor f1, HFunctor f2, HFunctor g, :<<: f1 g, :<<: f2 g) => HConst (f1 :++: f2) :-> HCxt h g a hinjectHConst3 :: (HFunctor f1, HFunctor f2, HFunctor f3, HFunctor g, :<<: f1 g, :<<: f2 g, :<<: f3 g) => HConst (f1 :++: (f2 :++: f3)) :-> HCxt h g a hprojectHConst :: (HFunctor g, :<<: g f) => NatM Maybe (HCxt h f a) (HConst g) -- | This function injects a whole context into another context. hinjectHCxt :: (HFunctor g, :<<: g f) => HCxt h' g (HCxt h f a) :-> HCxt h f a -- | This function lifts the given functor to a context. liftHCxt :: (HFunctor f, :<<: g f) => g a :-> HContext f a -- | This function applies the given context with hole type a to a -- family f of contexts (possibly terms) indexed by a. -- That is, each hole h is replaced by the context f h. substHHoles :: (HFunctor f, HFunctor g, :<<: f g) => (v :-> HCxt h g a) -> HCxt h' f v :-> HCxt h g a -- | This module defines an abstraction notion of a variable in a term. All -- definitions are generalised versions of those in -- Data.Comp.Variables. module Data.Comp.Multi.Variables type GSubst v a = NatM Maybe (K v) a type HCxtSubst h a f v = GSubst v (HCxt h f a) type Subst f v = HCxtSubst HNoHole HNothing f v -- | This multiparameter class defines functors with variables. An instance -- HasVar f v denotes that values over f might contain -- variables of type v. class HasVars f :: ((* -> *) -> * -> *) v isVar :: HasVars f v => f a :=> Maybe v varsToHHoles :: (HFunctor f, HasVars f v) => HTerm f :-> HContext f (K v) containsVarAlg :: (Eq v, HasVars f v, HFoldable f) => v -> HAlg f (K Bool) -- | This function checks whether a variable is contained in a context. containsVar :: (Eq v, HasVars f v, HFoldable f, HFunctor f) => v -> HCxt h f a :=> Bool variableListAlg :: (HasVars f v, HFoldable f) => HAlg f (K [v]) -- | This function computes the list of variables occurring in a context. variableList :: (HasVars f v, HFoldable f, HFunctor f) => HCxt h f a :=> [v] variablesAlg :: (Ord v, HasVars f v, HFoldable f) => HAlg f (K (Set v)) -- | This function computes the set of variables occurring in a context. variables :: (Ord v, HasVars f v, HFoldable f, HFunctor f) => HCxt h f a :=> Set v -- | This function computes the set of variables occurring in a context. variables' :: (Ord v, HasVars f v, HFoldable f, HFunctor f) => HConst f :=> Set v substAlg :: HasVars f v => HCxtSubst h a f v -> HAlg f (HCxt h f a) -- | This function substitutes variables in a context according to a -- partial mapping from variables to contexts. class SubstVars v t a substVars :: SubstVars v t a => GSubst v t -> a :-> a appSubst :: SubstVars v t a => GSubst v t -> a :-> a -- | This function composes two substitutions s1 and s2. -- That is, applying the resulting substitution is equivalent to first -- applying s2 and then s1. compSubst :: (Ord v, HasVars f v, HFunctor f) => HCxtSubst h a f v -> HCxtSubst h a f v -> HCxtSubst h a f v instance [overlap ok] (SubstVars v t a, HFunctor f) => SubstVars v t (f a) instance [overlap ok] (Ord v, HasVars f v, HFunctor f) => SubstVars v (HCxt h f a) (HCxt h f a) instance [overlap ok] HasVars f v => HasVars (HCxt h f) v instance [overlap ok] (HasVars f v, HasVars g v) => HasVars (f :++: g) v -- | This module defines products on signatures. All definitions are -- generalised versions of those in Data.Comp.Product. module Data.Comp.Multi.Product -- | This data type adds a constant product to a signature. Alternatively, -- this could have also been defined as -- --
-- data (f :&&: a) (g :: * -> *) e = f g e :&&: a e ---- -- This is too general, however, for example for -- productHTermHom. data (:&&:) f a g :: (* -> *) e (:&&:) :: f g e -> a -> :&&: f a e -- | This class defines how to distribute a product over a sum of -- signatures. class HDistProd s :: ((* -> *) -> * -> *) p s' | s' -> s, s' -> p hinjectP :: HDistProd s p s' => p -> s a :-> s' a hprojectP :: HDistProd s p s' => s' a :-> (s a :&: p) class HRemoveP s :: ((* -> *) -> * -> *) s' | s -> s' hremoveP :: HRemoveP s s' => s a :-> s' a -- | This function transforms a function with a domain constructed from a -- functor to a function with a domain constructed with the same functor -- but with an additional product. liftP :: HRemoveP s s' => (s' a :-> t) -> s a :-> t -- | This function annotates each sub term of the given term with the given -- value (of type a). constP :: (HDistProd f p g, HFunctor f, HFunctor g) => p -> HCxt h f a :-> HCxt h g a -- | This function transforms a function with a domain constructed from a -- functor to a function with a domain constructed with the same functor -- but with an additional product. liftP' :: (HDistProd s' p s, HFunctor s, HFunctor s') => (s' a :-> HCxt h s' a) -> s a :-> HCxt h s a -- | This function strips the products from a term over a functor whith -- products. stripP :: (HFunctor f, HRemoveP g f, HFunctor g) => HCxt h g a :-> HCxt h f a productHTermHom :: (HDistProd f p f', HDistProd g p g', HFunctor g, HFunctor g') => HTermHom f g -> HTermHom f' g' hproject' :: (HRemoveP g s', :<<: g f) => HCxt h f a i -> Maybe (s' (HCxt h f a) i) -- | This module defines the infrastructure necessary to use compositional -- data types for mutually recursive data types. Examples of usage are -- provided below. module Data.Comp.Multi -- | This module defines the central notion of terms and its -- generalisation to contexts. module Data.Comp.Term -- | This data type represents contexts over a signature. Contexts are -- terms containing zero or more holes. The first type parameter is -- supposed to be one of the phantom types Hole and NoHole. -- The second parameter is the signature of the context. The third -- parameter is the type of the holes. data Cxt :: * -> (* -> *) -> * -> * Term :: f (Cxt h f a) -> Cxt h f a Hole :: a -> Cxt Hole f a -- | Phantom type that signals that a Cxt might contain holes. data Hole -- | Phantom type that signals that a Cxt does not contain holes. data NoHole type Context = Cxt Hole -- | Phantom type used to define Term. data Nothing -- | A term is a context with no holes. type Term f = Cxt NoHole f Nothing -- | Polymorphic definition of a term. This formulation is more natural -- than Term, it leads to impredicative types in some cases, -- though. type PTerm f = forall h a. Cxt h f a type Const f = f () -- | This function unravels the given term at the topmost layer. unTerm :: Cxt NoHole f a -> f (Cxt NoHole f a) -- | Convert a functorial value into a context. simpCxt :: Functor f => f a -> Context f a -- | Cast a term over a signature to a context over the same signature. toCxt :: Term f -> Cxt h f a -- | This function converts a constant to a term. This assumes that the -- argument is indeed a constant, i.e. does not have a value for the -- argument type of the functor f. constTerm :: Functor f => Const f -> Term f instance Traversable f => Traversable (Cxt h f) instance Foldable f => Foldable (Cxt h f) instance Functor f => Functor (Cxt h f) instance Show Nothing instance Ord Nothing instance Eq Nothing -- | This module defines the notion of algebras and catamorphisms, and -- their generalizations to e.g. monadic versions and other (co)recursion -- schemes. module Data.Comp.Algebra -- | This type represents an algebra over a functor f and carrier -- a. type Alg f a = f a -> a -- | Construct a catamorphism for contexts over f with holes of -- type a, from the given algebra. free :: Functor f => Alg f b -> (a -> b) -> Cxt h f a -> b -- | Construct a catamorphism from the given algebra. cata :: Functor f => Alg f a -> Term f -> a -- | A generalisation of cata from terms over f to contexts -- over f, where the holes have the type of the algebra carrier. cata' :: Functor f => Alg f a -> Cxt h f a -> a -- | This function applies a whole context into another context. appCxt :: Functor f => Context f (Cxt h f a) -> Cxt h f a -- | This type represents a monadic algebra. It is similar to Alg -- but the return type is monadic. type AlgM m f a = f a -> m a -- | Convert a monadic algebra into an ordinary algebra with a monadic -- carrier. algM :: (Traversable f, Monad m) => AlgM m f a -> Alg f (m a) -- | Construct a monadic catamorphism for contexts over f with -- holes of type a, from the given monadic algebra. freeM :: (Traversable f, Monad m) => AlgM m f b -> (a -> m b) -> Cxt h f a -> m b -- | Construct a monadic catamorphism from the given monadic algebra. cataM :: (Traversable f, Monad m) => AlgM m f a -> Term f -> m a -- | A generalisation of cataM from terms over f to -- contexts over f, where the holes have the type of the monadic -- algebra carrier. cataM' :: (Traversable f, Monad m) => AlgM m f a -> Cxt h f a -> m a -- | This type represents a context function. type CxtFun f g = forall a h. Cxt h f a -> Cxt h g a -- | This type represents a signature function. type SigFun f g = forall a. f a -> g a -- | This type represents a term homomorphism. type TermHom f g = SigFun f (Context g) -- | Apply a term homomorphism recursively to a term/context. appTermHom :: (Traversable f, Functor g) => TermHom f g -> CxtFun f g -- | Compose two term homomorphisms. compTermHom :: (Functor g, Functor h) => TermHom g h -> TermHom f g -> TermHom f h -- | This function applies a signature function to the given context. appSigFun :: (Functor f, Functor g) => SigFun f g -> CxtFun f g -- | This function composes two signature functions. compSigFun :: SigFun g h -> SigFun f g -> SigFun f h -- | Lifts the given signature function to the canonical term homomorphism. termHom :: Functor g => SigFun f g -> TermHom f g -- | Compose an algebra with a term homomorphism to get a new algebra. compAlg :: Functor g => Alg g a -> TermHom f g -> Alg f a -- | Compose a term homomorphism with a coalgebra to get a cv-coalgebra. compCoalg :: TermHom f g -> Coalg f a -> CVCoalg' g a -- | Compose a term homomorphism with a cv-coalgebra to get a new -- cv-coalgebra. compCVCoalg :: (Functor f, Functor g) => TermHom f g -> CVCoalg' f a -> CVCoalg' g a -- | This type represents a monadic context function. type CxtFunM m f g = forall a h. Cxt h f a -> m (Cxt h g a) -- | This type represents a monadic signature function. type SigFunM m f g = forall a. f a -> m (g a) -- | This type represents a monadic term homomorphism. type TermHomM m f g = SigFunM m f (Context g) -- | This type represents a monadic signature function. It is similar to -- SigFunM but has monadic values also in the domain. type SigFunM' m f g = forall a. f (m a) -> m (g a) -- | This type represents a monadic term homomorphism. It is similar to -- TermHomM but has monadic values also in the domain. type TermHomM' m f g = SigFunM' m f (Context g) -- | Lift the given signature function to a monadic signature function. -- Note that term homomorphisms are instances of signature functions. -- Hence this function also applies to term homomorphisms. sigFunM :: Monad m => SigFun f g -> SigFunM m f g -- | Lift the give monadic signature function to a monadic term -- homomorphism. termHom' :: (Functor f, Functor g, Monad m) => SigFunM m f g -> TermHomM m f g -- | Apply a monadic term homomorphism recursively to a term/context. appTermHomM :: (Traversable f, Functor g, Monad m) => TermHomM m f g -> CxtFunM m f g -- | Lift the given signature function to a monadic term homomorphism. termHomM :: (Functor g, Monad m) => SigFun f g -> TermHomM m f g -- | This function constructs the unique monadic homomorphism from the -- initial term algebra to the given term algebra. termHomM' :: (Traversable f, Functor g, Monad m) => TermHomM' m f g -> CxtFunM m f g -- | This function applies a monadic signature function to the given -- context. appSigFunM :: (Traversable f, Functor g, Monad m) => SigFunM m f g -> CxtFunM m f g -- | This function applies a signature function to the given context. appSigFunM' :: (Traversable f, Functor g, Monad m) => SigFunM' m f g -> CxtFunM m f g -- | Compose two monadic term homomorphisms. compTermHomM :: (Traversable g, Functor h, Monad m) => TermHomM m g h -> TermHomM m f g -> TermHomM m f h -- | This function composes two monadic signature functions. compSigFunM :: Monad m => SigFunM m g h -> SigFunM m f g -> SigFunM m f h -- | Compose a monadic algebra with a monadic term homomorphism to get a -- new monadic algebra. compAlgM :: (Traversable g, Monad m) => AlgM m g a -> TermHomM m f g -> AlgM m f a -- | Compose a monadic algebra with a term homomorphism to get a new -- monadic algebra. compAlgM' :: (Traversable g, Monad m) => AlgM m g a -> TermHom f g -> AlgM m f a -- | This type represents a coalgebra over a functor f and carrier -- a. type Coalg f a = a -> f a -- | Construct an anamorphism from the given coalgebra. ana :: Functor f => Coalg f a -> a -> Term f -- | Shortcut fusion variant of ana. ana' :: Functor f => Coalg f a -> a -> Term f -- | This type represents a monadic coalgebra over a functor f and -- carrier a. type CoalgM m f a = a -> m (f a) -- | Construct a monadic anamorphism from the given monadic coalgebra. anaM :: (Traversable f, Monad m) => CoalgM m f a -> a -> m (Term f) -- | This type represents an r-algebra over a functor f and -- carrier a. type RAlg f a = f (Term f, a) -> a -- | Construct a paramorphism from the given r-algebra. para :: Functor f => RAlg f a -> Term f -> a -- | This type represents a monadic r-algebra over a functor f and -- carrier a. type RAlgM m f a = f (Term f, a) -> m a -- | Construct a monadic paramorphism from the given monadic r-algebra. paraM :: (Traversable f, Monad m) => RAlgM m f a -> Term f -> m a -- | This type represents an r-coalgebra over a functor f and -- carrier a. type RCoalg f a = a -> f (Either (Term f) a) -- | Construct an apomorphism from the given r-coalgebra. apo :: Functor f => RCoalg f a -> a -> Term f -- | This type represents a monadic r-coalgebra over a functor f -- and carrier a. type RCoalgM m f a = a -> m (f (Either (Term f) a)) -- | Construct a monadic apomorphism from the given monadic r-coalgebra. apoM :: (Traversable f, Monad m) => RCoalgM m f a -> a -> m (Term f) -- | This type represents a cv-algebra over a functor f and -- carrier a. type CVAlg f a f' = f (Term f') -> a -- | Construct a histomorphism from the given cv-algebra. histo :: (Functor f, DistProd f a f') => CVAlg f a f' -> Term f -> a -- | This type represents a monadic cv-algebra over a functor f -- and carrier a. type CVAlgM m f a f' = f (Term f') -> m a -- | Construct a monadic histomorphism from the given monadic cv-algebra. histoM :: (Traversable f, Monad m, DistProd f a f') => CVAlgM m f a f' -> Term f -> m a -- | This type represents a cv-coalgebra over a functor f and -- carrier a. type CVCoalg f a = a -> f (Context f a) -- | Construct a futumorphism from the given cv-coalgebra. futu :: Functor f => CVCoalg f a -> a -> Term f -- | This type represents a generalised cv-coalgebra over a functor -- f and carrier a. type CVCoalg' f a = a -> Context f a -- | Construct a futumorphism from the given generalised cv-coalgebra. futu' :: Functor f => CVCoalg' f a -> a -> Term f -- | This type represents a monadic cv-coalgebra over a functor f -- and carrier a. type CVCoalgM m f a = a -> m (f (Context f a)) -- | Construct a monadic futumorphism from the given monadic cv-coalgebra. futuM :: (Traversable f, Monad m) => CVCoalgM m f a -> a -> m (Term f) -- | Variant of appTermHom for term homomorphisms from and to -- ExpFunctor signatures. appTermHomE :: (ExpFunctor f, ExpFunctor g) => TermHom f g -> Term f -> Term g -- | Catamorphism for exponential functors. The intermediate -- cataFS originates from -- http://comonad.com/reader/2008/rotten-bananas/. cataE :: ExpFunctor f => Alg f a -> Term f -> a -- | Anamorphism for exponential functors. anaE :: ExpFunctor f => Coalg f a -> a -> Term f -- | Variant of appCxt for contexts over ExpFunctor -- signatures. appCxtE :: ExpFunctor f => Context f (Cxt h f a) -> Cxt h f a -- | This module provides the infrastructure to extend signatures. module Data.Comp.Sum -- | Signature containment relation for automatic injections. The left-hand -- must be an atomic signature, where as the right-hand side must have a -- list-like structure. Examples include f :<: f :+: g and -- g :<: f :+: (g :+: h), non-examples include f :+: g -- :<: f :+: (g :+: h) and f :<: (f :+: g) :+: h. class :<: sub sup inj :: :<: sub sup => sub a -> sup a proj :: :<: sub sup => sup a -> Maybe (sub a) -- | Formal sum of signatures (functors). data (:+:) f g e Inl :: (f e) -> :+: f g e Inr :: (g e) -> :+: f g e -- | A variant of proj for binary sum signatures. proj2 :: (:<: g1 f, :<: g2 f) => f a -> Maybe ((g1 :+: g2) a) -- | A variant of proj for ternary sum signatures. proj3 :: (:<: g1 f, :<: g2 f, :<: g3 f) => f a -> Maybe ((g1 :+: (g2 :+: g3)) a) -- | Project the outermost layer of a term to a sub signature. project :: :<: g f => Cxt h f a -> Maybe (g (Cxt h f a)) -- | Project the outermost layer of a term to a binary sub signature. project2 :: (:<: g1 f, :<: g2 f) => Cxt h f a -> Maybe ((g1 :+: g2) (Cxt h f a)) -- | Project the outermost layer of a term to a ternary sub signature. project3 :: (:<: g1 f, :<: g2 f, :<: g3 f) => Cxt h f a -> Maybe ((g1 :+: (g2 :+: g3)) (Cxt h f a)) -- | Project a term to a term over a sub signature. deepProject :: (Traversable f, Functor g, :<: g f) => Cxt h f a -> Maybe (Cxt h g a) -- | Project a term to a term over a binary sub signature. deepProject2 :: (Traversable f, Functor g1, Functor g2, :<: g1 f, :<: g2 f) => Cxt h f a -> Maybe (Cxt h (g1 :+: g2) a) -- | Project a term to a term over a ternary sub signature. deepProject3 :: (Traversable f, Functor g1, Functor g2, Functor g3, :<: g1 f, :<: g2 f, :<: g3 f) => Cxt h f a -> Maybe (Cxt h (g1 :+: (g2 :+: g3)) a) -- | A variant of deepProject where the sub signature is required to -- be Traversable rather than the whole signature. deepProject' :: (Traversable g, :<: g f) => Cxt h f a -> Maybe (Cxt h g a) -- | A variant of deepProject2 where the sub signatures are required -- to be Traversable rather than the whole signature. deepProject2' :: (Traversable g1, Traversable g2, :<: g1 f, :<: g2 f) => Cxt h f a -> Maybe (Cxt h (g1 :+: g2) a) -- | A variant of deepProject3 where the sub signatures are required -- to be Traversable rather than the whole signature. deepProject3' :: (Traversable g1, Traversable g2, Traversable g3, :<: g1 f, :<: g2 f, :<: g3 f) => Cxt h f a -> Maybe (Cxt h (g1 :+: (g2 :+: g3)) a) -- | A variant of inj for binary sum signatures. inj2 :: (:<: f1 g, :<: f2 g) => (f1 :+: f2) a -> g a -- | A variant of inj for ternary sum signatures. inj3 :: (:<: f1 g, :<: f2 g, :<: f3 g) => (f1 :+: (f2 :+: f3)) a -> g a -- | Inject a term where the outermost layer is a sub signature. inject :: :<: g f => g (Cxt h f a) -> Cxt h f a -- | Inject a term where the outermost layer is a binary sub signature. inject2 :: (:<: f1 g, :<: f2 g) => (f1 :+: f2) (Cxt h g a) -> Cxt h g a -- | Inject a term where the outermost layer is a ternary sub signature. inject3 :: (:<: f1 g, :<: f2 g, :<: f3 g) => (f1 :+: (f2 :+: f3)) (Cxt h g a) -> Cxt h g a -- | Inject a term over a sub signature to a term over larger signature. deepInject :: (Functor g, Functor f, :<: g f) => Cxt h g a -> Cxt h f a -- | Inject a term over a binary sub signature to a term over larger -- signature. deepInject2 :: (Functor f1, Functor f2, Functor g, :<: f1 g, :<: f2 g) => Cxt h (f1 :+: f2) a -> Cxt h g a -- | Inject a term over a ternary signature to a term over larger -- signature. deepInject3 :: (Functor f1, Functor f2, Functor f3, Functor g, :<: f1 g, :<: f2 g, :<: f3 g) => Cxt h (f1 :+: (f2 :+: f3)) a -> Cxt h g a -- | A variant of deepInject for exponential signatures. deepInjectE :: (ExpFunctor g, :<: g f) => Term g -> Term f -- | A variant of deepInject2 for exponential signatures. deepInjectE2 :: (ExpFunctor g1, ExpFunctor g2, :<: g1 f, :<: g2 f) => Term (g1 :+: g2) -> Term f -- | A variant of deepInject3 for exponential signatures. deepInjectE3 :: (ExpFunctor g1, ExpFunctor g2, ExpFunctor g3, :<: g1 f, :<: g2 f, :<: g3 f) => Term (g1 :+: (g2 :+: g3)) -> Term f injectConst :: (Functor g, :<: g f) => Const g -> Cxt h f a injectConst2 :: (Functor f1, Functor f2, Functor g, :<: f1 g, :<: f2 g) => Const (f1 :+: f2) -> Cxt h g a injectConst3 :: (Functor f1, Functor f2, Functor f3, Functor g, :<: f1 g, :<: f2 g, :<: f3 g) => Const (f1 :+: (f2 :+: f3)) -> Cxt h g a projectConst :: (Functor g, :<: g f) => Cxt h f a -> Maybe (Const g) -- | This function injects a whole context into another context. injectCxt :: (Functor g, :<: g f) => Cxt h' g (Cxt h f a) -> Cxt h f a -- | This function lifts the given functor to a context. liftCxt :: (Functor f, :<: g f) => g a -> Context f a -- | This function applies the given context with hole type a to a -- family f of contexts (possibly terms) indexed by a. -- That is, each hole h is replaced by the context f h. substHoles :: (Functor f, Functor g, :<: f g) => Cxt h' f v -> (v -> Cxt h g a) -> Cxt h g a substHoles' :: (Functor f, Functor g, :<: f g, Ord v) => Cxt h' f v -> Map v (Cxt h g a) -> Cxt h g a instance [incoherent] (Eq (f a), Eq (g a)) => Eq ((:+:) f g a) instance [incoherent] (Ord (f a), Ord (g a)) => Ord ((:+:) f g a) instance [incoherent] (Show (f a), Show (g a)) => Show ((:+:) f g a) instance [incoherent] Functor f => Monad (Context f) -- | This module defines products on signatures. module Data.Comp.Product -- | This data type adds a constant product to a signature. data (:&:) f a e (:&:) :: f e -> a -> :&: f a e -- | Formal product of signatures (functors). data (:*:) f g a (:*:) :: f a -> g a -> :*: f g a -- | This class defines how to distribute a product over a sum of -- signatures. class DistProd s p s' | s' -> s, s' -> p injectP :: DistProd s p s' => p -> s a -> s' a projectP :: DistProd s p s' => s' a -> (s a, p) class RemoveP s s' | s -> s' removeP :: RemoveP s s' => s a -> s' a -- | Transform a function with a domain constructed from a functor to a -- function with a domain constructed with the same functor, but with an -- additional product. liftP :: RemoveP s s' => (s' a -> t) -> s a -> t -- | Transform a function with a domain constructed from a functor to a -- function with a domain constructed with the same functor, but with an -- additional product. liftP' :: (DistProd s' p s, Functor s, Functor s') => (s' a -> Cxt h s' a) -> s a -> Cxt h s a -- | Strip the products from a term over a functor with products. stripP :: (Functor f, RemoveP g f, Functor g) => Cxt h g a -> Cxt h f a -- | Lift a term homomorphism over signatures f and g to -- a term homomorphism over the same signatures, but extended with -- products. productTermHom :: (DistProd f p f', DistProd g p g', Functor g, Functor g') => TermHom f g -> TermHom f' g' -- | Annotate each node of a term with a constant value. constP :: (DistProd f p g, Functor f, Functor g) => p -> Cxt h f a -> Cxt h g a project' :: (RemoveP g s', :<: g f) => Cxt h f a -> Maybe (s' (Cxt h f a)) -- | This module defines type generic functions and recursive schemes along -- the lines of the Uniplate library. module Data.Comp.Generic -- | This function returns a list of all subterms of the given term. This -- function is similar to Uniplate's universe function. subterms :: Foldable f => Term f -> [Term f] -- | This function returns a list of all subterms of the given term that -- are constructed from a particular functor. subterms' :: (Foldable f, :<: g f) => Term f -> [g (Term f)] -- | This function transforms every subterm according to the given function -- in a bottom-up manner. This function is similar to Uniplate's -- transform function. transform :: Functor f => (Term f -> Term f) -> Term f -> Term f transform' :: Functor f => (Term f -> Maybe (Term f)) -> Term f -> Term f -- | Monadic version of transform. transformM :: (Traversable f, Monad m) => (Term f -> m (Term f)) -> Term f -> m (Term f) query :: Foldable f => (Term f -> r) -> (r -> r -> r) -> Term f -> r gsize :: Foldable f => Term f -> Int -- | This function computes the generic size of the given term, i.e. the -- its number of subterm occurrences. size :: Foldable f => Cxt h f a -> Int -- | This function computes the generic depth of the given term. depth :: Foldable f => Cxt h f a -> Int -- | This module defines an abstraction notion of a variable in -- compositional data type. module Data.Comp.Variables -- | This multiparameter class defines functors with variables. An instance -- HasVar f v denotes that values over f might contain -- variables of type v. class HasVars f v isVar :: HasVars f v => f a -> Maybe v type Subst f v = CxtSubst NoHole Nothing f v type CxtSubst h a f v = Map v (Cxt h f a) varsToHoles :: (Functor f, HasVars f v) => Term f -> Context f v -- | This function checks whether a variable is contained in a context. containsVar :: (Eq v, HasVars f v, Foldable f, Functor f) => v -> Cxt h f a -> Bool -- | This function computes the set of variables occurring in a context. variables :: (Ord v, HasVars f v, Foldable f, Functor f) => Cxt h f a -> Set v -- | This function computes the list of variables occurring in a context. variableList :: (Ord v, HasVars f v, Foldable f, Functor f) => Cxt h f a -> [v] -- | This function computes the set of variables occurring in a context. variables' :: (Ord v, HasVars f v, Foldable f, Functor f) => Const f -> Set v substVars :: SubstVars v t a => (v -> Maybe t) -> a -> a appSubst :: (Ord v, SubstVars v t a) => Map v t -> a -> a -- | This function composes two substitutions s1 and s2. -- That is, applying the resulting substitution is equivalent to first -- applying s2 and then s1. compSubst :: (Ord v, HasVars f v, Functor f) => CxtSubst h a f v -> CxtSubst h a f v -> CxtSubst h a f v instance [overlap ok] (SubstVars v t a, Functor f) => SubstVars v t (f a) instance [overlap ok] (Ord v, HasVars f v, Functor f) => SubstVars v (Cxt h f a) (Cxt h f a) instance [overlap ok] HasVars f v => HasVars (Cxt h f) v instance [overlap ok] (HasVars f v, HasVars g v) => HasVars (f :+: g) v -- | This module implements the decomposition of terms into function -- symbols and arguments resp. variables. module Data.Comp.Decompose -- | This type represents decompositions of functorial values. data Decomp f v a Var :: v -> Decomp f v a Fun :: (Const f) -> [a] -> Decomp f v a -- | This type represents decompositions of terms. type DecompTerm f v = Decomp f v (Term f) -- | This class specifies the decomposability of a functorial value. class (HasVars f v, Functor f, Foldable f) => Decompose f v decomp :: Decompose f v => f a -> Decomp f v a -- | This function computes the structure of a functorial value. structure :: Functor f => f a -> Const f -- | This function computes the arguments of a functorial value. arguments :: Foldable f => f a -> [a] -- | This function decomposes a term. decompose :: Decompose f v => Term f -> DecompTerm f v instance (HasVars f v, Functor f, Foldable f) => Decompose f v -- | This module implements a simple unification algorithm using -- compositional data types. module Data.Comp.Unification -- | This type represents equations between terms over a specific -- signature. type Equation f = (Term f, Term f) -- | This type represents list of equations. type Equations f = [Equation f] -- | This type represents errors that might occur during the unification. data UnifError f v FailedOccursCheck :: v -> (Term f) -> UnifError f v HeadSymbolMismatch :: (Term f) -> (Term f) -> UnifError f v UnifError :: String -> UnifError f v failedOccursCheck :: MonadError (UnifError f v) m => v -> Term f -> m a headSymbolMismatch :: MonadError (UnifError f v) m => Term f -> Term f -> m a appSubstEq :: (Ord v, HasVars f v, Functor f) => Subst f v -> Equation f -> Equation f -- | This function returns the most general unifier of the given equations -- using the algorithm of Martelli and Montanari. unify :: (MonadError (UnifError f v) m, Decompose f v, Ord v, Eq (Const f)) => Equations f -> m (Subst f v) data UnifyState f v UnifyState :: Equations f -> Subst f v -> UnifyState f v usEqs :: UnifyState f v -> Equations f usSubst :: UnifyState f v -> Subst f v type UnifyM f v m a = StateT (UnifyState f v) m a runUnifyM :: MonadError (UnifError f v) m => UnifyM f v m a -> Equations f -> m (Subst f v) withNextEq :: Monad m => (Equation f -> UnifyM f v m ()) -> UnifyM f v m () putEqs :: Monad m => Equations f -> UnifyM f v m () putBinding :: (Monad m, Ord v, HasVars f v, Functor f) => (v, Term f) -> UnifyM f v m () runUnify :: (MonadError (UnifError f v) m, Decompose f v, Ord v, Eq (Const f)) => UnifyM f v m () unifyStep :: (MonadError (UnifError f v) m, Decompose f v, Ord v, Eq (Const f)) => Equation f -> UnifyM f v m () instance Error (UnifError f v) -- | This module contains functionality for automatically deriving -- boilerplate code using Template Haskell. Examples include instances of -- Functor, Foldable, and Traversable. module Data.Comp.Derive -- | Helper function for generating a list of instances for a list of named -- signatures. For example, in order to derive instances Functor -- and ShowF for a signature Exp, use derive as follows -- (requires Template Haskell): -- --
-- $(derive [instanceFunctor, instanceShowF] [''Exp]) --derive :: [Name -> Q [Dec]] -> [Name] -> Q [Dec] -- | Signature printing. An instance ShowF f gives rise to an -- instance Show (Term f). class ShowF f showF :: ShowF f => f String -> String -- | Derive an instance of ShowF for a type constructor of any -- first-order kind taking at least one argument. instanceShowF :: Name -> Q [Dec] -- | Signature equality. An instance EqF f gives rise to an -- instance Eq (Term f). class EqF f eqF :: (EqF f, Eq a) => f a -> f a -> Bool -- | Derive an instance of EqF for a type constructor of any -- first-order kind taking at least one argument. instanceEqF :: Name -> Q [Dec] -- | Signature ordering. An instance OrdF f gives rise to an -- instance Ord (Term f). class EqF f => OrdF f compareF :: (OrdF f, Ord a) => f a -> f a -> Ordering -- | Derive an instance of OrdF for a type constructor of any -- first-order kind taking at least one argument. instanceOrdF :: Name -> Q [Dec] -- | The Functor class is used for types that can be mapped over. -- Instances of Functor should satisfy the following laws: -- --
-- fmap id == id -- fmap (f . g) == fmap f . fmap g ---- -- The instances of Functor for lists, Data.Maybe.Maybe -- and System.IO.IO satisfy these laws. class Functor f :: (* -> *) -- | Derive an instance of Functor for a type constructor of any -- first-order kind taking at least one argument. instanceFunctor :: Name -> Q [Dec] -- | Data structures that can be folded. -- -- Minimal complete definition: foldMap or foldr. -- -- For example, given a data type -- --
-- data Tree a = Empty | Leaf a | Node (Tree a) a (Tree a) ---- -- a suitable instance would be -- --
-- instance Foldable Tree where -- foldMap f Empty = mempty -- foldMap f (Leaf x) = f x -- foldMap f (Node l k r) = foldMap f l `mappend` f k `mappend` foldMap f r ---- -- This is suitable even for abstract types, as the monoid is assumed to -- satisfy the monoid laws. Alternatively, one could define -- foldr: -- --
-- instance Foldable Tree where -- foldr f z Empty = z -- foldr f z (Leaf x) = f x z -- foldr f z (Node l k r) = foldr f (f k (foldr f z r)) l --class Foldable t :: (* -> *) -- | Derive an instance of Foldable for a type constructor of any -- first-order kind taking at least one argument. instanceFoldable :: Name -> Q [Dec] -- | Functors representing data structures that can be traversed from left -- to right. -- -- Minimal complete definition: traverse or sequenceA. -- -- Instances are similar to Functor, e.g. given a data type -- --
-- data Tree a = Empty | Leaf a | Node (Tree a) a (Tree a) ---- -- a suitable instance would be -- --
-- instance Traversable Tree where -- traverse f Empty = pure Empty -- traverse f (Leaf x) = Leaf <$> f x -- traverse f (Node l k r) = Node <$> traverse f l <*> f k <*> traverse f r ---- -- This is suitable even for abstract types, as the laws for -- <*> imply a form of associativity. -- -- The superclass instances should satisfy the following: -- --