src/HOL/Tools/Sledgehammer/sledgehammer_util.ML
author blanchet
Tue Jun 29 10:25:53 2010 +0200 (2010-06-29)
changeset 37626 1146291fe718
parent 37575 cfc5e281740f
child 37962 d7dbe01f48d7
permissions -rw-r--r--
move blacklisting completely out of the clausifier;
the only reason it was in the clausifier as well was the Skolem cache
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@36170
    11
  val timestamp : unit -> string
blanchet@35963
    12
  val parse_bool_option : bool -> string -> string -> bool option
blanchet@35963
    13
  val parse_time_option : string -> string -> Time.time option
blanchet@36486
    14
  val nat_subscript : int -> string
blanchet@36478
    15
  val unyxml : string -> string
blanchet@36478
    16
  val maybe_quote : string -> string
blanchet@36555
    17
  val monomorphic_term : Type.tyenv -> term -> term
blanchet@36555
    18
  val specialize_type : theory -> (string * typ) -> term -> term
blanchet@35963
    19
end;
blanchet@36170
    20
 
blanchet@35963
    21
structure Sledgehammer_Util : SLEDGEHAMMER_UTIL =
blanchet@35963
    22
struct
blanchet@35963
    23
blanchet@36142
    24
fun plural_s n = if n = 1 then "" else "s"
blanchet@36062
    25
blanchet@35963
    26
fun serial_commas _ [] = ["??"]
blanchet@35963
    27
  | serial_commas _ [s] = [s]
blanchet@35963
    28
  | serial_commas conj [s1, s2] = [s1, conj, s2]
blanchet@35963
    29
  | serial_commas conj [s1, s2, s3] = [s1 ^ ",", s2 ^ ",", conj, s3]
blanchet@35963
    30
  | serial_commas conj (s :: ss) = s ^ "," :: serial_commas conj ss
blanchet@35963
    31
blanchet@36170
    32
val timestamp = Date.fmt "%Y-%m-%d %H:%M:%S" o Date.fromTimeLocal o Time.now
blanchet@36170
    33
blanchet@35963
    34
fun parse_bool_option option name s =
blanchet@35963
    35
  (case s of
blanchet@35963
    36
     "smart" => if option then NONE else raise Option
blanchet@35963
    37
   | "false" => SOME false
blanchet@35963
    38
   | "true" => SOME true
blanchet@35963
    39
   | "" => SOME true
blanchet@35963
    40
   | _ => raise Option)
blanchet@35963
    41
  handle Option.Option =>
blanchet@35963
    42
         let val ss = map quote ((option ? cons "smart") ["true", "false"]) in
blanchet@35963
    43
           error ("Parameter " ^ quote name ^ " must be assigned " ^
blanchet@35963
    44
                  space_implode " " (serial_commas "or" ss) ^ ".")
blanchet@35963
    45
         end
blanchet@35963
    46
blanchet@35963
    47
fun parse_time_option _ "none" = NONE
blanchet@35963
    48
  | parse_time_option name s =
blanchet@35963
    49
    let
blanchet@35963
    50
      val msecs =
blanchet@35963
    51
        case space_explode " " s of
blanchet@35963
    52
          [s1, "min"] => 60000 * the (Int.fromString s1)
blanchet@35963
    53
        | [s1, "s"] => 1000 * the (Int.fromString s1)
blanchet@35963
    54
        | [s1, "ms"] => the (Int.fromString s1)
blanchet@35963
    55
        | _ => 0
blanchet@35963
    56
    in
blanchet@35963
    57
      if msecs <= 0 then
blanchet@35963
    58
        error ("Parameter " ^ quote name ^ " must be assigned a positive time \
blanchet@35963
    59
               \value (e.g., \"60 s\", \"200 ms\") or \"none\".")
blanchet@35963
    60
      else
blanchet@35963
    61
        SOME (Time.fromMilliseconds msecs)
blanchet@35963
    62
    end
blanchet@35963
    63
blanchet@36486
    64
val subscript = implode o map (prefix "\<^isub>") o explode
blanchet@37321
    65
fun nat_subscript n =
blanchet@37321
    66
  n |> string_of_int |> print_mode_active Symbol.xsymbolsN ? subscript
blanchet@36486
    67
blanchet@36478
    68
fun plain_string_from_xml_tree t =
blanchet@36478
    69
  Buffer.empty |> XML.add_content t |> Buffer.content
blanchet@36478
    70
val unyxml = plain_string_from_xml_tree o YXML.parse
blanchet@36478
    71
blanchet@36478
    72
val is_long_identifier = forall Syntax.is_identifier o space_explode "."
blanchet@36478
    73
fun maybe_quote y =
blanchet@36478
    74
  let val s = unyxml y in
blanchet@36478
    75
    y |> ((not (is_long_identifier (perhaps (try (unprefix "'")) s)) andalso
blanchet@36478
    76
           not (is_long_identifier (perhaps (try (unprefix "?")) s))) orelse
wenzelm@36960
    77
           Keyword.is_keyword s) ? quote
blanchet@36478
    78
  end
blanchet@36478
    79
blanchet@36555
    80
fun monomorphic_term subst t =
blanchet@36555
    81
  map_types (map_type_tvar (fn v =>
blanchet@36555
    82
      case Type.lookup subst v of
blanchet@36555
    83
        SOME typ => typ
blanchet@36555
    84
      | NONE => raise TERM ("monomorphic_term: uninstanitated schematic type \
blanchet@36555
    85
                            \variable", [t]))) t
blanchet@36555
    86
blanchet@36555
    87
fun specialize_type thy (s, T) t =
blanchet@36555
    88
  let
blanchet@36555
    89
    fun subst_for (Const (s', T')) =
blanchet@36555
    90
      if s = s' then
blanchet@36555
    91
        SOME (Sign.typ_match thy (T', T) Vartab.empty)
blanchet@36555
    92
        handle Type.TYPE_MATCH => NONE
blanchet@36555
    93
      else
blanchet@36555
    94
        NONE
blanchet@36555
    95
    | subst_for (t1 $ t2) =
blanchet@36555
    96
      (case subst_for t1 of SOME x => SOME x | NONE => subst_for t2)
blanchet@36555
    97
    | subst_for (Abs (_, _, t')) = subst_for t'
blanchet@36555
    98
    | subst_for _ = NONE
blanchet@36555
    99
  in
blanchet@36555
   100
    case subst_for t of
blanchet@36555
   101
      SOME subst => monomorphic_term subst t
blanchet@36555
   102
    | NONE => raise Type.TYPE_MATCH
blanchet@36555
   103
  end
blanchet@36555
   104
blanchet@36555
   105
blanchet@35963
   106
end;