Mon Feb 15 09:44:06 CET 2010
Commutative manipulations cont..
Hmm.. writing a ``generic'' evaluator doesn't seem to be a
well-defined problem. I think I'm starting to understand the main
idea behind the MetaOCaml approach: sometimes it's best to limit the
number of optimizations to those your generated program actually
needs, to keep the generation process simple.
Managing the algebraic manipulations as in Cas.hs can get complex.
So, shift focus? It would probably be a good idea to have a concrete
problem to solve.
EDIT: it turned out to be not so difficult to make the code a bit more
readable by removing the CPS recursion and writing the pattern match
out in full. Currently Cas.hs contains:
-- Commuative reduction is able to propagate ``literal bubbles to the
-- surface'' by keeping semi-literal terms in a (L a :<*>: b) normal form.
data CT l t = U -- unit
| Z -- zero
| L l -- literal
| T t -- opaque term
| (CT l t) :<*>: (CT l t) -- op
ctOp :: (Num l) => (l -> l -> l) -> CT l t -> CT l t -> CT l t
ctOp binop = (<*>) where
(*) = binop
-- unit and zero
Z <*> a = Z ; a <*> Z = Z
U <*> a = a ; a <*> U = a
-- reduce literals/semi-literals: (L a) or (L a :<*>: _)
L a <*> L b = L (a * b)
L a <*> (L b :<*>: c) = L (a * b) :<*>: c
(L a :<*>: b) <*> L c = L (a * c) :<*>: b
(L a :<*>: b) <*> (L c :<*>: d) = L (a * c) :<*>: (b :<*>: d)
-- no reduction -> enforce normalized semi-literal form
a <*> (L b :<*>: c) = L b :<*>: (a :<*>: c)
(L a :<*>: b) <*> c = L a :<*>: (b :<*>: c)
a <*> b = a :<*>: b