src/Pure/Isar/attrib.ML
author wenzelm
Mon Oct 15 20:32:13 2001 +0200 (2001-10-15)
changeset 11775 e7eeca372b7c
parent 11770 b6bb7a853dd2
child 11904 3a4b3c311a97
permissions -rw-r--r--
bang_args;
wenzelm@5823
     1
(*  Title:      Pure/Isar/attrib.ML
wenzelm@5823
     2
    ID:         $Id$
wenzelm@5823
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@8807
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
wenzelm@5823
     5
wenzelm@5823
     6
Symbolic theorem attributes.
wenzelm@5823
     7
*)
wenzelm@5823
     8
wenzelm@5823
     9
signature BASIC_ATTRIB =
wenzelm@5823
    10
sig
wenzelm@5823
    11
  val print_attributes: theory -> unit
wenzelm@5879
    12
  val Attribute: bstring -> (Args.src -> theory attribute) * (Args.src -> Proof.context attribute)
wenzelm@5879
    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@5912
    19
  exception ATTRIB_FAIL of (string * Position.T) * exn
wenzelm@5823
    20
  val global_attribute: theory -> Args.src -> theory attribute
wenzelm@5823
    21
  val local_attribute: theory -> Args.src -> Proof.context attribute
wenzelm@5912
    22
  val local_attribute': Proof.context -> Args.src -> Proof.context attribute
wenzelm@7673
    23
  val undef_global_attribute: theory attribute
wenzelm@7673
    24
  val undef_local_attribute: Proof.context attribute
wenzelm@5823
    25
  val add_attributes: (bstring * ((Args.src -> theory attribute) *
wenzelm@5823
    26
      (Args.src -> Proof.context attribute)) * string) list -> theory -> theory
wenzelm@6091
    27
  val global_thm: theory * Args.T list -> thm * (theory * Args.T list)
wenzelm@6091
    28
  val global_thms: theory * Args.T list -> thm list * (theory * Args.T list)
wenzelm@6091
    29
  val global_thmss: theory * Args.T list -> thm list * (theory * Args.T list)
wenzelm@6091
    30
  val local_thm: Proof.context * Args.T list -> thm * (Proof.context * Args.T list)
wenzelm@6091
    31
  val local_thms: Proof.context * Args.T list -> thm list * (Proof.context * Args.T list)
wenzelm@6091
    32
  val local_thmss: Proof.context * Args.T list -> thm list * (Proof.context * Args.T list)
wenzelm@5879
    33
  val syntax: ('a * Args.T list -> 'a attribute * ('a * Args.T list)) -> Args.src -> 'a attribute
wenzelm@5823
    34
  val no_args: 'a attribute -> Args.src -> 'a attribute
wenzelm@11775
    35
  val bang_args: 'a attribute -> 'a attribute -> Args.src -> 'a attribute
wenzelm@8633
    36
  val add_del_args: 'a attribute -> 'a attribute -> Args.src -> 'a attribute
wenzelm@5823
    37
  val setup: (theory -> theory) list
wenzelm@5823
    38
end;
wenzelm@5823
    39
wenzelm@5823
    40
structure Attrib: ATTRIB =
wenzelm@5823
    41
struct
wenzelm@5823
    42
wenzelm@5823
    43
wenzelm@5823
    44
(** attributes theory data **)
wenzelm@5823
    45
wenzelm@5823
    46
(* data kind 'Isar/attributes' *)
wenzelm@5823
    47
wenzelm@5823
    48
structure AttributesDataArgs =
wenzelm@5823
    49
struct
wenzelm@5823
    50
  val name = "Isar/attributes";
wenzelm@5823
    51
  type T =
wenzelm@5823
    52
    {space: NameSpace.T,
wenzelm@5823
    53
     attrs:
wenzelm@5823
    54
       ((((Args.src -> theory attribute) * (Args.src -> Proof.context attribute))
wenzelm@5823
    55
         * string) * stamp) Symtab.table};
wenzelm@5823
    56
wenzelm@5823
    57
  val empty = {space = NameSpace.empty, attrs = Symtab.empty};
wenzelm@6546
    58
  val copy = I;
wenzelm@5823
    59
  val prep_ext = I;
wenzelm@5823
    60
wenzelm@5823
    61
  fun merge ({space = space1, attrs = attrs1}, {space = space2, attrs = attrs2}) =
wenzelm@5823
    62
    {space = NameSpace.merge (space1, space2),
wenzelm@5823
    63
      attrs = Symtab.merge eq_snd (attrs1, attrs2) handle Symtab.DUPS dups =>
wenzelm@5823
    64
        error ("Attempt to merge different versions of attributes " ^ commas_quote dups)};
wenzelm@5823
    65
wenzelm@9216
    66
  fun print _ {space, attrs} =
wenzelm@5823
    67
    let
wenzelm@5823
    68
      fun prt_attr (name, ((_, comment), _)) = Pretty.block
wenzelm@6846
    69
        [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
wenzelm@5823
    70
    in
wenzelm@8720
    71
      [Pretty.big_list "attributes:" (map prt_attr (NameSpace.cond_extern_table space attrs))]
wenzelm@9216
    72
      |> Pretty.chunks |> Pretty.writeln
wenzelm@5823
    73
    end;
wenzelm@5823
    74
end;
wenzelm@5823
    75
wenzelm@5823
    76
structure AttributesData = TheoryDataFun(AttributesDataArgs);
wenzelm@5823
    77
val print_attributes = AttributesData.print;
wenzelm@7611
    78
wenzelm@5823
    79
wenzelm@5823
    80
(* get global / local attributes *)
wenzelm@5823
    81
wenzelm@5912
    82
exception ATTRIB_FAIL of (string * Position.T) * exn;
wenzelm@5912
    83
wenzelm@5823
    84
fun gen_attribute which thy =
wenzelm@5823
    85
  let
wenzelm@5823
    86
    val {space, attrs} = AttributesData.get thy;
wenzelm@5823
    87
wenzelm@5879
    88
    fun attr src =
wenzelm@5879
    89
      let
wenzelm@5879
    90
        val ((raw_name, _), pos) = Args.dest_src src;
wenzelm@5879
    91
        val name = NameSpace.intern space raw_name;
wenzelm@5879
    92
      in
wenzelm@5823
    93
        (case Symtab.lookup (attrs, name) of
wenzelm@5823
    94
          None => error ("Unknown attribute: " ^ quote name ^ Position.str_of pos)
wenzelm@5912
    95
        | Some ((p, _), _) => transform_failure (curry ATTRIB_FAIL (name, pos)) (which p src))
wenzelm@5823
    96
      end;
wenzelm@5823
    97
  in attr end;
wenzelm@5823
    98
wenzelm@5823
    99
val global_attribute = gen_attribute fst;
wenzelm@5823
   100
val local_attribute = gen_attribute snd;
wenzelm@5879
   101
val local_attribute' = local_attribute o ProofContext.theory_of;
wenzelm@5823
   102
wenzelm@7673
   103
val undef_global_attribute: theory attribute =
wenzelm@7673
   104
  fn _ => error "attribute undefined in theory context";
wenzelm@7673
   105
wenzelm@7673
   106
val undef_local_attribute: Proof.context attribute =
wenzelm@7673
   107
  fn _ => error "attribute undefined in proof context";
wenzelm@7673
   108
wenzelm@5823
   109
wenzelm@5823
   110
(* add_attributes *)
wenzelm@5823
   111
wenzelm@5823
   112
fun add_attributes raw_attrs thy =
wenzelm@5823
   113
  let
wenzelm@5823
   114
    val full = Sign.full_name (Theory.sign_of thy);
wenzelm@5823
   115
    val new_attrs =
wenzelm@5823
   116
      map (fn (name, (f, g), comment) => (full name, (((f, g), comment), stamp ()))) raw_attrs;
wenzelm@5823
   117
wenzelm@5823
   118
    val {space, attrs} = AttributesData.get thy;
wenzelm@5823
   119
    val space' = NameSpace.extend (space, map fst new_attrs);
wenzelm@5823
   120
    val attrs' = Symtab.extend (attrs, new_attrs) handle Symtab.DUPS dups =>
wenzelm@5823
   121
      error ("Duplicate declaration of attributes(s) " ^ commas_quote dups);
wenzelm@5823
   122
  in thy |> AttributesData.put {space = space', attrs = attrs'} end;
wenzelm@5823
   123
wenzelm@5879
   124
(*implicit version*)
wenzelm@5879
   125
fun Attribute name att cmt = Context.>> (add_attributes [(name, att, cmt)]);
wenzelm@5823
   126
wenzelm@5879
   127
wenzelm@5879
   128
wenzelm@5879
   129
(** attribute parsers **)
wenzelm@5879
   130
wenzelm@5879
   131
(* tags *)
wenzelm@5823
   132
wenzelm@5879
   133
fun tag x = Scan.lift (Args.name -- Scan.repeat Args.name) x;
wenzelm@5879
   134
wenzelm@5879
   135
wenzelm@5879
   136
(* theorems *)
wenzelm@5879
   137
wenzelm@5879
   138
fun gen_thm get attrib app =
wenzelm@5879
   139
  Scan.depend (fn st => Args.name -- Args.opt_attribs >>
wenzelm@5879
   140
    (fn (name, srcs) => app ((st, get st name), map (attrib st) srcs)));
wenzelm@5823
   141
wenzelm@6091
   142
val global_thm = gen_thm PureThy.get_thm global_attribute Thm.apply_attributes;
wenzelm@6091
   143
val global_thms = gen_thm PureThy.get_thms global_attribute Thm.applys_attributes;
wenzelm@5879
   144
val global_thmss = Scan.repeat global_thms >> flat;
wenzelm@5879
   145
wenzelm@6091
   146
val local_thm = gen_thm ProofContext.get_thm local_attribute' Thm.apply_attributes;
wenzelm@6091
   147
val local_thms = gen_thm ProofContext.get_thms local_attribute' Thm.applys_attributes;
wenzelm@5879
   148
val local_thmss = Scan.repeat local_thms >> flat;
wenzelm@5879
   149
wenzelm@5823
   150
wenzelm@5879
   151
wenzelm@5879
   152
(** attribute syntax **)
wenzelm@5823
   153
wenzelm@5879
   154
fun syntax scan src (st, th) =
wenzelm@8282
   155
  let val (st', f) = Args.syntax "attribute" scan src st
wenzelm@5879
   156
  in f (st', th) end;
wenzelm@5879
   157
wenzelm@5879
   158
fun no_args x = syntax (Scan.succeed x);
wenzelm@5823
   159
wenzelm@11775
   160
fun bang_args a b x = syntax
wenzelm@11775
   161
  (Scan.lift Args.bang >> K a || Scan.succeed b) x;
wenzelm@11775
   162
wenzelm@10034
   163
fun add_del_args add del x = syntax
wenzelm@10034
   164
  (Scan.lift (Args.add >> K add || Args.del >> K del || Scan.succeed add)) x;
wenzelm@8633
   165
wenzelm@5823
   166
wenzelm@5823
   167
wenzelm@5823
   168
(** Pure attributes **)
wenzelm@5823
   169
wenzelm@5823
   170
(* tags *)
wenzelm@5823
   171
wenzelm@9902
   172
fun gen_tagged x = syntax (tag >> Drule.tag) x;
wenzelm@9902
   173
fun gen_untagged x = syntax (Scan.lift Args.name >> Drule.untag) x;
wenzelm@5823
   174
wenzelm@5823
   175
wenzelm@6772
   176
(* COMP *)
wenzelm@6772
   177
wenzelm@6948
   178
fun comp (i, B) (x, A) = (x, Drule.compose_single (A, i, B));
wenzelm@6772
   179
wenzelm@10151
   180
fun gen_COMP thm = syntax (Scan.lift (Scan.optional (Args.bracks Args.nat) 1) -- thm >> comp);
wenzelm@9902
   181
val COMP_global = gen_COMP global_thm;
wenzelm@9902
   182
val COMP_local = gen_COMP local_thm;
wenzelm@6772
   183
wenzelm@6772
   184
wenzelm@5879
   185
(* RS *)
wenzelm@5879
   186
wenzelm@6091
   187
fun resolve (i, B) (x, A) = (x, A RSN (i, B));
wenzelm@5879
   188
wenzelm@10151
   189
fun gen_RS thm = syntax (Scan.lift (Scan.optional (Args.bracks Args.nat) 1) -- thm >> resolve);
wenzelm@9902
   190
val RS_global = gen_RS global_thm;
wenzelm@9902
   191
val RS_local = gen_RS local_thm;
wenzelm@5879
   192
wenzelm@5879
   193
wenzelm@9902
   194
(* OF *)
wenzelm@5879
   195
wenzelm@6091
   196
fun apply Bs (x, A) = (x, Bs MRS A);
wenzelm@5879
   197
wenzelm@9902
   198
val OF_global = syntax (global_thmss >> apply);
wenzelm@9902
   199
val OF_local = syntax (local_thmss >> apply);
wenzelm@5879
   200
wenzelm@5879
   201
wenzelm@5912
   202
(* where: named instantiations *)
wenzelm@5879
   203
wenzelm@10807
   204
fun read_instantiate _ [] _ thm = thm
wenzelm@10807
   205
  | read_instantiate context_of insts x thm =
wenzelm@10807
   206
      let
wenzelm@10807
   207
        val ctxt = context_of x;
wenzelm@10807
   208
        val sign = ProofContext.sign_of ctxt;
wenzelm@10807
   209
wenzelm@10807
   210
        val vars = Drule.vars_of thm;
wenzelm@10807
   211
        fun get_typ xi =
wenzelm@10807
   212
          (case assoc (vars, xi) of
wenzelm@10807
   213
            Some T => T
wenzelm@10807
   214
          | None => error ("No such variable in theorem: " ^ Syntax.string_of_vname xi));
wenzelm@5879
   215
wenzelm@10807
   216
        val (xs, ss) = Library.split_list insts;
wenzelm@10807
   217
        val Ts = map get_typ xs;
wenzelm@5879
   218
wenzelm@10807
   219
        val (ts, envT) = ProofContext.read_termTs ctxt (ss ~~ Ts);
wenzelm@10807
   220
        val cenvT = map (apsnd (Thm.ctyp_of sign)) envT;
wenzelm@10807
   221
        val cenv =
wenzelm@10807
   222
          map (fn (xi, t) => pairself (Thm.cterm_of sign) (Var (xi, fastype_of t), t))
wenzelm@10807
   223
            (gen_distinct (fn ((x1, t1), (x2, t2)) => x1 = x2 andalso t1 aconv t2) (xs ~~ ts));
wenzelm@10807
   224
      in
wenzelm@10807
   225
        thm
wenzelm@10807
   226
        |> Drule.instantiate (cenvT, cenv)
wenzelm@10807
   227
        |> RuleCases.save thm
wenzelm@10807
   228
      end;
wenzelm@5879
   229
wenzelm@6448
   230
fun insts x = Args.and_list (Scan.lift (Args.var --| Args.$$$ "=" -- Args.name)) x;
wenzelm@5879
   231
wenzelm@6091
   232
fun gen_where context_of = syntax (insts >> (Drule.rule_attribute o read_instantiate context_of));
wenzelm@5823
   233
wenzelm@9902
   234
val where_global = gen_where ProofContext.init;
wenzelm@9902
   235
val where_local = gen_where I;
wenzelm@5879
   236
wenzelm@5879
   237
wenzelm@9902
   238
(* of: positional instantiations *)
wenzelm@5912
   239
wenzelm@10807
   240
fun read_instantiate' _ ([], []) _ thm = thm
wenzelm@10807
   241
  | read_instantiate' context_of (args, concl_args) x thm =
wenzelm@10807
   242
      let
wenzelm@10807
   243
        fun zip_vars _ [] = []
wenzelm@10807
   244
          | zip_vars (_ :: xs) (None :: opt_ts) = zip_vars xs opt_ts
wenzelm@10807
   245
          | zip_vars ((x, _) :: xs) (Some t :: opt_ts) = (x, t) :: zip_vars xs opt_ts
wenzelm@10807
   246
          | zip_vars [] _ = error "More instantiations than variables in theorem";
wenzelm@10807
   247
        val insts =
wenzelm@10807
   248
          zip_vars (Drule.vars_of_terms [#prop (Thm.rep_thm thm)]) args @
wenzelm@10807
   249
          zip_vars (Drule.vars_of_terms [Thm.concl_of thm]) concl_args;
wenzelm@10807
   250
      in
wenzelm@10807
   251
        thm
wenzelm@10807
   252
        |> read_instantiate context_of insts x
wenzelm@10807
   253
        |> RuleCases.save thm
wenzelm@10807
   254
      end;
wenzelm@5912
   255
wenzelm@10807
   256
val concl = Args.$$$ "concl" -- Args.colon;
wenzelm@8687
   257
val inst_arg = Scan.unless concl Args.name_dummy;
wenzelm@5912
   258
val inst_args = Scan.repeat inst_arg;
wenzelm@10807
   259
fun insts' x = (inst_args -- Scan.optional (concl |-- Args.!!! inst_args) []) x;
wenzelm@5912
   260
wenzelm@10807
   261
fun gen_of context_of =
wenzelm@10807
   262
  syntax (Scan.lift insts' >> (Drule.rule_attribute o read_instantiate' context_of));
wenzelm@5912
   263
wenzelm@9902
   264
val of_global = gen_of ProofContext.init;
wenzelm@9902
   265
val of_local = gen_of I;
wenzelm@5912
   266
wenzelm@5912
   267
wenzelm@7598
   268
(* unfold / fold definitions *)
wenzelm@7598
   269
wenzelm@7598
   270
fun gen_rewrite rew defs (x, thm) = (x, rew defs thm);
wenzelm@7598
   271
wenzelm@9902
   272
val unfolded_global = syntax (global_thmss >> gen_rewrite Tactic.rewrite_rule);
wenzelm@9902
   273
val unfolded_local = syntax (local_thmss >> gen_rewrite Tactic.rewrite_rule);
wenzelm@9902
   274
val folded_global = syntax (global_thmss >> gen_rewrite Tactic.fold_rule);
wenzelm@9902
   275
val folded_local = syntax (local_thmss >> gen_rewrite Tactic.fold_rule);
wenzelm@7598
   276
wenzelm@7598
   277
wenzelm@8368
   278
(* rule cases *)
wenzelm@8368
   279
wenzelm@10528
   280
fun consumes x = syntax (Scan.lift (Scan.optional Args.nat 1) >> RuleCases.consumes) x;
wenzelm@8368
   281
fun case_names x = syntax (Scan.lift (Scan.repeat1 Args.name) >> RuleCases.case_names) x;
wenzelm@8368
   282
fun params x = syntax (Args.and_list1 (Scan.lift (Scan.repeat Args.name)) >> RuleCases.params) x;
wenzelm@8368
   283
wenzelm@8368
   284
wenzelm@11770
   285
(* rule_format *)
wenzelm@11770
   286
wenzelm@11770
   287
fun rule_format_att x = syntax
wenzelm@11770
   288
  (Scan.lift (Args.parens (Args.$$$ "no_asm")
wenzelm@11770
   289
  >> K ObjectLogic.rule_format_no_asm || Scan.succeed ObjectLogic.rule_format)) x;
wenzelm@11770
   290
wenzelm@11770
   291
wenzelm@5879
   292
(* misc rules *)
wenzelm@5879
   293
wenzelm@6091
   294
fun standard x = no_args (Drule.rule_attribute (K Drule.standard)) x;
wenzelm@9941
   295
fun elim_format x = no_args (Drule.rule_attribute (K Tactic.make_elim)) x;
wenzelm@9216
   296
fun no_vars x = no_args (Drule.rule_attribute (K (#1 o Drule.freeze_thaw))) x;
wenzelm@5879
   297
wenzelm@9902
   298
fun exported_global x = no_args (Drule.rule_attribute (Proof.export_thm o ProofContext.init)) x;
wenzelm@9902
   299
fun exported_local x = no_args (Drule.rule_attribute Proof.export_thm) x;
wenzelm@6933
   300
wenzelm@5879
   301
wenzelm@11770
   302
wenzelm@5879
   303
(** theory setup **)
wenzelm@5823
   304
wenzelm@5823
   305
(* pure_attributes *)
wenzelm@5823
   306
wenzelm@5823
   307
val pure_attributes =
wenzelm@9902
   308
 [("tagged", (gen_tagged, gen_tagged), "tagged theorem"),
wenzelm@9902
   309
  ("untagged", (gen_untagged, gen_untagged), "untagged theorem"),
wenzelm@9902
   310
  ("COMP", (COMP_global, COMP_local), "direct composition with rules (no lifting)"),
wenzelm@9902
   311
  ("THEN", (RS_global, RS_local), "resolution with rule"),
wenzelm@9902
   312
  ("OF", (OF_global, OF_local), "rule applied to facts"),
wenzelm@9902
   313
  ("where", (where_global, where_local), "named instantiation of theorem"),
wenzelm@9902
   314
  ("of", (of_global, of_local), "rule applied to terms"),
wenzelm@9902
   315
  ("unfolded", (unfolded_global, unfolded_local), "unfolded definitions"),
wenzelm@9902
   316
  ("folded", (folded_global, folded_local), "folded definitions"),
wenzelm@9902
   317
  ("standard", (standard, standard), "result put into standard form"),
wenzelm@9941
   318
  ("elim_format", (elim_format, elim_format), "destruct rule turned into elimination rule format"),
wenzelm@9902
   319
  ("no_vars", (no_vars, no_vars), "frozen schematic vars"),
wenzelm@10528
   320
  ("consumes", (consumes, consumes), "number of consumed facts"),
wenzelm@9902
   321
  ("case_names", (case_names, case_names), "named rule cases"),
wenzelm@9902
   322
  ("params", (params, params), "named rule parameters"),
wenzelm@11763
   323
  ("exported", (exported_global, exported_local), "theorem exported from context"),
wenzelm@11770
   324
  ("atomize", (no_args ObjectLogic.declare_atomize, no_args undef_local_attribute),
wenzelm@11770
   325
    "declaration of atomize rule"),
wenzelm@11770
   326
  ("rulify", (no_args ObjectLogic.declare_rulify, no_args undef_local_attribute),
wenzelm@11770
   327
    "declaration of rulify rule"),
wenzelm@11770
   328
  ("rule_format", (rule_format_att, rule_format_att), "result put into standard rule format")];
wenzelm@5823
   329
wenzelm@5823
   330
wenzelm@5879
   331
(* setup *)
wenzelm@5823
   332
wenzelm@5823
   333
val setup = [AttributesData.init, add_attributes pure_attributes];
wenzelm@5823
   334
wenzelm@5823
   335
end;
wenzelm@5823
   336
wenzelm@5823
   337
structure BasicAttrib: BASIC_ATTRIB = Attrib;
wenzelm@5823
   338
open BasicAttrib;