src/Pure/Isar/attrib.ML
author wenzelm
Mon Oct 09 02:19:58 2006 +0200 (2006-10-09)
changeset 20906 63150f3a103d
parent 20334 60157137a0eb
child 21031 a56e6d1e56a3
permissions -rw-r--r--
added kind attributes;
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@18905
    23
  val map_specs: ('a -> 'att) ->
wenzelm@18905
    24
    (('c * 'a list) * 'd) list -> (('c * 'att list) * 'd) list
wenzelm@18905
    25
  val map_facts: ('a -> 'att) ->
wenzelm@17105
    26
    (('c * 'a list) * ('d * 'a list) list) list ->
wenzelm@18905
    27
    (('c * 'att list) * ('d * 'att list) list) list
wenzelm@20289
    28
  val crude_closure: Proof.context -> src -> src
wenzelm@18734
    29
  val add_attributes: (bstring * (src -> attribute) * string) list -> theory -> theory
wenzelm@18636
    30
  val thm: Context.generic * Args.T list -> thm * (Context.generic * Args.T list)
wenzelm@18636
    31
  val thms: Context.generic * Args.T list -> thm list * (Context.generic * Args.T list)
wenzelm@18998
    32
  val multi_thm: Context.generic * Args.T list -> thm list * (Context.generic * Args.T list)
wenzelm@18734
    33
  val syntax: (Context.generic * Args.T list ->
wenzelm@18734
    34
    attribute * (Context.generic * Args.T list)) -> src -> attribute
wenzelm@18734
    35
  val no_args: attribute -> src -> attribute
wenzelm@18734
    36
  val add_del_args: attribute -> attribute -> src -> attribute
wenzelm@20906
    37
  val kind: string -> src
wenzelm@20906
    38
  val kind_theorem: src
wenzelm@20906
    39
  val kind_lemma: src
wenzelm@20906
    40
  val kind_corollary: src
wenzelm@20906
    41
  val kind_internal: src
wenzelm@18734
    42
  val internal: attribute -> src
wenzelm@5823
    43
end;
wenzelm@5823
    44
wenzelm@5823
    45
structure Attrib: ATTRIB =
wenzelm@5823
    46
struct
wenzelm@5823
    47
wenzelm@15703
    48
type src = Args.src;
wenzelm@15703
    49
wenzelm@5823
    50
wenzelm@18636
    51
wenzelm@18734
    52
(** named attributes **)
wenzelm@18636
    53
wenzelm@18734
    54
(* theory data *)
wenzelm@5823
    55
wenzelm@16458
    56
structure AttributesData = TheoryDataFun
wenzelm@16458
    57
(struct
wenzelm@5823
    58
  val name = "Isar/attributes";
wenzelm@18734
    59
  type T = (((src -> attribute) * string) * stamp) NameSpace.table;
wenzelm@5823
    60
wenzelm@16344
    61
  val empty = NameSpace.empty_table;
wenzelm@6546
    62
  val copy = I;
wenzelm@16458
    63
  val extend = I;
wenzelm@5823
    64
haftmann@17496
    65
  fun merge _ tables = NameSpace.merge_tables (eq_snd (op =)) tables handle Symtab.DUPS dups =>
wenzelm@16344
    66
    error ("Attempt to merge different versions of attribute(s) " ^ commas_quote dups);
wenzelm@5823
    67
wenzelm@16344
    68
  fun print _ attribs =
wenzelm@5823
    69
    let
wenzelm@5823
    70
      fun prt_attr (name, ((_, comment), _)) = Pretty.block
wenzelm@6846
    71
        [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
wenzelm@5823
    72
    in
wenzelm@16344
    73
      [Pretty.big_list "attributes:" (map prt_attr (NameSpace.extern_table attribs))]
wenzelm@9216
    74
      |> Pretty.chunks |> Pretty.writeln
wenzelm@5823
    75
    end;
wenzelm@16458
    76
end);
wenzelm@5823
    77
wenzelm@18708
    78
val _ = Context.add_setup AttributesData.init;
wenzelm@5823
    79
val print_attributes = AttributesData.print;
wenzelm@7611
    80
wenzelm@5823
    81
wenzelm@15703
    82
(* interning *)
wenzelm@15703
    83
wenzelm@16458
    84
val intern = NameSpace.intern o #1 o AttributesData.get;
wenzelm@15703
    85
val intern_src = Args.map_name o intern;
wenzelm@15703
    86
wenzelm@15703
    87
wenzelm@18734
    88
(* get attributes *)
wenzelm@5823
    89
wenzelm@5912
    90
exception ATTRIB_FAIL of (string * Position.T) * exn;
wenzelm@5912
    91
wenzelm@18734
    92
fun attribute_i thy =
wenzelm@5823
    93
  let
wenzelm@16344
    94
    val attrs = #2 (AttributesData.get thy);
wenzelm@5879
    95
    fun attr src =
wenzelm@16344
    96
      let val ((name, _), pos) = Args.dest_src src in
wenzelm@17412
    97
        (case Symtab.lookup attrs name of
skalberg@15531
    98
          NONE => error ("Unknown attribute: " ^ quote name ^ Position.str_of pos)
wenzelm@18734
    99
        | SOME ((att, _), _) => transform_failure (curry ATTRIB_FAIL (name, pos)) (att src))
wenzelm@5823
   100
      end;
wenzelm@5823
   101
  in attr end;
wenzelm@5823
   102
wenzelm@18734
   103
fun attribute thy = attribute_i thy o intern_src thy;
wenzelm@18636
   104
wenzelm@5823
   105
wenzelm@17105
   106
(* attributed declarations *)
wenzelm@17105
   107
wenzelm@17105
   108
fun map_specs f = map (apfst (apsnd (map f)));
wenzelm@17105
   109
fun map_facts f = map (apfst (apsnd (map f)) o apsnd (map (apsnd (map f))));
wenzelm@17105
   110
wenzelm@17105
   111
wenzelm@15703
   112
(* crude_closure *)
wenzelm@15703
   113
wenzelm@15703
   114
(*Produce closure without knowing facts in advance! The following
wenzelm@18734
   115
  works reasonably well for attribute parsers that do not peek at the
wenzelm@18734
   116
  thm structure.*)
wenzelm@15703
   117
wenzelm@15703
   118
fun crude_closure ctxt src =
wenzelm@18734
   119
 (try (fn () => attribute_i (ProofContext.theory_of ctxt) src
wenzelm@18734
   120
    (Context.Proof ctxt, Drule.asm_rl)) ();
wenzelm@15703
   121
  Args.closure src);
wenzelm@15703
   122
wenzelm@15703
   123
wenzelm@5823
   124
(* add_attributes *)
wenzelm@5823
   125
wenzelm@5823
   126
fun add_attributes raw_attrs thy =
wenzelm@5823
   127
  let
wenzelm@18734
   128
    val new_attrs =
wenzelm@18734
   129
      raw_attrs |> map (fn (name, att, comment) => (name, ((att, comment), stamp ())));
wenzelm@16458
   130
    fun add attrs = NameSpace.extend_table (Sign.naming_of thy) (attrs, new_attrs)
wenzelm@16344
   131
      handle Symtab.DUPS dups =>
wenzelm@16344
   132
        error ("Duplicate declaration of attributes(s) " ^ commas_quote dups);
wenzelm@16344
   133
  in AttributesData.map add thy end;
wenzelm@5823
   134
wenzelm@5879
   135
fun Attribute name att cmt = Context.>> (add_attributes [(name, att, cmt)]);
wenzelm@5823
   136
wenzelm@5879
   137
wenzelm@5879
   138
wenzelm@5879
   139
(** attribute parsers **)
wenzelm@5879
   140
wenzelm@5879
   141
(* tags *)
wenzelm@5823
   142
wenzelm@5879
   143
fun tag x = Scan.lift (Args.name -- Scan.repeat Args.name) x;
wenzelm@5879
   144
wenzelm@5879
   145
wenzelm@5879
   146
(* theorems *)
wenzelm@5879
   147
wenzelm@18636
   148
local
wenzelm@18636
   149
wenzelm@18998
   150
val get_thms = Context.cases PureThy.get_thms ProofContext.get_thms;
wenzelm@18998
   151
wenzelm@18998
   152
fun gen_thm pick = Scan.depend (fn st =>
wenzelm@20029
   153
 (Args.internal_fact
wenzelm@20029
   154
    >> (fn fact => ("<fact>", Name "", fact)) ||
wenzelm@20029
   155
  Scan.ahead Args.alt_name -- Args.named_fact (get_thms st o Fact)
wenzelm@18037
   156
    >> (fn (s, fact) => ("", Fact s, fact)) ||
wenzelm@18998
   157
  Scan.ahead Args.name -- Args.named_fact (get_thms st o Name) -- Args.thm_sel
wenzelm@18037
   158
    >> (fn ((name, fact), sel) => (name, NameSelection (name, sel), fact)) ||
wenzelm@18998
   159
  Scan.ahead Args.name -- Args.named_fact (get_thms st o Name)
wenzelm@18037
   160
    >> (fn (name, fact) => (name, Name name, fact))) --
wenzelm@18998
   161
  Args.opt_attribs (intern (Context.theory_of st))
wenzelm@18037
   162
  >> (fn ((name, thmref, fact), srcs) =>
wenzelm@15703
   163
    let
wenzelm@16498
   164
      val ths = PureThy.select_thm thmref fact;
wenzelm@18998
   165
      val atts = map (attribute_i (Context.theory_of st)) srcs;
wenzelm@18998
   166
      val (st', ths') = foldl_map (Library.apply atts) (st, ths);
wenzelm@15703
   167
    in (st', pick name ths') end));
berghofe@15456
   168
wenzelm@18636
   169
in
wenzelm@18636
   170
wenzelm@18998
   171
val thm = gen_thm PureThy.single_thm;
wenzelm@18998
   172
val multi_thm = gen_thm (K I);
wenzelm@19482
   173
val thms = Scan.repeat multi_thm >> flat;
wenzelm@18636
   174
wenzelm@18636
   175
end;
wenzelm@18636
   176
wenzelm@5823
   177
wenzelm@5879
   178
wenzelm@5879
   179
(** attribute syntax **)
wenzelm@5823
   180
wenzelm@5879
   181
fun syntax scan src (st, th) =
wenzelm@8282
   182
  let val (st', f) = Args.syntax "attribute" scan src st
wenzelm@5879
   183
  in f (st', th) end;
wenzelm@5879
   184
wenzelm@5879
   185
fun no_args x = syntax (Scan.succeed x);
wenzelm@5823
   186
wenzelm@10034
   187
fun add_del_args add del x = syntax
wenzelm@10034
   188
  (Scan.lift (Args.add >> K add || Args.del >> K del || Scan.succeed add)) x;
wenzelm@8633
   189
wenzelm@5823
   190
wenzelm@5823
   191
wenzelm@18636
   192
(** basic attributes **)
wenzelm@5823
   193
wenzelm@5823
   194
(* tags *)
wenzelm@5823
   195
wenzelm@18799
   196
fun tagged x = syntax (tag >> PureThy.tag) x;
wenzelm@18799
   197
fun untagged x = syntax (Scan.lift Args.name >> PureThy.untag) x;
wenzelm@6772
   198
wenzelm@6772
   199
wenzelm@20906
   200
(* kinds *)
wenzelm@20906
   201
wenzelm@20906
   202
fun kind_att x = syntax (Scan.lift Args.name >> PureThy.kind) x;
wenzelm@20906
   203
fun kind name = Args.src (("Pure.kind", [Args.mk_string (name, Position.none)]), Position.none);
wenzelm@20906
   204
wenzelm@20906
   205
val kind_theorem = kind PureThy.theoremK;
wenzelm@20906
   206
val kind_lemma = kind PureThy.lemmaK;
wenzelm@20906
   207
val kind_corollary = kind PureThy.corollaryK;
wenzelm@20906
   208
val kind_internal = kind PureThy.internalK;
wenzelm@20906
   209
wenzelm@20906
   210
wenzelm@18636
   211
(* rule composition *)
wenzelm@5879
   212
wenzelm@18636
   213
val COMP_att =
wenzelm@18636
   214
  syntax (Scan.lift (Scan.optional (Args.bracks Args.nat) 1) -- thm
wenzelm@18734
   215
    >> (fn (i, B) => Thm.rule_attribute (fn _ => fn A => Drule.compose_single (A, i, B))));
wenzelm@5879
   216
wenzelm@18636
   217
val THEN_att =
wenzelm@18636
   218
  syntax (Scan.lift (Scan.optional (Args.bracks Args.nat) 1) -- thm
wenzelm@18734
   219
    >> (fn (i, B) => Thm.rule_attribute (fn _ => fn A => A RSN (i, B))));
wenzelm@5879
   220
wenzelm@18636
   221
val OF_att =
wenzelm@18998
   222
  syntax (thms >> (fn Bs => Thm.rule_attribute (fn _ => fn A => Bs MRS A)));
wenzelm@5879
   223
wenzelm@5879
   224
berghofe@13782
   225
(* rename_abs *)
berghofe@13782
   226
berghofe@13782
   227
fun rename_abs src = syntax
wenzelm@15703
   228
  (Scan.lift (Scan.repeat (Args.maybe Args.name) >> (apsnd o Drule.rename_bvars'))) src;
berghofe@13782
   229
berghofe@13782
   230
wenzelm@7598
   231
(* unfold / fold definitions *)
wenzelm@7598
   232
wenzelm@18839
   233
fun unfolded_syntax rule =
wenzelm@18998
   234
  syntax (thms >>
wenzelm@18872
   235
    (fn ths => Thm.rule_attribute (fn context => rule (Context.proof_of context) ths)));
wenzelm@18839
   236
wenzelm@18839
   237
val unfolded = unfolded_syntax LocalDefs.unfold;
wenzelm@18839
   238
val folded = unfolded_syntax LocalDefs.fold;
wenzelm@7598
   239
wenzelm@7598
   240
wenzelm@8368
   241
(* rule cases *)
wenzelm@8368
   242
wenzelm@10528
   243
fun consumes x = syntax (Scan.lift (Scan.optional Args.nat 1) >> RuleCases.consumes) x;
wenzelm@8368
   244
fun case_names x = syntax (Scan.lift (Scan.repeat1 Args.name) >> RuleCases.case_names) x;
wenzelm@18236
   245
fun case_conclusion x =
wenzelm@18236
   246
  syntax (Scan.lift (Args.name -- Scan.repeat Args.name) >> RuleCases.case_conclusion) x;
wenzelm@8368
   247
fun params x = syntax (Args.and_list1 (Scan.lift (Scan.repeat Args.name)) >> RuleCases.params) x;
wenzelm@8368
   248
wenzelm@8368
   249
wenzelm@20241
   250
(* rule format *)
wenzelm@11770
   251
wenzelm@15703
   252
fun rule_format_att x = syntax (Args.mode "no_asm"
wenzelm@15703
   253
  >> (fn true => ObjectLogic.rule_format_no_asm | false => ObjectLogic.rule_format)) x;
wenzelm@11770
   254
wenzelm@20241
   255
fun elim_format x = no_args (Thm.rule_attribute (K Tactic.make_elim)) x;
wenzelm@20241
   256
wenzelm@11770
   257
wenzelm@5879
   258
(* misc rules *)
wenzelm@5879
   259
wenzelm@18734
   260
fun standard x = no_args (Thm.rule_attribute (K Drule.standard)) x;
wenzelm@20241
   261
wenzelm@20241
   262
fun no_vars x = no_args (Thm.rule_attribute (fn ctxt => fn th =>
wenzelm@20241
   263
  let val ((_, [th']), _) = Variable.import true [th] (Context.proof_of ctxt)
wenzelm@20241
   264
  in th' end)) x;
wenzelm@20241
   265
wenzelm@18734
   266
fun eta_long x = no_args (Thm.rule_attribute (K (Drule.fconv_rule Drule.eta_long_conversion))) x;
wenzelm@5879
   267
wenzelm@5879
   268
wenzelm@15703
   269
(* internal attribute *)
wenzelm@15703
   270
wenzelm@18734
   271
fun internal att = Args.src (("Pure.attribute", [Args.mk_attribute att]), Position.none);
wenzelm@15703
   272
wenzelm@18636
   273
fun internal_att x = syntax (Scan.lift Args.internal_attribute) x;
wenzelm@15703
   274
wenzelm@15703
   275
wenzelm@18636
   276
(* theory setup *)
wenzelm@5823
   277
wenzelm@18636
   278
val _ = Context.add_setup
wenzelm@18708
   279
 (add_attributes
wenzelm@18734
   280
   [("tagged", tagged, "tagged theorem"),
wenzelm@18734
   281
    ("untagged", untagged, "untagged theorem"),
wenzelm@20906
   282
    ("kind", kind_att, "theorem kind"),
wenzelm@18734
   283
    ("COMP", COMP_att, "direct composition with rules (no lifting)"),
wenzelm@18734
   284
    ("THEN", THEN_att, "resolution with rule"),
wenzelm@18734
   285
    ("OF", OF_att, "rule applied to facts"),
wenzelm@18734
   286
    ("rename_abs", rename_abs, "rename bound variables in abstractions"),
wenzelm@18734
   287
    ("unfolded", unfolded, "unfolded definitions"),
wenzelm@18734
   288
    ("folded", folded, "folded definitions"),
wenzelm@18734
   289
    ("standard", standard, "result put into standard form"),
wenzelm@18734
   290
    ("elim_format", elim_format, "destruct rule turned into elimination rule format"),
wenzelm@18734
   291
    ("no_vars", no_vars, "frozen schematic vars"),
wenzelm@18734
   292
    ("eta_long", eta_long, "put theorem into eta long beta normal form"),
wenzelm@18734
   293
    ("consumes", consumes, "number of consumed facts"),
wenzelm@18734
   294
    ("case_names", case_names, "named rule cases"),
wenzelm@18734
   295
    ("case_conclusion", case_conclusion, "named conclusion of rule cases"),
wenzelm@18734
   296
    ("params", params, "named rule parameters"),
wenzelm@18734
   297
    ("atomize", no_args ObjectLogic.declare_atomize, "declaration of atomize rule"),
wenzelm@18734
   298
    ("rulify", no_args ObjectLogic.declare_rulify, "declaration of rulify rule"),
wenzelm@18734
   299
    ("rule_format", rule_format_att, "result put into standard rule format"),
wenzelm@18839
   300
    ("defn", add_del_args LocalDefs.defn_add LocalDefs.defn_del,
wenzelm@18839
   301
      "declaration of definitional transformations"),
wenzelm@18734
   302
    ("attribute", internal_att, "internal attribute")]);
wenzelm@5823
   303
wenzelm@5823
   304
end;
wenzelm@5823
   305
wenzelm@5823
   306
structure BasicAttrib: BASIC_ATTRIB = Attrib;
wenzelm@5823
   307
open BasicAttrib;