Copyright | (c) gspia 2020- |
---|---|
License | BSD |
Maintainer | gspia |
Safe Haskell | Safe |
Language | Haskell2010 |
Fcf.Alg.List
Type-level ListF
to be used with Cata, Ana and Hylo.
This module also contains other list-related functions (that might move to other place some day).
Synopsis
- data ListF a b
- data ListToFix :: [a] -> Exp (Fix (ListF a))
- data LenAlg :: Algebra (ListF a) Nat
- data SumAlg :: Algebra (ListF Nat) Nat
- data ProdAlg :: Algebra (ListF Nat) Nat
- data ListToParaFix :: [a] -> Exp (Fix (ListF (a, [a])))
- data DedupAlg :: RAlgebra (ListF (a, [a])) [a]
- data Sliding :: Nat -> [a] -> Exp [[a]]
- data SlidingAlg :: Nat -> RAlgebra (ListF (a, [a])) [[a]]
- data EvensStrip :: ListF a (Ann (ListF a) [a]) -> Exp [a]
- data EvensAlg :: ListF a (Ann (ListF a) [a]) -> Exp [a]
- data Evens :: [a] -> Exp [a]
- data NumIter :: a -> Nat -> Exp (Maybe (a, Nat))
- data ListRunAlg :: Nat -> Exp (Maybe (Nat, Nat))
- data RunInc :: Nat -> Exp [Nat]
- data Sum :: [Nat] -> Exp Nat
- data ToList :: a -> Exp [a]
- data Equal :: [a] -> [a] -> Exp Bool
Documentation
>>>
import Fcf.Combinators
Base functor for a list of type [a]
.
Instances
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 (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 (FMap f (ConsF a3 b2) :: ListF a2 b1 -> Type) Source # | |
type Eval (FMap f (NilF :: ListF a2 a1) :: ListF a2 b -> Type) Source # | |
data ListToFix :: [a] -> Exp (Fix (ListF a)) Source #
ListToFix can be used to turn a norma type-level list into the base
functor type ListF, to be used with e.g. Cata. For examples in use, see
LenAlg
and SumAlg
.
Ideally, we would have one ToFix type-level function for which we could give type instances for different type-level types, like lists, trees etc. See TODO.md.
Example
>>>
:kind! Eval (ListToFix '[1,2,3])
Eval (ListToFix '[1,2,3]) :: Fix (ListF Nat) = 'Fix ('ConsF 1 ('Fix ('ConsF 2 ('Fix ('ConsF 3 ('Fix 'NilF))))))
data LenAlg :: Algebra (ListF a) Nat Source #
Example algebra to calculate list length.
Example
>>>
:kind! Eval (Cata LenAlg =<< ListToFix '[1,2,3])
Eval (Cata LenAlg =<< ListToFix '[1,2,3]) :: Nat = 3
data SumAlg :: Algebra (ListF Nat) Nat Source #
Example algebra to calculate the sum of Nats in a list.
Example
>>>
:kind! Eval (Cata SumAlg =<< ListToFix '[1,2,3,4])
Eval (Cata SumAlg =<< ListToFix '[1,2,3,4]) :: Nat = 10
data ProdAlg :: Algebra (ListF Nat) Nat Source #
Example algebra to calculate the prod of Nats in a list.
Example
>>>
:kind! Eval (Cata ProdAlg =<< ListToFix '[1,2,3,4])
Eval (Cata ProdAlg =<< ListToFix '[1,2,3,4]) :: Nat = 24
data ListToParaFix :: [a] -> Exp (Fix (ListF (a, [a]))) Source #
Form a Fix-structure that can be used with Para.
Example
>>>
:kind! Eval (ListToParaFix '[1,2,3])
Eval (ListToParaFix '[1,2,3]) :: Fix (ListF (Nat, [Nat])) = 'Fix ('ConsF '(1, '[2, 3]) ('Fix ('ConsF '(2, '[3]) ('Fix ('ConsF '(3, '[]) ('Fix 'NilF))))))
Instances
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 # | |
data DedupAlg :: RAlgebra (ListF (a, [a])) [a] Source #
Example from recursion-package by Vanessa McHale.
This removes duplicates from a list (by keeping the right-most one).
Example
>>>
:kind! Eval (Para DedupAlg =<< ListToParaFix '[1,1,3,2,5,1,3,2])
Eval (Para DedupAlg =<< ListToParaFix '[1,1,3,2,5,1,3,2]) :: [Nat] = '[5, 1, 3, 2]
data Sliding :: Nat -> [a] -> Exp [[a]] Source #
Example from Recursion Schemes by example by Tim Williams.
Example
>>>
:kind! Eval (Sliding 3 '[1,2,3,4,5,6])
Eval (Sliding 3 '[1,2,3,4,5,6]) :: [[Nat]] = '[ '[1, 2, 3], '[2, 3, 4], '[3, 4, 5], '[4, 5, 6], '[5, 6], '[6]]
data SlidingAlg :: Nat -> RAlgebra (ListF (a, [a])) [[a]] Source #
Tim Williams, Recursion Schemes by example, example for Para.
See Sliding
-function.
Instances
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 |
data EvensStrip :: ListF a (Ann (ListF a) [a]) -> Exp [a] Source #
Tim Williams, Recursion Schemes by example, example for Histo.
Instances
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 |
data EvensAlg :: ListF a (Ann (ListF a) [a]) -> Exp [a] Source #
Tim Williams, Recursion Schemes by example, example for Histo.
data Evens :: [a] -> Exp [a] Source #
This picks up the elements on even positions on a list and is an example on how to use Histo. This example is from Tim Williams, Recursion Schemes by example.
Example
>>>
:kind! Eval (Evens =<< RunInc 8)
Eval (Evens =<< RunInc 8) :: [Nat] = '[2, 4, 6, 8]
data RunInc :: Nat -> Exp [Nat] Source #
Construct a run (that is, a natuaral number sequence from 1 to arg).
Example
>>>
:kind! Eval (RunInc 8)
Eval (RunInc 8) :: [Nat] = '[1, 2, 3, 4, 5, 6, 7, 8]
data Sum :: [Nat] -> Exp Nat Source #
Sum a Nat-list.
Example
>>>
:kind! Eval (Sum '[1,2,3])
Eval (Sum '[1,2,3]) :: Nat = 6
data ToList :: a -> Exp [a] Source #
ToList for type-level lists.
Example
>>>
:kind! Eval (ToList 1)
Eval (ToList 1) :: [Nat] = '[1]