src/HOL/Tools/Sledgehammer/sledgehammer_util.ML
author blanchet
Tue Jun 22 14:28:22 2010 +0200 (2010-06-22)
changeset 37498 b426cbdb5a23
parent 37414 d0cea0796295
child 37575 cfc5e281740f
permissions -rw-r--r--
removed Sledgehammer's support for the DFG syntax;
this removes 350 buggy lines from Sledgehammer. SPASS 3.5 and above support the TPTP syntax.
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@36142
     9
  val plural_s : int -> string
blanchet@35963
    10
  val serial_commas : string -> string list -> string list
blanchet@36169
    11
  val replace_all : string -> string -> string -> string
blanchet@36169
    12
  val remove_all : string -> string -> string
blanchet@36170
    13
  val timestamp : unit -> string
blanchet@35963
    14
  val parse_bool_option : bool -> string -> string -> bool option
blanchet@35963
    15
  val parse_time_option : string -> string -> Time.time option
blanchet@36486
    16
  val nat_subscript : int -> string
blanchet@36478
    17
  val unyxml : string -> string
blanchet@36478
    18
  val maybe_quote : string -> string
blanchet@36555
    19
  val monomorphic_term : Type.tyenv -> term -> term
blanchet@36555
    20
  val specialize_type : theory -> (string * typ) -> term -> term
blanchet@35963
    21
end;
blanchet@36170
    22
 
blanchet@35963
    23
structure Sledgehammer_Util : SLEDGEHAMMER_UTIL =
blanchet@35963
    24
struct
blanchet@35963
    25
blanchet@36142
    26
fun plural_s n = if n = 1 then "" else "s"
blanchet@36062
    27
blanchet@35963
    28
fun serial_commas _ [] = ["??"]
blanchet@35963
    29
  | serial_commas _ [s] = [s]
blanchet@35963
    30
  | serial_commas conj [s1, s2] = [s1, conj, s2]
blanchet@35963
    31
  | serial_commas conj [s1, s2, s3] = [s1 ^ ",", s2 ^ ",", conj, s3]
blanchet@35963
    32
  | serial_commas conj (s :: ss) = s ^ "," :: serial_commas conj ss
blanchet@35963
    33
blanchet@36169
    34
fun replace_all bef aft =
blanchet@36169
    35
  let
blanchet@36169
    36
    fun aux seen "" = String.implode (rev seen)
blanchet@36169
    37
      | aux seen s =
blanchet@36183
    38
        if String.isPrefix bef s then
blanchet@36169
    39
          aux seen "" ^ aft ^ aux [] (unprefix bef s)
blanchet@36169
    40
        else
blanchet@36169
    41
          aux (String.sub (s, 0) :: seen) (String.extract (s, 1, NONE))
blanchet@36169
    42
  in aux [] end
blanchet@36169
    43
fun remove_all bef = replace_all bef ""
blanchet@36169
    44
blanchet@36170
    45
val timestamp = Date.fmt "%Y-%m-%d %H:%M:%S" o Date.fromTimeLocal o Time.now
blanchet@36170
    46
blanchet@35963
    47
fun parse_bool_option option name s =
blanchet@35963
    48
  (case s of
blanchet@35963
    49
     "smart" => if option then NONE else raise Option
blanchet@35963
    50
   | "false" => SOME false
blanchet@35963
    51
   | "true" => SOME true
blanchet@35963
    52
   | "" => SOME true
blanchet@35963
    53
   | _ => raise Option)
blanchet@35963
    54
  handle Option.Option =>
blanchet@35963
    55
         let val ss = map quote ((option ? cons "smart") ["true", "false"]) in
blanchet@35963
    56
           error ("Parameter " ^ quote name ^ " must be assigned " ^
blanchet@35963
    57
                  space_implode " " (serial_commas "or" ss) ^ ".")
blanchet@35963
    58
         end
blanchet@35963
    59
blanchet@35963
    60
fun parse_time_option _ "none" = NONE
blanchet@35963
    61
  | parse_time_option name s =
blanchet@35963
    62
    let
blanchet@35963
    63
      val msecs =
blanchet@35963
    64
        case space_explode " " s of
blanchet@35963
    65
          [s1, "min"] => 60000 * the (Int.fromString s1)
blanchet@35963
    66
        | [s1, "s"] => 1000 * the (Int.fromString s1)
blanchet@35963
    67
        | [s1, "ms"] => the (Int.fromString s1)
blanchet@35963
    68
        | _ => 0
blanchet@35963
    69
    in
blanchet@35963
    70
      if msecs <= 0 then
blanchet@35963
    71
        error ("Parameter " ^ quote name ^ " must be assigned a positive time \
blanchet@35963
    72
               \value (e.g., \"60 s\", \"200 ms\") or \"none\".")
blanchet@35963
    73
      else
blanchet@35963
    74
        SOME (Time.fromMilliseconds msecs)
blanchet@35963
    75
    end
blanchet@35963
    76
blanchet@36486
    77
val subscript = implode o map (prefix "\<^isub>") o explode
blanchet@37321
    78
fun nat_subscript n =
blanchet@37321
    79
  n |> string_of_int |> print_mode_active Symbol.xsymbolsN ? subscript
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
wenzelm@36960
    90
           Keyword.is_keyword s) ? quote
blanchet@36478
    91
  end
blanchet@36478
    92
blanchet@36555
    93
fun monomorphic_term subst t =
blanchet@36555
    94
  map_types (map_type_tvar (fn v =>
blanchet@36555
    95
      case Type.lookup subst v of
blanchet@36555
    96
        SOME typ => typ
blanchet@36555
    97
      | NONE => raise TERM ("monomorphic_term: uninstanitated schematic type \
blanchet@36555
    98
                            \variable", [t]))) t
blanchet@36555
    99
blanchet@36555
   100
fun specialize_type thy (s, T) t =
blanchet@36555
   101
  let
blanchet@36555
   102
    fun subst_for (Const (s', T')) =
blanchet@36555
   103
      if s = s' then
blanchet@36555
   104
        SOME (Sign.typ_match thy (T', T) Vartab.empty)
blanchet@36555
   105
        handle Type.TYPE_MATCH => NONE
blanchet@36555
   106
      else
blanchet@36555
   107
        NONE
blanchet@36555
   108
    | subst_for (t1 $ t2) =
blanchet@36555
   109
      (case subst_for t1 of SOME x => SOME x | NONE => subst_for t2)
blanchet@36555
   110
    | subst_for (Abs (_, _, t')) = subst_for t'
blanchet@36555
   111
    | subst_for _ = NONE
blanchet@36555
   112
  in
blanchet@36555
   113
    case subst_for t of
blanchet@36555
   114
      SOME subst => monomorphic_term subst t
blanchet@36555
   115
    | NONE => raise Type.TYPE_MATCH
blanchet@36555
   116
  end
blanchet@36555
   117
blanchet@36555
   118
blanchet@35963
   119
end;