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