src/HOL/Tools/ATP/atp_util.ML
author blanchet
Wed, 11 Jul 2012 21:43:19 +0200
changeset 48251 6cdcfbddc077
parent 48247 8f37d2ddabc8
child 48316 252f45c04042
permissions -rw-r--r--
moved most of MaSh exporter code to Sledgehammer
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43085
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
     1
(*  Title:      HOL/Tools/ATP/atp_util.ML
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
     2
    Author:     Jasmin Blanchette, TU Muenchen
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
     3
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
     4
General-purpose functions used by the ATP module.
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
     5
*)
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
     6
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
     7
signature ATP_UTIL =
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
     8
sig
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
     9
  val timestamp : unit -> string
43827
62d64709af3b added option to control which lambda translation to use (for experiments)
blanchet
parents: 43572
diff changeset
    10
  val hash_string : string -> int
62d64709af3b added option to control which lambda translation to use (for experiments)
blanchet
parents: 43572
diff changeset
    11
  val hash_term : term -> int
48251
6cdcfbddc077 moved most of MaSh exporter code to Sledgehammer
blanchet
parents: 48247
diff changeset
    12
  val stringN_of_int : int -> int -> string
43085
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
    13
  val strip_spaces : bool -> (char -> bool) -> string -> string
44784
blanchet
parents: 44500
diff changeset
    14
  val strip_spaces_except_between_idents : string -> string
43085
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
    15
  val nat_subscript : int -> string
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
    16
  val unyxml : string -> string
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
    17
  val maybe_quote : string -> string
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
    18
  val string_from_ext_time : bool * Time.time -> string
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
    19
  val string_from_time : Time.time -> string
47150
6df6e56fd7cd tuning (in particular, Symtab instead of AList)
blanchet
parents: 46711
diff changeset
    20
  val type_instance : theory -> typ -> typ -> bool
6df6e56fd7cd tuning (in particular, Symtab instead of AList)
blanchet
parents: 46711
diff changeset
    21
  val type_generalization : theory -> typ -> typ -> bool
6df6e56fd7cd tuning (in particular, Symtab instead of AList)
blanchet
parents: 46711
diff changeset
    22
  val type_intersect : theory -> typ -> typ -> bool
6df6e56fd7cd tuning (in particular, Symtab instead of AList)
blanchet
parents: 46711
diff changeset
    23
  val type_equiv : theory -> typ * typ -> bool
43085
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
    24
  val varify_type : Proof.context -> typ -> typ
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
    25
  val instantiate_type : theory -> typ -> typ -> typ -> typ
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
    26
  val varify_and_instantiate_type : Proof.context -> typ -> typ -> typ -> typ
45896
100fb1f33e3e tuned signature;
wenzelm
parents: 45570
diff changeset
    27
  val typ_of_dtyp : Datatype.descr -> (Datatype.dtyp * typ) list -> Datatype.dtyp -> typ
44393
23adec5984f1 make sound mode more sound (and clean up code)
blanchet
parents: 44392
diff changeset
    28
  val is_type_surely_finite : Proof.context -> typ -> bool
44399
cd1e32b8d4c4 added caching for (in)finiteness checks
blanchet
parents: 44393
diff changeset
    29
  val is_type_surely_infinite : Proof.context -> bool -> typ list -> typ -> bool
43863
a43d61270142 ensure that the lambda translation procedure is called only once with all the facts, which is necessary for soundness of lambda-lifting (freshness of new names)
blanchet
parents: 43827
diff changeset
    30
  val s_not : term -> term
a43d61270142 ensure that the lambda translation procedure is called only once with all the facts, which is necessary for soundness of lambda-lifting (freshness of new names)
blanchet
parents: 43827
diff changeset
    31
  val s_conj : term * term -> term
a43d61270142 ensure that the lambda translation procedure is called only once with all the facts, which is necessary for soundness of lambda-lifting (freshness of new names)
blanchet
parents: 43827
diff changeset
    32
  val s_disj : term * term -> term
a43d61270142 ensure that the lambda translation procedure is called only once with all the facts, which is necessary for soundness of lambda-lifting (freshness of new names)
blanchet
parents: 43827
diff changeset
    33
  val s_imp : term * term -> term
a43d61270142 ensure that the lambda translation procedure is called only once with all the facts, which is necessary for soundness of lambda-lifting (freshness of new names)
blanchet
parents: 43827
diff changeset
    34
  val s_iff : term * term -> term
46385
0ccf458a3633 third attempt at lambda lifting that works for both Sledgehammer and Metis (cf. dce6c3a460a9)
blanchet
parents: 45896
diff changeset
    35
  val close_form_prefix : string
43864
58a7b3fdc193 fixed lambda-liftg: must ensure the formulas are in close form
blanchet
parents: 43863
diff changeset
    36
  val close_form : term -> term
47718
39229c760636 smoother handling of conjecture, so that its Skolem constants get displayed in countermodels
blanchet
parents: 47715
diff changeset
    37
  val open_form : (string -> string) -> term -> term
43085
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
    38
  val monomorphic_term : Type.tyenv -> term -> term
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
    39
  val eta_expand : typ list -> term -> int -> term
47954
aada9fd08b58 make higher-order goals more first-order via extensionality
blanchet
parents: 47953
diff changeset
    40
  val cong_extensionalize_term : theory -> term -> term
47953
a2c3706c4cb1 added "ext_cong_neq" lemma (not used yet); tuning
blanchet
parents: 47718
diff changeset
    41
  val abs_extensionalize_term : Proof.context -> term -> term
47991
3eb598b044ad make Nitpick's handling of definitions more robust in the face of formulas that don't have the expected format (needed for soundness, cf. RNG100+1)
blanchet
parents: 47954
diff changeset
    42
  val unextensionalize_def : term -> term
3eb598b044ad make Nitpick's handling of definitions more robust in the face of formulas that don't have the expected format (needed for soundness, cf. RNG100+1)
blanchet
parents: 47954
diff changeset
    43
  val is_legitimate_tptp_def : term -> bool
43085
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
    44
  val transform_elim_prop : term -> term
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
    45
  val specialize_type : theory -> (string * typ) -> term -> term
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
    46
  val strip_subgoal :
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
    47
    Proof.context -> thm -> int -> (string * typ) list * term list * term
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
    48
end;
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
    49
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
    50
structure ATP_Util : ATP_UTIL =
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
    51
struct
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
    52
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
    53
val timestamp = Date.fmt "%Y-%m-%d %H:%M:%S" o Date.fromTimeLocal o Time.now
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
    54
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
    55
(* This hash function is recommended in "Compilers: Principles, Techniques, and
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
    56
   Tools" by Aho, Sethi, and Ullman. The "hashpjw" function, which they
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
    57
   particularly recommend, triggers a bug in versions of Poly/ML up to 4.2.0. *)
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
    58
fun hashw (u, w) = Word.+ (u, Word.* (0w65599, w))
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
    59
fun hashw_char (c, w) = hashw (Word.fromInt (Char.ord c), w)
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
    60
fun hashw_string (s : string, w) = CharVector.foldl hashw_char w s
43827
62d64709af3b added option to control which lambda translation to use (for experiments)
blanchet
parents: 43572
diff changeset
    61
fun hashw_term (t1 $ t2) = hashw (hashw_term t1, hashw_term t2)
62d64709af3b added option to control which lambda translation to use (for experiments)
blanchet
parents: 43572
diff changeset
    62
  | hashw_term (Const (s, _)) = hashw_string (s, 0w0)
62d64709af3b added option to control which lambda translation to use (for experiments)
blanchet
parents: 43572
diff changeset
    63
  | hashw_term (Free (s, _)) = hashw_string (s, 0w0)
62d64709af3b added option to control which lambda translation to use (for experiments)
blanchet
parents: 43572
diff changeset
    64
  | hashw_term _ = 0w0
62d64709af3b added option to control which lambda translation to use (for experiments)
blanchet
parents: 43572
diff changeset
    65
62d64709af3b added option to control which lambda translation to use (for experiments)
blanchet
parents: 43572
diff changeset
    66
fun hash_string s = Word.toInt (hashw_string (s, 0w0))
62d64709af3b added option to control which lambda translation to use (for experiments)
blanchet
parents: 43572
diff changeset
    67
val hash_term = Word.toInt o hashw_term
43085
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
    68
48251
6cdcfbddc077 moved most of MaSh exporter code to Sledgehammer
blanchet
parents: 48247
diff changeset
    69
fun stringN_of_int 0 _ = ""
6cdcfbddc077 moved most of MaSh exporter code to Sledgehammer
blanchet
parents: 48247
diff changeset
    70
  | stringN_of_int k n =
6cdcfbddc077 moved most of MaSh exporter code to Sledgehammer
blanchet
parents: 48247
diff changeset
    71
    stringN_of_int (k - 1) (n div 10) ^ string_of_int (n mod 10)
6cdcfbddc077 moved most of MaSh exporter code to Sledgehammer
blanchet
parents: 48247
diff changeset
    72
43085
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
    73
fun strip_spaces skip_comments is_evil =
44935
2e812384afa8 tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents: 44893
diff changeset
    74
  let
2e812384afa8 tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents: 44893
diff changeset
    75
    fun strip_c_style_comment [] accum = accum
2e812384afa8 tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents: 44893
diff changeset
    76
      | strip_c_style_comment (#"*" :: #"/" :: cs) accum =
2e812384afa8 tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents: 44893
diff changeset
    77
        strip_spaces_in_list true cs accum
2e812384afa8 tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents: 44893
diff changeset
    78
      | strip_c_style_comment (_ :: cs) accum = strip_c_style_comment cs accum
2e812384afa8 tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents: 44893
diff changeset
    79
    and strip_spaces_in_list _ [] accum = rev accum
2e812384afa8 tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents: 44893
diff changeset
    80
      | strip_spaces_in_list true (#"%" :: cs) accum =
2e812384afa8 tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents: 44893
diff changeset
    81
        strip_spaces_in_list true (cs |> chop_while (not_equal #"\n") |> snd)
2e812384afa8 tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents: 44893
diff changeset
    82
                             accum
2e812384afa8 tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents: 44893
diff changeset
    83
      | strip_spaces_in_list true (#"/" :: #"*" :: cs) accum =
2e812384afa8 tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents: 44893
diff changeset
    84
        strip_c_style_comment cs accum
2e812384afa8 tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents: 44893
diff changeset
    85
      | strip_spaces_in_list _ [c1] accum =
2e812384afa8 tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents: 44893
diff changeset
    86
        accum |> not (Char.isSpace c1) ? cons c1
2e812384afa8 tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents: 44893
diff changeset
    87
      | strip_spaces_in_list skip_comments (cs as [_, _]) accum =
2e812384afa8 tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents: 44893
diff changeset
    88
        accum |> fold (strip_spaces_in_list skip_comments o single) cs
2e812384afa8 tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents: 44893
diff changeset
    89
      | strip_spaces_in_list skip_comments (c1 :: c2 :: c3 :: cs) accum =
2e812384afa8 tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents: 44893
diff changeset
    90
        if Char.isSpace c1 then
2e812384afa8 tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents: 44893
diff changeset
    91
          strip_spaces_in_list skip_comments (c2 :: c3 :: cs) accum
2e812384afa8 tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents: 44893
diff changeset
    92
        else if Char.isSpace c2 then
2e812384afa8 tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents: 44893
diff changeset
    93
          if Char.isSpace c3 then
2e812384afa8 tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents: 44893
diff changeset
    94
            strip_spaces_in_list skip_comments (c1 :: c3 :: cs) accum
2e812384afa8 tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents: 44893
diff changeset
    95
          else
2e812384afa8 tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents: 44893
diff changeset
    96
            strip_spaces_in_list skip_comments (c3 :: cs)
2e812384afa8 tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents: 44893
diff changeset
    97
                (c1 :: accum |> forall is_evil [c1, c3] ? cons #" ")
2e812384afa8 tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents: 44893
diff changeset
    98
        else
2e812384afa8 tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents: 44893
diff changeset
    99
          strip_spaces_in_list skip_comments (c2 :: c3 :: cs) (cons c1 accum)
2e812384afa8 tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents: 44893
diff changeset
   100
  in
2e812384afa8 tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents: 44893
diff changeset
   101
    String.explode
2e812384afa8 tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents: 44893
diff changeset
   102
    #> rpair [] #-> strip_spaces_in_list skip_comments
2e812384afa8 tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents: 44893
diff changeset
   103
    #> rev #> String.implode
2e812384afa8 tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents: 44893
diff changeset
   104
  end
43085
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   105
44784
blanchet
parents: 44500
diff changeset
   106
fun is_ident_char c = Char.isAlphaNum c orelse c = #"_"
blanchet
parents: 44500
diff changeset
   107
val strip_spaces_except_between_idents = strip_spaces true is_ident_char
blanchet
parents: 44500
diff changeset
   108
43085
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   109
val subscript = implode o map (prefix "\<^isub>") o raw_explode  (* FIXME Symbol.explode (?) *)
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   110
fun nat_subscript n =
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   111
  n |> string_of_int |> print_mode_active Symbol.xsymbolsN ? subscript
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   112
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   113
val unyxml = XML.content_of o YXML.parse_body
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   114
46711
f745bcc4a1e5 more explicit Long_Name operations (NB: analyzing qualifiers is inherently fragile);
wenzelm
parents: 46385
diff changeset
   115
val is_long_identifier = forall Lexicon.is_identifier o Long_Name.explode
43085
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   116
fun maybe_quote y =
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   117
  let val s = unyxml y in
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   118
    y |> ((not (is_long_identifier (perhaps (try (unprefix "'")) s)) andalso
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   119
           not (is_long_identifier (perhaps (try (unprefix "?")) s))) orelse
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   120
           Keyword.is_keyword s) ? quote
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   121
  end
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   122
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   123
fun string_from_ext_time (plus, time) =
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   124
  let val ms = Time.toMilliseconds time in
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   125
    (if plus then "> " else "") ^
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   126
    (if plus andalso ms mod 1000 = 0 then
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   127
       signed_string_of_int (ms div 1000) ^ " s"
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   128
     else if ms < 1000 then
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   129
       signed_string_of_int ms ^ " ms"
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   130
     else
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   131
       string_of_real (0.01 * Real.fromInt (ms div 10)) ^ " s")
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   132
  end
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   133
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   134
val string_from_time = string_from_ext_time o pair false
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   135
47150
6df6e56fd7cd tuning (in particular, Symtab instead of AList)
blanchet
parents: 46711
diff changeset
   136
fun type_instance thy T T' = Sign.typ_instance thy (T, T')
6df6e56fd7cd tuning (in particular, Symtab instead of AList)
blanchet
parents: 46711
diff changeset
   137
fun type_generalization thy T T' = Sign.typ_instance thy (T', T)
48247
8f37d2ddabc8 optimized type intersection, hoping this will reduce the number of sudden Interrupts in the "incr_tvar" code
blanchet
parents: 48238
diff changeset
   138
8f37d2ddabc8 optimized type intersection, hoping this will reduce the number of sudden Interrupts in the "incr_tvar" code
blanchet
parents: 48238
diff changeset
   139
fun type_intersect _ (TVar _) _ = true
8f37d2ddabc8 optimized type intersection, hoping this will reduce the number of sudden Interrupts in the "incr_tvar" code
blanchet
parents: 48238
diff changeset
   140
  | type_intersect _ _ (TVar _) = true
8f37d2ddabc8 optimized type intersection, hoping this will reduce the number of sudden Interrupts in the "incr_tvar" code
blanchet
parents: 48238
diff changeset
   141
  | type_intersect thy T T' =
8f37d2ddabc8 optimized type intersection, hoping this will reduce the number of sudden Interrupts in the "incr_tvar" code
blanchet
parents: 48238
diff changeset
   142
    let
8f37d2ddabc8 optimized type intersection, hoping this will reduce the number of sudden Interrupts in the "incr_tvar" code
blanchet
parents: 48238
diff changeset
   143
      val tvars = Term.add_tvar_namesT T []
8f37d2ddabc8 optimized type intersection, hoping this will reduce the number of sudden Interrupts in the "incr_tvar" code
blanchet
parents: 48238
diff changeset
   144
      val tvars' = Term.add_tvar_namesT T' []
8f37d2ddabc8 optimized type intersection, hoping this will reduce the number of sudden Interrupts in the "incr_tvar" code
blanchet
parents: 48238
diff changeset
   145
      val T =
8f37d2ddabc8 optimized type intersection, hoping this will reduce the number of sudden Interrupts in the "incr_tvar" code
blanchet
parents: 48238
diff changeset
   146
        if exists (member (op =) tvars') tvars then
8f37d2ddabc8 optimized type intersection, hoping this will reduce the number of sudden Interrupts in the "incr_tvar" code
blanchet
parents: 48238
diff changeset
   147
          T |> Logic.incr_tvar (maxidx_of_typ T' + 1)
8f37d2ddabc8 optimized type intersection, hoping this will reduce the number of sudden Interrupts in the "incr_tvar" code
blanchet
parents: 48238
diff changeset
   148
        else
8f37d2ddabc8 optimized type intersection, hoping this will reduce the number of sudden Interrupts in the "incr_tvar" code
blanchet
parents: 48238
diff changeset
   149
          T
8f37d2ddabc8 optimized type intersection, hoping this will reduce the number of sudden Interrupts in the "incr_tvar" code
blanchet
parents: 48238
diff changeset
   150
    in can (Sign.typ_unify thy (T, T')) (Vartab.empty, 0) end
8f37d2ddabc8 optimized type intersection, hoping this will reduce the number of sudden Interrupts in the "incr_tvar" code
blanchet
parents: 48238
diff changeset
   151
47150
6df6e56fd7cd tuning (in particular, Symtab instead of AList)
blanchet
parents: 46711
diff changeset
   152
val type_equiv = Sign.typ_equiv
44399
cd1e32b8d4c4 added caching for (in)finiteness checks
blanchet
parents: 44393
diff changeset
   153
43085
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   154
fun varify_type ctxt T =
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   155
  Variable.polymorphic_types ctxt [Const (@{const_name undefined}, T)]
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   156
  |> snd |> the_single |> dest_Const |> snd
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   157
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   158
(* TODO: use "Term_Subst.instantiateT" instead? *)
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   159
fun instantiate_type thy T1 T1' T2 =
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   160
  Same.commit (Envir.subst_type_same
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   161
                   (Sign.typ_match thy (T1, T1') Vartab.empty)) T2
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   162
  handle Type.TYPE_MATCH => raise TYPE ("instantiate_type", [T1, T1'], [])
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   163
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   164
fun varify_and_instantiate_type ctxt T1 T1' T2 =
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   165
  let val thy = Proof_Context.theory_of ctxt in
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   166
    instantiate_type thy (varify_type ctxt T1) T1' (varify_type ctxt T2)
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   167
  end
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   168
45896
100fb1f33e3e tuned signature;
wenzelm
parents: 45570
diff changeset
   169
fun typ_of_dtyp _ typ_assoc (Datatype.DtTFree a) =
100fb1f33e3e tuned signature;
wenzelm
parents: 45570
diff changeset
   170
    the (AList.lookup (op =) typ_assoc (Datatype.DtTFree a))
100fb1f33e3e tuned signature;
wenzelm
parents: 45570
diff changeset
   171
  | typ_of_dtyp descr typ_assoc (Datatype.DtType (s, Us)) =
43085
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   172
    Type (s, map (typ_of_dtyp descr typ_assoc) Us)
45896
100fb1f33e3e tuned signature;
wenzelm
parents: 45570
diff changeset
   173
  | typ_of_dtyp descr typ_assoc (Datatype.DtRec i) =
43085
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   174
    let val (s, ds, _) = the (AList.lookup (op =) descr i) in
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   175
      Type (s, map (typ_of_dtyp descr typ_assoc) ds)
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   176
    end
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   177
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   178
fun datatype_constrs thy (T as Type (s, Ts)) =
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   179
    (case Datatype.get_info thy s of
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   180
       SOME {index, descr, ...} =>
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   181
       let val (_, dtyps, constrs) = AList.lookup (op =) descr index |> the in
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   182
         map (apsnd (fn Us => map (typ_of_dtyp descr (dtyps ~~ Ts)) Us ---> T))
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   183
             constrs
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   184
       end
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   185
     | NONE => [])
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   186
  | datatype_constrs _ _ = []
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   187
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   188
(* Similar to "Nitpick_HOL.bounded_exact_card_of_type".
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   189
   0 means infinite type, 1 means singleton type (e.g., "unit"), and 2 means
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   190
   cardinality 2 or more. The specified default cardinality is returned if the
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   191
   cardinality of the type can't be determined. *)
44500
dbd98b549597 make default unsound mode less unsound
blanchet
parents: 44491
diff changeset
   192
fun tiny_card_of_type ctxt sound assigns default_card T =
43085
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   193
  let
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   194
    val thy = Proof_Context.theory_of ctxt
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   195
    val max = 2 (* 1 would be too small for the "fun" case *)
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   196
    fun aux slack avoid T =
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   197
      if member (op =) avoid T then
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   198
        0
47150
6df6e56fd7cd tuning (in particular, Symtab instead of AList)
blanchet
parents: 46711
diff changeset
   199
      else case AList.lookup (type_equiv thy) assigns T of
44393
23adec5984f1 make sound mode more sound (and clean up code)
blanchet
parents: 44392
diff changeset
   200
        SOME k => k
23adec5984f1 make sound mode more sound (and clean up code)
blanchet
parents: 44392
diff changeset
   201
      | NONE =>
44392
6750b4297691 reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents: 43864
diff changeset
   202
        case T of
6750b4297691 reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents: 43864
diff changeset
   203
          Type (@{type_name fun}, [T1, T2]) =>
6750b4297691 reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents: 43864
diff changeset
   204
          (case (aux slack avoid T1, aux slack avoid T2) of
6750b4297691 reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents: 43864
diff changeset
   205
             (k, 1) => if slack andalso k = 0 then 0 else 1
6750b4297691 reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents: 43864
diff changeset
   206
           | (0, _) => 0
6750b4297691 reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents: 43864
diff changeset
   207
           | (_, 0) => 0
6750b4297691 reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents: 43864
diff changeset
   208
           | (k1, k2) =>
6750b4297691 reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents: 43864
diff changeset
   209
             if k1 >= max orelse k2 >= max then max
6750b4297691 reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents: 43864
diff changeset
   210
             else Int.min (max, Integer.pow k2 k1))
48230
0feb93dfb268 gracefully compute cardinality of sets (to avoid type protectors)
blanchet
parents: 47991
diff changeset
   211
        | Type (@{type_name set}, [T']) => aux slack avoid (T' --> @{typ bool})
44392
6750b4297691 reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents: 43864
diff changeset
   212
        | @{typ prop} => 2
6750b4297691 reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents: 43864
diff changeset
   213
        | @{typ bool} => 2 (* optimization *)
6750b4297691 reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents: 43864
diff changeset
   214
        | @{typ nat} => 0 (* optimization *)
6750b4297691 reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents: 43864
diff changeset
   215
        | Type ("Int.int", []) => 0 (* optimization *)
6750b4297691 reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents: 43864
diff changeset
   216
        | Type (s, _) =>
6750b4297691 reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents: 43864
diff changeset
   217
          (case datatype_constrs thy T of
6750b4297691 reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents: 43864
diff changeset
   218
             constrs as _ :: _ =>
6750b4297691 reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents: 43864
diff changeset
   219
             let
6750b4297691 reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents: 43864
diff changeset
   220
               val constr_cards =
6750b4297691 reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents: 43864
diff changeset
   221
                 map (Integer.prod o map (aux slack (T :: avoid)) o binder_types
6750b4297691 reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents: 43864
diff changeset
   222
                      o snd) constrs
6750b4297691 reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents: 43864
diff changeset
   223
             in
6750b4297691 reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents: 43864
diff changeset
   224
               if exists (curry (op =) 0) constr_cards then 0
6750b4297691 reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents: 43864
diff changeset
   225
               else Int.min (max, Integer.sum constr_cards)
6750b4297691 reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents: 43864
diff changeset
   226
             end
6750b4297691 reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents: 43864
diff changeset
   227
           | [] =>
6750b4297691 reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents: 43864
diff changeset
   228
             case Typedef.get_info ctxt s of
6750b4297691 reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents: 43864
diff changeset
   229
               ({abs_type, rep_type, ...}, _) :: _ =>
45299
ee584ff987c3 check "sound" flag before doing something unsound...
blanchet
parents: 44935
diff changeset
   230
               if not sound then
ee584ff987c3 check "sound" flag before doing something unsound...
blanchet
parents: 44935
diff changeset
   231
                 (* We cheat here by assuming that typedef types are infinite if
ee584ff987c3 check "sound" flag before doing something unsound...
blanchet
parents: 44935
diff changeset
   232
                    their underlying type is infinite. This is unsound in
ee584ff987c3 check "sound" flag before doing something unsound...
blanchet
parents: 44935
diff changeset
   233
                    general but it's hard to think of a realistic example where
ee584ff987c3 check "sound" flag before doing something unsound...
blanchet
parents: 44935
diff changeset
   234
                    this would not be the case. We are also slack with
ee584ff987c3 check "sound" flag before doing something unsound...
blanchet
parents: 44935
diff changeset
   235
                    representation types: If a representation type has the form
ee584ff987c3 check "sound" flag before doing something unsound...
blanchet
parents: 44935
diff changeset
   236
                    "sigma => tau", we consider it enough to check "sigma" for
ee584ff987c3 check "sound" flag before doing something unsound...
blanchet
parents: 44935
diff changeset
   237
                    infiniteness. *)
ee584ff987c3 check "sound" flag before doing something unsound...
blanchet
parents: 44935
diff changeset
   238
                 (case varify_and_instantiate_type ctxt
ee584ff987c3 check "sound" flag before doing something unsound...
blanchet
parents: 44935
diff changeset
   239
                           (Logic.varifyT_global abs_type) T
ee584ff987c3 check "sound" flag before doing something unsound...
blanchet
parents: 44935
diff changeset
   240
                           (Logic.varifyT_global rep_type)
ee584ff987c3 check "sound" flag before doing something unsound...
blanchet
parents: 44935
diff changeset
   241
                       |> aux true avoid of
ee584ff987c3 check "sound" flag before doing something unsound...
blanchet
parents: 44935
diff changeset
   242
                    0 => 0
ee584ff987c3 check "sound" flag before doing something unsound...
blanchet
parents: 44935
diff changeset
   243
                  | 1 => 1
ee584ff987c3 check "sound" flag before doing something unsound...
blanchet
parents: 44935
diff changeset
   244
                  | _ => default_card)
ee584ff987c3 check "sound" flag before doing something unsound...
blanchet
parents: 44935
diff changeset
   245
               else
ee584ff987c3 check "sound" flag before doing something unsound...
blanchet
parents: 44935
diff changeset
   246
                 default_card
44392
6750b4297691 reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents: 43864
diff changeset
   247
             | [] => default_card)
6750b4297691 reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents: 43864
diff changeset
   248
          (* Very slightly unsound: Type variables are assumed not to be
6750b4297691 reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents: 43864
diff changeset
   249
             constrained to cardinality 1. (In practice, the user would most
6750b4297691 reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents: 43864
diff changeset
   250
             likely have used "unit" directly anyway.) *)
44500
dbd98b549597 make default unsound mode less unsound
blanchet
parents: 44491
diff changeset
   251
        | TFree _ =>
dbd98b549597 make default unsound mode less unsound
blanchet
parents: 44491
diff changeset
   252
          if not sound andalso default_card = 1 then 2 else default_card
44392
6750b4297691 reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents: 43864
diff changeset
   253
        | TVar _ => default_card
43085
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   254
  in Int.min (max, aux false [] T) end
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   255
44500
dbd98b549597 make default unsound mode less unsound
blanchet
parents: 44491
diff changeset
   256
fun is_type_surely_finite ctxt T = tiny_card_of_type ctxt true [] 0 T <> 0
dbd98b549597 make default unsound mode less unsound
blanchet
parents: 44491
diff changeset
   257
fun is_type_surely_infinite ctxt sound infinite_Ts T =
dbd98b549597 make default unsound mode less unsound
blanchet
parents: 44491
diff changeset
   258
  tiny_card_of_type ctxt sound (map (rpair 0) infinite_Ts) 1 T = 0
43085
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   259
43863
a43d61270142 ensure that the lambda translation procedure is called only once with all the facts, which is necessary for soundness of lambda-lifting (freshness of new names)
blanchet
parents: 43827
diff changeset
   260
(* Simple simplifications to ensure that sort annotations don't leave a trail of
a43d61270142 ensure that the lambda translation procedure is called only once with all the facts, which is necessary for soundness of lambda-lifting (freshness of new names)
blanchet
parents: 43827
diff changeset
   261
   spurious "True"s. *)
a43d61270142 ensure that the lambda translation procedure is called only once with all the facts, which is necessary for soundness of lambda-lifting (freshness of new names)
blanchet
parents: 43827
diff changeset
   262
fun s_not (Const (@{const_name All}, T) $ Abs (s, T', t')) =
a43d61270142 ensure that the lambda translation procedure is called only once with all the facts, which is necessary for soundness of lambda-lifting (freshness of new names)
blanchet
parents: 43827
diff changeset
   263
    Const (@{const_name Ex}, T) $ Abs (s, T', s_not t')
a43d61270142 ensure that the lambda translation procedure is called only once with all the facts, which is necessary for soundness of lambda-lifting (freshness of new names)
blanchet
parents: 43827
diff changeset
   264
  | s_not (Const (@{const_name Ex}, T) $ Abs (s, T', t')) =
a43d61270142 ensure that the lambda translation procedure is called only once with all the facts, which is necessary for soundness of lambda-lifting (freshness of new names)
blanchet
parents: 43827
diff changeset
   265
    Const (@{const_name All}, T) $ Abs (s, T', s_not t')
a43d61270142 ensure that the lambda translation procedure is called only once with all the facts, which is necessary for soundness of lambda-lifting (freshness of new names)
blanchet
parents: 43827
diff changeset
   266
  | s_not (@{const HOL.implies} $ t1 $ t2) = @{const HOL.conj} $ t1 $ s_not t2
a43d61270142 ensure that the lambda translation procedure is called only once with all the facts, which is necessary for soundness of lambda-lifting (freshness of new names)
blanchet
parents: 43827
diff changeset
   267
  | s_not (@{const HOL.conj} $ t1 $ t2) =
a43d61270142 ensure that the lambda translation procedure is called only once with all the facts, which is necessary for soundness of lambda-lifting (freshness of new names)
blanchet
parents: 43827
diff changeset
   268
    @{const HOL.disj} $ s_not t1 $ s_not t2
a43d61270142 ensure that the lambda translation procedure is called only once with all the facts, which is necessary for soundness of lambda-lifting (freshness of new names)
blanchet
parents: 43827
diff changeset
   269
  | s_not (@{const HOL.disj} $ t1 $ t2) =
a43d61270142 ensure that the lambda translation procedure is called only once with all the facts, which is necessary for soundness of lambda-lifting (freshness of new names)
blanchet
parents: 43827
diff changeset
   270
    @{const HOL.conj} $ s_not t1 $ s_not t2
a43d61270142 ensure that the lambda translation procedure is called only once with all the facts, which is necessary for soundness of lambda-lifting (freshness of new names)
blanchet
parents: 43827
diff changeset
   271
  | s_not (@{const False}) = @{const True}
a43d61270142 ensure that the lambda translation procedure is called only once with all the facts, which is necessary for soundness of lambda-lifting (freshness of new names)
blanchet
parents: 43827
diff changeset
   272
  | s_not (@{const True}) = @{const False}
a43d61270142 ensure that the lambda translation procedure is called only once with all the facts, which is necessary for soundness of lambda-lifting (freshness of new names)
blanchet
parents: 43827
diff changeset
   273
  | s_not (@{const Not} $ t) = t
a43d61270142 ensure that the lambda translation procedure is called only once with all the facts, which is necessary for soundness of lambda-lifting (freshness of new names)
blanchet
parents: 43827
diff changeset
   274
  | s_not t = @{const Not} $ t
a43d61270142 ensure that the lambda translation procedure is called only once with all the facts, which is necessary for soundness of lambda-lifting (freshness of new names)
blanchet
parents: 43827
diff changeset
   275
fun s_conj (@{const True}, t2) = t2
a43d61270142 ensure that the lambda translation procedure is called only once with all the facts, which is necessary for soundness of lambda-lifting (freshness of new names)
blanchet
parents: 43827
diff changeset
   276
  | s_conj (t1, @{const True}) = t1
a43d61270142 ensure that the lambda translation procedure is called only once with all the facts, which is necessary for soundness of lambda-lifting (freshness of new names)
blanchet
parents: 43827
diff changeset
   277
  | s_conj p = HOLogic.mk_conj p
a43d61270142 ensure that the lambda translation procedure is called only once with all the facts, which is necessary for soundness of lambda-lifting (freshness of new names)
blanchet
parents: 43827
diff changeset
   278
fun s_disj (@{const False}, t2) = t2
a43d61270142 ensure that the lambda translation procedure is called only once with all the facts, which is necessary for soundness of lambda-lifting (freshness of new names)
blanchet
parents: 43827
diff changeset
   279
  | s_disj (t1, @{const False}) = t1
a43d61270142 ensure that the lambda translation procedure is called only once with all the facts, which is necessary for soundness of lambda-lifting (freshness of new names)
blanchet
parents: 43827
diff changeset
   280
  | s_disj p = HOLogic.mk_disj p
a43d61270142 ensure that the lambda translation procedure is called only once with all the facts, which is necessary for soundness of lambda-lifting (freshness of new names)
blanchet
parents: 43827
diff changeset
   281
fun s_imp (@{const True}, t2) = t2
a43d61270142 ensure that the lambda translation procedure is called only once with all the facts, which is necessary for soundness of lambda-lifting (freshness of new names)
blanchet
parents: 43827
diff changeset
   282
  | s_imp (t1, @{const False}) = s_not t1
a43d61270142 ensure that the lambda translation procedure is called only once with all the facts, which is necessary for soundness of lambda-lifting (freshness of new names)
blanchet
parents: 43827
diff changeset
   283
  | s_imp p = HOLogic.mk_imp p
a43d61270142 ensure that the lambda translation procedure is called only once with all the facts, which is necessary for soundness of lambda-lifting (freshness of new names)
blanchet
parents: 43827
diff changeset
   284
fun s_iff (@{const True}, t2) = t2
a43d61270142 ensure that the lambda translation procedure is called only once with all the facts, which is necessary for soundness of lambda-lifting (freshness of new names)
blanchet
parents: 43827
diff changeset
   285
  | s_iff (t1, @{const True}) = t1
a43d61270142 ensure that the lambda translation procedure is called only once with all the facts, which is necessary for soundness of lambda-lifting (freshness of new names)
blanchet
parents: 43827
diff changeset
   286
  | s_iff (t1, t2) = HOLogic.eq_const HOLogic.boolT $ t1 $ t2
a43d61270142 ensure that the lambda translation procedure is called only once with all the facts, which is necessary for soundness of lambda-lifting (freshness of new names)
blanchet
parents: 43827
diff changeset
   287
46385
0ccf458a3633 third attempt at lambda lifting that works for both Sledgehammer and Metis (cf. dce6c3a460a9)
blanchet
parents: 45896
diff changeset
   288
val close_form_prefix = "ATP.close_form."
0ccf458a3633 third attempt at lambda lifting that works for both Sledgehammer and Metis (cf. dce6c3a460a9)
blanchet
parents: 45896
diff changeset
   289
43864
58a7b3fdc193 fixed lambda-liftg: must ensure the formulas are in close form
blanchet
parents: 43863
diff changeset
   290
fun close_form t =
45570
6d95a66cce00 pull variables (Var) out of lambdas, so that the Isabelle theorems closely mirror the Metis lambda-lifted ones
blanchet
parents: 45511
diff changeset
   291
  fold (fn ((s, i), T) => fn t' =>
45511
9b0f8ca4388e continued implementation of lambda-lifting in Metis
blanchet
parents: 45299
diff changeset
   292
           HOLogic.all_const T
46385
0ccf458a3633 third attempt at lambda lifting that works for both Sledgehammer and Metis (cf. dce6c3a460a9)
blanchet
parents: 45896
diff changeset
   293
           $ Abs (close_form_prefix ^ s, T,
0ccf458a3633 third attempt at lambda lifting that works for both Sledgehammer and Metis (cf. dce6c3a460a9)
blanchet
parents: 45896
diff changeset
   294
                  abstract_over (Var ((s, i), T), t')))
43864
58a7b3fdc193 fixed lambda-liftg: must ensure the formulas are in close form
blanchet
parents: 43863
diff changeset
   295
       (Term.add_vars t []) t
58a7b3fdc193 fixed lambda-liftg: must ensure the formulas are in close form
blanchet
parents: 43863
diff changeset
   296
47718
39229c760636 smoother handling of conjecture, so that its Skolem constants get displayed in countermodels
blanchet
parents: 47715
diff changeset
   297
fun open_form unprefix (t as Const (@{const_name All}, _) $ Abs (s, T, t')) =
39229c760636 smoother handling of conjecture, so that its Skolem constants get displayed in countermodels
blanchet
parents: 47715
diff changeset
   298
    (case try unprefix s of
39229c760636 smoother handling of conjecture, so that its Skolem constants get displayed in countermodels
blanchet
parents: 47715
diff changeset
   299
       SOME s =>
39229c760636 smoother handling of conjecture, so that its Skolem constants get displayed in countermodels
blanchet
parents: 47715
diff changeset
   300
       let
39229c760636 smoother handling of conjecture, so that its Skolem constants get displayed in countermodels
blanchet
parents: 47715
diff changeset
   301
         val names = Name.make_context (map fst (Term.add_var_names t' []))
39229c760636 smoother handling of conjecture, so that its Skolem constants get displayed in countermodels
blanchet
parents: 47715
diff changeset
   302
         val (s, _) = Name.variant s names
39229c760636 smoother handling of conjecture, so that its Skolem constants get displayed in countermodels
blanchet
parents: 47715
diff changeset
   303
       in open_form unprefix (subst_bound (Var ((s, 0), T), t')) end
39229c760636 smoother handling of conjecture, so that its Skolem constants get displayed in countermodels
blanchet
parents: 47715
diff changeset
   304
     | NONE => t)
39229c760636 smoother handling of conjecture, so that its Skolem constants get displayed in countermodels
blanchet
parents: 47715
diff changeset
   305
  | open_form _ t = t
39229c760636 smoother handling of conjecture, so that its Skolem constants get displayed in countermodels
blanchet
parents: 47715
diff changeset
   306
43171
37e1431cc213 gracefully handle the case where a constant is partially or not instantiated at all, as may happen when reconstructing Metis proofs for polymorphic type encodings
blanchet
parents: 43085
diff changeset
   307
fun monomorphic_term subst =
43085
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   308
  map_types (map_type_tvar (fn v =>
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   309
      case Type.lookup subst v of
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   310
        SOME typ => typ
43171
37e1431cc213 gracefully handle the case where a constant is partially or not instantiated at all, as may happen when reconstructing Metis proofs for polymorphic type encodings
blanchet
parents: 43085
diff changeset
   311
      | NONE => TVar v))
43085
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   312
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   313
fun eta_expand _ t 0 = t
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   314
  | eta_expand Ts (Abs (s, T, t')) n =
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   315
    Abs (s, T, eta_expand (T :: Ts) t' (n - 1))
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   316
  | eta_expand Ts t n =
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   317
    fold_rev (fn T => fn t' => Abs ("x" ^ nat_subscript n, T, t'))
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   318
             (List.take (binder_types (fastype_of1 (Ts, t)), n))
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   319
             (list_comb (incr_boundvars n t, map Bound (n - 1 downto 0)))
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   320
47954
aada9fd08b58 make higher-order goals more first-order via extensionality
blanchet
parents: 47953
diff changeset
   321
fun cong_extensionalize_term thy t =
aada9fd08b58 make higher-order goals more first-order via extensionality
blanchet
parents: 47953
diff changeset
   322
  if exists_Const (fn (s, _) => s = @{const_name Not}) t then
aada9fd08b58 make higher-order goals more first-order via extensionality
blanchet
parents: 47953
diff changeset
   323
    t |> Skip_Proof.make_thm thy
aada9fd08b58 make higher-order goals more first-order via extensionality
blanchet
parents: 47953
diff changeset
   324
      |> Meson.cong_extensionalize_thm thy
aada9fd08b58 make higher-order goals more first-order via extensionality
blanchet
parents: 47953
diff changeset
   325
      |> prop_of
aada9fd08b58 make higher-order goals more first-order via extensionality
blanchet
parents: 47953
diff changeset
   326
  else
aada9fd08b58 make higher-order goals more first-order via extensionality
blanchet
parents: 47953
diff changeset
   327
    t
aada9fd08b58 make higher-order goals more first-order via extensionality
blanchet
parents: 47953
diff changeset
   328
47715
04400144c6fc handle TPTP definitions as definitions in Nitpick rather than as axioms
blanchet
parents: 47150
diff changeset
   329
fun is_fun_equality (@{const_name HOL.eq},
04400144c6fc handle TPTP definitions as definitions in Nitpick rather than as axioms
blanchet
parents: 47150
diff changeset
   330
                     Type (_, [Type (@{type_name fun}, _), _])) = true
04400144c6fc handle TPTP definitions as definitions in Nitpick rather than as axioms
blanchet
parents: 47150
diff changeset
   331
  | is_fun_equality _ = false
04400144c6fc handle TPTP definitions as definitions in Nitpick rather than as axioms
blanchet
parents: 47150
diff changeset
   332
47953
a2c3706c4cb1 added "ext_cong_neq" lemma (not used yet); tuning
blanchet
parents: 47718
diff changeset
   333
fun abs_extensionalize_term ctxt t =
47715
04400144c6fc handle TPTP definitions as definitions in Nitpick rather than as axioms
blanchet
parents: 47150
diff changeset
   334
  if exists_Const is_fun_equality t then
04400144c6fc handle TPTP definitions as definitions in Nitpick rather than as axioms
blanchet
parents: 47150
diff changeset
   335
    let val thy = Proof_Context.theory_of ctxt in
47953
a2c3706c4cb1 added "ext_cong_neq" lemma (not used yet); tuning
blanchet
parents: 47718
diff changeset
   336
      t |> cterm_of thy |> Meson.abs_extensionalize_conv ctxt
47715
04400144c6fc handle TPTP definitions as definitions in Nitpick rather than as axioms
blanchet
parents: 47150
diff changeset
   337
        |> prop_of |> Logic.dest_equals |> snd
04400144c6fc handle TPTP definitions as definitions in Nitpick rather than as axioms
blanchet
parents: 47150
diff changeset
   338
    end
04400144c6fc handle TPTP definitions as definitions in Nitpick rather than as axioms
blanchet
parents: 47150
diff changeset
   339
  else
04400144c6fc handle TPTP definitions as definitions in Nitpick rather than as axioms
blanchet
parents: 47150
diff changeset
   340
    t
04400144c6fc handle TPTP definitions as definitions in Nitpick rather than as axioms
blanchet
parents: 47150
diff changeset
   341
47991
3eb598b044ad make Nitpick's handling of definitions more robust in the face of formulas that don't have the expected format (needed for soundness, cf. RNG100+1)
blanchet
parents: 47954
diff changeset
   342
fun unextensionalize_def t =
3eb598b044ad make Nitpick's handling of definitions more robust in the face of formulas that don't have the expected format (needed for soundness, cf. RNG100+1)
blanchet
parents: 47954
diff changeset
   343
  case t of
3eb598b044ad make Nitpick's handling of definitions more robust in the face of formulas that don't have the expected format (needed for soundness, cf. RNG100+1)
blanchet
parents: 47954
diff changeset
   344
    @{const Trueprop} $ (Const (@{const_name HOL.eq}, _) $ lhs $ rhs) =>
3eb598b044ad make Nitpick's handling of definitions more robust in the face of formulas that don't have the expected format (needed for soundness, cf. RNG100+1)
blanchet
parents: 47954
diff changeset
   345
    (case strip_comb lhs of
3eb598b044ad make Nitpick's handling of definitions more robust in the face of formulas that don't have the expected format (needed for soundness, cf. RNG100+1)
blanchet
parents: 47954
diff changeset
   346
       (c as Const (_, T), args) =>
3eb598b044ad make Nitpick's handling of definitions more robust in the face of formulas that don't have the expected format (needed for soundness, cf. RNG100+1)
blanchet
parents: 47954
diff changeset
   347
       if forall is_Var args andalso not (has_duplicates (op =) args) then
3eb598b044ad make Nitpick's handling of definitions more robust in the face of formulas that don't have the expected format (needed for soundness, cf. RNG100+1)
blanchet
parents: 47954
diff changeset
   348
         @{const Trueprop}
3eb598b044ad make Nitpick's handling of definitions more robust in the face of formulas that don't have the expected format (needed for soundness, cf. RNG100+1)
blanchet
parents: 47954
diff changeset
   349
         $ (Const (@{const_name HOL.eq}, T --> T --> @{typ bool})
3eb598b044ad make Nitpick's handling of definitions more robust in the face of formulas that don't have the expected format (needed for soundness, cf. RNG100+1)
blanchet
parents: 47954
diff changeset
   350
            $ c $ fold_rev lambda args rhs)
3eb598b044ad make Nitpick's handling of definitions more robust in the face of formulas that don't have the expected format (needed for soundness, cf. RNG100+1)
blanchet
parents: 47954
diff changeset
   351
       else
3eb598b044ad make Nitpick's handling of definitions more robust in the face of formulas that don't have the expected format (needed for soundness, cf. RNG100+1)
blanchet
parents: 47954
diff changeset
   352
         t
3eb598b044ad make Nitpick's handling of definitions more robust in the face of formulas that don't have the expected format (needed for soundness, cf. RNG100+1)
blanchet
parents: 47954
diff changeset
   353
     | _ => t)
3eb598b044ad make Nitpick's handling of definitions more robust in the face of formulas that don't have the expected format (needed for soundness, cf. RNG100+1)
blanchet
parents: 47954
diff changeset
   354
  | _ => t
3eb598b044ad make Nitpick's handling of definitions more robust in the face of formulas that don't have the expected format (needed for soundness, cf. RNG100+1)
blanchet
parents: 47954
diff changeset
   355
3eb598b044ad make Nitpick's handling of definitions more robust in the face of formulas that don't have the expected format (needed for soundness, cf. RNG100+1)
blanchet
parents: 47954
diff changeset
   356
fun is_legitimate_tptp_def (@{const Trueprop} $ t) = is_legitimate_tptp_def t
3eb598b044ad make Nitpick's handling of definitions more robust in the face of formulas that don't have the expected format (needed for soundness, cf. RNG100+1)
blanchet
parents: 47954
diff changeset
   357
  | is_legitimate_tptp_def (Const (@{const_name HOL.eq}, _) $ t $ u) =
3eb598b044ad make Nitpick's handling of definitions more robust in the face of formulas that don't have the expected format (needed for soundness, cf. RNG100+1)
blanchet
parents: 47954
diff changeset
   358
    (is_Const t orelse is_Free t) andalso
3eb598b044ad make Nitpick's handling of definitions more robust in the face of formulas that don't have the expected format (needed for soundness, cf. RNG100+1)
blanchet
parents: 47954
diff changeset
   359
    not (exists_subterm (curry (op =) t) u)
3eb598b044ad make Nitpick's handling of definitions more robust in the face of formulas that don't have the expected format (needed for soundness, cf. RNG100+1)
blanchet
parents: 47954
diff changeset
   360
  | is_legitimate_tptp_def _ = false
3eb598b044ad make Nitpick's handling of definitions more robust in the face of formulas that don't have the expected format (needed for soundness, cf. RNG100+1)
blanchet
parents: 47954
diff changeset
   361
43085
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   362
(* Converts an elim-rule into an equivalent theorem that does not have the
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   363
   predicate variable. Leaves other theorems unchanged. We simply instantiate
44460
blanchet
parents: 44399
diff changeset
   364
   the conclusion variable to "False". (Cf. "transform_elim_theorem" in
43085
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   365
   "Meson_Clausify".) *)
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   366
fun transform_elim_prop t =
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   367
  case Logic.strip_imp_concl t of
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   368
    @{const Trueprop} $ Var (z, @{typ bool}) =>
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   369
    subst_Vars [(z, @{const False})] t
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   370
  | Var (z, @{typ prop}) => subst_Vars [(z, @{prop False})] t
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   371
  | _ => t
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   372
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   373
fun specialize_type thy (s, T) t =
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   374
  let
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   375
    fun subst_for (Const (s', T')) =
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   376
      if s = s' then
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   377
        SOME (Sign.typ_match thy (T', T) Vartab.empty)
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   378
        handle Type.TYPE_MATCH => NONE
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   379
      else
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   380
        NONE
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   381
    | subst_for (t1 $ t2) =
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   382
      (case subst_for t1 of SOME x => SOME x | NONE => subst_for t2)
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   383
    | subst_for (Abs (_, _, t')) = subst_for t'
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   384
    | subst_for _ = NONE
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   385
  in
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   386
    case subst_for t of
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   387
      SOME subst => monomorphic_term subst t
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   388
    | NONE => raise Type.TYPE_MATCH
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   389
  end
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   390
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   391
fun strip_subgoal ctxt goal i =
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   392
  let
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   393
    val (t, (frees, params)) =
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   394
      Logic.goal_params (prop_of goal) i
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   395
      ||> (map dest_Free #> Variable.variant_frees ctxt [] #> `(map Free))
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   396
    val hyp_ts = t |> Logic.strip_assums_hyp |> map (curry subst_bounds frees)
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   397
    val concl_t = t |> Logic.strip_assums_concl |> curry subst_bounds frees
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   398
  in (rev params, hyp_ts, concl_t) end
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   399
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff changeset
   400
end;