newtype Fix f
A fix-point type.
Fixed points of a functor.
Type f should be a Functor if you want to use
simple recursion schemes or Traversable if you want to
use monadic recursion schemes. This style allows you to express
recursive functions in non-recursive manner.
You can imagine that a non-recursive function
holds values of the previous iteration.
An example:
First we define a base functor. The arguments b are recursion points.
>>> data ListF a b = Nil | Cons a b deriving (Show, Functor)The list is then a fixed point of ListF
>>> type List a = Fix (ListF a)We can write length function. Note that the function we give
to foldFix is not recursive. Instead the results
of recursive calls are in b positions, and we need to deal
only with one layer of the structure.
>>> :{
let length :: List a -> Int
length = foldFix $ \x -> case x of
Nil -> 0
Cons _ n -> n + 1
:}If you already have recursive type, like '[Int]',
you can first convert it to `Fix (ListF a)` and then `foldFix`.
Alternatively you can use recursion-schemes combinators
which work directly on recursive types.
newtype Fix fA fix-point type.
Fix :: f (Fix f) -> Fix funFix :: Fix f -> f (Fix f)hoistFix :: Functor f => (forall a. f a -> g a) -> Fix f -> Fix gChange base functor in Fix.
hoistFix' :: Functor g => (forall a. f a -> g a) -> Fix f -> Fix gLike hoistFix but fmapping over g.
foldFix :: Functor f => (f a -> a) -> Fix f -> aFold Fix.
>>> let fp = unfoldFix (\i -> if i < 4 then Cons i (i + 1) else Nil) (0 :: Int)
>>> foldFix (elimListF 0 (+)) fp
6unfoldFix :: Functor f => (a -> f a) -> a -> Fix fUnfold Fix.
>>> unfoldFix (\i -> if i < 4 then Cons i (i + 1) else Nil) (0 :: Int)
Fix (Cons 0 (Fix (Cons 1 (Fix (Cons 2 (Fix (Cons 3 (Fix Nil))))))))wrapFix :: f (Fix f) -> Fix fWrap Fix.
>>> let x = unfoldFix (\i -> if i < 3 then Cons i (i + 1) else Nil) (0 :: Int)
>>> wrapFix (Cons 10 x)
Fix (Cons 10 (Fix (Cons 0 (Fix (Cons 1 (Fix (Cons 2 (Fix Nil))))))))unwrapFix :: Fix f -> f (Fix f)Unwrap Fix.
>>> let x = unfoldFix (\i -> if i < 3 then Cons i (i + 1) else Nil) (0 :: Int)
>>> unwrapFix x
Cons 0 (Fix (Cons 1 (Fix (Cons 2 (Fix Nil)))))newtype Mu fLeast fixed point. Efficient folding.
Mu :: forall a. (f a -> a) -> a -> Mu funMu :: Mu f -> forall a. (f a -> a) -> ahoistMu :: (forall a. f a -> g a) -> Mu f -> Mu gChange base functor in Mu.
foldMu :: (f a -> a) -> Mu f -> aFold Mu.
>>> let mu = unfoldMu (\i -> if i < 4 then Cons i (i + 1) else Nil) (0 :: Int)
>>> foldMu (elimListF 0 (+)) mu
6unfoldMu :: Functor f => (a -> f a) -> a -> Mu fUnfold Mu.
>>> unfoldMu (\i -> if i < 4 then Cons i (i + 1) else Nil) (0 :: Int)
unfoldMu unFix (Fix (Cons 0 (Fix (Cons 1 (Fix (Cons 2 (Fix (Cons 3 (Fix Nil)))))))))wrapMu :: Functor f => f (Mu f) -> Mu fWrap Mu.
>>> let x = unfoldMu (\i -> if i < 3 then Cons i (i + 1) else Nil) (0 :: Int)
>>> wrapMu (Cons 10 x)
unfoldMu unFix (Fix (Cons 10 (Fix (Cons 0 (Fix (Cons 1 (Fix (Cons 2 (Fix Nil)))))))))unwrapMu :: Functor f => Mu f -> f (Mu f)Unwrap Mu.
>>> let x = unfoldMu (\i -> if i < 3 then Cons i (i + 1) else Nil) (0 :: Int)
>>> unwrapMu x
Cons 0 (unfoldMu unFix (Fix (Cons 1 (Fix (Cons 2 (Fix Nil))))))data Nu fGreatest fixed point. Efficient unfolding.
Nu :: forall a. (a -> f a) -> a -> Nu fhoistNu :: (forall a. f a -> g a) -> Nu f -> Nu gChange base functor in Nu.
foldNu :: Functor f => (f a -> a) -> Nu f -> aFold Nu.
>>> let nu = unfoldNu (\i -> if i < 4 then Cons i (i + 1) else Nil) (0 :: Int)
>>> foldNu (elimListF 0 (+)) nu
6unfoldNu :: (a -> f a) -> a -> Nu fUnfold Nu.
>>> unfoldNu (\i -> if i < 4 then Cons i (i + 1) else Nil) (0 :: Int)
unfoldNu unFix (Fix (Cons 0 (Fix (Cons 1 (Fix (Cons 2 (Fix (Cons 3 (Fix Nil)))))))))wrapNu :: Functor f => f (Nu f) -> Nu fWrap Nu.
>>> let x = unfoldNu (\i -> if i < 3 then Cons i (i + 1) else Nil) (0 :: Int)
>>> wrapNu (Cons 10 x)
unfoldNu unFix (Fix (Cons 10 (Fix (Cons 0 (Fix (Cons 1 (Fix (Cons 2 (Fix Nil)))))))))unwrapNu :: Functor f => Nu f -> f (Nu f)Unwrap Nu.
>>> let x = unfoldNu (\i -> if i < 3 then Cons i (i + 1) else Nil) (0 :: Int)
>>> unwrapNu x
Cons 0 (unfoldNu unFix (Fix (Cons 1 (Fix (Cons 2 (Fix Nil))))))refold :: Functor f => (f b -> b) -> (a -> f a) -> a -> bRefold one recursive type into another, one layer at the time.
foldFixM :: (Monad m, Traversable t) => (t a -> m a) -> Fix t -> m aMonadic foldFix.
unfoldFixM :: (Monad m, Traversable t) => (a -> m (t a)) -> (a -> m (Fix t))Monadic anamorphism.
refoldM :: (Monad m, Traversable t) => (t b -> m b) -> (a -> m (t a)) -> (a -> m b)Monadic hylomorphism.
cata :: Functor f => (f a -> a) -> (Fix f -> a)Use foldFix
Catamorphism or generic function fold.
ana :: Functor f => (a -> f a) -> (a -> Fix f)Use unfoldFix
Anamorphism or generic function unfold.
hylo :: Functor f => (f b -> b) -> (a -> f a) -> (a -> b)Use refold
Hylomorphism is anamorphism followed by catamorphism.
cataM :: (Monad m, Traversable t) => (t a -> m a) -> Fix t -> m aUse foldFixM
Monadic catamorphism.
anaM :: (Monad m, Traversable t) => (a -> m (t a)) -> (a -> m (Fix t))Use unfoldFixM
Monadic anamorphism.
hyloM :: (Monad m, Traversable t) => (t b -> m b) -> (a -> m (t a)) -> (a -> m b)Use refoldM
Monadic hylomorphism.
instance Generic f => Generic (Fix f)instance Eq1 f => Eq (Fix f)instance Ord1 f => Ord (Fix f)instance Show1 f => Show (Fix f)instance Read1 f => Read (Fix f)instance Hashable1 f => Hashable (Fix f)instance NFData1 f => NFData (Fix f)instance Typeable Fixinstance (Typeable f, Data (f (Fix f))) => Data (Fix f)instance (Functor f, Eq1 f) => Eq (Mu f)instance (Functor f, Ord1 f) => Ord (Mu f)instance (Functor f, Show1 f) => Show (Mu f)instance (Functor f, Read1 f) => Read (Mu f)instance (Functor f, Eq1 f) => Eq (Nu f)instance (Functor f, Ord1 f) => Ord (Nu f)instance (Functor f, Show1 f) => Show (Nu f)instance (Functor f, Read1 f) => Read (Nu f)Community contribution. The service has not verified this documentation.
Target: Linux on x86-64. Some inferred types and resolved links are not available.
Generator: aihc-haddock-aa6debbd-web-3
Metadata snapshot: 6dd1de75214e2866e5532778b21a2471837dea5598a73e7199cb040aadd48ecd
Open the dependency plan · Download the documentation model