author | wenzelm |
Sun, 04 Aug 2024 17:39:47 +0200 | |
changeset 80636 | 4041e7c8059d |
parent 80306 | c2537860ccf8 |
child 80820 | db114ec720cb |
permissions | -rw-r--r-- |
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 |
79799
2746dfc9ceae
optional cartouche syntax and proper name printing in atp Isar output
Simon Wimmer <wimmers@in.tum.de>
parents:
77918
diff
changeset
|
9 |
val proof_cartouches: bool Config.T |
43085
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
10 |
val timestamp : unit -> string |
53514 | 11 |
val hashw : word * word -> word |
12 |
val hashw_string : string * word -> word |
|
43827
62d64709af3b
added option to control which lambda translation to use (for experiments)
blanchet
parents:
43572
diff
changeset
|
13 |
val hash_string : string -> int |
48323 | 14 |
val chunk_list : int -> 'a list -> 'a list list |
48251
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48247
diff
changeset
|
15 |
val stringN_of_int : int -> int -> string |
43085
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
16 |
val strip_spaces : bool -> (char -> bool) -> string -> string |
44784 | 17 |
val strip_spaces_except_between_idents : string -> string |
48316
252f45c04042
drastic overhaul of MaSh data structures + fixed a few performance issues
blanchet
parents:
48251
diff
changeset
|
18 |
val elide_string : int -> string -> string |
52077 | 19 |
val find_enclosed : string -> string -> string -> string list |
43085
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
20 |
val nat_subscript : int -> string |
52076
bfa28e1cba77
freeze types in Sledgehammer goal, not just terms
blanchet
parents:
52031
diff
changeset
|
21 |
val unquote_tvar : string -> string |
79799
2746dfc9ceae
optional cartouche syntax and proper name printing in atp Isar output
Simon Wimmer <wimmers@in.tum.de>
parents:
77918
diff
changeset
|
22 |
val maybe_quote : Proof.context -> string -> string |
52031
9a9238342963
tuning -- renamed '_from_' to '_of_' in Sledgehammer
blanchet
parents:
51209
diff
changeset
|
23 |
val string_of_ext_time : bool * Time.time -> string |
9a9238342963
tuning -- renamed '_from_' to '_of_' in Sledgehammer
blanchet
parents:
51209
diff
changeset
|
24 |
val string_of_time : Time.time -> string |
47150 | 25 |
val type_instance : theory -> typ -> typ -> bool |
26 |
val type_generalization : theory -> typ -> typ -> bool |
|
27 |
val type_intersect : theory -> typ -> typ -> bool |
|
28 |
val type_equiv : theory -> typ * typ -> bool |
|
43085
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
29 |
val varify_type : Proof.context -> typ -> typ |
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
30 |
val instantiate_type : theory -> typ -> typ -> typ -> typ |
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
31 |
val varify_and_instantiate_type : Proof.context -> typ -> typ -> typ -> typ |
44393 | 32 |
val is_type_surely_finite : Proof.context -> typ -> bool |
44399 | 33 |
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
|
34 |
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
|
35 |
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
|
36 |
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
|
37 |
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
|
38 |
val s_iff : term * term -> term |
49983
33e18e9916a8
use metaquantification when possible in Isar proofs
blanchet
parents:
49982
diff
changeset
|
39 |
val close_form : term -> term |
49982 | 40 |
val hol_close_form_prefix : string |
41 |
val hol_close_form : term -> term |
|
42 |
val hol_open_form : (string -> string) -> term -> term |
|
43085
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
43 |
val eta_expand : typ list -> term -> int -> term |
59632 | 44 |
val cong_extensionalize_term : Proof.context -> term -> term |
47953
a2c3706c4cb1
added "ext_cong_neq" lemma (not used yet); tuning
blanchet
parents:
47718
diff
changeset
|
45 |
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
|
46 |
val unextensionalize_def : term -> term |
43085
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
47 |
val transform_elim_prop : term -> term |
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
48 |
val specialize_type : theory -> (string * typ) -> term -> term |
56104
fd6e132ee4fb
correctly reconstruct helper facts (e.g. 'nat_int') in Isar proofs
blanchet
parents:
54768
diff
changeset
|
49 |
val strip_subgoal : thm -> int -> Proof.context -> (string * typ) list * term list * term |
57541 | 50 |
val extract_lambda_def : (term -> string * typ) -> term -> string * term |
56104
fd6e132ee4fb
correctly reconstruct helper facts (e.g. 'nat_int') in Isar proofs
blanchet
parents:
54768
diff
changeset
|
51 |
val short_thm_name : Proof.context -> thm -> string |
74328 | 52 |
val map_prod : ('a -> 'b) -> ('c -> 'd) -> 'a * 'c -> 'b * 'd |
75005
4106bc2a9cc8
optimized app_op_level selection in TPTP generation
desharna
parents:
74379
diff
changeset
|
53 |
val compare_length_with : 'a list -> int -> order |
77918
55b81d14a1b8
tuned; avoided intermediate list and list traversal
desharna
parents:
77430
diff
changeset
|
54 |
val forall2 : ('a -> 'b -> bool) -> 'a list -> 'b list -> bool |
43085
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
55 |
end; |
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
56 |
|
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
57 |
structure ATP_Util : ATP_UTIL = |
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
58 |
struct |
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
59 |
|
79799
2746dfc9ceae
optional cartouche syntax and proper name printing in atp Isar output
Simon Wimmer <wimmers@in.tum.de>
parents:
77918
diff
changeset
|
60 |
val proof_cartouches = Attrib.setup_config_bool \<^binding>\<open>atp_proof_cartouches\<close> (K false) |
2746dfc9ceae
optional cartouche syntax and proper name printing in atp Isar output
Simon Wimmer <wimmers@in.tum.de>
parents:
77918
diff
changeset
|
61 |
|
77918
55b81d14a1b8
tuned; avoided intermediate list and list traversal
desharna
parents:
77430
diff
changeset
|
62 |
fun forall2 _ [] [] = true |
55b81d14a1b8
tuned; avoided intermediate list and list traversal
desharna
parents:
77430
diff
changeset
|
63 |
| forall2 P (x :: xs) (y :: ys) = P x y andalso forall2 P xs ys |
55b81d14a1b8
tuned; avoided intermediate list and list traversal
desharna
parents:
77430
diff
changeset
|
64 |
| forall2 _ _ _ = false |
55b81d14a1b8
tuned; avoided intermediate list and list traversal
desharna
parents:
77430
diff
changeset
|
65 |
|
53800 | 66 |
fun timestamp_format time = |
67 |
Date.fmt "%Y-%m-%d %H:%M:%S." (Date.fromTimeLocal time) ^ |
|
68 |
(StringCvt.padLeft #"0" 3 (string_of_int (Time.toMilliseconds time - 1000 * Time.toSeconds time))) |
|
69 |
||
70 |
val timestamp = timestamp_format o Time.now |
|
43085
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
71 |
|
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
72 |
(* This hash function is recommended in "Compilers: Principles, Techniques, and |
77430
51dac6fcdd0e
reverted 0506c3273814 -- the message is still useful
blanchet
parents:
75125
diff
changeset
|
73 |
Tools" by Aho, Sethi, and Ullman. *) |
43085
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
74 |
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
|
75 |
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
|
76 |
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
|
77 |
fun hash_string s = Word.toInt (hashw_string (s, 0w0)) |
43085
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
78 |
|
48323 | 79 |
fun chunk_list _ [] = [] |
80 |
| chunk_list k xs = |
|
81 |
let val (xs1, xs2) = chop k xs in xs1 :: chunk_list k xs2 end |
|
82 |
||
48251
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48247
diff
changeset
|
83 |
fun stringN_of_int 0 _ = "" |
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48247
diff
changeset
|
84 |
| stringN_of_int k n = |
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48247
diff
changeset
|
85 |
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
|
86 |
|
57728
c21e7bdb40ad
remove YXML formatting when parsing backquoted facts supplied manually to Sledgehammer
blanchet
parents:
57547
diff
changeset
|
87 |
fun is_spaceish c = Char.isSpace c orelse c = #"\127" (* DEL -- no idea where these come from *) |
c21e7bdb40ad
remove YXML formatting when parsing backquoted facts supplied manually to Sledgehammer
blanchet
parents:
57547
diff
changeset
|
88 |
|
43085
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
89 |
fun strip_spaces skip_comments is_evil = |
44935
2e812384afa8
tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents:
44893
diff
changeset
|
90 |
let |
2e812384afa8
tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents:
44893
diff
changeset
|
91 |
fun strip_c_style_comment [] accum = accum |
57728
c21e7bdb40ad
remove YXML formatting when parsing backquoted facts supplied manually to Sledgehammer
blanchet
parents:
57547
diff
changeset
|
92 |
| strip_c_style_comment (#"*" :: #"/" :: cs) accum = strip_spaces_in_list true cs accum |
44935
2e812384afa8
tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents:
44893
diff
changeset
|
93 |
| strip_c_style_comment (_ :: cs) accum = strip_c_style_comment cs accum |
48766
553ad5f99968
fixed "double rev" bug that arose in situations where a % comment arose on the last line of a file without \n at the end
blanchet
parents:
48323
diff
changeset
|
94 |
and strip_spaces_in_list _ [] accum = accum |
44935
2e812384afa8
tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents:
44893
diff
changeset
|
95 |
| strip_spaces_in_list true (#"%" :: cs) accum = |
67522 | 96 |
strip_spaces_in_list true (cs |> drop_prefix (not_equal #"\n")) accum |
57728
c21e7bdb40ad
remove YXML formatting when parsing backquoted facts supplied manually to Sledgehammer
blanchet
parents:
57547
diff
changeset
|
97 |
| strip_spaces_in_list true (#"/" :: #"*" :: cs) accum = strip_c_style_comment cs accum |
c21e7bdb40ad
remove YXML formatting when parsing backquoted facts supplied manually to Sledgehammer
blanchet
parents:
57547
diff
changeset
|
98 |
| strip_spaces_in_list _ [c1] accum = accum |> not (is_spaceish c1) ? cons c1 |
44935
2e812384afa8
tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents:
44893
diff
changeset
|
99 |
| strip_spaces_in_list skip_comments (cs as [_, _]) accum = |
2e812384afa8
tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents:
44893
diff
changeset
|
100 |
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
|
101 |
| strip_spaces_in_list skip_comments (c1 :: c2 :: c3 :: cs) accum = |
57728
c21e7bdb40ad
remove YXML formatting when parsing backquoted facts supplied manually to Sledgehammer
blanchet
parents:
57547
diff
changeset
|
102 |
if is_spaceish c1 then |
44935
2e812384afa8
tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents:
44893
diff
changeset
|
103 |
strip_spaces_in_list skip_comments (c2 :: c3 :: cs) accum |
57728
c21e7bdb40ad
remove YXML formatting when parsing backquoted facts supplied manually to Sledgehammer
blanchet
parents:
57547
diff
changeset
|
104 |
else if is_spaceish c2 then |
c21e7bdb40ad
remove YXML formatting when parsing backquoted facts supplied manually to Sledgehammer
blanchet
parents:
57547
diff
changeset
|
105 |
if is_spaceish c3 then |
44935
2e812384afa8
tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents:
44893
diff
changeset
|
106 |
strip_spaces_in_list skip_comments (c1 :: c3 :: cs) accum |
2e812384afa8
tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents:
44893
diff
changeset
|
107 |
else |
2e812384afa8
tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents:
44893
diff
changeset
|
108 |
strip_spaces_in_list skip_comments (c3 :: cs) |
57728
c21e7bdb40ad
remove YXML formatting when parsing backquoted facts supplied manually to Sledgehammer
blanchet
parents:
57547
diff
changeset
|
109 |
(c1 :: accum |> forall is_evil [c1, c3] ? cons #" ") |
44935
2e812384afa8
tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents:
44893
diff
changeset
|
110 |
else |
2e812384afa8
tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents:
44893
diff
changeset
|
111 |
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
|
112 |
in |
2e812384afa8
tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents:
44893
diff
changeset
|
113 |
String.explode |
2e812384afa8
tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents:
44893
diff
changeset
|
114 |
#> rpair [] #-> strip_spaces_in_list skip_comments |
2e812384afa8
tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents:
44893
diff
changeset
|
115 |
#> rev #> String.implode |
2e812384afa8
tail recursive proof preprocessing (needed for huge proofs)
blanchet
parents:
44893
diff
changeset
|
116 |
end |
43085
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
117 |
|
44784 | 118 |
fun is_ident_char c = Char.isAlphaNum c orelse c = #"_" |
119 |
val strip_spaces_except_between_idents = strip_spaces true is_ident_char |
|
120 |
||
48316
252f45c04042
drastic overhaul of MaSh data structures + fixed a few performance issues
blanchet
parents:
48251
diff
changeset
|
121 |
fun elide_string threshold s = |
252f45c04042
drastic overhaul of MaSh data structures + fixed a few performance issues
blanchet
parents:
48251
diff
changeset
|
122 |
if size s > threshold then |
252f45c04042
drastic overhaul of MaSh data structures + fixed a few performance issues
blanchet
parents:
48251
diff
changeset
|
123 |
String.extract (s, 0, SOME (threshold div 2 - 5)) ^ " ...... " ^ |
252f45c04042
drastic overhaul of MaSh data structures + fixed a few performance issues
blanchet
parents:
48251
diff
changeset
|
124 |
String.extract (s, size s - (threshold + 1) div 2 + 6, NONE) |
252f45c04042
drastic overhaul of MaSh data structures + fixed a few performance issues
blanchet
parents:
48251
diff
changeset
|
125 |
else |
252f45c04042
drastic overhaul of MaSh data structures + fixed a few performance issues
blanchet
parents:
48251
diff
changeset
|
126 |
s |
252f45c04042
drastic overhaul of MaSh data structures + fixed a few performance issues
blanchet
parents:
48251
diff
changeset
|
127 |
|
52077 | 128 |
fun find_enclosed left right s = |
129 |
case first_field left s of |
|
130 |
SOME (_, s) => |
|
131 |
(case first_field right s of |
|
132 |
SOME (enclosed, s) => enclosed :: find_enclosed left right s |
|
133 |
| NONE => []) |
|
134 |
| NONE => [] |
|
135 |
||
53015
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents:
52196
diff
changeset
|
136 |
val subscript = implode o map (prefix "\<^sub>") o raw_explode (* FIXME Symbol.explode (?) *) |
43085
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
137 |
fun nat_subscript n = |
66020 | 138 |
n |> string_of_int |> not (print_mode_active Print_Mode.ASCII) ? subscript |
43085
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
139 |
|
52076
bfa28e1cba77
freeze types in Sledgehammer goal, not just terms
blanchet
parents:
52031
diff
changeset
|
140 |
val unquote_tvar = perhaps (try (unprefix "'")) |
bfa28e1cba77
freeze types in Sledgehammer goal, not just terms
blanchet
parents:
52031
diff
changeset
|
141 |
val unquery_var = perhaps (try (unprefix "?")) |
bfa28e1cba77
freeze types in Sledgehammer goal, not just terms
blanchet
parents:
52031
diff
changeset
|
142 |
|
50239 | 143 |
val is_long_identifier = forall Symbol_Pos.is_identifier o Long_Name.explode |
79799
2746dfc9ceae
optional cartouche syntax and proper name printing in atp Isar output
Simon Wimmer <wimmers@in.tum.de>
parents:
77918
diff
changeset
|
144 |
fun maybe_quote ctxt y = |
2746dfc9ceae
optional cartouche syntax and proper name printing in atp Isar output
Simon Wimmer <wimmers@in.tum.de>
parents:
77918
diff
changeset
|
145 |
let |
2746dfc9ceae
optional cartouche syntax and proper name printing in atp Isar output
Simon Wimmer <wimmers@in.tum.de>
parents:
77918
diff
changeset
|
146 |
val s = YXML.content_of y |
79825 | 147 |
val is_literal = Keyword.is_literal (Thy_Header.get_keywords' ctxt) |
148 |
val q = if Config.get ctxt proof_cartouches then cartouche else quote |
|
79799
2746dfc9ceae
optional cartouche syntax and proper name printing in atp Isar output
Simon Wimmer <wimmers@in.tum.de>
parents:
77918
diff
changeset
|
149 |
in |
52076
bfa28e1cba77
freeze types in Sledgehammer goal, not just terms
blanchet
parents:
52031
diff
changeset
|
150 |
y |> ((not (is_long_identifier (unquote_tvar s)) andalso |
bfa28e1cba77
freeze types in Sledgehammer goal, not just terms
blanchet
parents:
52031
diff
changeset
|
151 |
not (is_long_identifier (unquery_var s))) orelse |
79825 | 152 |
is_literal s) ? q |
43085
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
153 |
end |
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
154 |
|
52031
9a9238342963
tuning -- renamed '_from_' to '_of_' in Sledgehammer
blanchet
parents:
51209
diff
changeset
|
155 |
fun string_of_ext_time (plus, time) = |
58081
aa239fee063a
show microseconds as well (useful when playing with Isar proofs)
blanchet
parents:
57728
diff
changeset
|
156 |
let val us = Time.toMicroseconds time in |
43085
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
157 |
(if plus then "> " else "") ^ |
58088 | 158 |
(if us < 1000 then string_of_real (Real.fromInt (us div 100) / 10.0) ^ " ms" |
58081
aa239fee063a
show microseconds as well (useful when playing with Isar proofs)
blanchet
parents:
57728
diff
changeset
|
159 |
else if us < 1000000 then signed_string_of_int (us div 1000) ^ " ms" |
58088 | 160 |
else string_of_real (Real.fromInt (us div 100000) / 10.0) ^ " s") |
43085
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
161 |
end |
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
162 |
|
52031
9a9238342963
tuning -- renamed '_from_' to '_of_' in Sledgehammer
blanchet
parents:
51209
diff
changeset
|
163 |
val string_of_time = string_of_ext_time o pair false |
43085
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
164 |
|
47150 | 165 |
fun type_instance thy T T' = Sign.typ_instance thy (T, T') |
166 |
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
|
167 |
|
8f37d2ddabc8
optimized type intersection, hoping this will reduce the number of sudden Interrupts in the "incr_tvar" code
blanchet
parents:
48238
diff
changeset
|
168 |
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
|
169 |
| 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
|
170 |
| 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
|
171 |
let |
8f37d2ddabc8
optimized type intersection, hoping this will reduce the number of sudden Interrupts in the "incr_tvar" code
blanchet
parents:
48238
diff
changeset
|
172 |
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
|
173 |
val tvars' = Term.add_tvar_namesT T' [] |
50968
3686bc0d4df2
pass correct index to "Sign.typ_unify" -- this is important to avoid what appears to be an infinite loop in the unifier
blanchet
parents:
50239
diff
changeset
|
174 |
val maxidx' = maxidx_of_typ 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
|
175 |
val T = |
50968
3686bc0d4df2
pass correct index to "Sign.typ_unify" -- this is important to avoid what appears to be an infinite loop in the unifier
blanchet
parents:
50239
diff
changeset
|
176 |
T |> exists (member (op =) tvars') tvars ? Logic.incr_tvar (maxidx' + 1) |
3686bc0d4df2
pass correct index to "Sign.typ_unify" -- this is important to avoid what appears to be an infinite loop in the unifier
blanchet
parents:
50239
diff
changeset
|
177 |
val maxidx = Integer.max (maxidx_of_typ T) maxidx' |
3686bc0d4df2
pass correct index to "Sign.typ_unify" -- this is important to avoid what appears to be an infinite loop in the unifier
blanchet
parents:
50239
diff
changeset
|
178 |
in can (Sign.typ_unify thy (T, T')) (Vartab.empty, maxidx) end |
48247
8f37d2ddabc8
optimized type intersection, hoping this will reduce the number of sudden Interrupts in the "incr_tvar" code
blanchet
parents:
48238
diff
changeset
|
179 |
|
47150 | 180 |
val type_equiv = Sign.typ_equiv |
44399 | 181 |
|
43085
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
182 |
fun varify_type ctxt T = |
69593 | 183 |
Variable.polymorphic_types ctxt [Const (\<^const_name>\<open>undefined\<close>, T)] |
80636
4041e7c8059d
tuned: more explicit dest_Const_name and dest_Const_type;
wenzelm
parents:
80306
diff
changeset
|
184 |
|> snd |> the_single |> dest_Const_type |
43085
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
185 |
|
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
186 |
(* TODO: use "Term_Subst.instantiateT" instead? *) |
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
187 |
fun instantiate_type thy T1 T1' T2 = |
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
188 |
Same.commit (Envir.subst_type_same |
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
189 |
(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
|
190 |
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
|
191 |
|
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
192 |
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
|
193 |
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
|
194 |
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
|
195 |
end |
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
196 |
|
54554
b8d0d8407c3b
eliminated Sledgehammer's dependency on old-style datatypes
blanchet
parents:
53800
diff
changeset
|
197 |
fun free_constructors_of ctxt (Type (s, Ts)) = |
b8d0d8407c3b
eliminated Sledgehammer's dependency on old-style datatypes
blanchet
parents:
53800
diff
changeset
|
198 |
(case Ctr_Sugar.ctr_sugar_of ctxt s of |
b8d0d8407c3b
eliminated Sledgehammer's dependency on old-style datatypes
blanchet
parents:
53800
diff
changeset
|
199 |
SOME {ctrs, ...} => map_filter (try dest_Const o Ctr_Sugar.mk_ctr Ts) ctrs |
b8d0d8407c3b
eliminated Sledgehammer's dependency on old-style datatypes
blanchet
parents:
53800
diff
changeset
|
200 |
| NONE => []) |
b8d0d8407c3b
eliminated Sledgehammer's dependency on old-style datatypes
blanchet
parents:
53800
diff
changeset
|
201 |
| free_constructors_of _ _ = [] |
43085
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
202 |
|
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
203 |
(* 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
|
204 |
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
|
205 |
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
|
206 |
cardinality of the type can't be determined. *) |
44500 | 207 |
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
|
208 |
let |
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
209 |
val thy = Proof_Context.theory_of ctxt |
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
210 |
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
|
211 |
fun aux slack avoid T = |
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
212 |
if member (op =) avoid T then |
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
213 |
0 |
47150 | 214 |
else case AList.lookup (type_equiv thy) assigns T of |
44393 | 215 |
SOME k => k |
216 |
| NONE => |
|
44392
6750b4297691
reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents:
43864
diff
changeset
|
217 |
case T of |
69593 | 218 |
Type (\<^type_name>\<open>fun\<close>, [T1, T2]) => |
44392
6750b4297691
reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents:
43864
diff
changeset
|
219 |
(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
|
220 |
(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
|
221 |
| (0, _) => 0 |
6750b4297691
reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents:
43864
diff
changeset
|
222 |
| (_, 0) => 0 |
6750b4297691
reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents:
43864
diff
changeset
|
223 |
| (k1, k2) => |
6750b4297691
reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents:
43864
diff
changeset
|
224 |
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
|
225 |
else Int.min (max, Integer.pow k2 k1)) |
69593 | 226 |
| Type (\<^type_name>\<open>set\<close>, [T']) => aux slack avoid (T' --> \<^typ>\<open>bool\<close>) |
227 |
| \<^typ>\<open>prop\<close> => 2 |
|
228 |
| \<^typ>\<open>bool\<close> => 2 (* optimization *) |
|
229 |
| \<^typ>\<open>nat\<close> => 0 (* optimization *) |
|
44392
6750b4297691
reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents:
43864
diff
changeset
|
230 |
| 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
|
231 |
| Type (s, _) => |
54554
b8d0d8407c3b
eliminated Sledgehammer's dependency on old-style datatypes
blanchet
parents:
53800
diff
changeset
|
232 |
(case free_constructors_of ctxt T of |
44392
6750b4297691
reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents:
43864
diff
changeset
|
233 |
constrs as _ :: _ => |
6750b4297691
reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents:
43864
diff
changeset
|
234 |
let |
6750b4297691
reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents:
43864
diff
changeset
|
235 |
val constr_cards = |
54554
b8d0d8407c3b
eliminated Sledgehammer's dependency on old-style datatypes
blanchet
parents:
53800
diff
changeset
|
236 |
map (Integer.prod o map (aux slack (T :: avoid)) o binder_types o snd) constrs |
44392
6750b4297691
reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents:
43864
diff
changeset
|
237 |
in |
6750b4297691
reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents:
43864
diff
changeset
|
238 |
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
|
239 |
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
|
240 |
end |
6750b4297691
reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents:
43864
diff
changeset
|
241 |
| [] => |
6750b4297691
reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents:
43864
diff
changeset
|
242 |
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
|
243 |
({abs_type, rep_type, ...}, _) :: _ => |
45299
ee584ff987c3
check "sound" flag before doing something unsound...
blanchet
parents:
44935
diff
changeset
|
244 |
if not sound then |
ee584ff987c3
check "sound" flag before doing something unsound...
blanchet
parents:
44935
diff
changeset
|
245 |
(* We cheat here by assuming that typedef types are infinite if |
ee584ff987c3
check "sound" flag before doing something unsound...
blanchet
parents:
44935
diff
changeset
|
246 |
their underlying type is infinite. This is unsound in |
ee584ff987c3
check "sound" flag before doing something unsound...
blanchet
parents:
44935
diff
changeset
|
247 |
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
|
248 |
this would not be the case. We are also slack with |
ee584ff987c3
check "sound" flag before doing something unsound...
blanchet
parents:
44935
diff
changeset
|
249 |
representation types: If a representation type has the form |
ee584ff987c3
check "sound" flag before doing something unsound...
blanchet
parents:
44935
diff
changeset
|
250 |
"sigma => tau", we consider it enough to check "sigma" for |
ee584ff987c3
check "sound" flag before doing something unsound...
blanchet
parents:
44935
diff
changeset
|
251 |
infiniteness. *) |
ee584ff987c3
check "sound" flag before doing something unsound...
blanchet
parents:
44935
diff
changeset
|
252 |
(case varify_and_instantiate_type ctxt |
ee584ff987c3
check "sound" flag before doing something unsound...
blanchet
parents:
44935
diff
changeset
|
253 |
(Logic.varifyT_global abs_type) T |
ee584ff987c3
check "sound" flag before doing something unsound...
blanchet
parents:
44935
diff
changeset
|
254 |
(Logic.varifyT_global rep_type) |
ee584ff987c3
check "sound" flag before doing something unsound...
blanchet
parents:
44935
diff
changeset
|
255 |
|> aux true avoid of |
ee584ff987c3
check "sound" flag before doing something unsound...
blanchet
parents:
44935
diff
changeset
|
256 |
0 => 0 |
ee584ff987c3
check "sound" flag before doing something unsound...
blanchet
parents:
44935
diff
changeset
|
257 |
| 1 => 1 |
ee584ff987c3
check "sound" flag before doing something unsound...
blanchet
parents:
44935
diff
changeset
|
258 |
| _ => default_card) |
ee584ff987c3
check "sound" flag before doing something unsound...
blanchet
parents:
44935
diff
changeset
|
259 |
else |
ee584ff987c3
check "sound" flag before doing something unsound...
blanchet
parents:
44935
diff
changeset
|
260 |
default_card |
44392
6750b4297691
reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents:
43864
diff
changeset
|
261 |
| [] => default_card) |
54554
b8d0d8407c3b
eliminated Sledgehammer's dependency on old-style datatypes
blanchet
parents:
53800
diff
changeset
|
262 |
| TFree _ => |
44392
6750b4297691
reintroduced slightly unsound optimization taken out in 717880e98e6b, but only if "sound" is false
blanchet
parents:
43864
diff
changeset
|
263 |
(* 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
|
264 |
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
|
265 |
likely have used "unit" directly anyway.) *) |
44500 | 266 |
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
|
267 |
| TVar _ => default_card |
43085
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
268 |
in Int.min (max, aux false [] T) end |
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
269 |
|
44500 | 270 |
fun is_type_surely_finite ctxt T = tiny_card_of_type ctxt true [] 0 T <> 0 |
271 |
fun is_type_surely_infinite ctxt sound infinite_Ts T = |
|
272 |
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
|
273 |
|
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
|
274 |
(* 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
|
275 |
spurious "True"s. *) |
74379 | 276 |
fun s_not \<^Const_>\<open>All T for \<open>Abs (s, T', t')\<close>\<close> = \<^Const>\<open>Ex T for \<open>Abs (s, T', s_not t')\<close>\<close> |
277 |
| s_not \<^Const_>\<open>Ex T for \<open>Abs (s, T', t')\<close>\<close> = \<^Const>\<open>All T for \<open>Abs (s, T', s_not t')\<close>\<close> |
|
278 |
| s_not \<^Const_>\<open>implies for t1 t2\<close> = \<^Const>\<open>conj for t1 \<open>s_not t2\<close>\<close> |
|
279 |
| s_not \<^Const_>\<open>conj for t1 t2\<close> = \<^Const>\<open>disj for \<open>s_not t1\<close> \<open>s_not t2\<close>\<close> |
|
280 |
| s_not \<^Const_>\<open>disj for t1 t2\<close> = \<^Const>\<open>conj for \<open>s_not t1\<close> \<open>s_not t2\<close>\<close> |
|
281 |
| s_not \<^Const_>\<open>False\<close> = \<^Const>\<open>True\<close> |
|
282 |
| s_not \<^Const_>\<open>True\<close> = \<^Const>\<open>False\<close> |
|
283 |
| s_not \<^Const_>\<open>Not for t\<close> = t |
|
284 |
| s_not t = \<^Const>\<open>Not for t\<close> |
|
54758 | 285 |
|
74379 | 286 |
fun s_conj (\<^Const_>\<open>True\<close>, t2) = t2 |
287 |
| s_conj (t1, \<^Const_>\<open>True\<close>) = t1 |
|
288 |
| s_conj (\<^Const_>\<open>False\<close>, _) = \<^Const>\<open>False\<close> |
|
289 |
| s_conj (_, \<^Const_>\<open>False\<close>) = \<^Const>\<open>False\<close> |
|
51197 | 290 |
| s_conj (t1, t2) = if t1 aconv t2 then t1 else HOLogic.mk_conj (t1, t2) |
54758 | 291 |
|
74379 | 292 |
fun s_disj (\<^Const_>\<open>False\<close>, t2) = t2 |
293 |
| s_disj (t1, \<^Const_>\<open>False\<close>) = t1 |
|
294 |
| s_disj (\<^Const_>\<open>True\<close>, _) = \<^Const>\<open>True\<close> |
|
295 |
| s_disj (_, \<^Const_>\<open>True\<close>) = \<^Const>\<open>True\<close> |
|
51197 | 296 |
| s_disj (t1, t2) = if t1 aconv t2 then t1 else HOLogic.mk_disj (t1, t2) |
54757
4960647932ec
use 'prop' rather than 'bool' systematically in Isar reconstruction code
blanchet
parents:
54554
diff
changeset
|
297 |
|
74379 | 298 |
fun s_imp (\<^Const_>\<open>True\<close>, t2) = t2 |
299 |
| s_imp (t1, \<^Const_>\<open>False\<close>) = s_not t1 |
|
300 |
| s_imp (\<^Const_>\<open>False\<close>, _) = \<^Const>\<open>True\<close> |
|
301 |
| s_imp (_, \<^Const_>\<open>True\<close>) = \<^Const>\<open>True\<close> |
|
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
|
302 |
| s_imp p = HOLogic.mk_imp p |
54757
4960647932ec
use 'prop' rather than 'bool' systematically in Isar reconstruction code
blanchet
parents:
54554
diff
changeset
|
303 |
|
74379 | 304 |
fun s_iff (\<^Const_>\<open>True\<close>, t2) = t2 |
305 |
| s_iff (t1, \<^Const_>\<open>True\<close>) = t1 |
|
306 |
| s_iff (\<^Const_>\<open>False\<close>, t2) = s_not t2 |
|
307 |
| s_iff (t1, \<^Const_>\<open>False\<close>) = s_not t1 |
|
308 |
| s_iff (t1, t2) = if t1 aconv t2 then \<^Const>\<open>True\<close> else HOLogic.eq_const HOLogic.boolT $ t1 $ t2 |
|
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
|
309 |
|
49983
33e18e9916a8
use metaquantification when possible in Isar proofs
blanchet
parents:
49982
diff
changeset
|
310 |
fun close_form t = |
33e18e9916a8
use metaquantification when possible in Isar proofs
blanchet
parents:
49982
diff
changeset
|
311 |
fold (fn ((s, i), T) => fn t' => |
54757
4960647932ec
use 'prop' rather than 'bool' systematically in Isar reconstruction code
blanchet
parents:
54554
diff
changeset
|
312 |
Logic.all_const T $ Abs (s, T, abstract_over (Var ((s, i), T), t'))) |
4960647932ec
use 'prop' rather than 'bool' systematically in Isar reconstruction code
blanchet
parents:
54554
diff
changeset
|
313 |
(Term.add_vars t []) t |
49983
33e18e9916a8
use metaquantification when possible in Isar proofs
blanchet
parents:
49982
diff
changeset
|
314 |
|
58091 | 315 |
val hol_close_form_prefix = "ATP." |
46385
0ccf458a3633
third attempt at lambda lifting that works for both Sledgehammer and Metis (cf. dce6c3a460a9)
blanchet
parents:
45896
diff
changeset
|
316 |
|
49982 | 317 |
fun hol_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
|
318 |
fold (fn ((s, i), T) => fn t' => |
45511
9b0f8ca4388e
continued implementation of lambda-lifting in Metis
blanchet
parents:
45299
diff
changeset
|
319 |
HOLogic.all_const T |
49982 | 320 |
$ Abs (hol_close_form_prefix ^ s, T, |
46385
0ccf458a3633
third attempt at lambda lifting that works for both Sledgehammer and Metis (cf. dce6c3a460a9)
blanchet
parents:
45896
diff
changeset
|
321 |
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
|
322 |
(Term.add_vars t []) t |
58a7b3fdc193
fixed lambda-liftg: must ensure the formulas are in close form
blanchet
parents:
43863
diff
changeset
|
323 |
|
49982 | 324 |
fun hol_open_form unprefix |
69593 | 325 |
(t as Const (\<^const_name>\<open>All\<close>, _) $ Abs (s, T, t')) = |
47718
39229c760636
smoother handling of conjecture, so that its Skolem constants get displayed in countermodels
blanchet
parents:
47715
diff
changeset
|
326 |
(case try unprefix s of |
39229c760636
smoother handling of conjecture, so that its Skolem constants get displayed in countermodels
blanchet
parents:
47715
diff
changeset
|
327 |
SOME s => |
39229c760636
smoother handling of conjecture, so that its Skolem constants get displayed in countermodels
blanchet
parents:
47715
diff
changeset
|
328 |
let |
39229c760636
smoother handling of conjecture, so that its Skolem constants get displayed in countermodels
blanchet
parents:
47715
diff
changeset
|
329 |
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
|
330 |
val (s, _) = Name.variant s names |
49982 | 331 |
in hol_open_form unprefix (subst_bound (Var ((s, 0), T), t')) end |
47718
39229c760636
smoother handling of conjecture, so that its Skolem constants get displayed in countermodels
blanchet
parents:
47715
diff
changeset
|
332 |
| NONE => t) |
49982 | 333 |
| hol_open_form _ t = t |
47718
39229c760636
smoother handling of conjecture, so that its Skolem constants get displayed in countermodels
blanchet
parents:
47715
diff
changeset
|
334 |
|
43085
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
335 |
fun eta_expand _ t 0 = t |
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
336 |
| eta_expand Ts (Abs (s, T, t')) n = |
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
337 |
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
|
338 |
| eta_expand Ts t n = |
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
339 |
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
|
340 |
(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
|
341 |
(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
|
342 |
|
59632 | 343 |
fun cong_extensionalize_term ctxt t = |
69593 | 344 |
if exists_Const (fn (s, _) => s = \<^const_name>\<open>Not\<close>) t then |
59632 | 345 |
t |> Skip_Proof.make_thm (Proof_Context.theory_of ctxt) |
346 |
|> Meson.cong_extensionalize_thm ctxt |
|
59582 | 347 |
|> Thm.prop_of |
47954
aada9fd08b58
make higher-order goals more first-order via extensionality
blanchet
parents:
47953
diff
changeset
|
348 |
else |
aada9fd08b58
make higher-order goals more first-order via extensionality
blanchet
parents:
47953
diff
changeset
|
349 |
t |
aada9fd08b58
make higher-order goals more first-order via extensionality
blanchet
parents:
47953
diff
changeset
|
350 |
|
69593 | 351 |
fun is_fun_equality (\<^const_name>\<open>HOL.eq\<close>, |
352 |
Type (_, [Type (\<^type_name>\<open>fun\<close>, _), _])) = true |
|
47715
04400144c6fc
handle TPTP definitions as definitions in Nitpick rather than as axioms
blanchet
parents:
47150
diff
changeset
|
353 |
| is_fun_equality _ = false |
04400144c6fc
handle TPTP definitions as definitions in Nitpick rather than as axioms
blanchet
parents:
47150
diff
changeset
|
354 |
|
47953
a2c3706c4cb1
added "ext_cong_neq" lemma (not used yet); tuning
blanchet
parents:
47718
diff
changeset
|
355 |
fun abs_extensionalize_term ctxt t = |
47715
04400144c6fc
handle TPTP definitions as definitions in Nitpick rather than as axioms
blanchet
parents:
47150
diff
changeset
|
356 |
if exists_Const is_fun_equality t then |
59632 | 357 |
t |> Thm.cterm_of ctxt |> Meson.abs_extensionalize_conv ctxt |
358 |
|> Thm.prop_of |> Logic.dest_equals |> snd |
|
47715
04400144c6fc
handle TPTP definitions as definitions in Nitpick rather than as axioms
blanchet
parents:
47150
diff
changeset
|
359 |
else |
04400144c6fc
handle TPTP definitions as definitions in Nitpick rather than as axioms
blanchet
parents:
47150
diff
changeset
|
360 |
t |
04400144c6fc
handle TPTP definitions as definitions in Nitpick rather than as axioms
blanchet
parents:
47150
diff
changeset
|
361 |
|
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
|
362 |
fun unextensionalize_def t = |
74379 | 363 |
(case t of |
364 |
\<^Const_>\<open>Trueprop for \<^Const_>\<open>HOL.eq _ for lhs rhs\<close>\<close> => |
|
365 |
(case strip_comb lhs of |
|
366 |
(c as Const (_, T), args) => |
|
367 |
if forall is_Var args andalso not (has_duplicates (op =) args) then |
|
368 |
\<^Const>\<open>Trueprop for \<^Const>\<open>HOL.eq T for c \<open>fold_rev lambda args rhs\<close>\<close>\<close> |
|
369 |
else t |
|
370 |
| _ => t) |
|
371 |
| _ => t) |
|
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
|
372 |
|
43085
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
373 |
(* 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
|
374 |
predicate variable. Leaves other theorems unchanged. We simply instantiate |
44460 | 375 |
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
|
376 |
"Meson_Clausify".) *) |
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
377 |
fun transform_elim_prop t = |
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
378 |
case Logic.strip_imp_concl t of |
74379 | 379 |
\<^Const_>\<open>Trueprop for \<open>Var (z, \<^typ>\<open>bool\<close>)\<close>\<close> => subst_Vars [(z, \<^Const>\<open>False\<close>)] t |
380 |
| Var (z, \<^Type>\<open>prop\<close>) => subst_Vars [(z, \<^prop>\<open>False\<close>)] t |
|
43085
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
381 |
| _ => t |
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
382 |
|
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
383 |
fun specialize_type thy (s, T) t = |
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
384 |
let |
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
385 |
fun subst_for (Const (s', T')) = |
61769 | 386 |
if s = s' then |
387 |
SOME (Sign.typ_match thy (T', T) Vartab.empty) |
|
388 |
handle Type.TYPE_MATCH => NONE |
|
389 |
else |
|
390 |
NONE |
|
391 |
| subst_for (t1 $ t2) = (case subst_for t1 of SOME x => SOME x | NONE => subst_for t2) |
|
392 |
| subst_for (Abs (_, _, t')) = subst_for t' |
|
393 |
| subst_for _ = NONE |
|
43085
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
394 |
in |
61769 | 395 |
(case subst_for t of |
61770 | 396 |
SOME subst => Envir.subst_term_types subst t |
61769 | 397 |
| NONE => raise Type.TYPE_MATCH) |
43085
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
398 |
end |
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
399 |
|
52125
ac7830871177
improved handling of free variables' types in Isar proofs
blanchet
parents:
52077
diff
changeset
|
400 |
fun strip_subgoal goal i ctxt = |
43085
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
401 |
let |
52196
2281f33e8da6
redid rac7830871177 to avoid duplicate fixed variable (e.g. lemma "P (a::nat)" proof - have "!!a::int. Q a" sledgehammer [e])
blanchet
parents:
52125
diff
changeset
|
402 |
val (t, (frees, params)) = |
59582 | 403 |
Logic.goal_params (Thm.prop_of goal) i |
52196
2281f33e8da6
redid rac7830871177 to avoid duplicate fixed variable (e.g. lemma "P (a::nat)" proof - have "!!a::int. Q a" sledgehammer [e])
blanchet
parents:
52125
diff
changeset
|
404 |
||> (map dest_Free #> Variable.variant_frees ctxt [] #> `(map Free)) |
43085
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
405 |
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
|
406 |
val concl_t = t |> Logic.strip_assums_concl |> curry subst_bounds frees |
52196
2281f33e8da6
redid rac7830871177 to avoid duplicate fixed variable (e.g. lemma "P (a::nat)" proof - have "!!a::int. Q a" sledgehammer [e])
blanchet
parents:
52125
diff
changeset
|
407 |
in (rev params, hyp_ts, concl_t) end |
43085
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
408 |
|
69593 | 409 |
fun extract_lambda_def dest_head (Const (\<^const_name>\<open>HOL.eq\<close>, _) $ t $ u) = |
57541 | 410 |
let val (head, args) = strip_comb t in |
411 |
(head |> dest_head |> fst, |
|
412 |
fold_rev (fn t as Var ((s, _), T) => |
|
413 |
(fn u => Abs (s, T, abstract_over (t, u))) |
|
414 |
| _ => raise Fail "expected \"Var\"") args u) |
|
415 |
end |
|
416 |
| extract_lambda_def _ _ = raise Fail "malformed lifted lambda" |
|
417 |
||
56104
fd6e132ee4fb
correctly reconstruct helper facts (e.g. 'nat_int') in Isar proofs
blanchet
parents:
54768
diff
changeset
|
418 |
fun short_thm_name ctxt th = |
fd6e132ee4fb
correctly reconstruct helper facts (e.g. 'nat_int') in Isar proofs
blanchet
parents:
54768
diff
changeset
|
419 |
let |
80306 | 420 |
val long = Thm_Name.short (Thm.get_name_hint th) |
56104
fd6e132ee4fb
correctly reconstruct helper facts (e.g. 'nat_int') in Isar proofs
blanchet
parents:
54768
diff
changeset
|
421 |
val short = Long_Name.base_name long |
fd6e132ee4fb
correctly reconstruct helper facts (e.g. 'nat_int') in Isar proofs
blanchet
parents:
54768
diff
changeset
|
422 |
in |
75125 | 423 |
(case try (singleton (Attrib.eval_thms ctxt)) (Facts.named short, []) of |
424 |
SOME th' => if Thm.eq_thm_prop (th, th') then short else long |
|
425 |
| _ => long) |
|
56104
fd6e132ee4fb
correctly reconstruct helper facts (e.g. 'nat_int') in Isar proofs
blanchet
parents:
54768
diff
changeset
|
426 |
end |
fd6e132ee4fb
correctly reconstruct helper facts (e.g. 'nat_int') in Isar proofs
blanchet
parents:
54768
diff
changeset
|
427 |
|
75005
4106bc2a9cc8
optimized app_op_level selection in TPTP generation
desharna
parents:
74379
diff
changeset
|
428 |
val map_prod = Ctr_Sugar_Util.map_prod |
4106bc2a9cc8
optimized app_op_level selection in TPTP generation
desharna
parents:
74379
diff
changeset
|
429 |
|
77430
51dac6fcdd0e
reverted 0506c3273814 -- the message is still useful
blanchet
parents:
75125
diff
changeset
|
430 |
(* Compare the length of a list with an integer n while traversing at most n |
51dac6fcdd0e
reverted 0506c3273814 -- the message is still useful
blanchet
parents:
75125
diff
changeset
|
431 |
elements of the list. *) |
75005
4106bc2a9cc8
optimized app_op_level selection in TPTP generation
desharna
parents:
74379
diff
changeset
|
432 |
fun compare_length_with [] n = if n < 0 then GREATER else if n = 0 then EQUAL else LESS |
4106bc2a9cc8
optimized app_op_level selection in TPTP generation
desharna
parents:
74379
diff
changeset
|
433 |
| compare_length_with (_ :: xs) n = if n <= 0 then GREATER else compare_length_with xs (n - 1) |
74328 | 434 |
|
43085
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
blanchet
parents:
diff
changeset
|
435 |
end; |