|
|
1.1 root 1: (* Example of a complete ML-Twig specification: *)
2: (* Evaluation of expressions *)
3:
4: node Plus(2), Minus(2), Mul(2);
5: node Const(0);
6:
7: prologue (
8:
9: (* The type and function definitions *)
10:
11: datatype tree = Tree of (tree * symbol * tree) | Leaf of int
12: type cost = int
13:
14: fun get_subtrees(Leaf _) = []
15: | get_subtrees(Tree (t1,_,t2)) = [t1,t2]
16: fun node_value(Tree(_,ope,_)) = ope
17: | node_value (Leaf _) = Const
18: val cost_less : int * int -> bool = (op <)
19:
20: fun constValue (Leaf i) = i
21: datatype instr = PUSH of int | PLUS | MINUS | MUL | PLUSMUL
22: );
23:
24: label Expr of instr list;
25:
26: default_cost( fn subexprcost => fold (op +) subexprcost 0);
27:
28: (* Rules *)
29:
30: Expr Const =([PUSH (constValue $$)]);
31:
32: Expr Plus(Expr,Expr) : (2+Expr1+Expr2) =(Expr1@Expr2@[PLUS]);
33:
34: Expr Minus(Expr,Expr) : (2+Expr1+Expr2) =(Expr1@Expr2@[MINUS]);
35:
36: Expr Mul(Expr,Expr) : (2+DC) =(Expr1@Expr2@[MUL]);
37:
38: Expr Mul(Expr,Plus(Expr,Expr)) : (3+DC) =(Expr1@Expr2@Expr3@[PLUSMUL]);
39:
40: REWRITE Expr Mul(Plus(Expr,Expr),Expr) : (0) =(Tree($2$,Mul,$1$));
41:
42: (* End *)
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.