src/HOL/Tools/Sledgehammer/sledgehammer_util.ML
author blanchet
Thu Apr 29 01:17:14 2010 +0200 (2010-04-29)
changeset 36555 8ff45c2076da
parent 36496 8b2dc9b4bf4c
child 36960 01594f816e3a
permissions -rw-r--r--
expand combinators in Isar proofs constructed by Sledgehammer;
this requires shuffling around a couple of functions previously defined in Refute
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@36491
     9
  val is_new_spass_version : bool
blanchet@36378
    10
  val pairf : ('a -> 'b) -> ('a -> 'c) -> 'a -> 'b * 'c
blanchet@36142
    11
  val plural_s : int -> string
blanchet@35963
    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@35963
    16
  val parse_bool_option : bool -> string -> string -> bool option
blanchet@35963
    17
  val parse_time_option : string -> string -> Time.time option
blanchet@36486
    18
  val nat_subscript : int -> string
blanchet@36478
    19
  val unyxml : string -> string
blanchet@36478
    20
  val maybe_quote : string -> string
blanchet@36555
    21
  val monomorphic_term : Type.tyenv -> term -> term
blanchet@36555
    22
  val specialize_type : theory -> (string * typ) -> term -> term
blanchet@35963
    23
end;
blanchet@36170
    24
 
blanchet@35963
    25
structure Sledgehammer_Util : SLEDGEHAMMER_UTIL =
blanchet@35963
    26
struct
blanchet@35963
    27
blanchet@36491
    28
val is_new_spass_version =
blanchet@36491
    29
  case getenv "SPASS_VERSION" of
blanchet@36496
    30
    "" => (case getenv "SPASS_HOME" of
blanchet@36496
    31
             "" => false
blanchet@36496
    32
           | s =>
blanchet@36496
    33
             (* Hack: Preliminary versions of the SPASS 3.7 package don't set
blanchet@36496
    34
                "SPASS_VERSION". *)
blanchet@36496
    35
             String.isSubstring "/spass-3.7/" s)
blanchet@36496
    36
  | s => (case s |> space_explode "." |> map Int.fromString of
blanchet@36496
    37
            SOME m :: SOME n :: _ => m > 3 orelse (m = 3 andalso n >= 5)
blanchet@36496
    38
          | _ => false)
blanchet@36491
    39
blanchet@36378
    40
fun pairf f g x = (f x, g x)
blanchet@36378
    41
blanchet@36142
    42
fun plural_s n = if n = 1 then "" else "s"
blanchet@36062
    43
blanchet@35963
    44
fun serial_commas _ [] = ["??"]
blanchet@35963
    45
  | serial_commas _ [s] = [s]
blanchet@35963
    46
  | serial_commas conj [s1, s2] = [s1, conj, s2]
blanchet@35963
    47
  | serial_commas conj [s1, s2, s3] = [s1 ^ ",", s2 ^ ",", conj, s3]
blanchet@35963
    48
  | serial_commas conj (s :: ss) = s ^ "," :: serial_commas conj ss
blanchet@35963
    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@35963
    63
fun parse_bool_option option name s =
blanchet@35963
    64
  (case s of
blanchet@35963
    65
     "smart" => if option then NONE else raise Option
blanchet@35963
    66
   | "false" => SOME false
blanchet@35963
    67
   | "true" => SOME true
blanchet@35963
    68
   | "" => SOME true
blanchet@35963
    69
   | _ => raise Option)
blanchet@35963
    70
  handle Option.Option =>
blanchet@35963
    71
         let val ss = map quote ((option ? cons "smart") ["true", "false"]) in
blanchet@35963
    72
           error ("Parameter " ^ quote name ^ " must be assigned " ^
blanchet@35963
    73
                  space_implode " " (serial_commas "or" ss) ^ ".")
blanchet@35963
    74
         end
blanchet@35963
    75
blanchet@35963
    76
fun parse_time_option _ "none" = NONE
blanchet@35963
    77
  | parse_time_option name s =
blanchet@35963
    78
    let
blanchet@35963
    79
      val msecs =
blanchet@35963
    80
        case space_explode " " s of
blanchet@35963
    81
          [s1, "min"] => 60000 * the (Int.fromString s1)
blanchet@35963
    82
        | [s1, "s"] => 1000 * the (Int.fromString s1)
blanchet@35963
    83
        | [s1, "ms"] => the (Int.fromString s1)
blanchet@35963
    84
        | _ => 0
blanchet@35963
    85
    in
blanchet@35963
    86
      if msecs <= 0 then
blanchet@35963
    87
        error ("Parameter " ^ quote name ^ " must be assigned a positive time \
blanchet@35963
    88
               \value (e.g., \"60 s\", \"200 ms\") or \"none\".")
blanchet@35963
    89
      else
blanchet@35963
    90
        SOME (Time.fromMilliseconds msecs)
blanchet@35963
    91
    end
blanchet@35963
    92
blanchet@36486
    93
val subscript = implode o map (prefix "\<^isub>") o explode
blanchet@36486
    94
val nat_subscript = subscript o string_of_int
blanchet@36486
    95
blanchet@36478
    96
fun plain_string_from_xml_tree t =
blanchet@36478
    97
  Buffer.empty |> XML.add_content t |> Buffer.content
blanchet@36478
    98
val unyxml = plain_string_from_xml_tree o YXML.parse
blanchet@36478
    99
blanchet@36478
   100
val is_long_identifier = forall Syntax.is_identifier o space_explode "."
blanchet@36478
   101
fun maybe_quote y =
blanchet@36478
   102
  let val s = unyxml y in
blanchet@36478
   103
    y |> ((not (is_long_identifier (perhaps (try (unprefix "'")) s)) andalso
blanchet@36478
   104
           not (is_long_identifier (perhaps (try (unprefix "?")) s))) orelse
blanchet@36478
   105
           OuterKeyword.is_keyword s) ? quote
blanchet@36478
   106
  end
blanchet@36478
   107
blanchet@36555
   108
fun monomorphic_term subst t =
blanchet@36555
   109
  map_types (map_type_tvar (fn v =>
blanchet@36555
   110
      case Type.lookup subst v of
blanchet@36555
   111
        SOME typ => typ
blanchet@36555
   112
      | NONE => raise TERM ("monomorphic_term: uninstanitated schematic type \
blanchet@36555
   113
                            \variable", [t]))) t
blanchet@36555
   114
blanchet@36555
   115
fun specialize_type thy (s, T) t =
blanchet@36555
   116
  let
blanchet@36555
   117
    fun subst_for (Const (s', T')) =
blanchet@36555
   118
      if s = s' then
blanchet@36555
   119
        SOME (Sign.typ_match thy (T', T) Vartab.empty)
blanchet@36555
   120
        handle Type.TYPE_MATCH => NONE
blanchet@36555
   121
      else
blanchet@36555
   122
        NONE
blanchet@36555
   123
    | subst_for (t1 $ t2) =
blanchet@36555
   124
      (case subst_for t1 of SOME x => SOME x | NONE => subst_for t2)
blanchet@36555
   125
    | subst_for (Abs (_, _, t')) = subst_for t'
blanchet@36555
   126
    | subst_for _ = NONE
blanchet@36555
   127
  in
blanchet@36555
   128
    case subst_for t of
blanchet@36555
   129
      SOME subst => monomorphic_term subst t
blanchet@36555
   130
    | NONE => raise Type.TYPE_MATCH
blanchet@36555
   131
  end
blanchet@36555
   132
blanchet@36555
   133
blanchet@35963
   134
end;