/
eqelim.ml
268 lines (205 loc) · 9.41 KB
/
eqelim.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
(* ========================================================================= *)
(* Equality elimination including Brand transformation and relatives. *)
(* *)
(* Copyright (c) 2003-2007, John Harrison. (See "LICENSE.txt" for details.) *)
(* ========================================================================= *)
START_INTERACTIVE;;
(* ------------------------------------------------------------------------- *)
(* The x^2 = 1 implies Abelian problem. *)
(* ------------------------------------------------------------------------- *)
meson
<<(forall x. P(1,x,x)) /\
(forall x. P(x,x,1)) /\
(forall u v w x y z. P(x,y,u) /\ P(y,z,w)
==> (P(x,w,v) <=> P(u,z,v)))
==> forall a b c. P(a,b,c) ==> P(b,a,c)>>;;
(* ------------------------------------------------------------------------- *)
(* Lemma for equivalence elimination. *)
(* ------------------------------------------------------------------------- *)
meson
<<(forall x. R(x,x)) /\
(forall x y. R(x,y) ==> R(y,x)) /\
(forall x y z. R(x,y) /\ R(y,z) ==> R(x,z))
<=> (forall x y. R(x,y) <=> (forall z. R(x,z) <=> R(y,z)))>>;;
END_INTERACTIVE;;
(* ------------------------------------------------------------------------- *)
(* Brand's S and T modifications on clauses. *)
(* ------------------------------------------------------------------------- *)
let rec modify_S cl =
try let (s,t) = tryfind dest_eq cl in
let eq1 = mk_eq s t and eq2 = mk_eq t s in
let sub = modify_S (subtract cl [eq1]) in
map (insert eq1) sub @ map (insert eq2) sub
with Failure _ -> [cl];;
let rec modify_T cl =
match cl with
(Atom(R("=",[s;t])) as eq)::ps ->
let ps' = modify_T ps in
let w = Var(variant "w" (itlist (union ** fv) ps' (fv eq))) in
Not(mk_eq t w)::(mk_eq s w)::ps'
| p::ps -> p::(modify_T ps)
| [] -> [];;
(* ------------------------------------------------------------------------- *)
(* Finding nested non-variable subterms. *)
(* ------------------------------------------------------------------------- *)
let is_nonvar = function (Var x) -> false | _ -> true;;
let find_nestnonvar tm =
match tm with
Var x -> failwith "findnvsubt"
| Fn(f,args) -> find is_nonvar args;;
let rec find_nvsubterm fm =
match fm with
Atom(R("=",[s;t])) -> tryfind find_nestnonvar [s;t]
| Atom(R(p,args)) -> find is_nonvar args
| Not p -> find_nvsubterm p;;
(* ------------------------------------------------------------------------- *)
(* Replacement (substitution for non-variable) in term and literal. *)
(* ------------------------------------------------------------------------- *)
let rec replacet rfn tm =
try apply rfn tm with Failure _ ->
match tm with
Fn(f,args) -> Fn(f,map (replacet rfn) args)
| _ -> tm;;
let replace rfn = onformula (replacet rfn);;
(* ------------------------------------------------------------------------- *)
(* E-modification of a clause. *)
(* ------------------------------------------------------------------------- *)
let rec emodify fvs cls =
try let t = tryfind find_nvsubterm cls in
let w = variant "w" fvs in
let cls' = map (replace (t |=> Var w)) cls in
emodify (w::fvs) (Not(mk_eq t (Var w))::cls')
with Failure _ -> cls;;
let modify_E cls = emodify (itlist (union ** fv) cls []) cls;;
(* ------------------------------------------------------------------------- *)
(* Overall Brand transformation. *)
(* ------------------------------------------------------------------------- *)
let brand cls =
let cls1 = map modify_E cls in
let cls2 = itlist (union ** modify_S) cls1 [] in
[mk_eq (Var "x") (Var "x")]::(map modify_T cls2);;
(* ------------------------------------------------------------------------- *)
(* Incorporation into MESON. *)
(* ------------------------------------------------------------------------- *)
let bpuremeson fm =
let cls = brand(simpcnf(specialize(pnf fm))) in
let rules = itlist ((@) ** contrapositives) cls [] in
deepen (fun n ->
mexpand rules [] False (fun x -> x) (undefined,n,0); n) 0;;
let bmeson fm =
let fm1 = askolemize(Not(generalize fm)) in
map (bpuremeson ** list_conj) (simpdnf fm1);;
(* ------------------------------------------------------------------------- *)
(* Examples. *)
(* ------------------------------------------------------------------------- *)
START_INTERACTIVE;;
time bmeson
<<(exists x. x = f(g(x)) /\ forall x'. x' = f(g(x')) ==> x = x') <=>
(exists y. y = g(f(y)) /\ forall y'. y' = g(f(y')) ==> y = y')>>;;
time emeson
<<(exists x. x = f(g(x)) /\ forall x'. x' = f(g(x')) ==> x = x') <=>
(exists y. y = g(f(y)) /\ forall y'. y' = g(f(y')) ==> y = y')>>;;
time bmeson
<<(forall x y z. x * (y * z) = (x * y) * z) /\
(forall x. e * x = x) /\
(forall x. i(x) * x = e)
==> forall x. x * i(x) = e>>;;
END_INTERACTIVE;;
(* ------------------------------------------------------------------------- *)
(* Older stuff not now in the text. *)
(* ------------------------------------------------------------------------- *)
START_INTERACTIVE;;
let emeson fm = meson (equalitize fm);;
let ewd =
<<(forall x. f(x) ==> g(x)) /\
(exists x. f(x)) /\
(forall x y. g(x) /\ g(y) ==> x = y)
==> forall y. g(y) ==> f(y)>>;;
let wishnu =
<<(exists x. x = f(g(x)) /\ forall x'. x' = f(g(x')) ==> x = x') <=>
(exists y. y = g(f(y)) /\ forall y'. y' = g(f(y')) ==> y = y')>>;;
let group1 =
<<(forall x y z. x * (y * z) = (x * y) * z) /\
(forall x. e * x = x) /\
(forall x. i(x) * x = e)
==> forall x. x * e = x>>;;
let group2 =
<<(forall x y z. x * (y * z) = (x * y) * z) /\
(forall x. e * x = x) /\
(forall x. i(x) * x = e)
==> forall x. x * i(x) = e>>;;
time bmeson ewd;;
time emeson ewd;;
(***********
time bmeson wishnu;;
time emeson wishnu;;
time bmeson group1;;
time emeson group1;;
time bmeson group2;;
time emeson group2;;
*************)
(* ------------------------------------------------------------------------- *)
(* Nice function composition exercise from "Conceptual Mathematics". *)
(* ------------------------------------------------------------------------- *)
(**************
let fm =
<<(forall x y z. x * (y * z) = (x * y) * z) /\ p * q * p = p
==> exists q'. p * q' * p = p /\ q' * p * q' = q'>>;;
time bmeson fm;; (** Seems to take a bit longer than below version **)
time emeson fm;; (** Works in 64275 seconds(!), depth 30, on laptop **)
****************)
(**** Some other predicate formulations no longer in the main text
meson
<<(forall x. P(1,x,x)) /\
(forall x. P(i(x),x,1)) /\
(forall u v w x y z. P(x,y,u) /\ P(y,z,w) ==> (P(x,w,v) <=> P(u,z,v)))
==> forall x. P(x,1,x)>>;;
meson
<<(forall x. P(1,x,x)) /\
(forall x. P(i(x),x,1)) /\
(forall u v w x y z. P(x,y,u) /\ P(y,z,w) ==> (P(x,w,v) <=> P(u,z,v)))
==> forall x. P(x,i(x),1)>>;;
(* ------------------------------------------------------------------------- *)
(* See how efficiency drops when we assert completeness. *)
(* ------------------------------------------------------------------------- *)
meson
<<(forall x. P(1,x,x)) /\
(forall x. P(x,x,1)) /\
(forall x y. exists z. P(x,y,z)) /\
(forall u v w x y z. P(x,y,u) /\ P(y,z,w) ==> (P(x,w,v) <=> P(u,z,v)))
==> forall a b c. P(a,b,c) ==> P(b,a,c)>>;;
****)
(*** More reductions, not now explicitly in the text.
meson
<<(forall x. R(x,x)) /\
(forall x y z. R(x,y) /\ R(y,z) ==> R(x,z))
<=> (forall x y. R(x,y) <=> (forall z. R(y,z) ==> R(x,z)))>>;;
meson
<<(forall x y. R(x,y) ==> R(y,x)) <=>
(forall x y. R(x,y) <=> R(x,y) /\ R(y,x))>>;;
(* ------------------------------------------------------------------------- *)
(* Show how Equiv' reduces to triviality. *)
(* ------------------------------------------------------------------------- *)
meson
<<(forall x. (forall w. R'(x,w) <=> R'(x,w))) /\
(forall x y. (forall w. R'(x,w) <=> R'(y,w))
==> (forall w. R'(y,w) <=> R'(x,w))) /\
(forall x y z. (forall w. R'(x,w) <=> R'(y,w)) /\
(forall w. R'(y,w) <=> R'(z,w))
==> (forall w. R'(x,w) <=> R'(z,w)))>>;;
(* ------------------------------------------------------------------------- *)
(* More auxiliary proofs for Brand's S and T modification. *)
(* ------------------------------------------------------------------------- *)
meson
<<(forall x y. R(x,y) <=> (forall z. R'(x,z) <=> R'(y,z))) /\
(forall x. R'(x,x))
==> forall x y. ~R'(x,y) ==> ~R(x,y)>>;;
meson
<<(forall x y. R(x,y) <=> (forall z. R'(y,z) ==> R'(x,z))) /\
(forall x. R'(x,x))
==> forall x y. ~R'(x,y) ==> ~R(x,y)>>;;
meson
<<(forall x y. R(x,y) <=> R'(x,y) /\ R'(y,x))
==> forall x y. ~R'(x,y) ==> ~R(x,y)>>;;
***)
END_INTERACTIVE;;