src/HOL/Tools/Sledgehammer/sledgehammer_util.ML
author blanchet
Tue Jul 27 17:43:11 2010 +0200 (2010-07-27)
changeset 38019 e207a64e1e0b
parent 37995 06f02b15ef8a
child 38044 463177795c49
permissions -rw-r--r--
complete renaming of "Sledgehammer_TPTP_Format" to "ATP_Problem"
     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 strip_spaces : string -> string
    12   val parse_bool_option : bool -> string -> string -> bool option
    13   val parse_time_option : string -> string -> Time.time option
    14   val scan_integer : string list -> int * string list
    15   val nat_subscript : int -> string
    16   val unyxml : string -> string
    17   val maybe_quote : string -> string
    18   val monomorphic_term : Type.tyenv -> term -> term
    19   val specialize_type : theory -> (string * typ) -> term -> term
    20   val strip_subgoal : thm -> int -> (string * typ) list * term list * 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 is_ident_char c = Char.isAlphaNum c orelse c = #"_"
    35 
    36 fun strip_spaces_in_list [] = ""
    37   | strip_spaces_in_list [c1] = if Char.isSpace c1 then "" else str c1
    38   | strip_spaces_in_list [c1, c2] =
    39     strip_spaces_in_list [c1] ^ strip_spaces_in_list [c2]
    40   | strip_spaces_in_list (c1 :: c2 :: c3 :: cs) =
    41     if Char.isSpace c1 then
    42       strip_spaces_in_list (c2 :: c3 :: cs)
    43     else if Char.isSpace c2 then
    44       if Char.isSpace c3 then
    45         strip_spaces_in_list (c1 :: c3 :: cs)
    46       else
    47         str c1 ^ (if forall is_ident_char [c1, c3] then " " else "") ^
    48         strip_spaces_in_list (c3 :: cs)
    49     else
    50       str c1 ^ strip_spaces_in_list (c2 :: c3 :: cs)
    51 val strip_spaces = strip_spaces_in_list o String.explode
    52 
    53 fun parse_bool_option option name s =
    54   (case s of
    55      "smart" => if option then NONE else raise Option
    56    | "false" => SOME false
    57    | "true" => SOME true
    58    | "" => SOME true
    59    | _ => raise Option)
    60   handle Option.Option =>
    61          let val ss = map quote ((option ? cons "smart") ["true", "false"]) in
    62            error ("Parameter " ^ quote name ^ " must be assigned " ^
    63                   space_implode " " (serial_commas "or" ss) ^ ".")
    64          end
    65 
    66 fun parse_time_option _ "none" = NONE
    67   | parse_time_option name s =
    68     let
    69       val msecs =
    70         case space_explode " " s of
    71           [s1, "min"] => 60000 * the (Int.fromString s1)
    72         | [s1, "s"] => 1000 * the (Int.fromString s1)
    73         | [s1, "ms"] => the (Int.fromString s1)
    74         | _ => 0
    75     in
    76       if msecs <= 0 then
    77         error ("Parameter " ^ quote name ^ " must be assigned a positive time \
    78                \value (e.g., \"60 s\", \"200 ms\") or \"none\".")
    79       else
    80         SOME (Time.fromMilliseconds msecs)
    81     end
    82 
    83 fun is_head_digit s = Char.isDigit (String.sub (s, 0))
    84 val scan_integer = Scan.many1 is_head_digit >> (the o Int.fromString o implode)
    85 
    86 val subscript = implode o map (prefix "\<^isub>") o explode
    87 fun nat_subscript n =
    88   n |> string_of_int |> print_mode_active Symbol.xsymbolsN ? subscript
    89 
    90 fun plain_string_from_xml_tree t =
    91   Buffer.empty |> XML.add_content t |> Buffer.content
    92 val unyxml = plain_string_from_xml_tree o YXML.parse
    93 
    94 val is_long_identifier = forall Syntax.is_identifier o space_explode "."
    95 fun maybe_quote y =
    96   let val s = unyxml y in
    97     y |> ((not (is_long_identifier (perhaps (try (unprefix "'")) s)) andalso
    98            not (is_long_identifier (perhaps (try (unprefix "?")) s))) orelse
    99            Keyword.is_keyword s) ? quote
   100   end
   101 
   102 fun monomorphic_term subst t =
   103   map_types (map_type_tvar (fn v =>
   104       case Type.lookup subst v of
   105         SOME typ => typ
   106       | NONE => raise TERM ("monomorphic_term: uninstanitated schematic type \
   107                             \variable", [t]))) t
   108 
   109 fun specialize_type thy (s, T) t =
   110   let
   111     fun subst_for (Const (s', T')) =
   112       if s = s' then
   113         SOME (Sign.typ_match thy (T', T) Vartab.empty)
   114         handle Type.TYPE_MATCH => NONE
   115       else
   116         NONE
   117     | subst_for (t1 $ t2) =
   118       (case subst_for t1 of SOME x => SOME x | NONE => subst_for t2)
   119     | subst_for (Abs (_, _, t')) = subst_for t'
   120     | subst_for _ = NONE
   121   in
   122     case subst_for t of
   123       SOME subst => monomorphic_term subst t
   124     | NONE => raise Type.TYPE_MATCH
   125   end
   126 
   127 fun strip_subgoal goal i =
   128   let
   129     val (t, frees) = Logic.goal_params (prop_of goal) i
   130     val hyp_ts = t |> Logic.strip_assums_hyp |> map (curry subst_bounds frees)
   131     val concl_t = t |> Logic.strip_assums_concl |> curry subst_bounds frees
   132   in (rev (map dest_Free frees), hyp_ts, concl_t) end
   133 
   134 end;