1 (* Title: Pure/Syntax/syn_trans.ML
2 Author: Tobias Nipkow and Markus Wenzel, TU Muenchen
4 Syntax translation functions.
9 val eta_contract: bool ref
10 val atomic_abs_tr': string * typ * term -> term * term
11 val preserve_binder_abs_tr': string -> string -> string * (term list -> term)
12 val preserve_binder_abs2_tr': string -> string -> string * (term list -> term)
13 val mk_binder_tr: string * string -> string * (term list -> term)
14 val mk_binder_tr': string * string -> string * (term list -> term)
15 val dependent_tr': string * string -> term list -> term
16 val antiquote_tr: string -> term -> term
17 val quote_tr: string -> term -> term
18 val quote_antiquote_tr: string -> string -> string -> string * (term list -> term)
19 val antiquote_tr': string -> term -> term
20 val quote_tr': string -> term -> term
21 val quote_antiquote_tr': string -> string -> string -> string * (term list -> term)
22 val mark_bound: string -> term
23 val mark_boundT: string * typ -> term
24 val bound_vars: (string * typ) list -> term -> term
25 val variant_abs: string * typ * term -> string * term
26 val variant_abs': string * typ * term -> string * term
29 signature SYN_TRANS1 =
32 val non_typed_tr': (term list -> term) -> bool -> typ -> term list -> term
33 val non_typed_tr'': ('a -> term list -> term) -> 'a -> bool -> typ -> term list -> term
34 val constrainAbsC: string
36 (string * (Ast.ast list -> Ast.ast)) list *
37 (string * (term list -> term)) list *
38 (string * (term list -> term)) list *
39 (string * (Ast.ast list -> Ast.ast)) list
40 val pure_trfunsT: (string * (bool -> typ -> term list -> term)) list
41 val struct_trfuns: string list ->
42 (string * (Ast.ast list -> Ast.ast)) list *
43 (string * (term list -> term)) list *
44 (string * (bool -> typ -> term list -> term)) list *
45 (string * (Ast.ast list -> Ast.ast)) list
51 val abs_tr': term -> term
52 val prop_tr': term -> term
53 val appl_ast_tr': Ast.ast * Ast.ast list -> Ast.ast
54 val applC_ast_tr': Ast.ast * Ast.ast list -> Ast.ast
55 val pts_to_asts: Proof.context ->
56 (string -> (Proof.context -> Ast.ast list -> Ast.ast) option) ->
57 Parser.parsetree list -> Ast.ast list
58 val asts_to_terms: Proof.context ->
59 (string -> (Proof.context -> term list -> term) option) -> Ast.ast list -> term list
62 structure SynTrans: SYN_TRANS =
66 (** parse (ast) translations **)
70 fun constify_ast_tr [Ast.Variable c] = Ast.Constant c
71 | constify_ast_tr asts = raise Ast.AST ("constify_ast_tr", asts);
76 fun appl_ast_tr [f, args] = Ast.Appl (f :: Ast.unfold_ast "_args" args)
77 | appl_ast_tr asts = raise Ast.AST ("appl_ast_tr", asts);
79 fun applC_ast_tr [f, args] = Ast.Appl (f :: Ast.unfold_ast "_cargs" args)
80 | applC_ast_tr asts = raise Ast.AST ("applC_ast_tr", asts);
85 fun idtyp_ast_tr (*"_idtyp"*) [x, ty] = Ast.Appl [Ast.Constant "_constrain", x, ty]
86 | idtyp_ast_tr (*"_idtyp"*) asts = raise Ast.AST ("idtyp_ast_tr", asts);
88 fun idtypdummy_ast_tr (*"_idtypdummy"*) [ty] =
89 Ast.Appl [Ast.Constant "_constrain", Ast.Constant "_idtdummy", ty]
90 | idtypdummy_ast_tr (*"_idtypdummy"*) asts = raise Ast.AST ("idtyp_ast_tr", asts);
92 fun lambda_ast_tr (*"_lambda"*) [pats, body] =
93 Ast.fold_ast_p "_abs" (Ast.unfold_ast "_pttrns" pats, body)
94 | lambda_ast_tr (*"_lambda"*) asts = raise Ast.AST ("lambda_ast_tr", asts);
96 val constrainAbsC = "_constrainAbs";
98 fun absfree_proper (x, T, t) =
99 if can Name.dest_internal x then error ("Illegal internal variable in abstraction: " ^ quote x)
100 else Term.absfree (x, T, t);
102 fun abs_tr (*"_abs"*) [Free (x, T), t] = absfree_proper (x, T, t)
103 | abs_tr (*"_abs"*) [Const ("_idtdummy", T), t] = Term.absdummy (T, t)
104 | abs_tr (*"_abs"*) [Const ("_constrain", _) $ Free (x, T) $ tT, t] =
105 Lexicon.const constrainAbsC $ absfree_proper (x, T, t) $ tT
106 | abs_tr (*"_abs"*) [Const ("_constrain", _) $ Const ("_idtdummy", T) $ tT, t] =
107 Lexicon.const constrainAbsC $ Term.absdummy (T, t) $ tT
108 | abs_tr (*"_abs"*) ts = raise TERM ("abs_tr", ts);
113 fun mk_binder_tr (syn, name) =
115 fun tr (Free (x, T), t) = Lexicon.const name $ absfree_proper (x, T, t)
116 | tr (Const ("_idtdummy", T), t) = Lexicon.const name $ Term.absdummy (T, t)
117 | tr (Const ("_constrain", _) $ Free (x, T) $ tT, t) =
118 Lexicon.const name $ (Lexicon.const constrainAbsC $ absfree_proper (x, T, t) $ tT)
119 | tr (Const ("_constrain", _) $ Const ("_idtdummy", T) $ tT, t) =
120 Lexicon.const name $ (Lexicon.const constrainAbsC $ Term.absdummy (T, t) $ tT)
121 | tr (Const ("_idts", _) $ idt $ idts, t) = tr (idt, tr (idts, t))
122 | tr (t1, t2) = raise TERM ("binder_tr", [t1, t2]);
124 fun binder_tr [idts, body] = tr (idts, body)
125 | binder_tr ts = raise TERM ("binder_tr", ts);
126 in (syn, binder_tr) end;
129 (* type propositions *)
131 fun mk_type ty = Lexicon.const "_constrain" $ Lexicon.const "TYPE" $ (Lexicon.const "itself" $ ty);
133 fun ofclass_tr (*"_ofclass"*) [ty, cls] = cls $ mk_type ty
134 | ofclass_tr (*"_ofclass"*) ts = raise TERM ("ofclass_tr", ts);
136 fun sort_constraint_tr (*"_sort_constraint"*) [ty] =
137 Lexicon.const "Pure.sort_constraint" $ mk_type ty
138 | sort_constraint_tr (*"_sort_constraint"*) ts = raise TERM ("sort_constraint_tr", ts);
141 (* meta propositions *)
143 fun aprop_tr (*"_aprop"*) [t] = Lexicon.const "_constrain" $ t $ Lexicon.const "prop"
144 | aprop_tr (*"_aprop"*) ts = raise TERM ("aprop_tr", ts);
147 (* meta implication *)
149 fun bigimpl_ast_tr (*"_bigimpl"*) (asts as [asms, concl]) =
151 (case Ast.unfold_ast_p "_asms" asms of
152 (asms', Ast.Appl [Ast.Constant "_asm", asm']) => asms' @ [asm']
153 | _ => raise Ast.AST ("bigimpl_ast_tr", asts))
154 in Ast.fold_ast_p "==>" (prems, concl) end
155 | bigimpl_ast_tr (*"_bigimpl"*) asts = raise Ast.AST ("bigimpl_ast_tr", asts);
158 (* meta conjunction *)
160 fun conjunction_tr [t, u] = Lexicon.const "Pure.conjunction" $ t $ u
161 | conjunction_tr ts = raise TERM ("conjunction_tr", ts);
164 (* type/term reflection *)
166 fun type_tr (*"_TYPE"*) [ty] = mk_type ty
167 | type_tr (*"_TYPE"*) ts = raise TERM ("type_tr", ts);
169 fun term_tr [t] = Lexicon.const "Pure.term" $ t
170 | term_tr ts = raise TERM ("term_tr", ts);
175 fun dddot_tr (*"_DDDOT"*) ts = Term.list_comb (Lexicon.var SynExt.dddot_indexname, ts);
178 (* quote / antiquote *)
180 fun antiquote_tr name =
182 fun tr i ((t as Const (c, _)) $ u) =
183 if c = name then tr i u $ Bound i
185 | tr i (t $ u) = tr i t $ tr i u
186 | tr i (Abs (x, T, t)) = Abs (x, T, tr (i + 1) t)
190 fun quote_tr name t = Abs ("s", dummyT, antiquote_tr name (Term.incr_boundvars 1 t));
192 fun quote_antiquote_tr quoteN antiquoteN name =
194 fun tr [t] = Lexicon.const name $ quote_tr antiquoteN t
195 | tr ts = raise TERM ("quote_tr", ts);
201 fun struct_ast_tr (*"_struct"*) [Ast.Appl [Ast.Constant "_index", ast]] = ast
202 | struct_ast_tr (*"_struct"*) asts = Ast.mk_appl (Ast.Constant "_struct") asts;
204 fun index_ast_tr ast =
205 Ast.mk_appl (Ast.Constant "_index") [Ast.mk_appl (Ast.Constant "_struct") [ast]];
207 fun indexdefault_ast_tr (*"_indexdefault"*) [] =
208 index_ast_tr (Ast.Constant "_indexdefault")
209 | indexdefault_ast_tr (*"_indexdefault"*) asts =
210 raise Ast.AST ("indexdefault_ast_tr", asts);
212 fun indexnum_ast_tr (*"_indexnum"*) [ast] =
213 index_ast_tr (Ast.mk_appl (Ast.Constant "_indexnum") [ast])
214 | indexnum_ast_tr (*"_indexnum"*) asts = raise Ast.AST ("indexnum_ast_tr", asts);
216 fun indexvar_ast_tr (*"_indexvar"*) [] =
217 Ast.mk_appl (Ast.Constant "_index") [Ast.Variable "some_index"]
218 | indexvar_ast_tr (*"_indexvar"*) asts = raise Ast.AST ("indexvar_ast_tr", asts);
220 fun index_tr (*"_index"*) [t] = t
221 | index_tr (*"_index"*) ts = raise TERM ("index_tr", ts);
224 (* implicit structures *)
226 fun the_struct structs i =
227 if 1 <= i andalso i <= length structs then nth structs (i - 1)
228 else error ("Illegal reference to implicit structure #" ^ string_of_int i);
230 fun struct_tr structs (*"_struct"*) [Const ("_indexdefault", _)] =
231 Lexicon.free (the_struct structs 1)
232 | struct_tr structs (*"_struct"*) [t as (Const ("_indexnum", _) $ Const (s, _))] =
233 Lexicon.free (the_struct structs
234 (case Lexicon.read_nat s of SOME n => n | NONE => raise TERM ("struct_tr", [t])))
235 | struct_tr _ (*"_struct"*) ts = raise TERM ("struct_tr", ts);
239 (** print (ast) translations **)
243 fun non_typed_tr' f _ _ ts = f ts;
244 fun non_typed_tr'' f x _ _ ts = f x ts;
249 fun appl_ast_tr' (f, []) = raise Ast.AST ("appl_ast_tr'", [f])
250 | appl_ast_tr' (f, args) = Ast.Appl [Ast.Constant "_appl", f, Ast.fold_ast "_args" args];
252 fun applC_ast_tr' (f, []) = raise Ast.AST ("applC_ast_tr'", [f])
253 | applC_ast_tr' (f, args) = Ast.Appl [Ast.Constant "_applC", f, Ast.fold_ast "_cargs" args];
258 fun mark_boundT (x, T) = Const ("_bound", T --> T) $ Free (x, T);
259 fun mark_bound x = mark_boundT (x, dummyT);
261 fun bound_vars vars body =
262 subst_bounds (map mark_boundT (Term.rename_wrt_term body vars), body);
264 fun strip_abss vars_of body_of tm =
266 val vars = vars_of tm;
267 val body = body_of tm;
268 val rev_new_vars = Term.rename_wrt_term body vars;
270 if can Name.dest_internal x andalso not (Term.loose_bvar1 (b, 0))
271 then (Const ("_idtdummy", T), incr_boundvars ~1 b)
272 else (mark_boundT (x, T), Term.subst_bound (mark_bound x, b));
273 val (rev_vars', body') = fold_map subst rev_new_vars body;
274 in (rev rev_vars', body') end;
277 (*do (partial) eta-contraction before printing*)
279 val eta_contract = ref true;
283 fun is_aprop (Const ("_aprop", _)) = true
284 | is_aprop _ = false;
286 fun eta_abs (Abs (a, T, t)) =
291 if Term.loose_bvar1 (f, 0) orelse is_aprop f then Abs (a, T, t')
292 else incr_boundvars ~1 f
293 | _ => Abs (a, T, t'))
294 | t' => Abs (a, T, t'))
297 if ! eta_contract then eta_abs tm else tm
302 uncurry (fold_rev (fn x => fn t => Lexicon.const "_abs" $ x $ t))
303 (strip_abss strip_abs_vars strip_abs_body (eta_contr tm));
305 fun atomic_abs_tr' (x, T, t) =
306 let val [xT] = Term.rename_wrt_term t [(x, T)]
307 in (mark_boundT xT, subst_bound (mark_bound (fst xT), t)) end;
309 fun abs_ast_tr' (*"_abs"*) asts =
310 (case Ast.unfold_ast_p "_abs" (Ast.Appl (Ast.Constant "_abs" :: asts)) of
311 ([], _) => raise Ast.AST ("abs_ast_tr'", asts)
312 | (xs, body) => Ast.Appl [Ast.Constant "_lambda", Ast.fold_ast "_pttrns" xs, body]);
314 fun preserve_binder_abs_tr' name syn = (name, fn (Abs abs :: ts) =>
315 let val (x, t) = atomic_abs_tr' abs
316 in list_comb (Lexicon.const syn $ x $ t, ts) end);
318 fun preserve_binder_abs2_tr' name syn = (name, fn (A :: Abs abs :: ts) =>
319 let val (x, t) = atomic_abs_tr' abs
320 in list_comb (Lexicon.const syn $ x $ A $ t, ts) end);
325 fun mk_binder_tr' (name, syn) =
327 fun mk_idts [] = raise Match (*abort translation*)
328 | mk_idts [idt] = idt
329 | mk_idts (idt :: idts) = Lexicon.const "_idts" $ idt $ mk_idts idts;
333 val (xs, bd) = strip_abss (strip_qnt_vars name) (strip_qnt_body name) t;
334 in Lexicon.const syn $ mk_idts xs $ bd end;
336 fun binder_tr' (t :: ts) = Term.list_comb (tr' (Lexicon.const name $ t), ts)
337 | binder_tr' [] = raise Match;
338 in (name, binder_tr') end;
341 (* idtyp constraints *)
343 fun idtyp_ast_tr' a [Ast.Appl [Ast.Constant c, x, ty], xs] =
344 if c = "_constrain" then
345 Ast.Appl [Ast.Constant a, Ast.Appl [Ast.Constant "_idtyp", x, ty], xs]
347 | idtyp_ast_tr' _ _ = raise Match;
350 (* type propositions *)
352 fun type_prop_tr' _ (*"_type_prop"*) T [Const ("Pure.sort_constraint", _)] =
353 Lexicon.const "_sort_constraint" $ TypeExt.term_of_typ true T
354 | type_prop_tr' show_sorts (*"_type_prop"*) T [t] =
355 Lexicon.const "_ofclass" $ TypeExt.term_of_typ show_sorts T $ t
356 | type_prop_tr' _ (*"_type_prop"*) T ts = raise TYPE ("type_prop_tr'", [T], ts);
359 (* meta propositions *)
363 fun aprop t = Lexicon.const "_aprop" $ t;
366 fastype_of1 (Ts, t) = propT handle TERM _ => false;
368 fun is_term (Const ("Pure.term", _) $ _) = true
371 fun tr' _ (t as Const _) = t
372 | tr' Ts (t as Const ("_bound", _) $ u) =
373 if is_prop Ts u then aprop t else t
374 | tr' _ (t as Free (x, T)) =
375 if T = propT then aprop (Lexicon.free x) else t
376 | tr' _ (t as Var (xi, T)) =
377 if T = propT then aprop (Lexicon.var xi) else t
378 | tr' Ts (t as Bound _) =
379 if is_prop Ts t then aprop t else t
380 | tr' Ts (Abs (x, T, t)) = Abs (x, T, tr' (T :: Ts) t)
381 | tr' Ts (t as t1 $ (t2 as Const ("TYPE", Type ("itself", [T])))) =
382 if is_prop Ts t andalso not (is_term t) then Const ("_type_prop", T) $ tr' Ts t1
383 else tr' Ts t1 $ tr' Ts t2
384 | tr' Ts (t as t1 $ t2) =
385 (if is_Const (Term.head_of t) orelse not (is_prop Ts t)
386 then I else aprop) (tr' Ts t1 $ tr' Ts t2);
390 (* meta implication *)
392 fun impl_ast_tr' (*"==>"*) asts =
393 if TypeExt.no_brackets () then raise Match
395 (case Ast.unfold_ast_p "==>" (Ast.Appl (Ast.Constant "==>" :: asts)) of
396 (prems as _ :: _ :: _, concl) =>
398 val (asms, asm) = split_last prems;
399 val asms' = Ast.fold_ast_p "_asms" (asms, Ast.Appl [Ast.Constant "_asm", asm]);
400 in Ast.Appl [Ast.Constant "_bigimpl", asms', concl] end
404 (* type reflection *)
406 fun type_tr' show_sorts (*"TYPE"*) (Type ("itself", [T])) ts =
407 Term.list_comb (Lexicon.const "_TYPE" $ TypeExt.term_of_typ show_sorts T, ts)
408 | type_tr' _ _ _ = raise Match;
411 (* type constraints *)
413 fun type_constraint_tr' show_sorts (*"_type_constraint_"*) (Type ("fun", [T, _])) (t :: ts) =
414 Term.list_comb (Lexicon.const SynExt.constrainC $ t $ TypeExt.term_of_typ show_sorts T, ts)
415 | type_constraint_tr' _ _ _ = raise Match;
418 (* dependent / nondependent quantifiers *)
420 fun var_abs mark (x, T, b) =
421 let val ([x'], _) = Name.variants [x] (Term.declare_term_names b Name.context)
422 in (x', subst_bound (mark (x', T), b)) end;
424 val variant_abs = var_abs Free;
425 val variant_abs' = var_abs mark_boundT;
427 fun dependent_tr' (q, r) (A :: Abs (x, T, B) :: ts) =
428 if Term.loose_bvar1 (B, 0) then
429 let val (x', B') = variant_abs' (x, dummyT, B);
430 in Term.list_comb (Lexicon.const q $ mark_boundT (x', T) $ A $ B', ts) end
431 else Term.list_comb (Lexicon.const r $ A $ B, ts)
432 | dependent_tr' _ _ = raise Match;
435 (* quote / antiquote *)
437 fun antiquote_tr' name =
440 if u aconv Bound i then Lexicon.const name $ tr' i t
441 else tr' i t $ tr' i u
442 | tr' i (Abs (x, T, t)) = Abs (x, T, tr' (i + 1) t)
443 | tr' i a = if a aconv Bound i then raise Match else a;
446 fun quote_tr' name (Abs (_, _, t)) = Term.incr_boundvars ~1 (antiquote_tr' name t)
447 | quote_tr' _ _ = raise Match;
449 fun quote_antiquote_tr' quoteN antiquoteN name =
451 fun tr' (t :: ts) = Term.list_comb (Lexicon.const quoteN $ quote_tr' antiquoteN t, ts)
452 | tr' _ = raise Match;
458 fun index_ast_tr' (*"_index"*) [Ast.Appl [Ast.Constant "_struct", ast]] = ast
459 | index_ast_tr' _ = raise Match;
462 (* implicit structures *)
464 fun the_struct' structs s =
465 [(case Lexicon.read_nat s of
466 SOME i => Ast.Variable (the_struct structs i handle ERROR _ => raise Match)
467 | NONE => raise Match)] |> Ast.mk_appl (Ast.Constant "_free");
469 fun struct_ast_tr' structs (*"_struct"*) [Ast.Constant "_indexdefault"] =
470 the_struct' structs "1"
471 | struct_ast_tr' structs (*"_struct"*) [Ast.Appl [Ast.Constant "_indexnum", Ast.Constant s]] =
472 the_struct' structs s
473 | struct_ast_tr' _ _ = raise Match;
477 (** Pure translations **)
480 ([("_constify", constify_ast_tr), ("_appl", appl_ast_tr), ("_applC", applC_ast_tr),
481 ("_lambda", lambda_ast_tr), ("_idtyp", idtyp_ast_tr), ("_idtypdummy", idtypdummy_ast_tr),
482 ("_bigimpl", bigimpl_ast_tr), ("_indexdefault", indexdefault_ast_tr),
483 ("_indexnum", indexnum_ast_tr), ("_indexvar", indexvar_ast_tr), ("_struct", struct_ast_tr)],
484 [("_abs", abs_tr), ("_aprop", aprop_tr), ("_ofclass", ofclass_tr),
485 ("_sort_constraint", sort_constraint_tr), ("_TYPE", type_tr),
486 ("_DDDOT", dddot_tr), ("_index", index_tr)],
487 ([]: (string * (term list -> term)) list),
488 [("_abs", abs_ast_tr'), ("_idts", idtyp_ast_tr' "_idts"),
489 ("_pttrns", idtyp_ast_tr' "_pttrns"), ("==>", impl_ast_tr'),
490 ("_index", index_ast_tr')]);
493 [("_type_prop", type_prop_tr'), ("TYPE", type_tr'), ("_type_constraint_", type_constraint_tr')];
495 fun struct_trfuns structs =
496 ([], [("_struct", struct_tr structs)], [], [("_struct", struct_ast_tr' structs)]);
502 fun pts_to_asts ctxt trf pts =
506 NONE => Ast.mk_appl (Ast.Constant a) args
507 | SOME f => f ctxt args);
509 (*translate pt bottom-up*)
510 fun ast_of (Parser.Node (a, pts)) = trans a (map ast_of pts)
511 | ast_of (Parser.Tip tok) = Ast.Variable (Lexicon.str_of_token tok);
513 val exn_results = map (Exn.capture ast_of) pts;
514 val exns = map_filter Exn.get_exn exn_results;
515 val results = map_filter Exn.get_result exn_results
516 in (case (results, exns) of ([], exn :: _) => reraise exn | _ => results) end;
520 (** asts_to_terms **)
522 fun asts_to_terms ctxt trf asts =
526 NONE => Term.list_comb (Lexicon.const a, args)
527 | SOME f => f ctxt args);
529 fun term_of (Ast.Constant a) = trans a []
530 | term_of (Ast.Variable x) = Lexicon.read_var x
531 | term_of (Ast.Appl (Ast.Constant a :: (asts as _ :: _))) =
532 trans a (map term_of asts)
533 | term_of (Ast.Appl (ast :: (asts as _ :: _))) =
534 Term.list_comb (term_of ast, map term_of asts)
535 | term_of (ast as Ast.Appl _) = raise Ast.AST ("ast_to_term: malformed ast", [ast]);
537 val free_fixed = Term.map_aterms
538 (fn t as Const (c, T) =>
539 (case try (unprefix Lexicon.fixedN) c of
541 | SOME x => Free (x, T))
544 val exn_results = map (Exn.capture (term_of #> free_fixed)) asts;
545 val exns = map_filter Exn.get_exn exn_results;
546 val results = map_filter Exn.get_result exn_results
547 in (case (results, exns) of ([], exn :: _) => reraise exn | _ => results) end;