git.iko.soy/rowtype-yoga/purescript-backend-optimizer
on main help sign in
35 lines 716 B history raw
1-- @inline Snapshot.RecursionSchemes01.cata arity=2
2-- @inline Snapshot.RecursionSchemes01.functorExprF.map arity=2
3module Snapshot.RecursionSchemes01 (Fix, ExprF, test1, test2) where
5import Prelude
7newtype Fix f = Fix (f (Fix f))
9data ExprF a = Add a a | Mul a a | Lit Int
11derive instance Functor ExprF
13type Expr = Fix ExprF
15eval :: ExprF Int -> Int
16eval = case _ of
17 Add a b -> a + b
18 Mul a b -> a * b
19 Lit a -> a
21bump :: ExprF Int -> ExprF Int
22bump = case _ of
23 Lit a -> Lit (a + 1)
24 other -> other
26cata :: forall f a. Functor f => (f a -> a) -> Fix f -> a
27cata alg = go
28 where
29 go (Fix f) = alg (map go f)
31test1 :: Expr -> Int
32test1 = cata eval
34test2 :: Expr -> Int
35test2 = cata (eval <<< bump)