ProgTutorial/output_tutorial.ML
author Christian Urban <urbanc@in.tum.de>
Tue, 18 Aug 2009 01:05:56 +0200
changeset 311 ee864694315b
parent 302 0cbd34857b9e
child 315 de49d5780f57
permissions -rw-r--r--
polished
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
255
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
256
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
     2
structure OutputTutorial =
255
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
struct
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
(* rebuilding the output function from ThyOutput in order to *)
302
Christian Urban <urbanc@in.tum.de>
parents: 261
diff changeset
     6
(* enable the options [gray, linenos, index]                 *)
255
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
val gray = ref false
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
val linenos = ref false
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
311
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 302
diff changeset
    11
255
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
fun output txt =
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
let
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
  val prts = map Pretty.str txt
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
in   
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
  prts
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
  |> (if ! ThyOutput.quotes then map Pretty.quote else I)
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
  |> (if ! ThyOutput.display then
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
    map (Output.output o Pretty.string_of o Pretty.indent (! ThyOutput.indent))
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
    #> space_implode "\\isasep\\isanewline%\n"
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
    #> (if ! linenos then (enclose "\\begin{linenos}%\n" "%\n\\end{linenos}") else I) 
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
    #> (if ! gray then (enclose "\\begin{graybox}%\n" "%\n\\end{graybox}") else I) 
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
    #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
  else
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
    map (Output.output o (if ! ThyOutput.break then Pretty.string_of else Pretty.str_of))
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
    #> space_implode "\\isasep\\isanewline%\n"
256
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    27
    #> enclose "\\isa{" "}")
255
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
end
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
256
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    30
datatype qstring =
258
03145998190b slightly modified index generation
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
    31
  NoString
302
Christian Urban <urbanc@in.tum.de>
parents: 261
diff changeset
    32
| Plain   of string
Christian Urban <urbanc@in.tum.de>
parents: 261
diff changeset
    33
| Code    of string
Christian Urban <urbanc@in.tum.de>
parents: 261
diff changeset
    34
| IStruct of string
256
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    35
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    36
datatype entry = 
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    37
   No_Entry
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    38
 | Default 
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    39
 | Explicit of string
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    40
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    41
val index = ref No_Entry
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    42
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    43
fun translate f = Symbol.explode #> map f #> implode;
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    44
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    45
val clean_string = translate
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    46
  (fn "_" => "\\_"
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    47
    | "#" => "\\#"
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    48
    | "<" => "\\isacharless"
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    49
    | ">" => "\\isachargreater"
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    50
    | "{" => "\\{"
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    51
    | "|" => "\\isacharbar"
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    52
    | "}" => "\\}"
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    53
    | "$" => "\\isachardollar"
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    54
    | "!" => "\\isacharbang"
261
358f325f4db6 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 258
diff changeset
    55
    | "\<dash>" => "-"
256
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    56
    | c => c);
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    57
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    58
fun get_word str =
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    59
let 
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    60
  fun only_letters [] = true
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    61
    | only_letters (x::xs) = 
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    62
        if (Symbol.is_ascii_blank x) then false else only_letters xs
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    63
in
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    64
  if (only_letters (Symbol.explode str)) then (clean_string str)
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    65
  else error ("Underspecified index entry only for single words allowed! Error with " ^ quote str)
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    66
end  
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    67
258
03145998190b slightly modified index generation
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
    68
fun get_qstring NoString = ""
03145998190b slightly modified index generation
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
    69
  | get_qstring (Plain s) = get_word s
256
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    70
  | get_qstring (Code s) = let val w = get_word s in  (w ^ "@{\\tt\\slshape{}" ^ w ^ "}") end
302
Christian Urban <urbanc@in.tum.de>
parents: 261
diff changeset
    71
  | get_qstring (IStruct s) =  "in {\\tt\\slshape{}" ^ (get_word s) ^ "}"
256
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    72
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    73
fun is_empty_qstr (Plain "") = true
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    74
  | is_empty_qstr (Code "") = true
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    75
  | is_empty_qstr _ = false
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    76
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    77
fun get_index_info {main = m, minor = n} = 
258
03145998190b slightly modified index generation
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
    78
 (if n = NoString
256
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    79
  then "\\index{" ^ (get_qstring m) ^ "}"  
258
03145998190b slightly modified index generation
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
    80
  else "\\indexdef{" ^ (get_qstring m) ^ "}{" ^ (get_qstring n) ^ "}")  
256
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    81
    
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    82
fun index_entry entry index_info  =
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    83
 case entry of
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    84
   No_Entry => I
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    85
 | Explicit s => prefix s
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    86
 | Default => prefix (get_index_info index_info)
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    87
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    88
fun output_indexed txt index_info =
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    89
 txt |> output
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    90
     |> index_entry (!index) index_info
255
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
fun boolean "" = true
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
  | boolean "true" = true
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
  | boolean "false" = false
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
  | boolean s = error ("Bad boolean value: " ^ quote s);
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
256
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    97
fun explicit "" = Default
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    98
  | explicit s  = Explicit s
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    99
255
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
val _ = ThyOutput.add_options
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
 [("gray", Library.setmp gray o boolean),
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
  ("linenos", Library.setmp linenos o boolean),
256
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
   103
  ("index", Library.setmp index o explicit)]
255
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
end