src/Pure/Syntax/syntax_trans.ML
author wenzelm
Thu May 16 21:48:01 2013 +0200 (2013-05-16)
changeset 52043 286629271d65
parent 51656 4ce2f7607d3d
child 52143 36ffe23b25f8
permissions -rw-r--r--
more system options as context-sensitive config options;
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@39163
    31
  val eta_contract_raw: Config.raw
wenzelm@49660
    32
  val mark_bound_abs: string * typ -> term
wenzelm@49660
    33
  val mark_bound_body: string * typ -> term
wenzelm@42284
    34
  val bound_vars: (string * typ) list -> term -> term
wenzelm@42284
    35
  val abs_tr': Proof.context -> term -> term
nipkow@13762
    36
  val atomic_abs_tr': string * typ * term -> term * term
wenzelm@42086
    37
  val const_abs_tr': term -> term
wenzelm@42085
    38
  val mk_binder_tr': string * string -> string * (term list -> term)
haftmann@32120
    39
  val preserve_binder_abs_tr': string -> string -> string * (term list -> term)
haftmann@32120
    40
  val preserve_binder_abs2_tr': string -> string -> string * (term list -> term)
wenzelm@42284
    41
  val prop_tr': term -> term
wenzelm@42284
    42
  val variant_abs: string * typ * term -> string * term
wenzelm@42284
    43
  val variant_abs': string * typ * term -> string * term
wenzelm@548
    44
  val dependent_tr': string * string -> term list -> term
wenzelm@8577
    45
  val antiquote_tr': string -> term -> term
wenzelm@8577
    46
  val quote_tr': string -> term -> term
wenzelm@5084
    47
  val quote_antiquote_tr': string -> string -> string -> string * (term list -> term)
wenzelm@35145
    48
  val update_name_tr': term -> term
paulson@1511
    49
  val pure_trfuns:
wenzelm@35429
    50
    (string * (Ast.ast list -> Ast.ast)) list *
wenzelm@35429
    51
    (string * (term list -> term)) list *
wenzelm@35429
    52
    (string * (term list -> term)) list *
wenzelm@35429
    53
    (string * (Ast.ast list -> Ast.ast)) list
wenzelm@14697
    54
  val struct_trfuns: string list ->
wenzelm@35429
    55
    (string * (Ast.ast list -> Ast.ast)) list *
wenzelm@35429
    56
    (string * (term list -> term)) list *
wenzelm@42247
    57
    (string * (typ -> term list -> term)) list *
wenzelm@35429
    58
    (string * (Ast.ast list -> Ast.ast)) list
wenzelm@2698
    59
end;
wenzelm@548
    60
wenzelm@42284
    61
structure Syntax_Trans: SYNTAX_TRANS =
wenzelm@548
    62
struct
wenzelm@2698
    63
wenzelm@42476
    64
structure Syntax = Lexicon.Syntax;
wenzelm@42476
    65
wenzelm@42476
    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@46236
    95
fun constify_ast_tr [Ast.Appl [c as Ast.Constant "_constrain", ast1, ast2]] =
wenzelm@46236
    96
      Ast.Appl [c, constify_ast_tr [ast1], ast2]
wenzelm@46236
    97
  | constify_ast_tr [Ast.Variable c] = Ast.Constant c
wenzelm@11491
    98
  | constify_ast_tr asts = raise Ast.AST ("constify_ast_tr", asts);
wenzelm@11491
    99
wenzelm@11491
   100
wenzelm@42262
   101
(* type syntax *)
wenzelm@42262
   102
wenzelm@42262
   103
fun tapp_ast_tr [ty, c] = Ast.Appl [c, ty]
wenzelm@42262
   104
  | tapp_ast_tr asts = raise Ast.AST ("tapp_ast_tr", asts);
wenzelm@42262
   105
wenzelm@42262
   106
fun tappl_ast_tr [ty, tys, c] = Ast.mk_appl c (ty :: Ast.unfold_ast "_types" tys)
wenzelm@42262
   107
  | tappl_ast_tr asts = raise Ast.AST ("tappl_ast_tr", asts);
wenzelm@42262
   108
wenzelm@42262
   109
fun bracket_ast_tr [dom, cod] = Ast.fold_ast_p "\\<^type>fun" (Ast.unfold_ast "_types" dom, cod)
wenzelm@42262
   110
  | bracket_ast_tr asts = raise Ast.AST ("bracket_ast_tr", asts);
wenzelm@42262
   111
wenzelm@42262
   112
wenzelm@548
   113
(* application *)
wenzelm@548
   114
wenzelm@5690
   115
fun appl_ast_tr [f, args] = Ast.Appl (f :: Ast.unfold_ast "_args" args)
wenzelm@5690
   116
  | appl_ast_tr asts = raise Ast.AST ("appl_ast_tr", asts);
clasohm@922
   117
wenzelm@5690
   118
fun applC_ast_tr [f, args] = Ast.Appl (f :: Ast.unfold_ast "_cargs" args)
wenzelm@5690
   119
  | applC_ast_tr asts = raise Ast.AST ("applC_ast_tr", asts);
wenzelm@548
   120
wenzelm@548
   121
wenzelm@548
   122
(* abstraction *)
wenzelm@548
   123
wenzelm@42278
   124
fun idtyp_ast_tr [x, ty] = Ast.Appl [Ast.Constant "_constrain", x, ty]
wenzelm@42278
   125
  | idtyp_ast_tr asts = raise Ast.AST ("idtyp_ast_tr", asts);
wenzelm@548
   126
wenzelm@42278
   127
fun idtypdummy_ast_tr [ty] = Ast.Appl [Ast.Constant "_constrain", Ast.Constant "_idtdummy", ty]
wenzelm@42278
   128
  | idtypdummy_ast_tr asts = raise Ast.AST ("idtyp_ast_tr", asts);
wenzelm@42278
   129
wenzelm@42278
   130
fun lambda_ast_tr [pats, body] = Ast.fold_ast_p "_abs" (Ast.unfold_ast "_pttrns" pats, body)
wenzelm@42278
   131
  | lambda_ast_tr asts = raise Ast.AST ("lambda_ast_tr", asts);
wenzelm@548
   132
wenzelm@44241
   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@44241
   136
  else absfree (x, T) t;
wenzelm@21773
   137
wenzelm@44241
   138
fun abs_tr [Free x, t] = absfree_proper x t
wenzelm@44241
   139
  | abs_tr [Const ("_idtdummy", T), t] = absdummy T t
wenzelm@42284
   140
  | abs_tr [Const ("_constrain", _) $ x $ tT, t] =
wenzelm@42476
   141
      Syntax.const "_constrainAbs" $ 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@42476
   153
          in Syntax.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@42476
   161
  Syntax.const "_constrain" $
wenzelm@42476
   162
    Syntax.const "\\<^const>TYPE" $ (Syntax.const "\\<^type>itself" $ ty);
wenzelm@28628
   163
wenzelm@42278
   164
fun ofclass_tr [ty, cls] = cls $ mk_type ty
wenzelm@42278
   165
  | ofclass_tr ts = raise TERM ("ofclass_tr", ts);
wenzelm@28628
   166
wenzelm@42476
   167
fun sort_constraint_tr [ty] = Syntax.const "\\<^const>Pure.sort_constraint" $ mk_type ty
wenzelm@42278
   168
  | sort_constraint_tr ts = raise TERM ("sort_constraint_tr", ts);
wenzelm@28628
   169
wenzelm@28628
   170
wenzelm@548
   171
(* meta propositions *)
wenzelm@548
   172
wenzelm@42476
   173
fun aprop_tr [t] = Syntax.const "_constrain" $ t $ Syntax.const "\\<^type>prop"
wenzelm@42278
   174
  | aprop_tr ts = raise TERM ("aprop_tr", ts);
wenzelm@548
   175
wenzelm@548
   176
wenzelm@548
   177
(* meta implication *)
wenzelm@548
   178
wenzelm@42278
   179
fun bigimpl_ast_tr (asts as [asms, concl]) =
wenzelm@16612
   180
      let val prems =
wenzelm@16612
   181
        (case Ast.unfold_ast_p "_asms" asms of
wenzelm@16612
   182
          (asms', Ast.Appl [Ast.Constant "_asm", asm']) => asms' @ [asm']
wenzelm@16612
   183
        | _ => raise Ast.AST ("bigimpl_ast_tr", asts))
wenzelm@35255
   184
      in Ast.fold_ast_p "\\<^const>==>" (prems, concl) end
wenzelm@42278
   185
  | bigimpl_ast_tr asts = raise Ast.AST ("bigimpl_ast_tr", asts);
schirmer@15421
   186
wenzelm@548
   187
wenzelm@23824
   188
(* type/term reflection *)
wenzelm@4148
   189
wenzelm@42278
   190
fun type_tr [ty] = mk_type ty
wenzelm@42278
   191
  | type_tr ts = raise TERM ("type_tr", ts);
wenzelm@4148
   192
wenzelm@548
   193
wenzelm@6761
   194
(* dddot *)
wenzelm@6761
   195
wenzelm@42476
   196
fun dddot_tr ts = Term.list_comb (Syntax.var Syntax_Ext.dddot_indexname, ts);
wenzelm@6761
   197
wenzelm@6761
   198
wenzelm@5084
   199
(* quote / antiquote *)
wenzelm@5084
   200
wenzelm@8577
   201
fun antiquote_tr name =
wenzelm@8577
   202
  let
wenzelm@8577
   203
    fun tr i ((t as Const (c, _)) $ u) =
wenzelm@8577
   204
          if c = name then tr i u $ Bound i
wenzelm@8577
   205
          else tr i t $ tr i u
wenzelm@8577
   206
      | tr i (t $ u) = tr i t $ tr i u
wenzelm@8577
   207
      | tr i (Abs (x, T, t)) = Abs (x, T, tr (i + 1) t)
wenzelm@8577
   208
      | tr _ a = a;
wenzelm@8577
   209
  in tr 0 end;
wenzelm@8577
   210
wenzelm@8577
   211
fun quote_tr name t = Abs ("s", dummyT, antiquote_tr name (Term.incr_boundvars 1 t));
wenzelm@8577
   212
wenzelm@5084
   213
fun quote_antiquote_tr quoteN antiquoteN name =
wenzelm@5084
   214
  let
wenzelm@42476
   215
    fun tr [t] = Syntax.const name $ quote_tr antiquoteN t
wenzelm@8577
   216
      | tr ts = raise TERM ("quote_tr", ts);
wenzelm@8577
   217
  in (quoteN, tr) end;
wenzelm@5084
   218
wenzelm@5084
   219
wenzelm@35145
   220
(* corresponding updates *)
wenzelm@35145
   221
wenzelm@35145
   222
fun update_name_tr (Free (x, T) :: ts) = list_comb (Free (suffix "_update" x, T), ts)
wenzelm@35145
   223
  | update_name_tr (Const (x, T) :: ts) = list_comb (Const (suffix "_update" x, T), ts)
wenzelm@35145
   224
  | update_name_tr (((c as Const ("_constrain", _)) $ t $ ty) :: ts) =
wenzelm@42264
   225
      if Term_Position.is_position ty then list_comb (c $ update_name_tr [t] $ ty, ts)
wenzelm@42053
   226
      else
wenzelm@42053
   227
        list_comb (c $ update_name_tr [t] $
wenzelm@42080
   228
          (Lexicon.fun_type $
wenzelm@42080
   229
            (Lexicon.fun_type $ Lexicon.dummy_type $ ty) $ Lexicon.dummy_type), ts)
wenzelm@35145
   230
  | update_name_tr ts = raise TERM ("update_name_tr", ts);
wenzelm@35145
   231
wenzelm@35145
   232
wenzelm@14697
   233
(* indexed syntax *)
wenzelm@14697
   234
wenzelm@45389
   235
fun indexdefault_ast_tr [] =
wenzelm@45389
   236
      Ast.Appl [Ast.Constant "_index",
wenzelm@45389
   237
        Ast.Appl [Ast.Constant "_struct", Ast.Constant "_indexdefault"]]
wenzelm@45389
   238
  | indexdefault_ast_tr asts = raise Ast.AST ("indexdefault_ast_tr", asts);
wenzelm@45389
   239
wenzelm@45389
   240
fun indexvar_ast_tr [] = Ast.Appl [Ast.Constant "_index", Ast.Variable "some_index"]
wenzelm@45389
   241
  | indexvar_ast_tr asts = raise Ast.AST ("indexvar_ast_tr", asts);
wenzelm@45389
   242
wenzelm@42278
   243
fun struct_ast_tr [Ast.Appl [Ast.Constant "_index", ast]] = ast
wenzelm@42278
   244
  | struct_ast_tr asts = Ast.mk_appl (Ast.Constant "_struct") asts;
wenzelm@14697
   245
wenzelm@42278
   246
fun index_tr [t] = t
wenzelm@42278
   247
  | index_tr ts = raise TERM ("index_tr", ts);
wenzelm@14697
   248
wenzelm@45389
   249
fun the_struct [] = error "Illegal reference to implicit structure"
wenzelm@45389
   250
  | the_struct (x :: _) = x;
wenzelm@14697
   251
wenzelm@51656
   252
fun struct_tr structs [Const ("_indexdefault", _)] =
wenzelm@51656
   253
      Syntax.const (Lexicon.mark_fixed (the_struct structs))
wenzelm@42278
   254
  | struct_tr _ ts = raise TERM ("struct_tr", ts);
wenzelm@12122
   255
wenzelm@12122
   256
wenzelm@5084
   257
wenzelm@548
   258
(** print (ast) translations **)
wenzelm@548
   259
wenzelm@14647
   260
(* types *)
wenzelm@14647
   261
wenzelm@42247
   262
fun non_typed_tr' f _ ts = f ts;
wenzelm@42247
   263
fun non_typed_tr'' f x _ ts = f x ts;
wenzelm@14647
   264
wenzelm@14647
   265
wenzelm@42262
   266
(* type syntax *)
wenzelm@42262
   267
wenzelm@42262
   268
fun tappl_ast_tr' (f, []) = raise Ast.AST ("tappl_ast_tr'", [f])
wenzelm@42262
   269
  | tappl_ast_tr' (f, [ty]) = Ast.Appl [Ast.Constant "_tapp", ty, f]
wenzelm@42262
   270
  | tappl_ast_tr' (f, ty :: tys) =
wenzelm@42262
   271
      Ast.Appl [Ast.Constant "_tappl", ty, Ast.fold_ast "_types" tys, f];
wenzelm@42262
   272
wenzelm@42262
   273
fun fun_ast_tr' asts =
wenzelm@42262
   274
  if no_brackets () orelse no_type_brackets () then raise Match
wenzelm@42262
   275
  else
wenzelm@42262
   276
    (case Ast.unfold_ast_p "\\<^type>fun" (Ast.Appl (Ast.Constant "\\<^type>fun" :: asts)) of
wenzelm@42262
   277
      (dom as _ :: _ :: _, cod)
wenzelm@42262
   278
        => Ast.Appl [Ast.Constant "_bracket", Ast.fold_ast "_types" dom, cod]
wenzelm@42262
   279
    | _ => raise Match);
wenzelm@42262
   280
wenzelm@42262
   281
wenzelm@548
   282
(* application *)
wenzelm@548
   283
wenzelm@5690
   284
fun appl_ast_tr' (f, []) = raise Ast.AST ("appl_ast_tr'", [f])
wenzelm@5690
   285
  | appl_ast_tr' (f, args) = Ast.Appl [Ast.Constant "_appl", f, Ast.fold_ast "_args" args];
wenzelm@548
   286
wenzelm@5690
   287
fun applC_ast_tr' (f, []) = raise Ast.AST ("applC_ast_tr'", [f])
wenzelm@5690
   288
  | applC_ast_tr' (f, args) = Ast.Appl [Ast.Constant "_applC", f, Ast.fold_ast "_cargs" args];
clasohm@922
   289
wenzelm@548
   290
wenzelm@42085
   291
(* partial eta-contraction before printing *)
wenzelm@42085
   292
wenzelm@42085
   293
fun eta_abs (Abs (a, T, t)) =
wenzelm@42085
   294
      (case eta_abs t of
wenzelm@42085
   295
        t' as Const ("_aprop", _) $ _ => Abs (a, T, t')
wenzelm@42085
   296
      | t' as f $ u =>
wenzelm@42085
   297
          (case eta_abs u of
wenzelm@42085
   298
            Bound 0 =>
wenzelm@42085
   299
              if Term.is_dependent f then Abs (a, T, t')
wenzelm@42085
   300
              else incr_boundvars ~1 f
wenzelm@42085
   301
          | _ => Abs (a, T, t'))
wenzelm@42085
   302
      | t' => Abs (a, T, t'))
wenzelm@42085
   303
  | eta_abs t = t;
wenzelm@42085
   304
wenzelm@52043
   305
val eta_contract_raw = Config.declare_option "eta_contract";
wenzelm@42085
   306
val eta_contract = Config.bool eta_contract_raw;
wenzelm@42085
   307
wenzelm@42085
   308
fun eta_contr ctxt tm =
wenzelm@42085
   309
  if Config.get ctxt eta_contract then eta_abs tm else tm;
wenzelm@42085
   310
wenzelm@42085
   311
wenzelm@548
   312
(* abstraction *)
wenzelm@548
   313
wenzelm@49660
   314
fun mark_bound_abs (x, T) = Const ("_bound", T --> T) $ Free (x, T);
wenzelm@49660
   315
fun mark_bound_body (x, T) = Const ("_bound", dummyT) $ Free (x, T);
wenzelm@2698
   316
wenzelm@18958
   317
fun bound_vars vars body =
wenzelm@49660
   318
  subst_bounds (map mark_bound_abs (Term.rename_wrt_term body vars), body);
wenzelm@18958
   319
wenzelm@548
   320
fun strip_abss vars_of body_of tm =
wenzelm@548
   321
  let
wenzelm@548
   322
    val vars = vars_of tm;
wenzelm@548
   323
    val body = body_of tm;
wenzelm@29276
   324
    val rev_new_vars = Term.rename_wrt_term body vars;
wenzelm@21750
   325
    fun subst (x, T) b =
wenzelm@42083
   326
      if can Name.dest_internal x andalso not (Term.is_dependent b)
wenzelm@21750
   327
      then (Const ("_idtdummy", T), incr_boundvars ~1 b)
wenzelm@49660
   328
      else (mark_bound_abs (x, T), Term.subst_bound (mark_bound_body (x, T), b));
wenzelm@21750
   329
    val (rev_vars', body') = fold_map subst rev_new_vars body;
wenzelm@21750
   330
  in (rev rev_vars', body') end;
wenzelm@548
   331
wenzelm@3928
   332
wenzelm@39128
   333
fun abs_tr' ctxt tm =
wenzelm@42476
   334
  uncurry (fold_rev (fn x => fn t => Syntax.const "_abs" $ x $ t))
wenzelm@39128
   335
    (strip_abss strip_abs_vars strip_abs_body (eta_contr ctxt tm));
wenzelm@548
   336
wenzelm@14697
   337
fun atomic_abs_tr' (x, T, t) =
wenzelm@29276
   338
  let val [xT] = Term.rename_wrt_term t [(x, T)]
wenzelm@49660
   339
  in (mark_bound_abs xT, subst_bound (mark_bound_body xT, t)) end;
nipkow@13762
   340
wenzelm@42085
   341
fun abs_ast_tr' asts =
wenzelm@5690
   342
  (case Ast.unfold_ast_p "_abs" (Ast.Appl (Ast.Constant "_abs" :: asts)) of
wenzelm@5690
   343
    ([], _) => raise Ast.AST ("abs_ast_tr'", asts)
wenzelm@5690
   344
  | (xs, body) => Ast.Appl [Ast.Constant "_lambda", Ast.fold_ast "_pttrns" xs, body]);
wenzelm@548
   345
wenzelm@42086
   346
fun const_abs_tr' t =
wenzelm@42086
   347
  (case eta_abs t of
wenzelm@42086
   348
    Abs (_, _, t') =>
wenzelm@42086
   349
      if Term.is_dependent t' then raise Match
wenzelm@42086
   350
      else incr_boundvars ~1 t'
wenzelm@42086
   351
  | _ => raise Match);
wenzelm@42086
   352
haftmann@32120
   353
wenzelm@42085
   354
(* binders *)
wenzelm@548
   355
wenzelm@21535
   356
fun mk_binder_tr' (name, syn) =
wenzelm@548
   357
  let
wenzelm@548
   358
    fun mk_idts [] = raise Match    (*abort translation*)
wenzelm@548
   359
      | mk_idts [idt] = idt
wenzelm@42476
   360
      | mk_idts (idt :: idts) = Syntax.const "_idts" $ idt $ mk_idts idts;
wenzelm@548
   361
wenzelm@548
   362
    fun tr' t =
wenzelm@548
   363
      let
wenzelm@548
   364
        val (xs, bd) = strip_abss (strip_qnt_vars name) (strip_qnt_body name) t;
wenzelm@42476
   365
      in Syntax.const syn $ mk_idts xs $ bd end;
wenzelm@548
   366
wenzelm@42476
   367
    fun binder_tr' (t :: ts) = Term.list_comb (tr' (Syntax.const name $ t), ts)
wenzelm@21535
   368
      | binder_tr' [] = raise Match;
wenzelm@21535
   369
  in (name, binder_tr') end;
wenzelm@548
   370
wenzelm@42085
   371
fun preserve_binder_abs_tr' name syn = (name, fn Abs abs :: ts =>
wenzelm@42085
   372
  let val (x, t) = atomic_abs_tr' abs
wenzelm@42476
   373
  in list_comb (Syntax.const syn $ x $ t, ts) end);
wenzelm@42085
   374
wenzelm@42085
   375
fun preserve_binder_abs2_tr' name syn = (name, fn A :: Abs abs :: ts =>
wenzelm@42085
   376
  let val (x, t) = atomic_abs_tr' abs
wenzelm@42476
   377
  in list_comb (Syntax.const syn $ x $ A $ t, ts) end);
wenzelm@42085
   378
wenzelm@548
   379
wenzelm@3691
   380
(* idtyp constraints *)
wenzelm@548
   381
wenzelm@42045
   382
fun idtyp_ast_tr' a [Ast.Appl [Ast.Constant "_constrain", x, ty], xs] =
wenzelm@42045
   383
      Ast.Appl [Ast.Constant a, Ast.Appl [Ast.Constant "_idtyp", x, ty], xs]
wenzelm@3691
   384
  | idtyp_ast_tr' _ _ = raise Match;
wenzelm@548
   385
wenzelm@548
   386
wenzelm@548
   387
(* meta propositions *)
wenzelm@548
   388
wenzelm@4148
   389
fun prop_tr' tm =
wenzelm@548
   390
  let
wenzelm@42476
   391
    fun aprop t = Syntax.const "_aprop" $ t;
wenzelm@548
   392
wenzelm@2698
   393
    fun is_prop Ts t =
wenzelm@2698
   394
      fastype_of1 (Ts, t) = propT handle TERM _ => false;
wenzelm@548
   395
wenzelm@35429
   396
    fun is_term (Const ("Pure.term", _) $ _) = true
wenzelm@19848
   397
      | is_term _ = false;
wenzelm@19848
   398
wenzelm@548
   399
    fun tr' _ (t as Const _) = t
wenzelm@18478
   400
      | tr' Ts (t as Const ("_bound", _) $ u) =
wenzelm@18478
   401
          if is_prop Ts u then aprop t else t
wenzelm@2698
   402
      | tr' _ (t as Free (x, T)) =
wenzelm@42476
   403
          if T = propT then aprop (Syntax.free x) else t
wenzelm@2698
   404
      | tr' _ (t as Var (xi, T)) =
wenzelm@42476
   405
          if T = propT then aprop (Syntax.var xi) else t
wenzelm@2698
   406
      | tr' Ts (t as Bound _) =
wenzelm@2698
   407
          if is_prop Ts t then aprop t else t
wenzelm@2698
   408
      | tr' Ts (Abs (x, T, t)) = Abs (x, T, tr' (T :: Ts) t)
wenzelm@35429
   409
      | tr' Ts (t as t1 $ (t2 as Const ("TYPE", Type ("itself", [T])))) =
wenzelm@28628
   410
          if is_prop Ts t andalso not (is_term t) then Const ("_type_prop", T) $ tr' Ts t1
wenzelm@2698
   411
          else tr' Ts t1 $ tr' Ts t2
wenzelm@2698
   412
      | tr' Ts (t as t1 $ t2) =
wenzelm@5690
   413
          (if is_Const (Term.head_of t) orelse not (is_prop Ts t)
wenzelm@2698
   414
            then I else aprop) (tr' Ts t1 $ tr' Ts t2);
wenzelm@16612
   415
  in tr' [] tm end;
wenzelm@548
   416
wenzelm@2698
   417
wenzelm@548
   418
(* meta implication *)
wenzelm@548
   419
wenzelm@42278
   420
fun impl_ast_tr' asts =
wenzelm@42262
   421
  if no_brackets () then raise Match
wenzelm@10572
   422
  else
wenzelm@35255
   423
    (case Ast.unfold_ast_p "\\<^const>==>" (Ast.Appl (Ast.Constant "\\<^const>==>" :: asts)) of
wenzelm@16612
   424
      (prems as _ :: _ :: _, concl) =>
wenzelm@16612
   425
        let
wenzelm@16612
   426
          val (asms, asm) = split_last prems;
wenzelm@16612
   427
          val asms' = Ast.fold_ast_p "_asms" (asms, Ast.Appl [Ast.Constant "_asm", asm]);
wenzelm@16612
   428
        in Ast.Appl [Ast.Constant "_bigimpl", asms', concl] end
schirmer@15421
   429
    | _ => raise Match);
schirmer@15421
   430
wenzelm@548
   431
wenzelm@548
   432
(* dependent / nondependent quantifiers *)
wenzelm@548
   433
wenzelm@20202
   434
fun var_abs mark (x, T, b) =
wenzelm@43326
   435
  let val (x', _) = Name.variant x (Term.declare_term_names b Name.context)
wenzelm@20202
   436
  in (x', subst_bound (mark (x', T), b)) end;
wenzelm@20202
   437
wenzelm@20202
   438
val variant_abs = var_abs Free;
wenzelm@49660
   439
val variant_abs' = var_abs mark_bound_abs;
wenzelm@2698
   440
wenzelm@548
   441
fun dependent_tr' (q, r) (A :: Abs (x, T, B) :: ts) =
wenzelm@42083
   442
      if Term.is_dependent B then
wenzelm@2698
   443
        let val (x', B') = variant_abs' (x, dummyT, B);
wenzelm@49660
   444
        in Term.list_comb (Syntax.const q $ mark_bound_abs (x', T) $ A $ B', ts) end
wenzelm@42476
   445
      else Term.list_comb (Syntax.const r $ A $ incr_boundvars ~1 B, ts)
wenzelm@548
   446
  | dependent_tr' _ _ = raise Match;
wenzelm@548
   447
wenzelm@548
   448
wenzelm@5084
   449
(* quote / antiquote *)
wenzelm@5084
   450
wenzelm@8577
   451
fun antiquote_tr' name =
wenzelm@8577
   452
  let
wenzelm@8577
   453
    fun tr' i (t $ u) =
wenzelm@42476
   454
          if u aconv Bound i then Syntax.const name $ tr' i t
wenzelm@42084
   455
          else tr' i t $ tr' i u
wenzelm@8577
   456
      | tr' i (Abs (x, T, t)) = Abs (x, T, tr' (i + 1) t)
wenzelm@18139
   457
      | tr' i a = if a aconv Bound i then raise Match else a;
wenzelm@8577
   458
  in tr' 0 end;
wenzelm@8577
   459
wenzelm@8577
   460
fun quote_tr' name (Abs (_, _, t)) = Term.incr_boundvars ~1 (antiquote_tr' name t)
wenzelm@8577
   461
  | quote_tr' _ _ = raise Match;
wenzelm@8577
   462
wenzelm@5084
   463
fun quote_antiquote_tr' quoteN antiquoteN name =
wenzelm@5084
   464
  let
wenzelm@42476
   465
    fun tr' (t :: ts) = Term.list_comb (Syntax.const quoteN $ quote_tr' antiquoteN t, ts)
wenzelm@8577
   466
      | tr' _ = raise Match;
wenzelm@8577
   467
  in (name, tr') end;
wenzelm@5084
   468
wenzelm@5084
   469
wenzelm@35145
   470
(* corresponding updates *)
wenzelm@35145
   471
wenzelm@42080
   472
local
wenzelm@42080
   473
wenzelm@42080
   474
fun upd_type (Type ("fun", [Type ("fun", [_, T]), _])) = T
wenzelm@42080
   475
  | upd_type _ = dummyT;
wenzelm@42080
   476
wenzelm@35145
   477
fun upd_tr' (x_upd, T) =
wenzelm@35145
   478
  (case try (unsuffix "_update") x_upd of
wenzelm@42080
   479
    SOME x => (x, upd_type T)
wenzelm@35145
   480
  | NONE => raise Match);
wenzelm@35145
   481
wenzelm@42080
   482
in
wenzelm@42080
   483
wenzelm@35145
   484
fun update_name_tr' (Free x) = Free (upd_tr' x)
wenzelm@35145
   485
  | update_name_tr' ((c as Const ("_free", _)) $ Free x) = c $ Free (upd_tr' x)
wenzelm@35145
   486
  | update_name_tr' (Const x) = Const (upd_tr' x)
wenzelm@35145
   487
  | update_name_tr' _ = raise Match;
wenzelm@35145
   488
wenzelm@42080
   489
end;
wenzelm@42080
   490
wenzelm@35145
   491
wenzelm@14697
   492
(* indexed syntax *)
wenzelm@548
   493
wenzelm@42278
   494
fun index_ast_tr' [Ast.Appl [Ast.Constant "_struct", ast]] = ast
wenzelm@14697
   495
  | index_ast_tr' _ = raise Match;
wenzelm@14697
   496
wenzelm@45389
   497
fun struct_ast_tr' structs [Ast.Constant "_indexdefault"] =
wenzelm@45389
   498
      Ast.Appl [Ast.Constant "_free",
wenzelm@45389
   499
        Ast.Variable (the_struct structs handle ERROR _ => raise Match)]
wenzelm@14697
   500
  | struct_ast_tr' _ _ = raise Match;
wenzelm@14697
   501
wenzelm@14697
   502
wenzelm@14697
   503
wenzelm@14697
   504
(** Pure translations **)
wenzelm@548
   505
wenzelm@548
   506
val pure_trfuns =
wenzelm@42057
   507
  ([("_strip_positions", strip_positions_ast_tr),
wenzelm@42057
   508
    ("_constify", constify_ast_tr),
wenzelm@42262
   509
    ("_tapp", tapp_ast_tr),
wenzelm@42262
   510
    ("_tappl", tappl_ast_tr),
wenzelm@42262
   511
    ("_bracket", bracket_ast_tr),
wenzelm@35145
   512
    ("_appl", appl_ast_tr),
wenzelm@35145
   513
    ("_applC", applC_ast_tr),
wenzelm@35145
   514
    ("_lambda", lambda_ast_tr),
wenzelm@35145
   515
    ("_idtyp", idtyp_ast_tr),
wenzelm@35145
   516
    ("_idtypdummy", idtypdummy_ast_tr),
wenzelm@35145
   517
    ("_bigimpl", bigimpl_ast_tr),
wenzelm@35145
   518
    ("_indexdefault", indexdefault_ast_tr),
wenzelm@35145
   519
    ("_indexvar", indexvar_ast_tr),
wenzelm@35145
   520
    ("_struct", struct_ast_tr)],
wenzelm@35145
   521
   [("_abs", abs_tr),
wenzelm@35145
   522
    ("_aprop", aprop_tr),
wenzelm@35145
   523
    ("_ofclass", ofclass_tr),
wenzelm@35145
   524
    ("_sort_constraint", sort_constraint_tr),
wenzelm@35145
   525
    ("_TYPE", type_tr),
wenzelm@35145
   526
    ("_DDDOT", dddot_tr),
wenzelm@35145
   527
    ("_update_name", update_name_tr),
wenzelm@35145
   528
    ("_index", index_tr)],
wenzelm@35198
   529
   ([]: (string * (term list -> term)) list),
wenzelm@42262
   530
   [("\\<^type>fun", fun_ast_tr'),
wenzelm@42262
   531
    ("_abs", abs_ast_tr'),
wenzelm@35145
   532
    ("_idts", idtyp_ast_tr' "_idts"),
wenzelm@35145
   533
    ("_pttrns", idtyp_ast_tr' "_pttrns"),
wenzelm@35255
   534
    ("\\<^const>==>", impl_ast_tr'),
wenzelm@35145
   535
    ("_index", index_ast_tr')]);
wenzelm@548
   536
wenzelm@14697
   537
fun struct_trfuns structs =
wenzelm@14697
   538
  ([], [("_struct", struct_tr structs)], [], [("_struct", struct_ast_tr' structs)]);
wenzelm@14697
   539
wenzelm@548
   540
end;
wenzelm@42284
   541
wenzelm@42284
   542
structure Basic_Syntax_Trans: BASIC_SYNTAX_TRANS = Syntax_Trans;
wenzelm@42284
   543
open Basic_Syntax_Trans;