From 29ed06ce9d45d5e1945bc9874e1afeaec4d391e8 Mon Sep 17 00:00:00 2001 From: Rick Winfrey Date: Fri, 18 Nov 2016 11:33:47 -0600 Subject: [PATCH] Add hylomorphism example for Terms --- src/FDoc/RecursionSchemes.hs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/FDoc/RecursionSchemes.hs b/src/FDoc/RecursionSchemes.hs index 918fd8d0d..d67816d92 100644 --- a/src/FDoc/RecursionSchemes.hs +++ b/src/FDoc/RecursionSchemes.hs @@ -71,3 +71,29 @@ termToStringCata = cata algebra (_ :< Leaf value) -> [value] (_ :< Indexed values) -> ["indexed"] <> (Prologue.concat values) _ -> ["unknown"] + +{- +Hylomorphism -- An anamorphism followed by a catamorphism + +hylo :: Functor f => (f b -> b) -- an algebra + -> (a -> f a) -- a coalgebra + -> a -- seed value + -> b -- result + +Hylomorphisms work by first applying a coalgebra (anamorphism) to build up a virtual structure. +An algebra (catamorphism) is applied to this structure. Because of fusion the anamorphism and +catamorphism occur in a single pass. + +The example below shows how our algebra and coalgebra defined in the termToStringCata and stringToTermAna +can be utilized as a hylomorphism. +-} +stringTermHylo :: String -> [String] +stringTermHylo = hylo algebra coalgebra + where + algebra term = case term of + (_ :< Leaf value) -> [value] + (_ :< Indexed values) -> ["indexed"] <> (Prologue.concat values) + _ -> ["unknown"] + coalgebra representation = case representation of + "indexed" -> (Range 1 10 .: Category.MethodCall .: RNil) :< Indexed ["leaf"] + _ -> (Range 1 10 .: Category.MethodCall .: RNil) :< Leaf representation