src/HOL/Tools/Sledgehammer/sledgehammer_util.ML
author blanchet
Fri, 23 Jul 2010 21:29:29 +0200
changeset 38199 d7dbe01f48d7
parent 37575 cfc5e281740f
child 38229 06f02b15ef8a
permissions -rw-r--r--
keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
this is rather involved because the Flotter FOF-to-CNF translator is normally implicit.
We must make this an explicit step and parse the Flotter output to find out which clauses correspond to which formulas.
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@36140
     9
  val plural_s : int -> string
blanchet@35961
    10
  val serial_commas : string -> string list -> string list
blanchet@36170
    11
  val timestamp : unit -> string
blanchet@38199
    12
  val strip_spaces : string -> string
blanchet@35961
    13
  val parse_bool_option : bool -> string -> string -> bool option
blanchet@35961
    14
  val parse_time_option : string -> string -> Time.time option
blanchet@38199
    15
  val scan_integer : string list -> int * string list
blanchet@36482
    16
  val nat_subscript : int -> string
blanchet@36474
    17
  val unyxml : string -> string
blanchet@36474
    18
  val maybe_quote : string -> string
blanchet@36553
    19
  val monomorphic_term : Type.tyenv -> term -> term
blanchet@36553
    20
  val specialize_type : theory -> (string * typ) -> term -> term
blanchet@35961
    21
end;
blanchet@36170
    22
 
blanchet@35961
    23
structure Sledgehammer_Util : SLEDGEHAMMER_UTIL =
blanchet@35961
    24
struct
blanchet@35961
    25
blanchet@36140
    26
fun plural_s n = if n = 1 then "" else "s"
blanchet@36062
    27
blanchet@35961
    28
fun serial_commas _ [] = ["??"]
blanchet@35961
    29
  | serial_commas _ [s] = [s]
blanchet@35961
    30
  | serial_commas conj [s1, s2] = [s1, conj, s2]
blanchet@35961
    31
  | serial_commas conj [s1, s2, s3] = [s1 ^ ",", s2 ^ ",", conj, s3]
blanchet@35961
    32
  | serial_commas conj (s :: ss) = s ^ "," :: serial_commas conj ss
blanchet@35961
    33
blanchet@36170
    34
val timestamp = Date.fmt "%Y-%m-%d %H:%M:%S" o Date.fromTimeLocal o Time.now
blanchet@36170
    35
blanchet@38199
    36
fun is_ident_char c = Char.isAlphaNum c orelse c = #"_"
blanchet@38199
    37
blanchet@38199
    38
fun strip_spaces_in_list [] = ""
blanchet@38199
    39
  | strip_spaces_in_list [c1] = if Char.isSpace c1 then "" else str c1
blanchet@38199
    40
  | strip_spaces_in_list [c1, c2] =
blanchet@38199
    41
    strip_spaces_in_list [c1] ^ strip_spaces_in_list [c2]
blanchet@38199
    42
  | strip_spaces_in_list (c1 :: c2 :: c3 :: cs) =
blanchet@38199
    43
    if Char.isSpace c1 then
blanchet@38199
    44
      strip_spaces_in_list (c2 :: c3 :: cs)
blanchet@38199
    45
    else if Char.isSpace c2 then
blanchet@38199
    46
      if Char.isSpace c3 then
blanchet@38199
    47
        strip_spaces_in_list (c1 :: c3 :: cs)
blanchet@38199
    48
      else
blanchet@38199
    49
        str c1 ^ (if forall is_ident_char [c1, c3] then " " else "") ^
blanchet@38199
    50
        strip_spaces_in_list (c3 :: cs)
blanchet@38199
    51
    else
blanchet@38199
    52
      str c1 ^ strip_spaces_in_list (c2 :: c3 :: cs)
blanchet@38199
    53
val strip_spaces = strip_spaces_in_list o String.explode
blanchet@38199
    54
blanchet@35961
    55
fun parse_bool_option option name s =
blanchet@35961
    56
  (case s of
blanchet@35961
    57
     "smart" => if option then NONE else raise Option
blanchet@35961
    58
   | "false" => SOME false
blanchet@35961
    59
   | "true" => SOME true
blanchet@35961
    60
   | "" => SOME true
blanchet@35961
    61
   | _ => raise Option)
blanchet@35961
    62
  handle Option.Option =>
blanchet@35961
    63
         let val ss = map quote ((option ? cons "smart") ["true", "false"]) in
blanchet@35961
    64
           error ("Parameter " ^ quote name ^ " must be assigned " ^
blanchet@35961
    65
                  space_implode " " (serial_commas "or" ss) ^ ".")
blanchet@35961
    66
         end
blanchet@35961
    67
blanchet@35961
    68
fun parse_time_option _ "none" = NONE
blanchet@35961
    69
  | parse_time_option name s =
blanchet@35961
    70
    let
blanchet@35961
    71
      val msecs =
blanchet@35961
    72
        case space_explode " " s of
blanchet@35961
    73
          [s1, "min"] => 60000 * the (Int.fromString s1)
blanchet@35961
    74
        | [s1, "s"] => 1000 * the (Int.fromString s1)
blanchet@35961
    75
        | [s1, "ms"] => the (Int.fromString s1)
blanchet@35961
    76
        | _ => 0
blanchet@35961
    77
    in
blanchet@35961
    78
      if msecs <= 0 then
blanchet@35961
    79
        error ("Parameter " ^ quote name ^ " must be assigned a positive time \
blanchet@35961
    80
               \value (e.g., \"60 s\", \"200 ms\") or \"none\".")
blanchet@35961
    81
      else
blanchet@35961
    82
        SOME (Time.fromMilliseconds msecs)
blanchet@35961
    83
    end
blanchet@35961
    84
blanchet@38199
    85
fun is_head_digit s = Char.isDigit (String.sub (s, 0))
blanchet@38199
    86
val scan_integer = Scan.many1 is_head_digit >> (the o Int.fromString o implode)
blanchet@38199
    87
blanchet@36482
    88
val subscript = implode o map (prefix "\<^isub>") o explode
blanchet@37318
    89
fun nat_subscript n =
blanchet@37318
    90
  n |> string_of_int |> print_mode_active Symbol.xsymbolsN ? subscript
blanchet@36482
    91
blanchet@36474
    92
fun plain_string_from_xml_tree t =
blanchet@36474
    93
  Buffer.empty |> XML.add_content t |> Buffer.content
blanchet@36474
    94
val unyxml = plain_string_from_xml_tree o YXML.parse
blanchet@36474
    95
blanchet@36474
    96
val is_long_identifier = forall Syntax.is_identifier o space_explode "."
blanchet@36474
    97
fun maybe_quote y =
blanchet@36474
    98
  let val s = unyxml y in
blanchet@36474
    99
    y |> ((not (is_long_identifier (perhaps (try (unprefix "'")) s)) andalso
blanchet@36474
   100
           not (is_long_identifier (perhaps (try (unprefix "?")) s))) orelse
wenzelm@36970
   101
           Keyword.is_keyword s) ? quote
blanchet@36474
   102
  end
blanchet@36474
   103
blanchet@36553
   104
fun monomorphic_term subst t =
blanchet@36553
   105
  map_types (map_type_tvar (fn v =>
blanchet@36553
   106
      case Type.lookup subst v of
blanchet@36553
   107
        SOME typ => typ
blanchet@36553
   108
      | NONE => raise TERM ("monomorphic_term: uninstanitated schematic type \
blanchet@36553
   109
                            \variable", [t]))) t
blanchet@36553
   110
blanchet@36553
   111
fun specialize_type thy (s, T) t =
blanchet@36553
   112
  let
blanchet@36553
   113
    fun subst_for (Const (s', T')) =
blanchet@36553
   114
      if s = s' then
blanchet@36553
   115
        SOME (Sign.typ_match thy (T', T) Vartab.empty)
blanchet@36553
   116
        handle Type.TYPE_MATCH => NONE
blanchet@36553
   117
      else
blanchet@36553
   118
        NONE
blanchet@36553
   119
    | subst_for (t1 $ t2) =
blanchet@36553
   120
      (case subst_for t1 of SOME x => SOME x | NONE => subst_for t2)
blanchet@36553
   121
    | subst_for (Abs (_, _, t')) = subst_for t'
blanchet@36553
   122
    | subst_for _ = NONE
blanchet@36553
   123
  in
blanchet@36553
   124
    case subst_for t of
blanchet@36553
   125
      SOME subst => monomorphic_term subst t
blanchet@36553
   126
    | NONE => raise Type.TYPE_MATCH
blanchet@36553
   127
  end
blanchet@36553
   128
blanchet@36553
   129
blanchet@35961
   130
end;