author | nipkow |
Thu, 21 Dec 2017 21:01:47 +0100 | |
changeset 67240 | 2c9694a8c000 |
parent 64556 | 851ae0e7b09c |
child 67519 | 6905b156a030 |
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 |
||
42224
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
wenzelm
parents:
42048
diff
changeset
|
7 |
signature AST = |
16609 | 8 |
sig |
0 | 9 |
datatype ast = |
10 |
Constant of string | |
|
11 |
Variable of string | |
|
12 |
Appl of ast list |
|
42224
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
wenzelm
parents:
42048
diff
changeset
|
13 |
val mk_appl: ast -> ast list -> ast |
258 | 14 |
exception AST of string * ast list |
18 | 15 |
val pretty_ast: ast -> Pretty.T |
258 | 16 |
val pretty_rule: ast * ast -> Pretty.T |
42264 | 17 |
val strip_positions: ast -> ast |
42224
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
wenzelm
parents:
42048
diff
changeset
|
18 |
val head_of_rule: ast * ast -> string |
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
wenzelm
parents:
42048
diff
changeset
|
19 |
val rule_error: ast * ast -> string option |
10913 | 20 |
val fold_ast: string -> ast list -> ast |
21 |
val fold_ast_p: string -> ast list * ast -> ast |
|
22 |
val unfold_ast: string -> ast -> ast list |
|
23 |
val unfold_ast_p: string -> ast -> ast list * ast |
|
42277 | 24 |
val trace_raw: Config.raw |
25 |
val trace: bool Config.T |
|
43347 | 26 |
val stats_raw: Config.raw |
27 |
val stats: bool Config.T |
|
41377
390c53904220
configuration option "syntax_ast_trace" and "syntax_ast_stat";
wenzelm
parents:
38328
diff
changeset
|
28 |
val normalize: Proof.context -> (string -> (ast * ast) list) -> ast -> ast |
16609 | 29 |
end; |
0 | 30 |
|
42224
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
wenzelm
parents:
42048
diff
changeset
|
31 |
structure Ast: AST = |
0 | 32 |
struct |
33 |
||
18 | 34 |
(** abstract syntax trees **) |
0 | 35 |
|
36 |
(*asts come in two flavours: |
|
18 | 37 |
- ordinary asts representing terms and typs: Variables are (often) treated |
38 |
like Constants; |
|
0 | 39 |
- patterns used as lhs and rhs in rules: Variables are placeholders for |
40 |
proper asts*) |
|
41 |
||
42 |
datatype ast = |
|
18 | 43 |
Constant of string | (*"not", "_abs", "fun"*) |
44 |
Variable of string | (*x, ?x, 'a, ?'a*) |
|
45 |
Appl of ast list; (*(f x y z), ("fun" 'a 'b), ("_abs" x t)*) |
|
0 | 46 |
|
47 |
(*the list of subasts of an Appl node has to contain at least 2 elements, i.e. |
|
48 |
there are no empty asts or nullary applications; use mk_appl for convenience*) |
|
18 | 49 |
fun mk_appl f [] = f |
50 |
| mk_appl f args = Appl (f :: args); |
|
0 | 51 |
|
52 |
(*exception for system errors involving asts*) |
|
53 |
exception AST of string * ast list; |
|
54 |
||
55 |
||
56 |
||
18 | 57 |
(** print asts in a LISP-like style **) |
58 |
||
14599 | 59 |
fun pretty_ast (Constant a) = Pretty.quote (Pretty.str a) |
42048
afd11ca8e018
support for encoded positions (for id_position, longid_position) as pseudo type-constraints -- still inactive;
wenzelm
parents:
41377
diff
changeset
|
60 |
| pretty_ast (Variable x) = |
42264 | 61 |
(case Term_Position.decode x of |
62 |
SOME pos => Term_Position.pretty pos |
|
42048
afd11ca8e018
support for encoded positions (for id_position, longid_position) as pseudo type-constraints -- still inactive;
wenzelm
parents:
41377
diff
changeset
|
63 |
| NONE => Pretty.str x) |
38328 | 64 |
| pretty_ast (Appl asts) = Pretty.enclose "(" ")" (Pretty.breaks (map pretty_ast asts)); |
18 | 65 |
|
66 |
fun pretty_rule (lhs, rhs) = |
|
235 | 67 |
Pretty.block [pretty_ast lhs, Pretty.str " ->", Pretty.brk 2, pretty_ast rhs]; |
18 | 68 |
|
62819
d3ff367a16a0
careful export of type-dependent functions, without losing their special status;
wenzelm
parents:
62663
diff
changeset
|
69 |
val _ = ML_system_pp (fn _ => fn _ => Pretty.to_polyml o pretty_ast); |
62663 | 70 |
|
18 | 71 |
|
42264 | 72 |
(* strip_positions *) |
73 |
||
74 |
fun strip_positions (Appl ((t as Constant c) :: u :: (v as Variable x) :: asts)) = |
|
52175
626a757d3c2d
uniform Term_Position.markers (cf. dbadb4d03cbc);
wenzelm
parents:
43347
diff
changeset
|
75 |
if member (op =) Term_Position.markers c andalso is_some (Term_Position.decode x) |
42264 | 76 |
then mk_appl (strip_positions u) (map strip_positions asts) |
77 |
else Appl (map strip_positions (t :: u :: v :: asts)) |
|
78 |
| strip_positions (Appl asts) = Appl (map strip_positions asts) |
|
79 |
| strip_positions ast = ast; |
|
80 |
||
81 |
||
82 |
(* head_of_ast and head_of_rule *) |
|
0 | 83 |
|
9372
7834e56e2277
AST translation rules no longer require constant head on LHS;
wenzelm
parents:
8997
diff
changeset
|
84 |
fun head_of_ast (Constant a) = a |
7834e56e2277
AST translation rules no longer require constant head on LHS;
wenzelm
parents:
8997
diff
changeset
|
85 |
| head_of_ast (Appl (Constant a :: _)) = a |
7834e56e2277
AST translation rules no longer require constant head on LHS;
wenzelm
parents:
8997
diff
changeset
|
86 |
| head_of_ast _ = ""; |
0 | 87 |
|
9372
7834e56e2277
AST translation rules no longer require constant head on LHS;
wenzelm
parents:
8997
diff
changeset
|
88 |
fun head_of_rule (lhs, _) = head_of_ast lhs; |
0 | 89 |
|
90 |
||
91 |
||
18 | 92 |
(** check translation rules **) |
0 | 93 |
|
32784 | 94 |
fun rule_error (lhs, rhs) = |
0 | 95 |
let |
19486 | 96 |
fun add_vars (Constant _) = I |
97 |
| add_vars (Variable x) = cons x |
|
98 |
| add_vars (Appl asts) = fold add_vars asts; |
|
0 | 99 |
|
19486 | 100 |
val lvars = add_vars lhs []; |
101 |
val rvars = add_vars rhs []; |
|
0 | 102 |
in |
19486 | 103 |
if has_duplicates (op =) lvars then SOME "duplicate vars in lhs" |
33038 | 104 |
else if not (subset (op =) (rvars, lvars)) then SOME "rhs contains extra variables" |
15531 | 105 |
else NONE |
0 | 106 |
end; |
107 |
||
108 |
||
109 |
||
18 | 110 |
(** ast translation utilities **) |
0 | 111 |
|
112 |
(* fold asts *) |
|
113 |
||
114 |
fun fold_ast _ [] = raise Match |
|
115 |
| fold_ast _ [y] = y |
|
116 |
| fold_ast c (x :: xs) = Appl [Constant c, x, fold_ast c xs]; |
|
117 |
||
19473 | 118 |
fun fold_ast_p c = uncurry (fold_rev (fn x => fn xs => Appl [Constant c, x, xs])); |
0 | 119 |
|
120 |
||
121 |
(* unfold asts *) |
|
122 |
||
123 |
fun unfold_ast c (y as Appl [Constant c', x, xs]) = |
|
16609 | 124 |
if c = c' then x :: unfold_ast c xs else [y] |
0 | 125 |
| unfold_ast _ y = [y]; |
126 |
||
127 |
fun unfold_ast_p c (y as Appl [Constant c', x, xs]) = |
|
18 | 128 |
if c = c' then apfst (cons x) (unfold_ast_p c xs) |
0 | 129 |
else ([], y) |
130 |
| unfold_ast_p _ y = ([], y); |
|
131 |
||
132 |
||
16609 | 133 |
|
0 | 134 |
(** normalization of asts **) |
135 |
||
136 |
(* match *) |
|
137 |
||
138 |
fun match ast pat = |
|
139 |
let |
|
140 |
exception NO_MATCH; |
|
141 |
||
1127
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
142 |
fun mtch (Constant a) (Constant b) env = |
0 | 143 |
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
|
144 |
| mtch (Variable a) (Constant b) env = |
0 | 145 |
if a = b then env else raise NO_MATCH |
17412 | 146 |
| 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
|
147 |
| 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
|
148 |
| mtch _ _ _ = raise NO_MATCH |
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
149 |
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
|
150 |
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
|
151 |
| mtch_lst [] [] env = env |
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
152 |
| mtch_lst _ _ _ = raise NO_MATCH; |
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
153 |
|
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
154 |
val (head, args) = |
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
155 |
(case (ast, pat) of |
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
156 |
(Appl asts, Appl pats) => |
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
157 |
let val a = length asts and p = length pats in |
33957 | 158 |
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
|
159 |
else (ast, []) |
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
160 |
end |
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
161 |
| _ => (ast, [])); |
0 | 162 |
in |
15531 | 163 |
SOME (mtch head pat Symtab.empty, args) handle NO_MATCH => NONE |
0 | 164 |
end; |
165 |
||
166 |
||
18 | 167 |
(* normalize *) |
0 | 168 |
|
64556 | 169 |
val trace_raw = Config.declare ("syntax_ast_trace", \<^here>) (fn _ => Config.Bool false); |
42277 | 170 |
val trace = Config.bool trace_raw; |
41377
390c53904220
configuration option "syntax_ast_trace" and "syntax_ast_stat";
wenzelm
parents:
38328
diff
changeset
|
171 |
|
64556 | 172 |
val stats_raw = Config.declare ("syntax_ast_stats", \<^here>) (fn _ => Config.Bool false); |
43347 | 173 |
val stats = Config.bool stats_raw; |
0 | 174 |
|
42048
afd11ca8e018
support for encoded positions (for id_position, longid_position) as pseudo type-constraints -- still inactive;
wenzelm
parents:
41377
diff
changeset
|
175 |
fun message head body = |
afd11ca8e018
support for encoded positions (for id_position, longid_position) as pseudo type-constraints -- still inactive;
wenzelm
parents:
41377
diff
changeset
|
176 |
Pretty.string_of (Pretty.block [Pretty.str head, Pretty.brk 1, body]); |
afd11ca8e018
support for encoded positions (for id_position, longid_position) as pseudo type-constraints -- still inactive;
wenzelm
parents:
41377
diff
changeset
|
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 |
42277 | 181 |
val trace = Config.get ctxt trace; |
43347 | 182 |
val stats = Config.get ctxt stats; |
41377
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 = |
|
42048
afd11ca8e018
support for encoded positions (for id_position, longid_position) as pseudo type-constraints -- still inactive;
wenzelm
parents:
41377
diff
changeset
|
212 |
if trace then tracing (message "rewrote:" (pretty_rule (old_ast, new_ast))) |
21962 | 213 |
else (); |
0 | 214 |
|
215 |
fun norm_root ast = |
|
216 |
(case rewrite ast of |
|
15531 | 217 |
SOME new_ast => (rewrote ast new_ast; norm_root new_ast) |
218 |
| NONE => ast); |
|
0 | 219 |
|
220 |
fun norm ast = |
|
221 |
(case norm_root ast of |
|
222 |
Appl sub_asts => |
|
223 |
let |
|
224 |
val old_changes = ! changes; |
|
225 |
val new_ast = Appl (map norm sub_asts); |
|
226 |
in |
|
227 |
if old_changes = ! changes then new_ast else norm_root new_ast |
|
228 |
end |
|
229 |
| atomic_ast => atomic_ast); |
|
230 |
||
231 |
fun normal ast = |
|
232 |
let |
|
233 |
val old_changes = ! changes; |
|
234 |
val new_ast = norm ast; |
|
235 |
in |
|
32738 | 236 |
Unsynchronized.inc passes; |
0 | 237 |
if old_changes = ! changes then new_ast else normal new_ast |
238 |
end; |
|
239 |
||
240 |
||
42048
afd11ca8e018
support for encoded positions (for id_position, longid_position) as pseudo type-constraints -- still inactive;
wenzelm
parents:
41377
diff
changeset
|
241 |
val _ = if trace then tracing (message "pre:" (pretty_ast pre_ast)) else (); |
9372
7834e56e2277
AST translation rules no longer require constant head on LHS;
wenzelm
parents:
8997
diff
changeset
|
242 |
val post_ast = normal pre_ast; |
21962 | 243 |
val _ = |
43347 | 244 |
if trace orelse stats then |
42048
afd11ca8e018
support for encoded positions (for id_position, longid_position) as pseudo type-constraints -- still inactive;
wenzelm
parents:
41377
diff
changeset
|
245 |
tracing (message "post:" (pretty_ast post_ast) ^ "\nnormalize: " ^ |
21962 | 246 |
string_of_int (! passes) ^ " passes, " ^ |
247 |
string_of_int (! changes) ^ " changes, " ^ |
|
248 |
string_of_int (! failed_matches) ^ " matches failed") |
|
249 |
else (); |
|
250 |
in post_ast end; |
|
0 | 251 |
|
1506 | 252 |
end; |