42 end; |
42 end; |
43 |
43 |
44 structure SynTrans: SYN_TRANS = |
44 structure SynTrans: SYN_TRANS = |
45 struct |
45 struct |
46 |
46 |
47 open TypeExt Lexicon Ast SynExt Parser; |
|
48 |
|
49 |
47 |
50 (** parse (ast) translations **) |
48 (** parse (ast) translations **) |
51 |
49 |
52 (* application *) |
50 (* application *) |
53 |
51 |
54 fun appl_ast_tr [f, args] = Appl (f :: unfold_ast "_args" args) |
52 fun appl_ast_tr [f, args] = Ast.Appl (f :: Ast.unfold_ast "_args" args) |
55 | appl_ast_tr asts = raise AST ("appl_ast_tr", asts); |
53 | appl_ast_tr asts = raise Ast.AST ("appl_ast_tr", asts); |
56 |
54 |
57 fun applC_ast_tr [f, args] = Appl (f :: unfold_ast "_cargs" args) |
55 fun applC_ast_tr [f, args] = Ast.Appl (f :: Ast.unfold_ast "_cargs" args) |
58 | applC_ast_tr asts = raise AST ("applC_ast_tr", asts); |
56 | applC_ast_tr asts = raise Ast.AST ("applC_ast_tr", asts); |
59 |
57 |
60 |
58 |
61 (* abstraction *) |
59 (* abstraction *) |
62 |
60 |
63 fun idtyp_ast_tr (*"_idtyp"*) [x, ty] = Appl [Constant constrainC, x, ty] |
61 fun idtyp_ast_tr (*"_idtyp"*) [x, ty] = Ast.Appl [Ast.Constant SynExt.constrainC, x, ty] |
64 | idtyp_ast_tr (*"_idtyp"*) asts = raise AST ("idtyp_ast_tr", asts); |
62 | idtyp_ast_tr (*"_idtyp"*) asts = raise Ast.AST ("idtyp_ast_tr", asts); |
65 |
63 |
66 fun lambda_ast_tr (*"_lambda"*) [pats, body] = |
64 fun lambda_ast_tr (*"_lambda"*) [pats, body] = |
67 fold_ast_p "_abs" (unfold_ast "_pttrns" pats, body) |
65 Ast.fold_ast_p "_abs" (Ast.unfold_ast "_pttrns" pats, body) |
68 | lambda_ast_tr (*"_lambda"*) asts = raise AST ("lambda_ast_tr", asts); |
66 | lambda_ast_tr (*"_lambda"*) asts = raise Ast.AST ("lambda_ast_tr", asts); |
69 |
67 |
70 val constrainAbsC = "_constrainAbs"; |
68 val constrainAbsC = "_constrainAbs"; |
71 |
69 |
72 fun abs_tr (*"_abs"*) [Free (x, T), body] = absfree (x, T, body) |
70 fun abs_tr (*"_abs"*) [Free (x, T), body] = Term.absfree (x, T, body) |
73 | abs_tr (*"_abs"*) (ts as [Const (c, _) $ Free (x, T) $ tT, body]) = |
71 | abs_tr (*"_abs"*) (ts as [Const (c, _) $ Free (x, T) $ tT, body]) = |
74 if c = constrainC |
72 if c = SynExt.constrainC |
75 then const constrainAbsC $ absfree (x, T, body) $ tT |
73 then Lexicon.const constrainAbsC $ Term.absfree (x, T, body) $ tT |
76 else raise TERM ("abs_tr", ts) |
74 else raise TERM ("abs_tr", ts) |
77 | abs_tr (*"_abs"*) ts = raise TERM ("abs_tr", ts); |
75 | abs_tr (*"_abs"*) ts = raise TERM ("abs_tr", ts); |
78 |
76 |
79 |
77 |
80 (* nondependent abstraction *) |
78 (* nondependent abstraction *) |
81 |
79 |
82 fun k_tr (*"_K"*) [t] = Abs ("uu", dummyT, incr_boundvars 1 t) |
80 fun k_tr (*"_K"*) [t] = Abs ("uu", dummyT, Term.incr_boundvars 1 t) |
83 | k_tr (*"_K"*) ts = raise TERM ("k_tr", ts); |
81 | k_tr (*"_K"*) ts = raise TERM ("k_tr", ts); |
84 |
82 |
85 |
83 |
86 (* binder *) |
84 (* binder *) |
87 |
85 |
88 fun mk_binder_tr (sy, name) = |
86 fun mk_binder_tr (sy, name) = |
89 let |
87 let |
90 fun tr (Free (x, T), t) = const name $ absfree (x, T, t) |
88 fun tr (Free (x, T), t) = Lexicon.const name $ Term.absfree (x, T, t) |
91 | tr (Const ("_idts", _) $ idt $ idts, t) = tr (idt, tr (idts, t)) |
89 | tr (Const ("_idts", _) $ idt $ idts, t) = tr (idt, tr (idts, t)) |
92 | tr (t1 as Const (c, _) $ Free (x, T) $ tT, t) = |
90 | tr (t1 as Const (c, _) $ Free (x, T) $ tT, t) = |
93 if c = constrainC then |
91 if c = SynExt.constrainC then |
94 const name $ (const constrainAbsC $ absfree (x, T, t) $ tT) |
92 Lexicon.const name $ (Lexicon.const constrainAbsC $ Term.absfree (x, T, t) $ tT) |
95 else raise TERM ("binder_tr", [t1, t]) |
93 else raise TERM ("binder_tr", [t1, t]) |
96 | tr (t1, t2) = raise TERM ("binder_tr", [t1, t2]); |
94 | tr (t1, t2) = raise TERM ("binder_tr", [t1, t2]); |
97 |
95 |
98 fun binder_tr (*sy*) [idts, body] = tr (idts, body) |
96 fun binder_tr (*sy*) [idts, body] = tr (idts, body) |
99 | binder_tr (*sy*) ts = raise TERM ("binder_tr", ts); |
97 | binder_tr (*sy*) ts = raise TERM ("binder_tr", ts); |
102 end; |
100 end; |
103 |
101 |
104 |
102 |
105 (* meta propositions *) |
103 (* meta propositions *) |
106 |
104 |
107 fun aprop_tr (*"_aprop"*) [t] = const constrainC $ t $ const "prop" |
105 fun aprop_tr (*"_aprop"*) [t] = Lexicon.const SynExt.constrainC $ t $ Lexicon.const "prop" |
108 | aprop_tr (*"_aprop"*) ts = raise TERM ("aprop_tr", ts); |
106 | aprop_tr (*"_aprop"*) ts = raise TERM ("aprop_tr", ts); |
109 |
107 |
110 fun ofclass_tr (*"_ofclass"*) [ty, cls] = |
108 fun ofclass_tr (*"_ofclass"*) [ty, cls] = |
111 cls $ (const constrainC $ const "TYPE" $ (const "itself" $ ty)) |
109 cls $ (Lexicon.const SynExt.constrainC $ Lexicon.const "TYPE" $ |
|
110 (Lexicon.const "itself" $ ty)) |
112 | ofclass_tr (*"_ofclass"*) ts = raise TERM ("ofclass_tr", ts); |
111 | ofclass_tr (*"_ofclass"*) ts = raise TERM ("ofclass_tr", ts); |
113 |
112 |
114 |
113 |
115 (* meta implication *) |
114 (* meta implication *) |
116 |
115 |
117 fun bigimpl_ast_tr (*"_bigimpl"*) [asms, concl] = |
116 fun bigimpl_ast_tr (*"_bigimpl"*) [asms, concl] = |
118 fold_ast_p "==>" (unfold_ast "_asms" asms, concl) |
117 Ast.fold_ast_p "==>" (Ast.unfold_ast "_asms" asms, concl) |
119 | bigimpl_ast_tr (*"_bigimpl"*) asts = raise AST ("bigimpl_ast_tr", asts); |
118 | bigimpl_ast_tr (*"_bigimpl"*) asts = raise Ast.AST ("bigimpl_ast_tr", asts); |
120 |
119 |
121 |
120 |
122 (* type reflection *) |
121 (* type reflection *) |
123 |
122 |
124 fun type_tr (*"_TYPE"*) [ty] = |
123 fun type_tr (*"_TYPE"*) [ty] = |
125 const constrainC $ const "TYPE" $ (const "itself" $ ty) |
124 Lexicon.const SynExt.constrainC $ Lexicon.const "TYPE" $ (Lexicon.const "itself" $ ty) |
126 | type_tr (*"_TYPE"*) ts = raise TERM ("type_tr", ts); |
125 | type_tr (*"_TYPE"*) ts = raise TERM ("type_tr", ts); |
127 |
126 |
128 |
127 |
129 (* binds *) |
128 (* binds *) |
130 |
129 |
131 fun bind_ast_tr (*"_BIND"*) [Variable x] = Variable (string_of_vname (binding x, 0)) |
130 fun bind_ast_tr (*"_BIND"*) [Ast.Variable x] = |
132 | bind_ast_tr (*"_BIND"*) asts = raise AST ("bind_ast_tr", asts); |
131 Ast.Variable (Lexicon.string_of_vname (Lexicon.binding x, 0)) |
|
132 | bind_ast_tr (*"_BIND"*) asts = raise Ast.AST ("bind_ast_tr", asts); |
133 |
133 |
134 |
134 |
135 (* quote / antiquote *) |
135 (* quote / antiquote *) |
136 |
136 |
137 fun quote_antiquote_tr quoteN antiquoteN name = |
137 fun quote_antiquote_tr quoteN antiquoteN name = |
141 else a $ antiquote_tr i t |
141 else a $ antiquote_tr i t |
142 | antiquote_tr i (t $ u) = antiquote_tr i t $ antiquote_tr i u |
142 | antiquote_tr i (t $ u) = antiquote_tr i t $ antiquote_tr i u |
143 | antiquote_tr i (Abs (x, T, t)) = Abs (x, T, antiquote_tr (i + 1) t) |
143 | antiquote_tr i (Abs (x, T, t)) = Abs (x, T, antiquote_tr (i + 1) t) |
144 | antiquote_tr _ a = a; |
144 | antiquote_tr _ a = a; |
145 |
145 |
146 fun quote_tr [t] = const name $ Abs ("uu", dummyT, antiquote_tr 0 (incr_boundvars 1 t)) |
146 fun quote_tr [t] = Lexicon.const name $ |
|
147 Abs ("uu", dummyT, antiquote_tr 0 (Term.incr_boundvars 1 t)) |
147 | quote_tr ts = raise TERM ("quote_tr", ts); |
148 | quote_tr ts = raise TERM ("quote_tr", ts); |
148 in (quoteN, quote_tr) end; |
149 in (quoteN, quote_tr) end; |
149 |
150 |
150 |
151 |
151 |
152 |
152 (** print (ast) translations **) |
153 (** print (ast) translations **) |
153 |
154 |
154 (* application *) |
155 (* application *) |
155 |
156 |
156 fun appl_ast_tr' (f, []) = raise AST ("appl_ast_tr'", [f]) |
157 fun appl_ast_tr' (f, []) = raise Ast.AST ("appl_ast_tr'", [f]) |
157 | appl_ast_tr' (f, args) = Appl [Constant "_appl", f, fold_ast "_args" args]; |
158 | appl_ast_tr' (f, args) = Ast.Appl [Ast.Constant "_appl", f, Ast.fold_ast "_args" args]; |
158 |
159 |
159 fun applC_ast_tr' (f, []) = raise AST ("applC_ast_tr'", [f]) |
160 fun applC_ast_tr' (f, []) = raise Ast.AST ("applC_ast_tr'", [f]) |
160 | applC_ast_tr' (f, args) = |
161 | applC_ast_tr' (f, args) = Ast.Appl [Ast.Constant "_applC", f, Ast.fold_ast "_cargs" args]; |
161 Appl [Constant "_applC", f, fold_ast "_cargs" args]; |
|
162 |
162 |
163 |
163 |
164 (* abstraction *) |
164 (* abstraction *) |
165 |
165 |
166 fun mark_boundT x_T = const "_bound" $ Free x_T; |
166 fun mark_boundT x_T = Lexicon.const "_bound" $ Free x_T; |
167 fun mark_bound x = mark_boundT (x, dummyT); |
167 fun mark_bound x = mark_boundT (x, dummyT); |
168 |
168 |
169 fun strip_abss vars_of body_of tm = |
169 fun strip_abss vars_of body_of tm = |
170 let |
170 let |
171 val vars = vars_of tm; |
171 val vars = vars_of tm; |
200 if ! eta_contract then eta_abs tm else tm |
200 if ! eta_contract then eta_abs tm else tm |
201 end; |
201 end; |
202 |
202 |
203 |
203 |
204 fun abs_tr' tm = |
204 fun abs_tr' tm = |
205 foldr (fn (x, t) => const "_abs" $ x $ t) |
205 foldr (fn (x, t) => Lexicon.const "_abs" $ x $ t) |
206 (strip_abss strip_abs_vars strip_abs_body (eta_contr tm)); |
206 (strip_abss strip_abs_vars strip_abs_body (eta_contr tm)); |
207 |
207 |
208 |
208 |
209 fun abs_ast_tr' (*"_abs"*) asts = |
209 fun abs_ast_tr' (*"_abs"*) asts = |
210 (case unfold_ast_p "_abs" (Appl (Constant "_abs" :: asts)) of |
210 (case Ast.unfold_ast_p "_abs" (Ast.Appl (Ast.Constant "_abs" :: asts)) of |
211 ([], _) => raise AST ("abs_ast_tr'", asts) |
211 ([], _) => raise Ast.AST ("abs_ast_tr'", asts) |
212 | (xs, body) => Appl [Constant "_lambda", fold_ast "_pttrns" xs, body]); |
212 | (xs, body) => Ast.Appl [Ast.Constant "_lambda", Ast.fold_ast "_pttrns" xs, body]); |
213 |
213 |
214 |
214 |
215 (* binder *) |
215 (* binder *) |
216 |
216 |
217 fun mk_binder_tr' (name, sy) = |
217 fun mk_binder_tr' (name, sy) = |
218 let |
218 let |
219 fun mk_idts [] = raise Match (*abort translation*) |
219 fun mk_idts [] = raise Match (*abort translation*) |
220 | mk_idts [idt] = idt |
220 | mk_idts [idt] = idt |
221 | mk_idts (idt :: idts) = const "_idts" $ idt $ mk_idts idts; |
221 | mk_idts (idt :: idts) = Lexicon.const "_idts" $ idt $ mk_idts idts; |
222 |
222 |
223 fun tr' t = |
223 fun tr' t = |
224 let |
224 let |
225 val (xs, bd) = strip_abss (strip_qnt_vars name) (strip_qnt_body name) t; |
225 val (xs, bd) = strip_abss (strip_qnt_vars name) (strip_qnt_body name) t; |
226 in |
226 in Lexicon.const sy $ mk_idts xs $ bd end; |
227 const sy $ mk_idts xs $ bd |
227 |
228 end; |
228 fun binder_tr' (*name*) (t :: ts) = Term.list_comb (tr' (Lexicon.const name $ t), ts) |
229 |
|
230 fun binder_tr' (*name*) (t :: ts) = |
|
231 list_comb (tr' (const name $ t), ts) |
|
232 | binder_tr' (*name*) [] = raise Match; |
229 | binder_tr' (*name*) [] = raise Match; |
233 in |
230 in |
234 (name, binder_tr') |
231 (name, binder_tr') |
235 end; |
232 end; |
236 |
233 |
237 |
234 |
238 (* idtyp constraints *) |
235 (* idtyp constraints *) |
239 |
236 |
240 fun idtyp_ast_tr' a [Appl [Constant c, x, ty], xs] = |
237 fun idtyp_ast_tr' a [Ast.Appl [Ast.Constant c, x, ty], xs] = |
241 if c = constrainC then |
238 if c = SynExt.constrainC then |
242 Appl [Constant a, Appl [Constant "_idtyp", x, ty], xs] |
239 Ast.Appl [ Ast.Constant a, Ast.Appl [Ast.Constant "_idtyp", x, ty], xs] |
243 else raise Match |
240 else raise Match |
244 | idtyp_ast_tr' _ _ = raise Match; |
241 | idtyp_ast_tr' _ _ = raise Match; |
245 |
242 |
246 |
243 |
247 (* meta propositions *) |
244 (* meta propositions *) |
248 |
245 |
249 fun prop_tr' tm = |
246 fun prop_tr' tm = |
250 let |
247 let |
251 fun aprop t = const "_aprop" $ t; |
248 fun aprop t = Lexicon.const "_aprop" $ t; |
252 |
249 |
253 fun is_prop Ts t = |
250 fun is_prop Ts t = |
254 fastype_of1 (Ts, t) = propT handle TERM _ => false; |
251 fastype_of1 (Ts, t) = propT handle TERM _ => false; |
255 |
252 |
256 fun tr' _ (t as Const _) = t |
253 fun tr' _ (t as Const _) = t |
257 | tr' _ (t as Free (x, T)) = |
254 | tr' _ (t as Free (x, T)) = |
258 if T = propT then aprop (free x) else t |
255 if T = propT then aprop (Lexicon.free x) else t |
259 | tr' _ (t as Var (xi, T)) = |
256 | tr' _ (t as Var (xi, T)) = |
260 if T = propT then aprop (var xi) else t |
257 if T = propT then aprop (Lexicon.var xi) else t |
261 | tr' Ts (t as Bound _) = |
258 | tr' Ts (t as Bound _) = |
262 if is_prop Ts t then aprop t else t |
259 if is_prop Ts t then aprop t else t |
263 | tr' Ts (Abs (x, T, t)) = Abs (x, T, tr' (T :: Ts) t) |
260 | tr' Ts (Abs (x, T, t)) = Abs (x, T, tr' (T :: Ts) t) |
264 | tr' Ts (t as t1 $ (t2 as Const ("TYPE", Type ("itself", [T])))) = |
261 | tr' Ts (t as t1 $ (t2 as Const ("TYPE", Type ("itself", [T])))) = |
265 if is_prop Ts t then Const ("_mk_ofclass", T) $ tr' Ts t1 |
262 if is_prop Ts t then Const ("_mk_ofclass", T) $ tr' Ts t1 |
266 else tr' Ts t1 $ tr' Ts t2 |
263 else tr' Ts t1 $ tr' Ts t2 |
267 | tr' Ts (t as t1 $ t2) = |
264 | tr' Ts (t as t1 $ t2) = |
268 (if is_Const (head_of t) orelse not (is_prop Ts t) |
265 (if is_Const (Term.head_of t) orelse not (is_prop Ts t) |
269 then I else aprop) (tr' Ts t1 $ tr' Ts t2); |
266 then I else aprop) (tr' Ts t1 $ tr' Ts t2); |
270 in |
267 in |
271 tr' [] tm |
268 tr' [] tm |
272 end; |
269 end; |
273 |
270 |
274 fun mk_ofclass_tr' show_sorts (*"_mk_ofclass"*) T [t] = |
271 fun mk_ofclass_tr' show_sorts (*"_mk_ofclass"*) T [t] = |
275 const "_ofclass" $ term_of_typ show_sorts T $ t |
272 Lexicon.const "_ofclass" $ TypeExt.term_of_typ show_sorts T $ t |
276 | mk_ofclass_tr' _ (*"_mk_ofclass"*) T ts = raise TYPE ("mk_ofclass_tr'", [T], ts); |
273 | mk_ofclass_tr' _ (*"_mk_ofclass"*) T ts = raise TYPE ("mk_ofclass_tr'", [T], ts); |
277 |
274 |
278 |
275 |
279 (* meta implication *) |
276 (* meta implication *) |
280 |
277 |
281 fun impl_ast_tr' (*"==>"*) asts = |
278 fun impl_ast_tr' (*"==>"*) asts = |
282 (case unfold_ast_p "==>" (Appl (Constant "==>" :: asts)) of |
279 (case Ast.unfold_ast_p "==>" (Ast.Appl (Ast.Constant "==>" :: asts)) of |
283 (asms as _ :: _ :: _, concl) |
280 (asms as _ :: _ :: _, concl) |
284 => Appl [Constant "_bigimpl", fold_ast "_asms" asms, concl] |
281 => Ast.Appl [Ast.Constant "_bigimpl", Ast.fold_ast "_asms" asms, concl] |
285 | _ => raise Match); |
282 | _ => raise Match); |
286 |
283 |
287 |
284 |
288 (* type reflection *) |
285 (* type reflection *) |
289 |
286 |
290 fun type_tr' show_sorts (*"TYPE"*) (Type ("itself", [T])) ts = |
287 fun type_tr' show_sorts (*"TYPE"*) (Type ("itself", [T])) ts = |
291 list_comb (const "_TYPE" $ term_of_typ show_sorts T, ts) |
288 Term.list_comb (Lexicon.const "_TYPE" $ TypeExt.term_of_typ show_sorts T, ts) |
292 | type_tr' _ _ _ = raise Match; |
289 | type_tr' _ _ _ = raise Match; |
293 |
290 |
294 |
291 |
295 (* dependent / nondependent quantifiers *) |
292 (* dependent / nondependent quantifiers *) |
296 |
293 |
313 if Term.loose_bvar1 (t, i) then raise Match else t; |
310 if Term.loose_bvar1 (t, i) then raise Match else t; |
314 |
311 |
315 fun quote_antiquote_tr' quoteN antiquoteN name = |
312 fun quote_antiquote_tr' quoteN antiquoteN name = |
316 let |
313 let |
317 fun antiquote_tr' i (t $ u) = |
314 fun antiquote_tr' i (t $ u) = |
318 if u = Bound i then const antiquoteN $ no_loose_bvar i t |
315 if u = Bound i then Lexicon.const antiquoteN $ no_loose_bvar i t |
319 else antiquote_tr' i t $ antiquote_tr' i u |
316 else antiquote_tr' i t $ antiquote_tr' i u |
320 | antiquote_tr' i (Abs (x, T, t)) = Abs (x, T, antiquote_tr' (i + 1) t) |
317 | antiquote_tr' i (Abs (x, T, t)) = Abs (x, T, antiquote_tr' (i + 1) t) |
321 | antiquote_tr' i a = no_loose_bvar i a; |
318 | antiquote_tr' i a = no_loose_bvar i a; |
322 |
319 |
323 fun quote_tr' (Abs (x, T, t) :: ts) = |
320 fun quote_tr' (Abs (x, T, t) :: ts) = |
324 Term.list_comb (const quoteN $ incr_boundvars ~1 (antiquote_tr' 0 t), ts) |
321 Term.list_comb ( Lexicon.const quoteN $ Term.incr_boundvars ~1 (antiquote_tr' 0 t), ts) |
325 | quote_tr' _ = raise Match; |
322 | quote_tr' _ = raise Match; |
326 in (name, quote_tr') end; |
323 in (name, quote_tr') end; |
327 |
324 |
328 |
325 |
329 |
326 |