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