src/HOL/Tools/Sledgehammer/sledgehammer_util.ML
author blanchet
Tue Aug 17 16:47:40 2010 +0200 (2010-08-17)
changeset 38490 57de0f12516f
parent 38200 2f531f620cb8
child 38608 01ed56c46259
permissions -rw-r--r--
tuning
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@37962
    11
  val strip_spaces : string -> string
blanchet@35963
    12
  val parse_bool_option : bool -> string -> string -> bool option
blanchet@35963
    13
  val parse_time_option : string -> string -> Time.time option
blanchet@37962
    14
  val scan_integer : string list -> int * string list
blanchet@36486
    15
  val nat_subscript : int -> string
blanchet@36478
    16
  val unyxml : string -> string
blanchet@36478
    17
  val maybe_quote : string -> string
blanchet@38200
    18
  val extensionalize_term : term -> term
blanchet@36555
    19
  val monomorphic_term : Type.tyenv -> term -> term
blanchet@36555
    20
  val specialize_type : theory -> (string * typ) -> term -> term
blanchet@38044
    21
  val subgoal_count : Proof.state -> int
blanchet@37995
    22
  val strip_subgoal : thm -> int -> (string * typ) list * term list * term
blanchet@35963
    23
end;
blanchet@36170
    24
 
blanchet@35963
    25
structure Sledgehammer_Util : SLEDGEHAMMER_UTIL =
blanchet@35963
    26
struct
blanchet@35963
    27
blanchet@36142
    28
fun plural_s n = if n = 1 then "" else "s"
blanchet@36062
    29
blanchet@35963
    30
fun serial_commas _ [] = ["??"]
blanchet@35963
    31
  | serial_commas _ [s] = [s]
blanchet@35963
    32
  | serial_commas conj [s1, s2] = [s1, conj, s2]
blanchet@35963
    33
  | serial_commas conj [s1, s2, s3] = [s1 ^ ",", s2 ^ ",", conj, s3]
blanchet@35963
    34
  | serial_commas conj (s :: ss) = s ^ "," :: serial_commas conj ss
blanchet@35963
    35
blanchet@37962
    36
fun is_ident_char c = Char.isAlphaNum c orelse c = #"_"
blanchet@37962
    37
blanchet@37962
    38
fun strip_spaces_in_list [] = ""
blanchet@37962
    39
  | strip_spaces_in_list [c1] = if Char.isSpace c1 then "" else str c1
blanchet@37962
    40
  | strip_spaces_in_list [c1, c2] =
blanchet@37962
    41
    strip_spaces_in_list [c1] ^ strip_spaces_in_list [c2]
blanchet@37962
    42
  | strip_spaces_in_list (c1 :: c2 :: c3 :: cs) =
blanchet@37962
    43
    if Char.isSpace c1 then
blanchet@37962
    44
      strip_spaces_in_list (c2 :: c3 :: cs)
blanchet@37962
    45
    else if Char.isSpace c2 then
blanchet@37962
    46
      if Char.isSpace c3 then
blanchet@37962
    47
        strip_spaces_in_list (c1 :: c3 :: cs)
blanchet@37962
    48
      else
blanchet@37962
    49
        str c1 ^ (if forall is_ident_char [c1, c3] then " " else "") ^
blanchet@37962
    50
        strip_spaces_in_list (c3 :: cs)
blanchet@37962
    51
    else
blanchet@37962
    52
      str c1 ^ strip_spaces_in_list (c2 :: c3 :: cs)
blanchet@37962
    53
val strip_spaces = strip_spaces_in_list o String.explode
blanchet@37962
    54
blanchet@35963
    55
fun parse_bool_option option name s =
blanchet@35963
    56
  (case s of
blanchet@35963
    57
     "smart" => if option then NONE else raise Option
blanchet@35963
    58
   | "false" => SOME false
blanchet@35963
    59
   | "true" => SOME true
blanchet@35963
    60
   | "" => SOME true
blanchet@35963
    61
   | _ => raise Option)
blanchet@35963
    62
  handle Option.Option =>
blanchet@35963
    63
         let val ss = map quote ((option ? cons "smart") ["true", "false"]) in
blanchet@35963
    64
           error ("Parameter " ^ quote name ^ " must be assigned " ^
blanchet@35963
    65
                  space_implode " " (serial_commas "or" ss) ^ ".")
blanchet@35963
    66
         end
blanchet@35963
    67
blanchet@35963
    68
fun parse_time_option _ "none" = NONE
blanchet@35963
    69
  | parse_time_option name s =
blanchet@35963
    70
    let
blanchet@35963
    71
      val msecs =
blanchet@35963
    72
        case space_explode " " s of
blanchet@35963
    73
          [s1, "min"] => 60000 * the (Int.fromString s1)
blanchet@35963
    74
        | [s1, "s"] => 1000 * the (Int.fromString s1)
blanchet@35963
    75
        | [s1, "ms"] => the (Int.fromString s1)
blanchet@35963
    76
        | _ => 0
blanchet@35963
    77
    in
blanchet@35963
    78
      if msecs <= 0 then
blanchet@35963
    79
        error ("Parameter " ^ quote name ^ " must be assigned a positive time \
blanchet@35963
    80
               \value (e.g., \"60 s\", \"200 ms\") or \"none\".")
blanchet@35963
    81
      else
blanchet@35963
    82
        SOME (Time.fromMilliseconds msecs)
blanchet@35963
    83
    end
blanchet@35963
    84
blanchet@37962
    85
fun is_head_digit s = Char.isDigit (String.sub (s, 0))
blanchet@37962
    86
val scan_integer = Scan.many1 is_head_digit >> (the o Int.fromString o implode)
blanchet@37962
    87
blanchet@36486
    88
val subscript = implode o map (prefix "\<^isub>") o explode
blanchet@37321
    89
fun nat_subscript n =
blanchet@37321
    90
  n |> string_of_int |> print_mode_active Symbol.xsymbolsN ? subscript
blanchet@36486
    91
blanchet@36478
    92
fun plain_string_from_xml_tree t =
blanchet@36478
    93
  Buffer.empty |> XML.add_content t |> Buffer.content
blanchet@36478
    94
val unyxml = plain_string_from_xml_tree o YXML.parse
blanchet@36478
    95
blanchet@36478
    96
val is_long_identifier = forall Syntax.is_identifier o space_explode "."
blanchet@36478
    97
fun maybe_quote y =
blanchet@36478
    98
  let val s = unyxml y in
blanchet@36478
    99
    y |> ((not (is_long_identifier (perhaps (try (unprefix "'")) s)) andalso
blanchet@36478
   100
           not (is_long_identifier (perhaps (try (unprefix "?")) s))) orelse
wenzelm@36960
   101
           Keyword.is_keyword s) ? quote
blanchet@36478
   102
  end
blanchet@36478
   103
blanchet@38200
   104
(* Removes the lambdas from an equation of the form "t = (%x. u)".
blanchet@38200
   105
   (Cf. "extensionalize_theorem" in "Clausifier".) *)
blanchet@38200
   106
fun extensionalize_term t =
blanchet@38200
   107
  let
blanchet@38200
   108
    fun aux j (@{const Trueprop} $ t') = @{const Trueprop} $ aux j t'
blanchet@38200
   109
      | aux j (t as Const (s, Type (_, [Type (_, [_, T']),
blanchet@38200
   110
                                        Type (_, [_, res_T])]))
blanchet@38200
   111
                    $ t2 $ Abs (var_s, var_T, t')) =
blanchet@38200
   112
        if s = @{const_name "op ="} orelse s = @{const_name "=="} then
blanchet@38200
   113
          let val var_t = Var ((var_s, j), var_T) in
blanchet@38200
   114
            Const (s, T' --> T' --> res_T)
blanchet@38200
   115
              $ betapply (t2, var_t) $ subst_bound (var_t, t')
blanchet@38200
   116
            |> aux (j + 1)
blanchet@38200
   117
          end
blanchet@38200
   118
        else
blanchet@38200
   119
          t
blanchet@38200
   120
      | aux _ t = t
blanchet@38200
   121
  in aux (maxidx_of_term t + 1) t end
blanchet@38200
   122
blanchet@36555
   123
fun monomorphic_term subst t =
blanchet@36555
   124
  map_types (map_type_tvar (fn v =>
blanchet@36555
   125
      case Type.lookup subst v of
blanchet@36555
   126
        SOME typ => typ
blanchet@36555
   127
      | NONE => raise TERM ("monomorphic_term: uninstanitated schematic type \
blanchet@36555
   128
                            \variable", [t]))) t
blanchet@36555
   129
blanchet@36555
   130
fun specialize_type thy (s, T) t =
blanchet@36555
   131
  let
blanchet@36555
   132
    fun subst_for (Const (s', T')) =
blanchet@36555
   133
      if s = s' then
blanchet@36555
   134
        SOME (Sign.typ_match thy (T', T) Vartab.empty)
blanchet@36555
   135
        handle Type.TYPE_MATCH => NONE
blanchet@36555
   136
      else
blanchet@36555
   137
        NONE
blanchet@36555
   138
    | subst_for (t1 $ t2) =
blanchet@36555
   139
      (case subst_for t1 of SOME x => SOME x | NONE => subst_for t2)
blanchet@36555
   140
    | subst_for (Abs (_, _, t')) = subst_for t'
blanchet@36555
   141
    | subst_for _ = NONE
blanchet@36555
   142
  in
blanchet@36555
   143
    case subst_for t of
blanchet@36555
   144
      SOME subst => monomorphic_term subst t
blanchet@36555
   145
    | NONE => raise Type.TYPE_MATCH
blanchet@36555
   146
  end
blanchet@36555
   147
blanchet@38044
   148
val subgoal_count = Logic.count_prems o prop_of o #goal o Proof.goal
blanchet@38044
   149
blanchet@37995
   150
fun strip_subgoal goal i =
blanchet@37995
   151
  let
blanchet@37995
   152
    val (t, frees) = Logic.goal_params (prop_of goal) i
blanchet@37995
   153
    val hyp_ts = t |> Logic.strip_assums_hyp |> map (curry subst_bounds frees)
blanchet@37995
   154
    val concl_t = t |> Logic.strip_assums_concl |> curry subst_bounds frees
blanchet@37995
   155
  in (rev (map dest_Free frees), hyp_ts, concl_t) end
blanchet@36555
   156
blanchet@35963
   157
end;