author | wenzelm |
Mon, 09 Mar 1998 16:11:28 +0100 | |
changeset 4700 | 20ade76722d6 |
parent 4148 | e0e5a2820ac1 |
child 5084 | a676ada3b380 |
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 |
4148 | 28 |
val pure_trfunsT: (string * (bool -> typ -> term list -> term)) list |
2698 | 29 |
end; |
548 | 30 |
|
31 |
signature SYN_TRANS = |
|
2698 | 32 |
sig |
548 | 33 |
include SYN_TRANS1 |
1511 | 34 |
val abs_tr': term -> term |
4148 | 35 |
val prop_tr': term -> term |
1511 | 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 |
||
4148 | 120 |
(* type reflection *) |
121 |
||
122 |
fun type_tr (*"_TYPE"*) [ty] = |
|
123 |
const constrainC $ const "TYPE" $ (const "itself" $ ty) |
|
124 |
| type_tr (*"_TYPE"*) ts = raise TERM ("type_tr", ts); |
|
125 |
||
548 | 126 |
|
127 |
(** print (ast) translations **) |
|
128 |
||
129 |
(* application *) |
|
130 |
||
3777 | 131 |
fun appl_ast_tr' (f, []) = raise AST ("appl_ast_tr'", [f]) |
548 | 132 |
| appl_ast_tr' (f, args) = Appl [Constant "_appl", f, fold_ast "_args" args]; |
133 |
||
3777 | 134 |
fun applC_ast_tr' (f, []) = raise AST ("applC_ast_tr'", [f]) |
1178 | 135 |
| applC_ast_tr' (f, args) = |
136 |
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
|
137 |
|
548 | 138 |
|
139 |
(* abstraction *) |
|
140 |
||
2698 | 141 |
fun mark_boundT x_T = const "_bound" $ Free x_T; |
142 |
fun mark_bound x = mark_boundT (x, dummyT); |
|
143 |
||
548 | 144 |
fun strip_abss vars_of body_of tm = |
145 |
let |
|
146 |
val vars = vars_of tm; |
|
147 |
val body = body_of tm; |
|
148 |
val rev_new_vars = rename_wrt_term body vars; |
|
149 |
in |
|
2698 | 150 |
(map mark_boundT (rev rev_new_vars), |
151 |
subst_bounds (map (mark_bound o #1) rev_new_vars, body)) |
|
548 | 152 |
end; |
153 |
||
3928
787d2659ce4a
no longer tries bogus eta-contract involving aprops;
wenzelm
parents:
3777
diff
changeset
|
154 |
|
548 | 155 |
(*do (partial) eta-contraction before printing*) |
156 |
||
1326 | 157 |
val eta_contract = ref true; |
548 | 158 |
|
159 |
fun eta_contr tm = |
|
160 |
let |
|
3928
787d2659ce4a
no longer tries bogus eta-contract involving aprops;
wenzelm
parents:
3777
diff
changeset
|
161 |
fun is_aprop (Const ("_aprop", _)) = true |
787d2659ce4a
no longer tries bogus eta-contract involving aprops;
wenzelm
parents:
3777
diff
changeset
|
162 |
| is_aprop _ = false; |
787d2659ce4a
no longer tries bogus eta-contract involving aprops;
wenzelm
parents:
3777
diff
changeset
|
163 |
|
548 | 164 |
fun eta_abs (Abs (a, T, t)) = |
165 |
(case eta_abs t of |
|
166 |
t' as f $ u => |
|
167 |
(case eta_abs u of |
|
168 |
Bound 0 => |
|
3928
787d2659ce4a
no longer tries bogus eta-contract involving aprops;
wenzelm
parents:
3777
diff
changeset
|
169 |
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
|
170 |
else incr_boundvars ~1 f |
548 | 171 |
| _ => Abs (a, T, t')) |
172 |
| t' => Abs (a, T, t')) |
|
173 |
| eta_abs t = t; |
|
174 |
in |
|
175 |
if ! eta_contract then eta_abs tm else tm |
|
176 |
end; |
|
177 |
||
178 |
||
179 |
fun abs_tr' tm = |
|
180 |
foldr (fn (x, t) => const "_abs" $ x $ t) |
|
181 |
(strip_abss strip_abs_vars strip_abs_body (eta_contr tm)); |
|
182 |
||
183 |
||
184 |
fun abs_ast_tr' (*"_abs"*) asts = |
|
185 |
(case unfold_ast_p "_abs" (Appl (Constant "_abs" :: asts)) of |
|
3777 | 186 |
([], _) => 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
|
187 |
| (xs, body) => Appl [Constant "_lambda", fold_ast "_pttrns" xs, body]); |
548 | 188 |
|
189 |
||
190 |
(* binder *) |
|
191 |
||
192 |
fun mk_binder_tr' (name, sy) = |
|
193 |
let |
|
194 |
fun mk_idts [] = raise Match (*abort translation*) |
|
195 |
| mk_idts [idt] = idt |
|
196 |
| mk_idts (idt :: idts) = const "_idts" $ idt $ mk_idts idts; |
|
197 |
||
198 |
fun tr' t = |
|
199 |
let |
|
200 |
val (xs, bd) = strip_abss (strip_qnt_vars name) (strip_qnt_body name) t; |
|
201 |
in |
|
202 |
const sy $ mk_idts xs $ bd |
|
203 |
end; |
|
204 |
||
205 |
fun binder_tr' (*name*) (t :: ts) = |
|
206 |
list_comb (tr' (const name $ t), ts) |
|
207 |
| binder_tr' (*name*) [] = raise Match; |
|
208 |
in |
|
209 |
(name, binder_tr') |
|
210 |
end; |
|
211 |
||
212 |
||
3691
f0396ac63e12
tuned lambda_ast_tr, idtyp_ast_tr' to accomodate fix of idt/idts
wenzelm
parents:
2698
diff
changeset
|
213 |
(* idtyp constraints *) |
548 | 214 |
|
3691
f0396ac63e12
tuned lambda_ast_tr, idtyp_ast_tr' to accomodate fix of idt/idts
wenzelm
parents:
2698
diff
changeset
|
215 |
fun idtyp_ast_tr' a [Appl [Constant c, x, ty], xs] = |
548 | 216 |
if c = constrainC then |
3691
f0396ac63e12
tuned lambda_ast_tr, idtyp_ast_tr' to accomodate fix of idt/idts
wenzelm
parents:
2698
diff
changeset
|
217 |
Appl [Constant a, Appl [Constant "_idtyp", x, ty], xs] |
548 | 218 |
else raise Match |
3691
f0396ac63e12
tuned lambda_ast_tr, idtyp_ast_tr' to accomodate fix of idt/idts
wenzelm
parents:
2698
diff
changeset
|
219 |
| idtyp_ast_tr' _ _ = raise Match; |
548 | 220 |
|
221 |
||
222 |
(* meta propositions *) |
|
223 |
||
4148 | 224 |
fun prop_tr' tm = |
548 | 225 |
let |
226 |
fun aprop t = const "_aprop" $ t; |
|
227 |
||
2698 | 228 |
fun is_prop Ts t = |
229 |
fastype_of1 (Ts, t) = propT handle TERM _ => false; |
|
548 | 230 |
|
231 |
fun tr' _ (t as Const _) = t |
|
2698 | 232 |
| tr' _ (t as Free (x, T)) = |
233 |
if T = propT then aprop (free x) else t |
|
234 |
| tr' _ (t as Var (xi, T)) = |
|
235 |
if T = propT then aprop (var xi) else t |
|
236 |
| tr' Ts (t as Bound _) = |
|
237 |
if is_prop Ts t then aprop t else t |
|
238 |
| tr' Ts (Abs (x, T, t)) = Abs (x, T, tr' (T :: Ts) t) |
|
239 |
| tr' Ts (t as t1 $ (t2 as Const ("TYPE", Type ("itself", [T])))) = |
|
4148 | 240 |
if is_prop Ts t then Const ("_mk_ofclass", T) $ tr' Ts t1 |
2698 | 241 |
else tr' Ts t1 $ tr' Ts t2 |
242 |
| tr' Ts (t as t1 $ t2) = |
|
243 |
(if is_Const (head_of t) orelse not (is_prop Ts t) |
|
244 |
then I else aprop) (tr' Ts t1 $ tr' Ts t2); |
|
548 | 245 |
in |
246 |
tr' [] tm |
|
247 |
end; |
|
248 |
||
4148 | 249 |
fun mk_ofclass_tr' show_sorts (*"_mk_ofclass"*) T [t] = |
250 |
const "_ofclass" $ term_of_typ show_sorts T $ t |
|
251 |
| mk_ofclass_tr' _ (*"_mk_ofclass"*) T ts = raise TYPE ("mk_ofclass_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 |
||
4148 | 263 |
(* type reflection *) |
264 |
||
265 |
fun type_tr' show_sorts (*"TYPE"*) (Type ("itself", [T])) ts = |
|
266 |
list_comb (const "_TYPE" $ term_of_typ show_sorts T, ts) |
|
267 |
| type_tr' _ _ _ = raise Match; |
|
268 |
||
269 |
||
548 | 270 |
(* dependent / nondependent quantifiers *) |
271 |
||
2698 | 272 |
fun variant_abs' (x, T, B) = |
273 |
let val x' = variant (add_term_names (B, [])) x in |
|
274 |
(x', subst_bound (mark_boundT (x', T), B)) |
|
275 |
end; |
|
276 |
||
548 | 277 |
fun dependent_tr' (q, r) (A :: Abs (x, T, B) :: ts) = |
278 |
if 0 mem (loose_bnos B) then |
|
2698 | 279 |
let val (x', B') = variant_abs' (x, dummyT, B); |
280 |
in list_comb (const q $ mark_boundT (x', T) $ A $ B', ts) end |
|
548 | 281 |
else list_comb (const r $ A $ B, ts) |
282 |
| dependent_tr' _ _ = raise Match; |
|
283 |
||
284 |
||
285 |
||
286 |
(** pure_trfuns **) |
|
287 |
||
288 |
val pure_trfuns = |
|
922
196ca0973a6d
added CPure (curried functions) and ProtoPure (ancestor of Pure and CPure)
clasohm
parents:
639
diff
changeset
|
289 |
([("_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
|
290 |
("_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
|
291 |
("_bigimpl", bigimpl_ast_tr)], |
196ca0973a6d
added CPure (curried functions) and ProtoPure (ancestor of Pure and CPure)
clasohm
parents:
639
diff
changeset
|
292 |
[("_abs", abs_tr), ("_aprop", aprop_tr), ("_ofclass", ofclass_tr), |
4148 | 293 |
("_TYPE", type_tr), ("_K", k_tr)], |
3700 | 294 |
[]: (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
|
295 |
[("_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
|
296 |
("_pttrns", idtyp_ast_tr' "_pttrns"), ("==>", impl_ast_tr')]); |
548 | 297 |
|
2698 | 298 |
val pure_trfunsT = |
4148 | 299 |
[("_mk_ofclass", mk_ofclass_tr'), ("TYPE", type_tr')]; |
2698 | 300 |
|
548 | 301 |
|
302 |
||
303 |
(** pt_to_ast **) |
|
304 |
||
305 |
fun pt_to_ast trf pt = |
|
306 |
let |
|
307 |
fun trans a args = |
|
308 |
(case trf a of |
|
309 |
None => mk_appl (Constant a) args |
|
310 |
| Some f => f args handle exn |
|
987 | 311 |
=> (writeln ("Error in parse ast translation for " ^ quote a); |
312 |
raise exn)); |
|
548 | 313 |
|
987 | 314 |
(*translate pt bottom-up*) |
548 | 315 |
fun ast_of (Node (a, pts)) = trans a (map ast_of pts) |
316 |
| ast_of (Tip tok) = Variable (str_of_token tok); |
|
317 |
in |
|
1178 | 318 |
ast_of pt |
548 | 319 |
end; |
320 |
||
321 |
||
322 |
||
323 |
(** ast_to_term **) |
|
324 |
||
325 |
fun ast_to_term trf ast = |
|
326 |
let |
|
327 |
fun trans a args = |
|
328 |
(case trf a of |
|
329 |
None => list_comb (const a, args) |
|
330 |
| Some f => f args handle exn |
|
987 | 331 |
=> (writeln ("Error in parse translation for " ^ quote a); |
332 |
raise exn)); |
|
548 | 333 |
|
334 |
fun term_of (Constant a) = trans a [] |
|
4700 | 335 |
| term_of (Variable x) = read_var x |
548 | 336 |
| term_of (Appl (Constant a :: (asts as _ :: _))) = |
337 |
trans a (map term_of asts) |
|
338 |
| term_of (Appl (ast :: (asts as _ :: _))) = |
|
339 |
list_comb (term_of ast, map term_of asts) |
|
3777 | 340 |
| term_of (ast as Appl _) = raise AST ("ast_to_term: malformed ast", [ast]); |
548 | 341 |
in |
342 |
term_of ast |
|
343 |
end; |
|
344 |
||
345 |
end; |