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