author | nipkow |
Sun, 25 Jan 2004 00:42:22 +0100 | |
changeset 14360 | e654599b114e |
parent 13762 | 9dd78dab72bc |
child 14647 | 3f9d3d5cd0cd |
permissions | -rw-r--r-- |
548 | 1 |
(* Title: Pure/Syntax/syn_trans.ML |
2 |
ID: $Id$ |
|
3 |
Author: Tobias Nipkow and Markus Wenzel, TU Muenchen |
|
12785 | 4 |
License: GPL (GNU GENERAL PUBLIC LICENSE) |
548 | 5 |
|
6 |
Syntax translation functions. |
|
7 |
*) |
|
8 |
||
9 |
signature SYN_TRANS0 = |
|
2698 | 10 |
sig |
548 | 11 |
val eta_contract: bool ref |
13762 | 12 |
val atomic_abs_tr': string * typ * term -> term * term |
548 | 13 |
val mk_binder_tr: string * string -> string * (term list -> term) |
14 |
val mk_binder_tr': string * string -> string * (term list -> term) |
|
15 |
val dependent_tr': string * string -> term list -> term |
|
8577 | 16 |
val antiquote_tr: string -> term -> term |
17 |
val quote_tr: string -> term -> term |
|
5084 | 18 |
val quote_antiquote_tr: string -> string -> string -> string * (term list -> term) |
8577 | 19 |
val antiquote_tr': string -> term -> term |
20 |
val quote_tr': string -> term -> term |
|
5084 | 21 |
val quote_antiquote_tr': string -> string -> string -> string * (term list -> term) |
2698 | 22 |
val mark_bound: string -> term |
23 |
val mark_boundT: string * typ -> term |
|
24 |
val variant_abs': string * typ * term -> string * term |
|
25 |
end; |
|
548 | 26 |
|
27 |
signature SYN_TRANS1 = |
|
2698 | 28 |
sig |
548 | 29 |
include SYN_TRANS0 |
1511 | 30 |
val constrainAbsC: string |
31 |
val pure_trfuns: |
|
32 |
(string * (Ast.ast list -> Ast.ast)) list * |
|
548 | 33 |
(string * (term list -> term)) list * |
34 |
(string * (term list -> term)) list * |
|
1511 | 35 |
(string * (Ast.ast list -> Ast.ast)) list |
4148 | 36 |
val pure_trfunsT: (string * (bool -> typ -> term list -> term)) list |
2698 | 37 |
end; |
548 | 38 |
|
39 |
signature SYN_TRANS = |
|
2698 | 40 |
sig |
548 | 41 |
include SYN_TRANS1 |
1511 | 42 |
val abs_tr': term -> term |
4148 | 43 |
val prop_tr': term -> term |
1511 | 44 |
val appl_ast_tr': Ast.ast * Ast.ast list -> Ast.ast |
45 |
val applC_ast_tr': Ast.ast * Ast.ast list -> Ast.ast |
|
46 |
val pt_to_ast: (string -> (Ast.ast list -> Ast.ast) option) -> Parser.parsetree -> Ast.ast |
|
47 |
val ast_to_term: (string -> (term list -> term) option) -> Ast.ast -> term |
|
2698 | 48 |
end; |
548 | 49 |
|
2698 | 50 |
structure SynTrans: SYN_TRANS = |
548 | 51 |
struct |
2698 | 52 |
|
53 |
||
548 | 54 |
(** parse (ast) translations **) |
55 |
||
11491 | 56 |
(* constify *) |
57 |
||
58 |
fun constify_ast_tr [Ast.Variable c] = Ast.Constant c |
|
59 |
| constify_ast_tr asts = raise Ast.AST ("constify_ast_tr", asts); |
|
60 |
||
61 |
||
548 | 62 |
(* application *) |
63 |
||
5690 | 64 |
fun appl_ast_tr [f, args] = Ast.Appl (f :: Ast.unfold_ast "_args" args) |
65 |
| appl_ast_tr asts = raise Ast.AST ("appl_ast_tr", asts); |
|
922
196ca0973a6d
added CPure (curried functions) and ProtoPure (ancestor of Pure and CPure)
clasohm
parents:
639
diff
changeset
|
66 |
|
5690 | 67 |
fun applC_ast_tr [f, args] = Ast.Appl (f :: Ast.unfold_ast "_cargs" args) |
68 |
| applC_ast_tr asts = raise Ast.AST ("applC_ast_tr", asts); |
|
548 | 69 |
|
70 |
||
71 |
(* abstraction *) |
|
72 |
||
5690 | 73 |
fun idtyp_ast_tr (*"_idtyp"*) [x, ty] = Ast.Appl [Ast.Constant SynExt.constrainC, x, ty] |
74 |
| idtyp_ast_tr (*"_idtyp"*) asts = raise Ast.AST ("idtyp_ast_tr", asts); |
|
548 | 75 |
|
3691
f0396ac63e12
tuned lambda_ast_tr, idtyp_ast_tr' to accomodate fix of idt/idts
wenzelm
parents:
2698
diff
changeset
|
76 |
fun lambda_ast_tr (*"_lambda"*) [pats, body] = |
5690 | 77 |
Ast.fold_ast_p "_abs" (Ast.unfold_ast "_pttrns" pats, body) |
78 |
| lambda_ast_tr (*"_lambda"*) asts = raise Ast.AST ("lambda_ast_tr", asts); |
|
548 | 79 |
|
80 |
val constrainAbsC = "_constrainAbs"; |
|
81 |
||
5690 | 82 |
fun abs_tr (*"_abs"*) [Free (x, T), body] = Term.absfree (x, T, body) |
548 | 83 |
| abs_tr (*"_abs"*) (ts as [Const (c, _) $ Free (x, T) $ tT, body]) = |
5690 | 84 |
if c = SynExt.constrainC |
85 |
then Lexicon.const constrainAbsC $ Term.absfree (x, T, body) $ tT |
|
3777 | 86 |
else raise TERM ("abs_tr", ts) |
87 |
| abs_tr (*"_abs"*) ts = raise TERM ("abs_tr", ts); |
|
548 | 88 |
|
89 |
||
90 |
(* nondependent abstraction *) |
|
91 |
||
5690 | 92 |
fun k_tr (*"_K"*) [t] = Abs ("uu", dummyT, Term.incr_boundvars 1 t) |
3777 | 93 |
| k_tr (*"_K"*) ts = raise TERM ("k_tr", ts); |
548 | 94 |
|
95 |
||
96 |
(* binder *) |
|
97 |
||
98 |
fun mk_binder_tr (sy, name) = |
|
99 |
let |
|
5690 | 100 |
fun tr (Free (x, T), t) = Lexicon.const name $ Term.absfree (x, T, t) |
548 | 101 |
| tr (Const ("_idts", _) $ idt $ idts, t) = tr (idt, tr (idts, t)) |
102 |
| tr (t1 as Const (c, _) $ Free (x, T) $ tT, t) = |
|
5690 | 103 |
if c = SynExt.constrainC then |
104 |
Lexicon.const name $ (Lexicon.const constrainAbsC $ Term.absfree (x, T, t) $ tT) |
|
3777 | 105 |
else raise TERM ("binder_tr", [t1, t]) |
106 |
| tr (t1, t2) = raise TERM ("binder_tr", [t1, t2]); |
|
548 | 107 |
|
108 |
fun binder_tr (*sy*) [idts, body] = tr (idts, body) |
|
3777 | 109 |
| binder_tr (*sy*) ts = raise TERM ("binder_tr", ts); |
548 | 110 |
in |
111 |
(sy, binder_tr) |
|
112 |
end; |
|
113 |
||
114 |
||
115 |
(* meta propositions *) |
|
116 |
||
5690 | 117 |
fun aprop_tr (*"_aprop"*) [t] = Lexicon.const SynExt.constrainC $ t $ Lexicon.const "prop" |
3777 | 118 |
| aprop_tr (*"_aprop"*) ts = raise TERM ("aprop_tr", ts); |
548 | 119 |
|
120 |
fun ofclass_tr (*"_ofclass"*) [ty, cls] = |
|
5690 | 121 |
cls $ (Lexicon.const SynExt.constrainC $ Lexicon.const "TYPE" $ |
122 |
(Lexicon.const "itself" $ ty)) |
|
3777 | 123 |
| ofclass_tr (*"_ofclass"*) ts = raise TERM ("ofclass_tr", ts); |
548 | 124 |
|
125 |
||
126 |
(* meta implication *) |
|
127 |
||
128 |
fun bigimpl_ast_tr (*"_bigimpl"*) [asms, concl] = |
|
5690 | 129 |
Ast.fold_ast_p "==>" (Ast.unfold_ast "_asms" asms, concl) |
130 |
| bigimpl_ast_tr (*"_bigimpl"*) asts = raise Ast.AST ("bigimpl_ast_tr", asts); |
|
548 | 131 |
|
132 |
||
4148 | 133 |
(* type reflection *) |
134 |
||
135 |
fun type_tr (*"_TYPE"*) [ty] = |
|
5690 | 136 |
Lexicon.const SynExt.constrainC $ Lexicon.const "TYPE" $ (Lexicon.const "itself" $ ty) |
4148 | 137 |
| type_tr (*"_TYPE"*) ts = raise TERM ("type_tr", ts); |
138 |
||
548 | 139 |
|
6761 | 140 |
(* dddot *) |
141 |
||
8595 | 142 |
fun dddot_tr (*"_DDDOT"*) ts = Term.list_comb (Lexicon.var SynExt.dddot_indexname, ts); |
6761 | 143 |
|
144 |
||
5084 | 145 |
(* quote / antiquote *) |
146 |
||
8577 | 147 |
fun antiquote_tr name = |
148 |
let |
|
149 |
fun tr i ((t as Const (c, _)) $ u) = |
|
150 |
if c = name then tr i u $ Bound i |
|
151 |
else tr i t $ tr i u |
|
152 |
| tr i (t $ u) = tr i t $ tr i u |
|
153 |
| tr i (Abs (x, T, t)) = Abs (x, T, tr (i + 1) t) |
|
154 |
| tr _ a = a; |
|
155 |
in tr 0 end; |
|
156 |
||
157 |
fun quote_tr name t = Abs ("s", dummyT, antiquote_tr name (Term.incr_boundvars 1 t)); |
|
158 |
||
5084 | 159 |
fun quote_antiquote_tr quoteN antiquoteN name = |
160 |
let |
|
8577 | 161 |
fun tr [t] = Lexicon.const name $ quote_tr antiquoteN t |
162 |
| tr ts = raise TERM ("quote_tr", ts); |
|
163 |
in (quoteN, tr) end; |
|
5084 | 164 |
|
165 |
||
12122 | 166 |
(* index variable *) |
167 |
||
168 |
fun indexvar_ast_tr [] = Ast.Variable "some_index" |
|
169 |
| indexvar_ast_tr asts = raise Ast.AST ("indexvar_ast_tr", asts); |
|
170 |
||
171 |
||
5084 | 172 |
|
548 | 173 |
(** print (ast) translations **) |
174 |
||
175 |
(* application *) |
|
176 |
||
5690 | 177 |
fun appl_ast_tr' (f, []) = raise Ast.AST ("appl_ast_tr'", [f]) |
178 |
| appl_ast_tr' (f, args) = Ast.Appl [Ast.Constant "_appl", f, Ast.fold_ast "_args" args]; |
|
548 | 179 |
|
5690 | 180 |
fun applC_ast_tr' (f, []) = raise Ast.AST ("applC_ast_tr'", [f]) |
181 |
| applC_ast_tr' (f, args) = Ast.Appl [Ast.Constant "_applC", f, Ast.fold_ast "_cargs" args]; |
|
922
196ca0973a6d
added CPure (curried functions) and ProtoPure (ancestor of Pure and CPure)
clasohm
parents:
639
diff
changeset
|
182 |
|
548 | 183 |
|
184 |
(* abstraction *) |
|
185 |
||
5690 | 186 |
fun mark_boundT x_T = Lexicon.const "_bound" $ Free x_T; |
2698 | 187 |
fun mark_bound x = mark_boundT (x, dummyT); |
188 |
||
548 | 189 |
fun strip_abss vars_of body_of tm = |
190 |
let |
|
191 |
val vars = vars_of tm; |
|
192 |
val body = body_of tm; |
|
193 |
val rev_new_vars = rename_wrt_term body vars; |
|
194 |
in |
|
2698 | 195 |
(map mark_boundT (rev rev_new_vars), |
196 |
subst_bounds (map (mark_bound o #1) rev_new_vars, body)) |
|
548 | 197 |
end; |
198 |
||
3928
787d2659ce4a
no longer tries bogus eta-contract involving aprops;
wenzelm
parents:
3777
diff
changeset
|
199 |
|
548 | 200 |
(*do (partial) eta-contraction before printing*) |
201 |
||
1326 | 202 |
val eta_contract = ref true; |
548 | 203 |
|
204 |
fun eta_contr tm = |
|
205 |
let |
|
3928
787d2659ce4a
no longer tries bogus eta-contract involving aprops;
wenzelm
parents:
3777
diff
changeset
|
206 |
fun is_aprop (Const ("_aprop", _)) = true |
787d2659ce4a
no longer tries bogus eta-contract involving aprops;
wenzelm
parents:
3777
diff
changeset
|
207 |
| is_aprop _ = false; |
787d2659ce4a
no longer tries bogus eta-contract involving aprops;
wenzelm
parents:
3777
diff
changeset
|
208 |
|
548 | 209 |
fun eta_abs (Abs (a, T, t)) = |
210 |
(case eta_abs t of |
|
211 |
t' as f $ u => |
|
212 |
(case eta_abs u of |
|
213 |
Bound 0 => |
|
5084 | 214 |
if Term.loose_bvar1 (f, 0) orelse is_aprop f then Abs (a, T, t') |
3928
787d2659ce4a
no longer tries bogus eta-contract involving aprops;
wenzelm
parents:
3777
diff
changeset
|
215 |
else incr_boundvars ~1 f |
548 | 216 |
| _ => Abs (a, T, t')) |
217 |
| t' => Abs (a, T, t')) |
|
218 |
| eta_abs t = t; |
|
219 |
in |
|
220 |
if ! eta_contract then eta_abs tm else tm |
|
221 |
end; |
|
222 |
||
223 |
||
224 |
fun abs_tr' tm = |
|
5690 | 225 |
foldr (fn (x, t) => Lexicon.const "_abs" $ x $ t) |
548 | 226 |
(strip_abss strip_abs_vars strip_abs_body (eta_contr tm)); |
227 |
||
228 |
||
13762 | 229 |
fun atomic_abs_tr' (x,T,t) = |
230 |
let val [xT] = rename_wrt_term t [(x,T)]; |
|
231 |
in (mark_boundT xT, subst_bound (mark_bound(fst xT), t)) end; |
|
232 |
||
548 | 233 |
fun abs_ast_tr' (*"_abs"*) asts = |
5690 | 234 |
(case Ast.unfold_ast_p "_abs" (Ast.Appl (Ast.Constant "_abs" :: asts)) of |
235 |
([], _) => raise Ast.AST ("abs_ast_tr'", asts) |
|
236 |
| (xs, body) => Ast.Appl [Ast.Constant "_lambda", Ast.fold_ast "_pttrns" xs, body]); |
|
548 | 237 |
|
238 |
||
239 |
(* binder *) |
|
240 |
||
241 |
fun mk_binder_tr' (name, sy) = |
|
242 |
let |
|
243 |
fun mk_idts [] = raise Match (*abort translation*) |
|
244 |
| mk_idts [idt] = idt |
|
5690 | 245 |
| mk_idts (idt :: idts) = Lexicon.const "_idts" $ idt $ mk_idts idts; |
548 | 246 |
|
247 |
fun tr' t = |
|
248 |
let |
|
249 |
val (xs, bd) = strip_abss (strip_qnt_vars name) (strip_qnt_body name) t; |
|
5690 | 250 |
in Lexicon.const sy $ mk_idts xs $ bd end; |
548 | 251 |
|
5690 | 252 |
fun binder_tr' (*name*) (t :: ts) = Term.list_comb (tr' (Lexicon.const name $ t), ts) |
548 | 253 |
| binder_tr' (*name*) [] = raise Match; |
254 |
in |
|
255 |
(name, binder_tr') |
|
256 |
end; |
|
257 |
||
258 |
||
3691
f0396ac63e12
tuned lambda_ast_tr, idtyp_ast_tr' to accomodate fix of idt/idts
wenzelm
parents:
2698
diff
changeset
|
259 |
(* idtyp constraints *) |
548 | 260 |
|
5690 | 261 |
fun idtyp_ast_tr' a [Ast.Appl [Ast.Constant c, x, ty], xs] = |
262 |
if c = SynExt.constrainC then |
|
263 |
Ast.Appl [ Ast.Constant a, Ast.Appl [Ast.Constant "_idtyp", x, ty], xs] |
|
548 | 264 |
else raise Match |
3691
f0396ac63e12
tuned lambda_ast_tr, idtyp_ast_tr' to accomodate fix of idt/idts
wenzelm
parents:
2698
diff
changeset
|
265 |
| idtyp_ast_tr' _ _ = raise Match; |
548 | 266 |
|
267 |
||
268 |
(* meta propositions *) |
|
269 |
||
4148 | 270 |
fun prop_tr' tm = |
548 | 271 |
let |
5690 | 272 |
fun aprop t = Lexicon.const "_aprop" $ t; |
548 | 273 |
|
2698 | 274 |
fun is_prop Ts t = |
275 |
fastype_of1 (Ts, t) = propT handle TERM _ => false; |
|
548 | 276 |
|
277 |
fun tr' _ (t as Const _) = t |
|
2698 | 278 |
| tr' _ (t as Free (x, T)) = |
5690 | 279 |
if T = propT then aprop (Lexicon.free x) else t |
2698 | 280 |
| tr' _ (t as Var (xi, T)) = |
5690 | 281 |
if T = propT then aprop (Lexicon.var xi) else t |
2698 | 282 |
| tr' Ts (t as Bound _) = |
283 |
if is_prop Ts t then aprop t else t |
|
284 |
| tr' Ts (Abs (x, T, t)) = Abs (x, T, tr' (T :: Ts) t) |
|
285 |
| tr' Ts (t as t1 $ (t2 as Const ("TYPE", Type ("itself", [T])))) = |
|
4148 | 286 |
if is_prop Ts t then Const ("_mk_ofclass", T) $ tr' Ts t1 |
2698 | 287 |
else tr' Ts t1 $ tr' Ts t2 |
288 |
| tr' Ts (t as t1 $ t2) = |
|
5690 | 289 |
(if is_Const (Term.head_of t) orelse not (is_prop Ts t) |
2698 | 290 |
then I else aprop) (tr' Ts t1 $ tr' Ts t2); |
548 | 291 |
in |
292 |
tr' [] tm |
|
293 |
end; |
|
294 |
||
4148 | 295 |
fun mk_ofclass_tr' show_sorts (*"_mk_ofclass"*) T [t] = |
5690 | 296 |
Lexicon.const "_ofclass" $ TypeExt.term_of_typ show_sorts T $ t |
4148 | 297 |
| mk_ofclass_tr' _ (*"_mk_ofclass"*) T ts = raise TYPE ("mk_ofclass_tr'", [T], ts); |
2698 | 298 |
|
299 |
||
548 | 300 |
(* meta implication *) |
301 |
||
302 |
fun impl_ast_tr' (*"==>"*) asts = |
|
10572 | 303 |
if TypeExt.no_brackets () then raise Match |
304 |
else |
|
305 |
(case Ast.unfold_ast_p "==>" (Ast.Appl (Ast.Constant "==>" :: asts)) of |
|
306 |
(asms as _ :: _ :: _, concl) |
|
307 |
=> Ast.Appl [Ast.Constant "_bigimpl", Ast.fold_ast "_asms" asms, concl] |
|
308 |
| _ => raise Match); |
|
548 | 309 |
|
310 |
||
12150 | 311 |
(* meta conjunction *) |
312 |
||
313 |
fun meta_conjunction_tr' (*"all"*) |
|
314 |
[Abs (_, _, Const ("==>", _) $ |
|
315 |
(Const ("==>", _) $ A $ (Const ("==>", _) $ B $ (Const ("_aprop", _) $ Bound 0))) $ |
|
316 |
(Const ("_aprop", _) $ Bound 0))] = |
|
317 |
if 0 mem_int Term.loose_bnos A orelse 0 mem_int Term.loose_bnos B then raise Match |
|
318 |
else Lexicon.const "_meta_conjunction" $ A $ B |
|
319 |
| meta_conjunction_tr' (*"all"*) ts = raise Match; |
|
320 |
||
321 |
||
4148 | 322 |
(* type reflection *) |
323 |
||
324 |
fun type_tr' show_sorts (*"TYPE"*) (Type ("itself", [T])) ts = |
|
5690 | 325 |
Term.list_comb (Lexicon.const "_TYPE" $ TypeExt.term_of_typ show_sorts T, ts) |
4148 | 326 |
| type_tr' _ _ _ = raise Match; |
327 |
||
328 |
||
548 | 329 |
(* dependent / nondependent quantifiers *) |
330 |
||
2698 | 331 |
fun variant_abs' (x, T, B) = |
332 |
let val x' = variant (add_term_names (B, [])) x in |
|
333 |
(x', subst_bound (mark_boundT (x', T), B)) |
|
334 |
end; |
|
335 |
||
548 | 336 |
fun dependent_tr' (q, r) (A :: Abs (x, T, B) :: ts) = |
5084 | 337 |
if Term.loose_bvar1 (B, 0) then |
2698 | 338 |
let val (x', B') = variant_abs' (x, dummyT, B); |
5690 | 339 |
in Term.list_comb (Lexicon.const q $ mark_boundT (x', T) $ A $ B', ts) end |
340 |
else Term.list_comb (Lexicon.const r $ A $ B, ts) |
|
548 | 341 |
| dependent_tr' _ _ = raise Match; |
342 |
||
343 |
||
5084 | 344 |
(* quote / antiquote *) |
345 |
||
8577 | 346 |
fun antiquote_tr' name = |
347 |
let |
|
348 |
fun tr' i (t $ u) = |
|
349 |
if u = Bound i then Lexicon.const name $ tr' i t |
|
350 |
else tr' i t $ tr' i u |
|
351 |
| tr' i (Abs (x, T, t)) = Abs (x, T, tr' (i + 1) t) |
|
352 |
| tr' i a = if a = Bound i then raise Match else a; |
|
353 |
in tr' 0 end; |
|
354 |
||
355 |
fun quote_tr' name (Abs (_, _, t)) = Term.incr_boundvars ~1 (antiquote_tr' name t) |
|
356 |
| quote_tr' _ _ = raise Match; |
|
357 |
||
5084 | 358 |
fun quote_antiquote_tr' quoteN antiquoteN name = |
359 |
let |
|
8577 | 360 |
fun tr' (t :: ts) = Term.list_comb (Lexicon.const quoteN $ quote_tr' antiquoteN t, ts) |
361 |
| tr' _ = raise Match; |
|
362 |
in (name, tr') end; |
|
5084 | 363 |
|
364 |
||
548 | 365 |
|
366 |
(** pure_trfuns **) |
|
367 |
||
368 |
val pure_trfuns = |
|
11491 | 369 |
([("_constify", constify_ast_tr), ("_appl", appl_ast_tr), ("_applC", applC_ast_tr), |
12122 | 370 |
("_lambda", lambda_ast_tr), ("_idtyp", idtyp_ast_tr), ("_bigimpl", bigimpl_ast_tr), |
371 |
("_indexvar", indexvar_ast_tr)], |
|
922
196ca0973a6d
added CPure (curried functions) and ProtoPure (ancestor of Pure and CPure)
clasohm
parents:
639
diff
changeset
|
372 |
[("_abs", abs_tr), ("_aprop", aprop_tr), ("_ofclass", ofclass_tr), |
6761 | 373 |
("_TYPE", type_tr), ("_DDDOT", dddot_tr), ("_K", k_tr)], |
12150 | 374 |
[("all", meta_conjunction_tr')], |
3691
f0396ac63e12
tuned lambda_ast_tr, idtyp_ast_tr' to accomodate fix of idt/idts
wenzelm
parents:
2698
diff
changeset
|
375 |
[("_abs", abs_ast_tr'), ("_idts", idtyp_ast_tr' "_idts"), |
f0396ac63e12
tuned lambda_ast_tr, idtyp_ast_tr' to accomodate fix of idt/idts
wenzelm
parents:
2698
diff
changeset
|
376 |
("_pttrns", idtyp_ast_tr' "_pttrns"), ("==>", impl_ast_tr')]); |
548 | 377 |
|
2698 | 378 |
val pure_trfunsT = |
4148 | 379 |
[("_mk_ofclass", mk_ofclass_tr'), ("TYPE", type_tr')]; |
2698 | 380 |
|
548 | 381 |
|
382 |
||
383 |
(** pt_to_ast **) |
|
384 |
||
385 |
fun pt_to_ast trf pt = |
|
386 |
let |
|
387 |
fun trans a args = |
|
388 |
(case trf a of |
|
5690 | 389 |
None => Ast.mk_appl (Ast.Constant a) args |
548 | 390 |
| Some f => f args handle exn |
987 | 391 |
=> (writeln ("Error in parse ast translation for " ^ quote a); |
392 |
raise exn)); |
|
548 | 393 |
|
987 | 394 |
(*translate pt bottom-up*) |
5690 | 395 |
fun ast_of (Parser.Node (a, pts)) = trans a (map ast_of pts) |
396 |
| ast_of (Parser.Tip tok) = Ast.Variable (Lexicon.str_of_token tok); |
|
548 | 397 |
in |
1178 | 398 |
ast_of pt |
548 | 399 |
end; |
400 |
||
401 |
||
402 |
||
403 |
(** ast_to_term **) |
|
404 |
||
405 |
fun ast_to_term trf ast = |
|
406 |
let |
|
407 |
fun trans a args = |
|
408 |
(case trf a of |
|
5690 | 409 |
None => Term.list_comb (Lexicon.const a, args) |
548 | 410 |
| Some f => f args handle exn |
987 | 411 |
=> (writeln ("Error in parse translation for " ^ quote a); |
412 |
raise exn)); |
|
548 | 413 |
|
5690 | 414 |
fun term_of (Ast.Constant a) = trans a [] |
415 |
| term_of (Ast.Variable x) = Lexicon.read_var x |
|
416 |
| term_of (Ast.Appl (Ast.Constant a :: (asts as _ :: _))) = |
|
548 | 417 |
trans a (map term_of asts) |
5690 | 418 |
| term_of (Ast.Appl (ast :: (asts as _ :: _))) = |
419 |
Term.list_comb (term_of ast, map term_of asts) |
|
420 |
| term_of (ast as Ast.Appl _) = raise Ast.AST ("ast_to_term: malformed ast", [ast]); |
|
548 | 421 |
in |
422 |
term_of ast |
|
423 |
end; |
|
424 |
||
425 |
end; |