author | wenzelm |
Sat, 27 Oct 2001 00:05:50 +0200 | |
changeset 11958 | 2ece34b9fd8e |
parent 11733 | 9dd88f3aa8e0 |
child 12262 | 11ff5f47df6e |
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 |
10913 | 25 |
val fold_ast: string -> ast list -> ast |
26 |
val fold_ast_p: string -> ast list * ast -> ast |
|
27 |
val unfold_ast: string -> ast -> ast list |
|
28 |
val unfold_ast_p: string -> ast -> ast list * ast |
|
8997 | 29 |
val trace_ast: bool ref |
30 |
val stat_ast: bool ref |
|
1506 | 31 |
end; |
18 | 32 |
|
33 |
signature AST = |
|
1506 | 34 |
sig |
258 | 35 |
include AST1 |
0 | 36 |
val head_of_rule: ast * ast -> string |
37 |
val rule_error: ast * ast -> string option |
|
9372
7834e56e2277
AST translation rules no longer require constant head on LHS;
wenzelm
parents:
8997
diff
changeset
|
38 |
val normalize: bool -> bool -> (string -> (ast * ast) list) -> ast -> ast |
7834e56e2277
AST translation rules no longer require constant head on LHS;
wenzelm
parents:
8997
diff
changeset
|
39 |
val normalize_ast: (string -> (ast * ast) list) -> 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 |
||
9372
7834e56e2277
AST translation rules no longer require constant head on LHS;
wenzelm
parents:
8997
diff
changeset
|
102 |
fun head_of_ast (Constant a) = a |
7834e56e2277
AST translation rules no longer require constant head on LHS;
wenzelm
parents:
8997
diff
changeset
|
103 |
| head_of_ast (Appl (Constant a :: _)) = a |
7834e56e2277
AST translation rules no longer require constant head on LHS;
wenzelm
parents:
8997
diff
changeset
|
104 |
| head_of_ast _ = ""; |
0 | 105 |
|
9372
7834e56e2277
AST translation rules no longer require constant head on LHS;
wenzelm
parents:
8997
diff
changeset
|
106 |
fun head_of_rule (lhs, _) = head_of_ast lhs; |
0 | 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 lhs has unique vars, |
114 |
- vars of rhs is subset of vars of lhs*) |
|
115 |
||
116 |
fun rule_error (rule as (lhs, rhs)) = |
|
117 |
let |
|
118 |
fun vars_of (Constant _) = [] |
|
119 |
| vars_of (Variable x) = [x] |
|
120 |
| vars_of (Appl asts) = flat (map vars_of asts); |
|
121 |
||
122 |
fun unique (x :: xs) = not (x mem xs) andalso unique xs |
|
123 |
| unique [] = true; |
|
124 |
||
125 |
val lvars = vars_of lhs; |
|
126 |
val rvars = vars_of rhs; |
|
127 |
in |
|
9372
7834e56e2277
AST translation rules no longer require constant head on LHS;
wenzelm
parents:
8997
diff
changeset
|
128 |
if not (unique lvars) then Some "duplicate vars in lhs" |
0 | 129 |
else if not (rvars subset lvars) then Some "rhs contains extra variables" |
130 |
else None |
|
131 |
end; |
|
132 |
||
133 |
||
134 |
||
18 | 135 |
(** ast translation utilities **) |
0 | 136 |
|
137 |
(* fold asts *) |
|
138 |
||
139 |
fun fold_ast _ [] = raise Match |
|
140 |
| fold_ast _ [y] = y |
|
141 |
| fold_ast c (x :: xs) = Appl [Constant c, x, fold_ast c xs]; |
|
142 |
||
143 |
fun fold_ast_p c = foldr (fn (x, xs) => Appl [Constant c, x, xs]); |
|
144 |
||
145 |
||
146 |
(* unfold asts *) |
|
147 |
||
148 |
fun unfold_ast c (y as Appl [Constant c', x, xs]) = |
|
149 |
if c = c' then x :: (unfold_ast c xs) else [y] |
|
150 |
| unfold_ast _ y = [y]; |
|
151 |
||
152 |
fun unfold_ast_p c (y as Appl [Constant c', x, xs]) = |
|
18 | 153 |
if c = c' then apfst (cons x) (unfold_ast_p c xs) |
0 | 154 |
else ([], y) |
155 |
| unfold_ast_p _ y = ([], y); |
|
156 |
||
157 |
||
158 |
(** normalization of asts **) |
|
159 |
||
18 | 160 |
(* tracing options *) |
161 |
||
8997 | 162 |
val trace_ast = ref false; |
163 |
val stat_ast = ref false; |
|
18 | 164 |
|
165 |
||
0 | 166 |
(* match *) |
167 |
||
168 |
fun match ast pat = |
|
169 |
let |
|
170 |
exception NO_MATCH; |
|
171 |
||
1127
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
172 |
fun mtch (Constant a) (Constant b) env = |
0 | 173 |
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
|
174 |
| mtch (Variable a) (Constant b) env = |
0 | 175 |
if a = b then env else raise NO_MATCH |
5689 | 176 |
| 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
|
177 |
| 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
|
178 |
| mtch _ _ _ = raise NO_MATCH |
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
179 |
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
|
180 |
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
|
181 |
| mtch_lst [] [] env = env |
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
182 |
| mtch_lst _ _ _ = raise NO_MATCH; |
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
183 |
|
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
184 |
val (head, args) = |
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
185 |
(case (ast, pat) of |
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
186 |
(Appl asts, Appl pats) => |
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
187 |
let val a = length asts and p = length pats in |
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
188 |
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:
922
diff
changeset
|
189 |
else (ast, []) |
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
190 |
end |
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
191 |
| _ => (ast, [])); |
0 | 192 |
in |
5689 | 193 |
Some (mtch head pat Symtab.empty, args) handle NO_MATCH => None |
0 | 194 |
end; |
195 |
||
196 |
||
18 | 197 |
(* normalize *) |
0 | 198 |
|
199 |
(*the normalizer works yoyo-like: top-down, bottom-up, top-down, ...*) |
|
200 |
||
18 | 201 |
fun normalize trace stat get_rules pre_ast = |
0 | 202 |
let |
203 |
val passes = ref 0; |
|
204 |
val failed_matches = ref 0; |
|
205 |
val changes = ref 0; |
|
206 |
||
18 | 207 |
fun subst _ (ast as Constant _) = ast |
5689 | 208 |
| subst env (Variable x) = the (Symtab.lookup (env, x)) |
0 | 209 |
| subst env (Appl asts) = Appl (map (subst env) asts); |
210 |
||
11733 | 211 |
fun try_rules ((lhs, rhs) :: pats) ast = |
0 | 212 |
(case match ast lhs of |
1127
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
213 |
Some (env, args) => |
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
214 |
(inc changes; Some (mk_appl (subst env rhs) args)) |
11733 | 215 |
| None => (inc failed_matches; try_rules pats ast)) |
216 |
| try_rules [] _ = None; |
|
217 |
val try_headless_rules = try_rules (get_rules ""); |
|
0 | 218 |
|
11733 | 219 |
fun try ast a = |
220 |
(case try_rules (get_rules a) ast of |
|
221 |
None => try_headless_rules ast |
|
222 |
| some => some); |
|
0 | 223 |
|
224 |
fun rewrite (ast as Constant a) = try ast a |
|
225 |
| rewrite (ast as Variable a) = try ast a |
|
226 |
| rewrite (ast as Appl (Constant a :: _)) = try ast a |
|
227 |
| rewrite (ast as Appl (Variable a :: _)) = try ast a |
|
11733 | 228 |
| rewrite ast = try_headless_rules ast; |
0 | 229 |
|
230 |
fun rewrote old_ast new_ast = |
|
231 |
if trace then |
|
232 |
writeln ("rewrote: " ^ str_of_ast old_ast ^ " -> " ^ str_of_ast new_ast) |
|
233 |
else (); |
|
234 |
||
235 |
fun norm_root ast = |
|
236 |
(case rewrite ast of |
|
237 |
Some new_ast => (rewrote ast new_ast; norm_root new_ast) |
|
238 |
| None => ast); |
|
239 |
||
240 |
fun norm ast = |
|
241 |
(case norm_root ast of |
|
242 |
Appl sub_asts => |
|
243 |
let |
|
244 |
val old_changes = ! changes; |
|
245 |
val new_ast = Appl (map norm sub_asts); |
|
246 |
in |
|
247 |
if old_changes = ! changes then new_ast else norm_root new_ast |
|
248 |
end |
|
249 |
| atomic_ast => atomic_ast); |
|
250 |
||
251 |
fun normal ast = |
|
252 |
let |
|
253 |
val old_changes = ! changes; |
|
254 |
val new_ast = norm ast; |
|
255 |
in |
|
256 |
inc passes; |
|
257 |
if old_changes = ! changes then new_ast else normal new_ast |
|
258 |
end; |
|
259 |
||
260 |
||
18 | 261 |
val _ = if trace then writeln ("pre: " ^ str_of_ast pre_ast) else (); |
0 | 262 |
|
9372
7834e56e2277
AST translation rules no longer require constant head on LHS;
wenzelm
parents:
8997
diff
changeset
|
263 |
val post_ast = normal pre_ast; |
0 | 264 |
in |
18 | 265 |
if trace orelse stat then |
0 | 266 |
writeln ("post: " ^ str_of_ast post_ast ^ "\nnormalize: " ^ |
267 |
string_of_int (! passes) ^ " passes, " ^ |
|
268 |
string_of_int (! changes) ^ " changes, " ^ |
|
269 |
string_of_int (! failed_matches) ^ " matches failed") |
|
270 |
else (); |
|
271 |
post_ast |
|
272 |
end; |
|
273 |
||
274 |
||
18 | 275 |
(* normalize_ast *) |
276 |
||
277 |
fun normalize_ast get_rules ast = |
|
8997 | 278 |
normalize (! trace_ast) (! stat_ast) get_rules ast; |
18 | 279 |
|
1506 | 280 |
end; |
18 | 281 |