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