src/HOL/Tools/Sledgehammer/sledgehammer_util.ML
author blanchet
Mon Jun 14 16:43:44 2010 +0200 (2010-06-14)
changeset 37414 d0cea0796295
parent 37321 9d7cfae95b30
child 37498 b426cbdb5a23
permissions -rw-r--r--
expect SPASS 3.7, and give a friendly warning if an older version is used
     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 pairf : ('a -> 'b) -> ('a -> 'c) -> 'a -> 'b * 'c
    10   val plural_s : int -> string
    11   val serial_commas : string -> string list -> string list
    12   val replace_all : string -> string -> string -> string
    13   val remove_all : string -> string -> string
    14   val timestamp : unit -> string
    15   val parse_bool_option : bool -> string -> string -> bool option
    16   val parse_time_option : string -> string -> Time.time option
    17   val nat_subscript : int -> string
    18   val unyxml : string -> string
    19   val maybe_quote : string -> string
    20   val monomorphic_term : Type.tyenv -> term -> term
    21   val specialize_type : theory -> (string * typ) -> term -> term
    22 end;
    23  
    24 structure Sledgehammer_Util : SLEDGEHAMMER_UTIL =
    25 struct
    26 
    27 fun pairf f g x = (f x, g x)
    28 
    29 fun plural_s n = if n = 1 then "" else "s"
    30 
    31 fun serial_commas _ [] = ["??"]
    32   | serial_commas _ [s] = [s]
    33   | serial_commas conj [s1, s2] = [s1, conj, s2]
    34   | serial_commas conj [s1, s2, s3] = [s1 ^ ",", s2 ^ ",", conj, s3]
    35   | serial_commas conj (s :: ss) = s ^ "," :: serial_commas conj ss
    36 
    37 fun replace_all bef aft =
    38   let
    39     fun aux seen "" = String.implode (rev seen)
    40       | aux seen s =
    41         if String.isPrefix bef s then
    42           aux seen "" ^ aft ^ aux [] (unprefix bef s)
    43         else
    44           aux (String.sub (s, 0) :: seen) (String.extract (s, 1, NONE))
    45   in aux [] end
    46 fun remove_all bef = replace_all bef ""
    47 
    48 val timestamp = Date.fmt "%Y-%m-%d %H:%M:%S" o Date.fromTimeLocal o Time.now
    49 
    50 fun parse_bool_option option name s =
    51   (case s of
    52      "smart" => if option then NONE else raise Option
    53    | "false" => SOME false
    54    | "true" => SOME true
    55    | "" => SOME true
    56    | _ => raise Option)
    57   handle Option.Option =>
    58          let val ss = map quote ((option ? cons "smart") ["true", "false"]) in
    59            error ("Parameter " ^ quote name ^ " must be assigned " ^
    60                   space_implode " " (serial_commas "or" ss) ^ ".")
    61          end
    62 
    63 fun parse_time_option _ "none" = NONE
    64   | parse_time_option name s =
    65     let
    66       val msecs =
    67         case space_explode " " s of
    68           [s1, "min"] => 60000 * the (Int.fromString s1)
    69         | [s1, "s"] => 1000 * the (Int.fromString s1)
    70         | [s1, "ms"] => the (Int.fromString s1)
    71         | _ => 0
    72     in
    73       if msecs <= 0 then
    74         error ("Parameter " ^ quote name ^ " must be assigned a positive time \
    75                \value (e.g., \"60 s\", \"200 ms\") or \"none\".")
    76       else
    77         SOME (Time.fromMilliseconds msecs)
    78     end
    79 
    80 val subscript = implode o map (prefix "\<^isub>") o explode
    81 fun nat_subscript n =
    82   n |> string_of_int |> print_mode_active Symbol.xsymbolsN ? subscript
    83 
    84 fun plain_string_from_xml_tree t =
    85   Buffer.empty |> XML.add_content t |> Buffer.content
    86 val unyxml = plain_string_from_xml_tree o YXML.parse
    87 
    88 val is_long_identifier = forall Syntax.is_identifier o space_explode "."
    89 fun maybe_quote y =
    90   let val s = unyxml y in
    91     y |> ((not (is_long_identifier (perhaps (try (unprefix "'")) s)) andalso
    92            not (is_long_identifier (perhaps (try (unprefix "?")) s))) orelse
    93            Keyword.is_keyword s) ? quote
    94   end
    95 
    96 fun monomorphic_term subst t =
    97   map_types (map_type_tvar (fn v =>
    98       case Type.lookup subst v of
    99         SOME typ => typ
   100       | NONE => raise TERM ("monomorphic_term: uninstanitated schematic type \
   101                             \variable", [t]))) t
   102 
   103 fun specialize_type thy (s, T) t =
   104   let
   105     fun subst_for (Const (s', T')) =
   106       if s = s' then
   107         SOME (Sign.typ_match thy (T', T) Vartab.empty)
   108         handle Type.TYPE_MATCH => NONE
   109       else
   110         NONE
   111     | subst_for (t1 $ t2) =
   112       (case subst_for t1 of SOME x => SOME x | NONE => subst_for t2)
   113     | subst_for (Abs (_, _, t')) = subst_for t'
   114     | subst_for _ = NONE
   115   in
   116     case subst_for t of
   117       SOME subst => monomorphic_term subst t
   118     | NONE => raise Type.TYPE_MATCH
   119   end
   120 
   121 
   122 end;