Closed kevinbarabash closed 5 years ago
Hello! RAlgebra
is the same thing as GAlgebra
type RAlgebra f a = Mu f -> f a -> a
-uncurring->
Tuple (Mu f) (f a) -> a
-abstracting over Mu ->
Corecursive t f =>Tuple t (f a) -> a
-use type synonim ->
Corecursive t f => GAlgebra((Tuple t), f a)
BTW you can use the same "catch all case" with GAlgebra
-- using GAlgebra para
removeAddZero :: Expr -> Maybe Expr
removeAddZero = para removeAddZeroF
where
removeAddZeroF :: ExprF (Tuple Expr (Maybe Expr)) -> Maybe Expr
removeAddZeroF (Add args) = do
let
args' = (foldl (\acc x ->
case snd x of
Just y -> case unroll y of
Num 0.0 -> acc
_ -> acc <> [y]
Nothing -> acc
) [] args)
case args' of
[] -> Nothing
[x] -> Just x
xs -> Just (roll $ Add xs)
removeAddZeroF fx = Just $ embed fx
@cryogenian thank you for sharing this. I was struggling to figure out the "catch all case" with GAlgebra. That's super cool that RAlgebra can be written using GAlgebra.
I've been studying a series of blog posts on recursion schemes. The third in the series covers
para
. It shows a different formulation ofpara
using an RAlgebra instead of a GAlgebra like matryoshka'spara
. I find both versions ofpara
are useful in different situations. Here's one example of where the RAlgebra version from the blog post results in less boilerplate:I was wondering if you'd be willing to include
para'
andRAlgebra
as part of matryoshka?