src/HOL/Tools/Sledgehammer/sledgehammer_util.ML
author blanchet
Fri, 20 May 2011 12:47:59 +0200
changeset 42886 208ec29cc013
parent 42885 91adf04946d1
child 42944 9e620869a576
permissions -rw-r--r--
improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36062
194cb6e3c13f get rid of Polyhash, since it's no longer used
blanchet
parents: 35963
diff changeset
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_util.ML
35963
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
     2
    Author:     Jasmin Blanchette, TU Muenchen
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
     3
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
     4
General-purpose functions used by the Sledgehammer modules.
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
     5
*)
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
     6
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
     7
signature SLEDGEHAMMER_UTIL =
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
     8
sig
36142
f5e15e9aae10 make Sledgehammer "minimize" output less confusing + round up (not down) time limits to nearest second
blanchet
parents: 36062
diff changeset
     9
  val plural_s : int -> string
35963
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    10
  val serial_commas : string -> string list -> string list
38738
0ce517c1970f make sure that "undo_ascii_of" is the inverse of "ascii_of", also for non-printable characters -- and avoid those in ``-style facts
blanchet
parents: 38698
diff changeset
    11
  val simplify_spaces : string -> string
35963
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    12
  val parse_bool_option : bool -> string -> string -> bool option
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    13
  val parse_time_option : string -> string -> Time.time option
40341
03156257040f standardize on seconds for Nitpick and Sledgehammer timeouts
blanchet
parents: 39890
diff changeset
    14
  val string_from_time : Time.time -> string
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36478
diff changeset
    15
  val nat_subscript : int -> string
36478
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36402
diff changeset
    16
  val unyxml : string -> string
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36402
diff changeset
    17
  val maybe_quote : string -> string
42680
b6c27cf04fe9 exploit inferred monotonicity
blanchet
parents: 42290
diff changeset
    18
  val typ_of_dtyp :
b6c27cf04fe9 exploit inferred monotonicity
blanchet
parents: 42290
diff changeset
    19
    Datatype_Aux.descr -> (Datatype_Aux.dtyp * typ) list -> Datatype_Aux.dtyp
b6c27cf04fe9 exploit inferred monotonicity
blanchet
parents: 42290
diff changeset
    20
    -> typ
42697
9bc5dc48f1a5 query typedefs as well for monotonicity
blanchet
parents: 42680
diff changeset
    21
  val varify_type : Proof.context -> typ -> typ
9bc5dc48f1a5 query typedefs as well for monotonicity
blanchet
parents: 42680
diff changeset
    22
  val instantiate_type : theory -> typ -> typ -> typ -> typ
9bc5dc48f1a5 query typedefs as well for monotonicity
blanchet
parents: 42680
diff changeset
    23
  val varify_and_instantiate_type : Proof.context -> typ -> typ -> typ -> typ
42730
d6db5a815477 improve detection of quantifications over dangerous types by leveraging "is_type_surely_finite" predicate and added "prop" to the list of surely finite types
blanchet
parents: 42697
diff changeset
    24
  val is_type_surely_finite : Proof.context -> typ -> bool
42886
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
    25
  val is_type_surely_infinite : Proof.context -> typ list -> typ -> bool
36555
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
    26
  val monomorphic_term : Type.tyenv -> term -> term
38652
e063be321438 perform eta-expansion of quantifier bodies in Sledgehammer translation when needed + transform elim rules later;
blanchet
parents: 38608
diff changeset
    27
  val eta_expand : typ list -> term -> int -> term
e063be321438 perform eta-expansion of quantifier bodies in Sledgehammer translation when needed + transform elim rules later;
blanchet
parents: 38608
diff changeset
    28
  val transform_elim_term : term -> term
36555
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
    29
  val specialize_type : theory -> (string * typ) -> term -> term
38044
463177795c49 minor refactoring
blanchet
parents: 38019
diff changeset
    30
  val subgoal_count : Proof.state -> int
37995
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37962
diff changeset
    31
  val strip_subgoal : thm -> int -> (string * typ) list * term list * term
38696
4c6b65d6a135 quote facts whose names collide with a keyword or command name (cf. "subclass" in "Jinja/J/TypeSafe.thy")
blanchet
parents: 38652
diff changeset
    32
  val reserved_isar_keyword_table : unit -> unit Symtab.table
35963
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    33
end;
39318
ad9a1f9b0558 implemented Auto Sledgehammer
blanchet
parents: 38818
diff changeset
    34
35963
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    35
structure Sledgehammer_Util : SLEDGEHAMMER_UTIL =
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    36
struct
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    37
36142
f5e15e9aae10 make Sledgehammer "minimize" output less confusing + round up (not down) time limits to nearest second
blanchet
parents: 36062
diff changeset
    38
fun plural_s n = if n = 1 then "" else "s"
36062
194cb6e3c13f get rid of Polyhash, since it's no longer used
blanchet
parents: 35963
diff changeset
    39
35963
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    40
fun serial_commas _ [] = ["??"]
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    41
  | serial_commas _ [s] = [s]
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    42
  | serial_commas conj [s1, s2] = [s1, conj, s2]
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    43
  | serial_commas conj [s1, s2, s3] = [s1 ^ ",", s2 ^ ",", conj, s3]
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    44
  | serial_commas conj (s :: ss) = s ^ "," :: serial_commas conj ss
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    45
39457
b505208f435d avoid code duplication
blanchet
parents: 39357
diff changeset
    46
val simplify_spaces = ATP_Proof.strip_spaces (K true)
37962
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37575
diff changeset
    47
35963
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    48
fun parse_bool_option option name s =
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    49
  (case s of
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    50
     "smart" => if option then NONE else raise Option
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    51
   | "false" => SOME false
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    52
   | "true" => SOME true
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    53
   | "" => SOME true
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    54
   | _ => raise Option)
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    55
  handle Option.Option =>
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    56
         let val ss = map quote ((option ? cons "smart") ["true", "false"]) in
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    57
           error ("Parameter " ^ quote name ^ " must be assigned " ^
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    58
                  space_implode " " (serial_commas "or" ss) ^ ".")
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    59
         end
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    60
40341
03156257040f standardize on seconds for Nitpick and Sledgehammer timeouts
blanchet
parents: 39890
diff changeset
    61
val has_junk =
40627
becf5d5187cc renamed raw "explode" function to "raw_explode" to emphasize its meaning;
wenzelm
parents: 40341
diff changeset
    62
  exists (fn s => not (Symbol.is_digit s) andalso s <> ".") o raw_explode (* FIXME Symbol.explode (?) *)
40341
03156257040f standardize on seconds for Nitpick and Sledgehammer timeouts
blanchet
parents: 39890
diff changeset
    63
35963
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    64
fun parse_time_option _ "none" = NONE
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    65
  | parse_time_option name s =
40341
03156257040f standardize on seconds for Nitpick and Sledgehammer timeouts
blanchet
parents: 39890
diff changeset
    66
    let val secs = if has_junk s then NONE else Real.fromString s in
03156257040f standardize on seconds for Nitpick and Sledgehammer timeouts
blanchet
parents: 39890
diff changeset
    67
      if is_none secs orelse Real.<= (the secs, 0.0) then
03156257040f standardize on seconds for Nitpick and Sledgehammer timeouts
blanchet
parents: 39890
diff changeset
    68
        error ("Parameter " ^ quote name ^ " must be assigned a positive \
03156257040f standardize on seconds for Nitpick and Sledgehammer timeouts
blanchet
parents: 39890
diff changeset
    69
               \number of seconds (e.g., \"60\", \"0.5\") or \"none\".")
35963
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    70
      else
40341
03156257040f standardize on seconds for Nitpick and Sledgehammer timeouts
blanchet
parents: 39890
diff changeset
    71
        SOME (seconds (the secs))
35963
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    72
    end
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    73
40341
03156257040f standardize on seconds for Nitpick and Sledgehammer timeouts
blanchet
parents: 39890
diff changeset
    74
fun string_from_time t =
03156257040f standardize on seconds for Nitpick and Sledgehammer timeouts
blanchet
parents: 39890
diff changeset
    75
  string_of_real (0.01 * Real.fromInt (Time.toMilliseconds t div 10)) ^ " s"
03156257040f standardize on seconds for Nitpick and Sledgehammer timeouts
blanchet
parents: 39890
diff changeset
    76
40627
becf5d5187cc renamed raw "explode" function to "raw_explode" to emphasize its meaning;
wenzelm
parents: 40341
diff changeset
    77
val subscript = implode o map (prefix "\<^isub>") o raw_explode  (* FIXME Symbol.explode (?) *)
37321
9d7cfae95b30 honor "xsymbols"
blanchet
parents: 36960
diff changeset
    78
fun nat_subscript n =
9d7cfae95b30 honor "xsymbols"
blanchet
parents: 36960
diff changeset
    79
  n |> string_of_int |> print_mode_active Symbol.xsymbolsN ? subscript
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36478
diff changeset
    80
39555
ccb223a4d49c added XML.content_of convenience -- cover XML.body, which is the general situation;
wenzelm
parents: 39500
diff changeset
    81
val unyxml = XML.content_of o YXML.parse_body
36478
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36402
diff changeset
    82
42290
b1f544c84040 discontinued special treatment of structure Lexicon;
wenzelm
parents: 41211
diff changeset
    83
val is_long_identifier = forall Lexicon.is_identifier o space_explode "."
36478
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36402
diff changeset
    84
fun maybe_quote y =
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36402
diff changeset
    85
  let val s = unyxml y in
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36402
diff changeset
    86
    y |> ((not (is_long_identifier (perhaps (try (unprefix "'")) s)) andalso
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36402
diff changeset
    87
           not (is_long_identifier (perhaps (try (unprefix "?")) s))) orelse
36960
01594f816e3a prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
wenzelm
parents: 36555
diff changeset
    88
           Keyword.is_keyword s) ? quote
36478
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36402
diff changeset
    89
  end
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36402
diff changeset
    90
42680
b6c27cf04fe9 exploit inferred monotonicity
blanchet
parents: 42290
diff changeset
    91
fun typ_of_dtyp _ typ_assoc (Datatype_Aux.DtTFree a) =
b6c27cf04fe9 exploit inferred monotonicity
blanchet
parents: 42290
diff changeset
    92
    the (AList.lookup (op =) typ_assoc (Datatype_Aux.DtTFree a))
b6c27cf04fe9 exploit inferred monotonicity
blanchet
parents: 42290
diff changeset
    93
  | typ_of_dtyp descr typ_assoc (Datatype_Aux.DtType (s, Us)) =
b6c27cf04fe9 exploit inferred monotonicity
blanchet
parents: 42290
diff changeset
    94
    Type (s, map (typ_of_dtyp descr typ_assoc) Us)
b6c27cf04fe9 exploit inferred monotonicity
blanchet
parents: 42290
diff changeset
    95
  | typ_of_dtyp descr typ_assoc (Datatype_Aux.DtRec i) =
b6c27cf04fe9 exploit inferred monotonicity
blanchet
parents: 42290
diff changeset
    96
    let val (s, ds, _) = the (AList.lookup (op =) descr i) in
b6c27cf04fe9 exploit inferred monotonicity
blanchet
parents: 42290
diff changeset
    97
      Type (s, map (typ_of_dtyp descr typ_assoc) ds)
b6c27cf04fe9 exploit inferred monotonicity
blanchet
parents: 42290
diff changeset
    98
    end
b6c27cf04fe9 exploit inferred monotonicity
blanchet
parents: 42290
diff changeset
    99
42697
9bc5dc48f1a5 query typedefs as well for monotonicity
blanchet
parents: 42680
diff changeset
   100
fun varify_type ctxt T =
9bc5dc48f1a5 query typedefs as well for monotonicity
blanchet
parents: 42680
diff changeset
   101
  Variable.polymorphic_types ctxt [Const (@{const_name undefined}, T)]
9bc5dc48f1a5 query typedefs as well for monotonicity
blanchet
parents: 42680
diff changeset
   102
  |> snd |> the_single |> dest_Const |> snd
9bc5dc48f1a5 query typedefs as well for monotonicity
blanchet
parents: 42680
diff changeset
   103
9bc5dc48f1a5 query typedefs as well for monotonicity
blanchet
parents: 42680
diff changeset
   104
(* TODO: use "Term_Subst.instantiateT" instead? *)
9bc5dc48f1a5 query typedefs as well for monotonicity
blanchet
parents: 42680
diff changeset
   105
fun instantiate_type thy T1 T1' T2 =
9bc5dc48f1a5 query typedefs as well for monotonicity
blanchet
parents: 42680
diff changeset
   106
  Same.commit (Envir.subst_type_same
9bc5dc48f1a5 query typedefs as well for monotonicity
blanchet
parents: 42680
diff changeset
   107
                   (Sign.typ_match thy (T1, T1') Vartab.empty)) T2
9bc5dc48f1a5 query typedefs as well for monotonicity
blanchet
parents: 42680
diff changeset
   108
  handle Type.TYPE_MATCH => raise TYPE ("instantiate_type", [T1, T1'], [])
9bc5dc48f1a5 query typedefs as well for monotonicity
blanchet
parents: 42680
diff changeset
   109
9bc5dc48f1a5 query typedefs as well for monotonicity
blanchet
parents: 42680
diff changeset
   110
fun varify_and_instantiate_type ctxt T1 T1' T2 =
9bc5dc48f1a5 query typedefs as well for monotonicity
blanchet
parents: 42680
diff changeset
   111
  let val thy = Proof_Context.theory_of ctxt in
9bc5dc48f1a5 query typedefs as well for monotonicity
blanchet
parents: 42680
diff changeset
   112
    instantiate_type thy (varify_type ctxt T1) T1' (varify_type ctxt T2)
9bc5dc48f1a5 query typedefs as well for monotonicity
blanchet
parents: 42680
diff changeset
   113
  end
9bc5dc48f1a5 query typedefs as well for monotonicity
blanchet
parents: 42680
diff changeset
   114
42730
d6db5a815477 improve detection of quantifications over dangerous types by leveraging "is_type_surely_finite" predicate and added "prop" to the list of surely finite types
blanchet
parents: 42697
diff changeset
   115
fun datatype_constrs thy (T as Type (s, Ts)) =
d6db5a815477 improve detection of quantifications over dangerous types by leveraging "is_type_surely_finite" predicate and added "prop" to the list of surely finite types
blanchet
parents: 42697
diff changeset
   116
    (case Datatype.get_info thy s of
d6db5a815477 improve detection of quantifications over dangerous types by leveraging "is_type_surely_finite" predicate and added "prop" to the list of surely finite types
blanchet
parents: 42697
diff changeset
   117
       SOME {index, descr, ...} =>
d6db5a815477 improve detection of quantifications over dangerous types by leveraging "is_type_surely_finite" predicate and added "prop" to the list of surely finite types
blanchet
parents: 42697
diff changeset
   118
       let val (_, dtyps, constrs) = AList.lookup (op =) descr index |> the in
d6db5a815477 improve detection of quantifications over dangerous types by leveraging "is_type_surely_finite" predicate and added "prop" to the list of surely finite types
blanchet
parents: 42697
diff changeset
   119
         map (apsnd (fn Us => map (typ_of_dtyp descr (dtyps ~~ Ts)) Us ---> T))
d6db5a815477 improve detection of quantifications over dangerous types by leveraging "is_type_surely_finite" predicate and added "prop" to the list of surely finite types
blanchet
parents: 42697
diff changeset
   120
             constrs
d6db5a815477 improve detection of quantifications over dangerous types by leveraging "is_type_surely_finite" predicate and added "prop" to the list of surely finite types
blanchet
parents: 42697
diff changeset
   121
       end
d6db5a815477 improve detection of quantifications over dangerous types by leveraging "is_type_surely_finite" predicate and added "prop" to the list of surely finite types
blanchet
parents: 42697
diff changeset
   122
     | NONE => [])
d6db5a815477 improve detection of quantifications over dangerous types by leveraging "is_type_surely_finite" predicate and added "prop" to the list of surely finite types
blanchet
parents: 42697
diff changeset
   123
  | datatype_constrs _ _ = []
d6db5a815477 improve detection of quantifications over dangerous types by leveraging "is_type_surely_finite" predicate and added "prop" to the list of surely finite types
blanchet
parents: 42697
diff changeset
   124
d6db5a815477 improve detection of quantifications over dangerous types by leveraging "is_type_surely_finite" predicate and added "prop" to the list of surely finite types
blanchet
parents: 42697
diff changeset
   125
(* Similar to "Nitpick_HOL.bounded_exact_card_of_type".
d6db5a815477 improve detection of quantifications over dangerous types by leveraging "is_type_surely_finite" predicate and added "prop" to the list of surely finite types
blanchet
parents: 42697
diff changeset
   126
   0 means infinite type, 1 means singleton type (e.g., "unit"), and 2 means
d6db5a815477 improve detection of quantifications over dangerous types by leveraging "is_type_surely_finite" predicate and added "prop" to the list of surely finite types
blanchet
parents: 42697
diff changeset
   127
   cardinality 2 or more. The specified default cardinality is returned if the
d6db5a815477 improve detection of quantifications over dangerous types by leveraging "is_type_surely_finite" predicate and added "prop" to the list of surely finite types
blanchet
parents: 42697
diff changeset
   128
   cardinality of the type can't be determined. *)
42886
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   129
fun tiny_card_of_type ctxt default_card assigns T =
42730
d6db5a815477 improve detection of quantifications over dangerous types by leveraging "is_type_surely_finite" predicate and added "prop" to the list of surely finite types
blanchet
parents: 42697
diff changeset
   130
  let
42886
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   131
    val thy = Proof_Context.theory_of ctxt
42730
d6db5a815477 improve detection of quantifications over dangerous types by leveraging "is_type_surely_finite" predicate and added "prop" to the list of surely finite types
blanchet
parents: 42697
diff changeset
   132
    val max = 2 (* 1 would be too small for the "fun" case *)
d6db5a815477 improve detection of quantifications over dangerous types by leveraging "is_type_surely_finite" predicate and added "prop" to the list of surely finite types
blanchet
parents: 42697
diff changeset
   133
    fun aux slack avoid T =
42885
91adf04946d1 reintroduced type encodings "poly_preds_{bang,query}", but this time being more liberal about type variables of known safe sorts
blanchet
parents: 42730
diff changeset
   134
      if member (op =) avoid T then
91adf04946d1 reintroduced type encodings "poly_preds_{bang,query}", but this time being more liberal about type variables of known safe sorts
blanchet
parents: 42730
diff changeset
   135
        0
42886
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   136
      else case AList.lookup (Sign.typ_instance thy o swap) assigns T of
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   137
        SOME k => k
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   138
      | NONE =>
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   139
        case T of
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   140
          Type (@{type_name fun}, [T1, T2]) =>
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   141
          (case (aux slack avoid T1, aux slack avoid T2) of
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   142
             (k, 1) => if slack andalso k = 0 then 0 else 1
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   143
           | (0, _) => 0
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   144
           | (_, 0) => 0
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   145
           | (k1, k2) =>
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   146
             if k1 >= max orelse k2 >= max then max
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   147
             else Int.min (max, Integer.pow k2 k1))
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   148
        | @{typ prop} => 2
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   149
        | @{typ bool} => 2 (* optimization *)
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   150
        | @{typ nat} => 0 (* optimization *)
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   151
        | @{typ int} => 0 (* optimization *)
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   152
        | Type (s, _) =>
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   153
          (case datatype_constrs thy T of
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   154
             constrs as _ :: _ =>
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   155
             let
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   156
               val constr_cards =
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   157
                 map (Integer.prod o map (aux slack (T :: avoid)) o binder_types
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   158
                      o snd) constrs
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   159
             in
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   160
               if exists (curry (op =) 0) constr_cards then 0
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   161
               else Int.min (max, Integer.sum constr_cards)
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   162
             end
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   163
           | [] =>
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   164
             case Typedef.get_info ctxt s of
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   165
               ({abs_type, rep_type, ...}, _) :: _ =>
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   166
               (* We cheat here by assuming that typedef types are infinite if
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   167
                  their underlying type is infinite. This is unsound in general
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   168
                  but it's hard to think of a realistic example where this would
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   169
                  not be the case. We are also slack with representation types:
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   170
                  If a representation type has the form "sigma => tau", we
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   171
                  consider it enough to check "sigma" for infiniteness. (Look
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   172
                  for "slack" in this function.) *)
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   173
               (case varify_and_instantiate_type ctxt
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   174
                         (Logic.varifyT_global abs_type) T
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   175
                         (Logic.varifyT_global rep_type)
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   176
                     |> aux true avoid of
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   177
                  0 => 0
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   178
                | 1 => 1
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   179
                | _ => default_card)
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   180
             | [] => default_card)
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   181
          (* Very slightly unsound: Type variables are assumed not to be
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   182
             constrained to cardinality 1. (In practice, the user would most
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   183
             likely have used "unit" directly anyway.) *)
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   184
        | TFree _ => if default_card = 1 then 2 else default_card
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   185
          (* Schematic type variables that contain only unproblematic sorts
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   186
             (with no finiteness axiom) can safely be considered infinite. *)
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   187
        | TVar _ => default_card
42730
d6db5a815477 improve detection of quantifications over dangerous types by leveraging "is_type_surely_finite" predicate and added "prop" to the list of surely finite types
blanchet
parents: 42697
diff changeset
   188
  in Int.min (max, aux false [] T) end
d6db5a815477 improve detection of quantifications over dangerous types by leveraging "is_type_surely_finite" predicate and added "prop" to the list of surely finite types
blanchet
parents: 42697
diff changeset
   189
42886
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   190
fun is_type_surely_finite ctxt T = tiny_card_of_type ctxt 0 [] T <> 0
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   191
fun is_type_surely_infinite ctxt infinite_Ts T =
208ec29cc013 improved "poly_preds_{bang,query}" by picking up good witnesses for the possible infinity of common type classes and ensuring that "?'a::type" doesn't ruin everything
blanchet
parents: 42885
diff changeset
   192
  tiny_card_of_type ctxt 1 (map (rpair 0) infinite_Ts) T = 0
42730
d6db5a815477 improve detection of quantifications over dangerous types by leveraging "is_type_surely_finite" predicate and added "prop" to the list of surely finite types
blanchet
parents: 42697
diff changeset
   193
36555
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   194
fun monomorphic_term subst t =
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   195
  map_types (map_type_tvar (fn v =>
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   196
      case Type.lookup subst v of
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   197
        SOME typ => typ
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   198
      | NONE => raise TERM ("monomorphic_term: uninstanitated schematic type \
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   199
                            \variable", [t]))) t
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   200
38652
e063be321438 perform eta-expansion of quantifier bodies in Sledgehammer translation when needed + transform elim rules later;
blanchet
parents: 38608
diff changeset
   201
fun eta_expand _ t 0 = t
e063be321438 perform eta-expansion of quantifier bodies in Sledgehammer translation when needed + transform elim rules later;
blanchet
parents: 38608
diff changeset
   202
  | eta_expand Ts (Abs (s, T, t')) n =
e063be321438 perform eta-expansion of quantifier bodies in Sledgehammer translation when needed + transform elim rules later;
blanchet
parents: 38608
diff changeset
   203
    Abs (s, T, eta_expand (T :: Ts) t' (n - 1))
e063be321438 perform eta-expansion of quantifier bodies in Sledgehammer translation when needed + transform elim rules later;
blanchet
parents: 38608
diff changeset
   204
  | eta_expand Ts t n =
e063be321438 perform eta-expansion of quantifier bodies in Sledgehammer translation when needed + transform elim rules later;
blanchet
parents: 38608
diff changeset
   205
    fold_rev (fn T => fn t' => Abs ("x" ^ nat_subscript n, T, t'))
e063be321438 perform eta-expansion of quantifier bodies in Sledgehammer translation when needed + transform elim rules later;
blanchet
parents: 38608
diff changeset
   206
             (List.take (binder_types (fastype_of1 (Ts, t)), n))
e063be321438 perform eta-expansion of quantifier bodies in Sledgehammer translation when needed + transform elim rules later;
blanchet
parents: 38608
diff changeset
   207
             (list_comb (incr_boundvars n t, map Bound (n - 1 downto 0)))
e063be321438 perform eta-expansion of quantifier bodies in Sledgehammer translation when needed + transform elim rules later;
blanchet
parents: 38608
diff changeset
   208
e063be321438 perform eta-expansion of quantifier bodies in Sledgehammer translation when needed + transform elim rules later;
blanchet
parents: 38608
diff changeset
   209
(* Converts an elim-rule into an equivalent theorem that does not have the
e063be321438 perform eta-expansion of quantifier bodies in Sledgehammer translation when needed + transform elim rules later;
blanchet
parents: 38608
diff changeset
   210
   predicate variable. Leaves other theorems unchanged. We simply instantiate
e063be321438 perform eta-expansion of quantifier bodies in Sledgehammer translation when needed + transform elim rules later;
blanchet
parents: 38608
diff changeset
   211
   the conclusion variable to False. (Cf. "transform_elim_theorem" in
39890
a1695e2169d0 finished renaming file and module
blanchet
parents: 39720
diff changeset
   212
   "Meson_Clausify".) *)
38652
e063be321438 perform eta-expansion of quantifier bodies in Sledgehammer translation when needed + transform elim rules later;
blanchet
parents: 38608
diff changeset
   213
fun transform_elim_term t =
e063be321438 perform eta-expansion of quantifier bodies in Sledgehammer translation when needed + transform elim rules later;
blanchet
parents: 38608
diff changeset
   214
  case Logic.strip_imp_concl t of
e063be321438 perform eta-expansion of quantifier bodies in Sledgehammer translation when needed + transform elim rules later;
blanchet
parents: 38608
diff changeset
   215
    @{const Trueprop} $ Var (z, @{typ bool}) =>
e063be321438 perform eta-expansion of quantifier bodies in Sledgehammer translation when needed + transform elim rules later;
blanchet
parents: 38608
diff changeset
   216
    subst_Vars [(z, @{const False})] t
e063be321438 perform eta-expansion of quantifier bodies in Sledgehammer translation when needed + transform elim rules later;
blanchet
parents: 38608
diff changeset
   217
  | Var (z, @{typ prop}) => subst_Vars [(z, @{prop False})] t
e063be321438 perform eta-expansion of quantifier bodies in Sledgehammer translation when needed + transform elim rules later;
blanchet
parents: 38608
diff changeset
   218
  | _ => t
e063be321438 perform eta-expansion of quantifier bodies in Sledgehammer translation when needed + transform elim rules later;
blanchet
parents: 38608
diff changeset
   219
36555
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   220
fun specialize_type thy (s, T) t =
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   221
  let
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   222
    fun subst_for (Const (s', T')) =
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   223
      if s = s' then
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   224
        SOME (Sign.typ_match thy (T', T) Vartab.empty)
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   225
        handle Type.TYPE_MATCH => NONE
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   226
      else
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   227
        NONE
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   228
    | subst_for (t1 $ t2) =
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   229
      (case subst_for t1 of SOME x => SOME x | NONE => subst_for t2)
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   230
    | subst_for (Abs (_, _, t')) = subst_for t'
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   231
    | subst_for _ = NONE
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   232
  in
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   233
    case subst_for t of
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   234
      SOME subst => monomorphic_term subst t
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   235
    | NONE => raise Type.TYPE_MATCH
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   236
  end
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   237
38044
463177795c49 minor refactoring
blanchet
parents: 38019
diff changeset
   238
val subgoal_count = Logic.count_prems o prop_of o #goal o Proof.goal
463177795c49 minor refactoring
blanchet
parents: 38019
diff changeset
   239
37995
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37962
diff changeset
   240
fun strip_subgoal goal i =
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37962
diff changeset
   241
  let
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37962
diff changeset
   242
    val (t, frees) = Logic.goal_params (prop_of goal) i
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37962
diff changeset
   243
    val hyp_ts = t |> Logic.strip_assums_hyp |> map (curry subst_bounds frees)
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37962
diff changeset
   244
    val concl_t = t |> Logic.strip_assums_concl |> curry subst_bounds frees
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37962
diff changeset
   245
  in (rev (map dest_Free frees), hyp_ts, concl_t) end
36555
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   246
38696
4c6b65d6a135 quote facts whose names collide with a keyword or command name (cf. "subclass" in "Jinja/J/TypeSafe.thy")
blanchet
parents: 38652
diff changeset
   247
fun reserved_isar_keyword_table () =
4c6b65d6a135 quote facts whose names collide with a keyword or command name (cf. "subclass" in "Jinja/J/TypeSafe.thy")
blanchet
parents: 38652
diff changeset
   248
  union (op =) (Keyword.dest_keywords ()) (Keyword.dest_commands ())
4c6b65d6a135 quote facts whose names collide with a keyword or command name (cf. "subclass" in "Jinja/J/TypeSafe.thy")
blanchet
parents: 38652
diff changeset
   249
  |> map (rpair ()) |> Symtab.make
4c6b65d6a135 quote facts whose names collide with a keyword or command name (cf. "subclass" in "Jinja/J/TypeSafe.thy")
blanchet
parents: 38652
diff changeset
   250
35963
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
   251
end;