ProgTutorial/Recipes/Config.thy
author Christian Urban <urbanc@in.tum.de>
Tue, 28 Jul 2009 08:53:05 +0200
changeset 292 41a802bbb7df
parent 191 0150cf5982ae
permissions -rw-r--r--
added more to the ML-antiquotation section
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
61
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
     1
theory Config
63
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
     2
imports "../Base"
61
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
     3
begin
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
     4
119
4536782969fa added an acknowledgement section
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
     5
section {* Configuration Options\label{rec:config} *} 
61
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
     6
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
     7
text {*
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
     8
  {\bf Problem:} 
63
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
     9
  You would like to enhance your tool with options that can be changed 
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    10
  by the user without having to resort to the ML-level.\smallskip
61
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    11
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    12
  {\bf Solution:} This can be achieved using configuration values.\smallskip
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    13
168
009ca4807baa polished somewhat the recipes and solutions
Christian Urban <urbanc@in.tum.de>
parents: 119
diff changeset
    14
  Assume you want to control three values, say @{text bval} containing a
102
5e309df58557 general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents: 74
diff changeset
    15
  boolean,  @{text ival} containing an integer and @{text sval} 
168
009ca4807baa polished somewhat the recipes and solutions
Christian Urban <urbanc@in.tum.de>
parents: 119
diff changeset
    16
  containing a string. These values can be declared on the ML-level by
61
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    17
*}
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    18
69
19106a9975c1 highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    19
ML{*val (bval, setup_bval) = Attrib.config_bool "bval" false
63
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    20
val (ival, setup_ival) = Attrib.config_int "ival" 0
69
19106a9975c1 highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    21
val (sval, setup_sval) = Attrib.config_string "sval" "some string" *}
63
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    22
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    23
text {* 
67
Christian Urban <urbanc@in.tum.de>
parents: 65
diff changeset
    24
  where each value needs to be given a default. To enable these values, they need to 
168
009ca4807baa polished somewhat the recipes and solutions
Christian Urban <urbanc@in.tum.de>
parents: 119
diff changeset
    25
  be set up with
63
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    26
*}
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    27
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    28
setup {* setup_bval *} 
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    29
setup {* setup_ival *}
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    30
191
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    31
text {* or on the ML-level with *}
63
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    32
69
19106a9975c1 highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    33
ML{*setup_sval @{theory} *}
63
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    34
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    35
text {*
67
Christian Urban <urbanc@in.tum.de>
parents: 65
diff changeset
    36
  The user can now manipulate the values from within Isabelle with the command
61
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    37
*}
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    38
63
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    39
declare [[bval = true, ival = 3]]
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    40
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    41
text {*
67
Christian Urban <urbanc@in.tum.de>
parents: 65
diff changeset
    42
  On the ML-level these values can be retrieved using the 
65
c8e9a4f97916 tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents: 63
diff changeset
    43
  function @{ML Config.get}:
63
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    44
72
7b8c4fe235aa added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
    45
  @{ML_response [display,gray] "Config.get @{context} bval" "true"}
61
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    46
72
7b8c4fe235aa added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
    47
  @{ML_response [display,gray] "Config.get @{context} ival" "3"}
63
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    48
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    49
  The function @{ML Config.put} manipulates the values. For example
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    50
72
7b8c4fe235aa added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
    51
  @{ML_response [display,gray] "Config.put sval \"foo\" @{context}; Config.get @{context} sval" "foo"}
63
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    52
168
009ca4807baa polished somewhat the recipes and solutions
Christian Urban <urbanc@in.tum.de>
parents: 119
diff changeset
    53
  The same can be achieved using the command \isacommand{setup}.
61
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    54
*}
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    55
63
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    56
setup {* Config.put_thy sval "bar" *}
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    57
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    58
text {* 
191
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    59
  Now the retrieval of this value yields:
63
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    60
72
7b8c4fe235aa added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
    61
  @{ML_response [display,gray] "Config.get @{context} sval" "\"bar\""}
63
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    62
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    63
  We can apply a function to a value using @{ML Config.map}. For example incrementing
168
009ca4807baa polished somewhat the recipes and solutions
Christian Urban <urbanc@in.tum.de>
parents: 119
diff changeset
    64
  @{ML ival} can be done by:
63
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    65
72
7b8c4fe235aa added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
    66
  @{ML_response [display,gray] 
63
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    67
"let 
191
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    68
  val ctxt' = Config.map ival (fn i => i + 1) @{context}
63
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    69
in 
191
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    70
  Config.get ctxt' ival
63
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    71
end" "4"}
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    72
68
e7519207c2b7 added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents: 67
diff changeset
    73
  \begin{readmore}
63
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    74
  For more information see @{ML_file "Pure/Isar/attrib.ML"} and @{ML_file "Pure/config.ML"}.
68
e7519207c2b7 added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents: 67
diff changeset
    75
  \end{readmore}
63
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    76
68
e7519207c2b7 added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents: 67
diff changeset
    77
  There are many good reasons to control parameters in this way. One is
191
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    78
  that no global reference is needed, which would cause many headaches 
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    79
  with the multithreaded execution of Isabelle.
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    80
*}
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    81
61
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    82
end