fcf-containers-0.3.0: Data structures and algorithms for first-class-families
Copyright(c) gspia 2020-
LicenseBSD
Maintainergspia
Safe HaskellSafe
LanguageHaskell2010

Fcf.Alg.Tree

Description

Fcf.Alg.Tree

Type-level TreeF and BTreeF to be used with Cata, Ana and Hylo. This also provides some algorithms: general purpose sorting with Qsort, Size of an Tree, Fibonaccis.

Synopsis

Documentation

data TreeF a b Source #

TreeF is functor for Trees. TreeF has Map-instance (on structure).

Constructors

NodeF a [b] 

Instances

Instances details
type Eval (TreeToFix ('Node a2 (b ': bs)) :: Fix (TreeF a1) -> Type) Source # 
Instance details

Defined in Fcf.Alg.Tree

type Eval (TreeToFix ('Node a2 (b ': bs)) :: Fix (TreeF a1) -> Type) = 'Fix ('NodeF a2 (Eval (Map (TreeToFix :: Tree a1 -> Fix (TreeF a1) -> Type) (b ': bs))))
type Eval (TreeToFix ('Node a2 ('[] :: [Tree a1])) :: Fix (TreeF a1) -> Type) Source # 
Instance details

Defined in Fcf.Alg.Tree

type Eval (TreeToFix ('Node a2 ('[] :: [Tree a1])) :: Fix (TreeF a1) -> Type) = 'Fix ('NodeF a2 ('[] :: [Fix (TreeF a1)]))
type Eval (BuildFibTreeCoA n :: TreeF Nat Nat -> Type) Source # 
Instance details

Defined in Fcf.Alg.Tree

type Eval (BuildFibTreeCoA n :: TreeF Nat Nat -> Type) = If (Eval (n >= 2)) ('NodeF 0 '[n - 1, n - 2]) ('NodeF n ('[] :: [Nat]))
type Eval (BuildNodeCoA n :: TreeF Nat Nat -> Type) Source # 
Instance details

Defined in Fcf.Alg.Tree

type Eval (BuildNodeCoA n :: TreeF Nat Nat -> Type) = If (Eval (((2 * n) + 1) >= 8)) ('NodeF n ('[] :: [Nat])) ('NodeF n '[2 * n, (2 * n) + 1])
type Eval (Map f ('NodeF a3 (b2 ': bs)) :: TreeF a2 b1 -> Type) Source # 
Instance details

Defined in Fcf.Alg.Tree

type Eval (Map f ('NodeF a3 (b2 ': bs)) :: TreeF a2 b1 -> Type) = 'NodeF a3 (Eval (Map f (b2 ': bs)))
type Eval (Map f ('NodeF a3 ('[] :: [a1])) :: TreeF a2 b -> Type) Source # 
Instance details

Defined in Fcf.Alg.Tree

type Eval (Map f ('NodeF a3 ('[] :: [a1])) :: TreeF a2 b -> Type) = 'NodeF a3 ('[] :: [b])

data TreeToFix :: Tree a -> Exp (Fix (TreeF a)) Source #

A function to transform a Tree into fixed structure that can be used by Cata and Ana.

See the implementation of Size for an example.

Instances

Instances details
type Eval (TreeToFix ('Node a2 (b ': bs)) :: Fix (TreeF a1) -> Type) Source # 
Instance details

Defined in Fcf.Alg.Tree

type Eval (TreeToFix ('Node a2 (b ': bs)) :: Fix (TreeF a1) -> Type) = 'Fix ('NodeF a2 (Eval (Map (TreeToFix :: Tree a1 -> Fix (TreeF a1) -> Type) (b ': bs))))
type Eval (TreeToFix ('Node a2 ('[] :: [Tree a1])) :: Fix (TreeF a1) -> Type) Source # 
Instance details

Defined in Fcf.Alg.Tree

type Eval (TreeToFix ('Node a2 ('[] :: [Tree a1])) :: Fix (TreeF a1) -> Type) = 'Fix ('NodeF a2 ('[] :: [Fix (TreeF a1)]))

data SumNodesAlg :: Algebra (TreeF Nat) Nat Source #

Sum the nodes of TreeF containing Nats.

See the implementation of Fib for an example.

Instances

Instances details
type Eval (SumNodesAlg ('NodeF x (b ': bs)) :: Nat -> Type) Source # 
Instance details

Defined in Fcf.Alg.Tree

type Eval (SumNodesAlg ('NodeF x (b ': bs)) :: Nat -> Type) = x + Eval (Sum (b ': bs))
type Eval (SumNodesAlg ('NodeF x ('[] :: [Nat])) :: Nat -> Type) Source # 
Instance details

Defined in Fcf.Alg.Tree

type Eval (SumNodesAlg ('NodeF x ('[] :: [Nat])) :: Nat -> Type) = x

data CountNodesAlg :: Algebra (TreeF a) Nat Source #

Count the nodes of TreeF.

See the Size for an example.

Instances

Instances details
type Eval (CountNodesAlg ('NodeF x (b ': bs)) :: Nat -> Type) Source # 
Instance details

Defined in Fcf.Alg.Tree

type Eval (CountNodesAlg ('NodeF x (b ': bs)) :: Nat -> Type) = 1 + Eval (Sum (b ': bs))
type Eval (CountNodesAlg ('NodeF x ('[] :: [Nat])) :: Nat -> Type) Source # 
Instance details

Defined in Fcf.Alg.Tree

type Eval (CountNodesAlg ('NodeF x ('[] :: [Nat])) :: Nat -> Type) = 1

data Size :: Tree a -> Exp Nat Source #

Size of the Tree is the number of nodes in it.

Example

Size is defined as Cata CountNodesAlg =<< TreeToFix tr and can be used with the following.

>>> data BuildNode :: Nat -> Exp (Nat,[Nat])
>>> :{
  type instance Eval (BuildNode x) =
      If (Eval ((2 TL.* x TL.+ 1) >= 8))
          '(x, '[])
          '(x, '[ 2 TL.* x, (2 TL.* x) TL.+ 1 ])
:}
>>> :kind! Eval (Size =<< UnfoldTree BuildNode 1)
Eval (Size =<< UnfoldTree BuildNode 1) :: Nat
= 7

Instances

Instances details
type Eval (Size tr :: Nat -> Type) Source # 
Instance details

Defined in Fcf.Alg.Tree

type Eval (Size tr :: Nat -> Type) = Eval (Cata (CountNodesAlg :: TreeF a Nat -> Nat -> Type) =<< TreeToFix tr)

data BuildNodeCoA :: CoAlgebra (TreeF Nat) Nat Source #

CoAlgebra to build TreeF's. This is an example from containers-package. See Size and example in there.

:kind! Eval (Ana BuildNodeCoA 1) :kind! Eval (Hylo CountNodesAlg BuildNodeCoA 1)

Instances

Instances details
type Eval (BuildNodeCoA n :: TreeF Nat Nat -> Type) Source # 
Instance details

Defined in Fcf.Alg.Tree

type Eval (BuildNodeCoA n :: TreeF Nat Nat -> Type) = If (Eval (((2 * n) + 1) >= 8)) ('NodeF n ('[] :: [Nat])) ('NodeF n '[2 * n, (2 * n) + 1])

data BuildFibTreeCoA :: CoAlgebra (TreeF Nat) Nat Source #

CoAlgebra for the Fib-function.

Instances

Instances details
type Eval (BuildFibTreeCoA n :: TreeF Nat Nat -> Type) Source # 
Instance details

Defined in Fcf.Alg.Tree

type Eval (BuildFibTreeCoA n :: TreeF Nat Nat -> Type) = If (Eval (n >= 2)) ('NodeF 0 '[n - 1, n - 2]) ('NodeF n ('[] :: [Nat]))

data Fib :: Nat -> Exp Nat Source #

Fibonaccis with Hylo, not efficient

Example

>>> :kind! Eval (Fib 10)
Eval (Fib 10) :: Nat
= 55

Instances

Instances details
type Eval (Fib n :: Nat -> Type) Source # 
Instance details

Defined in Fcf.Alg.Tree

data BTreeF a b Source #

BTreeF is a btree functor. At the moment, it is used to build sorting algorithms.

Constructors

BEmptyF 
BNodeF a b b 

Instances

Instances details
type Eval (PartHlp smaller (h ': t) :: BTreeF a [a] -> Type) Source # 
Instance details

Defined in Fcf.Alg.Sort

type Eval (PartHlp smaller (h ': t) :: BTreeF a [a] -> Type) = 'BNodeF h (Eval (Filter (smaller h) t)) (Eval (Filter (Not <=< smaller h) t))
type Eval (PartHlp _1 ('[] :: [a]) :: BTreeF a [a] -> Type) Source # 
Instance details

Defined in Fcf.Alg.Sort

type Eval (PartHlp _1 ('[] :: [a]) :: BTreeF a [a] -> Type) = 'BEmptyF :: BTreeF a [a]
type Eval (PartCmp cmp coalg :: BTreeF a [a] -> Type) Source # 
Instance details

Defined in Fcf.Alg.Sort

type Eval (PartCmp cmp coalg :: BTreeF a [a] -> Type) = Eval (PartHlp (Flip cmp) coalg)
type Eval (Map f ('BNodeF a4 b1 b2) :: BTreeF a3 a2 -> Type) Source # 
Instance details

Defined in Fcf.Alg.Tree

type Eval (Map f ('BNodeF a4 b1 b2) :: BTreeF a3 a2 -> Type) = 'BNodeF a4 (Eval (f b1)) (Eval (f b2))
type Eval (Map f ('BEmptyF :: BTreeF a2 a1) :: BTreeF a2 b -> Type) Source # 
Instance details

Defined in Fcf.Alg.Tree

type Eval (Map f ('BEmptyF :: BTreeF a2 a1) :: BTreeF a2 b -> Type) = 'BEmptyF :: BTreeF a2 b