src/Pure/Syntax/syn_trans.ML
author wenzelm
Sun Nov 11 21:38:25 2001 +0100 (2001-11-11)
changeset 12150 f83dc4202b78
parent 12122 7f8d88ed4f21
child 12785 27debaf2112d
permissions -rw-r--r--
added meta_conjunction_tr';
wenzelm@548
     1
(*  Title:      Pure/Syntax/syn_trans.ML
wenzelm@548
     2
    ID:         $Id$
wenzelm@548
     3
    Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
wenzelm@548
     4
wenzelm@548
     5
Syntax translation functions.
wenzelm@548
     6
*)
wenzelm@548
     7
wenzelm@548
     8
signature SYN_TRANS0 =
wenzelm@2698
     9
sig
wenzelm@548
    10
  val eta_contract: bool ref
wenzelm@548
    11
  val mk_binder_tr: string * string -> string * (term list -> term)
wenzelm@548
    12
  val mk_binder_tr': string * string -> string * (term list -> term)
wenzelm@548
    13
  val dependent_tr': string * string -> term list -> term
wenzelm@8577
    14
  val antiquote_tr: string -> term -> term
wenzelm@8577
    15
  val quote_tr: string -> term -> term
wenzelm@5084
    16
  val quote_antiquote_tr: string -> string -> string -> string * (term list -> term)
wenzelm@8577
    17
  val antiquote_tr': string -> term -> term
wenzelm@8577
    18
  val quote_tr': string -> term -> term
wenzelm@5084
    19
  val quote_antiquote_tr': string -> string -> string -> string * (term list -> term)
wenzelm@2698
    20
  val mark_bound: string -> term
wenzelm@2698
    21
  val mark_boundT: string * typ -> term
wenzelm@2698
    22
  val variant_abs': string * typ * term -> string * term
wenzelm@2698
    23
end;
wenzelm@548
    24
wenzelm@548
    25
signature SYN_TRANS1 =
wenzelm@2698
    26
sig
wenzelm@548
    27
  include SYN_TRANS0
paulson@1511
    28
  val constrainAbsC: string
paulson@1511
    29
  val pure_trfuns:
paulson@1511
    30
      (string * (Ast.ast list -> Ast.ast)) list *
wenzelm@548
    31
      (string * (term list -> term)) list *
wenzelm@548
    32
      (string * (term list -> term)) list *
paulson@1511
    33
      (string * (Ast.ast list -> Ast.ast)) list
wenzelm@4148
    34
  val pure_trfunsT: (string * (bool -> typ -> term list -> term)) list
wenzelm@2698
    35
end;
wenzelm@548
    36
wenzelm@548
    37
signature SYN_TRANS =
wenzelm@2698
    38
sig
wenzelm@548
    39
  include SYN_TRANS1
paulson@1511
    40
  val abs_tr': term -> term
wenzelm@4148
    41
  val prop_tr': term -> term
paulson@1511
    42
  val appl_ast_tr': Ast.ast * Ast.ast list -> Ast.ast
paulson@1511
    43
  val applC_ast_tr': Ast.ast * Ast.ast list -> Ast.ast
paulson@1511
    44
  val pt_to_ast: (string -> (Ast.ast list -> Ast.ast) option) -> Parser.parsetree -> Ast.ast
paulson@1511
    45
  val ast_to_term: (string -> (term list -> term) option) -> Ast.ast -> term
wenzelm@2698
    46
end;
wenzelm@548
    47
wenzelm@2698
    48
structure SynTrans: SYN_TRANS =
wenzelm@548
    49
struct
wenzelm@2698
    50
wenzelm@2698
    51
wenzelm@548
    52
(** parse (ast) translations **)
wenzelm@548
    53
wenzelm@11491
    54
(* constify *)
wenzelm@11491
    55
wenzelm@11491
    56
fun constify_ast_tr [Ast.Variable c] = Ast.Constant c
wenzelm@11491
    57
  | constify_ast_tr asts = raise Ast.AST ("constify_ast_tr", asts);
wenzelm@11491
    58
wenzelm@11491
    59
wenzelm@548
    60
(* application *)
wenzelm@548
    61
wenzelm@5690
    62
fun appl_ast_tr [f, args] = Ast.Appl (f :: Ast.unfold_ast "_args" args)
wenzelm@5690
    63
  | appl_ast_tr asts = raise Ast.AST ("appl_ast_tr", asts);
clasohm@922
    64
wenzelm@5690
    65
fun applC_ast_tr [f, args] = Ast.Appl (f :: Ast.unfold_ast "_cargs" args)
wenzelm@5690
    66
  | applC_ast_tr asts = raise Ast.AST ("applC_ast_tr", asts);
wenzelm@548
    67
wenzelm@548
    68
wenzelm@548
    69
(* abstraction *)
wenzelm@548
    70
wenzelm@5690
    71
fun idtyp_ast_tr (*"_idtyp"*) [x, ty] = Ast.Appl [Ast.Constant SynExt.constrainC, x, ty]
wenzelm@5690
    72
  | idtyp_ast_tr (*"_idtyp"*) asts = raise Ast.AST ("idtyp_ast_tr", asts);
wenzelm@548
    73
wenzelm@3691
    74
fun lambda_ast_tr (*"_lambda"*) [pats, body] =
wenzelm@5690
    75
      Ast.fold_ast_p "_abs" (Ast.unfold_ast "_pttrns" pats, body)
wenzelm@5690
    76
  | lambda_ast_tr (*"_lambda"*) asts = raise Ast.AST ("lambda_ast_tr", asts);
wenzelm@548
    77
wenzelm@548
    78
val constrainAbsC = "_constrainAbs";
wenzelm@548
    79
wenzelm@5690
    80
fun abs_tr (*"_abs"*) [Free (x, T), body] = Term.absfree (x, T, body)
wenzelm@548
    81
  | abs_tr (*"_abs"*) (ts as [Const (c, _) $ Free (x, T) $ tT, body]) =
wenzelm@5690
    82
      if c = SynExt.constrainC
wenzelm@5690
    83
        then Lexicon.const constrainAbsC $ Term.absfree (x, T, body) $ tT
wenzelm@3777
    84
      else raise TERM ("abs_tr", ts)
wenzelm@3777
    85
  | abs_tr (*"_abs"*) ts = raise TERM ("abs_tr", ts);
wenzelm@548
    86
wenzelm@548
    87
wenzelm@548
    88
(* nondependent abstraction *)
wenzelm@548
    89
wenzelm@5690
    90
fun k_tr (*"_K"*) [t] = Abs ("uu", dummyT, Term.incr_boundvars 1 t)
wenzelm@3777
    91
  | k_tr (*"_K"*) ts = raise TERM ("k_tr", ts);
wenzelm@548
    92
wenzelm@548
    93
wenzelm@548
    94
(* binder *)
wenzelm@548
    95
wenzelm@548
    96
fun mk_binder_tr (sy, name) =
wenzelm@548
    97
  let
wenzelm@5690
    98
    fun tr (Free (x, T), t) = Lexicon.const name $ Term.absfree (x, T, t)
wenzelm@548
    99
      | tr (Const ("_idts", _) $ idt $ idts, t) = tr (idt, tr (idts, t))
wenzelm@548
   100
      | tr (t1 as Const (c, _) $ Free (x, T) $ tT, t) =
wenzelm@5690
   101
          if c = SynExt.constrainC then
wenzelm@5690
   102
            Lexicon.const name $ (Lexicon.const constrainAbsC $ Term.absfree (x, T, t) $ tT)
wenzelm@3777
   103
          else raise TERM ("binder_tr", [t1, t])
wenzelm@3777
   104
      | tr (t1, t2) = raise TERM ("binder_tr", [t1, t2]);
wenzelm@548
   105
wenzelm@548
   106
    fun binder_tr (*sy*) [idts, body] = tr (idts, body)
wenzelm@3777
   107
      | binder_tr (*sy*) ts = raise TERM ("binder_tr", ts);
wenzelm@548
   108
  in
wenzelm@548
   109
    (sy, binder_tr)
wenzelm@548
   110
  end;
wenzelm@548
   111
wenzelm@548
   112
wenzelm@548
   113
(* meta propositions *)
wenzelm@548
   114
wenzelm@5690
   115
fun aprop_tr (*"_aprop"*) [t] = Lexicon.const SynExt.constrainC $ t $ Lexicon.const "prop"
wenzelm@3777
   116
  | aprop_tr (*"_aprop"*) ts = raise TERM ("aprop_tr", ts);
wenzelm@548
   117
wenzelm@548
   118
fun ofclass_tr (*"_ofclass"*) [ty, cls] =
wenzelm@5690
   119
      cls $ (Lexicon.const SynExt.constrainC $ Lexicon.const "TYPE" $
wenzelm@5690
   120
        (Lexicon.const "itself" $ ty))
wenzelm@3777
   121
  | ofclass_tr (*"_ofclass"*) ts = raise TERM ("ofclass_tr", ts);
wenzelm@548
   122
wenzelm@548
   123
wenzelm@548
   124
(* meta implication *)
wenzelm@548
   125
wenzelm@548
   126
fun bigimpl_ast_tr (*"_bigimpl"*) [asms, concl] =
wenzelm@5690
   127
      Ast.fold_ast_p "==>" (Ast.unfold_ast "_asms" asms, concl)
wenzelm@5690
   128
  | bigimpl_ast_tr (*"_bigimpl"*) asts = raise Ast.AST ("bigimpl_ast_tr", asts);
wenzelm@548
   129
wenzelm@548
   130
wenzelm@4148
   131
(* type reflection *)
wenzelm@4148
   132
wenzelm@4148
   133
fun type_tr (*"_TYPE"*) [ty] =
wenzelm@5690
   134
      Lexicon.const SynExt.constrainC $ Lexicon.const "TYPE" $ (Lexicon.const "itself" $ ty)
wenzelm@4148
   135
  | type_tr (*"_TYPE"*) ts = raise TERM ("type_tr", ts);
wenzelm@4148
   136
wenzelm@548
   137
wenzelm@6761
   138
(* dddot *)
wenzelm@6761
   139
wenzelm@8595
   140
fun dddot_tr (*"_DDDOT"*) ts = Term.list_comb (Lexicon.var SynExt.dddot_indexname, ts);
wenzelm@6761
   141
wenzelm@6761
   142
wenzelm@5084
   143
(* quote / antiquote *)
wenzelm@5084
   144
wenzelm@8577
   145
fun antiquote_tr name =
wenzelm@8577
   146
  let
wenzelm@8577
   147
    fun tr i ((t as Const (c, _)) $ u) =
wenzelm@8577
   148
          if c = name then tr i u $ Bound i
wenzelm@8577
   149
          else tr i t $ tr i u
wenzelm@8577
   150
      | tr i (t $ u) = tr i t $ tr i u
wenzelm@8577
   151
      | tr i (Abs (x, T, t)) = Abs (x, T, tr (i + 1) t)
wenzelm@8577
   152
      | tr _ a = a;
wenzelm@8577
   153
  in tr 0 end;
wenzelm@8577
   154
wenzelm@8577
   155
fun quote_tr name t = Abs ("s", dummyT, antiquote_tr name (Term.incr_boundvars 1 t));
wenzelm@8577
   156
wenzelm@5084
   157
fun quote_antiquote_tr quoteN antiquoteN name =
wenzelm@5084
   158
  let
wenzelm@8577
   159
    fun tr [t] = Lexicon.const name $ quote_tr antiquoteN t
wenzelm@8577
   160
      | tr ts = raise TERM ("quote_tr", ts);
wenzelm@8577
   161
  in (quoteN, tr) end;
wenzelm@5084
   162
wenzelm@5084
   163
wenzelm@12122
   164
(* index variable *)
wenzelm@12122
   165
wenzelm@12122
   166
fun indexvar_ast_tr [] = Ast.Variable "some_index"
wenzelm@12122
   167
  | indexvar_ast_tr asts = raise Ast.AST ("indexvar_ast_tr", asts);
wenzelm@12122
   168
wenzelm@12122
   169
wenzelm@5084
   170
wenzelm@548
   171
(** print (ast) translations **)
wenzelm@548
   172
wenzelm@548
   173
(* application *)
wenzelm@548
   174
wenzelm@5690
   175
fun appl_ast_tr' (f, []) = raise Ast.AST ("appl_ast_tr'", [f])
wenzelm@5690
   176
  | appl_ast_tr' (f, args) = Ast.Appl [Ast.Constant "_appl", f, Ast.fold_ast "_args" args];
wenzelm@548
   177
wenzelm@5690
   178
fun applC_ast_tr' (f, []) = raise Ast.AST ("applC_ast_tr'", [f])
wenzelm@5690
   179
  | applC_ast_tr' (f, args) = Ast.Appl [Ast.Constant "_applC", f, Ast.fold_ast "_cargs" args];
clasohm@922
   180
wenzelm@548
   181
wenzelm@548
   182
(* abstraction *)
wenzelm@548
   183
wenzelm@5690
   184
fun mark_boundT x_T = Lexicon.const "_bound" $ Free x_T;
wenzelm@2698
   185
fun mark_bound x = mark_boundT (x, dummyT);
wenzelm@2698
   186
wenzelm@548
   187
fun strip_abss vars_of body_of tm =
wenzelm@548
   188
  let
wenzelm@548
   189
    val vars = vars_of tm;
wenzelm@548
   190
    val body = body_of tm;
wenzelm@548
   191
    val rev_new_vars = rename_wrt_term body vars;
wenzelm@548
   192
  in
wenzelm@2698
   193
    (map mark_boundT (rev rev_new_vars),
wenzelm@2698
   194
      subst_bounds (map (mark_bound o #1) rev_new_vars, body))
wenzelm@548
   195
  end;
wenzelm@548
   196
wenzelm@3928
   197
wenzelm@548
   198
(*do (partial) eta-contraction before printing*)
wenzelm@548
   199
nipkow@1326
   200
val eta_contract = ref true;
wenzelm@548
   201
wenzelm@548
   202
fun eta_contr tm =
wenzelm@548
   203
  let
wenzelm@3928
   204
    fun is_aprop (Const ("_aprop", _)) = true
wenzelm@3928
   205
      | is_aprop _ = false;
wenzelm@3928
   206
wenzelm@548
   207
    fun eta_abs (Abs (a, T, t)) =
wenzelm@548
   208
          (case eta_abs t of
wenzelm@548
   209
            t' as f $ u =>
wenzelm@548
   210
              (case eta_abs u of
wenzelm@548
   211
                Bound 0 =>
wenzelm@5084
   212
                  if Term.loose_bvar1 (f, 0) orelse is_aprop f then Abs (a, T, t')
wenzelm@3928
   213
                  else  incr_boundvars ~1 f
wenzelm@548
   214
              | _ => Abs (a, T, t'))
wenzelm@548
   215
          | t' => Abs (a, T, t'))
wenzelm@548
   216
      | eta_abs t = t;
wenzelm@548
   217
  in
wenzelm@548
   218
    if ! eta_contract then eta_abs tm else tm
wenzelm@548
   219
  end;
wenzelm@548
   220
wenzelm@548
   221
wenzelm@548
   222
fun abs_tr' tm =
wenzelm@5690
   223
  foldr (fn (x, t) => Lexicon.const "_abs" $ x $ t)
wenzelm@548
   224
    (strip_abss strip_abs_vars strip_abs_body (eta_contr tm));
wenzelm@548
   225
wenzelm@548
   226
wenzelm@548
   227
fun abs_ast_tr' (*"_abs"*) asts =
wenzelm@5690
   228
  (case Ast.unfold_ast_p "_abs" (Ast.Appl (Ast.Constant "_abs" :: asts)) of
wenzelm@5690
   229
    ([], _) => raise Ast.AST ("abs_ast_tr'", asts)
wenzelm@5690
   230
  | (xs, body) => Ast.Appl [Ast.Constant "_lambda", Ast.fold_ast "_pttrns" xs, body]);
wenzelm@548
   231
wenzelm@548
   232
wenzelm@548
   233
(* binder *)
wenzelm@548
   234
wenzelm@548
   235
fun mk_binder_tr' (name, sy) =
wenzelm@548
   236
  let
wenzelm@548
   237
    fun mk_idts [] = raise Match    (*abort translation*)
wenzelm@548
   238
      | mk_idts [idt] = idt
wenzelm@5690
   239
      | mk_idts (idt :: idts) = Lexicon.const "_idts" $ idt $ mk_idts idts;
wenzelm@548
   240
wenzelm@548
   241
    fun tr' t =
wenzelm@548
   242
      let
wenzelm@548
   243
        val (xs, bd) = strip_abss (strip_qnt_vars name) (strip_qnt_body name) t;
wenzelm@5690
   244
      in Lexicon.const sy $ mk_idts xs $ bd end;
wenzelm@548
   245
wenzelm@5690
   246
    fun binder_tr' (*name*) (t :: ts) = Term.list_comb (tr' (Lexicon.const name $ t), ts)
wenzelm@548
   247
      | binder_tr' (*name*) [] = raise Match;
wenzelm@548
   248
  in
wenzelm@548
   249
    (name, binder_tr')
wenzelm@548
   250
  end;
wenzelm@548
   251
wenzelm@548
   252
wenzelm@3691
   253
(* idtyp constraints *)
wenzelm@548
   254
wenzelm@5690
   255
fun idtyp_ast_tr' a [Ast.Appl [Ast.Constant c, x, ty], xs] =
wenzelm@5690
   256
      if c = SynExt.constrainC then
wenzelm@5690
   257
        Ast.Appl [ Ast.Constant a,  Ast.Appl [Ast.Constant "_idtyp", x, ty], xs]
wenzelm@548
   258
      else raise Match
wenzelm@3691
   259
  | idtyp_ast_tr' _ _ = raise Match;
wenzelm@548
   260
wenzelm@548
   261
wenzelm@548
   262
(* meta propositions *)
wenzelm@548
   263
wenzelm@4148
   264
fun prop_tr' tm =
wenzelm@548
   265
  let
wenzelm@5690
   266
    fun aprop t = Lexicon.const "_aprop" $ t;
wenzelm@548
   267
wenzelm@2698
   268
    fun is_prop Ts t =
wenzelm@2698
   269
      fastype_of1 (Ts, t) = propT handle TERM _ => false;
wenzelm@548
   270
wenzelm@548
   271
    fun tr' _ (t as Const _) = t
wenzelm@2698
   272
      | tr' _ (t as Free (x, T)) =
wenzelm@5690
   273
          if T = propT then aprop (Lexicon.free x) else t
wenzelm@2698
   274
      | tr' _ (t as Var (xi, T)) =
wenzelm@5690
   275
          if T = propT then aprop (Lexicon.var xi) else t
wenzelm@2698
   276
      | tr' Ts (t as Bound _) =
wenzelm@2698
   277
          if is_prop Ts t then aprop t else t
wenzelm@2698
   278
      | tr' Ts (Abs (x, T, t)) = Abs (x, T, tr' (T :: Ts) t)
wenzelm@2698
   279
      | tr' Ts (t as t1 $ (t2 as Const ("TYPE", Type ("itself", [T])))) =
wenzelm@4148
   280
          if is_prop Ts t then Const ("_mk_ofclass", T) $ tr' Ts t1
wenzelm@2698
   281
          else tr' Ts t1 $ tr' Ts t2
wenzelm@2698
   282
      | tr' Ts (t as t1 $ t2) =
wenzelm@5690
   283
          (if is_Const (Term.head_of t) orelse not (is_prop Ts t)
wenzelm@2698
   284
            then I else aprop) (tr' Ts t1 $ tr' Ts t2);
wenzelm@548
   285
  in
wenzelm@548
   286
    tr' [] tm
wenzelm@548
   287
  end;
wenzelm@548
   288
wenzelm@4148
   289
fun mk_ofclass_tr' show_sorts (*"_mk_ofclass"*) T [t] =
wenzelm@5690
   290
      Lexicon.const "_ofclass" $ TypeExt.term_of_typ show_sorts T $ t
wenzelm@4148
   291
  | mk_ofclass_tr' _ (*"_mk_ofclass"*) T ts = raise TYPE ("mk_ofclass_tr'", [T], ts);
wenzelm@2698
   292
wenzelm@2698
   293
wenzelm@548
   294
(* meta implication *)
wenzelm@548
   295
wenzelm@548
   296
fun impl_ast_tr' (*"==>"*) asts =
wenzelm@10572
   297
  if TypeExt.no_brackets () then raise Match
wenzelm@10572
   298
  else
wenzelm@10572
   299
    (case Ast.unfold_ast_p "==>" (Ast.Appl (Ast.Constant "==>" :: asts)) of
wenzelm@10572
   300
      (asms as _ :: _ :: _, concl)
wenzelm@10572
   301
        => Ast.Appl [Ast.Constant "_bigimpl", Ast.fold_ast "_asms" asms, concl]
wenzelm@10572
   302
    | _ => raise Match);
wenzelm@548
   303
wenzelm@548
   304
wenzelm@12150
   305
(* meta conjunction *)
wenzelm@12150
   306
wenzelm@12150
   307
fun meta_conjunction_tr' (*"all"*)
wenzelm@12150
   308
      [Abs (_, _, Const ("==>", _) $
wenzelm@12150
   309
        (Const ("==>", _) $ A $ (Const ("==>", _) $ B $ (Const ("_aprop", _) $ Bound 0))) $
wenzelm@12150
   310
        (Const ("_aprop", _) $ Bound 0))] =
wenzelm@12150
   311
      if 0 mem_int Term.loose_bnos A orelse 0 mem_int Term.loose_bnos B then raise Match
wenzelm@12150
   312
      else Lexicon.const "_meta_conjunction" $ A $ B
wenzelm@12150
   313
  | meta_conjunction_tr' (*"all"*) ts = raise Match;
wenzelm@12150
   314
wenzelm@12150
   315
wenzelm@4148
   316
(* type reflection *)
wenzelm@4148
   317
wenzelm@4148
   318
fun type_tr' show_sorts (*"TYPE"*) (Type ("itself", [T])) ts =
wenzelm@5690
   319
      Term.list_comb (Lexicon.const "_TYPE" $ TypeExt.term_of_typ show_sorts T, ts)
wenzelm@4148
   320
  | type_tr' _ _ _ = raise Match;
wenzelm@4148
   321
wenzelm@4148
   322
wenzelm@548
   323
(* dependent / nondependent quantifiers *)
wenzelm@548
   324
wenzelm@2698
   325
fun variant_abs' (x, T, B) =
wenzelm@2698
   326
  let val x' = variant (add_term_names (B, [])) x in
wenzelm@2698
   327
    (x', subst_bound (mark_boundT (x', T), B))
wenzelm@2698
   328
  end;
wenzelm@2698
   329
wenzelm@548
   330
fun dependent_tr' (q, r) (A :: Abs (x, T, B) :: ts) =
wenzelm@5084
   331
      if Term.loose_bvar1 (B, 0) then
wenzelm@2698
   332
        let val (x', B') = variant_abs' (x, dummyT, B);
wenzelm@5690
   333
        in Term.list_comb (Lexicon.const q $ mark_boundT (x', T) $ A $ B', ts) end
wenzelm@5690
   334
      else Term.list_comb (Lexicon.const r $ A $ B, ts)
wenzelm@548
   335
  | dependent_tr' _ _ = raise Match;
wenzelm@548
   336
wenzelm@548
   337
wenzelm@5084
   338
(* quote / antiquote *)
wenzelm@5084
   339
wenzelm@8577
   340
fun antiquote_tr' name =
wenzelm@8577
   341
  let
wenzelm@8577
   342
    fun tr' i (t $ u) =
wenzelm@8577
   343
      if u = Bound i then Lexicon.const name $ tr' i t
wenzelm@8577
   344
      else tr' i t $ tr' i u
wenzelm@8577
   345
      | tr' i (Abs (x, T, t)) = Abs (x, T, tr' (i + 1) t)
wenzelm@8577
   346
      | tr' i a = if a = Bound i then raise Match else a;
wenzelm@8577
   347
  in tr' 0 end;
wenzelm@8577
   348
wenzelm@8577
   349
fun quote_tr' name (Abs (_, _, t)) = Term.incr_boundvars ~1 (antiquote_tr' name t)
wenzelm@8577
   350
  | quote_tr' _ _ = raise Match;
wenzelm@8577
   351
wenzelm@5084
   352
fun quote_antiquote_tr' quoteN antiquoteN name =
wenzelm@5084
   353
  let
wenzelm@8577
   354
    fun tr' (t :: ts) = Term.list_comb (Lexicon.const quoteN $ quote_tr' antiquoteN t, ts)
wenzelm@8577
   355
      | tr' _ = raise Match;
wenzelm@8577
   356
  in (name, tr') end;
wenzelm@5084
   357
wenzelm@5084
   358
wenzelm@548
   359
wenzelm@548
   360
(** pure_trfuns **)
wenzelm@548
   361
wenzelm@548
   362
val pure_trfuns =
wenzelm@11491
   363
 ([("_constify", constify_ast_tr), ("_appl", appl_ast_tr), ("_applC", applC_ast_tr),
wenzelm@12122
   364
   ("_lambda", lambda_ast_tr), ("_idtyp", idtyp_ast_tr), ("_bigimpl", bigimpl_ast_tr),
wenzelm@12122
   365
   ("_indexvar", indexvar_ast_tr)],
clasohm@922
   366
  [("_abs", abs_tr), ("_aprop", aprop_tr), ("_ofclass", ofclass_tr),
wenzelm@6761
   367
   ("_TYPE", type_tr), ("_DDDOT", dddot_tr), ("_K", k_tr)],
wenzelm@12150
   368
  [("all", meta_conjunction_tr')],
wenzelm@3691
   369
  [("_abs", abs_ast_tr'), ("_idts", idtyp_ast_tr' "_idts"),
wenzelm@3691
   370
   ("_pttrns", idtyp_ast_tr' "_pttrns"), ("==>", impl_ast_tr')]);
wenzelm@548
   371
wenzelm@2698
   372
val pure_trfunsT =
wenzelm@4148
   373
  [("_mk_ofclass", mk_ofclass_tr'), ("TYPE", type_tr')];
wenzelm@2698
   374
wenzelm@548
   375
wenzelm@548
   376
wenzelm@548
   377
(** pt_to_ast **)
wenzelm@548
   378
wenzelm@548
   379
fun pt_to_ast trf pt =
wenzelm@548
   380
  let
wenzelm@548
   381
    fun trans a args =
wenzelm@548
   382
      (case trf a of
wenzelm@5690
   383
        None => Ast.mk_appl (Ast.Constant a) args
wenzelm@548
   384
      | Some f => f args handle exn
clasohm@987
   385
          => (writeln ("Error in parse ast translation for " ^ quote a);
clasohm@987
   386
              raise exn));
wenzelm@548
   387
clasohm@987
   388
    (*translate pt bottom-up*)
wenzelm@5690
   389
    fun ast_of (Parser.Node (a, pts)) = trans a (map ast_of pts)
wenzelm@5690
   390
      | ast_of (Parser.Tip tok) = Ast.Variable (Lexicon.str_of_token tok);
wenzelm@548
   391
  in
clasohm@1178
   392
    ast_of pt
wenzelm@548
   393
  end;
wenzelm@548
   394
wenzelm@548
   395
wenzelm@548
   396
wenzelm@548
   397
(** ast_to_term **)
wenzelm@548
   398
wenzelm@548
   399
fun ast_to_term trf ast =
wenzelm@548
   400
  let
wenzelm@548
   401
    fun trans a args =
wenzelm@548
   402
      (case trf a of
wenzelm@5690
   403
        None => Term.list_comb (Lexicon.const a, args)
wenzelm@548
   404
      | Some f => f args handle exn
clasohm@987
   405
          => (writeln ("Error in parse translation for " ^ quote a);
clasohm@987
   406
              raise exn));
wenzelm@548
   407
wenzelm@5690
   408
    fun term_of (Ast.Constant a) = trans a []
wenzelm@5690
   409
      | term_of (Ast.Variable x) = Lexicon.read_var x
wenzelm@5690
   410
      | term_of (Ast.Appl (Ast.Constant a :: (asts as _ :: _))) =
wenzelm@548
   411
          trans a (map term_of asts)
wenzelm@5690
   412
      | term_of (Ast.Appl (ast :: (asts as _ :: _))) =
wenzelm@5690
   413
          Term.list_comb (term_of ast, map term_of asts)
wenzelm@5690
   414
      | term_of (ast as Ast.Appl _) = raise Ast.AST ("ast_to_term: malformed ast", [ast]);
wenzelm@548
   415
  in
wenzelm@548
   416
    term_of ast
wenzelm@548
   417
  end;
wenzelm@548
   418
wenzelm@548
   419
end;