Description

Functor helpers

Synopsis

# Documentation

transverse :: (Recursive s, Corecursive t, Functor f) => (forall a. Base s (f a) -> f (Base t a)) -> s -> f t #

An effectful version of hoist.

Properties:

transverse sequenceA = pure


Examples:

The weird type of first argument allows user to decide an order of sequencing:

>>> transverse (\x -> print (void x) *> sequence x) "foo" :: IO String
Cons 'f' ()
Cons 'o' ()
Cons 'o' ()
Nil
"foo"

>>> transverse (\x -> sequence x <* print (void x)) "foo" :: IO String
Nil
Cons 'o' ()
Cons 'o' ()
Cons 'f' ()
"foo"


cataA :: Recursive t => (Base t (f a) -> f a) -> t -> f a #

Effectful |fold|.

This is a type specialisation of cata.

An example terminating a recursion immediately:

>>> cataA (\alg -> case alg of { Nil -> pure (); Cons a _ -> Const [a] })  "hello"
Const "h"


zygoHistoPrepro :: (Corecursive t, Recursive t) => (Base t b -> b) -> (forall c. Base t c -> Base t c) -> (Base t (EnvT b (Cofree (Base t)) a) -> a) -> t -> a #

Zygohistomorphic prepromorphisms:

coelgot :: Functor f => ((a, f b) -> b) -> (a -> f a) -> a -> b #

elgot :: Functor f => (f a -> a) -> (b -> Either a (f b)) -> b -> a #

Elgot algebras

mhisto :: (forall y. (y -> c) -> (y -> f y) -> f y -> c) -> Fix f -> c #

Mendler-style course-of-value iteration

mcata :: (forall y. (y -> c) -> f y -> c) -> Fix f -> c #

Mendler-style iteration

gchrono :: (Functor f, Functor w, Functor m, Comonad w, Monad m) => (forall c. f (w c) -> w (f c)) -> (forall c. m (f c) -> f (m c)) -> (f (CofreeT f w b) -> b) -> (a -> f (FreeT f m a)) -> a -> b #

chrono :: Functor f => (f (Cofree f b) -> b) -> (a -> f (Free f a)) -> a -> b #

distGHisto :: (Functor f, Functor h) => (forall b. f (h b) -> h (f b)) -> f (CofreeT f h a) -> CofreeT f h (f a) #

distHisto :: Functor f => f (Cofree f a) -> Cofree f (f a) #

ghisto :: (Recursive t, Comonad w) => (forall b. Base t (w b) -> w (Base t b)) -> (Base t (CofreeT (Base t) w a) -> a) -> t -> a #

histo :: Recursive t => (Base t (Cofree (Base t) a) -> a) -> t -> a #

Course-of-value iteration

distGApoT :: (Functor f, Functor m) => (b -> f b) -> (forall c. m (f c) -> f (m c)) -> ExceptT b m (f a) -> f (ExceptT b m a) #

distGApo :: Functor f => (b -> f b) -> Either b (f a) -> f (Either b a) #

distApo :: Recursive t => Either t (Base t a) -> Base t (Either t a) #

gapo :: Corecursive t => (b -> Base t b) -> (a -> Base t (Either b a)) -> a -> t #

distZygoT :: (Functor f, Comonad w) => (f b -> b) -> (forall c. f (w c) -> w (f c)) -> f (EnvT b w a) -> EnvT b w (f a) #

gzygo :: (Recursive t, Comonad w) => (Base t b -> b) -> (forall c. Base t (w c) -> w (Base t c)) -> (Base t (EnvT b w a) -> a) -> t -> a #

Arguments

 :: Functor f => (f b -> b) -> f (b, a) A distributive for semi-mutual recursion -> (b, f a)

zygo :: Recursive t => (Base t b -> b) -> (Base t (b, a) -> a) -> t -> a #

hoistNu :: (forall a. f a -> g a) -> Nu f -> Nu g #

A specialized, faster version of hoist for Nu.

hoistMu :: (forall a. f a -> g a) -> Mu f -> Mu g #

A specialized, faster version of hoist for Mu.

refix :: (Recursive s, Corecursive t, Base s ~ Base t) => s -> t #

hoist :: (Recursive s, Corecursive t) => (forall a. Base s a -> Base t a) -> s -> t #

unfix :: Fix f -> f (Fix f) #

distGFutu :: (Functor f, Functor h) => (forall b. h (f b) -> f (h b)) -> FreeT f h (f a) -> f (FreeT f h a) #

distFutu :: Functor f => Free f (f a) -> f (Free f a) #

gfutu :: (Corecursive t, Functor m, Monad m) => (forall b. m (Base t b) -> Base t (m b)) -> (a -> Base t (FreeT (Base t) m a)) -> a -> t #

futu :: Corecursive t => (a -> Base t (Free (Base t) a)) -> a -> t #

grefold :: (Comonad w, Functor f, Monad m) => (forall c. f (w c) -> w (f c)) -> (forall d. m (f d) -> f (m d)) -> (f (w b) -> b) -> (a -> f (m a)) -> a -> b #

A generalized hylomorphism

ghylo :: (Comonad w, Functor f, Monad m) => (forall c. f (w c) -> w (f c)) -> (forall d. m (f d) -> f (m d)) -> (f (w b) -> b) -> (a -> f (m a)) -> a -> b #

A generalized hylomorphism

distAna :: Functor f => Identity (f a) -> f (Identity a) #

Arguments

 :: (Corecursive t, Monad m) => (forall b. m (Base t b) -> Base t (m b)) a distributive law -> (a -> Base t (m a)) a (Base t)-m-coalgebra -> a seed -> t

A generalized anamorphism

Arguments

 :: (Corecursive t, Monad m) => (forall b. m (Base t b) -> Base t (m b)) a distributive law -> (a -> Base t (m a)) a (Base t)-m-coalgebra -> a seed -> t

A generalized anamorphism

distCata :: Functor f => f (Identity a) -> Identity (f a) #

Arguments

 :: (Recursive t, Comonad w) => (forall b. Base t (w b) -> w (Base t b)) a distributive law -> (Base t (w a) -> a) a (Base t)-w-algebra -> t fixed point -> a

A generalized catamorphism

Arguments

 :: (Recursive t, Comonad w) => (forall b. Base t (w b) -> w (Base t b)) a distributive law -> (Base t (w a) -> a) a (Base t)-w-algebra -> t fixed point -> a

A generalized catamorphism

refold :: Functor f => (f b -> b) -> (a -> f a) -> a -> b #

unfold :: Corecursive t => (a -> Base t a) -> a -> t #

fold :: Recursive t => (Base t a -> a) -> t -> a #

hylo :: Functor f => (f b -> b) -> (a -> f a) -> a -> b #

distParaT :: (Corecursive t, Comonad w) => (forall b. Base t (w b) -> w (Base t b)) -> Base t (EnvT t w a) -> EnvT t w (Base t a) #

distPara :: Corecursive t => Base t (t, a) -> (t, Base t a) #

type family Base t :: Type -> Type #

Instances
 type Base Natural Instance detailsDefined in Data.Functor.Foldable type Base Natural = Maybe type Base [a] Instance detailsDefined in Data.Functor.Foldable type Base [a] = ListF a type Base (Maybe a) Example boring stub for non-recursive data types Instance detailsDefined in Data.Functor.Foldable type Base (Maybe a) = (Const (Maybe a) :: Type -> Type) type Base (NonEmpty a) Instance detailsDefined in Data.Functor.Foldable type Base (NonEmpty a) = NonEmptyF a type Base (Fix f) Instance detailsDefined in Data.Functor.Foldable type Base (Fix f) = f type Base (Mu f) Instance detailsDefined in Data.Functor.Foldable type Base (Mu f) = f type Base (Nu f) Instance detailsDefined in Data.Functor.Foldable type Base (Nu f) = f type Base (Either a b) Example boring stub for non-recursive data types Instance detailsDefined in Data.Functor.Foldable type Base (Either a b) = (Const (Either a b) :: Type -> Type) type Base (Cofree f a) Cofree comonads are Recursive/Corecursive Instance detailsDefined in Data.Functor.Foldable type Base (Cofree f a) = CofreeF f a type Base (F f a) Church encoded free monads are Recursive/Corecursive, in the same way that Mu is. Instance detailsDefined in Data.Functor.Foldable type Base (F f a) = FreeF f a type Base (Free f a) Free monads are Recursive/Corecursive Instance detailsDefined in Data.Functor.Foldable type Base (Free f a) = FreeF f a type Base (FreeT f m a) Free transformations of monads are Recursive/Corecursive Instance detailsDefined in Data.Functor.Foldable type Base (FreeT f m a) = Compose m (FreeF f a) type Base (CofreeT f w a) Cofree tranformations of comonads are Recursive/Corecusive Instance detailsDefined in Data.Functor.Foldable type Base (CofreeT f w a) = Compose w (CofreeF f a)

class Functor (Base t) => Recursive t where #

Minimal complete definition

project

Methods

project :: t -> Base t t #

Arguments

 :: (Base t a -> a) a (Base t)-algebra -> t fixed point -> a result

para :: (Base t (t, a) -> a) -> t -> a #

gpara :: (Corecursive t, Comonad w) => (forall b. Base t (w b) -> w (Base t b)) -> (Base t (EnvT t w a) -> a) -> t -> a #

prepro :: Corecursive t => (forall b. Base t b -> Base t b) -> (Base t a -> a) -> t -> a #

Fokkinga's prepromorphism

gprepro :: (Corecursive t, Comonad w) => (forall b. Base t (w b) -> w (Base t b)) -> (forall c. Base t c -> Base t c) -> (Base t (w a) -> a) -> t -> a #

Instances

class Functor (Base t) => Corecursive t where #

Minimal complete definition

embed

Methods

embed :: Base t t -> t #

Arguments

 :: (a -> Base t a) a (Base t)-coalgebra -> a seed -> t resulting fixed point

apo :: (a -> Base t (Either t a)) -> a -> t #

postpro :: Recursive t => (forall b. Base t b -> Base t b) -> (a -> Base t a) -> a -> t #

Fokkinga's postpromorphism

gpostpro :: (Recursive t, Monad m) => (forall b. m (Base t b) -> Base t (m b)) -> (forall c. Base t c -> Base t c) -> (a -> Base t (m a)) -> a -> t #

A generalized postpromorphism

Instances
 Instance detailsDefined in Data.Functor.Foldable Methodsana :: (a -> Base Natural a) -> a -> Natural #apo :: (a -> Base Natural (Either Natural a)) -> a -> Natural #postpro :: Recursive Natural => (forall b. Base Natural b -> Base Natural b) -> (a -> Base Natural a) -> a -> Natural #gpostpro :: (Recursive Natural, Monad m) => (forall b. m (Base Natural b) -> Base Natural (m b)) -> (forall c. Base Natural c -> Base Natural c) -> (a -> Base Natural (m a)) -> a -> Natural # Corecursive [a] Instance detailsDefined in Data.Functor.Foldable Methodsembed :: Base [a] [a] -> [a] #ana :: (a0 -> Base [a] a0) -> a0 -> [a] #apo :: (a0 -> Base [a] (Either [a] a0)) -> a0 -> [a] #postpro :: Recursive [a] => (forall b. Base [a] b -> Base [a] b) -> (a0 -> Base [a] a0) -> a0 -> [a] #gpostpro :: (Recursive [a], Monad m) => (forall b. m (Base [a] b) -> Base [a] (m b)) -> (forall c. Base [a] c -> Base [a] c) -> (a0 -> Base [a] (m a0)) -> a0 -> [a] # Instance detailsDefined in Data.Functor.Foldable Methodsembed :: Base (Maybe a) (Maybe a) -> Maybe a #ana :: (a0 -> Base (Maybe a) a0) -> a0 -> Maybe a #apo :: (a0 -> Base (Maybe a) (Either (Maybe a) a0)) -> a0 -> Maybe a #postpro :: Recursive (Maybe a) => (forall b. Base (Maybe a) b -> Base (Maybe a) b) -> (a0 -> Base (Maybe a) a0) -> a0 -> Maybe a #gpostpro :: (Recursive (Maybe a), Monad m) => (forall b. m (Base (Maybe a) b) -> Base (Maybe a) (m b)) -> (forall c. Base (Maybe a) c -> Base (Maybe a) c) -> (a0 -> Base (Maybe a) (m a0)) -> a0 -> Maybe a # Instance detailsDefined in Data.Functor.Foldable Methodsembed :: Base (NonEmpty a) (NonEmpty a) -> NonEmpty a #ana :: (a0 -> Base (NonEmpty a) a0) -> a0 -> NonEmpty a #apo :: (a0 -> Base (NonEmpty a) (Either (NonEmpty a) a0)) -> a0 -> NonEmpty a #postpro :: Recursive (NonEmpty a) => (forall b. Base (NonEmpty a) b -> Base (NonEmpty a) b) -> (a0 -> Base (NonEmpty a) a0) -> a0 -> NonEmpty a #gpostpro :: (Recursive (NonEmpty a), Monad m) => (forall b. m (Base (NonEmpty a) b) -> Base (NonEmpty a) (m b)) -> (forall c. Base (NonEmpty a) c -> Base (NonEmpty a) c) -> (a0 -> Base (NonEmpty a) (m a0)) -> a0 -> NonEmpty a # Functor f => Corecursive (Fix f) Instance detailsDefined in Data.Functor.Foldable Methodsembed :: Base (Fix f) (Fix f) -> Fix f #ana :: (a -> Base (Fix f) a) -> a -> Fix f #apo :: (a -> Base (Fix f) (Either (Fix f) a)) -> a -> Fix f #postpro :: Recursive (Fix f) => (forall b. Base (Fix f) b -> Base (Fix f) b) -> (a -> Base (Fix f) a) -> a -> Fix f #gpostpro :: (Recursive (Fix f), Monad m) => (forall b. m (Base (Fix f) b) -> Base (Fix f) (m b)) -> (forall c. Base (Fix f) c -> Base (Fix f) c) -> (a -> Base (Fix f) (m a)) -> a -> Fix f # Functor f => Corecursive (Mu f) Instance detailsDefined in Data.Functor.Foldable Methodsembed :: Base (Mu f) (Mu f) -> Mu f #ana :: (a -> Base (Mu f) a) -> a -> Mu f #apo :: (a -> Base (Mu f) (Either (Mu f) a)) -> a -> Mu f #postpro :: Recursive (Mu f) => (forall b. Base (Mu f) b -> Base (Mu f) b) -> (a -> Base (Mu f) a) -> a -> Mu f #gpostpro :: (Recursive (Mu f), Monad m) => (forall b. m (Base (Mu f) b) -> Base (Mu f) (m b)) -> (forall c. Base (Mu f) c -> Base (Mu f) c) -> (a -> Base (Mu f) (m a)) -> a -> Mu f # Functor f => Corecursive (Nu f) Instance detailsDefined in Data.Functor.Foldable Methodsembed :: Base (Nu f) (Nu f) -> Nu f #ana :: (a -> Base (Nu f) a) -> a -> Nu f #apo :: (a -> Base (Nu f) (Either (Nu f) a)) -> a -> Nu f #postpro :: Recursive (Nu f) => (forall b. Base (Nu f) b -> Base (Nu f) b) -> (a -> Base (Nu f) a) -> a -> Nu f #gpostpro :: (Recursive (Nu f), Monad m) => (forall b. m (Base (Nu f) b) -> Base (Nu f) (m b)) -> (forall c. Base (Nu f) c -> Base (Nu f) c) -> (a -> Base (Nu f) (m a)) -> a -> Nu f # Corecursive (Either a b) Instance detailsDefined in Data.Functor.Foldable Methodsembed :: Base (Either a b) (Either a b) -> Either a b #ana :: (a0 -> Base (Either a b) a0) -> a0 -> Either a b #apo :: (a0 -> Base (Either a b) (Either (Either a b) a0)) -> a0 -> Either a b #postpro :: Recursive (Either a b) => (forall b0. Base (Either a b) b0 -> Base (Either a b) b0) -> (a0 -> Base (Either a b) a0) -> a0 -> Either a b #gpostpro :: (Recursive (Either a b), Monad m) => (forall b0. m (Base (Either a b) b0) -> Base (Either a b) (m b0)) -> (forall c. Base (Either a b) c -> Base (Either a b) c) -> (a0 -> Base (Either a b) (m a0)) -> a0 -> Either a b # Functor f => Corecursive (Cofree f a) Instance detailsDefined in Data.Functor.Foldable Methodsembed :: Base (Cofree f a) (Cofree f a) -> Cofree f a #ana :: (a0 -> Base (Cofree f a) a0) -> a0 -> Cofree f a #apo :: (a0 -> Base (Cofree f a) (Either (Cofree f a) a0)) -> a0 -> Cofree f a #postpro :: Recursive (Cofree f a) => (forall b. Base (Cofree f a) b -> Base (Cofree f a) b) -> (a0 -> Base (Cofree f a) a0) -> a0 -> Cofree f a #gpostpro :: (Recursive (Cofree f a), Monad m) => (forall b. m (Base (Cofree f a) b) -> Base (Cofree f a) (m b)) -> (forall c. Base (Cofree f a) c -> Base (Cofree f a) c) -> (a0 -> Base (Cofree f a) (m a0)) -> a0 -> Cofree f a # Functor f => Corecursive (F f a) Instance detailsDefined in Data.Functor.Foldable Methodsembed :: Base (F f a) (F f a) -> F f a #ana :: (a0 -> Base (F f a) a0) -> a0 -> F f a #apo :: (a0 -> Base (F f a) (Either (F f a) a0)) -> a0 -> F f a #postpro :: Recursive (F f a) => (forall b. Base (F f a) b -> Base (F f a) b) -> (a0 -> Base (F f a) a0) -> a0 -> F f a #gpostpro :: (Recursive (F f a), Monad m) => (forall b. m (Base (F f a) b) -> Base (F f a) (m b)) -> (forall c. Base (F f a) c -> Base (F f a) c) -> (a0 -> Base (F f a) (m a0)) -> a0 -> F f a # Functor f => Corecursive (Free f a) It may be better to work with the instance for F directly. Instance detailsDefined in Data.Functor.Foldable Methodsembed :: Base (Free f a) (Free f a) -> Free f a #ana :: (a0 -> Base (Free f a) a0) -> a0 -> Free f a #apo :: (a0 -> Base (Free f a) (Either (Free f a) a0)) -> a0 -> Free f a #postpro :: Recursive (Free f a) => (forall b. Base (Free f a) b -> Base (Free f a) b) -> (a0 -> Base (Free f a) a0) -> a0 -> Free f a #gpostpro :: (Recursive (Free f a), Monad m) => (forall b. m (Base (Free f a) b) -> Base (Free f a) (m b)) -> (forall c. Base (Free f a) c -> Base (Free f a) c) -> (a0 -> Base (Free f a) (m a0)) -> a0 -> Free f a # (Functor m, Functor f) => Corecursive (FreeT f m a) Instance detailsDefined in Data.Functor.Foldable Methodsembed :: Base (FreeT f m a) (FreeT f m a) -> FreeT f m a #ana :: (a0 -> Base (FreeT f m a) a0) -> a0 -> FreeT f m a #apo :: (a0 -> Base (FreeT f m a) (Either (FreeT f m a) a0)) -> a0 -> FreeT f m a #postpro :: Recursive (FreeT f m a) => (forall b. Base (FreeT f m a) b -> Base (FreeT f m a) b) -> (a0 -> Base (FreeT f m a) a0) -> a0 -> FreeT f m a #gpostpro :: (Recursive (FreeT f m a), Monad m0) => (forall b. m0 (Base (FreeT f m a) b) -> Base (FreeT f m a) (m0 b)) -> (forall c. Base (FreeT f m a) c -> Base (FreeT f m a) c) -> (a0 -> Base (FreeT f m a) (m0 a0)) -> a0 -> FreeT f m a # (Functor w, Functor f) => Corecursive (CofreeT f w a) Instance detailsDefined in Data.Functor.Foldable Methodsembed :: Base (CofreeT f w a) (CofreeT f w a) -> CofreeT f w a #ana :: (a0 -> Base (CofreeT f w a) a0) -> a0 -> CofreeT f w a #apo :: (a0 -> Base (CofreeT f w a) (Either (CofreeT f w a) a0)) -> a0 -> CofreeT f w a #postpro :: Recursive (CofreeT f w a) => (forall b. Base (CofreeT f w a) b -> Base (CofreeT f w a) b) -> (a0 -> Base (CofreeT f w a) a0) -> a0 -> CofreeT f w a #gpostpro :: (Recursive (CofreeT f w a), Monad m) => (forall b. m (Base (CofreeT f w a) b) -> Base (CofreeT f w a) (m b)) -> (forall c. Base (CofreeT f w a) c -> Base (CofreeT f w a) c) -> (a0 -> Base (CofreeT f w a) (m a0)) -> a0 -> CofreeT f w a #

newtype Fix (f :: Type -> Type) #

Constructors

 Fix (f (Fix f))
Instances

newtype Mu (f :: Type -> Type) #

Constructors

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