src/HOL/Tools/Sledgehammer/sledgehammer_util.ML
author blanchet
Wed, 02 Jun 2010 15:18:48 +0200
changeset 37318 9d7cfae95b30
parent 36970 01594f816e3a
child 37389 d0cea0796295
permissions -rw-r--r--
honor "xsymbols"
blanchet@36062
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_util.ML
blanchet@35961
     2
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@35961
     3
blanchet@35961
     4
General-purpose functions used by the Sledgehammer modules.
blanchet@35961
     5
*)
blanchet@35961
     6
blanchet@35961
     7
signature SLEDGEHAMMER_UTIL =
blanchet@35961
     8
sig
blanchet@36487
     9
  val is_new_spass_version : bool
blanchet@36378
    10
  val pairf : ('a -> 'b) -> ('a -> 'c) -> 'a -> 'b * 'c
blanchet@36140
    11
  val plural_s : int -> string
blanchet@35961
    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@35961
    16
  val parse_bool_option : bool -> string -> string -> bool option
blanchet@35961
    17
  val parse_time_option : string -> string -> Time.time option
blanchet@36482
    18
  val nat_subscript : int -> string
blanchet@36474
    19
  val unyxml : string -> string
blanchet@36474
    20
  val maybe_quote : string -> string
blanchet@36553
    21
  val monomorphic_term : Type.tyenv -> term -> term
blanchet@36553
    22
  val specialize_type : theory -> (string * typ) -> term -> term
blanchet@35961
    23
end;
blanchet@36170
    24
 
blanchet@35961
    25
structure Sledgehammer_Util : SLEDGEHAMMER_UTIL =
blanchet@35961
    26
struct
blanchet@35961
    27
blanchet@36487
    28
val is_new_spass_version =
blanchet@36487
    29
  case getenv "SPASS_VERSION" of
blanchet@36492
    30
    "" => (case getenv "SPASS_HOME" of
blanchet@36492
    31
             "" => false
blanchet@36492
    32
           | s =>
blanchet@36492
    33
             (* Hack: Preliminary versions of the SPASS 3.7 package don't set
blanchet@36492
    34
                "SPASS_VERSION". *)
blanchet@36492
    35
             String.isSubstring "/spass-3.7/" s)
blanchet@36492
    36
  | s => (case s |> space_explode "." |> map Int.fromString of
blanchet@36492
    37
            SOME m :: SOME n :: _ => m > 3 orelse (m = 3 andalso n >= 5)
blanchet@36492
    38
          | _ => false)
blanchet@36487
    39
blanchet@36378
    40
fun pairf f g x = (f x, g x)
blanchet@36378
    41
blanchet@36140
    42
fun plural_s n = if n = 1 then "" else "s"
blanchet@36062
    43
blanchet@35961
    44
fun serial_commas _ [] = ["??"]
blanchet@35961
    45
  | serial_commas _ [s] = [s]
blanchet@35961
    46
  | serial_commas conj [s1, s2] = [s1, conj, s2]
blanchet@35961
    47
  | serial_commas conj [s1, s2, s3] = [s1 ^ ",", s2 ^ ",", conj, s3]
blanchet@35961
    48
  | serial_commas conj (s :: ss) = s ^ "," :: serial_commas conj ss
blanchet@35961
    49
blanchet@36169
    50
fun replace_all bef aft =
blanchet@36169
    51
  let
blanchet@36169
    52
    fun aux seen "" = String.implode (rev seen)
blanchet@36169
    53
      | aux seen s =
blanchet@36183
    54
        if String.isPrefix bef s then
blanchet@36169
    55
          aux seen "" ^ aft ^ aux [] (unprefix bef s)
blanchet@36169
    56
        else
blanchet@36169
    57
          aux (String.sub (s, 0) :: seen) (String.extract (s, 1, NONE))
blanchet@36169
    58
  in aux [] end
blanchet@36169
    59
fun remove_all bef = replace_all bef ""
blanchet@36169
    60
blanchet@36170
    61
val timestamp = Date.fmt "%Y-%m-%d %H:%M:%S" o Date.fromTimeLocal o Time.now
blanchet@36170
    62
blanchet@35961
    63
fun parse_bool_option option name s =
blanchet@35961
    64
  (case s of
blanchet@35961
    65
     "smart" => if option then NONE else raise Option
blanchet@35961
    66
   | "false" => SOME false
blanchet@35961
    67
   | "true" => SOME true
blanchet@35961
    68
   | "" => SOME true
blanchet@35961
    69
   | _ => raise Option)
blanchet@35961
    70
  handle Option.Option =>
blanchet@35961
    71
         let val ss = map quote ((option ? cons "smart") ["true", "false"]) in
blanchet@35961
    72
           error ("Parameter " ^ quote name ^ " must be assigned " ^
blanchet@35961
    73
                  space_implode " " (serial_commas "or" ss) ^ ".")
blanchet@35961
    74
         end
blanchet@35961
    75
blanchet@35961
    76
fun parse_time_option _ "none" = NONE
blanchet@35961
    77
  | parse_time_option name s =
blanchet@35961
    78
    let
blanchet@35961
    79
      val msecs =
blanchet@35961
    80
        case space_explode " " s of
blanchet@35961
    81
          [s1, "min"] => 60000 * the (Int.fromString s1)
blanchet@35961
    82
        | [s1, "s"] => 1000 * the (Int.fromString s1)
blanchet@35961
    83
        | [s1, "ms"] => the (Int.fromString s1)
blanchet@35961
    84
        | _ => 0
blanchet@35961
    85
    in
blanchet@35961
    86
      if msecs <= 0 then
blanchet@35961
    87
        error ("Parameter " ^ quote name ^ " must be assigned a positive time \
blanchet@35961
    88
               \value (e.g., \"60 s\", \"200 ms\") or \"none\".")
blanchet@35961
    89
      else
blanchet@35961
    90
        SOME (Time.fromMilliseconds msecs)
blanchet@35961
    91
    end
blanchet@35961
    92
blanchet@36482
    93
val subscript = implode o map (prefix "\<^isub>") o explode
blanchet@37318
    94
fun nat_subscript n =
blanchet@37318
    95
  n |> string_of_int |> print_mode_active Symbol.xsymbolsN ? subscript
blanchet@36482
    96
blanchet@36474
    97
fun plain_string_from_xml_tree t =
blanchet@36474
    98
  Buffer.empty |> XML.add_content t |> Buffer.content
blanchet@36474
    99
val unyxml = plain_string_from_xml_tree o YXML.parse
blanchet@36474
   100
blanchet@36474
   101
val is_long_identifier = forall Syntax.is_identifier o space_explode "."
blanchet@36474
   102
fun maybe_quote y =
blanchet@36474
   103
  let val s = unyxml y in
blanchet@36474
   104
    y |> ((not (is_long_identifier (perhaps (try (unprefix "'")) s)) andalso
blanchet@36474
   105
           not (is_long_identifier (perhaps (try (unprefix "?")) s))) orelse
wenzelm@36970
   106
           Keyword.is_keyword s) ? quote
blanchet@36474
   107
  end
blanchet@36474
   108
blanchet@36553
   109
fun monomorphic_term subst t =
blanchet@36553
   110
  map_types (map_type_tvar (fn v =>
blanchet@36553
   111
      case Type.lookup subst v of
blanchet@36553
   112
        SOME typ => typ
blanchet@36553
   113
      | NONE => raise TERM ("monomorphic_term: uninstanitated schematic type \
blanchet@36553
   114
                            \variable", [t]))) t
blanchet@36553
   115
blanchet@36553
   116
fun specialize_type thy (s, T) t =
blanchet@36553
   117
  let
blanchet@36553
   118
    fun subst_for (Const (s', T')) =
blanchet@36553
   119
      if s = s' then
blanchet@36553
   120
        SOME (Sign.typ_match thy (T', T) Vartab.empty)
blanchet@36553
   121
        handle Type.TYPE_MATCH => NONE
blanchet@36553
   122
      else
blanchet@36553
   123
        NONE
blanchet@36553
   124
    | subst_for (t1 $ t2) =
blanchet@36553
   125
      (case subst_for t1 of SOME x => SOME x | NONE => subst_for t2)
blanchet@36553
   126
    | subst_for (Abs (_, _, t')) = subst_for t'
blanchet@36553
   127
    | subst_for _ = NONE
blanchet@36553
   128
  in
blanchet@36553
   129
    case subst_for t of
blanchet@36553
   130
      SOME subst => monomorphic_term subst t
blanchet@36553
   131
    | NONE => raise Type.TYPE_MATCH
blanchet@36553
   132
  end
blanchet@36553
   133
blanchet@36553
   134
blanchet@35961
   135
end;