src/HOL/Tools/Sledgehammer/sledgehammer_util.ML
author blanchet
Fri Apr 16 16:13:49 2010 +0200 (2010-04-16)
changeset 36183 f723348b2231
parent 36170 0cdb76723c88
child 36378 f32c567dbcaa
permissions -rw-r--r--
Sledgehammer: the empty set of fact () should mean nothing, not unchanged
blanchet@36062
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_util.ML
blanchet@35963
     2
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@35963
     3
blanchet@35963
     4
General-purpose functions used by the Sledgehammer modules.
blanchet@35963
     5
*)
blanchet@35963
     6
blanchet@35963
     7
signature SLEDGEHAMMER_UTIL =
blanchet@35963
     8
sig
blanchet@36142
     9
  val plural_s : int -> string
blanchet@35963
    10
  val serial_commas : string -> string list -> string list
blanchet@36169
    11
  val replace_all : string -> string -> string -> string
blanchet@36169
    12
  val remove_all : string -> string -> string
blanchet@36170
    13
  val timestamp : unit -> string
blanchet@35963
    14
  val parse_bool_option : bool -> string -> string -> bool option
blanchet@35963
    15
  val parse_time_option : string -> string -> Time.time option
blanchet@36062
    16
  val hashw : word * word -> word
blanchet@36062
    17
  val hashw_char : Char.char * word -> word
blanchet@36062
    18
  val hashw_string : string * word -> word
blanchet@35963
    19
end;
blanchet@36170
    20
 
blanchet@35963
    21
structure Sledgehammer_Util : SLEDGEHAMMER_UTIL =
blanchet@35963
    22
struct
blanchet@35963
    23
blanchet@36142
    24
fun plural_s n = if n = 1 then "" else "s"
blanchet@36062
    25
blanchet@35963
    26
fun serial_commas _ [] = ["??"]
blanchet@35963
    27
  | serial_commas _ [s] = [s]
blanchet@35963
    28
  | serial_commas conj [s1, s2] = [s1, conj, s2]
blanchet@35963
    29
  | serial_commas conj [s1, s2, s3] = [s1 ^ ",", s2 ^ ",", conj, s3]
blanchet@35963
    30
  | serial_commas conj (s :: ss) = s ^ "," :: serial_commas conj ss
blanchet@35963
    31
blanchet@36169
    32
fun replace_all bef aft =
blanchet@36169
    33
  let
blanchet@36169
    34
    fun aux seen "" = String.implode (rev seen)
blanchet@36169
    35
      | aux seen s =
blanchet@36183
    36
        if String.isPrefix bef s then
blanchet@36169
    37
          aux seen "" ^ aft ^ aux [] (unprefix bef s)
blanchet@36169
    38
        else
blanchet@36169
    39
          aux (String.sub (s, 0) :: seen) (String.extract (s, 1, NONE))
blanchet@36169
    40
  in aux [] end
blanchet@36169
    41
blanchet@36169
    42
fun remove_all bef = replace_all bef ""
blanchet@36169
    43
blanchet@36170
    44
val timestamp = Date.fmt "%Y-%m-%d %H:%M:%S" o Date.fromTimeLocal o Time.now
blanchet@36170
    45
blanchet@35963
    46
fun parse_bool_option option name s =
blanchet@35963
    47
  (case s of
blanchet@35963
    48
     "smart" => if option then NONE else raise Option
blanchet@35963
    49
   | "false" => SOME false
blanchet@35963
    50
   | "true" => SOME true
blanchet@35963
    51
   | "" => SOME true
blanchet@35963
    52
   | _ => raise Option)
blanchet@35963
    53
  handle Option.Option =>
blanchet@35963
    54
         let val ss = map quote ((option ? cons "smart") ["true", "false"]) in
blanchet@35963
    55
           error ("Parameter " ^ quote name ^ " must be assigned " ^
blanchet@35963
    56
                  space_implode " " (serial_commas "or" ss) ^ ".")
blanchet@35963
    57
         end
blanchet@35963
    58
blanchet@35963
    59
fun parse_time_option _ "none" = NONE
blanchet@35963
    60
  | parse_time_option name s =
blanchet@35963
    61
    let
blanchet@35963
    62
      val msecs =
blanchet@35963
    63
        case space_explode " " s of
blanchet@35963
    64
          [s1, "min"] => 60000 * the (Int.fromString s1)
blanchet@35963
    65
        | [s1, "s"] => 1000 * the (Int.fromString s1)
blanchet@35963
    66
        | [s1, "ms"] => the (Int.fromString s1)
blanchet@35963
    67
        | _ => 0
blanchet@35963
    68
    in
blanchet@35963
    69
      if msecs <= 0 then
blanchet@35963
    70
        error ("Parameter " ^ quote name ^ " must be assigned a positive time \
blanchet@35963
    71
               \value (e.g., \"60 s\", \"200 ms\") or \"none\".")
blanchet@35963
    72
      else
blanchet@35963
    73
        SOME (Time.fromMilliseconds msecs)
blanchet@35963
    74
    end
blanchet@35963
    75
blanchet@36142
    76
(* This hash function is recommended in Compilers: Principles, Techniques, and
blanchet@36142
    77
   Tools, by Aho, Sethi and Ullman. The hashpjw function, which they
blanchet@36142
    78
   particularly recommend, triggers a bug in versions of Poly/ML up to 4.2.0. *)
blanchet@36142
    79
fun hashw (u, w) = Word.+ (u, Word.* (0w65599, w))
blanchet@36142
    80
fun hashw_char (c, w) = hashw (Word.fromInt (Char.ord c), w)
blanchet@36142
    81
fun hashw_string (s:string, w) = CharVector.foldl hashw_char w s
blanchet@36142
    82
blanchet@35963
    83
end;