src/Pure/Syntax/syntax_trans.ML
author wenzelm
Mon Nov 07 16:41:16 2011 +0100 (2011-11-07)
changeset 45389 bc0d50f8ae19
parent 45057 86c9b73158a8
child 46236 ae79f2978a67
permissions -rw-r--r--
discontinued numbered structure indexes (legacy feature);
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@45389
   234
fun indexdefault_ast_tr [] =
wenzelm@45389
   235
      Ast.Appl [Ast.Constant "_index",
wenzelm@45389
   236
        Ast.Appl [Ast.Constant "_struct", Ast.Constant "_indexdefault"]]
wenzelm@45389
   237
  | indexdefault_ast_tr asts = raise Ast.AST ("indexdefault_ast_tr", asts);
wenzelm@45389
   238
wenzelm@45389
   239
fun indexvar_ast_tr [] = Ast.Appl [Ast.Constant "_index", Ast.Variable "some_index"]
wenzelm@45389
   240
  | indexvar_ast_tr asts = raise Ast.AST ("indexvar_ast_tr", asts);
wenzelm@45389
   241
wenzelm@42278
   242
fun struct_ast_tr [Ast.Appl [Ast.Constant "_index", ast]] = ast
wenzelm@42278
   243
  | struct_ast_tr asts = Ast.mk_appl (Ast.Constant "_struct") asts;
wenzelm@14697
   244
wenzelm@42278
   245
fun index_tr [t] = t
wenzelm@42278
   246
  | index_tr ts = raise TERM ("index_tr", ts);
wenzelm@14697
   247
wenzelm@45389
   248
fun the_struct [] = error "Illegal reference to implicit structure"
wenzelm@45389
   249
  | the_struct (x :: _) = x;
wenzelm@14697
   250
wenzelm@45389
   251
fun struct_tr structs [Const ("_indexdefault", _)] = Syntax.free (the_struct structs)
wenzelm@42278
   252
  | struct_tr _ ts = raise TERM ("struct_tr", ts);
wenzelm@12122
   253
wenzelm@12122
   254
wenzelm@5084
   255
wenzelm@548
   256
(** print (ast) translations **)
wenzelm@548
   257
wenzelm@14647
   258
(* types *)
wenzelm@14647
   259
wenzelm@42247
   260
fun non_typed_tr' f _ ts = f ts;
wenzelm@42247
   261
fun non_typed_tr'' f x _ ts = f x ts;
wenzelm@14647
   262
wenzelm@14647
   263
wenzelm@42262
   264
(* type syntax *)
wenzelm@42262
   265
wenzelm@42262
   266
fun tappl_ast_tr' (f, []) = raise Ast.AST ("tappl_ast_tr'", [f])
wenzelm@42262
   267
  | tappl_ast_tr' (f, [ty]) = Ast.Appl [Ast.Constant "_tapp", ty, f]
wenzelm@42262
   268
  | tappl_ast_tr' (f, ty :: tys) =
wenzelm@42262
   269
      Ast.Appl [Ast.Constant "_tappl", ty, Ast.fold_ast "_types" tys, f];
wenzelm@42262
   270
wenzelm@42262
   271
fun fun_ast_tr' asts =
wenzelm@42262
   272
  if no_brackets () orelse no_type_brackets () then raise Match
wenzelm@42262
   273
  else
wenzelm@42262
   274
    (case Ast.unfold_ast_p "\\<^type>fun" (Ast.Appl (Ast.Constant "\\<^type>fun" :: asts)) of
wenzelm@42262
   275
      (dom as _ :: _ :: _, cod)
wenzelm@42262
   276
        => Ast.Appl [Ast.Constant "_bracket", Ast.fold_ast "_types" dom, cod]
wenzelm@42262
   277
    | _ => raise Match);
wenzelm@42262
   278
wenzelm@42262
   279
wenzelm@548
   280
(* application *)
wenzelm@548
   281
wenzelm@5690
   282
fun appl_ast_tr' (f, []) = raise Ast.AST ("appl_ast_tr'", [f])
wenzelm@5690
   283
  | appl_ast_tr' (f, args) = Ast.Appl [Ast.Constant "_appl", f, Ast.fold_ast "_args" args];
wenzelm@548
   284
wenzelm@5690
   285
fun applC_ast_tr' (f, []) = raise Ast.AST ("applC_ast_tr'", [f])
wenzelm@5690
   286
  | applC_ast_tr' (f, args) = Ast.Appl [Ast.Constant "_applC", f, Ast.fold_ast "_cargs" args];
clasohm@922
   287
wenzelm@548
   288
wenzelm@42085
   289
(* partial eta-contraction before printing *)
wenzelm@42085
   290
wenzelm@42085
   291
fun eta_abs (Abs (a, T, t)) =
wenzelm@42085
   292
      (case eta_abs t of
wenzelm@42085
   293
        t' as Const ("_aprop", _) $ _ => Abs (a, T, t')
wenzelm@42085
   294
      | t' as f $ u =>
wenzelm@42085
   295
          (case eta_abs u of
wenzelm@42085
   296
            Bound 0 =>
wenzelm@42085
   297
              if Term.is_dependent f then Abs (a, T, t')
wenzelm@42085
   298
              else incr_boundvars ~1 f
wenzelm@42085
   299
          | _ => Abs (a, T, t'))
wenzelm@42085
   300
      | t' => Abs (a, T, t'))
wenzelm@42085
   301
  | eta_abs t = t;
wenzelm@42085
   302
wenzelm@42085
   303
val eta_contract_default = Unsynchronized.ref true;
wenzelm@42085
   304
val eta_contract_raw = Config.declare "eta_contract" (fn _ => Config.Bool (! eta_contract_default));
wenzelm@42085
   305
val eta_contract = Config.bool eta_contract_raw;
wenzelm@42085
   306
wenzelm@42085
   307
fun eta_contr ctxt tm =
wenzelm@42085
   308
  if Config.get ctxt eta_contract then eta_abs tm else tm;
wenzelm@42085
   309
wenzelm@42085
   310
wenzelm@548
   311
(* abstraction *)
wenzelm@548
   312
wenzelm@19311
   313
fun mark_boundT (x, T) = Const ("_bound", T --> T) $ Free (x, T);
wenzelm@2698
   314
fun mark_bound x = mark_boundT (x, dummyT);
wenzelm@2698
   315
wenzelm@18958
   316
fun bound_vars vars body =
wenzelm@18958
   317
  subst_bounds (map mark_boundT (Term.rename_wrt_term body vars), body);
wenzelm@18958
   318
wenzelm@548
   319
fun strip_abss vars_of body_of tm =
wenzelm@548
   320
  let
wenzelm@548
   321
    val vars = vars_of tm;
wenzelm@548
   322
    val body = body_of tm;
wenzelm@29276
   323
    val rev_new_vars = Term.rename_wrt_term body vars;
wenzelm@21750
   324
    fun subst (x, T) b =
wenzelm@42083
   325
      if can Name.dest_internal x andalso not (Term.is_dependent b)
wenzelm@21750
   326
      then (Const ("_idtdummy", T), incr_boundvars ~1 b)
wenzelm@21750
   327
      else (mark_boundT (x, T), Term.subst_bound (mark_bound x, b));
wenzelm@21750
   328
    val (rev_vars', body') = fold_map subst rev_new_vars body;
wenzelm@21750
   329
  in (rev rev_vars', body') end;
wenzelm@548
   330
wenzelm@3928
   331
wenzelm@39128
   332
fun abs_tr' ctxt tm =
wenzelm@42476
   333
  uncurry (fold_rev (fn x => fn t => Syntax.const "_abs" $ x $ t))
wenzelm@39128
   334
    (strip_abss strip_abs_vars strip_abs_body (eta_contr ctxt tm));
wenzelm@548
   335
wenzelm@14697
   336
fun atomic_abs_tr' (x, T, t) =
wenzelm@29276
   337
  let val [xT] = Term.rename_wrt_term t [(x, T)]
wenzelm@14697
   338
  in (mark_boundT xT, subst_bound (mark_bound (fst xT), t)) end;
nipkow@13762
   339
wenzelm@42085
   340
fun abs_ast_tr' asts =
wenzelm@5690
   341
  (case Ast.unfold_ast_p "_abs" (Ast.Appl (Ast.Constant "_abs" :: asts)) of
wenzelm@5690
   342
    ([], _) => raise Ast.AST ("abs_ast_tr'", asts)
wenzelm@5690
   343
  | (xs, body) => Ast.Appl [Ast.Constant "_lambda", Ast.fold_ast "_pttrns" xs, body]);
wenzelm@548
   344
wenzelm@42086
   345
fun const_abs_tr' t =
wenzelm@42086
   346
  (case eta_abs t of
wenzelm@42086
   347
    Abs (_, _, t') =>
wenzelm@42086
   348
      if Term.is_dependent t' then raise Match
wenzelm@42086
   349
      else incr_boundvars ~1 t'
wenzelm@42086
   350
  | _ => raise Match);
wenzelm@42086
   351
haftmann@32120
   352
wenzelm@42085
   353
(* binders *)
wenzelm@548
   354
wenzelm@21535
   355
fun mk_binder_tr' (name, syn) =
wenzelm@548
   356
  let
wenzelm@548
   357
    fun mk_idts [] = raise Match    (*abort translation*)
wenzelm@548
   358
      | mk_idts [idt] = idt
wenzelm@42476
   359
      | mk_idts (idt :: idts) = Syntax.const "_idts" $ idt $ mk_idts idts;
wenzelm@548
   360
wenzelm@548
   361
    fun tr' t =
wenzelm@548
   362
      let
wenzelm@548
   363
        val (xs, bd) = strip_abss (strip_qnt_vars name) (strip_qnt_body name) t;
wenzelm@42476
   364
      in Syntax.const syn $ mk_idts xs $ bd end;
wenzelm@548
   365
wenzelm@42476
   366
    fun binder_tr' (t :: ts) = Term.list_comb (tr' (Syntax.const name $ t), ts)
wenzelm@21535
   367
      | binder_tr' [] = raise Match;
wenzelm@21535
   368
  in (name, binder_tr') end;
wenzelm@548
   369
wenzelm@42085
   370
fun preserve_binder_abs_tr' name syn = (name, fn Abs abs :: ts =>
wenzelm@42085
   371
  let val (x, t) = atomic_abs_tr' abs
wenzelm@42476
   372
  in list_comb (Syntax.const syn $ x $ t, ts) end);
wenzelm@42085
   373
wenzelm@42085
   374
fun preserve_binder_abs2_tr' name syn = (name, fn A :: Abs abs :: ts =>
wenzelm@42085
   375
  let val (x, t) = atomic_abs_tr' abs
wenzelm@42476
   376
  in list_comb (Syntax.const syn $ x $ A $ t, ts) end);
wenzelm@42085
   377
wenzelm@548
   378
wenzelm@3691
   379
(* idtyp constraints *)
wenzelm@548
   380
wenzelm@42045
   381
fun idtyp_ast_tr' a [Ast.Appl [Ast.Constant "_constrain", x, ty], xs] =
wenzelm@42045
   382
      Ast.Appl [Ast.Constant a, Ast.Appl [Ast.Constant "_idtyp", x, ty], xs]
wenzelm@3691
   383
  | idtyp_ast_tr' _ _ = raise Match;
wenzelm@548
   384
wenzelm@548
   385
wenzelm@548
   386
(* meta propositions *)
wenzelm@548
   387
wenzelm@4148
   388
fun prop_tr' tm =
wenzelm@548
   389
  let
wenzelm@42476
   390
    fun aprop t = Syntax.const "_aprop" $ t;
wenzelm@548
   391
wenzelm@2698
   392
    fun is_prop Ts t =
wenzelm@2698
   393
      fastype_of1 (Ts, t) = propT handle TERM _ => false;
wenzelm@548
   394
wenzelm@35429
   395
    fun is_term (Const ("Pure.term", _) $ _) = true
wenzelm@19848
   396
      | is_term _ = false;
wenzelm@19848
   397
wenzelm@548
   398
    fun tr' _ (t as Const _) = t
wenzelm@18478
   399
      | tr' Ts (t as Const ("_bound", _) $ u) =
wenzelm@18478
   400
          if is_prop Ts u then aprop t else t
wenzelm@2698
   401
      | tr' _ (t as Free (x, T)) =
wenzelm@42476
   402
          if T = propT then aprop (Syntax.free x) else t
wenzelm@2698
   403
      | tr' _ (t as Var (xi, T)) =
wenzelm@42476
   404
          if T = propT then aprop (Syntax.var xi) else t
wenzelm@2698
   405
      | tr' Ts (t as Bound _) =
wenzelm@2698
   406
          if is_prop Ts t then aprop t else t
wenzelm@2698
   407
      | tr' Ts (Abs (x, T, t)) = Abs (x, T, tr' (T :: Ts) t)
wenzelm@35429
   408
      | tr' Ts (t as t1 $ (t2 as Const ("TYPE", Type ("itself", [T])))) =
wenzelm@28628
   409
          if is_prop Ts t andalso not (is_term t) then Const ("_type_prop", T) $ tr' Ts t1
wenzelm@2698
   410
          else tr' Ts t1 $ tr' Ts t2
wenzelm@2698
   411
      | tr' Ts (t as t1 $ t2) =
wenzelm@5690
   412
          (if is_Const (Term.head_of t) orelse not (is_prop Ts t)
wenzelm@2698
   413
            then I else aprop) (tr' Ts t1 $ tr' Ts t2);
wenzelm@16612
   414
  in tr' [] tm end;
wenzelm@548
   415
wenzelm@2698
   416
wenzelm@548
   417
(* meta implication *)
wenzelm@548
   418
wenzelm@42278
   419
fun impl_ast_tr' asts =
wenzelm@42262
   420
  if no_brackets () then raise Match
wenzelm@10572
   421
  else
wenzelm@35255
   422
    (case Ast.unfold_ast_p "\\<^const>==>" (Ast.Appl (Ast.Constant "\\<^const>==>" :: asts)) of
wenzelm@16612
   423
      (prems as _ :: _ :: _, concl) =>
wenzelm@16612
   424
        let
wenzelm@16612
   425
          val (asms, asm) = split_last prems;
wenzelm@16612
   426
          val asms' = Ast.fold_ast_p "_asms" (asms, Ast.Appl [Ast.Constant "_asm", asm]);
wenzelm@16612
   427
        in Ast.Appl [Ast.Constant "_bigimpl", asms', concl] end
schirmer@15421
   428
    | _ => raise Match);
schirmer@15421
   429
wenzelm@548
   430
wenzelm@548
   431
(* dependent / nondependent quantifiers *)
wenzelm@548
   432
wenzelm@20202
   433
fun var_abs mark (x, T, b) =
wenzelm@43326
   434
  let val (x', _) = Name.variant x (Term.declare_term_names b Name.context)
wenzelm@20202
   435
  in (x', subst_bound (mark (x', T), b)) end;
wenzelm@20202
   436
wenzelm@20202
   437
val variant_abs = var_abs Free;
wenzelm@20202
   438
val variant_abs' = var_abs mark_boundT;
wenzelm@2698
   439
wenzelm@548
   440
fun dependent_tr' (q, r) (A :: Abs (x, T, B) :: ts) =
wenzelm@42083
   441
      if Term.is_dependent B then
wenzelm@2698
   442
        let val (x', B') = variant_abs' (x, dummyT, B);
wenzelm@42476
   443
        in Term.list_comb (Syntax.const q $ mark_boundT (x', T) $ A $ B', ts) end
wenzelm@42476
   444
      else Term.list_comb (Syntax.const r $ A $ incr_boundvars ~1 B, ts)
wenzelm@548
   445
  | dependent_tr' _ _ = raise Match;
wenzelm@548
   446
wenzelm@548
   447
wenzelm@5084
   448
(* quote / antiquote *)
wenzelm@5084
   449
wenzelm@8577
   450
fun antiquote_tr' name =
wenzelm@8577
   451
  let
wenzelm@8577
   452
    fun tr' i (t $ u) =
wenzelm@42476
   453
          if u aconv Bound i then Syntax.const name $ tr' i t
wenzelm@42084
   454
          else tr' i t $ tr' i u
wenzelm@8577
   455
      | tr' i (Abs (x, T, t)) = Abs (x, T, tr' (i + 1) t)
wenzelm@18139
   456
      | tr' i a = if a aconv Bound i then raise Match else a;
wenzelm@8577
   457
  in tr' 0 end;
wenzelm@8577
   458
wenzelm@8577
   459
fun quote_tr' name (Abs (_, _, t)) = Term.incr_boundvars ~1 (antiquote_tr' name t)
wenzelm@8577
   460
  | quote_tr' _ _ = raise Match;
wenzelm@8577
   461
wenzelm@5084
   462
fun quote_antiquote_tr' quoteN antiquoteN name =
wenzelm@5084
   463
  let
wenzelm@42476
   464
    fun tr' (t :: ts) = Term.list_comb (Syntax.const quoteN $ quote_tr' antiquoteN t, ts)
wenzelm@8577
   465
      | tr' _ = raise Match;
wenzelm@8577
   466
  in (name, tr') end;
wenzelm@5084
   467
wenzelm@5084
   468
wenzelm@35145
   469
(* corresponding updates *)
wenzelm@35145
   470
wenzelm@42080
   471
local
wenzelm@42080
   472
wenzelm@42080
   473
fun upd_type (Type ("fun", [Type ("fun", [_, T]), _])) = T
wenzelm@42080
   474
  | upd_type _ = dummyT;
wenzelm@42080
   475
wenzelm@35145
   476
fun upd_tr' (x_upd, T) =
wenzelm@35145
   477
  (case try (unsuffix "_update") x_upd of
wenzelm@42080
   478
    SOME x => (x, upd_type T)
wenzelm@35145
   479
  | NONE => raise Match);
wenzelm@35145
   480
wenzelm@42080
   481
in
wenzelm@42080
   482
wenzelm@35145
   483
fun update_name_tr' (Free x) = Free (upd_tr' x)
wenzelm@35145
   484
  | update_name_tr' ((c as Const ("_free", _)) $ Free x) = c $ Free (upd_tr' x)
wenzelm@35145
   485
  | update_name_tr' (Const x) = Const (upd_tr' x)
wenzelm@35145
   486
  | update_name_tr' _ = raise Match;
wenzelm@35145
   487
wenzelm@42080
   488
end;
wenzelm@42080
   489
wenzelm@35145
   490
wenzelm@14697
   491
(* indexed syntax *)
wenzelm@548
   492
wenzelm@42278
   493
fun index_ast_tr' [Ast.Appl [Ast.Constant "_struct", ast]] = ast
wenzelm@14697
   494
  | index_ast_tr' _ = raise Match;
wenzelm@14697
   495
wenzelm@45389
   496
fun struct_ast_tr' structs [Ast.Constant "_indexdefault"] =
wenzelm@45389
   497
      Ast.Appl [Ast.Constant "_free",
wenzelm@45389
   498
        Ast.Variable (the_struct structs handle ERROR _ => raise Match)]
wenzelm@14697
   499
  | struct_ast_tr' _ _ = raise Match;
wenzelm@14697
   500
wenzelm@14697
   501
wenzelm@14697
   502
wenzelm@14697
   503
(** Pure translations **)
wenzelm@548
   504
wenzelm@548
   505
val pure_trfuns =
wenzelm@42057
   506
  ([("_strip_positions", strip_positions_ast_tr),
wenzelm@42057
   507
    ("_constify", constify_ast_tr),
wenzelm@42262
   508
    ("_tapp", tapp_ast_tr),
wenzelm@42262
   509
    ("_tappl", tappl_ast_tr),
wenzelm@42262
   510
    ("_bracket", bracket_ast_tr),
wenzelm@35145
   511
    ("_appl", appl_ast_tr),
wenzelm@35145
   512
    ("_applC", applC_ast_tr),
wenzelm@35145
   513
    ("_lambda", lambda_ast_tr),
wenzelm@35145
   514
    ("_idtyp", idtyp_ast_tr),
wenzelm@35145
   515
    ("_idtypdummy", idtypdummy_ast_tr),
wenzelm@35145
   516
    ("_bigimpl", bigimpl_ast_tr),
wenzelm@35145
   517
    ("_indexdefault", indexdefault_ast_tr),
wenzelm@35145
   518
    ("_indexvar", indexvar_ast_tr),
wenzelm@35145
   519
    ("_struct", struct_ast_tr)],
wenzelm@35145
   520
   [("_abs", abs_tr),
wenzelm@35145
   521
    ("_aprop", aprop_tr),
wenzelm@35145
   522
    ("_ofclass", ofclass_tr),
wenzelm@35145
   523
    ("_sort_constraint", sort_constraint_tr),
wenzelm@35145
   524
    ("_TYPE", type_tr),
wenzelm@35145
   525
    ("_DDDOT", dddot_tr),
wenzelm@35145
   526
    ("_update_name", update_name_tr),
wenzelm@35145
   527
    ("_index", index_tr)],
wenzelm@35198
   528
   ([]: (string * (term list -> term)) list),
wenzelm@42262
   529
   [("\\<^type>fun", fun_ast_tr'),
wenzelm@42262
   530
    ("_abs", abs_ast_tr'),
wenzelm@35145
   531
    ("_idts", idtyp_ast_tr' "_idts"),
wenzelm@35145
   532
    ("_pttrns", idtyp_ast_tr' "_pttrns"),
wenzelm@35255
   533
    ("\\<^const>==>", impl_ast_tr'),
wenzelm@35145
   534
    ("_index", index_ast_tr')]);
wenzelm@548
   535
wenzelm@14697
   536
fun struct_trfuns structs =
wenzelm@14697
   537
  ([], [("_struct", struct_tr structs)], [], [("_struct", struct_ast_tr' structs)]);
wenzelm@14697
   538
wenzelm@548
   539
end;
wenzelm@42284
   540
wenzelm@42284
   541
structure Basic_Syntax_Trans: BASIC_SYNTAX_TRANS = Syntax_Trans;
wenzelm@42284
   542
open Basic_Syntax_Trans;