1
1
mirror of https://github.com/github/semantic.git synced 2024-12-22 22:31:36 +03:00

Add paramorphism example

This commit is contained in:
Rick Winfrey 2016-11-18 17:12:26 -06:00
parent 6a77dbd868
commit 91d9c05c52

View File

@ -97,3 +97,20 @@ stringTermHylo = hylo algebra coalgebra
coalgebra representation = case representation of
"indexed" -> (Range 1 10 .: Category.MethodCall .: RNil) :< Indexed ["leaf"]
_ -> (Range 1 10 .: Category.MethodCall .: RNil) :< Leaf representation
{-
Paramorphism -- primitive recursion that maintains a reference to the original subobject and its computed value.
para :: (Base t (t, a) -> a) -- an algebra that takes a tuple of the last input
-> t -- fixed point
-> a -- result
Paramorphisms work by providing both the original subobject and the value computed recursively from it.
-}
termPara :: Term (Syntax String) (Record '[Range, Category]) -> [(Term (Syntax String) (Record '[Range, Category]), String)]
termPara = para algebra
where
algebra term = case term of
(annotation :< Leaf representation) -> [(cofree (annotation :< Leaf representation), representation)]
(annotation :< Indexed values) -> [(cofree (annotation :< Indexed []), "indexed")] <> (values >>= Prelude.snd) -- (values >>= Prelude.snd) = Prologue.concat $ Prelude.snd <$> values
_ -> [(cofree ((Range 1 10 .: Category.MethodCall .: RNil) :< Leaf "unknown"), "unknown")]