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