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