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