61
|
1 |
theory Config
|
63
|
2 |
imports "../Base"
|
61
|
3 |
begin
|
|
4 |
|
|
5 |
section {* Configuration Options *}
|
|
6 |
|
|
7 |
|
|
8 |
text {*
|
|
9 |
{\bf Problem:}
|
63
|
10 |
You would like to enhance your tool with options that can be changed
|
|
11 |
by the user without having to resort to the ML-level.\smallskip
|
61
|
12 |
|
|
13 |
{\bf Solution:} This can be achieved using configuration values.\smallskip
|
|
14 |
|
63
|
15 |
Assume you want to control three values, say @{ML_text bval} containing a
|
|
16 |
boolean, @{ML_text ival} containing an integer and @{ML_text sval}
|
|
17 |
containing a string. These values can be declared on the ML-level by
|
61
|
18 |
*}
|
|
19 |
|
|
20 |
ML {*
|
63
|
21 |
val (bval, setup_bval) = Attrib.config_bool "bval" false
|
|
22 |
val (ival, setup_ival) = Attrib.config_int "ival" 0
|
|
23 |
val (sval, setup_sval) = Attrib.config_string "sval" "some string"
|
|
24 |
*}
|
|
25 |
|
|
26 |
text {*
|
|
27 |
where each value is given a default. To enable these values, they need to
|
|
28 |
be set up using
|
|
29 |
*}
|
|
30 |
|
|
31 |
setup {* setup_bval *}
|
|
32 |
setup {* setup_ival *}
|
|
33 |
|
|
34 |
text {* or on the ML-level *}
|
|
35 |
|
|
36 |
ML {*setup_sval @{theory}
|
|
37 |
*}
|
|
38 |
|
|
39 |
text {*
|
|
40 |
The user can now manipulate the values with the command
|
61
|
41 |
*}
|
|
42 |
|
63
|
43 |
declare [[bval = true, ival = 3]]
|
|
44 |
|
|
45 |
text {*
|
|
46 |
On the ML-level these values can be retrieved using the function @{ML Config.get}:
|
|
47 |
|
|
48 |
@{ML_response [display] "Config.get @{context} bval" "true"}
|
61
|
49 |
|
63
|
50 |
@{ML_response [display] "Config.get @{context} ival" "3"}
|
|
51 |
|
|
52 |
The function @{ML Config.put} manipulates the values. For example
|
|
53 |
|
|
54 |
@{ML_response [display] "Config.put sval \"foo\" @{context}; Config.get @{context} sval" "foo"}
|
|
55 |
|
|
56 |
The same can be achived using the command \isacommand{setup}.
|
61
|
57 |
*}
|
|
58 |
|
63
|
59 |
setup {* Config.put_thy sval "bar" *}
|
|
60 |
|
|
61 |
text {*
|
|
62 |
Then retrival of this value yields now
|
|
63 |
|
|
64 |
@{ML_response [display] "Config.get @{context} sval" "\"bar\""}
|
|
65 |
|
|
66 |
We can apply a function to a value using @{ML Config.map}. For example incrementing
|
|
67 |
@{ML ival} can be done by
|
|
68 |
|
|
69 |
@{ML_response [display]
|
|
70 |
"let
|
|
71 |
val ctxt = Config.map ival (fn i => i + 1) @{context}
|
|
72 |
in
|
|
73 |
Config.get ctxt ival
|
|
74 |
end" "4"}
|
|
75 |
|
|
76 |
\begin{readmore}
|
|
77 |
For more information see @{ML_file "Pure/Isar/attrib.ML"} and @{ML_file "Pure/config.ML"}.
|
|
78 |
\end{readmore}
|
|
79 |
|
61
|
80 |
*}
|
|
81 |
|
|
82 |
|
|
83 |
text {*
|
|
84 |
|
|
85 |
Note: Avoid to use references for this purpose!
|
|
86 |
*}
|
|
87 |
|
|
88 |
end |