author | krauss |
Tue, 22 Feb 2011 16:47:18 +0100 | |
changeset 41817 | c7be23634728 |
parent 41377 | 390c53904220 |
child 42048 | afd11ca8e018 |
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 |
10913 | 23 |
val fold_ast: string -> ast list -> ast |
24 |
val fold_ast_p: string -> ast list * ast -> ast |
|
25 |
val unfold_ast: string -> ast -> ast list |
|
26 |
val unfold_ast_p: string -> ast -> ast list * ast |
|
41377
390c53904220
configuration option "syntax_ast_trace" and "syntax_ast_stat";
wenzelm
parents:
38328
diff
changeset
|
27 |
val ast_trace_raw: Config.raw |
390c53904220
configuration option "syntax_ast_trace" and "syntax_ast_stat";
wenzelm
parents:
38328
diff
changeset
|
28 |
val ast_trace: bool Config.T |
390c53904220
configuration option "syntax_ast_trace" and "syntax_ast_stat";
wenzelm
parents:
38328
diff
changeset
|
29 |
val ast_stat_raw: Config.raw |
390c53904220
configuration option "syntax_ast_trace" and "syntax_ast_stat";
wenzelm
parents:
38328
diff
changeset
|
30 |
val ast_stat: bool Config.T |
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 |
|
41377
390c53904220
configuration option "syntax_ast_trace" and "syntax_ast_stat";
wenzelm
parents:
38328
diff
changeset
|
38 |
val normalize: Proof.context -> (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 |
38328 | 79 |
| pretty_ast (Appl asts) = Pretty.enclose "(" ")" (Pretty.breaks (map pretty_ast asts)); |
18 | 80 |
|
81 |
fun pretty_rule (lhs, rhs) = |
|
235 | 82 |
Pretty.block [pretty_ast lhs, Pretty.str " ->", Pretty.brk 2, pretty_ast rhs]; |
18 | 83 |
|
84 |
||
0 | 85 |
(* head_of_ast, head_of_rule *) |
86 |
||
9372
7834e56e2277
AST translation rules no longer require constant head on LHS;
wenzelm
parents:
8997
diff
changeset
|
87 |
fun head_of_ast (Constant a) = a |
7834e56e2277
AST translation rules no longer require constant head on LHS;
wenzelm
parents:
8997
diff
changeset
|
88 |
| head_of_ast (Appl (Constant a :: _)) = a |
7834e56e2277
AST translation rules no longer require constant head on LHS;
wenzelm
parents:
8997
diff
changeset
|
89 |
| head_of_ast _ = ""; |
0 | 90 |
|
9372
7834e56e2277
AST translation rules no longer require constant head on LHS;
wenzelm
parents:
8997
diff
changeset
|
91 |
fun head_of_rule (lhs, _) = head_of_ast lhs; |
0 | 92 |
|
93 |
||
94 |
||
18 | 95 |
(** check translation rules **) |
0 | 96 |
|
32784 | 97 |
fun rule_error (lhs, rhs) = |
0 | 98 |
let |
19486 | 99 |
fun add_vars (Constant _) = I |
100 |
| add_vars (Variable x) = cons x |
|
101 |
| add_vars (Appl asts) = fold add_vars asts; |
|
0 | 102 |
|
19486 | 103 |
val lvars = add_vars lhs []; |
104 |
val rvars = add_vars rhs []; |
|
0 | 105 |
in |
19486 | 106 |
if has_duplicates (op =) lvars then SOME "duplicate vars in lhs" |
33038 | 107 |
else if not (subset (op =) (rvars, lvars)) then SOME "rhs contains extra variables" |
15531 | 108 |
else NONE |
0 | 109 |
end; |
110 |
||
111 |
||
112 |
||
18 | 113 |
(** ast translation utilities **) |
0 | 114 |
|
115 |
(* fold asts *) |
|
116 |
||
117 |
fun fold_ast _ [] = raise Match |
|
118 |
| fold_ast _ [y] = y |
|
119 |
| fold_ast c (x :: xs) = Appl [Constant c, x, fold_ast c xs]; |
|
120 |
||
19473 | 121 |
fun fold_ast_p c = uncurry (fold_rev (fn x => fn xs => Appl [Constant c, x, xs])); |
0 | 122 |
|
123 |
||
124 |
(* unfold asts *) |
|
125 |
||
126 |
fun unfold_ast c (y as Appl [Constant c', x, xs]) = |
|
16609 | 127 |
if c = c' then x :: unfold_ast c xs else [y] |
0 | 128 |
| unfold_ast _ y = [y]; |
129 |
||
130 |
fun unfold_ast_p c (y as Appl [Constant c', x, xs]) = |
|
18 | 131 |
if c = c' then apfst (cons x) (unfold_ast_p c xs) |
0 | 132 |
else ([], y) |
133 |
| unfold_ast_p _ y = ([], y); |
|
134 |
||
135 |
||
16609 | 136 |
|
0 | 137 |
(** normalization of asts **) |
138 |
||
139 |
(* match *) |
|
140 |
||
141 |
fun match ast pat = |
|
142 |
let |
|
143 |
exception NO_MATCH; |
|
144 |
||
1127
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
145 |
fun mtch (Constant a) (Constant b) env = |
0 | 146 |
if a = b then env else raise NO_MATCH |
1127
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
147 |
| mtch (Variable a) (Constant b) env = |
0 | 148 |
if a = b then env else raise NO_MATCH |
17412 | 149 |
| 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:
922
diff
changeset
|
150 |
| 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:
922
diff
changeset
|
151 |
| mtch _ _ _ = raise NO_MATCH |
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
152 |
and mtch_lst (ast :: asts) (pat :: pats) env = |
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
153 |
mtch_lst asts pats (mtch ast pat env) |
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
154 |
| mtch_lst [] [] env = env |
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
155 |
| mtch_lst _ _ _ = raise NO_MATCH; |
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
156 |
|
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
157 |
val (head, args) = |
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
158 |
(case (ast, pat) of |
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
159 |
(Appl asts, Appl pats) => |
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
160 |
let val a = length asts and p = length pats in |
33957 | 161 |
if a > p then (Appl (take p asts), drop p asts) |
1127
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
162 |
else (ast, []) |
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
163 |
end |
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
164 |
| _ => (ast, [])); |
0 | 165 |
in |
15531 | 166 |
SOME (mtch head pat Symtab.empty, args) handle NO_MATCH => NONE |
0 | 167 |
end; |
168 |
||
169 |
||
18 | 170 |
(* normalize *) |
0 | 171 |
|
41377
390c53904220
configuration option "syntax_ast_trace" and "syntax_ast_stat";
wenzelm
parents:
38328
diff
changeset
|
172 |
val ast_trace_raw = Config.declare "syntax_ast_trace" (fn _ => Config.Bool false); |
390c53904220
configuration option "syntax_ast_trace" and "syntax_ast_stat";
wenzelm
parents:
38328
diff
changeset
|
173 |
val ast_trace = Config.bool ast_trace_raw; |
390c53904220
configuration option "syntax_ast_trace" and "syntax_ast_stat";
wenzelm
parents:
38328
diff
changeset
|
174 |
|
390c53904220
configuration option "syntax_ast_trace" and "syntax_ast_stat";
wenzelm
parents:
38328
diff
changeset
|
175 |
val ast_stat_raw = Config.declare "syntax_ast_stat" (fn _ => Config.Bool false); |
390c53904220
configuration option "syntax_ast_trace" and "syntax_ast_stat";
wenzelm
parents:
38328
diff
changeset
|
176 |
val ast_stat = Config.bool ast_stat_raw; |
0 | 177 |
|
41377
390c53904220
configuration option "syntax_ast_trace" and "syntax_ast_stat";
wenzelm
parents:
38328
diff
changeset
|
178 |
(*the normalizer works yoyo-like: top-down, bottom-up, top-down, ...*) |
390c53904220
configuration option "syntax_ast_trace" and "syntax_ast_stat";
wenzelm
parents:
38328
diff
changeset
|
179 |
fun normalize ctxt get_rules pre_ast = |
0 | 180 |
let |
41377
390c53904220
configuration option "syntax_ast_trace" and "syntax_ast_stat";
wenzelm
parents:
38328
diff
changeset
|
181 |
val trace = Config.get ctxt ast_trace; |
390c53904220
configuration option "syntax_ast_trace" and "syntax_ast_stat";
wenzelm
parents:
38328
diff
changeset
|
182 |
val stat = Config.get ctxt ast_stat; |
390c53904220
configuration option "syntax_ast_trace" and "syntax_ast_stat";
wenzelm
parents:
38328
diff
changeset
|
183 |
|
32738 | 184 |
val passes = Unsynchronized.ref 0; |
185 |
val failed_matches = Unsynchronized.ref 0; |
|
186 |
val changes = Unsynchronized.ref 0; |
|
0 | 187 |
|
18 | 188 |
fun subst _ (ast as Constant _) = ast |
17412 | 189 |
| subst env (Variable x) = the (Symtab.lookup env x) |
0 | 190 |
| subst env (Appl asts) = Appl (map (subst env) asts); |
191 |
||
11733 | 192 |
fun try_rules ((lhs, rhs) :: pats) ast = |
0 | 193 |
(case match ast lhs of |
15531 | 194 |
SOME (env, args) => |
32738 | 195 |
(Unsynchronized.inc changes; SOME (mk_appl (subst env rhs) args)) |
196 |
| NONE => (Unsynchronized.inc failed_matches; try_rules pats ast)) |
|
15531 | 197 |
| try_rules [] _ = NONE; |
11733 | 198 |
val try_headless_rules = try_rules (get_rules ""); |
0 | 199 |
|
11733 | 200 |
fun try ast a = |
201 |
(case try_rules (get_rules a) ast of |
|
15531 | 202 |
NONE => try_headless_rules ast |
11733 | 203 |
| some => some); |
0 | 204 |
|
205 |
fun rewrite (ast as Constant a) = try ast a |
|
206 |
| rewrite (ast as Variable a) = try ast a |
|
207 |
| rewrite (ast as Appl (Constant a :: _)) = try ast a |
|
208 |
| rewrite (ast as Appl (Variable a :: _)) = try ast a |
|
11733 | 209 |
| rewrite ast = try_headless_rules ast; |
0 | 210 |
|
211 |
fun rewrote old_ast new_ast = |
|
21962 | 212 |
if trace then |
213 |
tracing ("rewrote: " ^ str_of_ast old_ast ^ " -> " ^ str_of_ast new_ast) |
|
214 |
else (); |
|
0 | 215 |
|
216 |
fun norm_root ast = |
|
217 |
(case rewrite ast of |
|
15531 | 218 |
SOME new_ast => (rewrote ast new_ast; norm_root new_ast) |
219 |
| NONE => ast); |
|
0 | 220 |
|
221 |
fun norm ast = |
|
222 |
(case norm_root ast of |
|
223 |
Appl sub_asts => |
|
224 |
let |
|
225 |
val old_changes = ! changes; |
|
226 |
val new_ast = Appl (map norm sub_asts); |
|
227 |
in |
|
228 |
if old_changes = ! changes then new_ast else norm_root new_ast |
|
229 |
end |
|
230 |
| atomic_ast => atomic_ast); |
|
231 |
||
232 |
fun normal ast = |
|
233 |
let |
|
234 |
val old_changes = ! changes; |
|
235 |
val new_ast = norm ast; |
|
236 |
in |
|
32738 | 237 |
Unsynchronized.inc passes; |
0 | 238 |
if old_changes = ! changes then new_ast else normal new_ast |
239 |
end; |
|
240 |
||
241 |
||
21962 | 242 |
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:
8997
diff
changeset
|
243 |
val post_ast = normal pre_ast; |
21962 | 244 |
val _ = |
245 |
if trace orelse stat then |
|
246 |
tracing ("post: " ^ str_of_ast post_ast ^ "\nnormalize: " ^ |
|
247 |
string_of_int (! passes) ^ " passes, " ^ |
|
248 |
string_of_int (! changes) ^ " changes, " ^ |
|
249 |
string_of_int (! failed_matches) ^ " matches failed") |
|
250 |
else (); |
|
251 |
in post_ast end; |
|
0 | 252 |
|
1506 | 253 |
end; |