src/HOL/Tools/Sledgehammer/sledgehammer_util.ML
author wenzelm
Fri, 08 Apr 2011 16:34:14 +0200
changeset 43162 b1f544c84040
parent 41459 1e2e16bc0077
child 43550 b6c27cf04fe9
permissions -rw-r--r--
discontinued special treatment of structure Lexicon;
blanchet@36062
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_util.ML
blanchet@35961
     2
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@35961
     3
blanchet@35961
     4
General-purpose functions used by the Sledgehammer modules.
blanchet@35961
     5
*)
blanchet@35961
     6
blanchet@35961
     7
signature SLEDGEHAMMER_UTIL =
blanchet@35961
     8
sig
blanchet@36140
     9
  val plural_s : int -> string
blanchet@35961
    10
  val serial_commas : string -> string list -> string list
blanchet@38977
    11
  val simplify_spaces : string -> string
blanchet@35961
    12
  val parse_bool_option : bool -> string -> string -> bool option
blanchet@35961
    13
  val parse_time_option : string -> string -> Time.time option
blanchet@40582
    14
  val string_from_time : Time.time -> string
blanchet@36482
    15
  val nat_subscript : int -> string
blanchet@36474
    16
  val unyxml : string -> string
blanchet@36474
    17
  val maybe_quote : string -> string
blanchet@36553
    18
  val monomorphic_term : Type.tyenv -> term -> term
blanchet@38890
    19
  val eta_expand : typ list -> term -> int -> term
blanchet@38890
    20
  val transform_elim_term : term -> term
blanchet@36553
    21
  val specialize_type : theory -> (string * typ) -> term -> term
blanchet@38290
    22
  val subgoal_count : Proof.state -> int
blanchet@38229
    23
  val strip_subgoal : thm -> int -> (string * typ) list * term list * term
blanchet@38935
    24
  val reserved_isar_keyword_table : unit -> unit Symtab.table
blanchet@35961
    25
end;
blanchet@39564
    26
blanchet@35961
    27
structure Sledgehammer_Util : SLEDGEHAMMER_UTIL =
blanchet@35961
    28
struct
blanchet@35961
    29
blanchet@36140
    30
fun plural_s n = if n = 1 then "" else "s"
blanchet@36062
    31
blanchet@35961
    32
fun serial_commas _ [] = ["??"]
blanchet@35961
    33
  | serial_commas _ [s] = [s]
blanchet@35961
    34
  | serial_commas conj [s1, s2] = [s1, conj, s2]
blanchet@35961
    35
  | serial_commas conj [s1, s2, s3] = [s1 ^ ",", s2 ^ ",", conj, s3]
blanchet@35961
    36
  | serial_commas conj (s :: ss) = s ^ "," :: serial_commas conj ss
blanchet@35961
    37
blanchet@39697
    38
val simplify_spaces = ATP_Proof.strip_spaces (K true)
blanchet@38199
    39
blanchet@35961
    40
fun parse_bool_option option name s =
blanchet@35961
    41
  (case s of
blanchet@35961
    42
     "smart" => if option then NONE else raise Option
blanchet@35961
    43
   | "false" => SOME false
blanchet@35961
    44
   | "true" => SOME true
blanchet@35961
    45
   | "" => SOME true
blanchet@35961
    46
   | _ => raise Option)
blanchet@35961
    47
  handle Option.Option =>
blanchet@35961
    48
         let val ss = map quote ((option ? cons "smart") ["true", "false"]) in
blanchet@35961
    49
           error ("Parameter " ^ quote name ^ " must be assigned " ^
blanchet@35961
    50
                  space_implode " " (serial_commas "or" ss) ^ ".")
blanchet@35961
    51
         end
blanchet@35961
    52
blanchet@40582
    53
val has_junk =
wenzelm@40875
    54
  exists (fn s => not (Symbol.is_digit s) andalso s <> ".") o raw_explode (* FIXME Symbol.explode (?) *)
blanchet@40582
    55
blanchet@35961
    56
fun parse_time_option _ "none" = NONE
blanchet@35961
    57
  | parse_time_option name s =
blanchet@40582
    58
    let val secs = if has_junk s then NONE else Real.fromString s in
blanchet@40582
    59
      if is_none secs orelse Real.<= (the secs, 0.0) then
blanchet@40582
    60
        error ("Parameter " ^ quote name ^ " must be assigned a positive \
blanchet@40582
    61
               \number of seconds (e.g., \"60\", \"0.5\") or \"none\".")
blanchet@35961
    62
      else
blanchet@40582
    63
        SOME (seconds (the secs))
blanchet@35961
    64
    end
blanchet@35961
    65
blanchet@40582
    66
fun string_from_time t =
blanchet@40582
    67
  string_of_real (0.01 * Real.fromInt (Time.toMilliseconds t div 10)) ^ " s"
blanchet@40582
    68
wenzelm@40875
    69
val subscript = implode o map (prefix "\<^isub>") o raw_explode  (* FIXME Symbol.explode (?) *)
blanchet@37318
    70
fun nat_subscript n =
blanchet@37318
    71
  n |> string_of_int |> print_mode_active Symbol.xsymbolsN ? subscript
blanchet@36482
    72
wenzelm@39812
    73
val unyxml = XML.content_of o YXML.parse_body
blanchet@36474
    74
wenzelm@43162
    75
val is_long_identifier = forall Lexicon.is_identifier o space_explode "."
blanchet@36474
    76
fun maybe_quote y =
blanchet@36474
    77
  let val s = unyxml y in
blanchet@36474
    78
    y |> ((not (is_long_identifier (perhaps (try (unprefix "'")) s)) andalso
blanchet@36474
    79
           not (is_long_identifier (perhaps (try (unprefix "?")) s))) orelse
wenzelm@36970
    80
           Keyword.is_keyword s) ? quote
blanchet@36474
    81
  end
blanchet@36474
    82
blanchet@36553
    83
fun monomorphic_term subst t =
blanchet@36553
    84
  map_types (map_type_tvar (fn v =>
blanchet@36553
    85
      case Type.lookup subst v of
blanchet@36553
    86
        SOME typ => typ
blanchet@36553
    87
      | NONE => raise TERM ("monomorphic_term: uninstanitated schematic type \
blanchet@36553
    88
                            \variable", [t]))) t
blanchet@36553
    89
blanchet@38890
    90
fun eta_expand _ t 0 = t
blanchet@38890
    91
  | eta_expand Ts (Abs (s, T, t')) n =
blanchet@38890
    92
    Abs (s, T, eta_expand (T :: Ts) t' (n - 1))
blanchet@38890
    93
  | eta_expand Ts t n =
blanchet@38890
    94
    fold_rev (fn T => fn t' => Abs ("x" ^ nat_subscript n, T, t'))
blanchet@38890
    95
             (List.take (binder_types (fastype_of1 (Ts, t)), n))
blanchet@38890
    96
             (list_comb (incr_boundvars n t, map Bound (n - 1 downto 0)))
blanchet@38890
    97
blanchet@38890
    98
(* Converts an elim-rule into an equivalent theorem that does not have the
blanchet@38890
    99
   predicate variable. Leaves other theorems unchanged. We simply instantiate
blanchet@38890
   100
   the conclusion variable to False. (Cf. "transform_elim_theorem" in
blanchet@40071
   101
   "Meson_Clausify".) *)
blanchet@38890
   102
fun transform_elim_term t =
blanchet@38890
   103
  case Logic.strip_imp_concl t of
blanchet@38890
   104
    @{const Trueprop} $ Var (z, @{typ bool}) =>
blanchet@38890
   105
    subst_Vars [(z, @{const False})] t
blanchet@38890
   106
  | Var (z, @{typ prop}) => subst_Vars [(z, @{prop False})] t
blanchet@38890
   107
  | _ => t
blanchet@38890
   108
blanchet@36553
   109
fun specialize_type thy (s, T) t =
blanchet@36553
   110
  let
blanchet@36553
   111
    fun subst_for (Const (s', T')) =
blanchet@36553
   112
      if s = s' then
blanchet@36553
   113
        SOME (Sign.typ_match thy (T', T) Vartab.empty)
blanchet@36553
   114
        handle Type.TYPE_MATCH => NONE
blanchet@36553
   115
      else
blanchet@36553
   116
        NONE
blanchet@36553
   117
    | subst_for (t1 $ t2) =
blanchet@36553
   118
      (case subst_for t1 of SOME x => SOME x | NONE => subst_for t2)
blanchet@36553
   119
    | subst_for (Abs (_, _, t')) = subst_for t'
blanchet@36553
   120
    | subst_for _ = NONE
blanchet@36553
   121
  in
blanchet@36553
   122
    case subst_for t of
blanchet@36553
   123
      SOME subst => monomorphic_term subst t
blanchet@36553
   124
    | NONE => raise Type.TYPE_MATCH
blanchet@36553
   125
  end
blanchet@36553
   126
blanchet@38290
   127
val subgoal_count = Logic.count_prems o prop_of o #goal o Proof.goal
blanchet@38290
   128
blanchet@38229
   129
fun strip_subgoal goal i =
blanchet@38229
   130
  let
blanchet@38229
   131
    val (t, frees) = Logic.goal_params (prop_of goal) i
blanchet@38229
   132
    val hyp_ts = t |> Logic.strip_assums_hyp |> map (curry subst_bounds frees)
blanchet@38229
   133
    val concl_t = t |> Logic.strip_assums_concl |> curry subst_bounds frees
blanchet@38229
   134
  in (rev (map dest_Free frees), hyp_ts, concl_t) end
blanchet@36553
   135
blanchet@38935
   136
fun reserved_isar_keyword_table () =
blanchet@38935
   137
  union (op =) (Keyword.dest_keywords ()) (Keyword.dest_commands ())
blanchet@38935
   138
  |> map (rpair ()) |> Symtab.make
blanchet@38935
   139
blanchet@35961
   140
end;