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