src/Pure/Syntax/syn_trans.ML
author haftmann
Tue, 21 Jul 2009 14:38:07 +0200
changeset 32098 53a21a5e6889
parent 31545 3371a3c19bb1
child 32738 15bb09ca0378
permissions -rw-r--r--
attempt for more concise setup of non-etacontracting binders
     1 (*  Title:      Pure/Syntax/syn_trans.ML
     2     Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
     3 
     4 Syntax translation functions.
     5 *)
     6 
     7 signature SYN_TRANS0 =
     8 sig
     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
    27 end;
    28 
    29 signature SYN_TRANS1 =
    30 sig
    31   include SYN_TRANS0
    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
    35   val pure_trfuns:
    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
    46 end;
    47 
    48 signature SYN_TRANS =
    49 sig
    50   include SYN_TRANS1
    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
    60 end;
    61 
    62 structure SynTrans: SYN_TRANS =
    63 struct
    64 
    65 
    66 (** parse (ast) translations **)
    67 
    68 (* constify *)
    69 
    70 fun constify_ast_tr [Ast.Variable c] = Ast.Constant c
    71   | constify_ast_tr asts = raise Ast.AST ("constify_ast_tr", asts);
    72 
    73 
    74 (* application *)
    75 
    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);
    78 
    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);
    81 
    82 
    83 (* abstraction *)
    84 
    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);
    87 
    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);
    91 
    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);
    95 
    96 val constrainAbsC = "_constrainAbs";
    97 
    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);
   101 
   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);
   109 
   110 
   111 (* binder *)
   112 
   113 fun mk_binder_tr (syn, name) =
   114   let
   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]);
   123 
   124     fun binder_tr [idts, body] = tr (idts, body)
   125       | binder_tr ts = raise TERM ("binder_tr", ts);
   126   in (syn, binder_tr) end;
   127 
   128 
   129 (* type propositions *)
   130 
   131 fun mk_type ty = Lexicon.const "_constrain" $ Lexicon.const "TYPE" $ (Lexicon.const "itself" $ ty);
   132 
   133 fun ofclass_tr (*"_ofclass"*) [ty, cls] = cls $ mk_type ty
   134   | ofclass_tr (*"_ofclass"*) ts = raise TERM ("ofclass_tr", ts);
   135 
   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);
   139 
   140 
   141 (* meta propositions *)
   142 
   143 fun aprop_tr (*"_aprop"*) [t] = Lexicon.const "_constrain" $ t $ Lexicon.const "prop"
   144   | aprop_tr (*"_aprop"*) ts = raise TERM ("aprop_tr", ts);
   145 
   146 
   147 (* meta implication *)
   148 
   149 fun bigimpl_ast_tr (*"_bigimpl"*) (asts as [asms, concl]) =
   150       let val prems =
   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);
   156 
   157 
   158 (* meta conjunction *)
   159 
   160 fun conjunction_tr [t, u] = Lexicon.const "Pure.conjunction" $ t $ u
   161   | conjunction_tr ts = raise TERM ("conjunction_tr", ts);
   162 
   163 
   164 (* type/term reflection *)
   165 
   166 fun type_tr (*"_TYPE"*) [ty] = mk_type ty
   167   | type_tr (*"_TYPE"*) ts = raise TERM ("type_tr", ts);
   168 
   169 fun term_tr [t] = Lexicon.const "Pure.term" $ t
   170   | term_tr ts = raise TERM ("term_tr", ts);
   171 
   172 
   173 (* dddot *)
   174 
   175 fun dddot_tr (*"_DDDOT"*) ts = Term.list_comb (Lexicon.var SynExt.dddot_indexname, ts);
   176 
   177 
   178 (* quote / antiquote *)
   179 
   180 fun antiquote_tr name =
   181   let
   182     fun tr i ((t as Const (c, _)) $ u) =
   183           if c = name then tr i u $ Bound i
   184           else tr i t $ tr i u
   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)
   187       | tr _ a = a;
   188   in tr 0 end;
   189 
   190 fun quote_tr name t = Abs ("s", dummyT, antiquote_tr name (Term.incr_boundvars 1 t));
   191 
   192 fun quote_antiquote_tr quoteN antiquoteN name =
   193   let
   194     fun tr [t] = Lexicon.const name $ quote_tr antiquoteN t
   195       | tr ts = raise TERM ("quote_tr", ts);
   196   in (quoteN, tr) end;
   197 
   198 
   199 (* indexed syntax *)
   200 
   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;
   203 
   204 fun index_ast_tr ast =
   205   Ast.mk_appl (Ast.Constant "_index") [Ast.mk_appl (Ast.Constant "_struct") [ast]];
   206 
   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);
   211 
   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);
   215 
   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);
   219 
   220 fun index_tr (*"_index"*) [t] = t
   221   | index_tr (*"_index"*) ts = raise TERM ("index_tr", ts);
   222 
   223 
   224 (* implicit structures *)
   225 
   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);
   229 
   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);
   236 
   237 
   238 
   239 (** print (ast) translations **)
   240 
   241 (* types *)
   242 
   243 fun non_typed_tr' f _ _ ts = f ts;
   244 fun non_typed_tr'' f x _ _ ts = f x ts;
   245 
   246 
   247 (* application *)
   248 
   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];
   251 
   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];
   254 
   255 
   256 (* abstraction *)
   257 
   258 fun mark_boundT (x, T) = Const ("_bound", T --> T) $ Free (x, T);
   259 fun mark_bound x = mark_boundT (x, dummyT);
   260 
   261 fun bound_vars vars body =
   262   subst_bounds (map mark_boundT (Term.rename_wrt_term body vars), body);
   263 
   264 fun strip_abss vars_of body_of tm =
   265   let
   266     val vars = vars_of tm;
   267     val body = body_of tm;
   268     val rev_new_vars = Term.rename_wrt_term body vars;
   269     fun subst (x, T) b =
   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;
   275 
   276 
   277 (*do (partial) eta-contraction before printing*)
   278 
   279 val eta_contract = ref true;
   280 
   281 fun eta_contr tm =
   282   let
   283     fun is_aprop (Const ("_aprop", _)) = true
   284       | is_aprop _ = false;
   285 
   286     fun eta_abs (Abs (a, T, t)) =
   287           (case eta_abs t of
   288             t' as f $ u =>
   289               (case eta_abs u of
   290                 Bound 0 =>
   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'))
   295       | eta_abs t = t;
   296   in
   297     if ! eta_contract then eta_abs tm else tm
   298   end;
   299 
   300 
   301 fun abs_tr' 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));
   304 
   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;
   308 
   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]);
   313 
   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);
   317 
   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);
   321 
   322 
   323 (* binder *)
   324 
   325 fun mk_binder_tr' (name, syn) =
   326   let
   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;
   330 
   331     fun tr' t =
   332       let
   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;
   335 
   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;
   339 
   340 
   341 (* idtyp constraints *)
   342 
   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]
   346       else raise Match
   347   | idtyp_ast_tr' _ _ = raise Match;
   348 
   349 
   350 (* type propositions *)
   351 
   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);
   357 
   358 
   359 (* meta propositions *)
   360 
   361 fun prop_tr' tm =
   362   let
   363     fun aprop t = Lexicon.const "_aprop" $ t;
   364 
   365     fun is_prop Ts t =
   366       fastype_of1 (Ts, t) = propT handle TERM _ => false;
   367 
   368     fun is_term (Const ("Pure.term", _) $ _) = true
   369       | is_term _ = false;
   370 
   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);
   387   in tr' [] tm end;
   388 
   389 
   390 (* meta implication *)
   391 
   392 fun impl_ast_tr' (*"==>"*) asts =
   393   if TypeExt.no_brackets () then raise Match
   394   else
   395     (case Ast.unfold_ast_p "==>" (Ast.Appl (Ast.Constant "==>" :: asts)) of
   396       (prems as _ :: _ :: _, concl) =>
   397         let
   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
   401     | _ => raise Match);
   402 
   403 
   404 (* type reflection *)
   405 
   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;
   409 
   410 
   411 (* type constraints *)
   412 
   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;
   416 
   417 
   418 (* dependent / nondependent quantifiers *)
   419 
   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;
   423 
   424 val variant_abs = var_abs Free;
   425 val variant_abs' = var_abs mark_boundT;
   426 
   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;
   433 
   434 
   435 (* quote / antiquote *)
   436 
   437 fun antiquote_tr' name =
   438   let
   439     fun tr' i (t $ u) =
   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;
   444   in tr' 0 end;
   445 
   446 fun quote_tr' name (Abs (_, _, t)) = Term.incr_boundvars ~1 (antiquote_tr' name t)
   447   | quote_tr' _ _ = raise Match;
   448 
   449 fun quote_antiquote_tr' quoteN antiquoteN name =
   450   let
   451     fun tr' (t :: ts) = Term.list_comb (Lexicon.const quoteN $ quote_tr' antiquoteN t, ts)
   452       | tr' _ = raise Match;
   453   in (name, tr') end;
   454 
   455 
   456 (* indexed syntax *)
   457 
   458 fun index_ast_tr' (*"_index"*) [Ast.Appl [Ast.Constant "_struct", ast]] = ast
   459   | index_ast_tr' _ = raise Match;
   460 
   461 
   462 (* implicit structures *)
   463 
   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");
   468 
   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;
   474 
   475 
   476 
   477 (** Pure translations **)
   478 
   479 val pure_trfuns =
   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')]);
   491 
   492 val pure_trfunsT =
   493   [("_type_prop", type_prop_tr'), ("TYPE", type_tr'), ("_type_constraint_", type_constraint_tr')];
   494 
   495 fun struct_trfuns structs =
   496   ([], [("_struct", struct_tr structs)], [], [("_struct", struct_ast_tr' structs)]);
   497 
   498 
   499 
   500 (** pts_to_asts **)
   501 
   502 fun pts_to_asts ctxt trf pts =
   503   let
   504     fun trans a args =
   505       (case trf a of
   506         NONE => Ast.mk_appl (Ast.Constant a) args
   507       | SOME f => f ctxt args);
   508 
   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);
   512 
   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;
   517 
   518 
   519 
   520 (** asts_to_terms **)
   521 
   522 fun asts_to_terms ctxt trf asts =
   523   let
   524     fun trans a args =
   525       (case trf a of
   526         NONE => Term.list_comb (Lexicon.const a, args)
   527       | SOME f => f ctxt args);
   528 
   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]);
   536 
   537     val free_fixed = Term.map_aterms
   538       (fn t as Const (c, T) =>
   539           (case try (unprefix Lexicon.fixedN) c of
   540             NONE => t
   541           | SOME x => Free (x, T))
   542         | t => t);
   543 
   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;
   548 
   549 end;