Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] View of /archive/mldist.44/lib/twig/example.mtw
ViewVC logotype

View of /archive/mldist.44/lib/twig/example.mtw

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4942 - (download) (annotate)
Sat Apr 6 00:29:33 2019 UTC (3 months, 2 weeks ago) by dbm
File size: 1312 byte(s)
adding a batch of new versions from tapes
   (* 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                                                 *)

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0