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