src/HOL/Tools/Sledgehammer/sledgehammer_util.ML
author blanchet
Mon Jun 14 16:43:44 2010 +0200 (2010-06-14 ago)
changeset 37414 d0cea0796295
parent 37321 9d7cfae95b30
child 37498 b426cbdb5a23
permissions -rw-r--r--
expect SPASS 3.7, and give a friendly warning if an older version is used
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@36555
    20
  val monomorphic_term : Type.tyenv -> term -> term
blanchet@36555
    21
  val specialize_type : theory -> (string * typ) -> term -> term
blanchet@35963
    22
end;
blanchet@36170
    23
 
blanchet@35963
    24
structure Sledgehammer_Util : SLEDGEHAMMER_UTIL =
blanchet@35963
    25
struct
blanchet@35963
    26
blanchet@36378
    27
fun pairf f g x = (f x, g x)
blanchet@36378
    28
blanchet@36142
    29
fun plural_s n = if n = 1 then "" else "s"
blanchet@36062
    30
blanchet@35963
    31
fun serial_commas _ [] = ["??"]
blanchet@35963
    32
  | serial_commas _ [s] = [s]
blanchet@35963
    33
  | serial_commas conj [s1, s2] = [s1, conj, s2]
blanchet@35963
    34
  | serial_commas conj [s1, s2, s3] = [s1 ^ ",", s2 ^ ",", conj, s3]
blanchet@35963
    35
  | serial_commas conj (s :: ss) = s ^ "," :: serial_commas conj ss
blanchet@35963
    36
blanchet@36169
    37
fun replace_all bef aft =
blanchet@36169
    38
  let
blanchet@36169
    39
    fun aux seen "" = String.implode (rev seen)
blanchet@36169
    40
      | aux seen s =
blanchet@36183
    41
        if String.isPrefix bef s then
blanchet@36169
    42
          aux seen "" ^ aft ^ aux [] (unprefix bef s)
blanchet@36169
    43
        else
blanchet@36169
    44
          aux (String.sub (s, 0) :: seen) (String.extract (s, 1, NONE))
blanchet@36169
    45
  in aux [] end
blanchet@36169
    46
fun remove_all bef = replace_all bef ""
blanchet@36169
    47
blanchet@36170
    48
val timestamp = Date.fmt "%Y-%m-%d %H:%M:%S" o Date.fromTimeLocal o Time.now
blanchet@36170
    49
blanchet@35963
    50
fun parse_bool_option option name s =
blanchet@35963
    51
  (case s of
blanchet@35963
    52
     "smart" => if option then NONE else raise Option
blanchet@35963
    53
   | "false" => SOME false
blanchet@35963
    54
   | "true" => SOME true
blanchet@35963
    55
   | "" => SOME true
blanchet@35963
    56
   | _ => raise Option)
blanchet@35963
    57
  handle Option.Option =>
blanchet@35963
    58
         let val ss = map quote ((option ? cons "smart") ["true", "false"]) in
blanchet@35963
    59
           error ("Parameter " ^ quote name ^ " must be assigned " ^
blanchet@35963
    60
                  space_implode " " (serial_commas "or" ss) ^ ".")
blanchet@35963
    61
         end
blanchet@35963
    62
blanchet@35963
    63
fun parse_time_option _ "none" = NONE
blanchet@35963
    64
  | parse_time_option name s =
blanchet@35963
    65
    let
blanchet@35963
    66
      val msecs =
blanchet@35963
    67
        case space_explode " " s of
blanchet@35963
    68
          [s1, "min"] => 60000 * the (Int.fromString s1)
blanchet@35963
    69
        | [s1, "s"] => 1000 * the (Int.fromString s1)
blanchet@35963
    70
        | [s1, "ms"] => the (Int.fromString s1)
blanchet@35963
    71
        | _ => 0
blanchet@35963
    72
    in
blanchet@35963
    73
      if msecs <= 0 then
blanchet@35963
    74
        error ("Parameter " ^ quote name ^ " must be assigned a positive time \
blanchet@35963
    75
               \value (e.g., \"60 s\", \"200 ms\") or \"none\".")
blanchet@35963
    76
      else
blanchet@35963
    77
        SOME (Time.fromMilliseconds msecs)
blanchet@35963
    78
    end
blanchet@35963
    79
blanchet@36486
    80
val subscript = implode o map (prefix "\<^isub>") o explode
blanchet@37321
    81
fun nat_subscript n =
blanchet@37321
    82
  n |> string_of_int |> print_mode_active Symbol.xsymbolsN ? subscript
blanchet@36486
    83
blanchet@36478
    84
fun plain_string_from_xml_tree t =
blanchet@36478
    85
  Buffer.empty |> XML.add_content t |> Buffer.content
blanchet@36478
    86
val unyxml = plain_string_from_xml_tree o YXML.parse
blanchet@36478
    87
blanchet@36478
    88
val is_long_identifier = forall Syntax.is_identifier o space_explode "."
blanchet@36478
    89
fun maybe_quote y =
blanchet@36478
    90
  let val s = unyxml y in
blanchet@36478
    91
    y |> ((not (is_long_identifier (perhaps (try (unprefix "'")) s)) andalso
blanchet@36478
    92
           not (is_long_identifier (perhaps (try (unprefix "?")) s))) orelse
wenzelm@36960
    93
           Keyword.is_keyword s) ? quote
blanchet@36478
    94
  end
blanchet@36478
    95
blanchet@36555
    96
fun monomorphic_term subst t =
blanchet@36555
    97
  map_types (map_type_tvar (fn v =>
blanchet@36555
    98
      case Type.lookup subst v of
blanchet@36555
    99
        SOME typ => typ
blanchet@36555
   100
      | NONE => raise TERM ("monomorphic_term: uninstanitated schematic type \
blanchet@36555
   101
                            \variable", [t]))) t
blanchet@36555
   102
blanchet@36555
   103
fun specialize_type thy (s, T) t =
blanchet@36555
   104
  let
blanchet@36555
   105
    fun subst_for (Const (s', T')) =
blanchet@36555
   106
      if s = s' then
blanchet@36555
   107
        SOME (Sign.typ_match thy (T', T) Vartab.empty)
blanchet@36555
   108
        handle Type.TYPE_MATCH => NONE
blanchet@36555
   109
      else
blanchet@36555
   110
        NONE
blanchet@36555
   111
    | subst_for (t1 $ t2) =
blanchet@36555
   112
      (case subst_for t1 of SOME x => SOME x | NONE => subst_for t2)
blanchet@36555
   113
    | subst_for (Abs (_, _, t')) = subst_for t'
blanchet@36555
   114
    | subst_for _ = NONE
blanchet@36555
   115
  in
blanchet@36555
   116
    case subst_for t of
blanchet@36555
   117
      SOME subst => monomorphic_term subst t
blanchet@36555
   118
    | NONE => raise Type.TYPE_MATCH
blanchet@36555
   119
  end
blanchet@36555
   120
blanchet@36555
   121
blanchet@35963
   122
end;