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