author | wenzelm |
Sat, 12 Oct 2024 14:16:15 +0200 | |
changeset 81151 | 0d728eadad86 |
parent 80881 | 94ea065a8881 |
child 81152 | ece9fe9bf440 |
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 |
80881 | 15 |
val ast_ord: ast ord |
16 |
structure Table: TABLE |
|
17 |
structure Set: SET |
|
18 | 18 |
val pretty_ast: ast -> Pretty.T |
258 | 19 |
val pretty_rule: ast * ast -> Pretty.T |
42264 | 20 |
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
|
21 |
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
|
22 |
val rule_error: ast * ast -> string option |
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 |
|
42277 | 27 |
val trace: bool Config.T |
43347 | 28 |
val stats: bool Config.T |
41377
390c53904220
configuration option "syntax_ast_trace" and "syntax_ast_stat";
wenzelm
parents:
38328
diff
changeset
|
29 |
val normalize: Proof.context -> (string -> (ast * ast) list) -> ast -> ast |
16609 | 30 |
end; |
0 | 31 |
|
42224
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
wenzelm
parents:
42048
diff
changeset
|
32 |
structure Ast: AST = |
0 | 33 |
struct |
34 |
||
18 | 35 |
(** abstract syntax trees **) |
0 | 36 |
|
37 |
(*asts come in two flavours: |
|
18 | 38 |
- ordinary asts representing terms and typs: Variables are (often) treated |
39 |
like Constants; |
|
0 | 40 |
- patterns used as lhs and rhs in rules: Variables are placeholders for |
41 |
proper asts*) |
|
42 |
||
43 |
datatype ast = |
|
18 | 44 |
Constant of string | (*"not", "_abs", "fun"*) |
45 |
Variable of string | (*x, ?x, 'a, ?'a*) |
|
46 |
Appl of ast list; (*(f x y z), ("fun" 'a 'b), ("_abs" x t)*) |
|
0 | 47 |
|
48 |
(*the list of subasts of an Appl node has to contain at least 2 elements, i.e. |
|
49 |
there are no empty asts or nullary applications; use mk_appl for convenience*) |
|
18 | 50 |
fun mk_appl f [] = f |
51 |
| mk_appl f args = Appl (f :: args); |
|
0 | 52 |
|
53 |
(*exception for system errors involving asts*) |
|
54 |
exception AST of string * ast list; |
|
55 |
||
56 |
||
80881 | 57 |
(* order *) |
58 |
||
59 |
local |
|
60 |
||
61 |
fun cons_nr (Constant _) = 0 |
|
62 |
| cons_nr (Variable _) = 1 |
|
63 |
| cons_nr (Appl _) = 2; |
|
64 |
||
65 |
in |
|
66 |
||
67 |
fun ast_ord asts = |
|
68 |
if pointer_eq asts then EQUAL |
|
69 |
else |
|
70 |
(case asts of |
|
71 |
(Constant a, Constant b) => fast_string_ord (a, b) |
|
72 |
| (Variable a, Variable b) => fast_string_ord (a, b) |
|
73 |
| (Appl a, Appl b) => list_ord ast_ord (a, b) |
|
74 |
| _ => int_ord (apply2 cons_nr asts)); |
|
75 |
||
76 |
end; |
|
77 |
||
78 |
structure Table = Table(type key = ast val ord = ast_ord); |
|
79 |
structure Set = Set(Table.Key); |
|
80 |
||
81 |
||
0 | 82 |
|
18 | 83 |
(** print asts in a LISP-like style **) |
84 |
||
14599 | 85 |
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
|
86 |
| pretty_ast (Variable x) = |
42264 | 87 |
(case Term_Position.decode x of |
88 |
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
|
89 |
| NONE => Pretty.str x) |
38328 | 90 |
| pretty_ast (Appl asts) = Pretty.enclose "(" ")" (Pretty.breaks (map pretty_ast asts)); |
18 | 91 |
|
92 |
fun pretty_rule (lhs, rhs) = |
|
67519 | 93 |
Pretty.block [pretty_ast lhs, Pretty.str " \<leadsto>", Pretty.brk 1, pretty_ast rhs]; |
18 | 94 |
|
80809
4a64fc4d1cde
clarified signature: type ML_Pretty.pretty coincides with PolyML.pretty;
wenzelm
parents:
74232
diff
changeset
|
95 |
val _ = ML_system_pp (fn _ => fn _ => Pretty.to_ML o pretty_ast); |
62663 | 96 |
|
18 | 97 |
|
42264 | 98 |
(* strip_positions *) |
99 |
||
100 |
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
|
101 |
if member (op =) Term_Position.markers c andalso is_some (Term_Position.decode x) |
42264 | 102 |
then mk_appl (strip_positions u) (map strip_positions asts) |
103 |
else Appl (map strip_positions (t :: u :: v :: asts)) |
|
104 |
| strip_positions (Appl asts) = Appl (map strip_positions asts) |
|
105 |
| strip_positions ast = ast; |
|
106 |
||
107 |
||
108 |
(* head_of_ast and head_of_rule *) |
|
0 | 109 |
|
9372
7834e56e2277
AST translation rules no longer require constant head on LHS;
wenzelm
parents:
8997
diff
changeset
|
110 |
fun head_of_ast (Constant a) = a |
7834e56e2277
AST translation rules no longer require constant head on LHS;
wenzelm
parents:
8997
diff
changeset
|
111 |
| head_of_ast (Appl (Constant a :: _)) = a |
7834e56e2277
AST translation rules no longer require constant head on LHS;
wenzelm
parents:
8997
diff
changeset
|
112 |
| head_of_ast _ = ""; |
0 | 113 |
|
9372
7834e56e2277
AST translation rules no longer require constant head on LHS;
wenzelm
parents:
8997
diff
changeset
|
114 |
fun head_of_rule (lhs, _) = head_of_ast lhs; |
0 | 115 |
|
116 |
||
117 |
||
18 | 118 |
(** check translation rules **) |
0 | 119 |
|
32784 | 120 |
fun rule_error (lhs, rhs) = |
0 | 121 |
let |
19486 | 122 |
fun add_vars (Constant _) = I |
123 |
| add_vars (Variable x) = cons x |
|
124 |
| add_vars (Appl asts) = fold add_vars asts; |
|
0 | 125 |
|
19486 | 126 |
val lvars = add_vars lhs []; |
127 |
val rvars = add_vars rhs []; |
|
0 | 128 |
in |
19486 | 129 |
if has_duplicates (op =) lvars then SOME "duplicate vars in lhs" |
33038 | 130 |
else if not (subset (op =) (rvars, lvars)) then SOME "rhs contains extra variables" |
15531 | 131 |
else NONE |
0 | 132 |
end; |
133 |
||
134 |
||
135 |
||
18 | 136 |
(** ast translation utilities **) |
0 | 137 |
|
138 |
(* fold asts *) |
|
139 |
||
140 |
fun fold_ast _ [] = raise Match |
|
141 |
| fold_ast _ [y] = y |
|
142 |
| fold_ast c (x :: xs) = Appl [Constant c, x, fold_ast c xs]; |
|
143 |
||
19473 | 144 |
fun fold_ast_p c = uncurry (fold_rev (fn x => fn xs => Appl [Constant c, x, xs])); |
0 | 145 |
|
146 |
||
147 |
(* unfold asts *) |
|
148 |
||
149 |
fun unfold_ast c (y as Appl [Constant c', x, xs]) = |
|
16609 | 150 |
if c = c' then x :: unfold_ast c xs else [y] |
0 | 151 |
| unfold_ast _ y = [y]; |
152 |
||
153 |
fun unfold_ast_p c (y as Appl [Constant c', x, xs]) = |
|
18 | 154 |
if c = c' then apfst (cons x) (unfold_ast_p c xs) |
0 | 155 |
else ([], y) |
156 |
| unfold_ast_p _ y = ([], y); |
|
157 |
||
158 |
||
16609 | 159 |
|
0 | 160 |
(** normalization of asts **) |
161 |
||
162 |
(* match *) |
|
163 |
||
81151 | 164 |
local exception NO_MATCH in |
0 | 165 |
|
81151 | 166 |
fun match obj pat = |
167 |
let |
|
168 |
fun match1 (Constant a) (Constant b) env = if a = b then env else raise NO_MATCH |
|
169 |
| match1 (Variable a) (Constant b) env = if a = b then env else raise NO_MATCH |
|
170 |
| match1 ast (Variable x) env = Symtab.update (x, ast) env |
|
171 |
| match1 (Appl asts) (Appl pats) env = match2 asts pats env |
|
172 |
| match1 _ _ _ = raise NO_MATCH |
|
173 |
and match2 (ast :: asts) (pat :: pats) env = match1 ast pat env |> match2 asts pats |
|
174 |
| match2 [] [] env = env |
|
175 |
| match2 _ _ _ = raise NO_MATCH; |
|
1127
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
176 |
|
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
177 |
val (head, args) = |
81151 | 178 |
(case (obj, pat) of |
1127
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
179 |
(Appl asts, Appl pats) => |
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
180 |
let val a = length asts and p = length pats in |
33957 | 181 |
if a > p then (Appl (take p asts), drop p asts) |
81151 | 182 |
else (obj, []) |
1127
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
183 |
end |
81151 | 184 |
| _ => (obj, [])); |
185 |
in SOME (Symtab.build (match1 head pat), args) handle NO_MATCH => NONE end; |
|
186 |
||
187 |
end; |
|
0 | 188 |
|
189 |
||
18 | 190 |
(* normalize *) |
0 | 191 |
|
69575 | 192 |
val trace = Config.declare_bool ("syntax_ast_trace", \<^here>) (K false); |
193 |
val stats = Config.declare_bool ("syntax_ast_stats", \<^here>) (K false); |
|
0 | 194 |
|
81151 | 195 |
local |
196 |
||
197 |
fun subst _ (ast as Constant _) = ast |
|
198 |
| subst env (Variable x) = the (Symtab.lookup env x) |
|
199 |
| subst env (Appl asts) = Appl (map (subst env) asts); |
|
200 |
||
201 |
fun head_name (Constant a) = SOME a |
|
202 |
| head_name (Variable a) = SOME a |
|
203 |
| head_name (Appl (Constant a :: _)) = SOME a |
|
204 |
| head_name (Appl (Variable a :: _)) = SOME a |
|
205 |
| head_name _ = NONE; |
|
206 |
||
42048
afd11ca8e018
support for encoded positions (for id_position, longid_position) as pseudo type-constraints -- still inactive;
wenzelm
parents:
41377
diff
changeset
|
207 |
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
|
208 |
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
|
209 |
|
81151 | 210 |
in |
211 |
||
41377
390c53904220
configuration option "syntax_ast_trace" and "syntax_ast_stat";
wenzelm
parents:
38328
diff
changeset
|
212 |
(*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
|
213 |
fun normalize ctxt get_rules pre_ast = |
0 | 214 |
let |
42277 | 215 |
val trace = Config.get ctxt trace; |
43347 | 216 |
val stats = Config.get ctxt stats; |
41377
390c53904220
configuration option "syntax_ast_trace" and "syntax_ast_stat";
wenzelm
parents:
38328
diff
changeset
|
217 |
|
32738 | 218 |
val passes = Unsynchronized.ref 0; |
219 |
val failed_matches = Unsynchronized.ref 0; |
|
220 |
val changes = Unsynchronized.ref 0; |
|
0 | 221 |
|
81151 | 222 |
fun rewrite1 ((lhs, rhs) :: pats) ast = |
0 | 223 |
(case match ast lhs of |
15531 | 224 |
SOME (env, args) => |
32738 | 225 |
(Unsynchronized.inc changes; SOME (mk_appl (subst env rhs) args)) |
81151 | 226 |
| NONE => (Unsynchronized.inc failed_matches; rewrite1 pats ast)) |
227 |
| rewrite1 [] _ = NONE; |
|
0 | 228 |
|
81151 | 229 |
fun rewrite2 (SOME a) ast = |
230 |
(case rewrite1 (get_rules a) ast of |
|
231 |
NONE => rewrite2 NONE ast |
|
232 |
| some => some) |
|
233 |
| rewrite2 NONE ast = rewrite1 (get_rules "") ast; |
|
0 | 234 |
|
81151 | 235 |
fun rewrite ast = rewrite2 (head_name ast) ast; |
0 | 236 |
|
237 |
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
|
238 |
if trace then tracing (message "rewrote:" (pretty_rule (old_ast, new_ast))) |
21962 | 239 |
else (); |
0 | 240 |
|
81151 | 241 |
fun norm1 ast = |
0 | 242 |
(case rewrite ast of |
81151 | 243 |
SOME new_ast => (rewrote ast new_ast; norm1 new_ast) |
15531 | 244 |
| NONE => ast); |
0 | 245 |
|
81151 | 246 |
fun norm2 ast = |
247 |
(case norm1 ast of |
|
0 | 248 |
Appl sub_asts => |
249 |
let |
|
250 |
val old_changes = ! changes; |
|
81151 | 251 |
val new_ast = Appl (map norm2 sub_asts); |
0 | 252 |
in |
81151 | 253 |
if old_changes = ! changes then new_ast else norm1 new_ast |
0 | 254 |
end |
255 |
| atomic_ast => atomic_ast); |
|
256 |
||
81151 | 257 |
fun norm ast = |
0 | 258 |
let |
259 |
val old_changes = ! changes; |
|
81151 | 260 |
val new_ast = norm2 ast; |
0 | 261 |
in |
32738 | 262 |
Unsynchronized.inc passes; |
81151 | 263 |
if old_changes = ! changes then new_ast else norm new_ast |
0 | 264 |
end; |
265 |
||
266 |
||
42048
afd11ca8e018
support for encoded positions (for id_position, longid_position) as pseudo type-constraints -- still inactive;
wenzelm
parents:
41377
diff
changeset
|
267 |
val _ = if trace then tracing (message "pre:" (pretty_ast pre_ast)) else (); |
81151 | 268 |
val post_ast = norm pre_ast; |
21962 | 269 |
val _ = |
43347 | 270 |
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
|
271 |
tracing (message "post:" (pretty_ast post_ast) ^ "\nnormalize: " ^ |
21962 | 272 |
string_of_int (! passes) ^ " passes, " ^ |
273 |
string_of_int (! changes) ^ " changes, " ^ |
|
274 |
string_of_int (! failed_matches) ^ " matches failed") |
|
275 |
else (); |
|
276 |
in post_ast end; |
|
0 | 277 |
|
1506 | 278 |
end; |
81151 | 279 |
|
280 |
end; |