yaya-0.4.2.1: Total recursion schemes.
Safe HaskellSafe-Inferred
LanguageHaskell2010

Yaya.Fold

Synopsis

Documentation

type Algebra c f a = f a `c` a Source #

type GAlgebra c w f a = f (w a) `c` a Source #

type ElgotAlgebra c w f a = w (f a) `c` a Source #

type AlgebraM c m f a = f a `c` m a Source #

type GAlgebraM c m w f a = f (w a) `c` m a Source #

type ElgotAlgebraM c m w f a = w (f a) `c` m a Source #

type Coalgebra c f a = a `c` f a Source #

type GCoalgebra c m f a = a `c` f (m a) Source #

type ElgotCoalgebra c m f a = a `c` m (f a) Source #

type CoalgebraM c m f a = a `c` m (f a) Source #

Note that using a CoalgebraM “directly” is partial (e.g., with anaM). However, ana . Compose can accept a CoalgebraM and produce something like an effectful stream.

type GCoalgebraM c m n f a = a `c` m (f (n a)) Source #

class Projectable c t f | t -> f where Source #

This type class is lawless on its own, but there exist types that can’t implement the corresponding embed operation. Laws are induced by implementing either Steppable (which extends this) or Corecursive (which doesn’t).

Methods

project :: Coalgebra c f t Source #

Instances

Instances details
Projectable ((->) :: Type -> Type -> Type) Natural Maybe Source # 
Instance details

Defined in Yaya.Fold

Projectable ((->) :: Type -> Type -> Type) Void Identity Source # 
Instance details

Defined in Yaya.Fold

Functor f => Projectable ((->) :: Type -> Type -> Type) (Nu f :: Type) (f :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

project :: Coalgebra (->) f (Nu f) Source #

Functor f => Projectable ((->) :: Type -> Type -> Type) (Mu f :: Type) (f :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

project :: Coalgebra (->) f (Mu f) Source #

Projectable ((->) :: Type -> Type -> Type) (Fix f :: Type) (f :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold.Native

Methods

project :: Coalgebra (->) f (Fix f) Source #

Projectable ((->) :: Type -> Type -> Type) ([a] :: Type) (XNor a :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

project :: Coalgebra (->) (XNor a) [a] Source #

Projectable ((->) :: Type -> Type -> Type) (NonEmpty a :: Type) (AndMaybe a :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

project :: Coalgebra (->) (AndMaybe a) (NonEmpty a) Source #

Projectable ((->) :: Type -> Type -> Type) (Maybe a :: Type) (Const (Maybe a) :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

project :: Coalgebra (->) (Const (Maybe a)) (Maybe a) Source #

Projectable ((->) :: Type -> Type -> Type) (Either a b :: Type) (Const (Either a b) :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

project :: Coalgebra (->) (Const (Either a b)) (Either a b) Source #

Projectable ((->) :: Type -> Type -> Type) (Cofree f a :: Type) (EnvT a f :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

project :: Coalgebra (->) (EnvT a f) (Cofree f a) Source #

Projectable ((->) :: Type -> Type -> Type) (Free f a :: Type) (FreeF f a :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

project :: Coalgebra (->) (FreeF f a) (Free f a) Source #

class Projectable c t f => Steppable c t f | t -> f where Source #

Structures you can walk through step-by-step.

Methods

embed :: Algebra c f t Source #

Instances

Instances details
Steppable ((->) :: Type -> Type -> Type) Natural Maybe Source # 
Instance details

Defined in Yaya.Fold

Steppable ((->) :: Type -> Type -> Type) Void Identity Source # 
Instance details

Defined in Yaya.Fold

Functor f => Steppable ((->) :: Type -> Type -> Type) (Nu f :: Type) (f :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

embed :: Algebra (->) f (Nu f) Source #

Functor f => Steppable ((->) :: Type -> Type -> Type) (Mu f :: Type) (f :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

embed :: Algebra (->) f (Mu f) Source #

Steppable ((->) :: Type -> Type -> Type) (Fix f :: Type) (f :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold.Native

Methods

embed :: Algebra (->) f (Fix f) Source #

Steppable ((->) :: Type -> Type -> Type) ([a] :: Type) (XNor a :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

embed :: Algebra (->) (XNor a) [a] Source #

Steppable ((->) :: Type -> Type -> Type) (NonEmpty a :: Type) (AndMaybe a :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

embed :: Algebra (->) (AndMaybe a) (NonEmpty a) Source #

Steppable ((->) :: Type -> Type -> Type) (Maybe a :: Type) (Const (Maybe a) :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

embed :: Algebra (->) (Const (Maybe a)) (Maybe a) Source #

Steppable ((->) :: Type -> Type -> Type) (Either a b :: Type) (Const (Either a b) :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

embed :: Algebra (->) (Const (Either a b)) (Either a b) Source #

Steppable ((->) :: Type -> Type -> Type) (Cofree f a :: Type) (EnvT a f :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

embed :: Algebra (->) (EnvT a f) (Cofree f a) Source #

Steppable ((->) :: Type -> Type -> Type) (Free f a :: Type) (FreeF f a :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

embed :: Algebra (->) (FreeF f a) (Free f a) Source #

class Recursive c t f | t -> f where Source #

Inductive structures that can be reasoned about in the way we usually do – with pattern matching.

Methods

cata :: Algebra c f a -> t `c` a Source #

Instances

Instances details
Recursive ((->) :: Type -> Type -> Type) Natural Maybe Source # 
Instance details

Defined in Yaya.Fold.Native

Methods

cata :: forall (a :: k). Algebra (->) Maybe a -> Natural -> a Source #

Recursive ((->) :: Type -> Type -> Type) Void Identity Source # 
Instance details

Defined in Yaya.Fold

Methods

cata :: forall (a :: k). Algebra (->) Identity a -> Void -> a Source #

Recursive ((->) :: Type -> Type -> Type) (Mu f :: Type) (f :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

cata :: forall (a :: k). Algebra (->) f a -> Mu f -> a Source #

Recursive ((->) :: Type -> Type -> Type) (Maybe a :: Type) (Const (Maybe a) :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

cata :: forall (a0 :: k). Algebra (->) (Const (Maybe a)) a0 -> Maybe a -> a0 Source #

Recursive ((->) :: Type -> Type -> Type) (Either a b :: Type) (Const (Either a b) :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

cata :: forall (a0 :: k). Algebra (->) (Const (Either a b)) a0 -> Either a b -> a0 Source #

class Corecursive c t f | t -> f where Source #

Coinductive (potentially-infinite) structures that guarantee _productivity_ rather than termination.

Methods

ana :: Coalgebra c f a -> a `c` t Source #

Instances

Instances details
Corecursive ((->) :: Type -> Type -> Type) (Nu f :: Type) (f :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

ana :: forall (a :: k). Coalgebra (->) f a -> a -> Nu f Source #

Functor f => Corecursive ((->) :: Type -> Type -> Type) (Fix f :: Type) (f :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold.Native

Methods

ana :: forall (a :: k). Coalgebra (->) f a -> a -> Fix f Source #

Corecursive ((->) :: Type -> Type -> Type) ([a] :: Type) (XNor a :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold.Native

Methods

ana :: forall (a0 :: k). Coalgebra (->) (XNor a) a0 -> a0 -> [a] Source #

Corecursive ((->) :: Type -> Type -> Type) (NonEmpty a :: Type) (AndMaybe a :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold.Native

Methods

ana :: forall (a0 :: k). Coalgebra (->) (AndMaybe a) a0 -> a0 -> NonEmpty a Source #

Corecursive ((->) :: Type -> Type -> Type) (Maybe a :: Type) (Const (Maybe a) :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

ana :: forall (a0 :: k). Coalgebra (->) (Const (Maybe a)) a0 -> a0 -> Maybe a Source #

Corecursive ((->) :: Type -> Type -> Type) (Either a b :: Type) (Const (Either a b) :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

ana :: forall (a0 :: k). Coalgebra (->) (Const (Either a b)) a0 -> a0 -> Either a b Source #

Functor f => Corecursive ((->) :: Type -> Type -> Type) (Cofree f a :: Type) (EnvT a f :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold.Native

Methods

ana :: forall (a0 :: k). Coalgebra (->) (EnvT a f) a0 -> a0 -> Cofree f a Source #

Functor f => Corecursive ((->) :: Type -> Type -> Type) (Free f a :: Type) (FreeF f a :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold.Native

Methods

ana :: forall (a0 :: k). Coalgebra (->) (FreeF f a) a0 -> a0 -> Free f a Source #

recursiveEq :: (Recursive (->) t f, Steppable (->) u f, Functor f, Foldable f, Eq1 f) => t -> u -> Bool Source #

An implementation of Eq for any Recursive instance. Note that this is actually more general than Eq, as it can compare between different fixed-point representations of the same functor.

recursiveShowsPrec :: (Recursive (->) t f, Show1 f) => Int -> t -> ShowS Source #

An implementation of Show for any Recursive instance.

newtype Mu f Source #

A fixed-point operator for inductive / finite data structures.

  • NB*: This is only guaranteed to be finite when f a is strict in a (having strict functors won't prevent Nu from being lazy). Using -XStrictData can help with this a lot.

Constructors

Mu (forall a. Algebra (->) f a -> a) 

Instances

Instances details
DFunctor Mu Source # 
Instance details

Defined in Yaya.Fold

Methods

dmap :: (forall x. f x -> g x) -> Mu f -> Mu g Source #

(Functor f, Foldable f, Eq1 f) => Eq (Mu f) Source # 
Instance details

Defined in Yaya.Fold

Methods

(==) :: Mu f -> Mu f -> Bool #

(/=) :: Mu f -> Mu f -> Bool #

Show1 f => Show (Mu f) Source # 
Instance details

Defined in Yaya.Fold

Methods

showsPrec :: Int -> Mu f -> ShowS #

show :: Mu f -> String #

showList :: [Mu f] -> ShowS #

Recursive ((->) :: Type -> Type -> Type) (Mu f :: Type) (f :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

cata :: forall (a :: k). Algebra (->) f a -> Mu f -> a Source #

Functor f => Projectable ((->) :: Type -> Type -> Type) (Mu f :: Type) (f :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

project :: Coalgebra (->) f (Mu f) Source #

Functor f => Steppable ((->) :: Type -> Type -> Type) (Mu f :: Type) (f :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

embed :: Algebra (->) f (Mu f) Source #

data Nu f where Source #

A fixed-point operator for coinductive / potentially-infinite data structures.

Constructors

Nu :: Coalgebra (->) f a -> a -> Nu f 

Instances

Instances details
DFunctor Nu Source # 
Instance details

Defined in Yaya.Fold

Methods

dmap :: (forall x. f x -> g x) -> Nu f -> Nu g Source #

Corecursive ((->) :: Type -> Type -> Type) (Nu f :: Type) (f :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

ana :: forall (a :: k). Coalgebra (->) f a -> a -> Nu f Source #

Functor f => Projectable ((->) :: Type -> Type -> Type) (Nu f :: Type) (f :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

project :: Coalgebra (->) f (Nu f) Source #

Functor f => Steppable ((->) :: Type -> Type -> Type) (Nu f :: Type) (f :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

embed :: Algebra (->) f (Nu f) Source #

zipAlgebras :: Functor f => Algebra (->) f a -> Algebra (->) f b -> Algebra (->) f (a, b) Source #

Combines two Algebras with different carriers into a single tupled Algebra.

zipAlgebraMs :: (Applicative m, Functor f) => AlgebraM (->) m f a -> AlgebraM (->) m f b -> AlgebraM (->) m f (a, b) Source #

Combines two AlgebraMs with different carriers into a single tupled AlgebraM.

lowerDay :: Projectable (->) t g => Algebra (->) (Day f g) a -> Algebra (->) f (t -> a) Source #

Algebras over Day convolution are convenient for binary operations, but aren’t directly handleable by cata.

cata2 :: (Recursive (->) t f, Projectable (->) u g) => Algebra (->) (Day f g) a -> t -> u -> a Source #

By analogy with liftA2 (which also relies on Day, at least conceptually).

lowerAlgebra :: (Functor f, Comonad w) => DistributiveLaw (->) f w -> GAlgebra (->) w f a -> Algebra (->) f (w a) Source #

Makes it possible to provide a GAlgebra to cata.

lowerAlgebraM :: (Applicative m, Traversable f, Comonad w, Traversable w) => DistributiveLaw (->) f w -> GAlgebraM (->) m w f a -> AlgebraM (->) m f (w a) Source #

Makes it possible to provide a GAlgebraM to cataM.

lowerCoalgebra :: (Functor f, Monad m) => DistributiveLaw (->) m f -> GCoalgebra (->) m f a -> Coalgebra (->) f (m a) Source #

Makes it possible to provide a GCoalgebra to ana.

lowerCoalgebraM :: (Applicative m, Traversable f, Monad n, Traversable n) => DistributiveLaw (->) n f -> GCoalgebraM (->) m n f a -> CoalgebraM (->) m f (n a) Source #

Makes it possible to provide a GCoalgebraM to anaM.

gcata :: (Recursive (->) t f, Functor f, Comonad w) => DistributiveLaw (->) f w -> GAlgebra (->) w f a -> t -> a Source #

elgotCata :: (Recursive (->) t f, Functor f, Comonad w) => DistributiveLaw (->) f w -> ElgotAlgebra (->) w f a -> t -> a Source #

gcataM :: (Monad m, Recursive (->) t f, Traversable f, Comonad w, Traversable w) => DistributiveLaw (->) f w -> GAlgebraM (->) m w f a -> t -> m a Source #

elgotCataM :: (Monad m, Recursive (->) t f, Traversable f, Comonad w, Traversable w) => DistributiveLaw (->) f w -> ElgotAlgebraM (->) m w f a -> t -> m a Source #

ezygoM :: (Monad m, Recursive (->) t f, Traversable f) => AlgebraM (->) m f b -> ElgotAlgebraM (->) m ((,) b) f a -> t -> m a Source #

gana :: (Corecursive (->) t f, Functor f, Monad m) => DistributiveLaw (->) m f -> GCoalgebra (->) m f a -> a -> t Source #

elgotAna :: (Corecursive (->) t f, Functor f, Monad m) => DistributiveLaw (->) m f -> ElgotCoalgebra (->) m f a -> a -> t Source #

lambek :: (Steppable (->) t f, Recursive (->) t f, Functor f) => Coalgebra (->) f t Source #

colambek :: (Projectable (->) t f, Corecursive (->) t f, Functor f) => Algebra (->) f t Source #

type DistributiveLaw c f g = forall a. f (g a) `c` g (f a) Source #

There are a number of distributive laws, including sequenceA, distribute, and sequenceL. Yaya also provides others for specific recursion schemes.

distIdentity :: Functor f => DistributiveLaw (->) f Identity Source #

A less-constrained distribute for Identity.

seqIdentity :: Functor f => DistributiveLaw (->) Identity f Source #

A less-constrained sequenceA for Identity.

distTuple :: Functor f => Algebra (->) f a -> DistributiveLaw (->) f ((,) a) Source #

distEnvT :: Functor f => Algebra (->) f a -> DistributiveLaw (->) f w -> DistributiveLaw (->) f (EnvT a w) Source #

seqEither :: Functor f => Coalgebra (->) f a -> DistributiveLaw (->) (Either a) f Source #

attributeAlgebra :: (Steppable (->) t (EnvT a f), Functor f) => Algebra (->) f a -> Algebra (->) f t Source #

Converts an Algebra to one that annotates the tree with the result for each node.

attributeCoalgebra :: Coalgebra (->) f a -> Coalgebra (->) (EnvT a f) a Source #

Converts a Coalgebra to one that annotates the tree with the seed that generated each node.

ignoringAttribute :: Algebra (->) f a -> Algebra (->) (EnvT b f) a Source #

This is just a more obvious name for composing lowerEnvT with your algebra directly.

unFree :: Steppable (->) t f => Algebra (->) (FreeF f t) t Source #

It is somewhat common to have a natural transformation that looks like η :: forall a. f a -> Free g a. This maps naturally to a GCoalgebra (to pass to apo) with η . project, but the desired Algebra is more likely to be cata unFree . η than embed . η. See yaya-streams for some examples of this.

instances for non-recursive types

constCata :: Algebra (->) (Const b) a -> b -> a Source #

constAna :: Coalgebra (->) (Const b) a -> a -> b Source #

Optics

type BialgebraIso f a = Iso' (f a) a Source #

type AlgebraPrism f a = Prism' (f a) a Source #

type CoalgebraPrism f a = Prism' a (f a) Source #

birecursiveIso :: (Recursive (->) t f, Corecursive (->) t f) => BialgebraIso f a -> Iso' t a Source #

recursivePrism :: (Recursive (->) t f, Corecursive (->) t f, Traversable f) => AlgebraPrism f a -> Prism' t a Source #