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