src/Pure/Isar/attrib.ML
author wenzelm
Sun Jun 30 11:37:34 2013 +0200 (2013-06-30)
changeset 52487 48bc24467008
parent 52470 dedd7952a62c
child 52683 fb028440473e
permissions -rw-r--r--
backout dedd7952a62c: static "proofs" value within theory prevents later inferencing with different configuration;
wenzelm@5823
     1
(*  Title:      Pure/Isar/attrib.ML
wenzelm@5823
     2
    Author:     Markus Wenzel, TU Muenchen
wenzelm@5823
     3
wenzelm@18734
     4
Symbolic representation of attributes -- with name and syntax.
wenzelm@5823
     5
*)
wenzelm@5823
     6
wenzelm@5823
     7
signature ATTRIB =
wenzelm@5823
     8
sig
wenzelm@27729
     9
  type src = Args.src
haftmann@29581
    10
  type binding = binding * src list
haftmann@28965
    11
  val empty_binding: binding
wenzelm@45584
    12
  val is_empty_binding: binding -> bool
wenzelm@27729
    13
  val print_attributes: theory -> unit
wenzelm@16458
    14
  val intern: theory -> xstring -> string
wenzelm@16458
    15
  val intern_src: theory -> src -> src
wenzelm@21031
    16
  val pretty_attribs: Proof.context -> src list -> Pretty.T list
wenzelm@26891
    17
  val defined: theory -> string -> bool
wenzelm@47815
    18
  val attribute: Proof.context -> src -> attribute
wenzelm@47815
    19
  val attribute_global: theory -> src -> attribute
wenzelm@47815
    20
  val attribute_cmd: Proof.context -> src -> attribute
wenzelm@47815
    21
  val attribute_cmd_global: theory -> src -> attribute
wenzelm@45390
    22
  val map_specs: ('a list -> 'att list) ->
wenzelm@30759
    23
    (('c * 'a list) * 'b) list -> (('c * 'att list) * 'b) list
wenzelm@45390
    24
  val map_facts: ('a list -> 'att list) ->
wenzelm@17105
    25
    (('c * 'a list) * ('d * 'a list) list) list ->
wenzelm@18905
    26
    (('c * 'att list) * ('d * 'att list) list) list
wenzelm@45390
    27
  val map_facts_refs: ('a list -> 'att list) -> ('b -> 'fact) ->
wenzelm@30759
    28
    (('c * 'a list) * ('b * 'a list) list) list ->
wenzelm@30759
    29
    (('c * 'att list) * ('fact * 'att list) list) list
wenzelm@47249
    30
  val global_notes: string -> (binding * (thm list * src list) list) list ->
wenzelm@47249
    31
    theory -> (string * thm list) list * theory
wenzelm@47249
    32
  val local_notes: string -> (binding * (thm list * src list) list) list ->
wenzelm@47249
    33
    Proof.context -> (string * thm list) list * Proof.context
wenzelm@47249
    34
  val generic_notes: string -> (binding * (thm list * src list) list) list ->
wenzelm@47249
    35
    Context.generic -> (string * thm list) list * Context.generic
wenzelm@38330
    36
  val eval_thms: Proof.context -> (Facts.ref * src list) list -> thm list
wenzelm@30525
    37
  val setup: Binding.binding -> attribute context_parser -> string -> theory -> theory
wenzelm@30575
    38
  val attribute_setup: bstring * Position.T -> Symbol_Pos.text * Position.T -> string ->
wenzelm@30575
    39
    theory -> theory
wenzelm@30525
    40
  val add_del: attribute -> attribute -> attribute context_parser
wenzelm@30513
    41
  val thm_sel: Facts.interval list parser
wenzelm@30513
    42
  val thm: thm context_parser
wenzelm@30513
    43
  val thms: thm list context_parser
wenzelm@30513
    44
  val multi_thm: thm list context_parser
wenzelm@45493
    45
  val partial_evaluation: Proof.context ->
wenzelm@45493
    46
    (binding * (thm list * Args.src list) list) list ->
wenzelm@45493
    47
    (binding * (thm list * Args.src list) list) list
wenzelm@45493
    48
  val internal: (morphism -> attribute) -> src
wenzelm@52060
    49
  val print_options: Proof.context -> unit
wenzelm@42616
    50
  val config_bool: Binding.binding ->
wenzelm@42616
    51
    (Context.generic -> bool) -> bool Config.T * (theory -> theory)
wenzelm@42616
    52
  val config_int: Binding.binding ->
wenzelm@42616
    53
    (Context.generic -> int) -> int Config.T * (theory -> theory)
wenzelm@42616
    54
  val config_real: Binding.binding ->
wenzelm@42616
    55
    (Context.generic -> real) -> real Config.T * (theory -> theory)
wenzelm@42616
    56
  val config_string: Binding.binding ->
wenzelm@42616
    57
    (Context.generic -> string) -> string Config.T * (theory -> theory)
wenzelm@42616
    58
  val setup_config_bool: Binding.binding -> (Context.generic -> bool) -> bool Config.T
wenzelm@42616
    59
  val setup_config_int: Binding.binding -> (Context.generic -> int) -> int Config.T
wenzelm@52040
    60
  val setup_config_real: Binding.binding -> (Context.generic -> real) -> real Config.T
wenzelm@42616
    61
  val setup_config_string: Binding.binding -> (Context.generic -> string) -> string Config.T
wenzelm@52040
    62
  val option_bool: string -> bool Config.T * (theory -> theory)
wenzelm@52040
    63
  val option_int: string -> int Config.T * (theory -> theory)
wenzelm@52040
    64
  val option_real: string -> real Config.T * (theory -> theory)
wenzelm@52040
    65
  val option_string: string -> string Config.T * (theory -> theory)
wenzelm@52040
    66
  val setup_option_bool: string -> bool Config.T
wenzelm@52040
    67
  val setup_option_int: string -> int Config.T
wenzelm@52040
    68
  val setup_option_real: string -> real Config.T
wenzelm@52040
    69
  val setup_option_string: string -> string Config.T
wenzelm@5823
    70
end;
wenzelm@5823
    71
wenzelm@5823
    72
structure Attrib: ATTRIB =
wenzelm@5823
    73
struct
wenzelm@5823
    74
wenzelm@28084
    75
(* source and bindings *)
wenzelm@28084
    76
wenzelm@15703
    77
type src = Args.src;
wenzelm@15703
    78
haftmann@29581
    79
type binding = binding * src list;
wenzelm@45584
    80
haftmann@28965
    81
val empty_binding: binding = (Binding.empty, []);
wenzelm@45584
    82
fun is_empty_binding ((b, srcs): binding) = Binding.is_empty b andalso null srcs;
wenzelm@28084
    83
wenzelm@28084
    84
wenzelm@27729
    85
wenzelm@18734
    86
(** named attributes **)
wenzelm@18636
    87
wenzelm@18734
    88
(* theory data *)
wenzelm@5823
    89
wenzelm@33522
    90
structure Attributes = Theory_Data
wenzelm@22846
    91
(
wenzelm@33095
    92
  type T = ((src -> attribute) * string) Name_Space.table;
wenzelm@33159
    93
  val empty : T = Name_Space.empty_table "attribute";
wenzelm@16458
    94
  val extend = I;
wenzelm@33522
    95
  fun merge data : T = Name_Space.merge_tables data;
wenzelm@22846
    96
);
wenzelm@5823
    97
wenzelm@22846
    98
fun print_attributes thy =
wenzelm@22846
    99
  let
wenzelm@42360
   100
    val ctxt = Proof_Context.init_global thy;
wenzelm@24110
   101
    val attribs = Attributes.get thy;
wenzelm@50301
   102
    fun prt_attr (name, (_, "")) = Pretty.mark_str name
wenzelm@42813
   103
      | prt_attr (name, (_, comment)) =
wenzelm@50301
   104
          Pretty.block
wenzelm@50301
   105
            (Pretty.mark_str name :: Pretty.str ":" :: Pretty.brk 2 :: Pretty.text comment);
wenzelm@22846
   106
  in
wenzelm@42358
   107
    [Pretty.big_list "attributes:" (map prt_attr (Name_Space.extern_table ctxt attribs))]
wenzelm@22846
   108
    |> Pretty.chunks |> Pretty.writeln
wenzelm@22846
   109
  end;
wenzelm@7611
   110
wenzelm@33092
   111
fun add_attribute name att comment thy = thy
wenzelm@47005
   112
  |> Attributes.map (Name_Space.define (Context.Theory thy) true (name, (att, comment)) #> snd);
wenzelm@31306
   113
wenzelm@5823
   114
wenzelm@21031
   115
(* name space *)
wenzelm@15703
   116
wenzelm@33095
   117
val intern = Name_Space.intern o #1 o Attributes.get;
wenzelm@15703
   118
val intern_src = Args.map_name o intern;
wenzelm@15703
   119
wenzelm@42360
   120
fun extern ctxt = Name_Space.extern ctxt (#1 (Attributes.get (Proof_Context.theory_of ctxt)));
wenzelm@21031
   121
wenzelm@21031
   122
wenzelm@21031
   123
(* pretty printing *)
wenzelm@21031
   124
wenzelm@21031
   125
fun pretty_attribs _ [] = []
wenzelm@21031
   126
  | pretty_attribs ctxt srcs =
wenzelm@38329
   127
      [Pretty.enum "," "[" "]" (map (Args.pretty_src ctxt o Args.map_name (extern ctxt)) srcs)];
wenzelm@21031
   128
wenzelm@15703
   129
wenzelm@18734
   130
(* get attributes *)
wenzelm@5823
   131
wenzelm@26891
   132
val defined = Symtab.defined o #2 o Attributes.get;
wenzelm@26891
   133
wenzelm@47816
   134
fun attribute_generic context =
wenzelm@5823
   135
  let
wenzelm@47816
   136
    val thy = Context.theory_of context;
wenzelm@42380
   137
    val (space, tab) = Attributes.get thy;
wenzelm@5879
   138
    fun attr src =
wenzelm@16344
   139
      let val ((name, _), pos) = Args.dest_src src in
wenzelm@42380
   140
        (case Symtab.lookup tab name of
wenzelm@48992
   141
          NONE => error ("Unknown attribute: " ^ quote name ^ Position.here pos)
wenzelm@47816
   142
        | SOME (att, _) =>
wenzelm@47816
   143
            (Context_Position.report_generic context pos (Name_Space.markup space name); att src))
wenzelm@5823
   144
      end;
wenzelm@5823
   145
  in attr end;
wenzelm@5823
   146
wenzelm@47816
   147
val attribute = attribute_generic o Context.Proof;
wenzelm@47816
   148
val attribute_global = attribute_generic o Context.Theory;
wenzelm@47815
   149
wenzelm@47816
   150
fun attribute_cmd ctxt = attribute ctxt o intern_src (Proof_Context.theory_of ctxt);
wenzelm@47815
   151
fun attribute_cmd_global thy = attribute_global thy o intern_src thy;
wenzelm@18636
   152
wenzelm@5823
   153
wenzelm@17105
   154
(* attributed declarations *)
wenzelm@17105
   155
wenzelm@45390
   156
fun map_specs f = map (apfst (apsnd f));
wenzelm@30759
   157
wenzelm@45390
   158
fun map_facts f = map (apfst (apsnd f) o apsnd (map (apsnd f)));
wenzelm@30759
   159
fun map_facts_refs f g = map_facts f #> map (apsnd (map (apfst g)));
wenzelm@17105
   160
wenzelm@17105
   161
wenzelm@38330
   162
(* fact expressions *)
wenzelm@38330
   163
wenzelm@47249
   164
fun global_notes kind facts thy = thy |>
wenzelm@47815
   165
  Global_Theory.note_thmss kind (map_facts (map (attribute_global thy)) facts);
wenzelm@47249
   166
wenzelm@47249
   167
fun local_notes kind facts ctxt = ctxt |>
wenzelm@47815
   168
  Proof_Context.note_thmss kind (map_facts (map (attribute ctxt)) facts);
wenzelm@47249
   169
wenzelm@47249
   170
fun generic_notes kind facts context = context |>
wenzelm@47249
   171
  Context.mapping_result (global_notes kind facts) (local_notes kind facts);
wenzelm@47249
   172
wenzelm@38330
   173
fun eval_thms ctxt srcs = ctxt
wenzelm@42360
   174
  |> Proof_Context.note_thmss ""
wenzelm@47815
   175
    (map_facts_refs (map (attribute_cmd ctxt)) (Proof_Context.get_fact ctxt)
wenzelm@38330
   176
      [((Binding.empty, []), srcs)])
wenzelm@38330
   177
  |> fst |> maps snd;
wenzelm@38330
   178
wenzelm@38330
   179
wenzelm@30525
   180
(* attribute setup *)
wenzelm@30525
   181
wenzelm@31306
   182
fun syntax scan = Args.syntax "attribute" scan;
wenzelm@30525
   183
wenzelm@31306
   184
fun setup name scan =
wenzelm@31306
   185
  add_attribute name
wenzelm@31306
   186
    (fn src => fn (ctxt, th) => let val (a, ctxt') = syntax scan src ctxt in a (ctxt', th) end);
wenzelm@5879
   187
wenzelm@30525
   188
fun attribute_setup name (txt, pos) cmt =
wenzelm@30525
   189
  Context.theory_map (ML_Context.expression pos
wenzelm@30525
   190
    "val (name, scan, comment): binding * attribute context_parser * string"
wenzelm@30525
   191
    "Context.map_theory (Attrib.setup name scan comment)"
wenzelm@37198
   192
    (ML_Lex.read Position.none ("(" ^ ML_Syntax.make_binding name ^ ", ") @
wenzelm@37198
   193
      ML_Lex.read pos txt @
wenzelm@37198
   194
      ML_Lex.read Position.none (", " ^ ML_Syntax.print_string cmt ^ ")")));
wenzelm@30525
   195
wenzelm@30525
   196
wenzelm@31305
   197
(* add/del syntax *)
wenzelm@5823
   198
wenzelm@31305
   199
fun add_del add del = Scan.lift (Args.add >> K add || Args.del >> K del || Scan.succeed add);
wenzelm@5879
   200
wenzelm@5879
   201
wenzelm@25983
   202
wenzelm@25983
   203
(** parsing attributed theorems **)
wenzelm@5879
   204
wenzelm@36950
   205
val thm_sel = Parse.$$$ "(" |-- Parse.list1
wenzelm@36950
   206
 (Parse.nat --| Parse.minus -- Parse.nat >> Facts.FromTo ||
wenzelm@36950
   207
  Parse.nat --| Parse.minus >> Facts.From ||
wenzelm@36950
   208
  Parse.nat >> Facts.Single) --| Parse.$$$ ")";
wenzelm@27812
   209
wenzelm@18636
   210
local
wenzelm@18636
   211
wenzelm@21698
   212
val fact_name = Args.internal_fact >> K "<fact>" || Args.name;
wenzelm@21698
   213
wenzelm@26336
   214
fun gen_thm pick = Scan.depend (fn context =>
wenzelm@26336
   215
  let
wenzelm@26336
   216
    val thy = Context.theory_of context;
wenzelm@42360
   217
    val get = Context.cases (Global_Theory.get_fact context) Proof_Context.get_fact context;
wenzelm@26361
   218
    val get_fact = get o Facts.Fact;
wenzelm@27820
   219
    fun get_named pos name = get (Facts.Named ((name, pos), NONE));
wenzelm@26336
   220
  in
wenzelm@36950
   221
    Parse.$$$ "[" |-- Args.attribs (intern thy) --| Parse.$$$ "]" >> (fn srcs =>
wenzelm@24008
   222
      let
wenzelm@47815
   223
        val atts = map (attribute_generic context) srcs;
wenzelm@46775
   224
        val (th', context') = fold (uncurry o Thm.apply_attribute) atts (Drule.dummy_thm, context);
wenzelm@26336
   225
      in (context', pick "" [th']) end)
wenzelm@26336
   226
    ||
wenzelm@26336
   227
    (Scan.ahead Args.alt_name -- Args.named_fact get_fact
wenzelm@27820
   228
      >> (fn (s, fact) => ("", Facts.Fact s, fact)) ||
wenzelm@36950
   229
     Scan.ahead (Parse.position fact_name) :|-- (fn (name, pos) =>
wenzelm@27820
   230
      Args.named_fact (get_named pos) -- Scan.option thm_sel
wenzelm@27820
   231
        >> (fn (fact, sel) => (name, Facts.Named ((name, pos), sel), fact))))
wenzelm@26336
   232
    -- Args.opt_attribs (intern thy) >> (fn ((name, thmref, fact), srcs) =>
wenzelm@26336
   233
      let
wenzelm@26336
   234
        val ths = Facts.select thmref fact;
wenzelm@47815
   235
        val atts = map (attribute_generic context) srcs;
wenzelm@46775
   236
        val (ths', context') =
wenzelm@46775
   237
          fold_map (curry (fold (uncurry o Thm.apply_attribute) atts)) ths context;
wenzelm@26336
   238
      in (context', pick name ths') end)
wenzelm@26336
   239
  end);
berghofe@15456
   240
wenzelm@18636
   241
in
wenzelm@18636
   242
wenzelm@26336
   243
val thm = gen_thm Facts.the_single;
wenzelm@18998
   244
val multi_thm = gen_thm (K I);
wenzelm@19482
   245
val thms = Scan.repeat multi_thm >> flat;
wenzelm@18636
   246
wenzelm@18636
   247
end;
wenzelm@18636
   248
wenzelm@5823
   249
wenzelm@5879
   250
wenzelm@45586
   251
(** partial evaluation -- observing rule/declaration/mixed attributes **)
wenzelm@45493
   252
wenzelm@45493
   253
local
wenzelm@45526
   254
wenzelm@45586
   255
val strict_eq_thm = op = o pairself Thm.rep_thm;
wenzelm@45493
   256
wenzelm@45493
   257
fun apply_att src (context, th) =
wenzelm@45526
   258
  let
wenzelm@45526
   259
    val src1 = Args.assignable src;
wenzelm@47815
   260
    val result = attribute_generic context src1 (context, th);
wenzelm@45526
   261
    val src2 = Args.closure src1;
wenzelm@45526
   262
  in (src2, result) end;
wenzelm@45493
   263
wenzelm@45527
   264
fun err msg src =
wenzelm@45527
   265
  let val ((name, _), pos) = Args.dest_src src
wenzelm@48992
   266
  in error (msg ^ " " ^ quote name ^ Position.here pos) end;
wenzelm@45527
   267
wenzelm@45527
   268
fun eval src ((th, dyn), (decls, context)) =
wenzelm@45527
   269
  (case (apply_att src (context, th), dyn) of
wenzelm@45527
   270
    ((_, (NONE, SOME th')), NONE) => ((th', NONE), (decls, context))
wenzelm@45527
   271
  | ((_, (NONE, SOME _)), SOME _) => err "Mixed dynamic attribute followed by static rule" src
wenzelm@45527
   272
  | ((src', (SOME context', NONE)), NONE) =>
wenzelm@45493
   273
      let
wenzelm@45493
   274
        val decls' =
wenzelm@45493
   275
          (case decls of
wenzelm@45526
   276
            [] => [(th, [src'])]
wenzelm@45493
   277
          | (th2, srcs2) :: rest =>
wenzelm@45586
   278
              if strict_eq_thm (th, th2)
wenzelm@45526
   279
              then ((th2, src' :: srcs2) :: rest)
wenzelm@45526
   280
              else (th, [src']) :: (th2, srcs2) :: rest);
wenzelm@45527
   281
      in ((th, NONE), (decls', context')) end
wenzelm@45527
   282
  | ((src', (opt_context', opt_th')), _) =>
wenzelm@45527
   283
      let
wenzelm@45527
   284
        val context' = the_default context opt_context';
wenzelm@45527
   285
        val th' = the_default th opt_th';
wenzelm@45527
   286
        val dyn' =
wenzelm@45527
   287
          (case dyn of
wenzelm@45527
   288
            NONE => SOME (th, [src'])
wenzelm@45527
   289
          | SOME (dyn_th, srcs) => SOME (dyn_th, src' :: srcs));
wenzelm@45527
   290
      in ((th', dyn'), (decls, context')) end);
wenzelm@45493
   291
wenzelm@45493
   292
in
wenzelm@45493
   293
wenzelm@45493
   294
fun partial_evaluation ctxt facts =
wenzelm@45586
   295
  (facts, Context.Proof (Context_Position.set_visible false ctxt)) |->
wenzelm@45586
   296
    fold_map (fn ((b, more_atts), fact) => fn context =>
wenzelm@45586
   297
      let
wenzelm@45586
   298
        val (fact', (decls, context')) =
wenzelm@45586
   299
          (fact, ([], context)) |-> fold_map (fn (ths, atts) => fn res1 =>
wenzelm@45586
   300
            (ths, res1) |-> fold_map (fn th => fn res2 =>
wenzelm@45586
   301
              let
wenzelm@45586
   302
                val ((th', dyn'), res3) = fold eval (atts @ more_atts) ((th, NONE), res2);
wenzelm@45586
   303
                val th_atts' =
wenzelm@45586
   304
                  (case dyn' of
wenzelm@45586
   305
                    NONE => (th', [])
wenzelm@45586
   306
                  | SOME (dyn_th', atts') => (dyn_th', rev atts'));
wenzelm@45586
   307
              in (th_atts', res3) end))
wenzelm@45586
   308
          |>> flat;
wenzelm@45586
   309
        val decls' = rev (map (apsnd rev) decls);
wenzelm@45586
   310
        val facts' =
wenzelm@45586
   311
          if eq_list (eq_fst strict_eq_thm) (decls', fact') then
wenzelm@45586
   312
            [((b, []), map2 (fn (th, atts1) => fn (_, atts2) => (th, atts1 @ atts2)) decls' fact')]
wenzelm@45619
   313
          else if null decls' then [((b, []), fact')]
wenzelm@45586
   314
          else [(empty_binding, decls'), ((b, []), fact')];
wenzelm@45586
   315
      in (facts', context') end)
wenzelm@46906
   316
  |> fst |> flat |> map (apsnd (map (apfst single)))
wenzelm@46906
   317
  |> filter_out (fn (b, fact) => is_empty_binding b andalso forall (null o #2) fact);
wenzelm@45493
   318
wenzelm@45493
   319
end;
wenzelm@45493
   320
wenzelm@45493
   321
wenzelm@45493
   322
wenzelm@25983
   323
(** basic attributes **)
wenzelm@25983
   324
wenzelm@25983
   325
(* internal *)
wenzelm@25983
   326
wenzelm@36959
   327
fun internal att = Args.src (("Pure.attribute", [Token.mk_attribute att]), Position.none);
wenzelm@25983
   328
wenzelm@25983
   329
wenzelm@25983
   330
(* rule composition *)
wenzelm@25983
   331
wenzelm@25983
   332
val THEN_att =
wenzelm@36950
   333
  Scan.lift (Scan.optional (Args.bracks Parse.nat) 1) -- thm
wenzelm@31305
   334
    >> (fn (i, B) => Thm.rule_attribute (fn _ => fn A => A RSN (i, B)));
wenzelm@25983
   335
wenzelm@25983
   336
val OF_att =
wenzelm@31305
   337
  thms >> (fn Bs => Thm.rule_attribute (fn _ => fn A => Bs MRS A));
wenzelm@25983
   338
wenzelm@25983
   339
wenzelm@25983
   340
(* rename_abs *)
wenzelm@25983
   341
wenzelm@45375
   342
val rename_abs =
wenzelm@45375
   343
  Scan.repeat (Args.maybe Args.name)
wenzelm@45375
   344
  >> (fn args => Thm.rule_attribute (K (Drule.rename_bvars' args)));
wenzelm@25983
   345
wenzelm@25983
   346
wenzelm@25983
   347
(* unfold / fold definitions *)
wenzelm@25983
   348
wenzelm@25983
   349
fun unfolded_syntax rule =
wenzelm@31305
   350
  thms >> (fn ths => Thm.rule_attribute (fn context => rule (Context.proof_of context) ths));
wenzelm@25983
   351
wenzelm@35624
   352
val unfolded = unfolded_syntax Local_Defs.unfold;
wenzelm@35624
   353
val folded = unfolded_syntax Local_Defs.fold;
wenzelm@25983
   354
wenzelm@25983
   355
wenzelm@25983
   356
(* rule format *)
wenzelm@25983
   357
wenzelm@31305
   358
val rule_format = Args.mode "no_asm"
wenzelm@35625
   359
  >> (fn true => Object_Logic.rule_format_no_asm | false => Object_Logic.rule_format);
wenzelm@25983
   360
wenzelm@31305
   361
val elim_format = Thm.rule_attribute (K Tactic.make_elim);
wenzelm@25983
   362
wenzelm@25983
   363
nipkow@44046
   364
(* case names *)
nipkow@44046
   365
wenzelm@45375
   366
val case_names =
wenzelm@45375
   367
  Scan.repeat1 (Args.name --
wenzelm@45375
   368
    Scan.optional (Parse.$$$ "[" |-- Scan.repeat1 (Args.maybe Args.name) --| Parse.$$$ "]") []) >>
wenzelm@45375
   369
  (fn cs =>
wenzelm@45375
   370
    Rule_Cases.cases_hyp_names (map fst cs)
wenzelm@45375
   371
      (map (map (the_default Rule_Cases.case_hypsN) o snd) cs));
wenzelm@44053
   372
nipkow@44046
   373
wenzelm@25983
   374
(* misc rules *)
wenzelm@25983
   375
wenzelm@31305
   376
val no_vars = Thm.rule_attribute (fn context => fn th =>
wenzelm@26715
   377
  let
wenzelm@26715
   378
    val ctxt = Variable.set_body false (Context.proof_of context);
wenzelm@31794
   379
    val ((_, [th']), _) = Variable.import true [th] ctxt;
wenzelm@31305
   380
  in th' end);
wenzelm@25983
   381
wenzelm@25983
   382
val eta_long =
wenzelm@31305
   383
  Thm.rule_attribute (K (Conv.fconv_rule Drule.eta_long_conversion));
wenzelm@25983
   384
wenzelm@36950
   385
val rotated = Scan.optional Parse.int 1 >> (fn n => Thm.rule_attribute (K (rotate_prems n)));
berghofe@29690
   386
wenzelm@25983
   387
wenzelm@25983
   388
(* theory setup *)
wenzelm@5823
   389
wenzelm@26463
   390
val _ = Context.>> (Context.map_theory
wenzelm@31305
   391
 (setup (Binding.name "attribute") (Scan.lift Args.internal_attribute >> Morphism.form)
wenzelm@31305
   392
    "internal attribute" #>
wenzelm@31305
   393
  setup (Binding.name "tagged") (Scan.lift (Args.name -- Args.name) >> Thm.tag) "tagged theorem" #>
wenzelm@31305
   394
  setup (Binding.name "untagged") (Scan.lift Args.name >> Thm.untag) "untagged theorem" #>
wenzelm@31305
   395
  setup (Binding.name "kind") (Scan.lift Args.name >> Thm.kind) "theorem kind" #>
wenzelm@31305
   396
  setup (Binding.name "THEN") THEN_att "resolution with rule" #>
wenzelm@31305
   397
  setup (Binding.name "OF") OF_att "rule applied to facts" #>
wenzelm@31305
   398
  setup (Binding.name "rename_abs") (Scan.lift rename_abs)
wenzelm@31305
   399
    "rename bound variables in abstractions" #>
wenzelm@31305
   400
  setup (Binding.name "unfolded") unfolded "unfolded definitions" #>
wenzelm@31305
   401
  setup (Binding.name "folded") folded "folded definitions" #>
wenzelm@50772
   402
  setup (Binding.name "consumes") (Scan.lift (Scan.optional Parse.int 1) >> Rule_Cases.consumes)
wenzelm@31305
   403
    "number of consumed facts" #>
wenzelm@36950
   404
  setup (Binding.name "constraints") (Scan.lift Parse.nat >> Rule_Cases.constraints)
berghofe@34986
   405
    "number of equality constraints" #>
wenzelm@45375
   406
  setup (Binding.name "case_names") (Scan.lift case_names) "named rule cases" #>
wenzelm@31305
   407
  setup (Binding.name "case_conclusion")
wenzelm@33368
   408
    (Scan.lift (Args.name -- Scan.repeat Args.name) >> Rule_Cases.case_conclusion)
wenzelm@31305
   409
    "named conclusion of rule cases" #>
wenzelm@31305
   410
  setup (Binding.name "params")
wenzelm@36950
   411
    (Scan.lift (Parse.and_list1 (Scan.repeat Args.name)) >> Rule_Cases.params)
wenzelm@31305
   412
    "named rule parameters" #>
wenzelm@35021
   413
  setup (Binding.name "standard") (Scan.succeed (Thm.rule_attribute (K Drule.export_without_context)))
wenzelm@31305
   414
    "result put into standard form (legacy)" #>
wenzelm@31305
   415
  setup (Binding.name "rule_format") rule_format "result put into canonical rule format" #>
wenzelm@31305
   416
  setup (Binding.name "elim_format") (Scan.succeed elim_format)
wenzelm@31305
   417
    "destruct rule turned into elimination rule format" #>
wenzelm@31305
   418
  setup (Binding.name "no_vars") (Scan.succeed no_vars) "frozen schematic vars" #>
wenzelm@31305
   419
  setup (Binding.name "eta_long") (Scan.succeed eta_long)
wenzelm@31305
   420
    "put theorem into eta long beta normal form" #>
wenzelm@35625
   421
  setup (Binding.name "atomize") (Scan.succeed Object_Logic.declare_atomize)
wenzelm@31305
   422
    "declaration of atomize rule" #>
wenzelm@35625
   423
  setup (Binding.name "rulify") (Scan.succeed Object_Logic.declare_rulify)
wenzelm@31305
   424
    "declaration of rulify rule" #>
wenzelm@31305
   425
  setup (Binding.name "rotated") (Scan.lift rotated) "rotated theorem premises" #>
wenzelm@35624
   426
  setup (Binding.name "defn") (add_del Local_Defs.defn_add Local_Defs.defn_del)
wenzelm@31305
   427
    "declaration of definitional transformations" #>
wenzelm@46903
   428
  setup (Binding.name "abs_def")
wenzelm@46903
   429
    (Scan.succeed (Thm.rule_attribute (fn context =>
wenzelm@46903
   430
      Local_Defs.meta_rewrite_rule (Context.proof_of context) #> Drule.abs_def)))
webertj@51035
   431
    "abstract over free variables of definitional theorem"));
wenzelm@8633
   432
wenzelm@5823
   433
wenzelm@5823
   434
wenzelm@24110
   435
(** configuration options **)
wenzelm@24110
   436
wenzelm@24110
   437
(* naming *)
wenzelm@24110
   438
wenzelm@33522
   439
structure Configs = Theory_Data
wenzelm@24713
   440
(
wenzelm@39163
   441
  type T = Config.raw Symtab.table;
wenzelm@24110
   442
  val empty = Symtab.empty;
wenzelm@24110
   443
  val extend = I;
wenzelm@33522
   444
  fun merge data = Symtab.merge (K true) data;
wenzelm@24713
   445
);
wenzelm@5823
   446
wenzelm@52060
   447
fun print_options ctxt =
wenzelm@24110
   448
  let
wenzelm@42360
   449
    val thy = Proof_Context.theory_of ctxt;
wenzelm@24110
   450
    fun prt (name, config) =
wenzelm@24110
   451
      let val value = Config.get ctxt config in
wenzelm@50301
   452
        Pretty.block [Pretty.mark_str name, Pretty.str (": " ^ Config.print_type value ^ " ="),
wenzelm@50301
   453
          Pretty.brk 1, Pretty.str (Config.print_value value)]
wenzelm@24110
   454
      end;
wenzelm@42358
   455
    val configs = Name_Space.extern_table ctxt (#1 (Attributes.get thy), Configs.get thy);
wenzelm@24110
   456
  in Pretty.writeln (Pretty.big_list "configuration options" (map prt configs)) end;
wenzelm@24110
   457
wenzelm@24110
   458
wenzelm@24110
   459
(* concrete syntax *)
wenzelm@23988
   460
wenzelm@24003
   461
local
wenzelm@24003
   462
wenzelm@36950
   463
val equals = Parse.$$$ "=";
wenzelm@24003
   464
wenzelm@24110
   465
fun scan_value (Config.Bool _) =
wenzelm@24110
   466
      equals -- Args.$$$ "false" >> K (Config.Bool false) ||
wenzelm@24110
   467
      equals -- Args.$$$ "true" >> K (Config.Bool true) ||
wenzelm@24110
   468
      Scan.succeed (Config.Bool true)
wenzelm@36950
   469
  | scan_value (Config.Int _) = equals |-- Parse.int >> Config.Int
wenzelm@40291
   470
  | scan_value (Config.Real _) = equals |-- Parse.real >> Config.Real
wenzelm@24110
   471
  | scan_value (Config.String _) = equals |-- Args.name >> Config.String;
wenzelm@24003
   472
wenzelm@24110
   473
fun scan_config thy config =
wenzelm@36787
   474
  let val config_type = Config.get_global thy config
wenzelm@24110
   475
  in scan_value config_type >> (K o Thm.declaration_attribute o K o Config.put_generic config) end;
wenzelm@24003
   476
wenzelm@42616
   477
fun register binding config thy =
wenzelm@42616
   478
  let val name = Sign.full_name thy binding in
wenzelm@24126
   479
    thy
wenzelm@42616
   480
    |> setup binding (Scan.lift (scan_config thy config) >> Morphism.form) "configuration option"
wenzelm@24126
   481
    |> Configs.map (Symtab.update (name, config))
wenzelm@24126
   482
  end;
wenzelm@24110
   483
wenzelm@42808
   484
fun declare make coerce binding default =
wenzelm@24110
   485
  let
wenzelm@42616
   486
    val name = Binding.name_of binding;
wenzelm@52039
   487
    val config_value = Config.declare name (make o default);
wenzelm@24110
   488
    val config = coerce config_value;
wenzelm@42616
   489
  in (config, register binding config_value) end;
wenzelm@42616
   490
wenzelm@42616
   491
in
wenzelm@42616
   492
wenzelm@52040
   493
fun register_config config = register (Binding.name (Config.name_of config)) config;
wenzelm@52040
   494
wenzelm@42808
   495
val config_bool = declare Config.Bool Config.bool;
wenzelm@42808
   496
val config_int = declare Config.Int Config.int;
wenzelm@42808
   497
val config_real = declare Config.Real Config.real;
wenzelm@42808
   498
val config_string = declare Config.String Config.string;
wenzelm@42616
   499
wenzelm@42616
   500
end;
wenzelm@24110
   501
wenzelm@42616
   502
wenzelm@42616
   503
(* implicit setup *)
wenzelm@42616
   504
wenzelm@42616
   505
local
wenzelm@24110
   506
wenzelm@42616
   507
fun setup_config declare_config binding default =
wenzelm@42616
   508
  let
wenzelm@42616
   509
    val (config, setup) = declare_config binding default;
wenzelm@42616
   510
    val _ = Context.>> (Context.map_theory setup);
wenzelm@42616
   511
  in config end;
wenzelm@42616
   512
wenzelm@42616
   513
in
wenzelm@42616
   514
wenzelm@42616
   515
val setup_config_bool = setup_config config_bool;
wenzelm@42616
   516
val setup_config_int = setup_config config_int;
wenzelm@42616
   517
val setup_config_string = setup_config config_string;
wenzelm@42616
   518
val setup_config_real = setup_config config_real;
wenzelm@24003
   519
wenzelm@24003
   520
end;
wenzelm@23988
   521
wenzelm@23988
   522
wenzelm@52040
   523
(* system options *)
wenzelm@52040
   524
wenzelm@52040
   525
local
wenzelm@52040
   526
wenzelm@52040
   527
fun declare_option coerce name =
wenzelm@52040
   528
  let
wenzelm@52040
   529
    val config = Config.declare_option name;
wenzelm@52040
   530
  in (coerce config, register_config config) end;
wenzelm@52040
   531
wenzelm@52040
   532
fun setup_option coerce name =
wenzelm@52040
   533
  let
wenzelm@52040
   534
    val config = Config.declare_option name;
wenzelm@52040
   535
    val _ = Context.>> (Context.map_theory (register_config config));
wenzelm@52040
   536
  in coerce config end;
wenzelm@52040
   537
wenzelm@52040
   538
in
wenzelm@52040
   539
wenzelm@52040
   540
val option_bool = declare_option Config.bool;
wenzelm@52040
   541
val option_int = declare_option Config.int;
wenzelm@52040
   542
val option_real = declare_option Config.real;
wenzelm@52040
   543
val option_string = declare_option Config.string;
wenzelm@52040
   544
wenzelm@52040
   545
val setup_option_bool = setup_option Config.bool;
wenzelm@52040
   546
val setup_option_int = setup_option Config.int;
wenzelm@52040
   547
val setup_option_real = setup_option Config.real;
wenzelm@52040
   548
val setup_option_string = setup_option Config.string;
wenzelm@52040
   549
wenzelm@52040
   550
end;
wenzelm@52040
   551
wenzelm@52040
   552
wenzelm@18636
   553
(* theory setup *)
wenzelm@5823
   554
wenzelm@26463
   555
val _ = Context.>> (Context.map_theory
wenzelm@52059
   556
 (register_config quick_and_dirty_raw #>
wenzelm@52059
   557
  register_config Ast.trace_raw #>
wenzelm@43347
   558
  register_config Ast.stats_raw #>
wenzelm@42289
   559
  register_config Printer.show_brackets_raw #>
wenzelm@42289
   560
  register_config Printer.show_sorts_raw #>
wenzelm@42289
   561
  register_config Printer.show_types_raw #>
wenzelm@49657
   562
  register_config Printer.show_markup_raw #>
wenzelm@42289
   563
  register_config Printer.show_structs_raw #>
wenzelm@42289
   564
  register_config Printer.show_question_marks_raw #>
wenzelm@46512
   565
  register_config Syntax.ambiguity_warning_raw #>
wenzelm@46506
   566
  register_config Syntax.ambiguity_limit_raw #>
wenzelm@42284
   567
  register_config Syntax_Trans.eta_contract_raw #>
wenzelm@42669
   568
  register_config Name_Space.names_long_raw #>
wenzelm@42669
   569
  register_config Name_Space.names_short_raw #>
wenzelm@42669
   570
  register_config Name_Space.names_unique_raw #>
wenzelm@41375
   571
  register_config ML_Context.trace_raw #>
wenzelm@42360
   572
  register_config Proof_Context.show_abbrevs_raw #>
wenzelm@39163
   573
  register_config Goal_Display.goals_limit_raw #>
wenzelm@39163
   574
  register_config Goal_Display.show_main_goal_raw #>
wenzelm@39163
   575
  register_config Goal_Display.show_consts_raw #>
wenzelm@39166
   576
  register_config Display.show_hyps_raw #>
wenzelm@39166
   577
  register_config Display.show_tags_raw #>
wenzelm@39163
   578
  register_config Unify.trace_bound_raw #>
wenzelm@39163
   579
  register_config Unify.search_bound_raw #>
wenzelm@39163
   580
  register_config Unify.trace_simp_raw #>
wenzelm@39163
   581
  register_config Unify.trace_types_raw #>
wenzelm@41228
   582
  register_config Raw_Simplifier.simp_depth_limit_raw #>
wenzelm@41228
   583
  register_config Raw_Simplifier.simp_trace_depth_limit_raw #>
wenzelm@41228
   584
  register_config Raw_Simplifier.simp_debug_raw #>
wenzelm@41228
   585
  register_config Raw_Simplifier.simp_trace_raw));
wenzelm@5823
   586
wenzelm@5823
   587
end;