author | wenzelm |
Wed, 16 Jan 2002 23:19:34 +0100 | |
changeset 12785 | 27debaf2112d |
parent 12262 | 11ff5f47df6e |
child 14599 | c3177fffd31a |
permissions | -rw-r--r-- |
18 | 1 |
(* Title: Pure/Syntax/ast.ML |
0 | 2 |
ID: $Id$ |
3 |
Author: Markus Wenzel, TU Muenchen |
|
12785 | 4 |
License: GPL (GNU GENERAL PUBLIC LICENSE) |
0 | 5 |
|
18 | 6 |
Abstract syntax trees, translation rules, matching and normalization of asts. |
0 | 7 |
*) |
8 |
||
18 | 9 |
signature AST0 = |
1506 | 10 |
sig |
0 | 11 |
datatype ast = |
12 |
Constant of string | |
|
13 |
Variable of string | |
|
14 |
Appl of ast list |
|
258 | 15 |
exception AST of string * ast list |
1506 | 16 |
end; |
258 | 17 |
|
18 |
signature AST1 = |
|
1506 | 19 |
sig |
258 | 20 |
include AST0 |
0 | 21 |
val mk_appl: ast -> ast list -> ast |
18 | 22 |
val str_of_ast: ast -> string |
23 |
val pretty_ast: ast -> Pretty.T |
|
258 | 24 |
val pretty_rule: ast * ast -> Pretty.T |
18 | 25 |
val pprint_ast: ast -> pprint_args -> unit |
10913 | 26 |
val fold_ast: string -> ast list -> ast |
27 |
val fold_ast_p: string -> ast list * ast -> ast |
|
28 |
val unfold_ast: string -> ast -> ast list |
|
29 |
val unfold_ast_p: string -> ast -> ast list * ast |
|
8997 | 30 |
val trace_ast: bool ref |
31 |
val stat_ast: bool ref |
|
1506 | 32 |
end; |
18 | 33 |
|
34 |
signature AST = |
|
1506 | 35 |
sig |
258 | 36 |
include AST1 |
0 | 37 |
val head_of_rule: ast * ast -> string |
38 |
val rule_error: ast * ast -> string option |
|
9372
7834e56e2277
AST translation rules no longer require constant head on LHS;
wenzelm
parents:
8997
diff
changeset
|
39 |
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
|
40 |
val normalize_ast: (string -> (ast * ast) list) -> ast -> ast |
1506 | 41 |
end; |
0 | 42 |
|
1506 | 43 |
structure Ast : AST = |
0 | 44 |
struct |
45 |
||
18 | 46 |
(** abstract syntax trees **) |
0 | 47 |
|
48 |
(*asts come in two flavours: |
|
18 | 49 |
- ordinary asts representing terms and typs: Variables are (often) treated |
50 |
like Constants; |
|
0 | 51 |
- patterns used as lhs and rhs in rules: Variables are placeholders for |
52 |
proper asts*) |
|
53 |
||
54 |
datatype ast = |
|
18 | 55 |
Constant of string | (*"not", "_abs", "fun"*) |
56 |
Variable of string | (*x, ?x, 'a, ?'a*) |
|
57 |
Appl of ast list; (*(f x y z), ("fun" 'a 'b), ("_abs" x t)*) |
|
0 | 58 |
|
59 |
||
60 |
(*the list of subasts of an Appl node has to contain at least 2 elements, i.e. |
|
61 |
there are no empty asts or nullary applications; use mk_appl for convenience*) |
|
62 |
||
18 | 63 |
fun mk_appl f [] = f |
64 |
| mk_appl f args = Appl (f :: args); |
|
0 | 65 |
|
66 |
||
67 |
(*exception for system errors involving asts*) |
|
68 |
||
69 |
exception AST of string * ast list; |
|
70 |
||
71 |
||
72 |
||
18 | 73 |
(** print asts in a LISP-like style **) |
74 |
||
75 |
(* str_of_ast *) |
|
0 | 76 |
|
77 |
fun str_of_ast (Constant a) = quote a |
|
78 |
| str_of_ast (Variable x) = x |
|
79 |
| str_of_ast (Appl asts) = "(" ^ (space_implode " " (map str_of_ast asts)) ^ ")"; |
|
80 |
||
81 |
||
18 | 82 |
(* pretty_ast *) |
83 |
||
84 |
fun pretty_ast (Constant a) = Pretty.str (quote a) |
|
85 |
| pretty_ast (Variable x) = Pretty.str x |
|
86 |
| pretty_ast (Appl asts) = |
|
513 | 87 |
Pretty.enclose "(" ")" (Pretty.breaks (map pretty_ast asts)); |
18 | 88 |
|
89 |
||
90 |
(* pprint_ast *) |
|
91 |
||
92 |
val pprint_ast = Pretty.pprint o pretty_ast; |
|
93 |
||
94 |
||
95 |
(* pretty_rule *) |
|
96 |
||
97 |
fun pretty_rule (lhs, rhs) = |
|
235 | 98 |
Pretty.block [pretty_ast lhs, Pretty.str " ->", Pretty.brk 2, pretty_ast rhs]; |
18 | 99 |
|
100 |
||
0 | 101 |
(* head_of_ast, head_of_rule *) |
102 |
||
9372
7834e56e2277
AST translation rules no longer require constant head on LHS;
wenzelm
parents:
8997
diff
changeset
|
103 |
fun head_of_ast (Constant a) = a |
7834e56e2277
AST translation rules no longer require constant head on LHS;
wenzelm
parents:
8997
diff
changeset
|
104 |
| head_of_ast (Appl (Constant a :: _)) = a |
7834e56e2277
AST translation rules no longer require constant head on LHS;
wenzelm
parents:
8997
diff
changeset
|
105 |
| head_of_ast _ = ""; |
0 | 106 |
|
9372
7834e56e2277
AST translation rules no longer require constant head on LHS;
wenzelm
parents:
8997
diff
changeset
|
107 |
fun head_of_rule (lhs, _) = head_of_ast lhs; |
0 | 108 |
|
109 |
||
110 |
||
18 | 111 |
(** check translation rules **) |
0 | 112 |
|
18 | 113 |
(*a wellformed rule (lhs, rhs): (ast * ast) obeys the following conditions: |
0 | 114 |
- the lhs has unique vars, |
115 |
- vars of rhs is subset of vars of lhs*) |
|
116 |
||
117 |
fun rule_error (rule as (lhs, rhs)) = |
|
118 |
let |
|
119 |
fun vars_of (Constant _) = [] |
|
120 |
| vars_of (Variable x) = [x] |
|
121 |
| vars_of (Appl asts) = flat (map vars_of asts); |
|
122 |
||
123 |
fun unique (x :: xs) = not (x mem xs) andalso unique xs |
|
124 |
| unique [] = true; |
|
125 |
||
126 |
val lvars = vars_of lhs; |
|
127 |
val rvars = vars_of rhs; |
|
128 |
in |
|
9372
7834e56e2277
AST translation rules no longer require constant head on LHS;
wenzelm
parents:
8997
diff
changeset
|
129 |
if not (unique lvars) then Some "duplicate vars in lhs" |
0 | 130 |
else if not (rvars subset lvars) then Some "rhs contains extra variables" |
131 |
else None |
|
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 |
||
144 |
fun fold_ast_p c = foldr (fn (x, xs) => Appl [Constant c, x, xs]); |
|
145 |
||
146 |
||
147 |
(* unfold asts *) |
|
148 |
||
149 |
fun unfold_ast c (y as Appl [Constant c', x, xs]) = |
|
150 |
if c = c' then x :: (unfold_ast c xs) else [y] |
|
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 |
||
159 |
(** normalization of asts **) |
|
160 |
||
161 |
(* match *) |
|
162 |
||
163 |
fun match ast pat = |
|
164 |
let |
|
165 |
exception NO_MATCH; |
|
166 |
||
1127
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
167 |
fun mtch (Constant a) (Constant b) env = |
0 | 168 |
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
|
169 |
| mtch (Variable a) (Constant b) env = |
0 | 170 |
if a = b then env else raise NO_MATCH |
5689 | 171 |
| 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
|
172 |
| 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
|
173 |
| mtch _ _ _ = raise NO_MATCH |
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
174 |
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
|
175 |
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
|
176 |
| mtch_lst [] [] env = env |
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
177 |
| mtch_lst _ _ _ = raise NO_MATCH; |
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
178 |
|
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
179 |
val (head, args) = |
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
180 |
(case (ast, pat) of |
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
181 |
(Appl asts, Appl pats) => |
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
182 |
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
|
183 |
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
|
184 |
else (ast, []) |
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
185 |
end |
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
186 |
| _ => (ast, [])); |
0 | 187 |
in |
5689 | 188 |
Some (mtch head pat Symtab.empty, args) handle NO_MATCH => None |
0 | 189 |
end; |
190 |
||
191 |
||
18 | 192 |
(* normalize *) |
0 | 193 |
|
194 |
(*the normalizer works yoyo-like: top-down, bottom-up, top-down, ...*) |
|
195 |
||
18 | 196 |
fun normalize trace stat get_rules pre_ast = |
0 | 197 |
let |
198 |
val passes = ref 0; |
|
199 |
val failed_matches = ref 0; |
|
200 |
val changes = ref 0; |
|
201 |
||
18 | 202 |
fun subst _ (ast as Constant _) = ast |
5689 | 203 |
| subst env (Variable x) = the (Symtab.lookup (env, x)) |
0 | 204 |
| subst env (Appl asts) = Appl (map (subst env) asts); |
205 |
||
11733 | 206 |
fun try_rules ((lhs, rhs) :: pats) ast = |
0 | 207 |
(case match ast lhs of |
1127
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
208 |
Some (env, args) => |
42ec82147d83
changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents:
922
diff
changeset
|
209 |
(inc changes; Some (mk_appl (subst env rhs) args)) |
11733 | 210 |
| None => (inc failed_matches; try_rules pats ast)) |
211 |
| try_rules [] _ = None; |
|
212 |
val try_headless_rules = try_rules (get_rules ""); |
|
0 | 213 |
|
11733 | 214 |
fun try ast a = |
215 |
(case try_rules (get_rules a) ast of |
|
216 |
None => try_headless_rules ast |
|
217 |
| some => some); |
|
0 | 218 |
|
219 |
fun rewrite (ast as Constant a) = try ast a |
|
220 |
| rewrite (ast as Variable a) = try ast a |
|
221 |
| rewrite (ast as Appl (Constant a :: _)) = try ast a |
|
222 |
| rewrite (ast as Appl (Variable a :: _)) = try ast a |
|
11733 | 223 |
| rewrite ast = try_headless_rules ast; |
0 | 224 |
|
225 |
fun rewrote old_ast new_ast = |
|
12262 | 226 |
conditional trace (fn () => |
227 |
tracing ("rewrote: " ^ str_of_ast old_ast ^ " -> " ^ str_of_ast new_ast)); |
|
0 | 228 |
|
229 |
fun norm_root ast = |
|
230 |
(case rewrite ast of |
|
231 |
Some new_ast => (rewrote ast new_ast; norm_root new_ast) |
|
232 |
| None => ast); |
|
233 |
||
234 |
fun norm ast = |
|
235 |
(case norm_root ast of |
|
236 |
Appl sub_asts => |
|
237 |
let |
|
238 |
val old_changes = ! changes; |
|
239 |
val new_ast = Appl (map norm sub_asts); |
|
240 |
in |
|
241 |
if old_changes = ! changes then new_ast else norm_root new_ast |
|
242 |
end |
|
243 |
| atomic_ast => atomic_ast); |
|
244 |
||
245 |
fun normal ast = |
|
246 |
let |
|
247 |
val old_changes = ! changes; |
|
248 |
val new_ast = norm ast; |
|
249 |
in |
|
250 |
inc passes; |
|
251 |
if old_changes = ! changes then new_ast else normal new_ast |
|
252 |
end; |
|
253 |
||
254 |
||
12262 | 255 |
val _ = conditional trace (fn () => tracing ("pre: " ^ str_of_ast pre_ast)); |
0 | 256 |
|
9372
7834e56e2277
AST translation rules no longer require constant head on LHS;
wenzelm
parents:
8997
diff
changeset
|
257 |
val post_ast = normal pre_ast; |
0 | 258 |
in |
12262 | 259 |
conditional (trace orelse stat) (fn () => |
260 |
tracing ("post: " ^ str_of_ast post_ast ^ "\nnormalize: " ^ |
|
0 | 261 |
string_of_int (! passes) ^ " passes, " ^ |
262 |
string_of_int (! changes) ^ " changes, " ^ |
|
12262 | 263 |
string_of_int (! failed_matches) ^ " matches failed")); |
0 | 264 |
post_ast |
265 |
end; |
|
266 |
||
267 |
||
18 | 268 |
(* normalize_ast *) |
269 |
||
12262 | 270 |
val trace_ast = ref false; |
271 |
val stat_ast = ref false; |
|
272 |
||
18 | 273 |
fun normalize_ast get_rules ast = |
8997 | 274 |
normalize (! trace_ast) (! stat_ast) get_rules ast; |
18 | 275 |
|
1506 | 276 |
end; |
18 | 277 |