mirror of
https://github.com/github/semantic.git
synced 2025-01-03 21:16:12 +03:00
Update hylomorphism example to be more interesting
This commit is contained in:
parent
e5f36bb29e
commit
2d0769d0e0
@ -107,10 +107,14 @@ hylo :: Functor f => (f b -> b) -- an algebra
|
|||||||
|
|
||||||
Hylomorphisms work by first applying a coalgebra (anamorphism) to build up a virtual structure.
|
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
|
An algebra (catamorphism) is applied to this structure. Because of fusion the anamorphism and
|
||||||
catamorphism occur in a single pass.
|
catamorphism occur in a single pass rather than two separate traversals.
|
||||||
|
|
||||||
The example below shows how our algebra and coalgebra defined in the termToStringCata and stringToTermAna
|
The example below shows how our algebra and coalgebra defined in the termToStringCata and stringToTermAna
|
||||||
can be utilized as a hylomorphism.
|
can be utilized as a hylomorphism.
|
||||||
|
|
||||||
|
Example Usage:
|
||||||
|
stringTermHylo "indexed" => ["indexed", "leaf1", "leaf2", "leaf3"]
|
||||||
|
|
||||||
-}
|
-}
|
||||||
stringTermHylo :: String -> [String]
|
stringTermHylo :: String -> [String]
|
||||||
stringTermHylo = hylo algebra coalgebra
|
stringTermHylo = hylo algebra coalgebra
|
||||||
@ -119,9 +123,9 @@ stringTermHylo = hylo algebra coalgebra
|
|||||||
(_ :< Leaf value) -> [value]
|
(_ :< Leaf value) -> [value]
|
||||||
(_ :< Indexed values) -> ["indexed"] <> (Prologue.concat values)
|
(_ :< Indexed values) -> ["indexed"] <> (Prologue.concat values)
|
||||||
_ -> ["unknown"]
|
_ -> ["unknown"]
|
||||||
coalgebra representation = case representation of
|
coalgebra stringRepresentation = case stringRepresentation of
|
||||||
"indexed" -> (Range 1 10 .: Category.MethodCall .: RNil) :< Indexed ["leaf"]
|
"indexed" -> (Range 1 10 .: Category.MethodCall .: RNil) :< Indexed ["leaf1", "leaf2", "leaf3"]
|
||||||
_ -> (Range 1 10 .: Category.MethodCall .: RNil) :< Leaf representation
|
_ -> (Range 1 10 .: Category.MethodCall .: RNil) :< Leaf stringRepresentation
|
||||||
|
|
||||||
{-
|
{-
|
||||||
Paramorphism -- primitive recursion that maintains a reference to the original subobject and its computed value.
|
Paramorphism -- primitive recursion that maintains a reference to the original subobject and its computed value.
|
||||||
|
Loading…
Reference in New Issue
Block a user