src/Pure/Isar/attrib.ML
author wenzelm
Thu Jan 19 21:22:08 2006 +0100 (2006-01-19)
changeset 18708 4b3dadb4fe33
parent 18678 dd0c569fa43d
child 18734 f5ea6b0d3501
permissions -rw-r--r--
setup: theory -> theory;
wenzelm@5823
     1
(*  Title:      Pure/Isar/attrib.ML
wenzelm@5823
     2
    ID:         $Id$
wenzelm@5823
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@5823
     4
wenzelm@5823
     5
Symbolic theorem attributes.
wenzelm@5823
     6
*)
wenzelm@5823
     7
wenzelm@5823
     8
signature BASIC_ATTRIB =
wenzelm@5823
     9
sig
wenzelm@5823
    10
  val print_attributes: theory -> unit
wenzelm@16458
    11
  val Attribute: bstring ->
wenzelm@16458
    12
    (Args.src -> theory attribute) * (Args.src -> ProofContext.context attribute) ->
wenzelm@16458
    13
    string -> unit
wenzelm@5823
    14
end;
wenzelm@5823
    15
wenzelm@5823
    16
signature ATTRIB =
wenzelm@5823
    17
sig
wenzelm@5823
    18
  include BASIC_ATTRIB
wenzelm@18636
    19
  val rule: ('a -> thm -> thm) -> 'a attribute
wenzelm@18636
    20
  val declaration: (thm -> 'a -> 'a) -> 'a attribute
wenzelm@15703
    21
  type src
wenzelm@18636
    22
  val theory: Context.generic attribute -> theory attribute
wenzelm@18636
    23
  val context: Context.generic attribute -> ProofContext.context attribute
wenzelm@18636
    24
  val generic: theory attribute * ProofContext.context attribute -> Context.generic attribute
wenzelm@18636
    25
  val common: (src -> Context.generic attribute) ->
wenzelm@18636
    26
    (src -> theory attribute) * (src -> ProofContext.context attribute)
wenzelm@5912
    27
  exception ATTRIB_FAIL of (string * Position.T) * exn
wenzelm@16458
    28
  val intern: theory -> xstring -> string
wenzelm@16458
    29
  val intern_src: theory -> src -> src
wenzelm@15703
    30
  val global_attribute_i: theory -> src -> theory attribute
wenzelm@15703
    31
  val global_attribute: theory -> src -> theory attribute
wenzelm@15703
    32
  val local_attribute_i: theory -> src -> ProofContext.context attribute
wenzelm@15703
    33
  val local_attribute: theory -> src -> ProofContext.context attribute
wenzelm@15703
    34
  val context_attribute_i: ProofContext.context -> Args.src -> ProofContext.context attribute
wenzelm@15703
    35
  val context_attribute: ProofContext.context -> Args.src -> ProofContext.context attribute
wenzelm@7673
    36
  val undef_global_attribute: theory attribute
wenzelm@15703
    37
  val undef_local_attribute: ProofContext.context attribute
wenzelm@18636
    38
  val generic_attribute_i: theory -> src -> Context.generic attribute
wenzelm@18636
    39
  val generic_attribute: theory -> src -> Context.generic attribute
wenzelm@17105
    40
  val map_specs: ('a -> 'b attribute) ->
wenzelm@17105
    41
    (('c * 'a list) * 'd) list -> (('c * 'b attribute list) * 'd) list
wenzelm@17105
    42
  val map_facts: ('a -> 'b attribute) ->
wenzelm@17105
    43
    (('c * 'a list) * ('d * 'a list) list) list ->
wenzelm@17105
    44
    (('c * 'b attribute list) * ('d * 'b attribute list) list) list
wenzelm@15703
    45
  val crude_closure: ProofContext.context -> src -> src
wenzelm@15703
    46
  val add_attributes: (bstring * ((src -> theory attribute) *
wenzelm@15703
    47
      (src -> ProofContext.context attribute)) * string) list -> theory -> theory
wenzelm@6091
    48
  val global_thm: theory * Args.T list -> thm * (theory * Args.T list)
wenzelm@6091
    49
  val global_thms: theory * Args.T list -> thm list * (theory * Args.T list)
wenzelm@6091
    50
  val global_thmss: theory * Args.T list -> thm list * (theory * Args.T list)
wenzelm@16458
    51
  val local_thm: ProofContext.context * Args.T list ->
wenzelm@16458
    52
    thm * (ProofContext.context * Args.T list)
wenzelm@16458
    53
  val local_thms: ProofContext.context * Args.T list ->
wenzelm@16458
    54
    thm list * (ProofContext.context * Args.T list)
wenzelm@16458
    55
  val local_thmss: ProofContext.context * Args.T list ->
wenzelm@16458
    56
    thm list * (ProofContext.context * Args.T list)
wenzelm@18636
    57
  val thm: Context.generic * Args.T list -> thm * (Context.generic * Args.T list)
wenzelm@18636
    58
  val thms: Context.generic * Args.T list -> thm list * (Context.generic * Args.T list)
wenzelm@18636
    59
  val thmss: Context.generic * Args.T list -> thm list * (Context.generic * Args.T list)
wenzelm@15703
    60
  val syntax: ('a * Args.T list -> 'a attribute * ('a * Args.T list)) -> src -> 'a attribute
wenzelm@15703
    61
  val no_args: 'a attribute -> src -> 'a attribute
wenzelm@15703
    62
  val add_del_args: 'a attribute -> 'a attribute -> src -> 'a attribute
wenzelm@18636
    63
  val attribute: Context.generic attribute -> src
wenzelm@5823
    64
end;
wenzelm@5823
    65
wenzelm@5823
    66
structure Attrib: ATTRIB =
wenzelm@5823
    67
struct
wenzelm@5823
    68
wenzelm@18636
    69
val rule = Drule.rule_attribute;
wenzelm@18636
    70
val declaration = Drule.declaration_attribute;
wenzelm@18636
    71
wenzelm@15703
    72
type src = Args.src;
wenzelm@15703
    73
wenzelm@5823
    74
wenzelm@18636
    75
wenzelm@18636
    76
(** generic attributes **)
wenzelm@18636
    77
wenzelm@18636
    78
fun generic (global_att, local_att) =
wenzelm@18636
    79
   fn (Context.Theory thy, th) => apfst Context.Theory (global_att (thy, th))
wenzelm@18636
    80
    | (Context.Proof ctxt, th) => apfst Context.Proof (local_att (ctxt, th));
wenzelm@18636
    81
wenzelm@18636
    82
fun theory att (thy, th) = apfst Context.the_theory (att (Context.Theory thy, th));
wenzelm@18636
    83
fun context att (ctxt, th) = apfst Context.the_proof (att (Context.Proof ctxt, th));
wenzelm@18636
    84
wenzelm@18636
    85
fun common att = (theory o att, context o att);
wenzelm@18636
    86
wenzelm@18636
    87
wenzelm@18636
    88
wenzelm@5823
    89
(** attributes theory data **)
wenzelm@5823
    90
wenzelm@16458
    91
(* datatype attributes *)
wenzelm@5823
    92
wenzelm@16458
    93
structure AttributesData = TheoryDataFun
wenzelm@16458
    94
(struct
wenzelm@5823
    95
  val name = "Isar/attributes";
wenzelm@5823
    96
  type T =
wenzelm@16344
    97
    ((((src -> theory attribute) * (src -> ProofContext.context attribute))
wenzelm@16344
    98
        * string) * stamp) NameSpace.table;
wenzelm@5823
    99
wenzelm@16344
   100
  val empty = NameSpace.empty_table;
wenzelm@6546
   101
  val copy = I;
wenzelm@16458
   102
  val extend = I;
wenzelm@5823
   103
haftmann@17496
   104
  fun merge _ tables = NameSpace.merge_tables (eq_snd (op =)) tables handle Symtab.DUPS dups =>
wenzelm@16344
   105
    error ("Attempt to merge different versions of attribute(s) " ^ commas_quote dups);
wenzelm@5823
   106
wenzelm@16344
   107
  fun print _ attribs =
wenzelm@5823
   108
    let
wenzelm@5823
   109
      fun prt_attr (name, ((_, comment), _)) = Pretty.block
wenzelm@6846
   110
        [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
wenzelm@5823
   111
    in
wenzelm@16344
   112
      [Pretty.big_list "attributes:" (map prt_attr (NameSpace.extern_table attribs))]
wenzelm@9216
   113
      |> Pretty.chunks |> Pretty.writeln
wenzelm@5823
   114
    end;
wenzelm@16458
   115
end);
wenzelm@5823
   116
wenzelm@18708
   117
val _ = Context.add_setup AttributesData.init;
wenzelm@5823
   118
val print_attributes = AttributesData.print;
wenzelm@7611
   119
wenzelm@5823
   120
wenzelm@15703
   121
(* interning *)
wenzelm@15703
   122
wenzelm@16458
   123
val intern = NameSpace.intern o #1 o AttributesData.get;
wenzelm@15703
   124
val intern_src = Args.map_name o intern;
wenzelm@15703
   125
wenzelm@15703
   126
wenzelm@5823
   127
(* get global / local attributes *)
wenzelm@5823
   128
wenzelm@5912
   129
exception ATTRIB_FAIL of (string * Position.T) * exn;
wenzelm@5912
   130
wenzelm@5823
   131
fun gen_attribute which thy =
wenzelm@5823
   132
  let
wenzelm@16344
   133
    val attrs = #2 (AttributesData.get thy);
wenzelm@5879
   134
    fun attr src =
wenzelm@16344
   135
      let val ((name, _), pos) = Args.dest_src src in
wenzelm@17412
   136
        (case Symtab.lookup attrs name of
skalberg@15531
   137
          NONE => error ("Unknown attribute: " ^ quote name ^ Position.str_of pos)
skalberg@15531
   138
        | SOME ((p, _), _) => transform_failure (curry ATTRIB_FAIL (name, pos)) (which p src))
wenzelm@5823
   139
      end;
wenzelm@5823
   140
  in attr end;
wenzelm@5823
   141
wenzelm@15703
   142
val global_attribute_i = gen_attribute fst;
wenzelm@16458
   143
fun global_attribute thy = global_attribute_i thy o intern_src thy;
wenzelm@15703
   144
wenzelm@15703
   145
val local_attribute_i = gen_attribute snd;
wenzelm@16458
   146
fun local_attribute thy = local_attribute_i thy o intern_src thy;
wenzelm@15703
   147
wenzelm@15703
   148
val context_attribute_i = local_attribute_i o ProofContext.theory_of;
wenzelm@15703
   149
val context_attribute = local_attribute o ProofContext.theory_of;
wenzelm@5823
   150
wenzelm@7673
   151
val undef_global_attribute: theory attribute =
wenzelm@7673
   152
  fn _ => error "attribute undefined in theory context";
wenzelm@7673
   153
wenzelm@15703
   154
val undef_local_attribute: ProofContext.context attribute =
wenzelm@7673
   155
  fn _ => error "attribute undefined in proof context";
wenzelm@7673
   156
wenzelm@18636
   157
fun generic_attribute thy src = generic (global_attribute thy src, local_attribute thy src);
wenzelm@18636
   158
fun generic_attribute_i thy src = generic (global_attribute_i thy src, local_attribute_i thy src);
wenzelm@18636
   159
wenzelm@5823
   160
wenzelm@17105
   161
(* attributed declarations *)
wenzelm@17105
   162
wenzelm@17105
   163
fun map_specs f = map (apfst (apsnd (map f)));
wenzelm@17105
   164
fun map_facts f = map (apfst (apsnd (map f)) o apsnd (map (apsnd (map f))));
wenzelm@17105
   165
wenzelm@17105
   166
wenzelm@15703
   167
(* crude_closure *)
wenzelm@15703
   168
wenzelm@15703
   169
(*Produce closure without knowing facts in advance! The following
wenzelm@15703
   170
  should work reasonably well for attribute parsers that do not peek
wenzelm@15703
   171
  at the thm structure.*)
wenzelm@15703
   172
wenzelm@15703
   173
fun crude_closure ctxt src =
wenzelm@18678
   174
 (try (fn () => context_attribute_i ctxt src (ctxt, Drule.asm_rl)) ();
wenzelm@15703
   175
  Args.closure src);
wenzelm@15703
   176
wenzelm@15703
   177
wenzelm@5823
   178
(* add_attributes *)
wenzelm@5823
   179
wenzelm@5823
   180
fun add_attributes raw_attrs thy =
wenzelm@5823
   181
  let
wenzelm@16141
   182
    val new_attrs = raw_attrs |> map (fn (name, (f, g), comment) =>
wenzelm@16344
   183
      (name, (((f, g), comment), stamp ())));
wenzelm@16458
   184
    fun add attrs = NameSpace.extend_table (Sign.naming_of thy) (attrs, new_attrs)
wenzelm@16344
   185
      handle Symtab.DUPS dups =>
wenzelm@16344
   186
        error ("Duplicate declaration of attributes(s) " ^ commas_quote dups);
wenzelm@16344
   187
  in AttributesData.map add thy end;
wenzelm@5823
   188
wenzelm@5879
   189
(*implicit version*)
wenzelm@5879
   190
fun Attribute name att cmt = Context.>> (add_attributes [(name, att, cmt)]);
wenzelm@5823
   191
wenzelm@5879
   192
wenzelm@5879
   193
wenzelm@5879
   194
(** attribute parsers **)
wenzelm@5879
   195
wenzelm@5879
   196
(* tags *)
wenzelm@5823
   197
wenzelm@5879
   198
fun tag x = Scan.lift (Args.name -- Scan.repeat Args.name) x;
wenzelm@5879
   199
wenzelm@5879
   200
wenzelm@5879
   201
(* theorems *)
wenzelm@5879
   202
wenzelm@18636
   203
local
wenzelm@18636
   204
wenzelm@15703
   205
fun gen_thm theory_of attrib get pick = Scan.depend (fn st =>
wenzelm@18037
   206
 (Scan.ahead Args.alt_name -- Args.named_fact (get st o Fact)
wenzelm@18037
   207
    >> (fn (s, fact) => ("", Fact s, fact)) ||
wenzelm@18037
   208
  Scan.ahead Args.name -- Args.named_fact (get st o Name) -- Args.thm_sel
wenzelm@18037
   209
    >> (fn ((name, fact), sel) => (name, NameSelection (name, sel), fact)) ||
wenzelm@18037
   210
  Scan.ahead Args.name -- Args.named_fact (get st o Name)
wenzelm@18037
   211
    >> (fn (name, fact) => (name, Name name, fact))) --
wenzelm@18037
   212
  Args.opt_attribs (intern (theory_of st))
wenzelm@18037
   213
  >> (fn ((name, thmref, fact), srcs) =>
wenzelm@15703
   214
    let
wenzelm@16498
   215
      val ths = PureThy.select_thm thmref fact;
wenzelm@15703
   216
      val atts = map (attrib (theory_of st)) srcs;
haftmann@18418
   217
      val (st', ths') = Thm.applys_attributes atts (st, ths);
wenzelm@15703
   218
    in (st', pick name ths') end));
berghofe@15456
   219
wenzelm@18636
   220
val get_thms = Context.cases PureThy.get_thms ProofContext.get_thms;
wenzelm@18636
   221
wenzelm@18636
   222
in
wenzelm@18636
   223
wenzelm@15703
   224
val global_thm = gen_thm I global_attribute_i PureThy.get_thms PureThy.single_thm;
wenzelm@15703
   225
val global_thms = gen_thm I global_attribute_i PureThy.get_thms (K I);
skalberg@15570
   226
val global_thmss = Scan.repeat global_thms >> List.concat;
wenzelm@5879
   227
wenzelm@15703
   228
val local_thm =
wenzelm@15703
   229
  gen_thm ProofContext.theory_of local_attribute_i ProofContext.get_thms PureThy.single_thm;
wenzelm@15703
   230
val local_thms =
wenzelm@15703
   231
  gen_thm ProofContext.theory_of local_attribute_i ProofContext.get_thms (K I);
skalberg@15570
   232
val local_thmss = Scan.repeat local_thms >> List.concat;
wenzelm@5879
   233
wenzelm@18636
   234
val thm = gen_thm Context.theory_of generic_attribute_i get_thms PureThy.single_thm;
wenzelm@18636
   235
val thms = gen_thm Context.theory_of generic_attribute_i get_thms (K I);
wenzelm@18636
   236
val thmss = Scan.repeat thms >> List.concat;
wenzelm@18636
   237
wenzelm@18636
   238
end;
wenzelm@18636
   239
wenzelm@5823
   240
wenzelm@5879
   241
wenzelm@5879
   242
(** attribute syntax **)
wenzelm@5823
   243
wenzelm@5879
   244
fun syntax scan src (st, th) =
wenzelm@8282
   245
  let val (st', f) = Args.syntax "attribute" scan src st
wenzelm@5879
   246
  in f (st', th) end;
wenzelm@5879
   247
wenzelm@5879
   248
fun no_args x = syntax (Scan.succeed x);
wenzelm@5823
   249
wenzelm@10034
   250
fun add_del_args add del x = syntax
wenzelm@10034
   251
  (Scan.lift (Args.add >> K add || Args.del >> K del || Scan.succeed add)) x;
wenzelm@8633
   252
wenzelm@5823
   253
wenzelm@5823
   254
wenzelm@18636
   255
(** basic attributes **)
wenzelm@5823
   256
wenzelm@5823
   257
(* tags *)
wenzelm@5823
   258
wenzelm@18636
   259
fun tagged x = syntax (tag >> Drule.tag) x;
wenzelm@18636
   260
fun untagged x = syntax (Scan.lift Args.name >> Drule.untag) x;
wenzelm@6772
   261
wenzelm@6772
   262
wenzelm@18636
   263
(* rule composition *)
wenzelm@5879
   264
wenzelm@18636
   265
val COMP_att =
wenzelm@18636
   266
  syntax (Scan.lift (Scan.optional (Args.bracks Args.nat) 1) -- thm
wenzelm@18636
   267
    >> (fn (i, B) => Drule.rule_attribute (fn _ => fn A => Drule.compose_single (A, i, B))));
wenzelm@5879
   268
wenzelm@18636
   269
val THEN_att =
wenzelm@18636
   270
  syntax (Scan.lift (Scan.optional (Args.bracks Args.nat) 1) -- thm
wenzelm@18636
   271
    >> (fn (i, B) => Drule.rule_attribute (fn _ => fn A => A RSN (i, B))));
wenzelm@5879
   272
wenzelm@18636
   273
val OF_att =
wenzelm@18636
   274
  syntax (thmss >> (fn Bs => Drule.rule_attribute (fn _ => fn A => Bs MRS A)));
wenzelm@5879
   275
wenzelm@5879
   276
wenzelm@15703
   277
(* read_instantiate: named instantiation of type and term variables *)
wenzelm@14718
   278
wenzelm@15703
   279
local
wenzelm@5879
   280
wenzelm@15703
   281
fun is_tvar (x, _) = (case Symbol.explode x of "'" :: _ => true | _ => false);
wenzelm@15703
   282
wenzelm@15703
   283
fun error_var msg xi = error (msg ^ Syntax.string_of_vname xi);
wenzelm@15703
   284
wenzelm@15973
   285
fun the_sort sorts xi = the (sorts xi)
wenzelm@15703
   286
  handle Option.Option => error_var "No such type variable in theorem: " xi;
wenzelm@10807
   287
wenzelm@15973
   288
fun the_type types xi = the (types xi)
wenzelm@15703
   289
  handle Option.Option => error_var "No such variable in theorem: " xi;
ballarin@14287
   290
wenzelm@16458
   291
fun unify_types thy types ((unifier, maxidx), (xi, u)) =
wenzelm@15703
   292
  let
wenzelm@15703
   293
    val T = the_type types xi;
wenzelm@15703
   294
    val U = Term.fastype_of u;
wenzelm@15703
   295
    val maxidx' = Int.max (maxidx, Int.max (#2 xi, Term.maxidx_of_term u));
wenzelm@15703
   296
  in
wenzelm@16934
   297
    Sign.typ_unify thy (T, U) (unifier, maxidx')
wenzelm@15703
   298
      handle Type.TUNIFY => error_var "Incompatible type for instantiation of " xi
wenzelm@15703
   299
  end;
wenzelm@15703
   300
wenzelm@15703
   301
fun typ_subst env = apsnd (Term.typ_subst_TVars env);
wenzelm@15703
   302
fun subst env = apsnd (Term.subst_TVars env);
ballarin@14287
   303
wenzelm@16458
   304
fun instantiate thy envT env thm =
wenzelm@15703
   305
  let
berghofe@15798
   306
    val (_, sorts) = Drule.types_sorts thm;
wenzelm@16458
   307
    fun prepT (a, T) = (Thm.ctyp_of thy (TVar (a, the_sort sorts a)), Thm.ctyp_of thy T);
wenzelm@16458
   308
    fun prep (xi, t) = pairself (Thm.cterm_of thy) (Var (xi, Term.fastype_of t), t);
wenzelm@15703
   309
  in
wenzelm@15703
   310
    Drule.instantiate (map prepT (distinct envT),
berghofe@15798
   311
      map prep (gen_distinct (fn ((xi, t), (yj, u)) => xi = yj andalso t aconv u) env)) thm
wenzelm@15703
   312
  end;
wenzelm@15703
   313
wenzelm@15703
   314
in
wenzelm@15703
   315
wenzelm@18636
   316
fun read_instantiate mixed_insts (generic, thm) =
wenzelm@15703
   317
  let
wenzelm@18636
   318
    val thy = Context.theory_of generic;
wenzelm@18636
   319
    val ctxt = Context.proof_of generic;
ballarin@14287
   320
wenzelm@17756
   321
    val (type_insts, term_insts) = List.partition (is_tvar o fst) (map snd mixed_insts);
wenzelm@15703
   322
    val internal_insts = term_insts |> List.mapPartial
wenzelm@15703
   323
      (fn (xi, Args.Term t) => SOME (xi, t)
wenzelm@15703
   324
      | (_, Args.Name _) => NONE
wenzelm@15703
   325
      | (xi, _) => error_var "Term argument expected for " xi);
wenzelm@15703
   326
    val external_insts = term_insts |> List.mapPartial
wenzelm@15703
   327
      (fn (xi, Args.Name s) => SOME (xi, s) | _ => NONE);
wenzelm@5879
   328
wenzelm@15703
   329
wenzelm@15703
   330
    (* type instantiations *)
wenzelm@5879
   331
wenzelm@15703
   332
    val sorts = #2 (Drule.types_sorts thm);
wenzelm@14718
   333
wenzelm@15703
   334
    fun readT (xi, arg) =
wenzelm@15703
   335
      let
wenzelm@15703
   336
        val S = the_sort sorts xi;
wenzelm@15703
   337
        val T =
wenzelm@15703
   338
          (case arg of
wenzelm@15703
   339
            Args.Name s => ProofContext.read_typ ctxt s
wenzelm@15703
   340
          | Args.Typ T => T
wenzelm@15703
   341
          | _ => error_var "Type argument expected for " xi);
wenzelm@10807
   342
      in
wenzelm@16458
   343
        if Sign.of_sort thy (T, S) then (xi, T)
wenzelm@15703
   344
        else error_var "Incompatible sort for typ instantiation of " xi
wenzelm@10807
   345
      end;
wenzelm@5879
   346
wenzelm@15703
   347
    val type_insts' = map readT type_insts;
wenzelm@16458
   348
    val thm' = instantiate thy type_insts' [] thm;
wenzelm@15703
   349
wenzelm@15703
   350
wenzelm@15703
   351
    (* internal term instantiations *)
wenzelm@15703
   352
wenzelm@15703
   353
    val types' = #1 (Drule.types_sorts thm');
berghofe@15798
   354
    val unifier = map (apsnd snd) (Vartab.dest (#1
wenzelm@16458
   355
      (Library.foldl (unify_types thy types') ((Vartab.empty, 0), internal_insts))));
wenzelm@15703
   356
wenzelm@15703
   357
    val type_insts'' = map (typ_subst unifier) type_insts';
wenzelm@15703
   358
    val internal_insts'' = map (subst unifier) internal_insts;
wenzelm@16458
   359
    val thm'' = instantiate thy unifier internal_insts'' thm';
wenzelm@15703
   360
wenzelm@15703
   361
wenzelm@15703
   362
    (* external term instantiations *)
wenzelm@15703
   363
wenzelm@15703
   364
    val types'' = #1 (Drule.types_sorts thm'');
wenzelm@15703
   365
wenzelm@15703
   366
    val (xs, ss) = split_list external_insts;
wenzelm@15703
   367
    val Ts = map (the_type types'') xs;
wenzelm@15703
   368
    val (ts, inferred) = ProofContext.read_termTs ctxt (K false)
wenzelm@15703
   369
        (K NONE) (K NONE) (Drule.add_used thm'' []) (ss ~~ Ts);
wenzelm@15703
   370
wenzelm@15703
   371
    val type_insts''' = map (typ_subst inferred) type_insts'';
wenzelm@15703
   372
    val internal_insts''' = map (subst inferred) internal_insts'';
wenzelm@5879
   373
wenzelm@15703
   374
    val external_insts''' = xs ~~ ts;
wenzelm@15703
   375
    val term_insts''' = internal_insts''' @ external_insts''';
wenzelm@16458
   376
    val thm''' = instantiate thy inferred external_insts''' thm'';
wenzelm@17184
   377
wenzelm@15703
   378
wenzelm@15703
   379
    (* assign internalized values *)
wenzelm@15703
   380
wenzelm@17184
   381
    val _ =
wenzelm@17184
   382
      mixed_insts |> List.app (fn (arg, (xi, _)) =>
wenzelm@17184
   383
        if is_tvar xi then
wenzelm@17184
   384
          Args.assign (SOME (Args.Typ (the (AList.lookup (op =) type_insts''' xi)))) arg
wenzelm@17184
   385
        else
wenzelm@17184
   386
          Args.assign (SOME (Args.Term (the (AList.lookup (op =) term_insts''' xi)))) arg);
wenzelm@15703
   387
wenzelm@18636
   388
  in (generic, thm''' |> RuleCases.save thm) end;
wenzelm@15703
   389
wenzelm@15703
   390
end;
wenzelm@15703
   391
wenzelm@15703
   392
wenzelm@15703
   393
(* where: named instantiation *)
wenzelm@15703
   394
wenzelm@15703
   395
local
wenzelm@15703
   396
wenzelm@15703
   397
val value =
wenzelm@15703
   398
  Args.internal_typ >> Args.Typ ||
wenzelm@15703
   399
  Args.internal_term >> Args.Term ||
wenzelm@15703
   400
  Args.name >> Args.Name;
wenzelm@15703
   401
wenzelm@15703
   402
val inst = Args.var -- (Args.$$$ "=" |-- Args.ahead -- value)
wenzelm@15703
   403
  >> (fn (xi, (a, v)) => (a, (xi, v)));
wenzelm@15703
   404
wenzelm@15703
   405
in
wenzelm@5823
   406
wenzelm@18636
   407
val where_att = syntax (Args.and_list (Scan.lift inst) >> read_instantiate);
wenzelm@5879
   408
wenzelm@15703
   409
end;
wenzelm@5879
   410
wenzelm@15703
   411
wenzelm@15703
   412
(* of: positional instantiation (term arguments only) *)
wenzelm@15703
   413
wenzelm@15703
   414
local
wenzelm@5912
   415
wenzelm@18636
   416
fun read_instantiate' (args, concl_args) (generic, thm) =
wenzelm@15703
   417
  let
wenzelm@15703
   418
    fun zip_vars _ [] = []
wenzelm@15703
   419
      | zip_vars (_ :: xs) ((_, NONE) :: rest) = zip_vars xs rest
wenzelm@15703
   420
      | zip_vars ((x, _) :: xs) ((a, SOME t) :: rest) = (a, (x, t)) :: zip_vars xs rest
wenzelm@15703
   421
      | zip_vars [] _ = error "More instantiations than variables in theorem";
wenzelm@15703
   422
    val insts =
wenzelm@15703
   423
      zip_vars (Drule.vars_of_terms [Thm.prop_of thm]) args @
wenzelm@15703
   424
      zip_vars (Drule.vars_of_terms [Thm.concl_of thm]) concl_args;
wenzelm@18636
   425
  in read_instantiate insts (generic, thm) end;
wenzelm@5912
   426
wenzelm@15703
   427
val value =
wenzelm@15703
   428
  Args.internal_term >> Args.Term ||
wenzelm@15703
   429
  Args.name >> Args.Name;
wenzelm@15703
   430
wenzelm@15703
   431
val inst = Args.ahead -- Args.maybe value;
wenzelm@10807
   432
val concl = Args.$$$ "concl" -- Args.colon;
wenzelm@5912
   433
wenzelm@15703
   434
val insts =
wenzelm@15703
   435
  Scan.repeat (Scan.unless concl inst) --
wenzelm@15703
   436
  Scan.optional (concl |-- Scan.repeat inst) [];
wenzelm@15703
   437
wenzelm@15703
   438
in
wenzelm@5912
   439
wenzelm@18636
   440
val of_att = syntax (Scan.lift insts >> read_instantiate');
wenzelm@5912
   441
wenzelm@15703
   442
end;
wenzelm@15703
   443
wenzelm@5912
   444
berghofe@13782
   445
(* rename_abs *)
berghofe@13782
   446
berghofe@13782
   447
fun rename_abs src = syntax
wenzelm@15703
   448
  (Scan.lift (Scan.repeat (Args.maybe Args.name) >> (apsnd o Drule.rename_bvars'))) src;
berghofe@13782
   449
berghofe@13782
   450
wenzelm@7598
   451
(* unfold / fold definitions *)
wenzelm@7598
   452
wenzelm@18636
   453
val unfolded = syntax (thmss >> (fn defs => Drule.rule_attribute (K (Tactic.rewrite_rule defs))));
wenzelm@18636
   454
val folded = syntax (thmss >> (fn defs => Drule.rule_attribute (K (Tactic.fold_rule defs))));
wenzelm@7598
   455
wenzelm@7598
   456
wenzelm@8368
   457
(* rule cases *)
wenzelm@8368
   458
wenzelm@10528
   459
fun consumes x = syntax (Scan.lift (Scan.optional Args.nat 1) >> RuleCases.consumes) x;
wenzelm@8368
   460
fun case_names x = syntax (Scan.lift (Scan.repeat1 Args.name) >> RuleCases.case_names) x;
wenzelm@18236
   461
fun case_conclusion x =
wenzelm@18236
   462
  syntax (Scan.lift (Args.name -- Scan.repeat Args.name) >> RuleCases.case_conclusion) x;
wenzelm@8368
   463
fun params x = syntax (Args.and_list1 (Scan.lift (Scan.repeat Args.name)) >> RuleCases.params) x;
wenzelm@8368
   464
wenzelm@8368
   465
wenzelm@11770
   466
(* rule_format *)
wenzelm@11770
   467
wenzelm@15703
   468
fun rule_format_att x = syntax (Args.mode "no_asm"
wenzelm@15703
   469
  >> (fn true => ObjectLogic.rule_format_no_asm | false => ObjectLogic.rule_format)) x;
wenzelm@11770
   470
wenzelm@11770
   471
wenzelm@5879
   472
(* misc rules *)
wenzelm@5879
   473
wenzelm@6091
   474
fun standard x = no_args (Drule.rule_attribute (K Drule.standard)) x;
wenzelm@9941
   475
fun elim_format x = no_args (Drule.rule_attribute (K Tactic.make_elim)) x;
wenzelm@9216
   476
fun no_vars x = no_args (Drule.rule_attribute (K (#1 o Drule.freeze_thaw))) x;
berghofe@15926
   477
fun eta_long x = no_args (Drule.rule_attribute (K (Drule.fconv_rule Drule.eta_long_conversion))) x;
wenzelm@5879
   478
wenzelm@5879
   479
wenzelm@15703
   480
(* internal attribute *)
wenzelm@15703
   481
wenzelm@15828
   482
fun attribute att = Args.src (("Pure.attribute", [Args.mk_attribute att]), Position.none);
wenzelm@15703
   483
wenzelm@18636
   484
fun internal_att x = syntax (Scan.lift Args.internal_attribute) x;
wenzelm@15703
   485
wenzelm@15703
   486
wenzelm@18636
   487
(* theory setup *)
wenzelm@5823
   488
wenzelm@18636
   489
val _ = Context.add_setup
wenzelm@18708
   490
 (add_attributes
wenzelm@18636
   491
   [("tagged", common tagged, "tagged theorem"),
wenzelm@18636
   492
    ("untagged", common untagged, "untagged theorem"),
wenzelm@18636
   493
    ("COMP", common COMP_att, "direct composition with rules (no lifting)"),
wenzelm@18636
   494
    ("THEN", common THEN_att, "resolution with rule"),
wenzelm@18636
   495
    ("OF", common OF_att, "rule applied to facts"),
wenzelm@18636
   496
    ("where", common where_att, "named instantiation of theorem"),
wenzelm@18636
   497
    ("of", common of_att, "rule applied to terms"),
wenzelm@18636
   498
    ("rename_abs", common rename_abs, "rename bound variables in abstractions"),
wenzelm@18636
   499
    ("unfolded", common unfolded, "unfolded definitions"),
wenzelm@18636
   500
    ("folded", common folded, "folded definitions"),
wenzelm@18636
   501
    ("standard", common standard, "result put into standard form"),
wenzelm@18636
   502
    ("elim_format", common elim_format, "destruct rule turned into elimination rule format"),
wenzelm@18636
   503
    ("no_vars", common no_vars, "frozen schematic vars"),
wenzelm@18636
   504
    ("eta_long", common eta_long, "put theorem into eta long beta normal form"),
wenzelm@18636
   505
    ("consumes", common consumes, "number of consumed facts"),
wenzelm@18636
   506
    ("case_names", common case_names, "named rule cases"),
wenzelm@18636
   507
    ("case_conclusion", common case_conclusion, "named conclusion of rule cases"),
wenzelm@18636
   508
    ("params", common params, "named rule parameters"),
wenzelm@18636
   509
    ("atomize", (no_args ObjectLogic.declare_atomize, no_args undef_local_attribute),
wenzelm@18636
   510
      "declaration of atomize rule"),
wenzelm@18636
   511
    ("rulify", (no_args ObjectLogic.declare_rulify, no_args undef_local_attribute),
wenzelm@18636
   512
      "declaration of rulify rule"),
wenzelm@18636
   513
    ("rule_format", common rule_format_att, "result put into standard rule format"),
wenzelm@18708
   514
    ("attribute", common internal_att, "internal attribute")]);
wenzelm@5823
   515
wenzelm@5823
   516
end;
wenzelm@5823
   517
wenzelm@5823
   518
structure BasicAttrib: BASIC_ATTRIB = Attrib;
wenzelm@5823
   519
open BasicAttrib;