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