37 lines
873 B
Haskell
37 lines
873 B
Haskell
{-|
|
|
Module : Data.Functor.Foldable
|
|
Description : Simplified recursion schemes
|
|
Copyright : (c) Mats Rauhala, 2020
|
|
License : BSD-3-Clause
|
|
Maintainer : mats.rauhala@iki.fi
|
|
Stability : experimental
|
|
Portability : POSIX
|
|
|
|
Simplified recursion schemes, providing only the minimal schemes
|
|
-}
|
|
module Data.Functor.Foldable
|
|
( Fix(..)
|
|
, cata
|
|
, ana
|
|
, hylo
|
|
)
|
|
where
|
|
|
|
newtype Fix f = Fix { getFix :: f (Fix f) }
|
|
|
|
-- | Catamorphism or the fold
|
|
--
|
|
-- Fold a recursive structure into a value
|
|
cata :: Functor f => (f a -> a) -> Fix f -> a
|
|
cata f = a where a = f . fmap a . getFix
|
|
|
|
-- | Anamorphism or the unfold
|
|
--
|
|
-- Unfold a seed into a recursive structure
|
|
ana :: Functor f => (a -> f a) -> a -> Fix f
|
|
ana f = a where a = Fix . fmap a . f
|
|
|
|
-- | Combined fold and unfold
|
|
hylo :: (Functor f) => (f a -> a) -> (b -> f b) -> b -> a
|
|
hylo f u = a where a = f . fmap a . u
|