src/Pure/Syntax/syntax_trans.ML
author wenzelm
Fri Sep 23 14:13:15 2011 +0200 (2011-09-23)
changeset 45057 86c9b73158a8
parent 44433 9fbee4aab115
child 45389 bc0d50f8ae19
permissions -rw-r--r--
default print mode for Isabelle/Scala, not just Isabelle/jEdit;
wenzelm@42284
     1
(*  Title:      Pure/Syntax/syntax_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@42284
     7
signature BASIC_SYNTAX_TRANS =
wenzelm@42284
     8
sig
wenzelm@42284
     9
  val eta_contract: bool Config.T
wenzelm@42284
    10
end
wenzelm@42284
    11
wenzelm@42284
    12
signature SYNTAX_TRANS =
wenzelm@2698
    13
sig
wenzelm@42284
    14
  include BASIC_SYNTAX_TRANS
wenzelm@45057
    15
  val bracketsN: string
wenzelm@45057
    16
  val no_bracketsN: string
wenzelm@42284
    17
  val no_brackets: unit -> bool
wenzelm@45057
    18
  val type_bracketsN: string
wenzelm@45057
    19
  val no_type_bracketsN: string
wenzelm@42284
    20
  val no_type_brackets: unit -> bool
wenzelm@42284
    21
  val abs_tr: term list -> term
wenzelm@42284
    22
  val mk_binder_tr: string * string -> string * (term list -> term)
wenzelm@42284
    23
  val antiquote_tr: string -> term -> term
wenzelm@42284
    24
  val quote_tr: string -> term -> term
wenzelm@42284
    25
  val quote_antiquote_tr: string -> string -> string -> string * (term list -> term)
wenzelm@42284
    26
  val non_typed_tr': (term list -> term) -> typ -> term list -> term
wenzelm@42284
    27
  val non_typed_tr'': ('a -> term list -> term) -> 'a -> typ -> term list -> term
wenzelm@42284
    28
  val tappl_ast_tr': Ast.ast * Ast.ast list -> Ast.ast
wenzelm@42284
    29
  val appl_ast_tr': Ast.ast * Ast.ast list -> Ast.ast
wenzelm@42284
    30
  val applC_ast_tr': Ast.ast * Ast.ast list -> Ast.ast
wenzelm@39128
    31
  val eta_contract_default: bool Unsynchronized.ref
wenzelm@39163
    32
  val eta_contract_raw: Config.raw
wenzelm@42284
    33
  val mark_bound: string -> term
wenzelm@42284
    34
  val mark_boundT: string * typ -> term
wenzelm@42284
    35
  val bound_vars: (string * typ) list -> term -> term
wenzelm@42284
    36
  val abs_tr': Proof.context -> term -> term
nipkow@13762
    37
  val atomic_abs_tr': string * typ * term -> term * term
wenzelm@42086
    38
  val const_abs_tr': term -> term
wenzelm@42085
    39
  val mk_binder_tr': string * string -> string * (term list -> term)
haftmann@32120
    40
  val preserve_binder_abs_tr': string -> string -> string * (term list -> term)
haftmann@32120
    41
  val preserve_binder_abs2_tr': string -> string -> string * (term list -> term)
wenzelm@42284
    42
  val prop_tr': term -> term
wenzelm@42284
    43
  val variant_abs: string * typ * term -> string * term
wenzelm@42284
    44
  val variant_abs': string * typ * term -> string * term
wenzelm@548
    45
  val dependent_tr': string * string -> term list -> term
wenzelm@8577
    46
  val antiquote_tr': string -> term -> term
wenzelm@8577
    47
  val quote_tr': string -> term -> term
wenzelm@5084
    48
  val quote_antiquote_tr': string -> string -> string -> string * (term list -> term)
wenzelm@35145
    49
  val update_name_tr': term -> term
paulson@1511
    50
  val pure_trfuns:
wenzelm@35429
    51
    (string * (Ast.ast list -> Ast.ast)) list *
wenzelm@35429
    52
    (string * (term list -> term)) list *
wenzelm@35429
    53
    (string * (term list -> term)) list *
wenzelm@35429
    54
    (string * (Ast.ast list -> Ast.ast)) list
wenzelm@14697
    55
  val struct_trfuns: string list ->
wenzelm@35429
    56
    (string * (Ast.ast list -> Ast.ast)) list *
wenzelm@35429
    57
    (string * (term list -> term)) list *
wenzelm@42247
    58
    (string * (typ -> term list -> term)) list *
wenzelm@35429
    59
    (string * (Ast.ast list -> Ast.ast)) list
wenzelm@2698
    60
end;
wenzelm@548
    61
wenzelm@42284
    62
structure Syntax_Trans: SYNTAX_TRANS =
wenzelm@548
    63
struct
wenzelm@2698
    64
wenzelm@42476
    65
structure Syntax = Lexicon.Syntax;
wenzelm@42476
    66
wenzelm@42476
    67
wenzelm@42262
    68
(* print mode *)
wenzelm@42262
    69
wenzelm@42262
    70
val bracketsN = "brackets";
wenzelm@42262
    71
val no_bracketsN = "no_brackets";
wenzelm@42262
    72
wenzelm@42262
    73
fun no_brackets () =
wenzelm@42262
    74
  find_first (fn mode => mode = bracketsN orelse mode = no_bracketsN)
wenzelm@42262
    75
    (print_mode_value ()) = SOME no_bracketsN;
wenzelm@42262
    76
wenzelm@42262
    77
val type_bracketsN = "type_brackets";
wenzelm@42262
    78
val no_type_bracketsN = "no_type_brackets";
wenzelm@42262
    79
wenzelm@42262
    80
fun no_type_brackets () =
wenzelm@42262
    81
  find_first (fn mode => mode = type_bracketsN orelse mode = no_type_bracketsN)
wenzelm@42262
    82
    (print_mode_value ()) <> SOME type_bracketsN;
wenzelm@42262
    83
wenzelm@42262
    84
wenzelm@2698
    85
wenzelm@548
    86
(** parse (ast) translations **)
wenzelm@548
    87
wenzelm@42057
    88
(* strip_positions *)
wenzelm@42057
    89
wenzelm@42264
    90
fun strip_positions_ast_tr [ast] = Ast.strip_positions ast
wenzelm@42057
    91
  | strip_positions_ast_tr asts = raise Ast.AST ("strip_positions_ast_tr", asts);
wenzelm@42057
    92
wenzelm@42057
    93
wenzelm@11491
    94
(* constify *)
wenzelm@11491
    95
wenzelm@11491
    96
fun constify_ast_tr [Ast.Variable c] = Ast.Constant c
wenzelm@11491
    97
  | constify_ast_tr asts = raise Ast.AST ("constify_ast_tr", asts);
wenzelm@11491
    98
wenzelm@11491
    99
wenzelm@42262
   100
(* type syntax *)
wenzelm@42262
   101
wenzelm@42262
   102
fun tapp_ast_tr [ty, c] = Ast.Appl [c, ty]
wenzelm@42262
   103
  | tapp_ast_tr asts = raise Ast.AST ("tapp_ast_tr", asts);
wenzelm@42262
   104
wenzelm@42262
   105
fun tappl_ast_tr [ty, tys, c] = Ast.mk_appl c (ty :: Ast.unfold_ast "_types" tys)
wenzelm@42262
   106
  | tappl_ast_tr asts = raise Ast.AST ("tappl_ast_tr", asts);
wenzelm@42262
   107
wenzelm@42262
   108
fun bracket_ast_tr [dom, cod] = Ast.fold_ast_p "\\<^type>fun" (Ast.unfold_ast "_types" dom, cod)
wenzelm@42262
   109
  | bracket_ast_tr asts = raise Ast.AST ("bracket_ast_tr", asts);
wenzelm@42262
   110
wenzelm@42262
   111
wenzelm@548
   112
(* application *)
wenzelm@548
   113
wenzelm@5690
   114
fun appl_ast_tr [f, args] = Ast.Appl (f :: Ast.unfold_ast "_args" args)
wenzelm@5690
   115
  | appl_ast_tr asts = raise Ast.AST ("appl_ast_tr", asts);
clasohm@922
   116
wenzelm@5690
   117
fun applC_ast_tr [f, args] = Ast.Appl (f :: Ast.unfold_ast "_cargs" args)
wenzelm@5690
   118
  | applC_ast_tr asts = raise Ast.AST ("applC_ast_tr", asts);
wenzelm@548
   119
wenzelm@548
   120
wenzelm@548
   121
(* abstraction *)
wenzelm@548
   122
wenzelm@42278
   123
fun idtyp_ast_tr [x, ty] = Ast.Appl [Ast.Constant "_constrain", x, ty]
wenzelm@42278
   124
  | idtyp_ast_tr asts = raise Ast.AST ("idtyp_ast_tr", asts);
wenzelm@548
   125
wenzelm@42278
   126
fun idtypdummy_ast_tr [ty] = Ast.Appl [Ast.Constant "_constrain", Ast.Constant "_idtdummy", ty]
wenzelm@42278
   127
  | idtypdummy_ast_tr asts = raise Ast.AST ("idtyp_ast_tr", asts);
wenzelm@42278
   128
wenzelm@42278
   129
fun lambda_ast_tr [pats, body] = Ast.fold_ast_p "_abs" (Ast.unfold_ast "_pttrns" pats, body)
wenzelm@42278
   130
  | lambda_ast_tr asts = raise Ast.AST ("lambda_ast_tr", asts);
wenzelm@548
   131
wenzelm@44241
   132
fun absfree_proper (x, T) t =
wenzelm@42048
   133
  if can Name.dest_internal x
wenzelm@42048
   134
  then error ("Illegal internal variable in abstraction: " ^ quote x)
wenzelm@44241
   135
  else absfree (x, T) t;
wenzelm@21773
   136
wenzelm@44241
   137
fun abs_tr [Free x, t] = absfree_proper x t
wenzelm@44241
   138
  | abs_tr [Const ("_idtdummy", T), t] = absdummy T t
wenzelm@42284
   139
  | abs_tr [Const ("_constrain", _) $ x $ tT, t] =
wenzelm@42476
   140
      Syntax.const "_constrainAbs" $ 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@42476
   152
          in Syntax.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@42476
   160
  Syntax.const "_constrain" $
wenzelm@42476
   161
    Syntax.const "\\<^const>TYPE" $ (Syntax.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@42476
   166
fun sort_constraint_tr [ty] = Syntax.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@42476
   172
fun aprop_tr [t] = Syntax.const "_constrain" $ t $ Syntax.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@42476
   195
fun dddot_tr ts = Term.list_comb (Syntax.var Syntax_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@42476
   214
    fun tr [t] = Syntax.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@44433
   259
fun legacy_struct structs i =
wenzelm@44433
   260
  let
wenzelm@44433
   261
    val x = the_struct structs i;
wenzelm@44433
   262
    val _ =
wenzelm@44433
   263
      legacy_feature
wenzelm@44433
   264
        ("Old-style numbered structure index " ^ quote ("\\<^sub>" ^ string_of_int i) ^
wenzelm@44433
   265
          Position.str_of (Position.thread_data ()) ^
wenzelm@44433
   266
          " -- use " ^ quote ("\\<^bsub>" ^ x ^ "\\<^esub>") ^ " instead" ^
wenzelm@44433
   267
          (if i = 1 then " (may be omitted)" else ""))
wenzelm@44433
   268
  in x end;
wenzelm@44433
   269
wenzelm@44433
   270
fun struct_tr structs [Const ("_indexdefault", _)] = Syntax.free (the_struct structs 1)
wenzelm@44433
   271
  | struct_tr structs [Const ("_index1", _)] = Syntax.free (legacy_struct structs 1)
wenzelm@42278
   272
  | struct_tr structs [t as (Const ("_indexnum", _) $ Const (s, _))] =
wenzelm@44380
   273
      (case Lexicon.read_nat s of
wenzelm@44433
   274
        SOME i => Syntax.free (legacy_struct structs i)
wenzelm@44380
   275
      | NONE => raise TERM ("struct_tr", [t]))
wenzelm@42278
   276
  | struct_tr _ ts = raise TERM ("struct_tr", ts);
wenzelm@12122
   277
wenzelm@12122
   278
wenzelm@5084
   279
wenzelm@548
   280
(** print (ast) translations **)
wenzelm@548
   281
wenzelm@14647
   282
(* types *)
wenzelm@14647
   283
wenzelm@42247
   284
fun non_typed_tr' f _ ts = f ts;
wenzelm@42247
   285
fun non_typed_tr'' f x _ ts = f x ts;
wenzelm@14647
   286
wenzelm@14647
   287
wenzelm@42262
   288
(* type syntax *)
wenzelm@42262
   289
wenzelm@42262
   290
fun tappl_ast_tr' (f, []) = raise Ast.AST ("tappl_ast_tr'", [f])
wenzelm@42262
   291
  | tappl_ast_tr' (f, [ty]) = Ast.Appl [Ast.Constant "_tapp", ty, f]
wenzelm@42262
   292
  | tappl_ast_tr' (f, ty :: tys) =
wenzelm@42262
   293
      Ast.Appl [Ast.Constant "_tappl", ty, Ast.fold_ast "_types" tys, f];
wenzelm@42262
   294
wenzelm@42262
   295
fun fun_ast_tr' asts =
wenzelm@42262
   296
  if no_brackets () orelse no_type_brackets () then raise Match
wenzelm@42262
   297
  else
wenzelm@42262
   298
    (case Ast.unfold_ast_p "\\<^type>fun" (Ast.Appl (Ast.Constant "\\<^type>fun" :: asts)) of
wenzelm@42262
   299
      (dom as _ :: _ :: _, cod)
wenzelm@42262
   300
        => Ast.Appl [Ast.Constant "_bracket", Ast.fold_ast "_types" dom, cod]
wenzelm@42262
   301
    | _ => raise Match);
wenzelm@42262
   302
wenzelm@42262
   303
wenzelm@548
   304
(* application *)
wenzelm@548
   305
wenzelm@5690
   306
fun appl_ast_tr' (f, []) = raise Ast.AST ("appl_ast_tr'", [f])
wenzelm@5690
   307
  | appl_ast_tr' (f, args) = Ast.Appl [Ast.Constant "_appl", f, Ast.fold_ast "_args" args];
wenzelm@548
   308
wenzelm@5690
   309
fun applC_ast_tr' (f, []) = raise Ast.AST ("applC_ast_tr'", [f])
wenzelm@5690
   310
  | applC_ast_tr' (f, args) = Ast.Appl [Ast.Constant "_applC", f, Ast.fold_ast "_cargs" args];
clasohm@922
   311
wenzelm@548
   312
wenzelm@42085
   313
(* partial eta-contraction before printing *)
wenzelm@42085
   314
wenzelm@42085
   315
fun eta_abs (Abs (a, T, t)) =
wenzelm@42085
   316
      (case eta_abs t of
wenzelm@42085
   317
        t' as Const ("_aprop", _) $ _ => Abs (a, T, t')
wenzelm@42085
   318
      | t' as f $ u =>
wenzelm@42085
   319
          (case eta_abs u of
wenzelm@42085
   320
            Bound 0 =>
wenzelm@42085
   321
              if Term.is_dependent f then Abs (a, T, t')
wenzelm@42085
   322
              else incr_boundvars ~1 f
wenzelm@42085
   323
          | _ => Abs (a, T, t'))
wenzelm@42085
   324
      | t' => Abs (a, T, t'))
wenzelm@42085
   325
  | eta_abs t = t;
wenzelm@42085
   326
wenzelm@42085
   327
val eta_contract_default = Unsynchronized.ref true;
wenzelm@42085
   328
val eta_contract_raw = Config.declare "eta_contract" (fn _ => Config.Bool (! eta_contract_default));
wenzelm@42085
   329
val eta_contract = Config.bool eta_contract_raw;
wenzelm@42085
   330
wenzelm@42085
   331
fun eta_contr ctxt tm =
wenzelm@42085
   332
  if Config.get ctxt eta_contract then eta_abs tm else tm;
wenzelm@42085
   333
wenzelm@42085
   334
wenzelm@548
   335
(* abstraction *)
wenzelm@548
   336
wenzelm@19311
   337
fun mark_boundT (x, T) = Const ("_bound", T --> T) $ Free (x, T);
wenzelm@2698
   338
fun mark_bound x = mark_boundT (x, dummyT);
wenzelm@2698
   339
wenzelm@18958
   340
fun bound_vars vars body =
wenzelm@18958
   341
  subst_bounds (map mark_boundT (Term.rename_wrt_term body vars), body);
wenzelm@18958
   342
wenzelm@548
   343
fun strip_abss vars_of body_of tm =
wenzelm@548
   344
  let
wenzelm@548
   345
    val vars = vars_of tm;
wenzelm@548
   346
    val body = body_of tm;
wenzelm@29276
   347
    val rev_new_vars = Term.rename_wrt_term body vars;
wenzelm@21750
   348
    fun subst (x, T) b =
wenzelm@42083
   349
      if can Name.dest_internal x andalso not (Term.is_dependent b)
wenzelm@21750
   350
      then (Const ("_idtdummy", T), incr_boundvars ~1 b)
wenzelm@21750
   351
      else (mark_boundT (x, T), Term.subst_bound (mark_bound x, b));
wenzelm@21750
   352
    val (rev_vars', body') = fold_map subst rev_new_vars body;
wenzelm@21750
   353
  in (rev rev_vars', body') end;
wenzelm@548
   354
wenzelm@3928
   355
wenzelm@39128
   356
fun abs_tr' ctxt tm =
wenzelm@42476
   357
  uncurry (fold_rev (fn x => fn t => Syntax.const "_abs" $ x $ t))
wenzelm@39128
   358
    (strip_abss strip_abs_vars strip_abs_body (eta_contr ctxt tm));
wenzelm@548
   359
wenzelm@14697
   360
fun atomic_abs_tr' (x, T, t) =
wenzelm@29276
   361
  let val [xT] = Term.rename_wrt_term t [(x, T)]
wenzelm@14697
   362
  in (mark_boundT xT, subst_bound (mark_bound (fst xT), t)) end;
nipkow@13762
   363
wenzelm@42085
   364
fun abs_ast_tr' asts =
wenzelm@5690
   365
  (case Ast.unfold_ast_p "_abs" (Ast.Appl (Ast.Constant "_abs" :: asts)) of
wenzelm@5690
   366
    ([], _) => raise Ast.AST ("abs_ast_tr'", asts)
wenzelm@5690
   367
  | (xs, body) => Ast.Appl [Ast.Constant "_lambda", Ast.fold_ast "_pttrns" xs, body]);
wenzelm@548
   368
wenzelm@42086
   369
fun const_abs_tr' t =
wenzelm@42086
   370
  (case eta_abs t of
wenzelm@42086
   371
    Abs (_, _, t') =>
wenzelm@42086
   372
      if Term.is_dependent t' then raise Match
wenzelm@42086
   373
      else incr_boundvars ~1 t'
wenzelm@42086
   374
  | _ => raise Match);
wenzelm@42086
   375
haftmann@32120
   376
wenzelm@42085
   377
(* binders *)
wenzelm@548
   378
wenzelm@21535
   379
fun mk_binder_tr' (name, syn) =
wenzelm@548
   380
  let
wenzelm@548
   381
    fun mk_idts [] = raise Match    (*abort translation*)
wenzelm@548
   382
      | mk_idts [idt] = idt
wenzelm@42476
   383
      | mk_idts (idt :: idts) = Syntax.const "_idts" $ idt $ mk_idts idts;
wenzelm@548
   384
wenzelm@548
   385
    fun tr' t =
wenzelm@548
   386
      let
wenzelm@548
   387
        val (xs, bd) = strip_abss (strip_qnt_vars name) (strip_qnt_body name) t;
wenzelm@42476
   388
      in Syntax.const syn $ mk_idts xs $ bd end;
wenzelm@548
   389
wenzelm@42476
   390
    fun binder_tr' (t :: ts) = Term.list_comb (tr' (Syntax.const name $ t), ts)
wenzelm@21535
   391
      | binder_tr' [] = raise Match;
wenzelm@21535
   392
  in (name, binder_tr') end;
wenzelm@548
   393
wenzelm@42085
   394
fun preserve_binder_abs_tr' name syn = (name, fn Abs abs :: ts =>
wenzelm@42085
   395
  let val (x, t) = atomic_abs_tr' abs
wenzelm@42476
   396
  in list_comb (Syntax.const syn $ x $ t, ts) end);
wenzelm@42085
   397
wenzelm@42085
   398
fun preserve_binder_abs2_tr' name syn = (name, fn A :: Abs abs :: ts =>
wenzelm@42085
   399
  let val (x, t) = atomic_abs_tr' abs
wenzelm@42476
   400
  in list_comb (Syntax.const syn $ x $ A $ t, ts) end);
wenzelm@42085
   401
wenzelm@548
   402
wenzelm@3691
   403
(* idtyp constraints *)
wenzelm@548
   404
wenzelm@42045
   405
fun idtyp_ast_tr' a [Ast.Appl [Ast.Constant "_constrain", x, ty], xs] =
wenzelm@42045
   406
      Ast.Appl [Ast.Constant a, Ast.Appl [Ast.Constant "_idtyp", x, ty], xs]
wenzelm@3691
   407
  | idtyp_ast_tr' _ _ = raise Match;
wenzelm@548
   408
wenzelm@548
   409
wenzelm@548
   410
(* meta propositions *)
wenzelm@548
   411
wenzelm@4148
   412
fun prop_tr' tm =
wenzelm@548
   413
  let
wenzelm@42476
   414
    fun aprop t = Syntax.const "_aprop" $ t;
wenzelm@548
   415
wenzelm@2698
   416
    fun is_prop Ts t =
wenzelm@2698
   417
      fastype_of1 (Ts, t) = propT handle TERM _ => false;
wenzelm@548
   418
wenzelm@35429
   419
    fun is_term (Const ("Pure.term", _) $ _) = true
wenzelm@19848
   420
      | is_term _ = false;
wenzelm@19848
   421
wenzelm@548
   422
    fun tr' _ (t as Const _) = t
wenzelm@18478
   423
      | tr' Ts (t as Const ("_bound", _) $ u) =
wenzelm@18478
   424
          if is_prop Ts u then aprop t else t
wenzelm@2698
   425
      | tr' _ (t as Free (x, T)) =
wenzelm@42476
   426
          if T = propT then aprop (Syntax.free x) else t
wenzelm@2698
   427
      | tr' _ (t as Var (xi, T)) =
wenzelm@42476
   428
          if T = propT then aprop (Syntax.var xi) else t
wenzelm@2698
   429
      | tr' Ts (t as Bound _) =
wenzelm@2698
   430
          if is_prop Ts t then aprop t else t
wenzelm@2698
   431
      | tr' Ts (Abs (x, T, t)) = Abs (x, T, tr' (T :: Ts) t)
wenzelm@35429
   432
      | tr' Ts (t as t1 $ (t2 as Const ("TYPE", Type ("itself", [T])))) =
wenzelm@28628
   433
          if is_prop Ts t andalso not (is_term t) then Const ("_type_prop", T) $ tr' Ts t1
wenzelm@2698
   434
          else tr' Ts t1 $ tr' Ts t2
wenzelm@2698
   435
      | tr' Ts (t as t1 $ t2) =
wenzelm@5690
   436
          (if is_Const (Term.head_of t) orelse not (is_prop Ts t)
wenzelm@2698
   437
            then I else aprop) (tr' Ts t1 $ tr' Ts t2);
wenzelm@16612
   438
  in tr' [] tm end;
wenzelm@548
   439
wenzelm@2698
   440
wenzelm@548
   441
(* meta implication *)
wenzelm@548
   442
wenzelm@42278
   443
fun impl_ast_tr' asts =
wenzelm@42262
   444
  if no_brackets () then raise Match
wenzelm@10572
   445
  else
wenzelm@35255
   446
    (case Ast.unfold_ast_p "\\<^const>==>" (Ast.Appl (Ast.Constant "\\<^const>==>" :: asts)) of
wenzelm@16612
   447
      (prems as _ :: _ :: _, concl) =>
wenzelm@16612
   448
        let
wenzelm@16612
   449
          val (asms, asm) = split_last prems;
wenzelm@16612
   450
          val asms' = Ast.fold_ast_p "_asms" (asms, Ast.Appl [Ast.Constant "_asm", asm]);
wenzelm@16612
   451
        in Ast.Appl [Ast.Constant "_bigimpl", asms', concl] end
schirmer@15421
   452
    | _ => raise Match);
schirmer@15421
   453
wenzelm@548
   454
wenzelm@548
   455
(* dependent / nondependent quantifiers *)
wenzelm@548
   456
wenzelm@20202
   457
fun var_abs mark (x, T, b) =
wenzelm@43326
   458
  let val (x', _) = Name.variant x (Term.declare_term_names b Name.context)
wenzelm@20202
   459
  in (x', subst_bound (mark (x', T), b)) end;
wenzelm@20202
   460
wenzelm@20202
   461
val variant_abs = var_abs Free;
wenzelm@20202
   462
val variant_abs' = var_abs mark_boundT;
wenzelm@2698
   463
wenzelm@548
   464
fun dependent_tr' (q, r) (A :: Abs (x, T, B) :: ts) =
wenzelm@42083
   465
      if Term.is_dependent B then
wenzelm@2698
   466
        let val (x', B') = variant_abs' (x, dummyT, B);
wenzelm@42476
   467
        in Term.list_comb (Syntax.const q $ mark_boundT (x', T) $ A $ B', ts) end
wenzelm@42476
   468
      else Term.list_comb (Syntax.const r $ A $ incr_boundvars ~1 B, ts)
wenzelm@548
   469
  | dependent_tr' _ _ = raise Match;
wenzelm@548
   470
wenzelm@548
   471
wenzelm@5084
   472
(* quote / antiquote *)
wenzelm@5084
   473
wenzelm@8577
   474
fun antiquote_tr' name =
wenzelm@8577
   475
  let
wenzelm@8577
   476
    fun tr' i (t $ u) =
wenzelm@42476
   477
          if u aconv Bound i then Syntax.const name $ tr' i t
wenzelm@42084
   478
          else tr' i t $ tr' i u
wenzelm@8577
   479
      | tr' i (Abs (x, T, t)) = Abs (x, T, tr' (i + 1) t)
wenzelm@18139
   480
      | tr' i a = if a aconv Bound i then raise Match else a;
wenzelm@8577
   481
  in tr' 0 end;
wenzelm@8577
   482
wenzelm@8577
   483
fun quote_tr' name (Abs (_, _, t)) = Term.incr_boundvars ~1 (antiquote_tr' name t)
wenzelm@8577
   484
  | quote_tr' _ _ = raise Match;
wenzelm@8577
   485
wenzelm@5084
   486
fun quote_antiquote_tr' quoteN antiquoteN name =
wenzelm@5084
   487
  let
wenzelm@42476
   488
    fun tr' (t :: ts) = Term.list_comb (Syntax.const quoteN $ quote_tr' antiquoteN t, ts)
wenzelm@8577
   489
      | tr' _ = raise Match;
wenzelm@8577
   490
  in (name, tr') end;
wenzelm@5084
   491
wenzelm@5084
   492
wenzelm@35145
   493
(* corresponding updates *)
wenzelm@35145
   494
wenzelm@42080
   495
local
wenzelm@42080
   496
wenzelm@42080
   497
fun upd_type (Type ("fun", [Type ("fun", [_, T]), _])) = T
wenzelm@42080
   498
  | upd_type _ = dummyT;
wenzelm@42080
   499
wenzelm@35145
   500
fun upd_tr' (x_upd, T) =
wenzelm@35145
   501
  (case try (unsuffix "_update") x_upd of
wenzelm@42080
   502
    SOME x => (x, upd_type T)
wenzelm@35145
   503
  | NONE => raise Match);
wenzelm@35145
   504
wenzelm@42080
   505
in
wenzelm@42080
   506
wenzelm@35145
   507
fun update_name_tr' (Free x) = Free (upd_tr' x)
wenzelm@35145
   508
  | update_name_tr' ((c as Const ("_free", _)) $ Free x) = c $ Free (upd_tr' x)
wenzelm@35145
   509
  | update_name_tr' (Const x) = Const (upd_tr' x)
wenzelm@35145
   510
  | update_name_tr' _ = raise Match;
wenzelm@35145
   511
wenzelm@42080
   512
end;
wenzelm@42080
   513
wenzelm@35145
   514
wenzelm@14697
   515
(* indexed syntax *)
wenzelm@548
   516
wenzelm@42278
   517
fun index_ast_tr' [Ast.Appl [Ast.Constant "_struct", ast]] = ast
wenzelm@14697
   518
  | index_ast_tr' _ = raise Match;
wenzelm@14697
   519
wenzelm@14697
   520
wenzelm@14697
   521
(* implicit structures *)
wenzelm@14697
   522
wenzelm@14697
   523
fun the_struct' structs s =
wenzelm@14697
   524
  [(case Lexicon.read_nat s of
wenzelm@18678
   525
    SOME i => Ast.Variable (the_struct structs i handle ERROR _ => raise Match)
skalberg@15531
   526
  | NONE => raise Match)] |> Ast.mk_appl (Ast.Constant "_free");
wenzelm@14697
   527
wenzelm@42278
   528
fun struct_ast_tr' structs [Ast.Constant "_indexdefault"] = the_struct' structs "1"
wenzelm@42278
   529
  | struct_ast_tr' structs [Ast.Appl [Ast.Constant "_indexnum", Ast.Constant s]] =
wenzelm@14697
   530
      the_struct' structs s
wenzelm@14697
   531
  | struct_ast_tr' _ _ = raise Match;
wenzelm@14697
   532
wenzelm@14697
   533
wenzelm@14697
   534
wenzelm@14697
   535
(** Pure translations **)
wenzelm@548
   536
wenzelm@548
   537
val pure_trfuns =
wenzelm@42057
   538
  ([("_strip_positions", strip_positions_ast_tr),
wenzelm@42057
   539
    ("_constify", constify_ast_tr),
wenzelm@42262
   540
    ("_tapp", tapp_ast_tr),
wenzelm@42262
   541
    ("_tappl", tappl_ast_tr),
wenzelm@42262
   542
    ("_bracket", bracket_ast_tr),
wenzelm@35145
   543
    ("_appl", appl_ast_tr),
wenzelm@35145
   544
    ("_applC", applC_ast_tr),
wenzelm@35145
   545
    ("_lambda", lambda_ast_tr),
wenzelm@35145
   546
    ("_idtyp", idtyp_ast_tr),
wenzelm@35145
   547
    ("_idtypdummy", idtypdummy_ast_tr),
wenzelm@35145
   548
    ("_bigimpl", bigimpl_ast_tr),
wenzelm@35145
   549
    ("_indexdefault", indexdefault_ast_tr),
wenzelm@35145
   550
    ("_indexnum", indexnum_ast_tr),
wenzelm@35145
   551
    ("_indexvar", indexvar_ast_tr),
wenzelm@35145
   552
    ("_struct", struct_ast_tr)],
wenzelm@35145
   553
   [("_abs", abs_tr),
wenzelm@35145
   554
    ("_aprop", aprop_tr),
wenzelm@35145
   555
    ("_ofclass", ofclass_tr),
wenzelm@35145
   556
    ("_sort_constraint", sort_constraint_tr),
wenzelm@35145
   557
    ("_TYPE", type_tr),
wenzelm@35145
   558
    ("_DDDOT", dddot_tr),
wenzelm@35145
   559
    ("_update_name", update_name_tr),
wenzelm@35145
   560
    ("_index", index_tr)],
wenzelm@35198
   561
   ([]: (string * (term list -> term)) list),
wenzelm@42262
   562
   [("\\<^type>fun", fun_ast_tr'),
wenzelm@42262
   563
    ("_abs", abs_ast_tr'),
wenzelm@35145
   564
    ("_idts", idtyp_ast_tr' "_idts"),
wenzelm@35145
   565
    ("_pttrns", idtyp_ast_tr' "_pttrns"),
wenzelm@35255
   566
    ("\\<^const>==>", impl_ast_tr'),
wenzelm@35145
   567
    ("_index", index_ast_tr')]);
wenzelm@548
   568
wenzelm@14697
   569
fun struct_trfuns structs =
wenzelm@14697
   570
  ([], [("_struct", struct_tr structs)], [], [("_struct", struct_ast_tr' structs)]);
wenzelm@14697
   571
wenzelm@548
   572
end;
wenzelm@42284
   573
wenzelm@42284
   574
structure Basic_Syntax_Trans: BASIC_SYNTAX_TRANS = Syntax_Trans;
wenzelm@42284
   575
open Basic_Syntax_Trans;