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