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