src/Pure/Syntax/syn_trans.ML
author wenzelm
Tue Mar 22 15:32:47 2011 +0100 (2011-03-22)
changeset 42052 34f1d2d81284
parent 42048 afd11ca8e018
child 42053 006095137a81
permissions -rw-r--r--
statespace syntax: strip positions -- type constraints are unexpected here;
     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_default: bool Unsynchronized.ref
    10   val eta_contract_raw: Config.raw
    11   val eta_contract: bool Config.T
    12   val atomic_abs_tr': string * typ * term -> term * term
    13   val preserve_binder_abs_tr': string -> string -> string * (term list -> term)
    14   val preserve_binder_abs2_tr': string -> string -> string * (term list -> term)
    15   val mk_binder_tr: string * string -> string * (term list -> term)
    16   val mk_binder_tr': string * string -> string * (term list -> term)
    17   val dependent_tr': 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 antiquote_tr': string -> term -> term
    22   val quote_tr': string -> term -> term
    23   val quote_antiquote_tr': string -> string -> string -> string * (term list -> term)
    24   val update_name_tr': term -> term
    25   val mark_bound: string -> term
    26   val mark_boundT: string * typ -> term
    27   val bound_vars: (string * typ) list -> term -> term
    28   val variant_abs: string * typ * term -> string * term
    29   val variant_abs': string * typ * term -> string * term
    30 end;
    31 
    32 signature SYN_TRANS1 =
    33 sig
    34   include SYN_TRANS0
    35   val non_typed_tr': (term list -> term) -> bool -> typ -> term list -> term
    36   val non_typed_tr'': ('a -> term list -> term) -> 'a -> bool -> typ -> term list -> term
    37   val constrainAbsC: string
    38   val pure_trfuns:
    39     (string * (Ast.ast list -> Ast.ast)) list *
    40     (string * (term list -> term)) list *
    41     (string * (term list -> term)) list *
    42     (string * (Ast.ast list -> Ast.ast)) list
    43   val pure_trfunsT: (string * (bool -> typ -> term list -> term)) list
    44   val struct_trfuns: string list ->
    45     (string * (Ast.ast list -> Ast.ast)) list *
    46     (string * (term list -> term)) list *
    47     (string * (bool -> typ -> term list -> term)) list *
    48     (string * (Ast.ast list -> Ast.ast)) list
    49 end;
    50 
    51 signature SYN_TRANS =
    52 sig
    53   include SYN_TRANS1
    54   val abs_tr': Proof.context -> term -> term
    55   val prop_tr': term -> term
    56   val appl_ast_tr': Ast.ast * Ast.ast list -> Ast.ast
    57   val applC_ast_tr': Ast.ast * Ast.ast list -> Ast.ast
    58   val parsetree_to_ast: Proof.context -> bool ->
    59     (string -> (Proof.context -> Ast.ast list -> Ast.ast) option) -> Parser.parsetree -> Ast.ast
    60   val ast_to_term: Proof.context ->
    61     (string -> (Proof.context -> term list -> term) option) -> Ast.ast -> term
    62 end;
    63 
    64 structure Syn_Trans: SYN_TRANS =
    65 struct
    66 
    67 
    68 (** parse (ast) translations **)
    69 
    70 (* constify *)
    71 
    72 fun constify_ast_tr [Ast.Variable c] = Ast.Constant c
    73   | constify_ast_tr asts = raise Ast.AST ("constify_ast_tr", asts);
    74 
    75 
    76 (* application *)
    77 
    78 fun appl_ast_tr [f, args] = Ast.Appl (f :: Ast.unfold_ast "_args" args)
    79   | appl_ast_tr asts = raise Ast.AST ("appl_ast_tr", asts);
    80 
    81 fun applC_ast_tr [f, args] = Ast.Appl (f :: Ast.unfold_ast "_cargs" args)
    82   | applC_ast_tr asts = raise Ast.AST ("applC_ast_tr", asts);
    83 
    84 
    85 (* abstraction *)
    86 
    87 fun idtyp_ast_tr (*"_idtyp"*) [x, ty] = Ast.Appl [Ast.Constant "_constrain", x, ty]
    88   | idtyp_ast_tr (*"_idtyp"*) asts = raise Ast.AST ("idtyp_ast_tr", asts);
    89 
    90 fun idtypdummy_ast_tr (*"_idtypdummy"*) [ty] =
    91       Ast.Appl [Ast.Constant "_constrain", Ast.Constant "_idtdummy", ty]
    92   | idtypdummy_ast_tr (*"_idtypdummy"*) asts = raise Ast.AST ("idtyp_ast_tr", asts);
    93 
    94 fun lambda_ast_tr (*"_lambda"*) [pats, body] =
    95       Ast.fold_ast_p "_abs" (Ast.unfold_ast "_pttrns" pats, body)
    96   | lambda_ast_tr (*"_lambda"*) asts = raise Ast.AST ("lambda_ast_tr", asts);
    97 
    98 val constrainAbsC = "_constrainAbs";
    99 
   100 fun absfree_proper (x, T, t) =
   101   if can Name.dest_internal x
   102   then error ("Illegal internal variable in abstraction: " ^ quote x)
   103   else Term.absfree (x, T, t);
   104 
   105 fun abs_tr [Free (x, T), t] = absfree_proper (x, T, t)
   106   | abs_tr [Const ("_idtdummy", T), t] = Term.absdummy (T, t)
   107   | abs_tr [Const ("_constrain", _) $ x $ tT, t] = Lexicon.const constrainAbsC $ abs_tr [x, t] $ tT
   108   | abs_tr ts = raise TERM ("abs_tr", ts);
   109 
   110 
   111 (* binder *)
   112 
   113 fun mk_binder_tr (syn, name) =
   114   let
   115     fun binder_tr [Const ("_idts", _) $ idt $ idts, t] = binder_tr [idt, binder_tr [idts, t]]
   116       | binder_tr [x, t] =
   117           let val abs = abs_tr [x, t] handle TERM _ => raise TERM ("binder_tr", [x, t])
   118           in Lexicon.const name $ abs end
   119       | binder_tr ts = raise TERM ("binder_tr", ts);
   120   in (syn, binder_tr) end;
   121 
   122 
   123 (* type propositions *)
   124 
   125 fun mk_type ty =
   126   Lexicon.const "_constrain" $
   127     Lexicon.const "\\<^const>TYPE" $ (Lexicon.const "\\<^type>itself" $ ty);
   128 
   129 fun ofclass_tr (*"_ofclass"*) [ty, cls] = cls $ mk_type ty
   130   | ofclass_tr (*"_ofclass"*) ts = raise TERM ("ofclass_tr", ts);
   131 
   132 fun sort_constraint_tr (*"_sort_constraint"*) [ty] =
   133       Lexicon.const "\\<^const>Pure.sort_constraint" $ mk_type ty
   134   | sort_constraint_tr (*"_sort_constraint"*) ts = raise TERM ("sort_constraint_tr", ts);
   135 
   136 
   137 (* meta propositions *)
   138 
   139 fun aprop_tr (*"_aprop"*) [t] = Lexicon.const "_constrain" $ t $ Lexicon.const "\\<^type>prop"
   140   | aprop_tr (*"_aprop"*) ts = raise TERM ("aprop_tr", ts);
   141 
   142 
   143 (* meta implication *)
   144 
   145 fun bigimpl_ast_tr (*"_bigimpl"*) (asts as [asms, concl]) =
   146       let val prems =
   147         (case Ast.unfold_ast_p "_asms" asms of
   148           (asms', Ast.Appl [Ast.Constant "_asm", asm']) => asms' @ [asm']
   149         | _ => raise Ast.AST ("bigimpl_ast_tr", asts))
   150       in Ast.fold_ast_p "\\<^const>==>" (prems, concl) end
   151   | bigimpl_ast_tr (*"_bigimpl"*) asts = raise Ast.AST ("bigimpl_ast_tr", asts);
   152 
   153 
   154 (* type/term reflection *)
   155 
   156 fun type_tr (*"_TYPE"*) [ty] = mk_type ty
   157   | type_tr (*"_TYPE"*) ts = raise TERM ("type_tr", ts);
   158 
   159 
   160 (* dddot *)
   161 
   162 fun dddot_tr (*"_DDDOT"*) ts = Term.list_comb (Lexicon.var Syn_Ext.dddot_indexname, ts);
   163 
   164 
   165 (* quote / antiquote *)
   166 
   167 fun antiquote_tr name =
   168   let
   169     fun tr i ((t as Const (c, _)) $ u) =
   170           if c = name then tr i u $ Bound i
   171           else tr i t $ tr i u
   172       | tr i (t $ u) = tr i t $ tr i u
   173       | tr i (Abs (x, T, t)) = Abs (x, T, tr (i + 1) t)
   174       | tr _ a = a;
   175   in tr 0 end;
   176 
   177 fun quote_tr name t = Abs ("s", dummyT, antiquote_tr name (Term.incr_boundvars 1 t));
   178 
   179 fun quote_antiquote_tr quoteN antiquoteN name =
   180   let
   181     fun tr [t] = Lexicon.const name $ quote_tr antiquoteN t
   182       | tr ts = raise TERM ("quote_tr", ts);
   183   in (quoteN, tr) end;
   184 
   185 
   186 (* corresponding updates *)
   187 
   188 fun update_name_tr (Free (x, T) :: ts) = list_comb (Free (suffix "_update" x, T), ts)
   189   | update_name_tr (Const (x, T) :: ts) = list_comb (Const (suffix "_update" x, T), ts)
   190   | update_name_tr (((c as Const ("_constrain", _)) $ t $ ty) :: ts) =
   191       list_comb (c $ update_name_tr [t] $
   192         (Lexicon.const "\\<^type>fun" $ ty $ Lexicon.const "\\<^type>dummy"), ts)
   193   | update_name_tr ts = raise TERM ("update_name_tr", ts);
   194 
   195 
   196 (* indexed syntax *)
   197 
   198 fun struct_ast_tr (*"_struct"*) [Ast.Appl [Ast.Constant "_index", ast]] = ast
   199   | struct_ast_tr (*"_struct"*) asts = Ast.mk_appl (Ast.Constant "_struct") asts;
   200 
   201 fun index_ast_tr ast =
   202   Ast.mk_appl (Ast.Constant "_index") [Ast.mk_appl (Ast.Constant "_struct") [ast]];
   203 
   204 fun indexdefault_ast_tr (*"_indexdefault"*) [] =
   205       index_ast_tr (Ast.Constant "_indexdefault")
   206   | indexdefault_ast_tr (*"_indexdefault"*) asts =
   207       raise Ast.AST ("indexdefault_ast_tr", asts);
   208 
   209 fun indexnum_ast_tr (*"_indexnum"*) [ast] =
   210       index_ast_tr (Ast.mk_appl (Ast.Constant "_indexnum") [ast])
   211   | indexnum_ast_tr (*"_indexnum"*) asts = raise Ast.AST ("indexnum_ast_tr", asts);
   212 
   213 fun indexvar_ast_tr (*"_indexvar"*) [] =
   214       Ast.mk_appl (Ast.Constant "_index") [Ast.Variable "some_index"]
   215   | indexvar_ast_tr (*"_indexvar"*) asts = raise Ast.AST ("indexvar_ast_tr", asts);
   216 
   217 fun index_tr (*"_index"*) [t] = t
   218   | index_tr (*"_index"*) ts = raise TERM ("index_tr", ts);
   219 
   220 
   221 (* implicit structures *)
   222 
   223 fun the_struct structs i =
   224   if 1 <= i andalso i <= length structs then nth structs (i - 1)
   225   else error ("Illegal reference to implicit structure #" ^ string_of_int i);
   226 
   227 fun struct_tr structs (*"_struct"*) [Const ("_indexdefault", _)] =
   228       Lexicon.free (the_struct structs 1)
   229   | struct_tr structs (*"_struct"*) [t as (Const ("_indexnum", _) $ Const (s, _))] =
   230       Lexicon.free (the_struct structs
   231         (case Lexicon.read_nat s of SOME n => n | NONE => raise TERM ("struct_tr", [t])))
   232   | struct_tr _ (*"_struct"*) ts = raise TERM ("struct_tr", ts);
   233 
   234 
   235 
   236 (** print (ast) translations **)
   237 
   238 (* types *)
   239 
   240 fun non_typed_tr' f _ _ ts = f ts;
   241 fun non_typed_tr'' f x _ _ ts = f x ts;
   242 
   243 
   244 (* application *)
   245 
   246 fun appl_ast_tr' (f, []) = raise Ast.AST ("appl_ast_tr'", [f])
   247   | appl_ast_tr' (f, args) = Ast.Appl [Ast.Constant "_appl", f, Ast.fold_ast "_args" args];
   248 
   249 fun applC_ast_tr' (f, []) = raise Ast.AST ("applC_ast_tr'", [f])
   250   | applC_ast_tr' (f, args) = Ast.Appl [Ast.Constant "_applC", f, Ast.fold_ast "_cargs" args];
   251 
   252 
   253 (* abstraction *)
   254 
   255 fun mark_boundT (x, T) = Const ("_bound", T --> T) $ Free (x, T);
   256 fun mark_bound x = mark_boundT (x, dummyT);
   257 
   258 fun bound_vars vars body =
   259   subst_bounds (map mark_boundT (Term.rename_wrt_term body vars), body);
   260 
   261 fun strip_abss vars_of body_of tm =
   262   let
   263     val vars = vars_of tm;
   264     val body = body_of tm;
   265     val rev_new_vars = Term.rename_wrt_term body vars;
   266     fun subst (x, T) b =
   267       if can Name.dest_internal x andalso not (Term.loose_bvar1 (b, 0))
   268       then (Const ("_idtdummy", T), incr_boundvars ~1 b)
   269       else (mark_boundT (x, T), Term.subst_bound (mark_bound x, b));
   270     val (rev_vars', body') = fold_map subst rev_new_vars body;
   271   in (rev rev_vars', body') end;
   272 
   273 
   274 (*do (partial) eta-contraction before printing*)
   275 
   276 val eta_contract_default = Unsynchronized.ref true;
   277 val eta_contract_raw = Config.declare "eta_contract" (fn _ => Config.Bool (! eta_contract_default));
   278 val eta_contract = Config.bool eta_contract_raw;
   279 
   280 fun eta_contr ctxt tm =
   281   let
   282     fun is_aprop (Const ("_aprop", _)) = true
   283       | is_aprop _ = false;
   284 
   285     fun eta_abs (Abs (a, T, t)) =
   286           (case eta_abs t of
   287             t' as f $ u =>
   288               (case eta_abs u of
   289                 Bound 0 =>
   290                   if Term.loose_bvar1 (f, 0) orelse is_aprop f then Abs (a, T, t')
   291                   else  incr_boundvars ~1 f
   292               | _ => Abs (a, T, t'))
   293           | t' => Abs (a, T, t'))
   294       | eta_abs t = t;
   295   in
   296     if Config.get ctxt eta_contract then eta_abs tm else tm
   297   end;
   298 
   299 
   300 fun abs_tr' ctxt tm =
   301   uncurry (fold_rev (fn x => fn t => Lexicon.const "_abs" $ x $ t))
   302     (strip_abss strip_abs_vars strip_abs_body (eta_contr ctxt tm));
   303 
   304 fun atomic_abs_tr' (x, T, t) =
   305   let val [xT] = Term.rename_wrt_term t [(x, T)]
   306   in (mark_boundT xT, subst_bound (mark_bound (fst xT), t)) end;
   307 
   308 fun abs_ast_tr' (*"_abs"*) asts =
   309   (case Ast.unfold_ast_p "_abs" (Ast.Appl (Ast.Constant "_abs" :: asts)) of
   310     ([], _) => raise Ast.AST ("abs_ast_tr'", asts)
   311   | (xs, body) => Ast.Appl [Ast.Constant "_lambda", Ast.fold_ast "_pttrns" xs, body]);
   312 
   313 fun preserve_binder_abs_tr' name syn = (name, fn (Abs abs :: ts) =>
   314   let val (x, t) = atomic_abs_tr' abs
   315   in list_comb (Lexicon.const syn $ x $ t, ts) end);
   316 
   317 fun preserve_binder_abs2_tr' name syn = (name, fn (A :: Abs abs :: ts) =>
   318   let val (x, t) = atomic_abs_tr' abs
   319   in list_comb (Lexicon.const syn $ x $ A $ t, ts) end);
   320 
   321 
   322 (* binder *)
   323 
   324 fun mk_binder_tr' (name, syn) =
   325   let
   326     fun mk_idts [] = raise Match    (*abort translation*)
   327       | mk_idts [idt] = idt
   328       | mk_idts (idt :: idts) = Lexicon.const "_idts" $ idt $ mk_idts idts;
   329 
   330     fun tr' t =
   331       let
   332         val (xs, bd) = strip_abss (strip_qnt_vars name) (strip_qnt_body name) t;
   333       in Lexicon.const syn $ mk_idts xs $ bd end;
   334 
   335     fun binder_tr' (t :: ts) = Term.list_comb (tr' (Lexicon.const name $ t), ts)
   336       | binder_tr' [] = raise Match;
   337   in (name, binder_tr') end;
   338 
   339 
   340 (* idtyp constraints *)
   341 
   342 fun idtyp_ast_tr' a [Ast.Appl [Ast.Constant "_constrain", x, ty], xs] =
   343       Ast.Appl [Ast.Constant a, Ast.Appl [Ast.Constant "_idtyp", x, ty], xs]
   344   | idtyp_ast_tr' _ _ = raise Match;
   345 
   346 
   347 (* type propositions *)
   348 
   349 fun type_prop_tr' _ (*"_type_prop"*) T [Const ("\\<^const>Pure.sort_constraint", _)] =
   350       Lexicon.const "_sort_constraint" $ Type_Ext.term_of_typ true T
   351   | type_prop_tr' show_sorts (*"_type_prop"*) T [t] =
   352       Lexicon.const "_ofclass" $ Type_Ext.term_of_typ show_sorts T $ t
   353   | type_prop_tr' _ (*"_type_prop"*) T ts = raise TYPE ("type_prop_tr'", [T], ts);
   354 
   355 
   356 (* meta propositions *)
   357 
   358 fun prop_tr' tm =
   359   let
   360     fun aprop t = Lexicon.const "_aprop" $ t;
   361 
   362     fun is_prop Ts t =
   363       fastype_of1 (Ts, t) = propT handle TERM _ => false;
   364 
   365     fun is_term (Const ("Pure.term", _) $ _) = true
   366       | is_term _ = false;
   367 
   368     fun tr' _ (t as Const _) = t
   369       | tr' Ts (t as Const ("_bound", _) $ u) =
   370           if is_prop Ts u then aprop t else t
   371       | tr' _ (t as Free (x, T)) =
   372           if T = propT then aprop (Lexicon.free x) else t
   373       | tr' _ (t as Var (xi, T)) =
   374           if T = propT then aprop (Lexicon.var xi) else t
   375       | tr' Ts (t as Bound _) =
   376           if is_prop Ts t then aprop t else t
   377       | tr' Ts (Abs (x, T, t)) = Abs (x, T, tr' (T :: Ts) t)
   378       | tr' Ts (t as t1 $ (t2 as Const ("TYPE", Type ("itself", [T])))) =
   379           if is_prop Ts t andalso not (is_term t) then Const ("_type_prop", T) $ tr' Ts t1
   380           else tr' Ts t1 $ tr' Ts t2
   381       | tr' Ts (t as t1 $ t2) =
   382           (if is_Const (Term.head_of t) orelse not (is_prop Ts t)
   383             then I else aprop) (tr' Ts t1 $ tr' Ts t2);
   384   in tr' [] tm end;
   385 
   386 
   387 (* meta implication *)
   388 
   389 fun impl_ast_tr' (*"==>"*) asts =
   390   if Type_Ext.no_brackets () then raise Match
   391   else
   392     (case Ast.unfold_ast_p "\\<^const>==>" (Ast.Appl (Ast.Constant "\\<^const>==>" :: asts)) of
   393       (prems as _ :: _ :: _, concl) =>
   394         let
   395           val (asms, asm) = split_last prems;
   396           val asms' = Ast.fold_ast_p "_asms" (asms, Ast.Appl [Ast.Constant "_asm", asm]);
   397         in Ast.Appl [Ast.Constant "_bigimpl", asms', concl] end
   398     | _ => raise Match);
   399 
   400 
   401 (* type reflection *)
   402 
   403 fun type_tr' show_sorts (*"TYPE"*) (Type ("itself", [T])) ts =
   404       Term.list_comb (Lexicon.const "_TYPE" $ Type_Ext.term_of_typ show_sorts T, ts)
   405   | type_tr' _ _ _ = raise Match;
   406 
   407 
   408 (* type constraints *)
   409 
   410 fun type_constraint_tr' show_sorts (*"_type_constraint_"*) (Type ("fun", [T, _])) (t :: ts) =
   411       Term.list_comb (Lexicon.const Syn_Ext.constrainC $ t $ Type_Ext.term_of_typ show_sorts T, ts)
   412   | type_constraint_tr' _ _ _ = raise Match;
   413 
   414 
   415 (* dependent / nondependent quantifiers *)
   416 
   417 fun var_abs mark (x, T, b) =
   418   let val ([x'], _) = Name.variants [x] (Term.declare_term_names b Name.context)
   419   in (x', subst_bound (mark (x', T), b)) end;
   420 
   421 val variant_abs = var_abs Free;
   422 val variant_abs' = var_abs mark_boundT;
   423 
   424 fun dependent_tr' (q, r) (A :: Abs (x, T, B) :: ts) =
   425       if Term.loose_bvar1 (B, 0) then
   426         let val (x', B') = variant_abs' (x, dummyT, B);
   427         in Term.list_comb (Lexicon.const q $ mark_boundT (x', T) $ A $ B', ts) end
   428       else Term.list_comb (Lexicon.const r $ A $ B, ts)
   429   | dependent_tr' _ _ = raise Match;
   430 
   431 
   432 (* quote / antiquote *)
   433 
   434 fun antiquote_tr' name =
   435   let
   436     fun tr' i (t $ u) =
   437       if u aconv Bound i then Lexicon.const name $ tr' i t
   438       else tr' i t $ tr' i u
   439       | tr' i (Abs (x, T, t)) = Abs (x, T, tr' (i + 1) t)
   440       | tr' i a = if a aconv Bound i then raise Match else a;
   441   in tr' 0 end;
   442 
   443 fun quote_tr' name (Abs (_, _, t)) = Term.incr_boundvars ~1 (antiquote_tr' name t)
   444   | quote_tr' _ _ = raise Match;
   445 
   446 fun quote_antiquote_tr' quoteN antiquoteN name =
   447   let
   448     fun tr' (t :: ts) = Term.list_comb (Lexicon.const quoteN $ quote_tr' antiquoteN t, ts)
   449       | tr' _ = raise Match;
   450   in (name, tr') end;
   451 
   452 
   453 (* corresponding updates *)
   454 
   455 fun upd_tr' (x_upd, T) =
   456   (case try (unsuffix "_update") x_upd of
   457     SOME x => (x, if T = dummyT then T else Term.domain_type T)
   458   | NONE => raise Match);
   459 
   460 fun update_name_tr' (Free x) = Free (upd_tr' x)
   461   | update_name_tr' ((c as Const ("_free", _)) $ Free x) = c $ Free (upd_tr' x)
   462   | update_name_tr' (Const x) = Const (upd_tr' x)
   463   | update_name_tr' _ = raise Match;
   464 
   465 
   466 (* indexed syntax *)
   467 
   468 fun index_ast_tr' (*"_index"*) [Ast.Appl [Ast.Constant "_struct", ast]] = ast
   469   | index_ast_tr' _ = raise Match;
   470 
   471 
   472 (* implicit structures *)
   473 
   474 fun the_struct' structs s =
   475   [(case Lexicon.read_nat s of
   476     SOME i => Ast.Variable (the_struct structs i handle ERROR _ => raise Match)
   477   | NONE => raise Match)] |> Ast.mk_appl (Ast.Constant "_free");
   478 
   479 fun struct_ast_tr' structs (*"_struct"*) [Ast.Constant "_indexdefault"] =
   480       the_struct' structs "1"
   481   | struct_ast_tr' structs (*"_struct"*) [Ast.Appl [Ast.Constant "_indexnum", Ast.Constant s]] =
   482       the_struct' structs s
   483   | struct_ast_tr' _ _ = raise Match;
   484 
   485 
   486 
   487 (** Pure translations **)
   488 
   489 val pure_trfuns =
   490   ([("_constify", constify_ast_tr),
   491     ("_appl", appl_ast_tr),
   492     ("_applC", applC_ast_tr),
   493     ("_lambda", lambda_ast_tr),
   494     ("_idtyp", idtyp_ast_tr),
   495     ("_idtypdummy", idtypdummy_ast_tr),
   496     ("_bigimpl", bigimpl_ast_tr),
   497     ("_indexdefault", indexdefault_ast_tr),
   498     ("_indexnum", indexnum_ast_tr),
   499     ("_indexvar", indexvar_ast_tr),
   500     ("_struct", struct_ast_tr)],
   501    [("_abs", abs_tr),
   502     ("_aprop", aprop_tr),
   503     ("_ofclass", ofclass_tr),
   504     ("_sort_constraint", sort_constraint_tr),
   505     ("_TYPE", type_tr),
   506     ("_DDDOT", dddot_tr),
   507     ("_update_name", update_name_tr),
   508     ("_index", index_tr)],
   509    ([]: (string * (term list -> term)) list),
   510    [("_abs", abs_ast_tr'),
   511     ("_idts", idtyp_ast_tr' "_idts"),
   512     ("_pttrns", idtyp_ast_tr' "_pttrns"),
   513     ("\\<^const>==>", impl_ast_tr'),
   514     ("_index", index_ast_tr')]);
   515 
   516 val pure_trfunsT =
   517  [("_type_prop", type_prop_tr'),
   518   ("\\<^const>TYPE", type_tr'),
   519   ("_type_constraint_", type_constraint_tr')];
   520 
   521 fun struct_trfuns structs =
   522   ([], [("_struct", struct_tr structs)], [], [("_struct", struct_ast_tr' structs)]);
   523 
   524 
   525 
   526 (** parsetree_to_ast **)
   527 
   528 fun parsetree_to_ast ctxt constrain_pos trf =
   529   let
   530     fun trans a args =
   531       (case trf a of
   532         NONE => Ast.mk_appl (Ast.Constant a) args
   533       | SOME f => f ctxt args);
   534 
   535     fun ast_of (Parser.Node ("_constrain_position", [pt as Parser.Tip tok])) =
   536           if constrain_pos then
   537             Ast.Appl [Ast.Constant "_constrain", ast_of pt,
   538               Ast.Variable (Lexicon.encode_position (Lexicon.pos_of_token tok))]
   539           else ast_of pt
   540       | ast_of (Parser.Node (a, pts)) = trans a (map ast_of pts)
   541       | ast_of (Parser.Tip tok) = Ast.Variable (Lexicon.str_of_token tok);
   542   in ast_of end;
   543 
   544 
   545 
   546 (** ast_to_term **)
   547 
   548 fun ast_to_term ctxt trf =
   549   let
   550     fun trans a args =
   551       (case trf a of
   552         NONE => Term.list_comb (Lexicon.const a, args)
   553       | SOME f => f ctxt args);
   554 
   555     fun term_of (Ast.Constant a) = trans a []
   556       | term_of (Ast.Variable x) = Lexicon.read_var x
   557       | term_of (Ast.Appl (Ast.Constant a :: (asts as _ :: _))) =
   558           trans a (map term_of asts)
   559       | term_of (Ast.Appl (ast :: (asts as _ :: _))) =
   560           Term.list_comb (term_of ast, map term_of asts)
   561       | term_of (ast as Ast.Appl _) = raise Ast.AST ("ast_to_term: malformed ast", [ast]);
   562   in term_of end;
   563 
   564 end;