src/HOL/Tools/Sledgehammer/sledgehammer_util.ML
author blanchet
Fri, 27 May 2011 10:30:08 +0200
changeset 43870 3e060b1c844b
parent 43846 c96f06bffd90
child 43874 c4b9b4be90c4
permissions -rw-r--r--
use helpers and tweak Quickcheck's priority to it comes second (to give Solve Direct slightly more time before another prover runs)
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@43550
    18
  val typ_of_dtyp :
blanchet@43550
    19
    Datatype_Aux.descr -> (Datatype_Aux.dtyp * typ) list -> Datatype_Aux.dtyp
blanchet@43550
    20
    -> typ
blanchet@43567
    21
  val varify_type : Proof.context -> typ -> typ
blanchet@43567
    22
  val instantiate_type : theory -> typ -> typ -> typ -> typ
blanchet@43567
    23
  val varify_and_instantiate_type : Proof.context -> typ -> typ -> typ -> typ
blanchet@43595
    24
  val is_type_surely_finite : Proof.context -> typ -> bool
blanchet@43755
    25
  val is_type_surely_infinite : Proof.context -> typ list -> typ -> bool
blanchet@36553
    26
  val monomorphic_term : Type.tyenv -> term -> term
blanchet@38890
    27
  val eta_expand : typ list -> term -> int -> term
blanchet@43785
    28
  val transform_elim_prop : term -> term
blanchet@36553
    29
  val specialize_type : theory -> (string * typ) -> term -> term
blanchet@38290
    30
  val subgoal_count : Proof.state -> int
blanchet@43845
    31
  val strip_subgoal :
blanchet@43845
    32
    Proof.context -> thm -> int -> (string * typ) list * term list * term
blanchet@38935
    33
  val reserved_isar_keyword_table : unit -> unit Symtab.table
blanchet@35961
    34
end;
blanchet@39564
    35
blanchet@35961
    36
structure Sledgehammer_Util : SLEDGEHAMMER_UTIL =
blanchet@35961
    37
struct
blanchet@35961
    38
blanchet@36140
    39
fun plural_s n = if n = 1 then "" else "s"
blanchet@36062
    40
blanchet@43870
    41
val serial_commas = Try.serial_commas
blanchet@43802
    42
val simplify_spaces = ATP_Proof.strip_spaces false (K true)
blanchet@38199
    43
blanchet@35961
    44
fun parse_bool_option option name s =
blanchet@35961
    45
  (case s of
blanchet@35961
    46
     "smart" => if option then NONE else raise Option
blanchet@35961
    47
   | "false" => SOME false
blanchet@35961
    48
   | "true" => SOME true
blanchet@35961
    49
   | "" => SOME true
blanchet@35961
    50
   | _ => raise Option)
blanchet@35961
    51
  handle Option.Option =>
blanchet@35961
    52
         let val ss = map quote ((option ? cons "smart") ["true", "false"]) in
blanchet@35961
    53
           error ("Parameter " ^ quote name ^ " must be assigned " ^
blanchet@35961
    54
                  space_implode " " (serial_commas "or" ss) ^ ".")
blanchet@35961
    55
         end
blanchet@35961
    56
blanchet@40582
    57
val has_junk =
wenzelm@40875
    58
  exists (fn s => not (Symbol.is_digit s) andalso s <> ".") o raw_explode (* FIXME Symbol.explode (?) *)
blanchet@40582
    59
blanchet@35961
    60
fun parse_time_option _ "none" = NONE
blanchet@35961
    61
  | parse_time_option name s =
blanchet@40582
    62
    let val secs = if has_junk s then NONE else Real.fromString s in
blanchet@40582
    63
      if is_none secs orelse Real.<= (the secs, 0.0) then
blanchet@40582
    64
        error ("Parameter " ^ quote name ^ " must be assigned a positive \
blanchet@40582
    65
               \number of seconds (e.g., \"60\", \"0.5\") or \"none\".")
blanchet@35961
    66
      else
blanchet@40582
    67
        SOME (seconds (the secs))
blanchet@35961
    68
    end
blanchet@35961
    69
blanchet@40582
    70
fun string_from_time t =
blanchet@40582
    71
  string_of_real (0.01 * Real.fromInt (Time.toMilliseconds t div 10)) ^ " s"
blanchet@40582
    72
wenzelm@40875
    73
val subscript = implode o map (prefix "\<^isub>") o raw_explode  (* FIXME Symbol.explode (?) *)
blanchet@37318
    74
fun nat_subscript n =
blanchet@37318
    75
  n |> string_of_int |> print_mode_active Symbol.xsymbolsN ? subscript
blanchet@36482
    76
wenzelm@39812
    77
val unyxml = XML.content_of o YXML.parse_body
blanchet@36474
    78
wenzelm@43162
    79
val is_long_identifier = forall Lexicon.is_identifier o space_explode "."
blanchet@36474
    80
fun maybe_quote y =
blanchet@36474
    81
  let val s = unyxml y in
blanchet@36474
    82
    y |> ((not (is_long_identifier (perhaps (try (unprefix "'")) s)) andalso
blanchet@36474
    83
           not (is_long_identifier (perhaps (try (unprefix "?")) s))) orelse
wenzelm@36970
    84
           Keyword.is_keyword s) ? quote
blanchet@36474
    85
  end
blanchet@36474
    86
blanchet@43550
    87
fun typ_of_dtyp _ typ_assoc (Datatype_Aux.DtTFree a) =
blanchet@43550
    88
    the (AList.lookup (op =) typ_assoc (Datatype_Aux.DtTFree a))
blanchet@43550
    89
  | typ_of_dtyp descr typ_assoc (Datatype_Aux.DtType (s, Us)) =
blanchet@43550
    90
    Type (s, map (typ_of_dtyp descr typ_assoc) Us)
blanchet@43550
    91
  | typ_of_dtyp descr typ_assoc (Datatype_Aux.DtRec i) =
blanchet@43550
    92
    let val (s, ds, _) = the (AList.lookup (op =) descr i) in
blanchet@43550
    93
      Type (s, map (typ_of_dtyp descr typ_assoc) ds)
blanchet@43550
    94
    end
blanchet@43550
    95
blanchet@43567
    96
fun varify_type ctxt T =
blanchet@43567
    97
  Variable.polymorphic_types ctxt [Const (@{const_name undefined}, T)]
blanchet@43567
    98
  |> snd |> the_single |> dest_Const |> snd
blanchet@43567
    99
blanchet@43567
   100
(* TODO: use "Term_Subst.instantiateT" instead? *)
blanchet@43567
   101
fun instantiate_type thy T1 T1' T2 =
blanchet@43567
   102
  Same.commit (Envir.subst_type_same
blanchet@43567
   103
                   (Sign.typ_match thy (T1, T1') Vartab.empty)) T2
blanchet@43567
   104
  handle Type.TYPE_MATCH => raise TYPE ("instantiate_type", [T1, T1'], [])
blanchet@43567
   105
blanchet@43567
   106
fun varify_and_instantiate_type ctxt T1 T1' T2 =
blanchet@43567
   107
  let val thy = Proof_Context.theory_of ctxt in
blanchet@43567
   108
    instantiate_type thy (varify_type ctxt T1) T1' (varify_type ctxt T2)
blanchet@43567
   109
  end
blanchet@43567
   110
blanchet@43595
   111
fun datatype_constrs thy (T as Type (s, Ts)) =
blanchet@43595
   112
    (case Datatype.get_info thy s of
blanchet@43595
   113
       SOME {index, descr, ...} =>
blanchet@43595
   114
       let val (_, dtyps, constrs) = AList.lookup (op =) descr index |> the in
blanchet@43595
   115
         map (apsnd (fn Us => map (typ_of_dtyp descr (dtyps ~~ Ts)) Us ---> T))
blanchet@43595
   116
             constrs
blanchet@43595
   117
       end
blanchet@43595
   118
     | NONE => [])
blanchet@43595
   119
  | datatype_constrs _ _ = []
blanchet@43595
   120
blanchet@43595
   121
(* Similar to "Nitpick_HOL.bounded_exact_card_of_type".
blanchet@43595
   122
   0 means infinite type, 1 means singleton type (e.g., "unit"), and 2 means
blanchet@43595
   123
   cardinality 2 or more. The specified default cardinality is returned if the
blanchet@43595
   124
   cardinality of the type can't be determined. *)
blanchet@43755
   125
fun tiny_card_of_type ctxt default_card assigns T =
blanchet@43595
   126
  let
blanchet@43755
   127
    val thy = Proof_Context.theory_of ctxt
blanchet@43595
   128
    val max = 2 (* 1 would be too small for the "fun" case *)
blanchet@43595
   129
    fun aux slack avoid T =
blanchet@43754
   130
      if member (op =) avoid T then
blanchet@43754
   131
        0
blanchet@43755
   132
      else case AList.lookup (Sign.typ_instance thy o swap) assigns T of
blanchet@43755
   133
        SOME k => k
blanchet@43755
   134
      | NONE =>
blanchet@43755
   135
        case T of
blanchet@43755
   136
          Type (@{type_name fun}, [T1, T2]) =>
blanchet@43755
   137
          (case (aux slack avoid T1, aux slack avoid T2) of
blanchet@43755
   138
             (k, 1) => if slack andalso k = 0 then 0 else 1
blanchet@43755
   139
           | (0, _) => 0
blanchet@43755
   140
           | (_, 0) => 0
blanchet@43755
   141
           | (k1, k2) =>
blanchet@43755
   142
             if k1 >= max orelse k2 >= max then max
blanchet@43755
   143
             else Int.min (max, Integer.pow k2 k1))
blanchet@43755
   144
        | @{typ prop} => 2
blanchet@43755
   145
        | @{typ bool} => 2 (* optimization *)
blanchet@43755
   146
        | @{typ nat} => 0 (* optimization *)
blanchet@43755
   147
        | @{typ int} => 0 (* optimization *)
blanchet@43755
   148
        | Type (s, _) =>
blanchet@43755
   149
          (case datatype_constrs thy T of
blanchet@43755
   150
             constrs as _ :: _ =>
blanchet@43755
   151
             let
blanchet@43755
   152
               val constr_cards =
blanchet@43755
   153
                 map (Integer.prod o map (aux slack (T :: avoid)) o binder_types
blanchet@43755
   154
                      o snd) constrs
blanchet@43755
   155
             in
blanchet@43755
   156
               if exists (curry (op =) 0) constr_cards then 0
blanchet@43755
   157
               else Int.min (max, Integer.sum constr_cards)
blanchet@43755
   158
             end
blanchet@43755
   159
           | [] =>
blanchet@43755
   160
             case Typedef.get_info ctxt s of
blanchet@43755
   161
               ({abs_type, rep_type, ...}, _) :: _ =>
blanchet@43755
   162
               (* We cheat here by assuming that typedef types are infinite if
blanchet@43755
   163
                  their underlying type is infinite. This is unsound in general
blanchet@43755
   164
                  but it's hard to think of a realistic example where this would
blanchet@43755
   165
                  not be the case. We are also slack with representation types:
blanchet@43755
   166
                  If a representation type has the form "sigma => tau", we
blanchet@43755
   167
                  consider it enough to check "sigma" for infiniteness. (Look
blanchet@43755
   168
                  for "slack" in this function.) *)
blanchet@43755
   169
               (case varify_and_instantiate_type ctxt
blanchet@43755
   170
                         (Logic.varifyT_global abs_type) T
blanchet@43755
   171
                         (Logic.varifyT_global rep_type)
blanchet@43755
   172
                     |> aux true avoid of
blanchet@43755
   173
                  0 => 0
blanchet@43755
   174
                | 1 => 1
blanchet@43755
   175
                | _ => default_card)
blanchet@43755
   176
             | [] => default_card)
blanchet@43755
   177
          (* Very slightly unsound: Type variables are assumed not to be
blanchet@43755
   178
             constrained to cardinality 1. (In practice, the user would most
blanchet@43755
   179
             likely have used "unit" directly anyway.) *)
blanchet@43755
   180
        | TFree _ => if default_card = 1 then 2 else default_card
blanchet@43755
   181
          (* Schematic type variables that contain only unproblematic sorts
blanchet@43755
   182
             (with no finiteness axiom) can safely be considered infinite. *)
blanchet@43755
   183
        | TVar _ => default_card
blanchet@43595
   184
  in Int.min (max, aux false [] T) end
blanchet@43595
   185
blanchet@43755
   186
fun is_type_surely_finite ctxt T = tiny_card_of_type ctxt 0 [] T <> 0
blanchet@43755
   187
fun is_type_surely_infinite ctxt infinite_Ts T =
blanchet@43755
   188
  tiny_card_of_type ctxt 1 (map (rpair 0) infinite_Ts) T = 0
blanchet@43595
   189
blanchet@36553
   190
fun monomorphic_term subst t =
blanchet@36553
   191
  map_types (map_type_tvar (fn v =>
blanchet@36553
   192
      case Type.lookup subst v of
blanchet@36553
   193
        SOME typ => typ
blanchet@36553
   194
      | NONE => raise TERM ("monomorphic_term: uninstanitated schematic type \
blanchet@36553
   195
                            \variable", [t]))) t
blanchet@36553
   196
blanchet@38890
   197
fun eta_expand _ t 0 = t
blanchet@38890
   198
  | eta_expand Ts (Abs (s, T, t')) n =
blanchet@38890
   199
    Abs (s, T, eta_expand (T :: Ts) t' (n - 1))
blanchet@38890
   200
  | eta_expand Ts t n =
blanchet@38890
   201
    fold_rev (fn T => fn t' => Abs ("x" ^ nat_subscript n, T, t'))
blanchet@38890
   202
             (List.take (binder_types (fastype_of1 (Ts, t)), n))
blanchet@38890
   203
             (list_comb (incr_boundvars n t, map Bound (n - 1 downto 0)))
blanchet@38890
   204
blanchet@38890
   205
(* Converts an elim-rule into an equivalent theorem that does not have the
blanchet@38890
   206
   predicate variable. Leaves other theorems unchanged. We simply instantiate
blanchet@38890
   207
   the conclusion variable to False. (Cf. "transform_elim_theorem" in
blanchet@40071
   208
   "Meson_Clausify".) *)
blanchet@43785
   209
fun transform_elim_prop t =
blanchet@38890
   210
  case Logic.strip_imp_concl t of
blanchet@38890
   211
    @{const Trueprop} $ Var (z, @{typ bool}) =>
blanchet@38890
   212
    subst_Vars [(z, @{const False})] t
blanchet@38890
   213
  | Var (z, @{typ prop}) => subst_Vars [(z, @{prop False})] t
blanchet@38890
   214
  | _ => t
blanchet@38890
   215
blanchet@36553
   216
fun specialize_type thy (s, T) t =
blanchet@36553
   217
  let
blanchet@36553
   218
    fun subst_for (Const (s', T')) =
blanchet@36553
   219
      if s = s' then
blanchet@36553
   220
        SOME (Sign.typ_match thy (T', T) Vartab.empty)
blanchet@36553
   221
        handle Type.TYPE_MATCH => NONE
blanchet@36553
   222
      else
blanchet@36553
   223
        NONE
blanchet@36553
   224
    | subst_for (t1 $ t2) =
blanchet@36553
   225
      (case subst_for t1 of SOME x => SOME x | NONE => subst_for t2)
blanchet@36553
   226
    | subst_for (Abs (_, _, t')) = subst_for t'
blanchet@36553
   227
    | subst_for _ = NONE
blanchet@36553
   228
  in
blanchet@36553
   229
    case subst_for t of
blanchet@36553
   230
      SOME subst => monomorphic_term subst t
blanchet@36553
   231
    | NONE => raise Type.TYPE_MATCH
blanchet@36553
   232
  end
blanchet@36553
   233
blanchet@43870
   234
val subgoal_count = Try.subgoal_count
blanchet@38290
   235
blanchet@43845
   236
fun strip_subgoal ctxt goal i =
blanchet@38229
   237
  let
blanchet@43845
   238
    val (t, (frees, params)) =
blanchet@43845
   239
      Logic.goal_params (prop_of goal) i
blanchet@43845
   240
      ||> (map dest_Free #> Variable.variant_frees ctxt [] #> `(map Free))
blanchet@38229
   241
    val hyp_ts = t |> Logic.strip_assums_hyp |> map (curry subst_bounds frees)
blanchet@38229
   242
    val concl_t = t |> Logic.strip_assums_concl |> curry subst_bounds frees
blanchet@43845
   243
  in (rev params, hyp_ts, concl_t) end
blanchet@36553
   244
blanchet@38935
   245
fun reserved_isar_keyword_table () =
blanchet@38935
   246
  union (op =) (Keyword.dest_keywords ()) (Keyword.dest_commands ())
blanchet@38935
   247
  |> map (rpair ()) |> Symtab.make
blanchet@38935
   248
blanchet@35961
   249
end;