24007
|
1 |
(* Title: Pure/config_option.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Makarius
|
|
4 |
|
|
5 |
Configuration options as values within the local context. Global
|
|
6 |
environment of named options, with type declaration.
|
|
7 |
*)
|
|
8 |
|
|
9 |
signature CONFIG_OPTION =
|
|
10 |
sig
|
|
11 |
datatype value = Bool of bool | Int of int | String of string
|
|
12 |
type 'a T
|
|
13 |
val get: Proof.context -> 'a T -> 'a
|
|
14 |
val get_thy: theory -> 'a T -> 'a
|
|
15 |
val get_generic: Context.generic -> 'a T -> 'a
|
|
16 |
val map: 'a T -> ('a -> 'a) -> Proof.context -> Proof.context
|
|
17 |
val map_thy: 'a T -> ('a -> 'a) -> theory -> theory
|
|
18 |
val map_generic: 'a T -> ('a -> 'a) -> Context.generic -> Context.generic
|
|
19 |
val put: 'a T -> 'a -> Proof.context -> Proof.context
|
|
20 |
val put_thy: 'a T -> 'a -> theory -> theory
|
|
21 |
val put_generic: 'a T -> 'a -> Context.generic -> Context.generic
|
|
22 |
val print_options: Proof.context -> unit
|
24028
|
23 |
val the_option: theory -> xstring -> value T * value
|
|
24 |
val bool: string -> bool -> bool T * (theory -> theory)
|
|
25 |
val int: string -> int -> int T * (theory -> theory)
|
|
26 |
val string: string -> string -> string T * (theory -> theory)
|
24007
|
27 |
end;
|
|
28 |
|
|
29 |
structure ConfigOption: CONFIG_OPTION =
|
|
30 |
struct
|
|
31 |
|
24028
|
32 |
(* simple values *)
|
24007
|
33 |
|
|
34 |
datatype value =
|
|
35 |
Bool of bool |
|
|
36 |
Int of int |
|
|
37 |
String of string;
|
|
38 |
|
|
39 |
fun print_value (Bool true) = "true"
|
|
40 |
| print_value (Bool false) = "false"
|
|
41 |
| print_value (Int i) = signed_string_of_int i
|
|
42 |
| print_value (String s) = quote s;
|
|
43 |
|
|
44 |
fun print_type (Bool _) = "boolean"
|
|
45 |
| print_type (Int _) = "integer"
|
|
46 |
| print_type (String _) = "string";
|
|
47 |
|
|
48 |
fun same_type (Bool _) (Bool _) = true
|
|
49 |
| same_type (Int _) (Int _) = true
|
|
50 |
| same_type (String _) (String _) = true
|
|
51 |
| same_type _ _ = false;
|
|
52 |
|
|
53 |
fun type_check f value =
|
|
54 |
let
|
|
55 |
val value' = f value;
|
|
56 |
val _ = same_type value value' orelse
|
|
57 |
error ("Ill-typed configuration option: " ^ print_type value ^
|
|
58 |
" expected,\nbut " ^ print_type value' ^ " was found");
|
|
59 |
in value' end;
|
|
60 |
|
|
61 |
|
|
62 |
(* abstract configuration options *)
|
|
63 |
|
|
64 |
datatype 'a T = ConfigOption of
|
|
65 |
{get_value: Context.generic -> 'a,
|
|
66 |
map_value: ('a -> 'a) -> Context.generic -> Context.generic};
|
|
67 |
|
|
68 |
fun get_generic context (ConfigOption {get_value, ...}) = get_value context;
|
|
69 |
fun get_ctxt ctxt = get_generic (Context.Proof ctxt);
|
|
70 |
fun get_thy thy = get_generic (Context.Theory thy);
|
|
71 |
|
|
72 |
fun map_generic (ConfigOption {map_value, ...}) f context = map_value f context;
|
|
73 |
fun map_ctxt config f = Context.proof_map (map_generic config f);
|
|
74 |
fun map_thy config f = Context.theory_map (map_generic config f);
|
|
75 |
|
|
76 |
fun put_generic config value = map_generic config (K value);
|
|
77 |
fun put_ctxt config value = map_ctxt config (K value);
|
|
78 |
fun put_thy config value = map_thy config (K value);
|
|
79 |
|
|
80 |
|
24028
|
81 |
(* context information *)
|
|
82 |
|
|
83 |
fun err_dup_config name =
|
|
84 |
error ("Duplicate declaration of configuration option " ^ quote name);
|
24007
|
85 |
|
24028
|
86 |
(*global declarations: name, default value and type*)
|
|
87 |
structure Declaration = TheoryDataFun
|
|
88 |
(struct
|
|
89 |
type T = ((value T * value) * serial) NameSpace.table;
|
|
90 |
val empty = NameSpace.empty_table;
|
|
91 |
val copy = I;
|
|
92 |
val extend = I;
|
|
93 |
fun merge _ tabs = NameSpace.merge_tables (eq_snd (op =)) tabs
|
|
94 |
handle Symtab.DUP dup => err_dup_config dup;
|
|
95 |
end);
|
|
96 |
|
|
97 |
(*local values*)
|
|
98 |
structure Value = GenericDataFun
|
|
99 |
(
|
|
100 |
type T = value Inttab.table;
|
|
101 |
val empty = Inttab.empty;
|
|
102 |
val extend = I;
|
|
103 |
fun merge _ = Inttab.merge (K true);
|
|
104 |
);
|
24007
|
105 |
|
|
106 |
fun print_options ctxt =
|
|
107 |
let
|
24028
|
108 |
val thy = ProofContext.theory_of ctxt;
|
|
109 |
fun prt (name, ((config, default), _)) =
|
24007
|
110 |
Pretty.block [Pretty.str (name ^ ": " ^ print_type default ^ " ="), Pretty.brk 1,
|
|
111 |
Pretty.str (print_value (get_ctxt ctxt config))];
|
24028
|
112 |
val configs = NameSpace.extern_table (Declaration.get thy);
|
24007
|
113 |
in Pretty.writeln (Pretty.big_list "configuration options" (map prt configs)) end;
|
|
114 |
|
24028
|
115 |
fun the_option thy xname =
|
|
116 |
let val (space, tab) = Declaration.get thy in
|
|
117 |
(case Symtab.lookup tab (NameSpace.intern space xname) of
|
|
118 |
SOME (config, _) => config
|
|
119 |
| NONE => error ("Unknown configuration option " ^ quote xname))
|
|
120 |
end;
|
24007
|
121 |
|
|
122 |
fun declare make dest name default =
|
|
123 |
let
|
|
124 |
val id = serial ();
|
|
125 |
|
|
126 |
val default_value = make default;
|
24028
|
127 |
fun get_value context = the_default default_value (Inttab.lookup (Value.get context) id);
|
|
128 |
fun map_value f = Value.map (Inttab.map_default (id, default_value) (type_check f));
|
24007
|
129 |
val config_value = ConfigOption {get_value = get_value, map_value = map_value};
|
|
130 |
|
24028
|
131 |
val config =
|
|
132 |
ConfigOption {get_value = dest o get_value, map_value = fn f => map_value (make o f o dest)};
|
|
133 |
fun setup thy = thy |> Declaration.map (fn tab =>
|
|
134 |
NameSpace.extend_table (Sign.naming_of thy) [(name, ((config_value, default_value), id))] tab
|
|
135 |
handle Symtab.DUP dup => err_dup_config dup);
|
|
136 |
in (config, setup) end;
|
24007
|
137 |
|
|
138 |
val bool = declare Bool (fn Bool b => b);
|
|
139 |
val int = declare Int (fn Int i => i);
|
|
140 |
val string = declare String (fn String s => s);
|
|
141 |
|
|
142 |
|
|
143 |
(*final declarations of this structure!*)
|
|
144 |
val get = get_ctxt;
|
|
145 |
val map = map_ctxt;
|
|
146 |
val put = put_ctxt;
|
|
147 |
|
|
148 |
end;
|