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