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