3

Using a histomorphism (histo) from recursion-schemes I can get the a list containing only the odd indexes from an initial list:

import Data.Functor.Foldable

odds :: [a] -> [a]
odds = histo $ \case
  Nil                           -> []
  Cons h (_ :< Nil)             -> [h]
  Cons h (_ :< Cons _ (t :< _)) -> h:t

How can get the same thing using mhisto?

nil      = Fix Nil
cons a b = Fix $ Cons a b
list = cons 1 $ cons 2 $ cons 3 $ nil

modds :: Fix (ListF a) -> [a]
modds = mhisto alg where
   alg _ _ Nil = []
   alg f g (Cons a b) = ?
duplode
  • 33,731
  • 7
  • 79
  • 150
Marius Catalin
  • 349
  • 1
  • 7

1 Answers1

4

This is it:

modds :: Fix (ListF a) -> [a]
modds = mhisto alg
    where
    alg _ _ Nil = []
    alg odd pre (Cons a b) = a : case pre b of
        Nil -> []
        Cons _ b' -> odd b' 
GHCi> list = cata embed [1..10] :: Fix (ListF Int)
GHCi> odds (cata embed list)
[1,3,5,7,9]
GHCi> modds list
[1,3,5,7,9]

odd folds the rest of the list, while pre digs the predecessor. Note how the availability of an y -> f y function in the Mendler algebra mirrors the introduction of Cofree in the ordinary histomorphism algebra (in which digging back can be done by reaching for the tail of the Cofree stream):

cata  :: Functor f => (f c -> c) -> Fix f -> c
histo :: Functor f => (f (Cofree f c) -> c) -> Fix f -> c

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

For further reading on mcata and mhisto, see chapters 5 and 6 of Categorical programming with inductive and coinductive types, by Varmo Vene.

duplode
  • 33,731
  • 7
  • 79
  • 150