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