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