src/Pure/Syntax/ast.ML
changeset 12262 11ff5f47df6e
parent 11733 9dd88f3aa8e0
child 12785 27debaf2112d
equal deleted inserted replaced
12261:ee14db2c571d 12262:11ff5f47df6e
   155   | unfold_ast_p _ y = ([], y);
   155   | unfold_ast_p _ y = ([], y);
   156 
   156 
   157 
   157 
   158 (** normalization of asts **)
   158 (** normalization of asts **)
   159 
   159 
   160 (* tracing options *)
       
   161 
       
   162 val trace_ast = ref false;
       
   163 val stat_ast = ref false;
       
   164 
       
   165 
       
   166 (* match *)
   160 (* match *)
   167 
   161 
   168 fun match ast pat =
   162 fun match ast pat =
   169   let
   163   let
   170     exception NO_MATCH;
   164     exception NO_MATCH;
   226       | rewrite (ast as Appl (Constant a :: _)) = try ast a
   220       | rewrite (ast as Appl (Constant a :: _)) = try ast a
   227       | rewrite (ast as Appl (Variable a :: _)) = try ast a
   221       | rewrite (ast as Appl (Variable a :: _)) = try ast a
   228       | rewrite ast = try_headless_rules ast;
   222       | rewrite ast = try_headless_rules ast;
   229 
   223 
   230     fun rewrote old_ast new_ast =
   224     fun rewrote old_ast new_ast =
   231       if trace then
   225       conditional trace (fn () =>
   232         writeln ("rewrote: " ^ str_of_ast old_ast ^ "  ->  " ^ str_of_ast new_ast)
   226         tracing ("rewrote: " ^ str_of_ast old_ast ^ "  ->  " ^ str_of_ast new_ast));
   233       else ();
       
   234 
   227 
   235     fun norm_root ast =
   228     fun norm_root ast =
   236       (case rewrite ast of
   229       (case rewrite ast of
   237         Some new_ast => (rewrote ast new_ast; norm_root new_ast)
   230         Some new_ast => (rewrote ast new_ast; norm_root new_ast)
   238       | None => ast);
   231       | None => ast);
   256         inc passes;
   249         inc passes;
   257         if old_changes = ! changes then new_ast else normal new_ast
   250         if old_changes = ! changes then new_ast else normal new_ast
   258       end;
   251       end;
   259 
   252 
   260 
   253 
   261     val _ = if trace then writeln ("pre: " ^ str_of_ast pre_ast) else ();
   254     val _ = conditional trace (fn () => tracing ("pre: " ^ str_of_ast pre_ast));
   262 
   255 
   263     val post_ast = normal pre_ast;
   256     val post_ast = normal pre_ast;
   264   in
   257   in
   265     if trace orelse stat then
   258     conditional (trace orelse stat) (fn () =>
   266       writeln ("post: " ^ str_of_ast post_ast ^ "\nnormalize: " ^
   259       tracing ("post: " ^ str_of_ast post_ast ^ "\nnormalize: " ^
   267         string_of_int (! passes) ^ " passes, " ^
   260         string_of_int (! passes) ^ " passes, " ^
   268         string_of_int (! changes) ^ " changes, " ^
   261         string_of_int (! changes) ^ " changes, " ^
   269         string_of_int (! failed_matches) ^ " matches failed")
   262         string_of_int (! failed_matches) ^ " matches failed"));
   270     else ();
       
   271     post_ast
   263     post_ast
   272   end;
   264   end;
   273 
   265 
   274 
   266 
   275 (* normalize_ast *)
   267 (* normalize_ast *)
       
   268 
       
   269 val trace_ast = ref false;
       
   270 val stat_ast = ref false;
   276 
   271 
   277 fun normalize_ast get_rules ast =
   272 fun normalize_ast get_rules ast =
   278   normalize (! trace_ast) (! stat_ast) get_rules ast;
   273   normalize (! trace_ast) (! stat_ast) get_rules ast;
   279 
   274 
   280 end;
   275 end;