src/Pure/Isar/attrib.ML
author wenzelm
Mon Mar 31 12:35:39 2014 +0200 (2014-03-31 ago)
changeset 56334 6b3739fee456
parent 56303 4cc3f4db3447
child 56436 30ccec1e82fb
permissions -rw-r--r--
some shortcuts for chunks, which sometimes avoid bulky string output;
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@55997
    13
  val print_attributes: Proof.context -> unit
wenzelm@55997
    14
  val check_name_generic: Context.generic -> xstring * Position.T -> string
wenzelm@55997
    15
  val check_name: Proof.context -> xstring * Position.T -> string
wenzelm@55997
    16
  val check_src: Proof.context -> src -> src
wenzelm@21031
    17
  val pretty_attribs: Proof.context -> src list -> Pretty.T list
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@55828
    38
  val attribute_setup: bstring * Position.T -> Symbol_Pos.source -> string -> theory -> theory
wenzelm@55140
    39
  val internal: (morphism -> attribute) -> src
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@52060
    48
  val print_options: Proof.context -> unit
wenzelm@42616
    49
  val config_bool: Binding.binding ->
wenzelm@42616
    50
    (Context.generic -> bool) -> bool Config.T * (theory -> theory)
wenzelm@42616
    51
  val config_int: Binding.binding ->
wenzelm@42616
    52
    (Context.generic -> int) -> int Config.T * (theory -> theory)
wenzelm@42616
    53
  val config_real: Binding.binding ->
wenzelm@42616
    54
    (Context.generic -> real) -> real Config.T * (theory -> theory)
wenzelm@42616
    55
  val config_string: Binding.binding ->
wenzelm@42616
    56
    (Context.generic -> string) -> string Config.T * (theory -> theory)
wenzelm@42616
    57
  val setup_config_bool: Binding.binding -> (Context.generic -> bool) -> bool Config.T
wenzelm@42616
    58
  val setup_config_int: Binding.binding -> (Context.generic -> int) -> int Config.T
wenzelm@52040
    59
  val setup_config_real: Binding.binding -> (Context.generic -> real) -> real Config.T
wenzelm@42616
    60
  val setup_config_string: Binding.binding -> (Context.generic -> string) -> string Config.T
wenzelm@52040
    61
  val option_bool: string -> bool Config.T * (theory -> theory)
wenzelm@52040
    62
  val option_int: string -> int Config.T * (theory -> theory)
wenzelm@52040
    63
  val option_real: string -> real Config.T * (theory -> theory)
wenzelm@52040
    64
  val option_string: string -> string Config.T * (theory -> theory)
wenzelm@52040
    65
  val setup_option_bool: string -> bool Config.T
wenzelm@52040
    66
  val setup_option_int: string -> int Config.T
wenzelm@52040
    67
  val setup_option_real: string -> real Config.T
wenzelm@52040
    68
  val setup_option_string: string -> string Config.T
wenzelm@5823
    69
end;
wenzelm@5823
    70
wenzelm@5823
    71
structure Attrib: ATTRIB =
wenzelm@5823
    72
struct
wenzelm@5823
    73
wenzelm@28084
    74
(* source and bindings *)
wenzelm@28084
    75
wenzelm@15703
    76
type src = Args.src;
wenzelm@15703
    77
haftmann@29581
    78
type binding = binding * src list;
wenzelm@45584
    79
haftmann@28965
    80
val empty_binding: binding = (Binding.empty, []);
wenzelm@45584
    81
fun is_empty_binding ((b, srcs): binding) = Binding.is_empty b andalso null srcs;
wenzelm@28084
    82
wenzelm@28084
    83
wenzelm@27729
    84
wenzelm@18734
    85
(** named attributes **)
wenzelm@18636
    86
wenzelm@18734
    87
(* theory data *)
wenzelm@5823
    88
wenzelm@33522
    89
structure Attributes = Theory_Data
wenzelm@22846
    90
(
wenzelm@33095
    91
  type T = ((src -> attribute) * string) Name_Space.table;
wenzelm@33159
    92
  val empty : T = Name_Space.empty_table "attribute";
wenzelm@16458
    93
  val extend = I;
wenzelm@33522
    94
  fun merge data : T = Name_Space.merge_tables data;
wenzelm@22846
    95
);
wenzelm@5823
    96
wenzelm@55997
    97
val get_attributes = Attributes.get o Context.theory_of;
wenzelm@55997
    98
wenzelm@55997
    99
fun print_attributes ctxt =
wenzelm@22846
   100
  let
wenzelm@55997
   101
    val attribs = get_attributes (Context.Proof ctxt);
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@56052
   107
    [Pretty.big_list "attributes:" (map prt_attr (Name_Space.markup_table ctxt attribs))]
wenzelm@56334
   108
    |> Pretty.writeln_chunks
wenzelm@22846
   109
  end;
wenzelm@7611
   110
wenzelm@56027
   111
val attribute_space = Name_Space.space_of_table o get_attributes o Context.Proof;
wenzelm@56027
   112
wenzelm@33092
   113
fun add_attribute name att comment thy = thy
wenzelm@47005
   114
  |> Attributes.map (Name_Space.define (Context.Theory thy) true (name, (att, comment)) #> snd);
wenzelm@31306
   115
wenzelm@5823
   116
wenzelm@55997
   117
(* check *)
wenzelm@15703
   118
wenzelm@55997
   119
fun check_name_generic context = #1 o Name_Space.check context (get_attributes context);
wenzelm@56032
   120
val check_name = check_name_generic o Context.Proof;
wenzelm@15703
   121
wenzelm@56033
   122
fun check_src ctxt src =
wenzelm@56033
   123
 (Context_Position.report ctxt (Args.range_of_src src) Markup.language_attribute;
wenzelm@56033
   124
  #1 (Args.check_src ctxt (get_attributes (Context.Proof ctxt)) src));
wenzelm@21031
   125
wenzelm@21031
   126
wenzelm@21031
   127
(* pretty printing *)
wenzelm@21031
   128
wenzelm@21031
   129
fun pretty_attribs _ [] = []
wenzelm@56032
   130
  | pretty_attribs ctxt srcs = [Pretty.enum "," "[" "]" (map (Args.pretty_src ctxt) srcs)];
wenzelm@21031
   131
wenzelm@15703
   132
wenzelm@18734
   133
(* get attributes *)
wenzelm@5823
   134
wenzelm@47816
   135
fun attribute_generic context =
wenzelm@56029
   136
  let val table = get_attributes context
wenzelm@56029
   137
  in fn src => #1 (Name_Space.get table (#1 (Args.name_of_src src))) src end;
wenzelm@5823
   138
wenzelm@47816
   139
val attribute = attribute_generic o Context.Proof;
wenzelm@47816
   140
val attribute_global = attribute_generic o Context.Theory;
wenzelm@47815
   141
wenzelm@55997
   142
fun attribute_cmd ctxt = attribute ctxt o check_src ctxt;
wenzelm@56032
   143
fun attribute_cmd_global thy = attribute_global thy o check_src (Proof_Context.init_global thy);
wenzelm@18636
   144
wenzelm@5823
   145
wenzelm@17105
   146
(* attributed declarations *)
wenzelm@17105
   147
wenzelm@45390
   148
fun map_specs f = map (apfst (apsnd f));
wenzelm@30759
   149
wenzelm@45390
   150
fun map_facts f = map (apfst (apsnd f) o apsnd (map (apsnd f)));
wenzelm@30759
   151
fun map_facts_refs f g = map_facts f #> map (apsnd (map (apfst g)));
wenzelm@17105
   152
wenzelm@17105
   153
wenzelm@38330
   154
(* fact expressions *)
wenzelm@38330
   155
wenzelm@47249
   156
fun global_notes kind facts thy = thy |>
wenzelm@47815
   157
  Global_Theory.note_thmss kind (map_facts (map (attribute_global thy)) facts);
wenzelm@47249
   158
wenzelm@47249
   159
fun local_notes kind facts ctxt = ctxt |>
wenzelm@47815
   160
  Proof_Context.note_thmss kind (map_facts (map (attribute ctxt)) facts);
wenzelm@47249
   161
wenzelm@47249
   162
fun generic_notes kind facts context = context |>
wenzelm@47249
   163
  Context.mapping_result (global_notes kind facts) (local_notes kind facts);
wenzelm@47249
   164
wenzelm@38330
   165
fun eval_thms ctxt srcs = ctxt
wenzelm@42360
   166
  |> Proof_Context.note_thmss ""
wenzelm@47815
   167
    (map_facts_refs (map (attribute_cmd ctxt)) (Proof_Context.get_fact ctxt)
wenzelm@38330
   168
      [((Binding.empty, []), srcs)])
wenzelm@38330
   169
  |> fst |> maps snd;
wenzelm@38330
   170
wenzelm@38330
   171
wenzelm@30525
   172
(* attribute setup *)
wenzelm@30525
   173
wenzelm@31306
   174
fun setup name scan =
wenzelm@31306
   175
  add_attribute name
wenzelm@56032
   176
    (fn src => fn (ctxt, th) =>
wenzelm@56032
   177
      let val (a, ctxt') = Args.syntax_generic scan src ctxt in a (ctxt', th) end);
wenzelm@5879
   178
wenzelm@55828
   179
fun attribute_setup name source cmt =
wenzelm@55828
   180
  Context.theory_map (ML_Context.expression (#pos source)
wenzelm@30525
   181
    "val (name, scan, comment): binding * attribute context_parser * string"
wenzelm@30525
   182
    "Context.map_theory (Attrib.setup name scan comment)"
wenzelm@37198
   183
    (ML_Lex.read Position.none ("(" ^ ML_Syntax.make_binding name ^ ", ") @
wenzelm@56278
   184
      ML_Lex.read_source false source @
wenzelm@37198
   185
      ML_Lex.read Position.none (", " ^ ML_Syntax.print_string cmt ^ ")")));
wenzelm@30525
   186
wenzelm@30525
   187
wenzelm@55140
   188
(* internal attribute *)
wenzelm@55140
   189
wenzelm@56029
   190
fun internal att = Args.src ("Pure.attribute", Position.none) [Token.mk_attribute att];
wenzelm@55140
   191
wenzelm@55140
   192
val _ = Theory.setup
wenzelm@55140
   193
 (setup (Binding.name "attribute") (Scan.lift Args.internal_attribute >> Morphism.form)
wenzelm@55140
   194
    "internal attribute");
wenzelm@55140
   195
wenzelm@55140
   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@56140
   216
    val get = Proof_Context.get_fact_generic context;
wenzelm@26361
   217
    val get_fact = get o Facts.Fact;
wenzelm@27820
   218
    fun get_named pos name = get (Facts.Named ((name, pos), NONE));
wenzelm@26336
   219
  in
wenzelm@55997
   220
    Parse.$$$ "[" |-- Args.attribs (check_name_generic context) --| Parse.$$$ "]" >> (fn srcs =>
wenzelm@24008
   221
      let
wenzelm@47815
   222
        val atts = map (attribute_generic context) srcs;
wenzelm@46775
   223
        val (th', context') = fold (uncurry o Thm.apply_attribute) atts (Drule.dummy_thm, context);
wenzelm@55740
   224
      in (context', pick ("", Position.none) [th']) end)
wenzelm@26336
   225
    ||
wenzelm@26336
   226
    (Scan.ahead Args.alt_name -- Args.named_fact get_fact
wenzelm@27820
   227
      >> (fn (s, fact) => ("", Facts.Fact s, fact)) ||
wenzelm@36950
   228
     Scan.ahead (Parse.position fact_name) :|-- (fn (name, pos) =>
wenzelm@27820
   229
      Args.named_fact (get_named pos) -- Scan.option thm_sel
wenzelm@27820
   230
        >> (fn (fact, sel) => (name, Facts.Named ((name, pos), sel), fact))))
wenzelm@55997
   231
    -- Args.opt_attribs (check_name_generic context) >> (fn ((name, thmref, fact), srcs) =>
wenzelm@26336
   232
      let
wenzelm@26336
   233
        val ths = Facts.select thmref fact;
wenzelm@47815
   234
        val atts = map (attribute_generic context) srcs;
wenzelm@46775
   235
        val (ths', context') =
wenzelm@46775
   236
          fold_map (curry (fold (uncurry o Thm.apply_attribute) atts)) ths context;
wenzelm@55740
   237
      in (context', pick (name, Facts.pos_of_ref thmref) ths') end)
wenzelm@26336
   238
  end);
berghofe@15456
   239
wenzelm@18636
   240
in
wenzelm@18636
   241
wenzelm@26336
   242
val thm = gen_thm Facts.the_single;
wenzelm@18998
   243
val multi_thm = gen_thm (K I);
wenzelm@19482
   244
val thms = Scan.repeat multi_thm >> flat;
wenzelm@18636
   245
wenzelm@18636
   246
end;
wenzelm@18636
   247
wenzelm@5823
   248
wenzelm@5879
   249
wenzelm@45586
   250
(** partial evaluation -- observing rule/declaration/mixed attributes **)
wenzelm@45493
   251
wenzelm@56140
   252
(*NB: result length may change due to rearrangement of symbolic expression*)
wenzelm@56140
   253
wenzelm@45493
   254
local
wenzelm@45526
   255
wenzelm@45493
   256
fun apply_att src (context, th) =
wenzelm@45526
   257
  let
wenzelm@55914
   258
    val src1 = Args.init_assignable src;
wenzelm@47815
   259
    val result = attribute_generic context src1 (context, th);
wenzelm@45526
   260
    val src2 = Args.closure src1;
wenzelm@45526
   261
  in (src2, result) end;
wenzelm@45493
   262
wenzelm@45527
   263
fun err msg src =
wenzelm@56029
   264
  let val (name, pos) = Args.name_of_src src
wenzelm@48992
   265
  in error (msg ^ " " ^ quote name ^ Position.here pos) end;
wenzelm@45527
   266
wenzelm@45527
   267
fun eval src ((th, dyn), (decls, context)) =
wenzelm@45527
   268
  (case (apply_att src (context, th), dyn) of
wenzelm@45527
   269
    ((_, (NONE, SOME th')), NONE) => ((th', NONE), (decls, context))
wenzelm@45527
   270
  | ((_, (NONE, SOME _)), SOME _) => err "Mixed dynamic attribute followed by static rule" src
wenzelm@45527
   271
  | ((src', (SOME context', NONE)), NONE) =>
wenzelm@45493
   272
      let
wenzelm@45493
   273
        val decls' =
wenzelm@45493
   274
          (case decls of
wenzelm@45526
   275
            [] => [(th, [src'])]
wenzelm@45493
   276
          | (th2, srcs2) :: rest =>
wenzelm@52683
   277
              if Thm.eq_thm_strict (th, th2)
wenzelm@45526
   278
              then ((th2, src' :: srcs2) :: rest)
wenzelm@45526
   279
              else (th, [src']) :: (th2, srcs2) :: rest);
wenzelm@45527
   280
      in ((th, NONE), (decls', context')) end
wenzelm@45527
   281
  | ((src', (opt_context', opt_th')), _) =>
wenzelm@45527
   282
      let
wenzelm@45527
   283
        val context' = the_default context opt_context';
wenzelm@45527
   284
        val th' = the_default th opt_th';
wenzelm@45527
   285
        val dyn' =
wenzelm@45527
   286
          (case dyn of
wenzelm@45527
   287
            NONE => SOME (th, [src'])
wenzelm@45527
   288
          | SOME (dyn_th, srcs) => SOME (dyn_th, src' :: srcs));
wenzelm@45527
   289
      in ((th', dyn'), (decls, context')) end);
wenzelm@45493
   290
wenzelm@45493
   291
in
wenzelm@45493
   292
wenzelm@45493
   293
fun partial_evaluation ctxt facts =
wenzelm@55998
   294
  (facts, Context.Proof ctxt) |->
wenzelm@45586
   295
    fold_map (fn ((b, more_atts), fact) => fn context =>
wenzelm@45586
   296
      let
wenzelm@45586
   297
        val (fact', (decls, context')) =
wenzelm@45586
   298
          (fact, ([], context)) |-> fold_map (fn (ths, atts) => fn res1 =>
wenzelm@45586
   299
            (ths, res1) |-> fold_map (fn th => fn res2 =>
wenzelm@45586
   300
              let
wenzelm@45586
   301
                val ((th', dyn'), res3) = fold eval (atts @ more_atts) ((th, NONE), res2);
wenzelm@45586
   302
                val th_atts' =
wenzelm@45586
   303
                  (case dyn' of
wenzelm@45586
   304
                    NONE => (th', [])
wenzelm@45586
   305
                  | SOME (dyn_th', atts') => (dyn_th', rev atts'));
wenzelm@45586
   306
              in (th_atts', res3) end))
wenzelm@45586
   307
          |>> flat;
wenzelm@45586
   308
        val decls' = rev (map (apsnd rev) decls);
wenzelm@45586
   309
        val facts' =
wenzelm@52683
   310
          if eq_list (eq_fst Thm.eq_thm_strict) (decls', fact') then
wenzelm@45586
   311
            [((b, []), map2 (fn (th, atts1) => fn (_, atts2) => (th, atts1 @ atts2)) decls' fact')]
wenzelm@45619
   312
          else if null decls' then [((b, []), fact')]
wenzelm@45586
   313
          else [(empty_binding, decls'), ((b, []), fact')];
wenzelm@45586
   314
      in (facts', context') end)
wenzelm@46906
   315
  |> fst |> flat |> map (apsnd (map (apfst single)))
wenzelm@46906
   316
  |> filter_out (fn (b, fact) => is_empty_binding b andalso forall (null o #2) fact);
wenzelm@45493
   317
wenzelm@45493
   318
end;
wenzelm@45493
   319
wenzelm@45493
   320
wenzelm@45493
   321
wenzelm@24110
   322
(** configuration options **)
wenzelm@24110
   323
wenzelm@24110
   324
(* naming *)
wenzelm@24110
   325
wenzelm@33522
   326
structure Configs = Theory_Data
wenzelm@24713
   327
(
wenzelm@39163
   328
  type T = Config.raw Symtab.table;
wenzelm@24110
   329
  val empty = Symtab.empty;
wenzelm@24110
   330
  val extend = I;
wenzelm@33522
   331
  fun merge data = Symtab.merge (K true) data;
wenzelm@24713
   332
);
wenzelm@5823
   333
wenzelm@52060
   334
fun print_options ctxt =
wenzelm@24110
   335
  let
wenzelm@24110
   336
    fun prt (name, config) =
wenzelm@24110
   337
      let val value = Config.get ctxt config in
wenzelm@50301
   338
        Pretty.block [Pretty.mark_str name, Pretty.str (": " ^ Config.print_type value ^ " ="),
wenzelm@50301
   339
          Pretty.brk 1, Pretty.str (Config.print_value value)]
wenzelm@24110
   340
      end;
wenzelm@56027
   341
    val space = attribute_space ctxt;
wenzelm@56052
   342
    val configs =
wenzelm@56052
   343
      Name_Space.markup_entries ctxt space
wenzelm@56052
   344
        (Symtab.dest (Configs.get (Proof_Context.theory_of ctxt)));
wenzelm@24110
   345
  in Pretty.writeln (Pretty.big_list "configuration options" (map prt configs)) end;
wenzelm@24110
   346
wenzelm@24110
   347
wenzelm@24110
   348
(* concrete syntax *)
wenzelm@23988
   349
wenzelm@24003
   350
local
wenzelm@24003
   351
wenzelm@36950
   352
val equals = Parse.$$$ "=";
wenzelm@24003
   353
wenzelm@24110
   354
fun scan_value (Config.Bool _) =
wenzelm@24110
   355
      equals -- Args.$$$ "false" >> K (Config.Bool false) ||
wenzelm@24110
   356
      equals -- Args.$$$ "true" >> K (Config.Bool true) ||
wenzelm@24110
   357
      Scan.succeed (Config.Bool true)
wenzelm@36950
   358
  | scan_value (Config.Int _) = equals |-- Parse.int >> Config.Int
wenzelm@40291
   359
  | scan_value (Config.Real _) = equals |-- Parse.real >> Config.Real
wenzelm@24110
   360
  | scan_value (Config.String _) = equals |-- Args.name >> Config.String;
wenzelm@24003
   361
wenzelm@24110
   362
fun scan_config thy config =
wenzelm@36787
   363
  let val config_type = Config.get_global thy config
wenzelm@24110
   364
  in scan_value config_type >> (K o Thm.declaration_attribute o K o Config.put_generic config) end;
wenzelm@24003
   365
wenzelm@42616
   366
fun register binding config thy =
wenzelm@42616
   367
  let val name = Sign.full_name thy binding in
wenzelm@24126
   368
    thy
wenzelm@42616
   369
    |> setup binding (Scan.lift (scan_config thy config) >> Morphism.form) "configuration option"
wenzelm@24126
   370
    |> Configs.map (Symtab.update (name, config))
wenzelm@24126
   371
  end;
wenzelm@24110
   372
wenzelm@42808
   373
fun declare make coerce binding default =
wenzelm@24110
   374
  let
wenzelm@42616
   375
    val name = Binding.name_of binding;
wenzelm@52039
   376
    val config_value = Config.declare name (make o default);
wenzelm@24110
   377
    val config = coerce config_value;
wenzelm@42616
   378
  in (config, register binding config_value) end;
wenzelm@42616
   379
wenzelm@42616
   380
in
wenzelm@42616
   381
wenzelm@52040
   382
fun register_config config = register (Binding.name (Config.name_of config)) config;
wenzelm@52040
   383
wenzelm@42808
   384
val config_bool = declare Config.Bool Config.bool;
wenzelm@42808
   385
val config_int = declare Config.Int Config.int;
wenzelm@42808
   386
val config_real = declare Config.Real Config.real;
wenzelm@42808
   387
val config_string = declare Config.String Config.string;
wenzelm@42616
   388
wenzelm@42616
   389
end;
wenzelm@24110
   390
wenzelm@42616
   391
wenzelm@42616
   392
(* implicit setup *)
wenzelm@42616
   393
wenzelm@42616
   394
local
wenzelm@24110
   395
wenzelm@42616
   396
fun setup_config declare_config binding default =
wenzelm@42616
   397
  let
wenzelm@42616
   398
    val (config, setup) = declare_config binding default;
wenzelm@53171
   399
    val _ = Theory.setup setup;
wenzelm@42616
   400
  in config end;
wenzelm@42616
   401
wenzelm@42616
   402
in
wenzelm@42616
   403
wenzelm@42616
   404
val setup_config_bool = setup_config config_bool;
wenzelm@42616
   405
val setup_config_int = setup_config config_int;
wenzelm@42616
   406
val setup_config_string = setup_config config_string;
wenzelm@42616
   407
val setup_config_real = setup_config config_real;
wenzelm@24003
   408
wenzelm@24003
   409
end;
wenzelm@23988
   410
wenzelm@23988
   411
wenzelm@52040
   412
(* system options *)
wenzelm@52040
   413
wenzelm@52040
   414
local
wenzelm@52040
   415
wenzelm@52040
   416
fun declare_option coerce name =
wenzelm@52040
   417
  let
wenzelm@52040
   418
    val config = Config.declare_option name;
wenzelm@52040
   419
  in (coerce config, register_config config) end;
wenzelm@52040
   420
wenzelm@52040
   421
fun setup_option coerce name =
wenzelm@52040
   422
  let
wenzelm@52040
   423
    val config = Config.declare_option name;
wenzelm@53171
   424
    val _ = Theory.setup (register_config config);
wenzelm@52040
   425
  in coerce config end;
wenzelm@52040
   426
wenzelm@52040
   427
in
wenzelm@52040
   428
wenzelm@52040
   429
val option_bool = declare_option Config.bool;
wenzelm@52040
   430
val option_int = declare_option Config.int;
wenzelm@52040
   431
val option_real = declare_option Config.real;
wenzelm@52040
   432
val option_string = declare_option Config.string;
wenzelm@52040
   433
wenzelm@52040
   434
val setup_option_bool = setup_option Config.bool;
wenzelm@52040
   435
val setup_option_int = setup_option Config.int;
wenzelm@52040
   436
val setup_option_real = setup_option Config.real;
wenzelm@52040
   437
val setup_option_string = setup_option Config.string;
wenzelm@52040
   438
wenzelm@52040
   439
end;
wenzelm@52040
   440
wenzelm@52040
   441
wenzelm@18636
   442
(* theory setup *)
wenzelm@5823
   443
wenzelm@53171
   444
val _ = Theory.setup
wenzelm@52059
   445
 (register_config quick_and_dirty_raw #>
wenzelm@52059
   446
  register_config Ast.trace_raw #>
wenzelm@43347
   447
  register_config Ast.stats_raw #>
wenzelm@42289
   448
  register_config Printer.show_brackets_raw #>
wenzelm@42289
   449
  register_config Printer.show_sorts_raw #>
wenzelm@42289
   450
  register_config Printer.show_types_raw #>
wenzelm@49657
   451
  register_config Printer.show_markup_raw #>
wenzelm@42289
   452
  register_config Printer.show_structs_raw #>
wenzelm@42289
   453
  register_config Printer.show_question_marks_raw #>
wenzelm@46512
   454
  register_config Syntax.ambiguity_warning_raw #>
wenzelm@46506
   455
  register_config Syntax.ambiguity_limit_raw #>
wenzelm@42284
   456
  register_config Syntax_Trans.eta_contract_raw #>
wenzelm@42669
   457
  register_config Name_Space.names_long_raw #>
wenzelm@42669
   458
  register_config Name_Space.names_short_raw #>
wenzelm@42669
   459
  register_config Name_Space.names_unique_raw #>
wenzelm@56303
   460
  register_config ML_Options.source_trace_raw #>
wenzelm@56303
   461
  register_config ML_Options.exception_trace_raw #>
wenzelm@56303
   462
  register_config ML_Options.print_depth_raw #>
wenzelm@42360
   463
  register_config Proof_Context.show_abbrevs_raw #>
wenzelm@39163
   464
  register_config Goal_Display.goals_limit_raw #>
wenzelm@39163
   465
  register_config Goal_Display.show_main_goal_raw #>
wenzelm@39163
   466
  register_config Goal_Display.show_consts_raw #>
wenzelm@39166
   467
  register_config Display.show_hyps_raw #>
wenzelm@39166
   468
  register_config Display.show_tags_raw #>
wenzelm@52709
   469
  register_config Pattern.unify_trace_failure_raw #>
wenzelm@39163
   470
  register_config Unify.trace_bound_raw #>
wenzelm@39163
   471
  register_config Unify.search_bound_raw #>
wenzelm@39163
   472
  register_config Unify.trace_simp_raw #>
wenzelm@39163
   473
  register_config Unify.trace_types_raw #>
wenzelm@41228
   474
  register_config Raw_Simplifier.simp_depth_limit_raw #>
wenzelm@41228
   475
  register_config Raw_Simplifier.simp_trace_depth_limit_raw #>
wenzelm@41228
   476
  register_config Raw_Simplifier.simp_debug_raw #>
wenzelm@53171
   477
  register_config Raw_Simplifier.simp_trace_raw);
wenzelm@5823
   478
wenzelm@5823
   479
end;