src/Pure/Syntax/syn_trans.ML
author wenzelm
Wed Aug 08 17:39:32 2001 +0200 (2001-08-08)
changeset 11491 085a0d2857e8
parent 10572 b070825579b8
child 12122 7f8d88ed4f21
permissions -rw-r--r--
added constify_ast_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@5084
   164
wenzelm@548
   165
(** print (ast) translations **)
wenzelm@548
   166
wenzelm@548
   167
(* application *)
wenzelm@548
   168
wenzelm@5690
   169
fun appl_ast_tr' (f, []) = raise Ast.AST ("appl_ast_tr'", [f])
wenzelm@5690
   170
  | appl_ast_tr' (f, args) = Ast.Appl [Ast.Constant "_appl", f, Ast.fold_ast "_args" args];
wenzelm@548
   171
wenzelm@5690
   172
fun applC_ast_tr' (f, []) = raise Ast.AST ("applC_ast_tr'", [f])
wenzelm@5690
   173
  | applC_ast_tr' (f, args) = Ast.Appl [Ast.Constant "_applC", f, Ast.fold_ast "_cargs" args];
clasohm@922
   174
wenzelm@548
   175
wenzelm@548
   176
(* abstraction *)
wenzelm@548
   177
wenzelm@5690
   178
fun mark_boundT x_T = Lexicon.const "_bound" $ Free x_T;
wenzelm@2698
   179
fun mark_bound x = mark_boundT (x, dummyT);
wenzelm@2698
   180
wenzelm@548
   181
fun strip_abss vars_of body_of tm =
wenzelm@548
   182
  let
wenzelm@548
   183
    val vars = vars_of tm;
wenzelm@548
   184
    val body = body_of tm;
wenzelm@548
   185
    val rev_new_vars = rename_wrt_term body vars;
wenzelm@548
   186
  in
wenzelm@2698
   187
    (map mark_boundT (rev rev_new_vars),
wenzelm@2698
   188
      subst_bounds (map (mark_bound o #1) rev_new_vars, body))
wenzelm@548
   189
  end;
wenzelm@548
   190
wenzelm@3928
   191
wenzelm@548
   192
(*do (partial) eta-contraction before printing*)
wenzelm@548
   193
nipkow@1326
   194
val eta_contract = ref true;
wenzelm@548
   195
wenzelm@548
   196
fun eta_contr tm =
wenzelm@548
   197
  let
wenzelm@3928
   198
    fun is_aprop (Const ("_aprop", _)) = true
wenzelm@3928
   199
      | is_aprop _ = false;
wenzelm@3928
   200
wenzelm@548
   201
    fun eta_abs (Abs (a, T, t)) =
wenzelm@548
   202
          (case eta_abs t of
wenzelm@548
   203
            t' as f $ u =>
wenzelm@548
   204
              (case eta_abs u of
wenzelm@548
   205
                Bound 0 =>
wenzelm@5084
   206
                  if Term.loose_bvar1 (f, 0) orelse is_aprop f then Abs (a, T, t')
wenzelm@3928
   207
                  else  incr_boundvars ~1 f
wenzelm@548
   208
              | _ => Abs (a, T, t'))
wenzelm@548
   209
          | t' => Abs (a, T, t'))
wenzelm@548
   210
      | eta_abs t = t;
wenzelm@548
   211
  in
wenzelm@548
   212
    if ! eta_contract then eta_abs tm else tm
wenzelm@548
   213
  end;
wenzelm@548
   214
wenzelm@548
   215
wenzelm@548
   216
fun abs_tr' tm =
wenzelm@5690
   217
  foldr (fn (x, t) => Lexicon.const "_abs" $ x $ t)
wenzelm@548
   218
    (strip_abss strip_abs_vars strip_abs_body (eta_contr tm));
wenzelm@548
   219
wenzelm@548
   220
wenzelm@548
   221
fun abs_ast_tr' (*"_abs"*) asts =
wenzelm@5690
   222
  (case Ast.unfold_ast_p "_abs" (Ast.Appl (Ast.Constant "_abs" :: asts)) of
wenzelm@5690
   223
    ([], _) => raise Ast.AST ("abs_ast_tr'", asts)
wenzelm@5690
   224
  | (xs, body) => Ast.Appl [Ast.Constant "_lambda", Ast.fold_ast "_pttrns" xs, body]);
wenzelm@548
   225
wenzelm@548
   226
wenzelm@548
   227
(* binder *)
wenzelm@548
   228
wenzelm@548
   229
fun mk_binder_tr' (name, sy) =
wenzelm@548
   230
  let
wenzelm@548
   231
    fun mk_idts [] = raise Match    (*abort translation*)
wenzelm@548
   232
      | mk_idts [idt] = idt
wenzelm@5690
   233
      | mk_idts (idt :: idts) = Lexicon.const "_idts" $ idt $ mk_idts idts;
wenzelm@548
   234
wenzelm@548
   235
    fun tr' t =
wenzelm@548
   236
      let
wenzelm@548
   237
        val (xs, bd) = strip_abss (strip_qnt_vars name) (strip_qnt_body name) t;
wenzelm@5690
   238
      in Lexicon.const sy $ mk_idts xs $ bd end;
wenzelm@548
   239
wenzelm@5690
   240
    fun binder_tr' (*name*) (t :: ts) = Term.list_comb (tr' (Lexicon.const name $ t), ts)
wenzelm@548
   241
      | binder_tr' (*name*) [] = raise Match;
wenzelm@548
   242
  in
wenzelm@548
   243
    (name, binder_tr')
wenzelm@548
   244
  end;
wenzelm@548
   245
wenzelm@548
   246
wenzelm@3691
   247
(* idtyp constraints *)
wenzelm@548
   248
wenzelm@5690
   249
fun idtyp_ast_tr' a [Ast.Appl [Ast.Constant c, x, ty], xs] =
wenzelm@5690
   250
      if c = SynExt.constrainC then
wenzelm@5690
   251
        Ast.Appl [ Ast.Constant a,  Ast.Appl [Ast.Constant "_idtyp", x, ty], xs]
wenzelm@548
   252
      else raise Match
wenzelm@3691
   253
  | idtyp_ast_tr' _ _ = raise Match;
wenzelm@548
   254
wenzelm@548
   255
wenzelm@548
   256
(* meta propositions *)
wenzelm@548
   257
wenzelm@4148
   258
fun prop_tr' tm =
wenzelm@548
   259
  let
wenzelm@5690
   260
    fun aprop t = Lexicon.const "_aprop" $ t;
wenzelm@548
   261
wenzelm@2698
   262
    fun is_prop Ts t =
wenzelm@2698
   263
      fastype_of1 (Ts, t) = propT handle TERM _ => false;
wenzelm@548
   264
wenzelm@548
   265
    fun tr' _ (t as Const _) = t
wenzelm@2698
   266
      | tr' _ (t as Free (x, T)) =
wenzelm@5690
   267
          if T = propT then aprop (Lexicon.free x) else t
wenzelm@2698
   268
      | tr' _ (t as Var (xi, T)) =
wenzelm@5690
   269
          if T = propT then aprop (Lexicon.var xi) else t
wenzelm@2698
   270
      | tr' Ts (t as Bound _) =
wenzelm@2698
   271
          if is_prop Ts t then aprop t else t
wenzelm@2698
   272
      | tr' Ts (Abs (x, T, t)) = Abs (x, T, tr' (T :: Ts) t)
wenzelm@2698
   273
      | tr' Ts (t as t1 $ (t2 as Const ("TYPE", Type ("itself", [T])))) =
wenzelm@4148
   274
          if is_prop Ts t then Const ("_mk_ofclass", T) $ tr' Ts t1
wenzelm@2698
   275
          else tr' Ts t1 $ tr' Ts t2
wenzelm@2698
   276
      | tr' Ts (t as t1 $ t2) =
wenzelm@5690
   277
          (if is_Const (Term.head_of t) orelse not (is_prop Ts t)
wenzelm@2698
   278
            then I else aprop) (tr' Ts t1 $ tr' Ts t2);
wenzelm@548
   279
  in
wenzelm@548
   280
    tr' [] tm
wenzelm@548
   281
  end;
wenzelm@548
   282
wenzelm@4148
   283
fun mk_ofclass_tr' show_sorts (*"_mk_ofclass"*) T [t] =
wenzelm@5690
   284
      Lexicon.const "_ofclass" $ TypeExt.term_of_typ show_sorts T $ t
wenzelm@4148
   285
  | mk_ofclass_tr' _ (*"_mk_ofclass"*) T ts = raise TYPE ("mk_ofclass_tr'", [T], ts);
wenzelm@2698
   286
wenzelm@2698
   287
wenzelm@548
   288
(* meta implication *)
wenzelm@548
   289
wenzelm@548
   290
fun impl_ast_tr' (*"==>"*) asts =
wenzelm@10572
   291
  if TypeExt.no_brackets () then raise Match
wenzelm@10572
   292
  else
wenzelm@10572
   293
    (case Ast.unfold_ast_p "==>" (Ast.Appl (Ast.Constant "==>" :: asts)) of
wenzelm@10572
   294
      (asms as _ :: _ :: _, concl)
wenzelm@10572
   295
        => Ast.Appl [Ast.Constant "_bigimpl", Ast.fold_ast "_asms" asms, concl]
wenzelm@10572
   296
    | _ => raise Match);
wenzelm@548
   297
wenzelm@548
   298
wenzelm@4148
   299
(* type reflection *)
wenzelm@4148
   300
wenzelm@4148
   301
fun type_tr' show_sorts (*"TYPE"*) (Type ("itself", [T])) ts =
wenzelm@5690
   302
      Term.list_comb (Lexicon.const "_TYPE" $ TypeExt.term_of_typ show_sorts T, ts)
wenzelm@4148
   303
  | type_tr' _ _ _ = raise Match;
wenzelm@4148
   304
wenzelm@4148
   305
wenzelm@548
   306
(* dependent / nondependent quantifiers *)
wenzelm@548
   307
wenzelm@2698
   308
fun variant_abs' (x, T, B) =
wenzelm@2698
   309
  let val x' = variant (add_term_names (B, [])) x in
wenzelm@2698
   310
    (x', subst_bound (mark_boundT (x', T), B))
wenzelm@2698
   311
  end;
wenzelm@2698
   312
wenzelm@548
   313
fun dependent_tr' (q, r) (A :: Abs (x, T, B) :: ts) =
wenzelm@5084
   314
      if Term.loose_bvar1 (B, 0) then
wenzelm@2698
   315
        let val (x', B') = variant_abs' (x, dummyT, B);
wenzelm@5690
   316
        in Term.list_comb (Lexicon.const q $ mark_boundT (x', T) $ A $ B', ts) end
wenzelm@5690
   317
      else Term.list_comb (Lexicon.const r $ A $ B, ts)
wenzelm@548
   318
  | dependent_tr' _ _ = raise Match;
wenzelm@548
   319
wenzelm@548
   320
wenzelm@5084
   321
(* quote / antiquote *)
wenzelm@5084
   322
wenzelm@8577
   323
fun antiquote_tr' name =
wenzelm@8577
   324
  let
wenzelm@8577
   325
    fun tr' i (t $ u) =
wenzelm@8577
   326
      if u = Bound i then Lexicon.const name $ tr' i t
wenzelm@8577
   327
      else tr' i t $ tr' i u
wenzelm@8577
   328
      | tr' i (Abs (x, T, t)) = Abs (x, T, tr' (i + 1) t)
wenzelm@8577
   329
      | tr' i a = if a = Bound i then raise Match else a;
wenzelm@8577
   330
  in tr' 0 end;
wenzelm@8577
   331
wenzelm@8577
   332
fun quote_tr' name (Abs (_, _, t)) = Term.incr_boundvars ~1 (antiquote_tr' name t)
wenzelm@8577
   333
  | quote_tr' _ _ = raise Match;
wenzelm@8577
   334
wenzelm@5084
   335
fun quote_antiquote_tr' quoteN antiquoteN name =
wenzelm@5084
   336
  let
wenzelm@8577
   337
    fun tr' (t :: ts) = Term.list_comb (Lexicon.const quoteN $ quote_tr' antiquoteN t, ts)
wenzelm@8577
   338
      | tr' _ = raise Match;
wenzelm@8577
   339
  in (name, tr') end;
wenzelm@5084
   340
wenzelm@5084
   341
wenzelm@548
   342
wenzelm@548
   343
(** pure_trfuns **)
wenzelm@548
   344
wenzelm@548
   345
val pure_trfuns =
wenzelm@11491
   346
 ([("_constify", constify_ast_tr), ("_appl", appl_ast_tr), ("_applC", applC_ast_tr),
wenzelm@11491
   347
   ("_lambda", lambda_ast_tr), ("_idtyp", idtyp_ast_tr), ("_bigimpl", bigimpl_ast_tr)],
clasohm@922
   348
  [("_abs", abs_tr), ("_aprop", aprop_tr), ("_ofclass", ofclass_tr),
wenzelm@6761
   349
   ("_TYPE", type_tr), ("_DDDOT", dddot_tr), ("_K", k_tr)],
wenzelm@3700
   350
  []: (string * (term list -> term)) list,
wenzelm@3691
   351
  [("_abs", abs_ast_tr'), ("_idts", idtyp_ast_tr' "_idts"),
wenzelm@3691
   352
   ("_pttrns", idtyp_ast_tr' "_pttrns"), ("==>", impl_ast_tr')]);
wenzelm@548
   353
wenzelm@2698
   354
val pure_trfunsT =
wenzelm@4148
   355
  [("_mk_ofclass", mk_ofclass_tr'), ("TYPE", type_tr')];
wenzelm@2698
   356
wenzelm@548
   357
wenzelm@548
   358
wenzelm@548
   359
(** pt_to_ast **)
wenzelm@548
   360
wenzelm@548
   361
fun pt_to_ast trf pt =
wenzelm@548
   362
  let
wenzelm@548
   363
    fun trans a args =
wenzelm@548
   364
      (case trf a of
wenzelm@5690
   365
        None => Ast.mk_appl (Ast.Constant a) args
wenzelm@548
   366
      | Some f => f args handle exn
clasohm@987
   367
          => (writeln ("Error in parse ast translation for " ^ quote a);
clasohm@987
   368
              raise exn));
wenzelm@548
   369
clasohm@987
   370
    (*translate pt bottom-up*)
wenzelm@5690
   371
    fun ast_of (Parser.Node (a, pts)) = trans a (map ast_of pts)
wenzelm@5690
   372
      | ast_of (Parser.Tip tok) = Ast.Variable (Lexicon.str_of_token tok);
wenzelm@548
   373
  in
clasohm@1178
   374
    ast_of pt
wenzelm@548
   375
  end;
wenzelm@548
   376
wenzelm@548
   377
wenzelm@548
   378
wenzelm@548
   379
(** ast_to_term **)
wenzelm@548
   380
wenzelm@548
   381
fun ast_to_term trf ast =
wenzelm@548
   382
  let
wenzelm@548
   383
    fun trans a args =
wenzelm@548
   384
      (case trf a of
wenzelm@5690
   385
        None => Term.list_comb (Lexicon.const a, args)
wenzelm@548
   386
      | Some f => f args handle exn
clasohm@987
   387
          => (writeln ("Error in parse translation for " ^ quote a);
clasohm@987
   388
              raise exn));
wenzelm@548
   389
wenzelm@5690
   390
    fun term_of (Ast.Constant a) = trans a []
wenzelm@5690
   391
      | term_of (Ast.Variable x) = Lexicon.read_var x
wenzelm@5690
   392
      | term_of (Ast.Appl (Ast.Constant a :: (asts as _ :: _))) =
wenzelm@548
   393
          trans a (map term_of asts)
wenzelm@5690
   394
      | term_of (Ast.Appl (ast :: (asts as _ :: _))) =
wenzelm@5690
   395
          Term.list_comb (term_of ast, map term_of asts)
wenzelm@5690
   396
      | term_of (ast as Ast.Appl _) = raise Ast.AST ("ast_to_term: malformed ast", [ast]);
wenzelm@548
   397
  in
wenzelm@548
   398
    term_of ast
wenzelm@548
   399
  end;
wenzelm@548
   400
wenzelm@548
   401
end;