src/HOL/Tools/Sledgehammer/sledgehammer_util.ML
author blanchet
Mon Mar 29 15:50:18 2010 +0200 (2010-03-29)
changeset 36062 194cb6e3c13f
parent 35963 943e2582dc87
child 36142 f5e15e9aae10
permissions -rw-r--r--
get rid of Polyhash, since it's no longer used
     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 serial_commas : string -> string list -> string list
    10   val parse_bool_option : bool -> string -> string -> bool option
    11   val parse_time_option : string -> string -> Time.time option
    12   val hashw : word * word -> word
    13   val hashw_char : Char.char * word -> word
    14   val hashw_string : string * word -> word
    15 end;
    16 
    17 structure Sledgehammer_Util : SLEDGEHAMMER_UTIL =
    18 struct
    19 
    20 (* This hash function is recommended in Compilers: Principles, Techniques, and
    21    Tools, by Aho, Sethi and Ullman. The hashpjw function, which they
    22    particularly recommend, triggers a bug in versions of Poly/ML up to 4.2.0. *)
    23 fun hashw (u, w) = Word.+ (u, Word.* (0w65599, w))
    24 fun hashw_char (c, w) = hashw (Word.fromInt (Char.ord c), w)
    25 fun hashw_string (s:string, w) = CharVector.foldl hashw_char w s
    26 
    27 fun serial_commas _ [] = ["??"]
    28   | serial_commas _ [s] = [s]
    29   | serial_commas conj [s1, s2] = [s1, conj, s2]
    30   | serial_commas conj [s1, s2, s3] = [s1 ^ ",", s2 ^ ",", conj, s3]
    31   | serial_commas conj (s :: ss) = s ^ "," :: serial_commas conj ss
    32 
    33 fun parse_bool_option option name s =
    34   (case s of
    35      "smart" => if option then NONE else raise Option
    36    | "false" => SOME false
    37    | "true" => SOME true
    38    | "" => SOME true
    39    | _ => raise Option)
    40   handle Option.Option =>
    41          let val ss = map quote ((option ? cons "smart") ["true", "false"]) in
    42            error ("Parameter " ^ quote name ^ " must be assigned " ^
    43                   space_implode " " (serial_commas "or" ss) ^ ".")
    44          end
    45 
    46 fun parse_time_option _ "none" = NONE
    47   | parse_time_option name s =
    48     let
    49       val msecs =
    50         case space_explode " " s of
    51           [s1, "min"] => 60000 * the (Int.fromString s1)
    52         | [s1, "s"] => 1000 * the (Int.fromString s1)
    53         | [s1, "ms"] => the (Int.fromString s1)
    54         | _ => 0
    55     in
    56       if msecs <= 0 then
    57         error ("Parameter " ^ quote name ^ " must be assigned a positive time \
    58                \value (e.g., \"60 s\", \"200 ms\") or \"none\".")
    59       else
    60         SOME (Time.fromMilliseconds msecs)
    61     end
    62 
    63 end;