src/HOL/Tools/Sledgehammer/sledgehammer_util.ML
author blanchet
Mon Aug 23 14:54:17 2010 +0200 (2010-08-23 ago)
changeset 38652 e063be321438
parent 38608 01ed56c46259
child 38696 4c6b65d6a135
permissions -rw-r--r--
perform eta-expansion of quantifier bodies in Sledgehammer translation when needed + transform elim rules later;
it's a mistake to transform the elim rules too early because then we lose some info, e.g. "no_atp" attributes
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@36555
    18
  val monomorphic_term : Type.tyenv -> term -> term
blanchet@38652
    19
  val eta_expand : typ list -> term -> int -> term
blanchet@38652
    20
  val transform_elim_term : term -> term
blanchet@36555
    21
  val specialize_type : theory -> (string * typ) -> term -> term
blanchet@38044
    22
  val subgoal_count : Proof.state -> int
blanchet@37995
    23
  val strip_subgoal : thm -> int -> (string * typ) list * term list * term
blanchet@35963
    24
end;
blanchet@36170
    25
 
blanchet@35963
    26
structure Sledgehammer_Util : SLEDGEHAMMER_UTIL =
blanchet@35963
    27
struct
blanchet@35963
    28
blanchet@36142
    29
fun plural_s n = if n = 1 then "" else "s"
blanchet@36062
    30
blanchet@35963
    31
fun serial_commas _ [] = ["??"]
blanchet@35963
    32
  | serial_commas _ [s] = [s]
blanchet@35963
    33
  | serial_commas conj [s1, s2] = [s1, conj, s2]
blanchet@35963
    34
  | serial_commas conj [s1, s2, s3] = [s1 ^ ",", s2 ^ ",", conj, s3]
blanchet@35963
    35
  | serial_commas conj (s :: ss) = s ^ "," :: serial_commas conj ss
blanchet@35963
    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@38652
   112
fun eta_expand _ t 0 = t
blanchet@38652
   113
  | eta_expand Ts (Abs (s, T, t')) n =
blanchet@38652
   114
    Abs (s, T, eta_expand (T :: Ts) t' (n - 1))
blanchet@38652
   115
  | eta_expand Ts t n =
blanchet@38652
   116
    fold_rev (fn T => fn t' => Abs ("x" ^ nat_subscript n, T, t'))
blanchet@38652
   117
             (List.take (binder_types (fastype_of1 (Ts, t)), n))
blanchet@38652
   118
             (list_comb (incr_boundvars n t, map Bound (n - 1 downto 0)))
blanchet@38652
   119
blanchet@38652
   120
(* Converts an elim-rule into an equivalent theorem that does not have the
blanchet@38652
   121
   predicate variable. Leaves other theorems unchanged. We simply instantiate
blanchet@38652
   122
   the conclusion variable to False. (Cf. "transform_elim_theorem" in
blanchet@38652
   123
   "Clausifier".) *)
blanchet@38652
   124
fun transform_elim_term t =
blanchet@38652
   125
  case Logic.strip_imp_concl t of
blanchet@38652
   126
    @{const Trueprop} $ Var (z, @{typ bool}) =>
blanchet@38652
   127
    subst_Vars [(z, @{const False})] t
blanchet@38652
   128
  | Var (z, @{typ prop}) => subst_Vars [(z, @{prop False})] t
blanchet@38652
   129
  | _ => t
blanchet@38652
   130
blanchet@36555
   131
fun specialize_type thy (s, T) t =
blanchet@36555
   132
  let
blanchet@36555
   133
    fun subst_for (Const (s', T')) =
blanchet@36555
   134
      if s = s' then
blanchet@36555
   135
        SOME (Sign.typ_match thy (T', T) Vartab.empty)
blanchet@36555
   136
        handle Type.TYPE_MATCH => NONE
blanchet@36555
   137
      else
blanchet@36555
   138
        NONE
blanchet@36555
   139
    | subst_for (t1 $ t2) =
blanchet@36555
   140
      (case subst_for t1 of SOME x => SOME x | NONE => subst_for t2)
blanchet@36555
   141
    | subst_for (Abs (_, _, t')) = subst_for t'
blanchet@36555
   142
    | subst_for _ = NONE
blanchet@36555
   143
  in
blanchet@36555
   144
    case subst_for t of
blanchet@36555
   145
      SOME subst => monomorphic_term subst t
blanchet@36555
   146
    | NONE => raise Type.TYPE_MATCH
blanchet@36555
   147
  end
blanchet@36555
   148
blanchet@38044
   149
val subgoal_count = Logic.count_prems o prop_of o #goal o Proof.goal
blanchet@38044
   150
blanchet@37995
   151
fun strip_subgoal goal i =
blanchet@37995
   152
  let
blanchet@37995
   153
    val (t, frees) = Logic.goal_params (prop_of goal) i
blanchet@37995
   154
    val hyp_ts = t |> Logic.strip_assums_hyp |> map (curry subst_bounds frees)
blanchet@37995
   155
    val concl_t = t |> Logic.strip_assums_concl |> curry subst_bounds frees
blanchet@37995
   156
  in (rev (map dest_Free frees), hyp_ts, concl_t) end
blanchet@36555
   157
blanchet@35963
   158
end;