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