CookBook/Recipes/Config.thy
author Christian Urban <urbanc@in.tum.de>
Sat, 10 Jan 2009 12:57:48 +0000
changeset 65 c8e9a4f97916
parent 63 83cea5dc6bac
child 67 5fbeeac2901b
permissions -rw-r--r--
tuned and added a section about creating keyword files
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
65
c8e9a4f97916 tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents: 63
diff changeset
    36
ML {*
c8e9a4f97916 tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents: 63
diff changeset
    37
setup_sval @{theory} 
63
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    38
*}
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    39
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    40
text {*
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    41
  The user can now manipulate the values with the command
61
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    42
*}
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    43
63
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    44
declare [[bval = true, ival = 3]]
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    45
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    46
text {*
65
c8e9a4f97916 tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents: 63
diff changeset
    47
  from within Isabelle. On the ML-level these values can be retrieved using the 
c8e9a4f97916 tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents: 63
diff changeset
    48
  function @{ML Config.get}:
63
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    49
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    50
  @{ML_response [display] "Config.get @{context} bval" "true"}
61
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    51
63
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    52
  @{ML_response [display] "Config.get @{context} ival" "3"}
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    53
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    54
  The function @{ML Config.put} manipulates the values. For example
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    55
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    56
  @{ML_response [display] "Config.put sval \"foo\" @{context}; Config.get @{context} sval" "foo"}
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    57
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    58
  The same can be achived using the command \isacommand{setup}.
61
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    59
*}
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    60
63
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    61
setup {* Config.put_thy sval "bar" *}
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    62
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    63
text {* 
65
c8e9a4f97916 tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents: 63
diff changeset
    64
  The retrival of this value yields now
63
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    65
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    66
  @{ML_response [display] "Config.get @{context} sval" "\"bar\""}
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    67
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    68
  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
    69
  @{ML ival} can be done by
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    70
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    71
  @{ML_response [display] 
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    72
"let 
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    73
  val ctxt = Config.map ival (fn i => i + 1) @{context}
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    74
in 
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    75
  Config.get ctxt ival
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    76
end" "4"}
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    77
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    78
\begin{readmore}
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    79
  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
    80
\end{readmore}
Christian Urban <urbanc@in.tum.de>
parents: 61
diff changeset
    81
61
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    82
*}
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    83
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    84
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    85
text {*
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    86
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    87
  Note: Avoid to use references for this purpose!
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    88
  *}
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    89
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    90
end