Copyright | (c) gspia 2020- |
---|---|
License | BSD |
Maintainer | gspia |
Safe Haskell | Safe |
Language | Haskell2010 |
Fcf.Alg.Morphism
Type-level Cata
and Ana
can be used to do complex computation that live only
on type-level or on compile-time. As an example, see the sorting algorithm
in Fcf.Alg.Tree -module.
This module also provides some other type-level functions that probably will
find other place after a while. E.g. First
and Second
and their instances
on Either and tuple.
Synopsis
- data Fix f = Fix (f (Fix f))
- type Algebra f a = f a -> Exp a
- type CoAlgebra f a = a -> Exp (f a)
- type RAlgebra f a = f (Fix f, a) -> Exp a
- data Cata :: Algebra f a -> Fix f -> Exp a
- data Ana :: CoAlgebra f a -> a -> Exp (Fix f)
- data Hylo :: Algebra f a -> CoAlgebra f b -> b -> Exp a
- data Fanout :: RAlgebra f a -> Fix f -> Exp (Fix f, a)
- data Para :: RAlgebra f a -> Fix f -> Exp a
- newtype AnnF f a r = AnnF (f r, a)
- type Ann f a = Fix (AnnF f a)
- data Attr :: Ann f a -> Exp a
- data Strip :: Ann f a -> Exp (f (Ann f a))
- data AnnConstr :: (f (Ann f a), a) -> Exp (Fix (AnnF f a))
- data SynthAlg :: (f a -> Exp a) -> f (Ann f a) -> Exp (Ann f a)
- data Synthesize :: (f a -> Exp a) -> Fix f -> Exp (Ann f a)
- data HistoAlg :: (f (Ann f a) -> Exp a) -> f (Ann f a) -> Exp (Ann f a)
- data Histo :: (f (Ann f a) -> Exp a) -> Fix f -> Exp a
- data First :: (a -> Exp b) -> f a c -> Exp (f b c)
- data Second :: (c -> Exp d) -> f a c -> Exp (f a d)
Documentation
>>>
import qualified GHC.TypeLits as TL
>>>
import Fcf.Alg.List
>>>
import Fcf.Data.Nat
Instances
type Eval (FibAlgebra (Succ (Fix (AnnF ((,) (Succ (Fix (AnnF ((,) _ n)))) m)))) :: Nat -> Type) Source # | |
type Eval (FibAlgebra (Succ (Fix (AnnF ((,) (Zero :: NatF (Fix (AnnF NatF Nat))) _)))) :: Nat -> Type) Source # | |
type Eval (FibAlgebra (Zero :: NatF (Ann NatF Nat))) Source # | |
Defined in Fcf.Alg.Tree | |
type Eval (Attr (Fix (AnnF ((,) _ a2))) :: a1 -> Type) Source # | |
type Eval (RecNTF n :: Fix NatF -> Type) Source # | |
type Eval (NatToFix n :: Fix NatF -> Type) Source # | |
type Eval (DedupAlg (ConsF ((,) a2 as) ((,) _fxs past)) :: [a1] -> Type) Source # | |
type Eval (DedupAlg (NilF :: ListF (a, [a]) (Fix (ListF (a, [a])), [a])) :: [a] -> Type) Source # | |
type Eval (EvensStrip (ConsF x y) :: [a] -> Type) Source # | |
Defined in Fcf.Alg.List | |
type Eval (EvensStrip (NilF :: ListF a (Ann (ListF a) [a])) :: [a] -> Type) Source # | |
Defined in Fcf.Alg.List | |
type Eval (EvensAlg (ConsF _ rst) :: [a] -> Type) Source # | |
type Eval (EvensAlg (NilF :: ListF a (Ann (ListF a) [a])) :: [a] -> Type) Source # | |
type Eval (ListToParaFix (a2 ': as) :: Fix (ListF (a1, [a1])) -> Type) Source # | |
Defined in Fcf.Alg.List type Eval (ListToParaFix (a2 ': as) :: Fix (ListF (a1, [a1])) -> Type) = Fix (ConsF ((,) a2 as) (Eval (ListToParaFix as))) | |
type Eval (ListToParaFix ([] :: [a]) :: Fix (ListF (a, [a])) -> Type) Source # | |
type Eval (ListToFix (a2 ': as) :: Fix (ListF a1) -> Type) Source # | |
type Eval (ListToFix ([] :: [a]) :: Fix (ListF a) -> Type) Source # | |
type Eval (TreeToFix (Node a2 (b ': bs)) :: Fix (TreeF a1) -> Type) Source # | |
type Eval (TreeToFix (Node a2 ([] :: [Tree a1])) :: Fix (TreeF a1) -> Type) Source # | |
type Eval (SlidingAlg n (ConsF ((,) a2 as) ((,) _fxs past)) :: [[a1]] -> Type) Source # | |
Defined in Fcf.Alg.List | |
type Eval (SlidingAlg _ (NilF :: ListF (a, [a]) (Fix (ListF (a, [a])), [[a]])) :: [[a]] -> Type) Source # | |
Defined in Fcf.Alg.List | |
type Eval (Strip (Fix (AnnF ((,) x _))) :: f (Ann f a) -> Type) Source # | |
type Eval (AnnConstr fxp :: Fix (AnnF f a) -> Type) Source # | |
type Eval (Ana coalg a2 :: Fix f -> Type) Source # | |
type Eval (Sizes fx :: Ann f Nat -> Type) Source # | Sizes example from Recursion Schemes by example, Tim Williams. This annotes each node with the size of its subtree. Example
|
type Eval (Fanout ralg (Fix f2) :: (Fix f1, k) -> Type) Source # | |
type Eval (SynthAlg alg faf :: Ann f a -> Type) Source # | |
type Eval (Synthesize f2 fx :: Ann f1 a -> Type) Source # | |
Defined in Fcf.Alg.Morphism | |
type Eval (HistoAlg alg faf :: Ann f a -> Type) Source # | |
type RAlgebra f a = f (Fix f, a) -> Exp a Source #
Commonly used name describing the method Para
eats.
data Ana :: CoAlgebra f a -> a -> Exp (Fix f) Source #
Ana can also be used to build a Fix
structure.
Example
>>>
data NToOneCoA :: CoAlgebra (ListF Nat) Nat
>>>
:{
type instance Eval (NToOneCoA b) = If (Eval (b < 1) ) 'NilF ('ConsF b ( b TL.- 1)) :}
>>>
:kind! Eval (Ana NToOneCoA 3)
Eval (Ana NToOneCoA 3) :: Fix (ListF Nat) = 'Fix ('ConsF 3 ('Fix ('ConsF 2 ('Fix ('ConsF 1 ('Fix 'NilF))))))
data Hylo :: Algebra f a -> CoAlgebra f b -> b -> Exp a Source #
Hylomorphism uses first Ana
to build a structure (unfold) and then Cata
to process the structure (fold).
Example
>>>
data NToOneCoA :: CoAlgebra (ListF Nat) Nat
>>>
:{
type instance Eval (NToOneCoA b) = If (Eval (b < 1) ) 'NilF ('ConsF b ( b TL.- 1)) :}
>>>
:kind! Eval (Hylo SumAlg NToOneCoA 5)
Eval (Hylo SumAlg NToOneCoA 5) :: Nat = 15
Annotate (f r) with attribute a (from Recursion Schemes by example, Tim Williams).
AnnF (f r, a) |
Instances
type Eval (FibAlgebra (Succ (Fix (AnnF ((,) (Succ (Fix (AnnF ((,) _ n)))) m)))) :: Nat -> Type) Source # | |
type Eval (FibAlgebra (Succ (Fix (AnnF ((,) (Zero :: NatF (Fix (AnnF NatF Nat))) _)))) :: Nat -> Type) Source # | |
type Eval (FibAlgebra (Zero :: NatF (Ann NatF Nat))) Source # | |
Defined in Fcf.Alg.Tree | |
type Eval (Attr (Fix (AnnF ((,) _ a2))) :: a1 -> Type) Source # | |
type Eval (EvensStrip (ConsF x y) :: [a] -> Type) Source # | |
Defined in Fcf.Alg.List | |
type Eval (EvensStrip (NilF :: ListF a (Ann (ListF a) [a])) :: [a] -> Type) Source # | |
Defined in Fcf.Alg.List | |
type Eval (EvensAlg (ConsF _ rst) :: [a] -> Type) Source # | |
type Eval (EvensAlg (NilF :: ListF a (Ann (ListF a) [a])) :: [a] -> Type) Source # | |
type Eval (Strip (Fix (AnnF ((,) x _))) :: f (Ann f a) -> Type) Source # | |
type Eval (AnnConstr fxp :: Fix (AnnF f a) -> Type) Source # | |
type Eval (Sizes fx :: Ann f Nat -> Type) Source # | Sizes example from Recursion Schemes by example, Tim Williams. This annotes each node with the size of its subtree. Example
|
type Eval (SynthAlg alg faf :: Ann f a -> Type) Source # | |
type Eval (Synthesize f2 fx :: Ann f1 a -> Type) Source # | |
Defined in Fcf.Alg.Morphism | |
type Eval (HistoAlg alg faf :: Ann f a -> Type) Source # | |
type Ann f a = Fix (AnnF f a) Source #
Annotated fixed-point type. A cofree comonad (from Recursion Schemes by example, Tim Williams).
data Attr :: Ann f a -> Exp a Source #
Attribute of the root node (from Recursion Schemes by example, Tim Williams).
data Strip :: Ann f a -> Exp (f (Ann f a)) Source #
Strip attribute from root (from Recursion Schemes by example, Tim Williams).
data AnnConstr :: (f (Ann f a), a) -> Exp (Fix (AnnF f a)) Source #
Annotation constructor (from Recursion Schemes by example, Tim Williams).
data SynthAlg :: (f a -> Exp a) -> f (Ann f a) -> Exp (Ann f a) Source #
Synthesized attributes are created in a bottom-up traversal using a catamorphism (from Recursion Schemes by example, Tim Williams).
This is the algebra that is fed to the cata.
data Synthesize :: (f a -> Exp a) -> Fix f -> Exp (Ann f a) Source #
Synthesized attributes are created in a bottom-up traversal using a catamorphism (from Recursion Schemes by example, Tim Williams).
For the example, see Fcf.Data.Alg.Tree.Sizes.
Instances
type Eval (Synthesize f2 fx :: Ann f1 a -> Type) Source # | |
Defined in Fcf.Alg.Morphism |
data HistoAlg :: (f (Ann f a) -> Exp a) -> f (Ann f a) -> Exp (Ann f a) Source #
Histo takes annotation algebra and takes a Fix-structure (from Recursion Schemes by example, Tim Williams).
data Histo :: (f (Ann f a) -> Exp a) -> Fix f -> Exp a Source #
Histo takes annotation algebra and takes a Fix-structure (from Recursion Schemes by example, Tim Williams).
Examples can be found from Fcf.Data.Alg.Tree and Fcf.Data.Alg.List modules.
data First :: (a -> Exp b) -> f a c -> Exp (f b c) Source #
Type-level First. Tuples (,)
and Either
have First-instances.
Example
>>>
:kind! Eval (First ((+) 1) '(3,"a"))
Eval (First ((+) 1) '(3,"a")) :: (Nat, TL.Symbol) = '(4, "a")
data Second :: (c -> Exp d) -> f a c -> Exp (f a d) Source #
Type-level Second. Tuples (,)
and Either
have Second-instances.
Example
>>>
:kind! Eval (Second ((+) 1) '("a",3))
Eval (Second ((+) 1) '("a",3)) :: (TL.Symbol, Nat) = '("a", 4)