src/HOL/Tools/Sledgehammer/sledgehammer_util.ML
author blanchet
Wed Apr 14 18:23:51 2010 +0200 (2010-04-14)
changeset 36142 f5e15e9aae10
parent 36062 194cb6e3c13f
child 36169 27b1cc58715e
permissions -rw-r--r--
make Sledgehammer "minimize" output less confusing + round up (not down) time limits to nearest second
     1 (*  Title:      HOL/Tools/Sledgehammer/sledgehammer_util.ML
     2     Author:     Jasmin Blanchette, TU Muenchen
     3 
     4 General-purpose functions used by the Sledgehammer modules.
     5 *)
     6 
     7 signature SLEDGEHAMMER_UTIL =
     8 sig
     9   val plural_s : int -> string
    10   val serial_commas : string -> string list -> string list
    11   val parse_bool_option : bool -> string -> string -> bool option
    12   val parse_time_option : string -> string -> Time.time option
    13   val hashw : word * word -> word
    14   val hashw_char : Char.char * word -> word
    15   val hashw_string : string * word -> word
    16 end;
    17 
    18 structure Sledgehammer_Util : SLEDGEHAMMER_UTIL =
    19 struct
    20 
    21 fun plural_s n = if n = 1 then "" else "s"
    22 
    23 fun serial_commas _ [] = ["??"]
    24   | serial_commas _ [s] = [s]
    25   | serial_commas conj [s1, s2] = [s1, conj, s2]
    26   | serial_commas conj [s1, s2, s3] = [s1 ^ ",", s2 ^ ",", conj, s3]
    27   | serial_commas conj (s :: ss) = s ^ "," :: serial_commas conj ss
    28 
    29 fun parse_bool_option option name s =
    30   (case s of
    31      "smart" => if option then NONE else raise Option
    32    | "false" => SOME false
    33    | "true" => SOME true
    34    | "" => SOME true
    35    | _ => raise Option)
    36   handle Option.Option =>
    37          let val ss = map quote ((option ? cons "smart") ["true", "false"]) in
    38            error ("Parameter " ^ quote name ^ " must be assigned " ^
    39                   space_implode " " (serial_commas "or" ss) ^ ".")
    40          end
    41 
    42 fun parse_time_option _ "none" = NONE
    43   | parse_time_option name s =
    44     let
    45       val msecs =
    46         case space_explode " " s of
    47           [s1, "min"] => 60000 * the (Int.fromString s1)
    48         | [s1, "s"] => 1000 * the (Int.fromString s1)
    49         | [s1, "ms"] => the (Int.fromString s1)
    50         | _ => 0
    51     in
    52       if msecs <= 0 then
    53         error ("Parameter " ^ quote name ^ " must be assigned a positive time \
    54                \value (e.g., \"60 s\", \"200 ms\") or \"none\".")
    55       else
    56         SOME (Time.fromMilliseconds msecs)
    57     end
    58 
    59 (* This hash function is recommended in Compilers: Principles, Techniques, and
    60    Tools, by Aho, Sethi and Ullman. The hashpjw function, which they
    61    particularly recommend, triggers a bug in versions of Poly/ML up to 4.2.0. *)
    62 fun hashw (u, w) = Word.+ (u, Word.* (0w65599, w))
    63 fun hashw_char (c, w) = hashw (Word.fromInt (Char.ord c), w)
    64 fun hashw_string (s:string, w) = CharVector.foldl hashw_char w s
    65 
    66 end;