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