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