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