Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
where<br />
f (Syn.EVar ”a”) = (Syn.EVar ”b”)<br />
f x = x<br />
is is good for pure logic, but most often our transformations will have to have access to some sort of<br />
state or context during traversal and thus the descedM will let us write the same rewrite but in a custom<br />
monadic context.<br />
transform :: Expr -> RewriteM Expr<br />
transform = descendM f<br />
where<br />
f (Syn.EVar x) = do<br />
env Expr)<br />
-> (Expr -> Expr)<br />
-> (Expr -> Expr)<br />
compose f g = descend (f . g)<br />
Recall from that monadic actions can be composed like functions using Kleisli composition operator.<br />
Functions<br />
: a -> b<br />
Monadic operations : a -> m b<br />
-- Function composition<br />
(.) :: (b -> c) -> (a -> b) -> a -> c<br />
f . g = \x -> g (f x)<br />
-- Monad composition<br />
( m c) -> (a -> m b) -> a -> m c<br />
f g x >>= f<br />
We can now write composition descendM functions in terms of of Kleisli composition to give us a very<br />
general notion of AST rewrite composition.<br />
composeM<br />
:: (Applicative m, Monad m)<br />
=> (Expr -> m Expr)<br />
-> (Expr -> m Expr)<br />
-> (Expr -> m Expr)<br />
composeM f g = descendM (f