src/HOL/Tools/Sledgehammer/sledgehammer_util.ML
author blanchet
Wed, 28 Apr 2010 12:46:50 +0200
changeset 36482 c2d7e2dff59e
parent 36474 1aba777a367f
child 36487 6769f8eacaac
permissions -rw-r--r--
support Vampire definitions of constants as "let" constructs in Isar proofs
     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   val nat_subscript : int -> string
    18   val unyxml : string -> string
    19   val maybe_quote : string -> string
    20 end;
    21  
    22 structure Sledgehammer_Util : SLEDGEHAMMER_UTIL =
    23 struct
    24 
    25 fun pairf f g x = (f x, g x)
    26 
    27 fun plural_s n = if n = 1 then "" else "s"
    28 
    29 fun serial_commas _ [] = ["??"]
    30   | serial_commas _ [s] = [s]
    31   | serial_commas conj [s1, s2] = [s1, conj, s2]
    32   | serial_commas conj [s1, s2, s3] = [s1 ^ ",", s2 ^ ",", conj, s3]
    33   | serial_commas conj (s :: ss) = s ^ "," :: serial_commas conj ss
    34 
    35 fun replace_all bef aft =
    36   let
    37     fun aux seen "" = String.implode (rev seen)
    38       | aux seen s =
    39         if String.isPrefix bef s then
    40           aux seen "" ^ aft ^ aux [] (unprefix bef s)
    41         else
    42           aux (String.sub (s, 0) :: seen) (String.extract (s, 1, NONE))
    43   in aux [] end
    44 fun remove_all bef = replace_all bef ""
    45 
    46 val timestamp = Date.fmt "%Y-%m-%d %H:%M:%S" o Date.fromTimeLocal o Time.now
    47 
    48 fun parse_bool_option option name s =
    49   (case s of
    50      "smart" => if option then NONE else raise Option
    51    | "false" => SOME false
    52    | "true" => SOME true
    53    | "" => SOME true
    54    | _ => raise Option)
    55   handle Option.Option =>
    56          let val ss = map quote ((option ? cons "smart") ["true", "false"]) in
    57            error ("Parameter " ^ quote name ^ " must be assigned " ^
    58                   space_implode " " (serial_commas "or" ss) ^ ".")
    59          end
    60 
    61 fun parse_time_option _ "none" = NONE
    62   | parse_time_option name s =
    63     let
    64       val msecs =
    65         case space_explode " " s of
    66           [s1, "min"] => 60000 * the (Int.fromString s1)
    67         | [s1, "s"] => 1000 * the (Int.fromString s1)
    68         | [s1, "ms"] => the (Int.fromString s1)
    69         | _ => 0
    70     in
    71       if msecs <= 0 then
    72         error ("Parameter " ^ quote name ^ " must be assigned a positive time \
    73                \value (e.g., \"60 s\", \"200 ms\") or \"none\".")
    74       else
    75         SOME (Time.fromMilliseconds msecs)
    76     end
    77 
    78 val subscript = implode o map (prefix "\<^isub>") o explode
    79 val nat_subscript = subscript o string_of_int
    80 
    81 fun plain_string_from_xml_tree t =
    82   Buffer.empty |> XML.add_content t |> Buffer.content
    83 val unyxml = plain_string_from_xml_tree o YXML.parse
    84 
    85 val is_long_identifier = forall Syntax.is_identifier o space_explode "."
    86 fun maybe_quote y =
    87   let val s = unyxml y in
    88     y |> ((not (is_long_identifier (perhaps (try (unprefix "'")) s)) andalso
    89            not (is_long_identifier (perhaps (try (unprefix "?")) s))) orelse
    90            OuterKeyword.is_keyword s) ? quote
    91   end
    92 
    93 end;