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