| author | wenzelm | 
| Sun, 01 Mar 2009 16:21:33 +0100 | |
| changeset 30187 | b92b3375e919 | 
| parent 29565 | 3f8b24fcfbd6 | 
| child 30628 | 4078276bcace | 
| permissions | -rw-r--r-- | 
| 18 | 1 | (* Title: Pure/Syntax/ast.ML | 
| 0 | 2 | Author: Markus Wenzel, TU Muenchen | 
| 3 | ||
| 18 | 4 | Abstract syntax trees, translation rules, matching and normalization of asts. | 
| 0 | 5 | *) | 
| 6 | ||
| 18 | 7 | signature AST0 = | 
| 16609 | 8 | sig | 
| 0 | 9 | datatype ast = | 
| 10 | Constant of string | | |
| 11 | Variable of string | | |
| 12 | Appl of ast list | |
| 258 | 13 | exception AST of string * ast list | 
| 16609 | 14 | end; | 
| 258 | 15 | |
| 16 | signature AST1 = | |
| 16609 | 17 | sig | 
| 258 | 18 | include AST0 | 
| 0 | 19 | val mk_appl: ast -> ast list -> ast | 
| 18 | 20 | val str_of_ast: ast -> string | 
| 21 | val pretty_ast: ast -> Pretty.T | |
| 258 | 22 | val pretty_rule: ast * ast -> Pretty.T | 
| 18 | 23 | val pprint_ast: ast -> pprint_args -> unit | 
| 10913 | 24 | val fold_ast: string -> ast list -> ast | 
| 25 | val fold_ast_p: string -> ast list * ast -> ast | |
| 26 | val unfold_ast: string -> ast -> ast list | |
| 27 | val unfold_ast_p: string -> ast -> ast list * ast | |
| 8997 | 28 | val trace_ast: bool ref | 
| 29 | val stat_ast: bool ref | |
| 16609 | 30 | end; | 
| 18 | 31 | |
| 32 | signature AST = | |
| 16609 | 33 | sig | 
| 258 | 34 | include AST1 | 
| 0 | 35 | val head_of_rule: ast * ast -> string | 
| 36 | val rule_error: ast * ast -> string option | |
| 9372 
7834e56e2277
AST translation rules no longer require constant head on LHS;
 wenzelm parents: 
8997diff
changeset | 37 | val normalize: bool -> bool -> (string -> (ast * ast) list) -> ast -> ast | 
| 
7834e56e2277
AST translation rules no longer require constant head on LHS;
 wenzelm parents: 
8997diff
changeset | 38 | val normalize_ast: (string -> (ast * ast) list) -> ast -> ast | 
| 16609 | 39 | end; | 
| 0 | 40 | |
| 1506 | 41 | structure Ast : AST = | 
| 0 | 42 | struct | 
| 43 | ||
| 18 | 44 | (** abstract syntax trees **) | 
| 0 | 45 | |
| 46 | (*asts come in two flavours: | |
| 18 | 47 | - ordinary asts representing terms and typs: Variables are (often) treated | 
| 48 | like Constants; | |
| 0 | 49 | - patterns used as lhs and rhs in rules: Variables are placeholders for | 
| 50 | proper asts*) | |
| 51 | ||
| 52 | datatype ast = | |
| 18 | 53 | Constant of string | (*"not", "_abs", "fun"*) | 
| 54 | Variable of string | (*x, ?x, 'a, ?'a*) | |
| 55 |   Appl of ast list;       (*(f x y z), ("fun" 'a 'b), ("_abs" x t)*)
 | |
| 0 | 56 | |
| 57 | ||
| 58 | (*the list of subasts of an Appl node has to contain at least 2 elements, i.e. | |
| 59 | there are no empty asts or nullary applications; use mk_appl for convenience*) | |
| 60 | ||
| 18 | 61 | fun mk_appl f [] = f | 
| 62 | | mk_appl f args = Appl (f :: args); | |
| 0 | 63 | |
| 64 | ||
| 65 | (*exception for system errors involving asts*) | |
| 66 | ||
| 67 | exception AST of string * ast list; | |
| 68 | ||
| 69 | ||
| 70 | ||
| 18 | 71 | (** print asts in a LISP-like style **) | 
| 72 | ||
| 0 | 73 | fun str_of_ast (Constant a) = quote a | 
| 74 | | str_of_ast (Variable x) = x | |
| 75 |   | str_of_ast (Appl asts) = "(" ^ (space_implode " " (map str_of_ast asts)) ^ ")";
 | |
| 76 | ||
| 14599 | 77 | fun pretty_ast (Constant a) = Pretty.quote (Pretty.str a) | 
| 18 | 78 | | pretty_ast (Variable x) = Pretty.str x | 
| 79 | | pretty_ast (Appl asts) = | |
| 513 | 80 |       Pretty.enclose "(" ")" (Pretty.breaks (map pretty_ast asts));
 | 
| 18 | 81 | |
| 82 | val pprint_ast = Pretty.pprint o pretty_ast; | |
| 83 | ||
| 84 | fun pretty_rule (lhs, rhs) = | |
| 235 | 85 | Pretty.block [pretty_ast lhs, Pretty.str " ->", Pretty.brk 2, pretty_ast rhs]; | 
| 18 | 86 | |
| 87 | ||
| 0 | 88 | (* head_of_ast, head_of_rule *) | 
| 89 | ||
| 9372 
7834e56e2277
AST translation rules no longer require constant head on LHS;
 wenzelm parents: 
8997diff
changeset | 90 | fun head_of_ast (Constant a) = a | 
| 
7834e56e2277
AST translation rules no longer require constant head on LHS;
 wenzelm parents: 
8997diff
changeset | 91 | | head_of_ast (Appl (Constant a :: _)) = a | 
| 
7834e56e2277
AST translation rules no longer require constant head on LHS;
 wenzelm parents: 
8997diff
changeset | 92 | | head_of_ast _ = ""; | 
| 0 | 93 | |
| 9372 
7834e56e2277
AST translation rules no longer require constant head on LHS;
 wenzelm parents: 
8997diff
changeset | 94 | fun head_of_rule (lhs, _) = head_of_ast lhs; | 
| 0 | 95 | |
| 96 | ||
| 97 | ||
| 18 | 98 | (** check translation rules **) | 
| 0 | 99 | |
| 100 | fun rule_error (rule as (lhs, rhs)) = | |
| 101 | let | |
| 19486 | 102 | fun add_vars (Constant _) = I | 
| 103 | | add_vars (Variable x) = cons x | |
| 104 | | add_vars (Appl asts) = fold add_vars asts; | |
| 0 | 105 | |
| 19486 | 106 | val lvars = add_vars lhs []; | 
| 107 | val rvars = add_vars rhs []; | |
| 0 | 108 | in | 
| 19486 | 109 | if has_duplicates (op =) lvars then SOME "duplicate vars in lhs" | 
| 15531 | 110 | else if not (rvars subset lvars) then SOME "rhs contains extra variables" | 
| 111 | else NONE | |
| 0 | 112 | end; | 
| 113 | ||
| 114 | ||
| 115 | ||
| 18 | 116 | (** ast translation utilities **) | 
| 0 | 117 | |
| 118 | (* fold asts *) | |
| 119 | ||
| 120 | fun fold_ast _ [] = raise Match | |
| 121 | | fold_ast _ [y] = y | |
| 122 | | fold_ast c (x :: xs) = Appl [Constant c, x, fold_ast c xs]; | |
| 123 | ||
| 19473 | 124 | fun fold_ast_p c = uncurry (fold_rev (fn x => fn xs => Appl [Constant c, x, xs])); | 
| 0 | 125 | |
| 126 | ||
| 127 | (* unfold asts *) | |
| 128 | ||
| 129 | fun unfold_ast c (y as Appl [Constant c', x, xs]) = | |
| 16609 | 130 | if c = c' then x :: unfold_ast c xs else [y] | 
| 0 | 131 | | unfold_ast _ y = [y]; | 
| 132 | ||
| 133 | fun unfold_ast_p c (y as Appl [Constant c', x, xs]) = | |
| 18 | 134 | if c = c' then apfst (cons x) (unfold_ast_p c xs) | 
| 0 | 135 | else ([], y) | 
| 136 | | unfold_ast_p _ y = ([], y); | |
| 137 | ||
| 138 | ||
| 16609 | 139 | |
| 0 | 140 | (** normalization of asts **) | 
| 141 | ||
| 142 | (* match *) | |
| 143 | ||
| 144 | fun match ast pat = | |
| 145 | let | |
| 146 | exception NO_MATCH; | |
| 147 | ||
| 1127 
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
 wenzelm parents: 
922diff
changeset | 148 | fun mtch (Constant a) (Constant b) env = | 
| 0 | 149 | if a = b then env else raise NO_MATCH | 
| 1127 
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
 wenzelm parents: 
922diff
changeset | 150 | | mtch (Variable a) (Constant b) env = | 
| 0 | 151 | if a = b then env else raise NO_MATCH | 
| 17412 | 152 | | mtch ast (Variable x) env = Symtab.update (x, ast) env | 
| 1127 
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
 wenzelm parents: 
922diff
changeset | 153 | | mtch (Appl asts) (Appl pats) env = mtch_lst asts pats env | 
| 
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
 wenzelm parents: 
922diff
changeset | 154 | | mtch _ _ _ = raise NO_MATCH | 
| 
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
 wenzelm parents: 
922diff
changeset | 155 | and mtch_lst (ast :: asts) (pat :: pats) env = | 
| 
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
 wenzelm parents: 
922diff
changeset | 156 | mtch_lst asts pats (mtch ast pat env) | 
| 
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
 wenzelm parents: 
922diff
changeset | 157 | | mtch_lst [] [] env = env | 
| 
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
 wenzelm parents: 
922diff
changeset | 158 | | mtch_lst _ _ _ = raise NO_MATCH; | 
| 
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
 wenzelm parents: 
922diff
changeset | 159 | |
| 
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
 wenzelm parents: 
922diff
changeset | 160 | val (head, args) = | 
| 
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
 wenzelm parents: 
922diff
changeset | 161 | (case (ast, pat) of | 
| 
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
 wenzelm parents: 
922diff
changeset | 162 | (Appl asts, Appl pats) => | 
| 
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
 wenzelm parents: 
922diff
changeset | 163 | let val a = length asts and p = length pats in | 
| 15570 | 164 | if a > p then (Appl (Library.take (p, asts)), Library.drop (p, asts)) | 
| 1127 
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
 wenzelm parents: 
922diff
changeset | 165 | else (ast, []) | 
| 
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
 wenzelm parents: 
922diff
changeset | 166 | end | 
| 
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
 wenzelm parents: 
922diff
changeset | 167 | | _ => (ast, [])); | 
| 0 | 168 | in | 
| 15531 | 169 | SOME (mtch head pat Symtab.empty, args) handle NO_MATCH => NONE | 
| 0 | 170 | end; | 
| 171 | ||
| 172 | ||
| 18 | 173 | (* normalize *) | 
| 0 | 174 | |
| 175 | (*the normalizer works yoyo-like: top-down, bottom-up, top-down, ...*) | |
| 176 | ||
| 18 | 177 | fun normalize trace stat get_rules pre_ast = | 
| 0 | 178 | let | 
| 179 | val passes = ref 0; | |
| 180 | val failed_matches = ref 0; | |
| 181 | val changes = ref 0; | |
| 182 | ||
| 18 | 183 | fun subst _ (ast as Constant _) = ast | 
| 17412 | 184 | | subst env (Variable x) = the (Symtab.lookup env x) | 
| 0 | 185 | | subst env (Appl asts) = Appl (map (subst env) asts); | 
| 186 | ||
| 11733 | 187 | fun try_rules ((lhs, rhs) :: pats) ast = | 
| 0 | 188 | (case match ast lhs of | 
| 15531 | 189 | SOME (env, args) => | 
| 190 | (inc changes; SOME (mk_appl (subst env rhs) args)) | |
| 191 | | NONE => (inc failed_matches; try_rules pats ast)) | |
| 192 | | try_rules [] _ = NONE; | |
| 11733 | 193 | val try_headless_rules = try_rules (get_rules ""); | 
| 0 | 194 | |
| 11733 | 195 | fun try ast a = | 
| 196 | (case try_rules (get_rules a) ast of | |
| 15531 | 197 | NONE => try_headless_rules ast | 
| 11733 | 198 | | some => some); | 
| 0 | 199 | |
| 200 | fun rewrite (ast as Constant a) = try ast a | |
| 201 | | rewrite (ast as Variable a) = try ast a | |
| 202 | | rewrite (ast as Appl (Constant a :: _)) = try ast a | |
| 203 | | rewrite (ast as Appl (Variable a :: _)) = try ast a | |
| 11733 | 204 | | rewrite ast = try_headless_rules ast; | 
| 0 | 205 | |
| 206 | fun rewrote old_ast new_ast = | |
| 21962 | 207 | if trace then | 
| 208 |         tracing ("rewrote: " ^ str_of_ast old_ast ^ "  ->  " ^ str_of_ast new_ast)
 | |
| 209 | else (); | |
| 0 | 210 | |
| 211 | fun norm_root ast = | |
| 212 | (case rewrite ast of | |
| 15531 | 213 | SOME new_ast => (rewrote ast new_ast; norm_root new_ast) | 
| 214 | | NONE => ast); | |
| 0 | 215 | |
| 216 | fun norm ast = | |
| 217 | (case norm_root ast of | |
| 218 | Appl sub_asts => | |
| 219 | let | |
| 220 | val old_changes = ! changes; | |
| 221 | val new_ast = Appl (map norm sub_asts); | |
| 222 | in | |
| 223 | if old_changes = ! changes then new_ast else norm_root new_ast | |
| 224 | end | |
| 225 | | atomic_ast => atomic_ast); | |
| 226 | ||
| 227 | fun normal ast = | |
| 228 | let | |
| 229 | val old_changes = ! changes; | |
| 230 | val new_ast = norm ast; | |
| 231 | in | |
| 232 | inc passes; | |
| 233 | if old_changes = ! changes then new_ast else normal new_ast | |
| 234 | end; | |
| 235 | ||
| 236 | ||
| 21962 | 237 |     val _ = if trace then tracing ("pre: " ^ str_of_ast pre_ast) else ();
 | 
| 9372 
7834e56e2277
AST translation rules no longer require constant head on LHS;
 wenzelm parents: 
8997diff
changeset | 238 | val post_ast = normal pre_ast; | 
| 21962 | 239 | val _ = | 
| 240 | if trace orelse stat then | |
| 241 |         tracing ("post: " ^ str_of_ast post_ast ^ "\nnormalize: " ^
 | |
| 242 | string_of_int (! passes) ^ " passes, " ^ | |
| 243 | string_of_int (! changes) ^ " changes, " ^ | |
| 244 | string_of_int (! failed_matches) ^ " matches failed") | |
| 245 | else (); | |
| 246 | in post_ast end; | |
| 0 | 247 | |
| 248 | ||
| 18 | 249 | (* normalize_ast *) | 
| 250 | ||
| 12262 | 251 | val trace_ast = ref false; | 
| 252 | val stat_ast = ref false; | |
| 253 | ||
| 18 | 254 | fun normalize_ast get_rules ast = | 
| 8997 | 255 | normalize (! trace_ast) (! stat_ast) get_rules ast; | 
| 18 | 256 | |
| 1506 | 257 | end; |