| author | wenzelm | 
| Wed, 29 Apr 1998 11:13:22 +0200 | |
| changeset 4844 | 4fb63c77f2df | 
| parent 3775 | a99fdf465dfb | 
| child 5689 | ffecea547501 | 
| 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 = | 
| 1506 | 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 | 
| 1506 | 15 | end; | 
| 258 | 16 | |
| 17 | signature AST1 = | |
| 1506 | 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 | 
| 25 | val trace_norm_ast: bool ref | |
| 26 | val stat_norm_ast: bool ref | |
| 1506 | 27 | end; | 
| 18 | 28 | |
| 29 | signature AST = | |
| 1506 | 30 | sig | 
| 258 | 31 | include AST1 | 
| 0 | 32 | val head_of_rule: ast * ast -> string | 
| 33 | val rule_error: ast * ast -> string option | |
| 34 | val fold_ast: string -> ast list -> ast | |
| 35 | val fold_ast_p: string -> ast list * ast -> ast | |
| 36 | val unfold_ast: string -> ast -> ast list | |
| 37 | val unfold_ast_p: string -> ast -> ast list * ast | |
| 18 | 38 | val normalize: bool -> bool -> (string -> (ast * ast) list) option -> ast -> ast | 
| 39 | val normalize_ast: (string -> (ast * ast) list) option -> ast -> ast | |
| 1506 | 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 | ||
| 74 | (* str_of_ast *) | |
| 0 | 75 | |
| 76 | fun str_of_ast (Constant a) = quote a | |
| 77 | | str_of_ast (Variable x) = x | |
| 78 |   | str_of_ast (Appl asts) = "(" ^ (space_implode " " (map str_of_ast asts)) ^ ")";
 | |
| 79 | ||
| 80 | ||
| 18 | 81 | (* pretty_ast *) | 
| 82 | ||
| 83 | fun pretty_ast (Constant a) = Pretty.str (quote a) | |
| 84 | | pretty_ast (Variable x) = Pretty.str x | |
| 85 | | pretty_ast (Appl asts) = | |
| 513 | 86 |       Pretty.enclose "(" ")" (Pretty.breaks (map pretty_ast asts));
 | 
| 18 | 87 | |
| 88 | ||
| 89 | (* pprint_ast *) | |
| 90 | ||
| 91 | val pprint_ast = Pretty.pprint o pretty_ast; | |
| 92 | ||
| 93 | ||
| 94 | (* pretty_rule *) | |
| 95 | ||
| 96 | fun pretty_rule (lhs, rhs) = | |
| 235 | 97 | Pretty.block [pretty_ast lhs, Pretty.str " ->", Pretty.brk 2, pretty_ast rhs]; | 
| 18 | 98 | |
| 99 | ||
| 0 | 100 | (* head_of_ast, head_of_rule *) | 
| 101 | ||
| 102 | fun head_of_ast (Constant a) = Some a | |
| 103 | | head_of_ast (Appl (Constant a :: _)) = Some a | |
| 104 | | head_of_ast _ = None; | |
| 105 | ||
| 106 | fun head_of_rule (lhs, _) = the (head_of_ast lhs); | |
| 107 | ||
| 108 | ||
| 109 | ||
| 18 | 110 | (** check translation rules **) | 
| 0 | 111 | |
| 18 | 112 | (*a wellformed rule (lhs, rhs): (ast * ast) obeys the following conditions: | 
| 0 | 113 | - the head of lhs is a constant, | 
| 114 | - the lhs has unique vars, | |
| 115 | - vars of rhs is subset of vars of lhs*) | |
| 116 | ||
| 117 | fun rule_error (rule as (lhs, rhs)) = | |
| 118 | let | |
| 119 | fun vars_of (Constant _) = [] | |
| 120 | | vars_of (Variable x) = [x] | |
| 121 | | vars_of (Appl asts) = flat (map vars_of asts); | |
| 122 | ||
| 123 | fun unique (x :: xs) = not (x mem xs) andalso unique xs | |
| 124 | | unique [] = true; | |
| 125 | ||
| 126 | val lvars = vars_of lhs; | |
| 127 | val rvars = vars_of rhs; | |
| 128 | in | |
| 129 | if is_none (head_of_ast lhs) then Some "lhs has no constant head" | |
| 130 | else if not (unique lvars) then Some "duplicate vars in lhs" | |
| 131 | else if not (rvars subset lvars) then Some "rhs contains extra variables" | |
| 132 | else None | |
| 133 | end; | |
| 134 | ||
| 135 | ||
| 136 | ||
| 18 | 137 | (** ast translation utilities **) | 
| 0 | 138 | |
| 139 | (* fold asts *) | |
| 140 | ||
| 141 | fun fold_ast _ [] = raise Match | |
| 142 | | fold_ast _ [y] = y | |
| 143 | | fold_ast c (x :: xs) = Appl [Constant c, x, fold_ast c xs]; | |
| 144 | ||
| 145 | fun fold_ast_p c = foldr (fn (x, xs) => Appl [Constant c, x, xs]); | |
| 146 | ||
| 147 | ||
| 148 | (* unfold asts *) | |
| 149 | ||
| 150 | fun unfold_ast c (y as Appl [Constant c', x, xs]) = | |
| 151 | if c = c' then x :: (unfold_ast c xs) else [y] | |
| 152 | | unfold_ast _ y = [y]; | |
| 153 | ||
| 154 | fun unfold_ast_p c (y as Appl [Constant c', x, xs]) = | |
| 18 | 155 | if c = c' then apfst (cons x) (unfold_ast_p c xs) | 
| 0 | 156 | else ([], y) | 
| 157 | | unfold_ast_p _ y = ([], y); | |
| 158 | ||
| 159 | ||
| 160 | (** normalization of asts **) | |
| 161 | ||
| 18 | 162 | (* tracing options *) | 
| 163 | ||
| 164 | val trace_norm_ast = ref false; | |
| 165 | val stat_norm_ast = ref false; | |
| 166 | ||
| 167 | ||
| 0 | 168 | (* simple env *) | 
| 169 | ||
| 170 | structure Env = | |
| 171 | struct | |
| 172 | val empty = []; | |
| 173 | val add = op ::; | |
| 2229 
64acb485ecce
Eta-expansion of a function definition, for value polymorphism
 paulson parents: 
1506diff
changeset | 174 | fun get (alist,x) = the (assoc (alist,x)); | 
| 0 | 175 | end; | 
| 176 | ||
| 177 | ||
| 178 | (* match *) | |
| 179 | ||
| 180 | fun match ast pat = | |
| 181 | let | |
| 182 | exception NO_MATCH; | |
| 183 | ||
| 1127 
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
 wenzelm parents: 
922diff
changeset | 184 | fun mtch (Constant a) (Constant b) env = | 
| 0 | 185 | 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 | 186 | | mtch (Variable a) (Constant b) env = | 
| 0 | 187 | 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 | 188 | | mtch ast (Variable x) env = Env.add ((x, ast), env) | 
| 
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
 wenzelm parents: 
922diff
changeset | 189 | | 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 | 190 | | mtch _ _ _ = raise NO_MATCH | 
| 
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
 wenzelm parents: 
922diff
changeset | 191 | and mtch_lst (ast :: asts) (pat :: pats) env = | 
| 
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
 wenzelm parents: 
922diff
changeset | 192 | mtch_lst asts pats (mtch ast pat env) | 
| 
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
 wenzelm parents: 
922diff
changeset | 193 | | mtch_lst [] [] env = env | 
| 
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
 wenzelm parents: 
922diff
changeset | 194 | | mtch_lst _ _ _ = raise NO_MATCH; | 
| 
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
 wenzelm parents: 
922diff
changeset | 195 | |
| 
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
 wenzelm parents: 
922diff
changeset | 196 | val (head, args) = | 
| 
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
 wenzelm parents: 
922diff
changeset | 197 | (case (ast, pat) of | 
| 
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
 wenzelm parents: 
922diff
changeset | 198 | (Appl asts, Appl pats) => | 
| 
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
 wenzelm parents: 
922diff
changeset | 199 | let val a = length asts and p = length pats in | 
| 
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
 wenzelm parents: 
922diff
changeset | 200 | if a > p then (Appl (take (p, asts)), drop (p, asts)) | 
| 
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
 wenzelm parents: 
922diff
changeset | 201 | else (ast, []) | 
| 
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
 wenzelm parents: 
922diff
changeset | 202 | end | 
| 
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
 wenzelm parents: 
922diff
changeset | 203 | | _ => (ast, [])); | 
| 0 | 204 | in | 
| 1127 
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
 wenzelm parents: 
922diff
changeset | 205 | Some (mtch head pat Env.empty, args) handle NO_MATCH => None | 
| 0 | 206 | end; | 
| 207 | ||
| 208 | ||
| 18 | 209 | (* normalize *) | 
| 0 | 210 | |
| 211 | (*the normalizer works yoyo-like: top-down, bottom-up, top-down, ...*) | |
| 212 | ||
| 18 | 213 | fun normalize trace stat get_rules pre_ast = | 
| 0 | 214 | let | 
| 215 | val passes = ref 0; | |
| 216 | val lookups = ref 0; | |
| 217 | val failed_matches = ref 0; | |
| 218 | val changes = ref 0; | |
| 219 | ||
| 18 | 220 | fun subst _ (ast as Constant _) = ast | 
| 0 | 221 | | subst env (Variable x) = Env.get (env, x) | 
| 222 | | subst env (Appl asts) = Appl (map (subst env) asts); | |
| 223 | ||
| 224 | fun try_rules ast ((lhs, rhs) :: pats) = | |
| 225 | (case match ast lhs of | |
| 1127 
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
 wenzelm parents: 
922diff
changeset | 226 | Some (env, args) => | 
| 
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
 wenzelm parents: 
922diff
changeset | 227 | (inc changes; Some (mk_appl (subst env rhs) args)) | 
| 0 | 228 | | None => (inc failed_matches; try_rules ast pats)) | 
| 18 | 229 | | try_rules _ [] = None; | 
| 0 | 230 | |
| 231 | fun try ast a = (inc lookups; try_rules ast (the get_rules a)); | |
| 232 | ||
| 233 | fun rewrite (ast as Constant a) = try ast a | |
| 234 | | rewrite (ast as Variable a) = try ast a | |
| 235 | | rewrite (ast as Appl (Constant a :: _)) = try ast a | |
| 236 | | rewrite (ast as Appl (Variable a :: _)) = try ast a | |
| 237 | | rewrite _ = None; | |
| 238 | ||
| 239 | fun rewrote old_ast new_ast = | |
| 240 | if trace then | |
| 241 |         writeln ("rewrote: " ^ str_of_ast old_ast ^ "  ->  " ^ str_of_ast new_ast)
 | |
| 242 | else (); | |
| 243 | ||
| 244 | fun norm_root ast = | |
| 245 | (case rewrite ast of | |
| 246 | Some new_ast => (rewrote ast new_ast; norm_root new_ast) | |
| 247 | | None => ast); | |
| 248 | ||
| 249 | fun norm ast = | |
| 250 | (case norm_root ast of | |
| 251 | Appl sub_asts => | |
| 252 | let | |
| 253 | val old_changes = ! changes; | |
| 254 | val new_ast = Appl (map norm sub_asts); | |
| 255 | in | |
| 256 | if old_changes = ! changes then new_ast else norm_root new_ast | |
| 257 | end | |
| 258 | | atomic_ast => atomic_ast); | |
| 259 | ||
| 260 | fun normal ast = | |
| 261 | let | |
| 262 | val old_changes = ! changes; | |
| 263 | val new_ast = norm ast; | |
| 264 | in | |
| 265 | inc passes; | |
| 266 | if old_changes = ! changes then new_ast else normal new_ast | |
| 267 | end; | |
| 268 | ||
| 269 | ||
| 18 | 270 |     val _ = if trace then writeln ("pre: " ^ str_of_ast pre_ast) else ();
 | 
| 0 | 271 | |
| 272 | val post_ast = if is_some get_rules then normal pre_ast else pre_ast; | |
| 273 | in | |
| 18 | 274 | if trace orelse stat then | 
| 0 | 275 |       writeln ("post: " ^ str_of_ast post_ast ^ "\nnormalize: " ^
 | 
| 276 | string_of_int (! passes) ^ " passes, " ^ | |
| 277 | string_of_int (! lookups) ^ " lookups, " ^ | |
| 278 | string_of_int (! changes) ^ " changes, " ^ | |
| 279 | string_of_int (! failed_matches) ^ " matches failed") | |
| 280 | else (); | |
| 281 | post_ast | |
| 282 | end; | |
| 283 | ||
| 284 | ||
| 18 | 285 | (* normalize_ast *) | 
| 286 | ||
| 287 | fun normalize_ast get_rules ast = | |
| 288 | normalize (! trace_norm_ast) (! stat_norm_ast) get_rules ast; | |
| 289 | ||
| 1506 | 290 | end; | 
| 18 | 291 |