src/HOL/Tools/Sledgehammer/sledgehammer_util.ML
author blanchet
Mon Apr 26 21:20:43 2010 +0200 (2010-04-26)
changeset 36402 1b20805974c7
parent 36378 f32c567dbcaa
child 36478 1aba777a367f
permissions -rw-r--r--
introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
the code is still somewhat experimental but any exceptions it throws are catched, and Sledgehammer will still yield a one-line metis proof in case of proof reconstruction failure
     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 pairf : ('a -> 'b) -> ('a -> 'c) -> 'a -> 'b * 'c
    10   val plural_s : int -> string
    11   val serial_commas : string -> string list -> string list
    12   val replace_all : string -> string -> string -> string
    13   val remove_all : string -> string -> string
    14   val timestamp : unit -> string
    15   val parse_bool_option : bool -> string -> string -> bool option
    16   val parse_time_option : string -> string -> Time.time option
    17 end;
    18  
    19 structure Sledgehammer_Util : SLEDGEHAMMER_UTIL =
    20 struct
    21 
    22 fun pairf f g x = (f x, g x)
    23 
    24 fun plural_s n = if n = 1 then "" else "s"
    25 
    26 fun serial_commas _ [] = ["??"]
    27   | serial_commas _ [s] = [s]
    28   | serial_commas conj [s1, s2] = [s1, conj, s2]
    29   | serial_commas conj [s1, s2, s3] = [s1 ^ ",", s2 ^ ",", conj, s3]
    30   | serial_commas conj (s :: ss) = s ^ "," :: serial_commas conj ss
    31 
    32 fun replace_all bef aft =
    33   let
    34     fun aux seen "" = String.implode (rev seen)
    35       | aux seen s =
    36         if String.isPrefix bef s then
    37           aux seen "" ^ aft ^ aux [] (unprefix bef s)
    38         else
    39           aux (String.sub (s, 0) :: seen) (String.extract (s, 1, NONE))
    40   in aux [] end
    41 fun remove_all bef = replace_all bef ""
    42 
    43 val timestamp = Date.fmt "%Y-%m-%d %H:%M:%S" o Date.fromTimeLocal o Time.now
    44 
    45 fun parse_bool_option option name s =
    46   (case s of
    47      "smart" => if option then NONE else raise Option
    48    | "false" => SOME false
    49    | "true" => SOME true
    50    | "" => SOME true
    51    | _ => raise Option)
    52   handle Option.Option =>
    53          let val ss = map quote ((option ? cons "smart") ["true", "false"]) in
    54            error ("Parameter " ^ quote name ^ " must be assigned " ^
    55                   space_implode " " (serial_commas "or" ss) ^ ".")
    56          end
    57 
    58 fun parse_time_option _ "none" = NONE
    59   | parse_time_option name s =
    60     let
    61       val msecs =
    62         case space_explode " " s of
    63           [s1, "min"] => 60000 * the (Int.fromString s1)
    64         | [s1, "s"] => 1000 * the (Int.fromString s1)
    65         | [s1, "ms"] => the (Int.fromString s1)
    66         | _ => 0
    67     in
    68       if msecs <= 0 then
    69         error ("Parameter " ^ quote name ^ " must be assigned a positive time \
    70                \value (e.g., \"60 s\", \"200 ms\") or \"none\".")
    71       else
    72         SOME (Time.fromMilliseconds msecs)
    73     end
    74 
    75 end;