src/HOL/Tools/Sledgehammer/sledgehammer_util.ML
author blanchet
Wed Apr 28 15:34:55 2010 +0200 (2010-04-28)
changeset 36491 6769f8eacaac
parent 36486 c2d7e2dff59e
child 36496 8b2dc9b4bf4c
permissions -rw-r--r--
unskolemize formulas in proof reconstruction + detect newer SPASS versions to avoid truncating identifiers if not necessary (truncating confuses proof reconstruction)
     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 is_new_spass_version : bool
    10   val pairf : ('a -> 'b) -> ('a -> 'c) -> 'a -> 'b * 'c
    11   val plural_s : int -> string
    12   val serial_commas : string -> string list -> string list
    13   val replace_all : string -> string -> string -> string
    14   val remove_all : string -> string -> string
    15   val timestamp : unit -> string
    16   val parse_bool_option : bool -> string -> string -> bool option
    17   val parse_time_option : string -> string -> Time.time option
    18   val nat_subscript : int -> string
    19   val unyxml : string -> string
    20   val maybe_quote : string -> string
    21 end;
    22  
    23 structure Sledgehammer_Util : SLEDGEHAMMER_UTIL =
    24 struct
    25 
    26 val is_new_spass_version =
    27   case getenv "SPASS_VERSION" of
    28     "" => case getenv "SPASS_HOME" of
    29             "" => false
    30           | s =>
    31             (* Hack: Preliminary versions of the SPASS 3.7 package don't set
    32                "SPASS_VERSION". *)
    33             String.isSubstring "/spass-3.7/" s
    34   | s => case s |> space_explode "." |> map Int.fromString of
    35            SOME m :: SOME n :: _ => m > 3 orelse (m = 3 andalso n >= 5)
    36          | _ => false
    37 
    38 fun pairf f g x = (f x, g x)
    39 
    40 fun plural_s n = if n = 1 then "" else "s"
    41 
    42 fun serial_commas _ [] = ["??"]
    43   | serial_commas _ [s] = [s]
    44   | serial_commas conj [s1, s2] = [s1, conj, s2]
    45   | serial_commas conj [s1, s2, s3] = [s1 ^ ",", s2 ^ ",", conj, s3]
    46   | serial_commas conj (s :: ss) = s ^ "," :: serial_commas conj ss
    47 
    48 fun replace_all bef aft =
    49   let
    50     fun aux seen "" = String.implode (rev seen)
    51       | aux seen s =
    52         if String.isPrefix bef s then
    53           aux seen "" ^ aft ^ aux [] (unprefix bef s)
    54         else
    55           aux (String.sub (s, 0) :: seen) (String.extract (s, 1, NONE))
    56   in aux [] end
    57 fun remove_all bef = replace_all bef ""
    58 
    59 val timestamp = Date.fmt "%Y-%m-%d %H:%M:%S" o Date.fromTimeLocal o Time.now
    60 
    61 fun parse_bool_option option name s =
    62   (case s of
    63      "smart" => if option then NONE else raise Option
    64    | "false" => SOME false
    65    | "true" => SOME true
    66    | "" => SOME true
    67    | _ => raise Option)
    68   handle Option.Option =>
    69          let val ss = map quote ((option ? cons "smart") ["true", "false"]) in
    70            error ("Parameter " ^ quote name ^ " must be assigned " ^
    71                   space_implode " " (serial_commas "or" ss) ^ ".")
    72          end
    73 
    74 fun parse_time_option _ "none" = NONE
    75   | parse_time_option name s =
    76     let
    77       val msecs =
    78         case space_explode " " s of
    79           [s1, "min"] => 60000 * the (Int.fromString s1)
    80         | [s1, "s"] => 1000 * the (Int.fromString s1)
    81         | [s1, "ms"] => the (Int.fromString s1)
    82         | _ => 0
    83     in
    84       if msecs <= 0 then
    85         error ("Parameter " ^ quote name ^ " must be assigned a positive time \
    86                \value (e.g., \"60 s\", \"200 ms\") or \"none\".")
    87       else
    88         SOME (Time.fromMilliseconds msecs)
    89     end
    90 
    91 val subscript = implode o map (prefix "\<^isub>") o explode
    92 val nat_subscript = subscript o string_of_int
    93 
    94 fun plain_string_from_xml_tree t =
    95   Buffer.empty |> XML.add_content t |> Buffer.content
    96 val unyxml = plain_string_from_xml_tree o YXML.parse
    97 
    98 val is_long_identifier = forall Syntax.is_identifier o space_explode "."
    99 fun maybe_quote y =
   100   let val s = unyxml y in
   101     y |> ((not (is_long_identifier (perhaps (try (unprefix "'")) s)) andalso
   102            not (is_long_identifier (perhaps (try (unprefix "?")) s))) orelse
   103            OuterKeyword.is_keyword s) ? quote
   104   end
   105 
   106 end;