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