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