1 (* Title: HOL/Tools/Sledgehammer/sledgehammer_util.ML
2 Author: Jasmin Blanchette, TU Muenchen
4 General-purpose functions used by the Sledgehammer modules.
7 signature SLEDGEHAMMER_UTIL =
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
22 structure Sledgehammer_Util : SLEDGEHAMMER_UTIL =
25 fun pairf f g x = (f x, g x)
27 fun plural_s n = if n = 1 then "" else "s"
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
35 fun replace_all bef aft =
37 fun aux seen "" = String.implode (rev seen)
39 if String.isPrefix bef s then
40 aux seen "" ^ aft ^ aux [] (unprefix bef s)
42 aux (String.sub (s, 0) :: seen) (String.extract (s, 1, NONE))
44 fun remove_all bef = replace_all bef ""
46 val timestamp = Date.fmt "%Y-%m-%d %H:%M:%S" o Date.fromTimeLocal o Time.now
48 fun parse_bool_option option name s =
50 "smart" => if option then NONE else raise Option
51 | "false" => SOME false
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) ^ ".")
61 fun parse_time_option _ "none" = NONE
62 | parse_time_option name s =
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)
72 error ("Parameter " ^ quote name ^ " must be assigned a positive time \
73 \value (e.g., \"60 s\", \"200 ms\") or \"none\".")
75 SOME (Time.fromMilliseconds msecs)
78 val subscript = implode o map (prefix "\<^isub>") o explode
79 val nat_subscript = subscript o string_of_int
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
85 val is_long_identifier = forall Syntax.is_identifier o space_explode "."
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