HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulefcf-containers-0.8.2Haskell2010

Fcf.Alg.Morphism

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.

  • 20 types
newtypenewtype Fix (f :: Type -> Type)
#

Structure that Cata can fold and that is a result structure of Ana.

Constructors

Instances28Eval, …
datadata Cata (b :: Algebra f a) (c :: Fix f) (d :: a)
#

Write the function to give a Fix, and feed it in together with an Algebra.

Check Fcf.Alg.List to see example algebras in use. There we have e.g. ListToFix-function.

Instances1Eval
datadata Ana (b :: CoAlgebra f a) (c :: a) (d :: Fix f)
#

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 TL.Natural) = 'Fix ('ConsF 3 ('Fix ('ConsF 2 ('Fix ('ConsF 1 ('Fix 'NilF))))))

Instances1Eval
  • type Eval (Ana coalg a2) = 'Fix (Eval (Map (Ana coalg) (Eval (coalg a2))))Defined in fcf-containers-0.8.2 · Fcf.Alg.Morphism
datadata Hylo (c :: Algebra f a) (d :: CoAlgebra f b) (e :: b) (g :: a)
#

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) :: TL.Natural = 15

Instances1Eval
  • type Eval (Hylo alg coalg a3) = Eval (Cata alg =<< Ana coalg a3)Defined in fcf-containers-0.8.2 · Fcf.Alg.Morphism
datadata Para (b :: RAlgebra f a) (c :: Fix f) (d :: a)
#

Write a function to give a Fix, and feed it in together with an RAlgebra

Check Fcf.Alg.List to see example algebras in use. There we have e.g. ListToParaFix-function.

Instances1Eval
newtypenewtype AnnF (f :: k -> Type) a (r :: k)
#

Annotate (f r) with attribute a (from Recursion Schemes by example, Tim Williams).

Constructors

Instances14Eval, …
  • type Eval (EvensAlg 'NilF) = '[]Defined in fcf-containers-0.8.2 · Fcf.Alg.List
  • type Eval (EvensAlg ('ConsF _1 rst)) = Eval (EvensStrip =<< Strip rst)Defined in fcf-containers-0.8.2 · Fcf.Alg.List
  • type Eval (EvensStrip 'NilF) = '[]Defined in fcf-containers-0.8.2 · Fcf.Alg.List
  • type Eval (EvensStrip ('ConsF x y)) = x ': Eval (Attr y)Defined in fcf-containers-0.8.2 · Fcf.Alg.List
  • type Eval (AnnConstr fxp) = Eval (Pure ('Fix ('AnnF fxp)))Defined in fcf-containers-0.8.2 · Fcf.Alg.Morphism
  • type Eval (Attr ('Fix ('AnnF '(_1, a2)))) = a2Defined in fcf-containers-0.8.2 · Fcf.Alg.Morphism
  • type Eval (HistoAlg alg faf) = Eval (AnnConstr '(faf, Eval (alg faf)))Defined in fcf-containers-0.8.2 · Fcf.Alg.Morphism
  • type Eval (Strip ('Fix ('AnnF '(x, _1)))) = xDefined in fcf-containers-0.8.2 · Fcf.Alg.Morphism
  • type Eval (SynthAlg alg faf) = Eval (AnnConstr '(faf, Eval (alg =<< Map Attr faf)))Defined in fcf-containers-0.8.2 · Fcf.Alg.Morphism
  • type Eval (Synthesize f2 fx) = Eval (Cata (SynthAlg f2) fx)Defined in fcf-containers-0.8.2 · Fcf.Alg.Morphism
  • type Eval (FibAlgebra 'Zero) = 0Defined in fcf-containers-0.8.2 · Fcf.Alg.Tree
  • type Eval (FibAlgebra ('Succ ('Fix ('AnnF '('Succ ('Fix ('AnnF '(_1, n))), m))))) = Eval (n + m)Defined in fcf-containers-0.8.2 · Fcf.Alg.Tree
  • type Eval (FibAlgebra ('Succ ('Fix ('AnnF '('Zero, _1))))) = 1Defined in fcf-containers-0.8.2 · Fcf.Alg.Tree
  • type Eval (Sizes fx) = Eval (Synthesize ((+) 1 <=< FSum) fx)Defined in fcf-containers-0.8.2 · Fcf.Alg.Tree

    Sizes example from Recursion Schemes by example, Tim Williams. This annotes each node with the size of its subtree.

    Example

    :kind! Eval (Sizes =<< Ana BuildNodeCoA 1)

    Eval (Sizes =<< Ana BuildNodeCoA 1) :: Fix (AnnF (TreeF TL.Natural) TL.Natural) = 'Fix ('AnnF '( 'NodeF 1 '[ 'Fix ('AnnF '( 'NodeF 2 '[ 'Fix ('AnnF '( 'NodeF 4 '[], 1)), 'Fix ('AnnF '( 'NodeF 5 '[], 1))], 3)), 'Fix ('AnnF '( 'NodeF 3 '[ 'Fix ('AnnF '( 'NodeF 6 '[], 1)), 'Fix ('AnnF '( 'NodeF 7 '[], 1))], 3))], 7))

typetype Ann (f :: Type -> Type) a = Fix (AnnF f a)
#

Annotated fixed-point type. A cofree comonad (from Recursion Schemes by example, Tim Williams).

datadata Attr (b :: Ann f a) (c :: a)
#

Attribute of the root node (from Recursion Schemes by example, Tim Williams).

Instances1Eval
  • type Eval (Attr ('Fix ('AnnF '(_1, a2)))) = a2Defined in fcf-containers-0.8.2 · Fcf.Alg.Morphism
datadata Strip (b :: Ann f a) (c :: f (Ann f a))
#

Strip attribute from root (from Recursion Schemes by example, Tim Williams).

Instances1Eval
  • type Eval (Strip ('Fix ('AnnF '(x, _1)))) = xDefined in fcf-containers-0.8.2 · Fcf.Alg.Morphism
datadata AnnConstr (b :: (f (Ann f a), a)) (c :: Fix (AnnF f a))
#

Annotation constructor (from Recursion Schemes by example, Tim Williams).

Instances1Eval
datadata SynthAlg (b :: f a -> Exp a) (c :: f (Ann f a)) (d :: Ann f a)
#

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.

Instances1Eval
datadata Synthesize (b :: f a -> Exp a) (c :: Fix f) (d :: Ann f a)
#

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.

Instances1Eval
datadata HistoAlg (b :: f (Ann f a) -> Exp a) (c :: f (Ann f a)) (d :: Ann f a)
#

Histo takes annotation algebra and takes a Fix-structure (from Recursion Schemes by example, Tim Williams).

This is a helper for Histo as it is implemented with Cata.

Instances1Eval
datadata Histo (b :: f (Ann f a) -> Exp a) (c :: Fix f) (d :: a)
#

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.

Instances1Eval
datadata First (d :: a -> Exp b) (e :: f a c) (g :: f b c)
#

Type-level First. Tuples (,) and Either have First-instances.

Example
:kind! Eval (First ((+) 1) '(3,"a"))

Eval (First ((+) 1) '(3,"a")) :: (TL.Natural, TL.Symbol) = '(4, "a")

Instances3Eval
  • type Eval (First f '(a, b)) = '(f @@ a, b)Defined in fcf-containers-0.8.2 · Fcf.Alg.Morphism
  • type Eval (First f ('Left a2)) = 'Left (f @@ a2)Defined in fcf-containers-0.8.2 · Fcf.Alg.Morphism
  • type Eval (First f ('Right b3)) = 'Right b3Defined in fcf-containers-0.8.2 · Fcf.Alg.Morphism
datadata Second (b :: c -> Exp d) (e :: f a c) (g :: f a d)
#

Type-level Second. Tuples (,) and Either have Second-instances.

Example
:kind! Eval (Second ((+) 1) '("a",3))

Eval (Second ((+) 1) '("a",3)) :: (TL.Symbol, TL.Natural) = '("a", 4)

Instances3Eval
  • type Eval (Second f '(a, b)) = '(a, f @@ b)Defined in fcf-containers-0.8.2 · Fcf.Alg.Morphism
  • type Eval (Second f ('Left a2)) = 'Left a2Defined in fcf-containers-0.8.2 · Fcf.Alg.Morphism
  • type Eval (Second f ('Right b2)) = 'Right (f @@ b2)Defined in fcf-containers-0.8.2 · Fcf.Alg.Morphism