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