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