|
|
researchv10 Norman
(* Example of a complete ML-Twig specification: *)
(* Evaluation of expressions *)
node Plus(2), Minus(2), Mul(2);
node Const(0);
prologue (
(* The type and function definitions *)
datatype tree = Tree of (tree * symbol * tree) | Leaf of int
type cost = int
fun get_subtrees(Leaf _) = []
| get_subtrees(Tree (t1,_,t2)) = [t1,t2]
fun node_value(Tree(_,ope,_)) = ope
| node_value (Leaf _) = Const
val cost_less : int * int -> bool = (op <)
fun constValue (Leaf i) = i
datatype instr = PUSH of int | PLUS | MINUS | MUL | PLUSMUL
);
label Expr of instr list;
default_cost( fn subexprcost => fold (op +) subexprcost 0);
(* Rules *)
Expr Const =([PUSH (constValue $$)]);
Expr Plus(Expr,Expr) : (2+Expr1+Expr2) =(Expr1@Expr2@[PLUS]);
Expr Minus(Expr,Expr) : (2+Expr1+Expr2) =(Expr1@Expr2@[MINUS]);
Expr Mul(Expr,Expr) : (2+DC) =(Expr1@Expr2@[MUL]);
Expr Mul(Expr,Plus(Expr,Expr)) : (3+DC) =(Expr1@Expr2@Expr3@[PLUSMUL]);
REWRITE Expr Mul(Plus(Expr,Expr),Expr) : (0) =(Tree($2$,Mul,$1$));
(* End *)
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.