author | wenzelm |
Fri, 17 Oct 1997 18:19:14 +0200 | |
changeset 3928 | 787d2659ce4a |
parent 3777 | 434d875f4661 |
child 4148 | e0e5a2820ac1 |
permissions | -rw-r--r-- |
548 | 1 |
(* Title: Pure/Syntax/syn_trans.ML |
2 |
ID: $Id$ |
|
3 |
Author: Tobias Nipkow and Markus Wenzel, TU Muenchen |
|
4 |
||
5 |
Syntax translation functions. |
|
6 |
*) |
|
7 |
||
8 |
signature SYN_TRANS0 = |
|
2698 | 9 |
sig |
548 | 10 |
val eta_contract: bool ref |
11 |
val mk_binder_tr: string * string -> string * (term list -> term) |
|
12 |
val mk_binder_tr': string * string -> string * (term list -> term) |
|
13 |
val dependent_tr': string * string -> term list -> term |
|
2698 | 14 |
val mark_bound: string -> term |
15 |
val mark_boundT: string * typ -> term |
|
16 |
val variant_abs': string * typ * term -> string * term |
|
17 |
end; |
|
548 | 18 |
|
19 |
signature SYN_TRANS1 = |
|
2698 | 20 |
sig |
548 | 21 |
include SYN_TRANS0 |
1511 | 22 |
val constrainAbsC: string |
23 |
val pure_trfuns: |
|
24 |
(string * (Ast.ast list -> Ast.ast)) list * |
|
548 | 25 |
(string * (term list -> term)) list * |
26 |
(string * (term list -> term)) list * |
|
1511 | 27 |
(string * (Ast.ast list -> Ast.ast)) list |
2698 | 28 |
val pure_trfunsT: (string * (typ -> term list -> term)) list |
29 |
end; |
|
548 | 30 |
|
31 |
signature SYN_TRANS = |
|
2698 | 32 |
sig |
548 | 33 |
include SYN_TRANS1 |
1511 | 34 |
val abs_tr': term -> term |
35 |
val prop_tr': bool -> term -> term |
|
36 |
val appl_ast_tr': Ast.ast * Ast.ast list -> Ast.ast |
|
37 |
val applC_ast_tr': Ast.ast * Ast.ast list -> Ast.ast |
|
38 |
val pt_to_ast: (string -> (Ast.ast list -> Ast.ast) option) -> Parser.parsetree -> Ast.ast |
|
39 |
val ast_to_term: (string -> (term list -> term) option) -> Ast.ast -> term |
|
2698 | 40 |
end; |
548 | 41 |
|
2698 | 42 |
structure SynTrans: SYN_TRANS = |
548 | 43 |
struct |
2698 | 44 |
|
1511 | 45 |
open TypeExt Lexicon Ast SynExt Parser; |
548 | 46 |
|
2698 | 47 |
|
548 | 48 |
(** parse (ast) translations **) |
49 |
||
50 |
(* application *) |
|
51 |
||
922
196ca0973a6d
added CPure (curried functions) and ProtoPure (ancestor of Pure and CPure)
clasohm
parents:
639
diff
changeset
|
52 |
fun appl_ast_tr [f, args] = Appl (f :: unfold_ast "_args" args) |
3777 | 53 |
| appl_ast_tr asts = raise AST ("appl_ast_tr", asts); |
922
196ca0973a6d
added CPure (curried functions) and ProtoPure (ancestor of Pure and CPure)
clasohm
parents:
639
diff
changeset
|
54 |
|
1178 | 55 |
fun applC_ast_tr [f, args] = Appl (f :: unfold_ast "_cargs" args) |
3777 | 56 |
| applC_ast_tr asts = raise AST ("applC_ast_tr", asts); |
548 | 57 |
|
58 |
||
59 |
(* abstraction *) |
|
60 |
||
61 |
fun idtyp_ast_tr (*"_idtyp"*) [x, ty] = Appl [Constant constrainC, x, ty] |
|
3777 | 62 |
| idtyp_ast_tr (*"_idtyp"*) asts = raise AST ("idtyp_ast_tr", asts); |
548 | 63 |
|
3691
f0396ac63e12
tuned lambda_ast_tr, idtyp_ast_tr' to accomodate fix of idt/idts
wenzelm
parents:
2698
diff
changeset
|
64 |
fun lambda_ast_tr (*"_lambda"*) [pats, body] = |
f0396ac63e12
tuned lambda_ast_tr, idtyp_ast_tr' to accomodate fix of idt/idts
wenzelm
parents:
2698
diff
changeset
|
65 |
fold_ast_p "_abs" (unfold_ast "_pttrns" pats, body) |
3777 | 66 |
| lambda_ast_tr (*"_lambda"*) asts = raise AST ("lambda_ast_tr", asts); |
548 | 67 |
|
68 |
val constrainAbsC = "_constrainAbs"; |
|
69 |
||
70 |
fun abs_tr (*"_abs"*) [Free (x, T), body] = absfree (x, T, body) |
|
71 |
| abs_tr (*"_abs"*) (ts as [Const (c, _) $ Free (x, T) $ tT, body]) = |
|
72 |
if c = constrainC |
|
73 |
then const constrainAbsC $ absfree (x, T, body) $ tT |
|
3777 | 74 |
else raise TERM ("abs_tr", ts) |
75 |
| abs_tr (*"_abs"*) ts = raise TERM ("abs_tr", ts); |
|
548 | 76 |
|
77 |
||
78 |
(* nondependent abstraction *) |
|
79 |
||
80 |
fun k_tr (*"_K"*) [t] = Abs ("uu", dummyT, incr_boundvars 1 t) |
|
3777 | 81 |
| k_tr (*"_K"*) ts = raise TERM ("k_tr", ts); |
548 | 82 |
|
83 |
||
84 |
(* binder *) |
|
85 |
||
86 |
fun mk_binder_tr (sy, name) = |
|
87 |
let |
|
88 |
fun tr (Free (x, T), t) = const name $ absfree (x, T, t) |
|
89 |
| tr (Const ("_idts", _) $ idt $ idts, t) = tr (idt, tr (idts, t)) |
|
90 |
| tr (t1 as Const (c, _) $ Free (x, T) $ tT, t) = |
|
91 |
if c = constrainC then |
|
92 |
const name $ (const constrainAbsC $ absfree (x, T, t) $ tT) |
|
3777 | 93 |
else raise TERM ("binder_tr", [t1, t]) |
94 |
| tr (t1, t2) = raise TERM ("binder_tr", [t1, t2]); |
|
548 | 95 |
|
96 |
fun binder_tr (*sy*) [idts, body] = tr (idts, body) |
|
3777 | 97 |
| binder_tr (*sy*) ts = raise TERM ("binder_tr", ts); |
548 | 98 |
in |
99 |
(sy, binder_tr) |
|
100 |
end; |
|
101 |
||
102 |
||
103 |
(* meta propositions *) |
|
104 |
||
105 |
fun aprop_tr (*"_aprop"*) [t] = const constrainC $ t $ const "prop" |
|
3777 | 106 |
| aprop_tr (*"_aprop"*) ts = raise TERM ("aprop_tr", ts); |
548 | 107 |
|
108 |
fun ofclass_tr (*"_ofclass"*) [ty, cls] = |
|
109 |
cls $ (const constrainC $ const "TYPE" $ (const "itself" $ ty)) |
|
3777 | 110 |
| ofclass_tr (*"_ofclass"*) ts = raise TERM ("ofclass_tr", ts); |
548 | 111 |
|
112 |
||
113 |
(* meta implication *) |
|
114 |
||
115 |
fun bigimpl_ast_tr (*"_bigimpl"*) [asms, concl] = |
|
116 |
fold_ast_p "==>" (unfold_ast "_asms" asms, concl) |
|
3777 | 117 |
| bigimpl_ast_tr (*"_bigimpl"*) asts = raise AST ("bigimpl_ast_tr", asts); |
548 | 118 |
|
119 |
||
120 |
||
121 |
(** print (ast) translations **) |
|
122 |
||
123 |
(* application *) |
|
124 |
||
3777 | 125 |
fun appl_ast_tr' (f, []) = raise AST ("appl_ast_tr'", [f]) |
548 | 126 |
| appl_ast_tr' (f, args) = Appl [Constant "_appl", f, fold_ast "_args" args]; |
127 |
||
3777 | 128 |
fun applC_ast_tr' (f, []) = raise AST ("applC_ast_tr'", [f]) |
1178 | 129 |
| applC_ast_tr' (f, args) = |
130 |
Appl [Constant "_applC", f, fold_ast "_cargs" args]; |
|
922
196ca0973a6d
added CPure (curried functions) and ProtoPure (ancestor of Pure and CPure)
clasohm
parents:
639
diff
changeset
|
131 |
|
548 | 132 |
|
133 |
(* abstraction *) |
|
134 |
||
2698 | 135 |
fun mark_boundT x_T = const "_bound" $ Free x_T; |
136 |
fun mark_bound x = mark_boundT (x, dummyT); |
|
137 |
||
548 | 138 |
fun strip_abss vars_of body_of tm = |
139 |
let |
|
140 |
val vars = vars_of tm; |
|
141 |
val body = body_of tm; |
|
142 |
val rev_new_vars = rename_wrt_term body vars; |
|
143 |
in |
|
2698 | 144 |
(map mark_boundT (rev rev_new_vars), |
145 |
subst_bounds (map (mark_bound o #1) rev_new_vars, body)) |
|
548 | 146 |
end; |
147 |
||
3928
787d2659ce4a
no longer tries bogus eta-contract involving aprops;
wenzelm
parents:
3777
diff
changeset
|
148 |
|
548 | 149 |
(*do (partial) eta-contraction before printing*) |
150 |
||
1326 | 151 |
val eta_contract = ref true; |
548 | 152 |
|
153 |
fun eta_contr tm = |
|
154 |
let |
|
3928
787d2659ce4a
no longer tries bogus eta-contract involving aprops;
wenzelm
parents:
3777
diff
changeset
|
155 |
fun is_aprop (Const ("_aprop", _)) = true |
787d2659ce4a
no longer tries bogus eta-contract involving aprops;
wenzelm
parents:
3777
diff
changeset
|
156 |
| is_aprop _ = false; |
787d2659ce4a
no longer tries bogus eta-contract involving aprops;
wenzelm
parents:
3777
diff
changeset
|
157 |
|
548 | 158 |
fun eta_abs (Abs (a, T, t)) = |
159 |
(case eta_abs t of |
|
160 |
t' as f $ u => |
|
161 |
(case eta_abs u of |
|
162 |
Bound 0 => |
|
3928
787d2659ce4a
no longer tries bogus eta-contract involving aprops;
wenzelm
parents:
3777
diff
changeset
|
163 |
if 0 mem loose_bnos f orelse is_aprop f then Abs (a, T, t') |
787d2659ce4a
no longer tries bogus eta-contract involving aprops;
wenzelm
parents:
3777
diff
changeset
|
164 |
else incr_boundvars ~1 f |
548 | 165 |
| _ => Abs (a, T, t')) |
166 |
| t' => Abs (a, T, t')) |
|
167 |
| eta_abs t = t; |
|
168 |
in |
|
169 |
if ! eta_contract then eta_abs tm else tm |
|
170 |
end; |
|
171 |
||
172 |
||
173 |
fun abs_tr' tm = |
|
174 |
foldr (fn (x, t) => const "_abs" $ x $ t) |
|
175 |
(strip_abss strip_abs_vars strip_abs_body (eta_contr tm)); |
|
176 |
||
177 |
||
178 |
fun abs_ast_tr' (*"_abs"*) asts = |
|
179 |
(case unfold_ast_p "_abs" (Appl (Constant "_abs" :: asts)) of |
|
3777 | 180 |
([], _) => raise AST ("abs_ast_tr'", asts) |
3691
f0396ac63e12
tuned lambda_ast_tr, idtyp_ast_tr' to accomodate fix of idt/idts
wenzelm
parents:
2698
diff
changeset
|
181 |
| (xs, body) => Appl [Constant "_lambda", fold_ast "_pttrns" xs, body]); |
548 | 182 |
|
183 |
||
184 |
(* binder *) |
|
185 |
||
186 |
fun mk_binder_tr' (name, sy) = |
|
187 |
let |
|
188 |
fun mk_idts [] = raise Match (*abort translation*) |
|
189 |
| mk_idts [idt] = idt |
|
190 |
| mk_idts (idt :: idts) = const "_idts" $ idt $ mk_idts idts; |
|
191 |
||
192 |
fun tr' t = |
|
193 |
let |
|
194 |
val (xs, bd) = strip_abss (strip_qnt_vars name) (strip_qnt_body name) t; |
|
195 |
in |
|
196 |
const sy $ mk_idts xs $ bd |
|
197 |
end; |
|
198 |
||
199 |
fun binder_tr' (*name*) (t :: ts) = |
|
200 |
list_comb (tr' (const name $ t), ts) |
|
201 |
| binder_tr' (*name*) [] = raise Match; |
|
202 |
in |
|
203 |
(name, binder_tr') |
|
204 |
end; |
|
205 |
||
206 |
||
3691
f0396ac63e12
tuned lambda_ast_tr, idtyp_ast_tr' to accomodate fix of idt/idts
wenzelm
parents:
2698
diff
changeset
|
207 |
(* idtyp constraints *) |
548 | 208 |
|
3691
f0396ac63e12
tuned lambda_ast_tr, idtyp_ast_tr' to accomodate fix of idt/idts
wenzelm
parents:
2698
diff
changeset
|
209 |
fun idtyp_ast_tr' a [Appl [Constant c, x, ty], xs] = |
548 | 210 |
if c = constrainC then |
3691
f0396ac63e12
tuned lambda_ast_tr, idtyp_ast_tr' to accomodate fix of idt/idts
wenzelm
parents:
2698
diff
changeset
|
211 |
Appl [Constant a, Appl [Constant "_idtyp", x, ty], xs] |
548 | 212 |
else raise Match |
3691
f0396ac63e12
tuned lambda_ast_tr, idtyp_ast_tr' to accomodate fix of idt/idts
wenzelm
parents:
2698
diff
changeset
|
213 |
| idtyp_ast_tr' _ _ = raise Match; |
548 | 214 |
|
215 |
||
216 |
(* meta propositions *) |
|
217 |
||
218 |
fun prop_tr' show_sorts tm = |
|
219 |
let |
|
220 |
fun aprop t = const "_aprop" $ t; |
|
2698 | 221 |
val mk_ofclass = if show_sorts then "_mk_ofclassS" else "_mk_ofclass"; |
548 | 222 |
|
2698 | 223 |
fun is_prop Ts t = |
224 |
fastype_of1 (Ts, t) = propT handle TERM _ => false; |
|
548 | 225 |
|
226 |
fun tr' _ (t as Const _) = t |
|
2698 | 227 |
| tr' _ (t as Free (x, T)) = |
228 |
if T = propT then aprop (free x) else t |
|
229 |
| tr' _ (t as Var (xi, T)) = |
|
230 |
if T = propT then aprop (var xi) else t |
|
231 |
| tr' Ts (t as Bound _) = |
|
232 |
if is_prop Ts t then aprop t else t |
|
233 |
| tr' Ts (Abs (x, T, t)) = Abs (x, T, tr' (T :: Ts) t) |
|
234 |
| tr' Ts (t as t1 $ (t2 as Const ("TYPE", Type ("itself", [T])))) = |
|
235 |
if is_prop Ts t then Const (mk_ofclass, T) $ tr' Ts t1 |
|
236 |
else tr' Ts t1 $ tr' Ts t2 |
|
237 |
| tr' Ts (t as t1 $ t2) = |
|
238 |
(if is_Const (head_of t) orelse not (is_prop Ts t) |
|
239 |
then I else aprop) (tr' Ts t1 $ tr' Ts t2); |
|
548 | 240 |
in |
241 |
tr' [] tm |
|
242 |
end; |
|
243 |
||
244 |
||
2698 | 245 |
fun mk_ofclass_tr' (*"_mk_ofclass"*) T [t] = |
246 |
const "_ofclass" $ term_of_typ false T $ t |
|
3777 | 247 |
| mk_ofclass_tr' (*"_mk_ofclass"*) T ts = raise TYPE ("mk_ofclass_tr'", [T], ts); |
2698 | 248 |
|
249 |
fun mk_ofclassS_tr' (*"_mk_ofclassS"*) T [t] = |
|
250 |
const "_ofclass" $ term_of_typ true T $ t |
|
3777 | 251 |
| mk_ofclassS_tr' (*"_mk_ofclassS"*) T ts = raise TYPE ("mk_ofclassS_tr'", [T], ts); |
2698 | 252 |
|
253 |
||
548 | 254 |
(* meta implication *) |
255 |
||
256 |
fun impl_ast_tr' (*"==>"*) asts = |
|
257 |
(case unfold_ast_p "==>" (Appl (Constant "==>" :: asts)) of |
|
258 |
(asms as _ :: _ :: _, concl) |
|
259 |
=> Appl [Constant "_bigimpl", fold_ast "_asms" asms, concl] |
|
260 |
| _ => raise Match); |
|
261 |
||
262 |
||
263 |
(* dependent / nondependent quantifiers *) |
|
264 |
||
2698 | 265 |
fun variant_abs' (x, T, B) = |
266 |
let val x' = variant (add_term_names (B, [])) x in |
|
267 |
(x', subst_bound (mark_boundT (x', T), B)) |
|
268 |
end; |
|
269 |
||
548 | 270 |
fun dependent_tr' (q, r) (A :: Abs (x, T, B) :: ts) = |
271 |
if 0 mem (loose_bnos B) then |
|
2698 | 272 |
let val (x', B') = variant_abs' (x, dummyT, B); |
273 |
in list_comb (const q $ mark_boundT (x', T) $ A $ B', ts) end |
|
548 | 274 |
else list_comb (const r $ A $ B, ts) |
275 |
| dependent_tr' _ _ = raise Match; |
|
276 |
||
277 |
||
278 |
||
279 |
(** pure_trfuns **) |
|
280 |
||
281 |
val pure_trfuns = |
|
922
196ca0973a6d
added CPure (curried functions) and ProtoPure (ancestor of Pure and CPure)
clasohm
parents:
639
diff
changeset
|
282 |
([("_appl", appl_ast_tr), ("_applC", applC_ast_tr), |
196ca0973a6d
added CPure (curried functions) and ProtoPure (ancestor of Pure and CPure)
clasohm
parents:
639
diff
changeset
|
283 |
("_lambda", lambda_ast_tr), ("_idtyp", idtyp_ast_tr), |
196ca0973a6d
added CPure (curried functions) and ProtoPure (ancestor of Pure and CPure)
clasohm
parents:
639
diff
changeset
|
284 |
("_bigimpl", bigimpl_ast_tr)], |
196ca0973a6d
added CPure (curried functions) and ProtoPure (ancestor of Pure and CPure)
clasohm
parents:
639
diff
changeset
|
285 |
[("_abs", abs_tr), ("_aprop", aprop_tr), ("_ofclass", ofclass_tr), |
196ca0973a6d
added CPure (curried functions) and ProtoPure (ancestor of Pure and CPure)
clasohm
parents:
639
diff
changeset
|
286 |
("_K", k_tr)], |
3700 | 287 |
[]: (string * (term list -> term)) list, |
3691
f0396ac63e12
tuned lambda_ast_tr, idtyp_ast_tr' to accomodate fix of idt/idts
wenzelm
parents:
2698
diff
changeset
|
288 |
[("_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
|
289 |
("_pttrns", idtyp_ast_tr' "_pttrns"), ("==>", impl_ast_tr')]); |
548 | 290 |
|
2698 | 291 |
val pure_trfunsT = |
292 |
[("_mk_ofclass", mk_ofclass_tr'), ("_mk_ofclassS", mk_ofclassS_tr')]; |
|
293 |
||
548 | 294 |
|
295 |
||
296 |
(** pt_to_ast **) |
|
297 |
||
298 |
fun pt_to_ast trf pt = |
|
299 |
let |
|
300 |
fun trans a args = |
|
301 |
(case trf a of |
|
302 |
None => mk_appl (Constant a) args |
|
303 |
| Some f => f args handle exn |
|
987 | 304 |
=> (writeln ("Error in parse ast translation for " ^ quote a); |
305 |
raise exn)); |
|
548 | 306 |
|
987 | 307 |
(*translate pt bottom-up*) |
548 | 308 |
fun ast_of (Node (a, pts)) = trans a (map ast_of pts) |
309 |
| ast_of (Tip tok) = Variable (str_of_token tok); |
|
310 |
in |
|
1178 | 311 |
ast_of pt |
548 | 312 |
end; |
313 |
||
314 |
||
315 |
||
316 |
(** ast_to_term **) |
|
317 |
||
318 |
fun ast_to_term trf ast = |
|
319 |
let |
|
320 |
fun trans a args = |
|
321 |
(case trf a of |
|
322 |
None => list_comb (const a, args) |
|
323 |
| Some f => f args handle exn |
|
987 | 324 |
=> (writeln ("Error in parse translation for " ^ quote a); |
325 |
raise exn)); |
|
548 | 326 |
|
327 |
fun term_of (Constant a) = trans a [] |
|
328 |
| term_of (Variable x) = scan_var x |
|
329 |
| term_of (Appl (Constant a :: (asts as _ :: _))) = |
|
330 |
trans a (map term_of asts) |
|
331 |
| term_of (Appl (ast :: (asts as _ :: _))) = |
|
332 |
list_comb (term_of ast, map term_of asts) |
|
3777 | 333 |
| term_of (ast as Appl _) = raise AST ("ast_to_term: malformed ast", [ast]); |
548 | 334 |
in |
335 |
term_of ast |
|
336 |
end; |
|
337 |
||
338 |
end; |