1 (* Title: Pure/Syntax/syn_trans.ML
3 Author: Tobias Nipkow and Markus Wenzel, TU Muenchen
5 Syntax translation functions.
10 val eta_contract: bool ref
11 val atomic_abs_tr': string * typ * term -> term * term
12 val mk_binder_tr: string * string -> string * (term list -> term)
13 val mk_binder_tr': string * string -> string * (term list -> term)
14 val dependent_tr': string * string -> term list -> term
15 val antiquote_tr: string -> term -> term
16 val quote_tr: string -> term -> term
17 val quote_antiquote_tr: string -> string -> string -> string * (term list -> term)
18 val antiquote_tr': string -> term -> term
19 val quote_tr': string -> term -> term
20 val quote_antiquote_tr': string -> string -> string -> string * (term list -> term)
21 val mark_bound: string -> term
22 val mark_boundT: string * typ -> term
23 val bound_vars: (string * typ) list -> term -> term
24 val variant_abs: string * typ * term -> string * term
25 val variant_abs': string * typ * term -> string * term
28 signature SYN_TRANS1 =
31 val non_typed_tr': (term list -> term) -> bool -> typ -> term list -> term
32 val non_typed_tr'': ('a -> term list -> term) -> 'a -> bool -> typ -> term list -> term
33 val constrainAbsC: string
35 (string * (Ast.ast list -> Ast.ast)) list *
36 (string * (term list -> term)) list *
37 (string * (term list -> term)) list *
38 (string * (Ast.ast list -> Ast.ast)) list
39 val pure_trfunsT: (string * (bool -> typ -> term list -> term)) list
40 val struct_trfuns: string list ->
41 (string * (Ast.ast list -> Ast.ast)) list *
42 (string * (term list -> term)) list *
43 (string * (bool -> typ -> term list -> term)) list *
44 (string * (Ast.ast list -> Ast.ast)) list
50 val abs_tr': term -> term
51 val prop_tr': term -> term
52 val appl_ast_tr': Ast.ast * Ast.ast list -> Ast.ast
53 val applC_ast_tr': Ast.ast * Ast.ast list -> Ast.ast
54 val pts_to_asts: Context.generic ->
55 (string -> (Context.generic -> Ast.ast list -> Ast.ast) option) ->
56 Parser.parsetree list -> Ast.ast list
57 val asts_to_terms: Context.generic ->
58 (string -> (Context.generic -> term list -> term) option) -> Ast.ast list -> term list
61 structure SynTrans: SYN_TRANS =
65 (** parse (ast) translations **)
69 fun constify_ast_tr [Ast.Variable c] = Ast.Constant c
70 | constify_ast_tr asts = raise Ast.AST ("constify_ast_tr", asts);
75 fun appl_ast_tr [f, args] = Ast.Appl (f :: Ast.unfold_ast "_args" args)
76 | appl_ast_tr asts = raise Ast.AST ("appl_ast_tr", asts);
78 fun applC_ast_tr [f, args] = Ast.Appl (f :: Ast.unfold_ast "_cargs" args)
79 | applC_ast_tr asts = raise Ast.AST ("applC_ast_tr", asts);
84 fun idtyp_ast_tr (*"_idtyp"*) [x, ty] = Ast.Appl [Ast.Constant "_constrain", x, ty]
85 | idtyp_ast_tr (*"_idtyp"*) asts = raise Ast.AST ("idtyp_ast_tr", asts);
87 fun idtypdummy_ast_tr (*"_idtypdummy"*) [ty] =
88 Ast.Appl [Ast.Constant "_constrain", Ast.Constant "_idtdummy", ty]
89 | idtypdummy_ast_tr (*"_idtypdummy"*) asts = raise Ast.AST ("idtyp_ast_tr", asts);
91 fun lambda_ast_tr (*"_lambda"*) [pats, body] =
92 Ast.fold_ast_p "_abs" (Ast.unfold_ast "_pttrns" pats, body)
93 | lambda_ast_tr (*"_lambda"*) asts = raise Ast.AST ("lambda_ast_tr", asts);
95 val constrainAbsC = "_constrainAbs";
97 fun abs_tr (*"_abs"*) [Free (x, T), t] = Term.absfree (x, T, t)
98 | abs_tr (*"_abs"*) [Const ("_idtdummy", T), t] = Term.absdummy (T, t)
99 | abs_tr (*"_abs"*) [Const ("_constrain", _) $ Free (x, T) $ tT, t] =
100 Lexicon.const constrainAbsC $ Term.absfree (x, T, t) $ tT
101 | abs_tr (*"_abs"*) [Const ("_constrain", _) $ Const ("_idtdummy", T) $ tT, t] =
102 Lexicon.const constrainAbsC $ Term.absdummy (T, t) $ tT
103 | abs_tr (*"_abs"*) ts = raise TERM ("abs_tr", ts);
108 fun mk_binder_tr (sy, name) =
110 fun tr (Free (x, T), t) = Lexicon.const name $ Term.absfree (x, T, t)
111 | tr (Const ("_idtdummy", T), t) = Lexicon.const name $ Term.absdummy (T, t)
112 | tr (Const ("_constrain", _) $ Free (x, T) $ tT, t) =
113 Lexicon.const name $ (Lexicon.const constrainAbsC $ Term.absfree (x, T, t) $ tT)
114 | tr (Const ("_constrain", _) $ Const ("_idtdummy", T) $ tT, t) =
115 Lexicon.const name $ (Lexicon.const constrainAbsC $ Term.absdummy (T, t) $ tT)
116 | tr (Const ("_idts", _) $ idt $ idts, t) = tr (idt, tr (idts, t))
117 | tr (t1, t2) = raise TERM ("binder_tr", [t1, t2]);
119 fun binder_tr (*sy*) [idts, body] = tr (idts, body)
120 | binder_tr (*sy*) ts = raise TERM ("binder_tr", ts);
121 in (sy, binder_tr) end;
124 (* meta propositions *)
126 fun aprop_tr (*"_aprop"*) [t] = Lexicon.const "_constrain" $ t $ Lexicon.const "prop"
127 | aprop_tr (*"_aprop"*) ts = raise TERM ("aprop_tr", ts);
129 fun ofclass_tr (*"_ofclass"*) [ty, cls] =
130 cls $ (Lexicon.const "_constrain" $ Lexicon.const "TYPE" $
131 (Lexicon.const "itself" $ ty))
132 | ofclass_tr (*"_ofclass"*) ts = raise TERM ("ofclass_tr", ts);
135 (* meta implication *)
137 fun bigimpl_ast_tr (*"_bigimpl"*) (asts as [asms, concl]) =
139 (case Ast.unfold_ast_p "_asms" asms of
140 (asms', Ast.Appl [Ast.Constant "_asm", asm']) => asms' @ [asm']
141 | _ => raise Ast.AST ("bigimpl_ast_tr", asts))
142 in Ast.fold_ast_p "==>" (prems, concl) end
143 | bigimpl_ast_tr (*"_bigimpl"*) asts = raise Ast.AST ("bigimpl_ast_tr", asts);
146 (* type reflection *)
148 fun type_tr (*"_TYPE"*) [ty] =
149 Lexicon.const "_constrain" $ Lexicon.const "TYPE" $ (Lexicon.const "itself" $ ty)
150 | type_tr (*"_TYPE"*) ts = raise TERM ("type_tr", ts);
155 fun dddot_tr (*"_DDDOT"*) ts = Term.list_comb (Lexicon.var SynExt.dddot_indexname, ts);
158 (* quote / antiquote *)
160 fun antiquote_tr name =
162 fun tr i ((t as Const (c, _)) $ u) =
163 if c = name then tr i u $ Bound i
165 | tr i (t $ u) = tr i t $ tr i u
166 | tr i (Abs (x, T, t)) = Abs (x, T, tr (i + 1) t)
170 fun quote_tr name t = Abs ("s", dummyT, antiquote_tr name (Term.incr_boundvars 1 t));
172 fun quote_antiquote_tr quoteN antiquoteN name =
174 fun tr [t] = Lexicon.const name $ quote_tr antiquoteN t
175 | tr ts = raise TERM ("quote_tr", ts);
181 fun struct_ast_tr (*"_struct"*) [Ast.Appl [Ast.Constant "_index", ast]] = ast
182 | struct_ast_tr (*"_struct"*) asts = Ast.mk_appl (Ast.Constant "_struct") asts;
184 fun index_ast_tr ast =
185 Ast.mk_appl (Ast.Constant "_index") [Ast.mk_appl (Ast.Constant "_struct") [ast]];
187 fun indexdefault_ast_tr (*"_indexdefault"*) [] =
188 index_ast_tr (Ast.Constant "_indexdefault")
189 | indexdefault_ast_tr (*"_indexdefault"*) asts =
190 raise Ast.AST ("indexdefault_ast_tr", asts);
192 fun indexnum_ast_tr (*"_indexnum"*) [ast] =
193 index_ast_tr (Ast.mk_appl (Ast.Constant "_indexnum") [ast])
194 | indexnum_ast_tr (*"_indexnum"*) asts = raise Ast.AST ("indexnum_ast_tr", asts);
196 fun indexvar_ast_tr (*"_indexvar"*) [] =
197 Ast.mk_appl (Ast.Constant "_index") [Ast.Variable "some_index"]
198 | indexvar_ast_tr (*"_indexvar"*) asts = raise Ast.AST ("indexvar_ast_tr", asts);
200 fun index_tr (*"_index"*) [t] = t
201 | index_tr (*"_index"*) ts = raise TERM ("index_tr", ts);
204 (* implicit structures *)
206 fun the_struct structs i =
207 if 1 <= i andalso i <= length structs then List.nth (structs, i - 1)
208 else raise error ("Illegal reference to implicit structure #" ^ string_of_int i);
210 fun struct_tr structs (*"_struct"*) [Const ("_indexdefault", _)] =
211 Lexicon.free (the_struct structs 1)
212 | struct_tr structs (*"_struct"*) [t as (Const ("_indexnum", _) $ Const (s, _))] =
213 Lexicon.free (the_struct structs
214 (case Lexicon.read_nat s of SOME n => n | NONE => raise TERM ("struct_tr", [t])))
215 | struct_tr _ (*"_struct"*) ts = raise TERM ("struct_tr", ts);
219 (** print (ast) translations **)
223 fun non_typed_tr' f _ _ ts = f ts;
224 fun non_typed_tr'' f x _ _ ts = f x ts;
229 fun appl_ast_tr' (f, []) = raise Ast.AST ("appl_ast_tr'", [f])
230 | appl_ast_tr' (f, args) = Ast.Appl [Ast.Constant "_appl", f, Ast.fold_ast "_args" args];
232 fun applC_ast_tr' (f, []) = raise Ast.AST ("applC_ast_tr'", [f])
233 | applC_ast_tr' (f, args) = Ast.Appl [Ast.Constant "_applC", f, Ast.fold_ast "_cargs" args];
238 fun mark_boundT (x, T) = Const ("_bound", T --> T) $ Free (x, T);
239 fun mark_bound x = mark_boundT (x, dummyT);
241 fun bound_vars vars body =
242 subst_bounds (map mark_boundT (Term.rename_wrt_term body vars), body);
244 fun strip_abss vars_of body_of tm =
246 val vars = vars_of tm;
247 val body = body_of tm;
248 val rev_new_vars = rename_wrt_term body vars;
250 (map mark_boundT (rev rev_new_vars),
251 subst_bounds (map (mark_bound o #1) rev_new_vars, body))
255 (*do (partial) eta-contraction before printing*)
257 val eta_contract = ref true;
261 fun is_aprop (Const ("_aprop", _)) = true
262 | is_aprop _ = false;
264 fun eta_abs (Abs (a, T, t)) =
269 if Term.loose_bvar1 (f, 0) orelse is_aprop f then Abs (a, T, t')
270 else incr_boundvars ~1 f
271 | _ => Abs (a, T, t'))
272 | t' => Abs (a, T, t'))
275 if ! eta_contract then eta_abs tm else tm
280 uncurry (fold_rev (fn x => fn t => Lexicon.const "_abs" $ x $ t))
281 (strip_abss strip_abs_vars strip_abs_body (eta_contr tm));
283 fun atomic_abs_tr' (x, T, t) =
284 let val [xT] = rename_wrt_term t [(x, T)]
285 in (mark_boundT xT, subst_bound (mark_bound (fst xT), t)) end;
287 fun abs_ast_tr' (*"_abs"*) asts =
288 (case Ast.unfold_ast_p "_abs" (Ast.Appl (Ast.Constant "_abs" :: asts)) of
289 ([], _) => raise Ast.AST ("abs_ast_tr'", asts)
290 | (xs, body) => Ast.Appl [Ast.Constant "_lambda", Ast.fold_ast "_pttrns" xs, body]);
295 fun mk_binder_tr' (name, sy) =
297 fun mk_idts [] = raise Match (*abort translation*)
298 | mk_idts [idt] = idt
299 | mk_idts (idt :: idts) = Lexicon.const "_idts" $ idt $ mk_idts idts;
303 val (xs, bd) = strip_abss (strip_qnt_vars name) (strip_qnt_body name) t;
304 in Lexicon.const sy $ mk_idts xs $ bd end;
306 fun binder_tr' (*name*) (t :: ts) = Term.list_comb (tr' (Lexicon.const name $ t), ts)
307 | binder_tr' (*name*) [] = raise Match;
313 (* idtyp constraints *)
315 fun idtyp_ast_tr' a [Ast.Appl [Ast.Constant c, x, ty], xs] =
316 if c = "_constrain" then
317 Ast.Appl [ Ast.Constant a, Ast.Appl [Ast.Constant "_idtyp", x, ty], xs]
319 | idtyp_ast_tr' _ _ = raise Match;
322 (* meta propositions *)
326 fun aprop t = Lexicon.const "_aprop" $ t;
329 fastype_of1 (Ts, t) = propT handle TERM _ => false;
331 fun is_term (Const ("ProtoPure.term", _) $ _) = true
334 fun tr' _ (t as Const _) = t
335 | tr' Ts (t as Const ("_bound", _) $ u) =
336 if is_prop Ts u then aprop t else t
337 | tr' _ (t as Free (x, T)) =
338 if T = propT then aprop (Lexicon.free x) else t
339 | tr' _ (t as Var (xi, T)) =
340 if T = propT then aprop (Lexicon.var xi) else t
341 | tr' Ts (t as Bound _) =
342 if is_prop Ts t then aprop t else t
343 | tr' Ts (Abs (x, T, t)) = Abs (x, T, tr' (T :: Ts) t)
344 | tr' Ts (t as t1 $ (t2 as Const ("TYPE", Type ("itself", [T])))) =
345 if is_prop Ts t andalso not (is_term t) then Const ("_mk_ofclass", T) $ tr' Ts t1
346 else tr' Ts t1 $ tr' Ts t2
347 | tr' Ts (t as t1 $ t2) =
348 (if is_Const (Term.head_of t) orelse not (is_prop Ts t)
349 then I else aprop) (tr' Ts t1 $ tr' Ts t2);
352 fun mk_ofclass_tr' show_sorts (*"_mk_ofclass"*) T [t] =
353 Lexicon.const "_ofclass" $ TypeExt.term_of_typ show_sorts T $ t
354 | mk_ofclass_tr' _ (*"_mk_ofclass"*) T ts = raise TYPE ("mk_ofclass_tr'", [T], ts);
357 (* meta implication *)
359 fun impl_ast_tr' (*"==>"*) asts =
360 if TypeExt.no_brackets () then raise Match
362 (case Ast.unfold_ast_p "==>" (Ast.Appl (Ast.Constant "==>" :: asts)) of
363 (prems as _ :: _ :: _, concl) =>
365 val (asms, asm) = split_last prems;
366 val asms' = Ast.fold_ast_p "_asms" (asms, Ast.Appl [Ast.Constant "_asm", asm]);
367 in Ast.Appl [Ast.Constant "_bigimpl", asms', concl] end
371 (* type reflection *)
373 fun type_tr' show_sorts (*"TYPE"*) (Type ("itself", [T])) ts =
374 Term.list_comb (Lexicon.const "_TYPE" $ TypeExt.term_of_typ show_sorts T, ts)
375 | type_tr' _ _ _ = raise Match;
378 (* type constraints *)
380 fun type_constraint_tr' show_sorts (*"_type_constraint_"*) (Type ("fun", [T, _])) (t :: ts) =
381 Term.list_comb (Lexicon.const SynExt.constrainC $ t $ TypeExt.term_of_typ show_sorts T, ts)
382 | type_constraint_tr' _ _ _ = raise Match;
385 (* dependent / nondependent quantifiers *)
387 fun var_abs mark (x, T, b) =
388 let val ([x'], _) = Name.variants [x] (Term.declare_term_names b Name.context)
389 in (x', subst_bound (mark (x', T), b)) end;
391 val variant_abs = var_abs Free;
392 val variant_abs' = var_abs mark_boundT;
394 fun dependent_tr' (q, r) (A :: Abs (x, T, B) :: ts) =
395 if Term.loose_bvar1 (B, 0) then
396 let val (x', B') = variant_abs' (x, dummyT, B);
397 in Term.list_comb (Lexicon.const q $ mark_boundT (x', T) $ A $ B', ts) end
398 else Term.list_comb (Lexicon.const r $ A $ B, ts)
399 | dependent_tr' _ _ = raise Match;
402 (* quote / antiquote *)
404 fun antiquote_tr' name =
407 if u aconv Bound i then Lexicon.const name $ tr' i t
408 else tr' i t $ tr' i u
409 | tr' i (Abs (x, T, t)) = Abs (x, T, tr' (i + 1) t)
410 | tr' i a = if a aconv Bound i then raise Match else a;
413 fun quote_tr' name (Abs (_, _, t)) = Term.incr_boundvars ~1 (antiquote_tr' name t)
414 | quote_tr' _ _ = raise Match;
416 fun quote_antiquote_tr' quoteN antiquoteN name =
418 fun tr' (t :: ts) = Term.list_comb (Lexicon.const quoteN $ quote_tr' antiquoteN t, ts)
419 | tr' _ = raise Match;
425 fun index_ast_tr' (*"_index"*) [Ast.Appl [Ast.Constant "_struct", ast]] = ast
426 | index_ast_tr' _ = raise Match;
429 (* implicit structures *)
431 fun the_struct' structs s =
432 [(case Lexicon.read_nat s of
433 SOME i => Ast.Variable (the_struct structs i handle ERROR _ => raise Match)
434 | NONE => raise Match)] |> Ast.mk_appl (Ast.Constant "_free");
436 fun struct_ast_tr' structs (*"_struct"*) [Ast.Constant "_indexdefault"] =
437 the_struct' structs "1"
438 | struct_ast_tr' structs (*"_struct"*) [Ast.Appl [Ast.Constant "_indexnum", Ast.Constant s]] =
439 the_struct' structs s
440 | struct_ast_tr' _ _ = raise Match;
444 (** Pure translations **)
447 ([("_constify", constify_ast_tr), ("_appl", appl_ast_tr), ("_applC", applC_ast_tr),
448 ("_lambda", lambda_ast_tr), ("_idtyp", idtyp_ast_tr), ("_idtypdummy", idtypdummy_ast_tr),
449 ("_bigimpl", bigimpl_ast_tr), ("_indexdefault", indexdefault_ast_tr),
450 ("_indexnum", indexnum_ast_tr), ("_indexvar", indexvar_ast_tr), ("_struct", struct_ast_tr)],
451 [("_abs", abs_tr), ("_aprop", aprop_tr), ("_ofclass", ofclass_tr),
452 ("_TYPE", type_tr), ("_DDDOT", dddot_tr),
453 ("_index", index_tr)],
454 ([]: (string * (term list -> term)) list),
455 [("_abs", abs_ast_tr'), ("_idts", idtyp_ast_tr' "_idts"),
456 ("_pttrns", idtyp_ast_tr' "_pttrns"), ("==>", impl_ast_tr'),
457 ("_index", index_ast_tr')]);
460 [("_mk_ofclass", mk_ofclass_tr'), ("TYPE", type_tr'),
461 ("_type_constraint_", type_constraint_tr')];
463 fun struct_trfuns structs =
464 ([], [("_struct", struct_tr structs)], [], [("_struct", struct_ast_tr' structs)]);
470 fun pts_to_asts context trf pts =
474 NONE => Ast.mk_appl (Ast.Constant a) args
475 | SOME f => transform_failure (fn exn =>
476 EXCEPTION (exn, "Error in parse ast translation for " ^ quote a))
477 (fn () => f context args) ());
479 (*translate pt bottom-up*)
480 fun ast_of (Parser.Node (a, pts)) = trans a (map ast_of pts)
481 | ast_of (Parser.Tip tok) = Ast.Variable (Lexicon.str_of_token tok);
483 val exn_results = map (capture ast_of) pts;
484 val exns = map_filter get_exn exn_results;
485 val results = map_filter get_result exn_results
486 in (case (results, exns) of ([], exn :: _) => raise exn | _ => results) end;
490 (** asts_to_terms **)
492 fun asts_to_terms context trf asts =
496 NONE => Term.list_comb (Lexicon.const a, args)
497 | SOME f => transform_failure (fn exn =>
498 EXCEPTION (exn, "Error in parse translation for " ^ quote a))
499 (fn () => f context args) ());
501 fun term_of (Ast.Constant a) = trans a []
502 | term_of (Ast.Variable x) = Lexicon.read_var x
503 | term_of (Ast.Appl (Ast.Constant a :: (asts as _ :: _))) =
504 trans a (map term_of asts)
505 | term_of (Ast.Appl (ast :: (asts as _ :: _))) =
506 Term.list_comb (term_of ast, map term_of asts)
507 | term_of (ast as Ast.Appl _) = raise Ast.AST ("ast_to_term: malformed ast", [ast]);
509 val free_fixed = Term.map_aterms
510 (fn t as Const (c, T) =>
511 (case try (unprefix Lexicon.fixedN) c of
513 | SOME x => Free (x, T))
516 val exn_results = map (capture (term_of #> free_fixed)) asts;
517 val exns = map_filter get_exn exn_results;
518 val results = map_filter get_result exn_results
519 in (case (results, exns) of ([], exn :: _) => raise exn | _ => results) end;