src/HOL/Integ/Presburger.thy
 author paulson Tue Jun 28 15:27:45 2005 +0200 (2005-06-28) changeset 16587 b34c8aa657a5 parent 16417 9bc16273c2d4 child 16836 45a3dc4688bc permissions -rw-r--r--
Constant "If" is now local
1 (*  Title:      HOL/Integ/Presburger.thy
2     ID:         \$Id\$
3     Author:     Amine Chaieb, Tobias Nipkow and Stefan Berghofer, TU Muenchen
5 File containing necessary theorems for the proof
6 generation for Cooper Algorithm
7 *)
9 header {* Presburger Arithmetic: Cooper's Algorithm *}
11 theory Presburger
12 imports NatSimprocs SetInterval
13 uses ("cooper_dec.ML") ("cooper_proof.ML") ("qelim.ML") ("presburger.ML")
14 begin
16 text {* Theorem for unitifying the coeffitients of @{text x} in an existential formula*}
18 theorem unity_coeff_ex: "(\<exists>x::int. P (l * x)) = (\<exists>x. l dvd (1*x+0) \<and> P x)"
19   apply (rule iffI)
20   apply (erule exE)
21   apply (rule_tac x = "l * x" in exI)
22   apply simp
23   apply (erule exE)
24   apply (erule conjE)
25   apply (erule dvdE)
26   apply (rule_tac x = k in exI)
27   apply simp
28   done
30 lemma uminus_dvd_conv: "(d dvd (t::int)) = (-d dvd t)"
31 apply(unfold dvd_def)
32 apply(rule iffI)
33 apply(clarsimp)
34 apply(rename_tac k)
35 apply(rule_tac x = "-k" in exI)
36 apply simp
37 apply(clarsimp)
38 apply(rename_tac k)
39 apply(rule_tac x = "-k" in exI)
40 apply simp
41 done
43 lemma uminus_dvd_conv': "(d dvd (t::int)) = (d dvd -t)"
44 apply(unfold dvd_def)
45 apply(rule iffI)
46 apply(clarsimp)
47 apply(rule_tac x = "-k" in exI)
48 apply simp
49 apply(clarsimp)
50 apply(rule_tac x = "-k" in exI)
51 apply simp
52 done
56 text {*Theorems for the combination of proofs of the equality of @{text P} and @{text P_m} for integers @{text x} less than some integer @{text z}.*}
58 theorem eq_minf_conjI: "\<exists>z1::int. \<forall>x. x < z1 \<longrightarrow> (A1 x = A2 x) \<Longrightarrow>
59   \<exists>z2::int. \<forall>x. x < z2 \<longrightarrow> (B1 x = B2 x) \<Longrightarrow>
60   \<exists>z::int. \<forall>x. x < z \<longrightarrow> ((A1 x \<and> B1 x) = (A2 x \<and> B2 x))"
61   apply (erule exE)+
62   apply (rule_tac x = "min z1 z2" in exI)
63   apply simp
64   done
67 theorem eq_minf_disjI: "\<exists>z1::int. \<forall>x. x < z1 \<longrightarrow> (A1 x = A2 x) \<Longrightarrow>
68   \<exists>z2::int. \<forall>x. x < z2 \<longrightarrow> (B1 x = B2 x) \<Longrightarrow>
69   \<exists>z::int. \<forall>x. x < z \<longrightarrow> ((A1 x \<or> B1 x) = (A2 x \<or> B2 x))"
71   apply (erule exE)+
72   apply (rule_tac x = "min z1 z2" in exI)
73   apply simp
74   done
77 text {*Theorems for the combination of proofs of the equality of @{text P} and @{text P_m} for integers @{text x} greather than some integer @{text z}.*}
79 theorem eq_pinf_conjI: "\<exists>z1::int. \<forall>x. z1 < x \<longrightarrow> (A1 x = A2 x) \<Longrightarrow>
80   \<exists>z2::int. \<forall>x. z2 < x \<longrightarrow> (B1 x = B2 x) \<Longrightarrow>
81   \<exists>z::int. \<forall>x. z < x \<longrightarrow> ((A1 x \<and> B1 x) = (A2 x \<and> B2 x))"
82   apply (erule exE)+
83   apply (rule_tac x = "max z1 z2" in exI)
84   apply simp
85   done
88 theorem eq_pinf_disjI: "\<exists>z1::int. \<forall>x. z1 < x \<longrightarrow> (A1 x = A2 x) \<Longrightarrow>
89   \<exists>z2::int. \<forall>x. z2 < x \<longrightarrow> (B1 x = B2 x) \<Longrightarrow>
90   \<exists>z::int. \<forall>x. z < x  \<longrightarrow> ((A1 x \<or> B1 x) = (A2 x \<or> B2 x))"
91   apply (erule exE)+
92   apply (rule_tac x = "max z1 z2" in exI)
93   apply simp
94   done
96 text {*
97   \medskip Theorems for the combination of proofs of the modulo @{text
98   D} property for @{text "P plusinfinity"}
100   FIXME: This is THE SAME theorem as for the @{text minusinf} version,
101   but with @{text "+k.."} instead of @{text "-k.."} In the future
102   replace these both with only one. *}
104 theorem modd_pinf_conjI: "\<forall>(x::int) k. A x = A (x+k*d) \<Longrightarrow>
105   \<forall>(x::int) k. B x = B (x+k*d) \<Longrightarrow>
106   \<forall>(x::int) (k::int). (A x \<and> B x) = (A (x+k*d) \<and> B (x+k*d))"
107   by simp
109 theorem modd_pinf_disjI: "\<forall>(x::int) k. A x = A (x+k*d) \<Longrightarrow>
110   \<forall>(x::int) k. B x = B (x+k*d) \<Longrightarrow>
111   \<forall>(x::int) (k::int). (A x \<or> B x) = (A (x+k*d) \<or> B (x+k*d))"
112   by simp
114 text {*
115   This is one of the cases where the simplifed formula is prooved to
116   habe some property (in relation to @{text P_m}) but we need to prove
117   the property for the original formula (@{text P_m})
119   FIXME: This is exaclty the same thm as for @{text minusinf}. *}
121 lemma pinf_simp_eq: "ALL x. P(x) = Q(x) ==> (EX (x::int). P(x)) --> (EX (x::int). F(x))  ==> (EX (x::int). Q(x)) --> (EX (x::int). F(x)) "
122   by blast
125 text {*
126   \medskip Theorems for the combination of proofs of the modulo @{text D}
127   property for @{text "P minusinfinity"} *}
129 theorem modd_minf_conjI: "\<forall>(x::int) k. A x = A (x-k*d) \<Longrightarrow>
130   \<forall>(x::int) k. B x = B (x-k*d) \<Longrightarrow>
131   \<forall>(x::int) (k::int). (A x \<and> B x) = (A (x-k*d) \<and> B (x-k*d))"
132   by simp
134 theorem modd_minf_disjI: "\<forall>(x::int) k. A x = A (x-k*d) \<Longrightarrow>
135   \<forall>(x::int) k. B x = B (x-k*d) \<Longrightarrow>
136   \<forall>(x::int) (k::int). (A x \<or> B x) = (A (x-k*d) \<or> B (x-k*d))"
137   by simp
139 text {*
140   This is one of the cases where the simplifed formula is prooved to
141   have some property (in relation to @{text P_m}) but we need to
142   prove the property for the original formula (@{text P_m}). *}
144 lemma minf_simp_eq: "ALL x. P(x) = Q(x) ==> (EX (x::int). P(x)) --> (EX (x::int). F(x))  ==> (EX (x::int). Q(x)) --> (EX (x::int). F(x)) "
145   by blast
147 text {*
148   Theorem needed for proving at runtime divide properties using the
149   arithmetic tactic (which knows only about modulo = 0). *}
151 lemma zdvd_iff_zmod_eq_0: "(m dvd n) = (n mod m = (0::int))"
152   by(simp add:dvd_def zmod_eq_0_iff)
154 text {*
155   \medskip Theorems used for the combination of proof for the
156   backwards direction of Cooper's Theorem. They rely exclusively on
157   Predicate calculus.*}
159 lemma not_ast_p_disjI: "(ALL x. Q(x::int) \<and> ~(EX (j::int) : {1..d}. EX (a::int) : A. Q(a - j)) --> P1(x) --> P1(x + d))
160 ==>
161 (ALL x. Q(x::int) \<and> ~(EX (j::int) : {1..d}. EX (a::int) : A. Q(a - j)) --> P2(x) --> P2(x + d))
162 ==>
163 (ALL x. Q(x::int) \<and> ~(EX (j::int) : {1..d}. EX (a::int) : A. Q(a - j)) -->(P1(x) \<or> P2(x)) --> (P1(x + d) \<or> P2(x + d))) "
164   by blast
167 lemma not_ast_p_conjI: "(ALL x. Q(x::int) \<and> ~(EX (j::int) : {1..d}. EX (a::int) : A. Q(a- j)) --> P1(x) --> P1(x + d))
168 ==>
169 (ALL x. Q(x::int) \<and> ~(EX (j::int) : {1..d}. EX (a::int) : A. Q(a - j)) --> P2(x) --> P2(x + d))
170 ==>
171 (ALL x. Q(x::int) \<and> ~(EX (j::int) : {1..d}. EX (a::int) : A. Q(a - j)) -->(P1(x) \<and> P2(x)) --> (P1(x + d)
172 \<and> P2(x + d))) "
173   by blast
175 lemma not_ast_p_Q_elim: "
176 (ALL x. Q(x::int) \<and> ~(EX (j::int) : {1..d}. EX (a::int) : A. Q(a - j)) -->P(x) --> P(x + d))
177 ==> ( P = Q )
178 ==> (ALL x. ~(EX (j::int) : {1..d}. EX (a::int) : A. P(a - j)) -->P(x) --> P(x + d))"
179   by blast
181 text {*
182   \medskip Theorems used for the combination of proof for the
183   backwards direction of Cooper's Theorem. They rely exclusively on
184   Predicate calculus.*}
186 lemma not_bst_p_disjI: "(ALL x. Q(x::int) \<and> ~(EX (j::int) : {1..d}. EX (b::int) : B. Q(b+j)) --> P1(x) --> P1(x - d))
187 ==>
188 (ALL x. Q(x::int) \<and> ~(EX (j::int) : {1..d}. EX (b::int) : B. Q(b+j)) --> P2(x) --> P2(x - d))
189 ==>
190 (ALL x. Q(x::int) \<and> ~(EX (j::int) : {1..d}. EX (b::int) : B. Q(b+j)) -->(P1(x) \<or> P2(x)) --> (P1(x - d)
191 \<or> P2(x-d))) "
192   by blast
194 lemma not_bst_p_conjI: "(ALL x. Q(x::int) \<and> ~(EX (j::int) : {1..d}. EX (b::int) : B. Q(b+j)) --> P1(x) --> P1(x - d))
195 ==>
196 (ALL x. Q(x::int) \<and> ~(EX (j::int) : {1..d}. EX (b::int) : B. Q(b+j)) --> P2(x) --> P2(x - d))
197 ==>
198 (ALL x. Q(x::int) \<and> ~(EX (j::int) : {1..d}. EX (b::int) : B. Q(b+j)) -->(P1(x) \<and> P2(x)) --> (P1(x - d)
199 \<and> P2(x-d))) "
200   by blast
202 lemma not_bst_p_Q_elim: "
203 (ALL x. Q(x::int) \<and> ~(EX (j::int) : {1..d}. EX (b::int) : B. Q(b+j)) -->P(x) --> P(x - d))
204 ==> ( P = Q )
205 ==> (ALL x. ~(EX (j::int) : {1..d}. EX (b::int) : B. P(b+j)) -->P(x) --> P(x - d))"
206   by blast
208 text {* \medskip This is the first direction of Cooper's Theorem. *}
209 lemma cooper_thm: "(R --> (EX x::int. P x))  ==> (Q -->(EX x::int.  P x )) ==> ((R|Q) --> (EX x::int. P x )) "
210   by blast
212 text {*
213   \medskip The full Cooper's Theorem in its equivalence Form. Given
214   the premises it is trivial too, it relies exclusively on prediacte calculus.*}
215 lemma cooper_eq_thm: "(R --> (EX x::int. P x))  ==> (Q -->(EX x::int.  P x )) ==> ((~Q)
216 --> (EX x::int. P x ) --> R) ==> (EX x::int. P x) = R|Q "
217   by blast
219 text {*
220   \medskip Some of the atomic theorems generated each time the atom
221   does not depend on @{text x}, they are trivial.*}
223 lemma  fm_eq_minf: "EX z::int. ALL x. x < z --> (P = P) "
224   by blast
226 lemma  fm_modd_minf: "ALL (x::int). ALL (k::int). (P = P)"
227   by blast
229 lemma not_bst_p_fm: "ALL (x::int). Q(x::int) \<and> ~(EX (j::int) : {1..d}. EX (b::int) : B. Q(b+j)) --> fm --> fm"
230   by blast
232 lemma  fm_eq_pinf: "EX z::int. ALL x. z < x --> (P = P) "
233   by blast
235 text {* The next two thms are the same as the @{text minusinf} version. *}
237 lemma  fm_modd_pinf: "ALL (x::int). ALL (k::int). (P = P)"
238   by blast
240 lemma not_ast_p_fm: "ALL (x::int). Q(x::int) \<and> ~(EX (j::int) : {1..d}. EX (a::int) : A. Q(a - j)) --> fm --> fm"
241   by blast
243 text {* Theorems to be deleted from simpset when proving simplified formulaes. *}
245 lemma P_eqtrue: "(P=True) = P"
246   by rules
248 lemma P_eqfalse: "(P=False) = (~P)"
249   by rules
251 text {*
252   \medskip Theorems for the generation of the bachwards direction of
253   Cooper's Theorem.
255   These are the 6 interesting atomic cases which have to be proved relying on the
256   properties of B-set and the arithmetic and contradiction proofs. *}
258 lemma not_bst_p_lt: "0 < (d::int) ==>
259  ALL x. Q(x::int) \<and> ~(EX (j::int) : {1..d}. EX (b::int) : B. Q(b+j)) --> ( 0 < -x + a) --> (0 < -(x - d) + a )"
260   by arith
262 lemma not_bst_p_gt: "\<lbrakk> (g::int) \<in> B; g = -a \<rbrakk> \<Longrightarrow>
263  ALL x. Q(x::int) \<and> ~(EX (j::int) : {1..d}. EX (b::int) : B. Q(b+j)) --> (0 < (x) + a) --> ( 0 < (x - d) + a)"
264 apply clarsimp
265 apply(rule ccontr)
266 apply(drule_tac x = "x+a" in bspec)
268 apply(drule_tac x = "-a" in bspec)
269 apply assumption
270 apply(simp)
271 done
273 lemma not_bst_p_eq: "\<lbrakk> 0 < d; (g::int) \<in> B; g = -a - 1 \<rbrakk> \<Longrightarrow>
274  ALL x. Q(x::int) \<and> ~(EX (j::int) : {1..d}. EX (b::int) : B. Q(b+j)) --> (0 = x + a) --> (0 = (x - d) + a )"
275 apply clarsimp
276 apply(subgoal_tac "x = -a")
277  prefer 2 apply arith
278 apply(drule_tac x = "1" in bspec)
280 apply(drule_tac x = "-a- 1" in bspec)
281 apply assumption
282 apply(simp)
283 done
286 lemma not_bst_p_ne: "\<lbrakk> 0 < d; (g::int) \<in> B; g = -a \<rbrakk> \<Longrightarrow>
287  ALL x. Q(x::int) \<and> ~(EX (j::int) : {1..d}. EX (b::int) : B. Q(b+j)) --> ~(0 = x + a) --> ~(0 = (x - d) + a)"
288 apply clarsimp
289 apply(subgoal_tac "x = -a+d")
290  prefer 2 apply arith
291 apply(drule_tac x = "d" in bspec)
293 apply(drule_tac x = "-a" in bspec)
294 apply assumption
295 apply(simp)
296 done
299 lemma not_bst_p_dvd: "(d1::int) dvd d ==>
300  ALL x. Q(x::int) \<and> ~(EX (j::int) : {1..d}. EX (b::int) : B. Q(b+j)) --> d1 dvd (x + a) --> d1 dvd ((x - d) + a )"
301 apply(clarsimp simp add:dvd_def)
302 apply(rename_tac m)
303 apply(rule_tac x = "m - k" in exI)
305 done
307 lemma not_bst_p_ndvd: "(d1::int) dvd d ==>
308  ALL x. Q(x::int) \<and> ~(EX (j::int) : {1..d}. EX (b::int) : B. Q(b+j)) --> ~(d1 dvd (x + a)) --> ~(d1 dvd ((x - d) + a ))"
309 apply(clarsimp simp add:dvd_def)
310 apply(rename_tac m)
311 apply(erule_tac x = "m + k" in allE)
313 done
315 text {*
316   \medskip Theorems for the generation of the bachwards direction of
317   Cooper's Theorem.
319   These are the 6 interesting atomic cases which have to be proved
320   relying on the properties of A-set ant the arithmetic and
321   contradiction proofs. *}
323 lemma not_ast_p_gt: "0 < (d::int) ==>
324  ALL x. Q(x::int) \<and> ~(EX (j::int) : {1..d}. EX (a::int) : A. Q(a - j)) --> ( 0 < x + t) --> (0 < (x + d) + t )"
325   by arith
327 lemma not_ast_p_lt: "\<lbrakk>0 < d ;(t::int) \<in> A \<rbrakk> \<Longrightarrow>
328  ALL x. Q(x::int) \<and> ~(EX (j::int) : {1..d}. EX (a::int) : A. Q(a - j)) --> (0 < -x + t) --> ( 0 < -(x + d) + t)"
329   apply clarsimp
330   apply (rule ccontr)
331   apply (drule_tac x = "t-x" in bspec)
332   apply simp
333   apply (drule_tac x = "t" in bspec)
334   apply assumption
335   apply simp
336   done
338 lemma not_ast_p_eq: "\<lbrakk> 0 < d; (g::int) \<in> A; g = -t + 1 \<rbrakk> \<Longrightarrow>
339  ALL x. Q(x::int) \<and> ~(EX (j::int) : {1..d}. EX (a::int) : A. Q(a - j)) --> (0 = x + t) --> (0 = (x + d) + t )"
340   apply clarsimp
341   apply (drule_tac x="1" in bspec)
342   apply simp
343   apply (drule_tac x="- t + 1" in bspec)
344   apply assumption
345   apply(subgoal_tac "x = -t")
346   prefer 2 apply arith
347   apply simp
348   done
350 lemma not_ast_p_ne: "\<lbrakk> 0 < d; (g::int) \<in> A; g = -t \<rbrakk> \<Longrightarrow>
351  ALL x. Q(x::int) \<and> ~(EX (j::int) : {1..d}. EX (a::int) : A. Q(a - j)) --> ~(0 = x + t) --> ~(0 = (x + d) + t)"
352   apply clarsimp
353   apply (subgoal_tac "x = -t-d")
354   prefer 2 apply arith
355   apply (drule_tac x = "d" in bspec)
356   apply simp
357   apply (drule_tac x = "-t" in bspec)
358   apply assumption
359   apply simp
360   done
362 lemma not_ast_p_dvd: "(d1::int) dvd d ==>
363  ALL x. Q(x::int) \<and> ~(EX (j::int) : {1..d}. EX (a::int) : A. Q(a - j)) --> d1 dvd (x + t) --> d1 dvd ((x + d) + t )"
364   apply(clarsimp simp add:dvd_def)
365   apply(rename_tac m)
366   apply(rule_tac x = "m + k" in exI)
368   done
370 lemma not_ast_p_ndvd: "(d1::int) dvd d ==>
371  ALL x. Q(x::int) \<and> ~(EX (j::int) : {1..d}. EX (a::int) : A. Q(a - j)) --> ~(d1 dvd (x + t)) --> ~(d1 dvd ((x + d) + t ))"
372   apply(clarsimp simp add:dvd_def)
373   apply(rename_tac m)
374   apply(erule_tac x = "m - k" in allE)
376   done
378 text {*
379   \medskip These are the atomic cases for the proof generation for the
380   modulo @{text D} property for @{text "P plusinfinity"}
382   They are fully based on arithmetics. *}
384 lemma  dvd_modd_pinf: "((d::int) dvd d1) ==>
385  (ALL (x::int). ALL (k::int). (((d::int) dvd (x + t)) = (d dvd (x+k*d1 + t))))"
386   apply(clarsimp simp add:dvd_def)
387   apply(rule iffI)
388   apply(clarsimp)
389   apply(rename_tac n m)
390   apply(rule_tac x = "m + n*k" in exI)
392   apply(clarsimp)
393   apply(rename_tac n m)
394   apply(rule_tac x = "m - n*k" in exI)
395   apply(simp add:int_distrib mult_ac)
396   done
398 lemma  not_dvd_modd_pinf: "((d::int) dvd d1) ==>
399  (ALL (x::int). ALL k. (~((d::int) dvd (x + t))) = (~(d dvd (x+k*d1 + t))))"
400   apply(clarsimp simp add:dvd_def)
401   apply(rule iffI)
402   apply(clarsimp)
403   apply(rename_tac n m)
404   apply(erule_tac x = "m - n*k" in allE)
405   apply(simp add:int_distrib mult_ac)
406   apply(clarsimp)
407   apply(rename_tac n m)
408   apply(erule_tac x = "m + n*k" in allE)
409   apply(simp add:int_distrib mult_ac)
410   done
412 text {*
413   \medskip These are the atomic cases for the proof generation for the
414   equivalence of @{text P} and @{text "P plusinfinity"} for integers
415   @{text x} greater than some integer @{text z}.
417   They are fully based on arithmetics. *}
419 lemma  eq_eq_pinf: "EX z::int. ALL x. z < x --> (( 0 = x +t ) = False )"
420   apply(rule_tac x = "-t" in exI)
421   apply simp
422   done
424 lemma  neq_eq_pinf: "EX z::int. ALL x.  z < x --> ((~( 0 = x +t )) = True )"
425   apply(rule_tac x = "-t" in exI)
426   apply simp
427   done
429 lemma  le_eq_pinf: "EX z::int. ALL x.  z < x --> ( 0 < x +t  = True )"
430   apply(rule_tac x = "-t" in exI)
431   apply simp
432   done
434 lemma  len_eq_pinf: "EX z::int. ALL x. z < x  --> (0 < -x +t  = False )"
435   apply(rule_tac x = "t" in exI)
436   apply simp
437   done
439 lemma  dvd_eq_pinf: "EX z::int. ALL x.  z < x --> ((d dvd (x + t)) = (d dvd (x + t))) "
440   by simp
442 lemma  not_dvd_eq_pinf: "EX z::int. ALL x. z < x  --> ((~(d dvd (x + t))) = (~(d dvd (x + t)))) "
443   by simp
445 text {*
446   \medskip These are the atomic cases for the proof generation for the
447   modulo @{text D} property for @{text "P minusinfinity"}.
449   They are fully based on arithmetics. *}
451 lemma  dvd_modd_minf: "((d::int) dvd d1) ==>
452  (ALL (x::int). ALL (k::int). (((d::int) dvd (x + t)) = (d dvd (x-k*d1 + t))))"
453 apply(clarsimp simp add:dvd_def)
454 apply(rule iffI)
455 apply(clarsimp)
456 apply(rename_tac n m)
457 apply(rule_tac x = "m - n*k" in exI)
459 apply(clarsimp)
460 apply(rename_tac n m)
461 apply(rule_tac x = "m + n*k" in exI)
462 apply(simp add:int_distrib mult_ac)
463 done
466 lemma  not_dvd_modd_minf: "((d::int) dvd d1) ==>
467  (ALL (x::int). ALL k. (~((d::int) dvd (x + t))) = (~(d dvd (x-k*d1 + t))))"
468 apply(clarsimp simp add:dvd_def)
469 apply(rule iffI)
470 apply(clarsimp)
471 apply(rename_tac n m)
472 apply(erule_tac x = "m + n*k" in allE)
473 apply(simp add:int_distrib mult_ac)
474 apply(clarsimp)
475 apply(rename_tac n m)
476 apply(erule_tac x = "m - n*k" in allE)
477 apply(simp add:int_distrib mult_ac)
478 done
480 text {*
481   \medskip These are the atomic cases for the proof generation for the
482   equivalence of @{text P} and @{text "P minusinfinity"} for integers
483   @{text x} less than some integer @{text z}.
485   They are fully based on arithmetics. *}
487 lemma  eq_eq_minf: "EX z::int. ALL x. x < z --> (( 0 = x +t ) = False )"
488 apply(rule_tac x = "-t" in exI)
489 apply simp
490 done
492 lemma  neq_eq_minf: "EX z::int. ALL x. x < z --> ((~( 0 = x +t )) = True )"
493 apply(rule_tac x = "-t" in exI)
494 apply simp
495 done
497 lemma  le_eq_minf: "EX z::int. ALL x. x < z --> ( 0 < x +t  = False )"
498 apply(rule_tac x = "-t" in exI)
499 apply simp
500 done
503 lemma  len_eq_minf: "EX z::int. ALL x. x < z --> (0 < -x +t  = True )"
504 apply(rule_tac x = "t" in exI)
505 apply simp
506 done
508 lemma  dvd_eq_minf: "EX z::int. ALL x. x < z --> ((d dvd (x + t)) = (d dvd (x + t))) "
509   by simp
511 lemma  not_dvd_eq_minf: "EX z::int. ALL x. x < z --> ((~(d dvd (x + t))) = (~(d dvd (x + t)))) "
512   by simp
514 text {*
515   \medskip This Theorem combines whithnesses about @{text "P
516   minusinfinity"} to show one component of the equivalence proof for
517   Cooper's Theorem.
519   FIXME: remove once they are part of the distribution. *}
521 theorem int_ge_induct[consumes 1,case_names base step]:
522   assumes ge: "k \<le> (i::int)" and
523         base: "P(k)" and
524         step: "\<And>i. \<lbrakk>k \<le> i; P i\<rbrakk> \<Longrightarrow> P(i+1)"
525   shows "P i"
526 proof -
527   { fix n have "\<And>i::int. n = nat(i-k) \<Longrightarrow> k <= i \<Longrightarrow> P i"
528     proof (induct n)
529       case 0
530       hence "i = k" by arith
531       thus "P i" using base by simp
532     next
533       case (Suc n)
534       hence "n = nat((i - 1) - k)" by arith
535       moreover
536       have ki1: "k \<le> i - 1" using Suc.prems by arith
537       ultimately
538       have "P(i - 1)" by(rule Suc.hyps)
539       from step[OF ki1 this] show ?case by simp
540     qed
541   }
542   from this ge show ?thesis by fast
543 qed
545 theorem int_gr_induct[consumes 1,case_names base step]:
546   assumes gr: "k < (i::int)" and
547         base: "P(k+1)" and
548         step: "\<And>i. \<lbrakk>k < i; P i\<rbrakk> \<Longrightarrow> P(i+1)"
549   shows "P i"
550 apply(rule int_ge_induct[of "k + 1"])
551   using gr apply arith
552  apply(rule base)
553 apply(rule step)
554  apply simp+
555 done
557 lemma decr_lemma: "0 < (d::int) \<Longrightarrow> x - (abs(x-z)+1) * d < z"
558 apply(induct rule: int_gr_induct)
559  apply simp
560  apply arith
561 apply (simp add:int_distrib)
562 apply arith
563 done
565 lemma incr_lemma: "0 < (d::int) \<Longrightarrow> z < x + (abs(x-z)+1) * d"
566 apply(induct rule: int_gr_induct)
567  apply simp
568  apply arith
569 apply (simp add:int_distrib)
570 apply arith
571 done
573 lemma  minusinfinity:
574   assumes "0 < d" and
575     P1eqP1: "ALL x k. P1 x = P1(x - k*d)" and
576     ePeqP1: "EX z::int. ALL x. x < z \<longrightarrow> (P x = P1 x)"
577   shows "(EX x. P1 x) \<longrightarrow> (EX x. P x)"
578 proof
579   assume eP1: "EX x. P1 x"
580   then obtain x where P1: "P1 x" ..
581   from ePeqP1 obtain z where P1eqP: "ALL x. x < z \<longrightarrow> (P x = P1 x)" ..
582   let ?w = "x - (abs(x-z)+1) * d"
583   show "EX x. P x"
584   proof
585     have w: "?w < z" by(rule decr_lemma)
586     have "P1 x = P1 ?w" using P1eqP1 by blast
587     also have "\<dots> = P(?w)" using w P1eqP by blast
588     finally show "P ?w" using P1 by blast
589   qed
590 qed
592 text {*
593   \medskip This Theorem combines whithnesses about @{text "P
594   minusinfinity"} to show one component of the equivalence proof for
595   Cooper's Theorem. *}
597 lemma plusinfinity:
598   assumes "0 < d" and
599     P1eqP1: "ALL (x::int) (k::int). P1 x = P1 (x + k * d)" and
600     ePeqP1: "EX z::int. ALL x. z < x  --> (P x = P1 x)"
601   shows "(EX x::int. P1 x) --> (EX x::int. P x)"
602 proof
603   assume eP1: "EX x. P1 x"
604   then obtain x where P1: "P1 x" ..
605   from ePeqP1 obtain z where P1eqP: "ALL x. z < x \<longrightarrow> (P x = P1 x)" ..
606   let ?w = "x + (abs(x-z)+1) * d"
607   show "EX x. P x"
608   proof
609     have w: "z < ?w" by(rule incr_lemma)
610     have "P1 x = P1 ?w" using P1eqP1 by blast
611     also have "\<dots> = P(?w)" using w P1eqP by blast
612     finally show "P ?w" using P1 by blast
613   qed
614 qed
616 text {*
617   \medskip Theorem for periodic function on discrete sets. *}
619 lemma minf_vee:
620   assumes dpos: "(0::int) < d" and modd: "ALL x k. P x = P(x - k*d)"
621   shows "(EX x. P x) = (EX j : {1..d}. P j)"
622   (is "?LHS = ?RHS")
623 proof
624   assume ?LHS
625   then obtain x where P: "P x" ..
626   have "x mod d = x - (x div d)*d"
627     by(simp add:zmod_zdiv_equality mult_ac eq_diff_eq)
628   hence Pmod: "P x = P(x mod d)" using modd by simp
629   show ?RHS
630   proof (cases)
631     assume "x mod d = 0"
632     hence "P 0" using P Pmod by simp
633     moreover have "P 0 = P(0 - (-1)*d)" using modd by blast
634     ultimately have "P d" by simp
635     moreover have "d : {1..d}" using dpos by(simp add:atLeastAtMost_iff)
636     ultimately show ?RHS ..
637   next
638     assume not0: "x mod d \<noteq> 0"
639     have "P(x mod d)" using dpos P Pmod by(simp add:pos_mod_sign pos_mod_bound)
640     moreover have "x mod d : {1..d}"
641     proof -
642       have "0 \<le> x mod d" by(rule pos_mod_sign)
643       moreover have "x mod d < d" by(rule pos_mod_bound)
644       ultimately show ?thesis using not0 by(simp add:atLeastAtMost_iff)
645     qed
646     ultimately show ?RHS ..
647   qed
648 next
649   assume ?RHS thus ?LHS by blast
650 qed
652 text {*
653   \medskip Theorem for periodic function on discrete sets. *}
655 lemma pinf_vee:
656   assumes dpos: "0 < (d::int)" and modd: "ALL (x::int) (k::int). P x = P (x+k*d)"
657   shows "(EX x::int. P x) = (EX (j::int) : {1..d} . P j)"
658   (is "?LHS = ?RHS")
659 proof
660   assume ?LHS
661   then obtain x where P: "P x" ..
662   have "x mod d = x + (-(x div d))*d"
663     by(simp add:zmod_zdiv_equality mult_ac eq_diff_eq)
664   hence Pmod: "P x = P(x mod d)" using modd by (simp only:)
665   show ?RHS
666   proof (cases)
667     assume "x mod d = 0"
668     hence "P 0" using P Pmod by simp
669     moreover have "P 0 = P(0 + 1*d)" using modd by blast
670     ultimately have "P d" by simp
671     moreover have "d : {1..d}" using dpos by(simp add:atLeastAtMost_iff)
672     ultimately show ?RHS ..
673   next
674     assume not0: "x mod d \<noteq> 0"
675     have "P(x mod d)" using dpos P Pmod by(simp add:pos_mod_sign pos_mod_bound)
676     moreover have "x mod d : {1..d}"
677     proof -
678       have "0 \<le> x mod d" by(rule pos_mod_sign)
679       moreover have "x mod d < d" by(rule pos_mod_bound)
680       ultimately show ?thesis using not0 by(simp add:atLeastAtMost_iff)
681     qed
682     ultimately show ?RHS ..
683   qed
684 next
685   assume ?RHS thus ?LHS by blast
686 qed
688 lemma decr_mult_lemma:
689   assumes dpos: "(0::int) < d" and
690           minus: "ALL x::int. P x \<longrightarrow> P(x - d)" and
691           knneg: "0 <= k"
692   shows "ALL x. P x \<longrightarrow> P(x - k*d)"
693 using knneg
694 proof (induct rule:int_ge_induct)
695   case base thus ?case by simp
696 next
697   case (step i)
698   show ?case
699   proof
700     fix x
701     have "P x \<longrightarrow> P (x - i * d)" using step.hyps by blast
702     also have "\<dots> \<longrightarrow> P(x - (i + 1) * d)"
703       using minus[THEN spec, of "x - i * d"]
704       by (simp add:int_distrib OrderedGroup.diff_diff_eq[symmetric])
705     ultimately show "P x \<longrightarrow> P(x - (i + 1) * d)" by blast
706   qed
707 qed
709 lemma incr_mult_lemma:
710   assumes dpos: "(0::int) < d" and
711           plus: "ALL x::int. P x \<longrightarrow> P(x + d)" and
712           knneg: "0 <= k"
713   shows "ALL x. P x \<longrightarrow> P(x + k*d)"
714 using knneg
715 proof (induct rule:int_ge_induct)
716   case base thus ?case by simp
717 next
718   case (step i)
719   show ?case
720   proof
721     fix x
722     have "P x \<longrightarrow> P (x + i * d)" using step.hyps by blast
723     also have "\<dots> \<longrightarrow> P(x + (i + 1) * d)"
724       using plus[THEN spec, of "x + i * d"]
726     ultimately show "P x \<longrightarrow> P(x + (i + 1) * d)" by blast
727   qed
728 qed
730 lemma cpmi_eq: "0 < D \<Longrightarrow> (EX z::int. ALL x. x < z --> (P x = P1 x))
731 ==> ALL x.~(EX (j::int) : {1..D}. EX (b::int) : B. P(b+j)) --> P (x) --> P (x - D)
732 ==> (ALL (x::int). ALL (k::int). ((P1 x)= (P1 (x-k*D))))
733 ==> (EX (x::int). P(x)) = ((EX (j::int) : {1..D} . (P1(j))) | (EX (j::int) : {1..D}. EX (b::int) : B. P (b+j)))"
734 apply(rule iffI)
735 prefer 2
736 apply(drule minusinfinity)
737 apply assumption+
738 apply(fastsimp)
739 apply clarsimp
740 apply(subgoal_tac "!!k. 0<=k \<Longrightarrow> !x. P x \<longrightarrow> P (x - k*D)")
741 apply(frule_tac x = x and z=z in decr_lemma)
742 apply(subgoal_tac "P1(x - (\<bar>x - z\<bar> + 1) * D)")
743 prefer 2
744 apply(subgoal_tac "0 <= (\<bar>x - z\<bar> + 1)")
745 prefer 2 apply arith
746  apply fastsimp
747 apply(drule (1) minf_vee)
748 apply blast
749 apply(blast dest:decr_mult_lemma)
750 done
752 text {* Cooper Theorem, plus infinity version. *}
753 lemma cppi_eq: "0 < D \<Longrightarrow> (EX z::int. ALL x. z < x --> (P x = P1 x))
754 ==> ALL x.~(EX (j::int) : {1..D}. EX (a::int) : A. P(a - j)) --> P (x) --> P (x + D)
755 ==> (ALL (x::int). ALL (k::int). ((P1 x)= (P1 (x+k*D))))
756 ==> (EX (x::int). P(x)) = ((EX (j::int) : {1..D} . (P1(j))) | (EX (j::int) : {1..D}. EX (a::int) : A. P (a - j)))"
757   apply(rule iffI)
758   prefer 2
759   apply(drule plusinfinity)
760   apply assumption+
761   apply(fastsimp)
762   apply clarsimp
763   apply(subgoal_tac "!!k. 0<=k \<Longrightarrow> !x. P x \<longrightarrow> P (x + k*D)")
764   apply(frule_tac x = x and z=z in incr_lemma)
765   apply(subgoal_tac "P1(x + (\<bar>x - z\<bar> + 1) * D)")
766   prefer 2
767   apply(subgoal_tac "0 <= (\<bar>x - z\<bar> + 1)")
768   prefer 2 apply arith
769   apply fastsimp
770   apply(drule (1) pinf_vee)
771   apply blast
772   apply(blast dest:incr_mult_lemma)
773   done
776 text {*
777   \bigskip Theorems for the quantifier elminination Functions. *}
779 lemma qe_ex_conj: "(EX (x::int). A x) = R
780 		==> (EX (x::int). P x) = (Q & (EX x::int. A x))
781 		==> (EX (x::int). P x) = (Q & R)"
782 by blast
784 lemma qe_ex_nconj: "(EX (x::int). P x) = (True & Q)
785 		==> (EX (x::int). P x) = Q"
786 by blast
788 lemma qe_conjI: "P1 = P2 ==> Q1 = Q2 ==> (P1 & Q1) = (P2 & Q2)"
789 by blast
791 lemma qe_disjI: "P1 = P2 ==> Q1 = Q2 ==> (P1 | Q1) = (P2 | Q2)"
792 by blast
794 lemma qe_impI: "P1 = P2 ==> Q1 = Q2 ==> (P1 --> Q1) = (P2 --> Q2)"
795 by blast
797 lemma qe_eqI: "P1 = P2 ==> Q1 = Q2 ==> (P1 = Q1) = (P2 = Q2)"
798 by blast
800 lemma qe_Not: "P = Q ==> (~P) = (~Q)"
801 by blast
803 lemma qe_ALL: "(EX x. ~P x) = R ==> (ALL x. P x) = (~R)"
804 by blast
806 text {* \bigskip Theorems for proving NNF *}
808 lemma nnf_im: "((~P) = P1) ==> (Q=Q1) ==> ((P --> Q) = (P1 | Q1))"
809 by blast
811 lemma nnf_eq: "((P & Q) = (P1 & Q1)) ==> (((~P) & (~Q)) = (P2 & Q2)) ==> ((P = Q) = ((P1 & Q1)|(P2 & Q2)))"
812 by blast
814 lemma nnf_nn: "(P = Q) ==> ((~~P) = Q)"
815   by blast
816 lemma nnf_ncj: "((~P) = P1) ==> ((~Q) = Q1) ==> ((~(P & Q)) = (P1 | Q1))"
817 by blast
819 lemma nnf_ndj: "((~P) = P1) ==> ((~Q) = Q1) ==> ((~(P | Q)) = (P1 & Q1))"
820 by blast
821 lemma nnf_nim: "(P = P1) ==> ((~Q) = Q1) ==> ((~(P --> Q)) = (P1 & Q1))"
822 by blast
823 lemma nnf_neq: "((P & (~Q)) = (P1 & Q1)) ==> (((~P) & Q) = (P2 & Q2)) ==> ((~(P = Q)) = ((P1 & Q1)|(P2 & Q2)))"
824 by blast
825 lemma nnf_sdj: "((A & (~B)) = (A1 & B1)) ==> ((C & (~D)) = (C1 & D1)) ==> (A = (~C)) ==> ((~((A & B) | (C & D))) = ((A1 & B1) | (C1 & D1)))"
826 by blast
829 lemma qe_exI2: "A = B ==> (EX (x::int). A(x)) = (EX (x::int). B(x))"
830   by simp
832 lemma qe_exI: "(!!x::int. A x = B x) ==> (EX (x::int). A(x)) = (EX (x::int). B(x))"
833   by rules
835 lemma qe_ALLI: "(!!x::int. A x = B x) ==> (ALL (x::int). A(x)) = (ALL (x::int). B(x))"
836   by rules
838 lemma cp_expand: "(EX (x::int). P (x)) = (EX (j::int) : {1..d}. EX (b::int) : B. (P1 (j) | P(b+j)))
839 ==>(EX (x::int). P (x)) = (EX (j::int) : {1..d}. EX (b::int) : B. (P1 (j) | P(b+j))) "
840 by blast
842 lemma cppi_expand: "(EX (x::int). P (x)) = (EX (j::int) : {1..d}. EX (a::int) : A. (P1 (j) | P(a - j)))
843 ==>(EX (x::int). P (x)) = (EX (j::int) : {1..d}. EX (a::int) : A. (P1 (j) | P(a - j))) "
844 by blast
847 lemma simp_from_to: "{i..j::int} = (if j < i then {} else insert i {i+1..j})"
848 apply(simp add:atLeastAtMost_def atLeast_def atMost_def)
849 apply(fastsimp)
850 done
852 text {* \bigskip Theorems required for the @{text adjustcoeffitienteq} *}
854 lemma ac_dvd_eq: assumes not0: "0 ~= (k::int)"
855 shows "((m::int) dvd (c*n+t)) = (k*m dvd ((k*c)*n+(k*t)))" (is "?P = ?Q")
856 proof
857   assume ?P
858   thus ?Q
860     apply clarify
861     apply(rename_tac d)
862     apply(drule_tac f = "op * k" in arg_cong)
863     apply(simp only:int_distrib)
864     apply(rule_tac x = "d" in exI)
865     apply(simp only:mult_ac)
866     done
867 next
868   assume ?Q
869   then obtain d where "k * c * n + k * t = (k*m)*d" by(fastsimp simp:dvd_def)
870   hence "(c * n + t) * k = (m*d) * k" by(simp add:int_distrib mult_ac)
871   hence "((c * n + t) * k) div k = ((m*d) * k) div k" by(rule arg_cong[of _ _ "%t. t div k"])
872   hence "c*n+t = m*d" by(simp add: zdiv_zmult_self1[OF not0[symmetric]])
873   thus ?P by(simp add:dvd_def)
874 qed
876 lemma ac_lt_eq: assumes gr0: "0 < (k::int)"
877 shows "((m::int) < (c*n+t)) = (k*m <((k*c)*n+(k*t)))" (is "?P = ?Q")
878 proof
879   assume P: ?P
880   show ?Q using zmult_zless_mono2[OF P gr0] by(simp add: int_distrib mult_ac)
881 next
882   assume ?Q
883   hence "0 < k*(c*n + t - m)" by(simp add: int_distrib mult_ac)
884   with gr0 have "0 < (c*n + t - m)" by(simp add: zero_less_mult_iff)
885   thus ?P by(simp)
886 qed
888 lemma ac_eq_eq : assumes not0: "0 ~= (k::int)" shows "((m::int) = (c*n+t)) = (k*m =((k*c)*n+(k*t)) )" (is "?P = ?Q")
889 proof
890   assume ?P
891   thus ?Q
892     apply(drule_tac f = "op * k" in arg_cong)
893     apply(simp only:int_distrib)
894     done
895 next
896   assume ?Q
897   hence "m * k = (c*n + t) * k" by(simp add:int_distrib mult_ac)
898   hence "((m) * k) div k = ((c*n + t) * k) div k" by(rule arg_cong[of _ _ "%t. t div k"])
899   thus ?P by(simp add: zdiv_zmult_self1[OF not0[symmetric]])
900 qed
902 lemma ac_pi_eq: assumes gr0: "0 < (k::int)" shows "(~((0::int) < (c*n + t))) = (0 < ((-k)*c)*n + ((-k)*t + k))"
903 proof -
904   have "(~ (0::int) < (c*n + t)) = (0<1-(c*n + t))" by arith
905   also have  "(1-(c*n + t)) = (-1*c)*n + (-t+1)" by(simp add: int_distrib mult_ac)
906   also have "0<(-1*c)*n + (-t+1) = (0 < (k*(-1*c)*n) + (k*(-t+1)))" by(rule ac_lt_eq[of _ 0,OF gr0,simplified])
907   also have "(k*(-1*c)*n) + (k*(-t+1)) = ((-k)*c)*n + ((-k)*t + k)" by(simp add: int_distrib mult_ac)
908   finally show ?thesis .
909 qed
911 lemma binminus_uminus_conv: "(a::int) - b = a + (-b)"
912 by arith
914 lemma  linearize_dvd: "(t::int) = t1 ==> (d dvd t) = (d dvd t1)"
915 by simp
917 lemma lf_lt: "(l::int) = ll ==> (r::int) = lr ==> (l < r) =(ll < lr)"
918 by simp
920 lemma lf_eq: "(l::int) = ll ==> (r::int) = lr ==> (l = r) =(ll = lr)"
921 by simp
923 lemma lf_dvd: "(l::int) = ll ==> (r::int) = lr ==> (l dvd r) =(ll dvd lr)"
924 by simp
926 text {* \bigskip Theorems for transforming predicates on nat to predicates on @{text int}*}
928 theorem all_nat: "(\<forall>x::nat. P x) = (\<forall>x::int. 0 <= x \<longrightarrow> P (nat x))"
929   by (simp split add: split_nat)
931 theorem ex_nat: "(\<exists>x::nat. P x) = (\<exists>x::int. 0 <= x \<and> P (nat x))"
932   apply (simp split add: split_nat)
933   apply (rule iffI)
934   apply (erule exE)
935   apply (rule_tac x = "int x" in exI)
936   apply simp
937   apply (erule exE)
938   apply (rule_tac x = "nat x" in exI)
939   apply (erule conjE)
940   apply (erule_tac x = "nat x" in allE)
941   apply simp
942   done
944 theorem zdiff_int_split: "P (int (x - y)) =
945   ((y \<le> x \<longrightarrow> P (int x - int y)) \<and> (x < y \<longrightarrow> P 0))"
946   apply (case_tac "y \<le> x")
947   apply (simp_all add: zdiff_int)
948   done
950 theorem zdvd_int: "(x dvd y) = (int x dvd int y)"
951   apply (simp only: dvd_def ex_nat int_int_eq [symmetric] zmult_int [symmetric]
952     nat_0_le cong add: conj_cong)
953   apply (rule iffI)
954   apply rules
955   apply (erule exE)
956   apply (case_tac "x=0")
957   apply (rule_tac x=0 in exI)
958   apply simp
959   apply (case_tac "0 \<le> k")
960   apply rules
961   apply (simp add: linorder_not_le)
962   apply (drule mult_strict_left_mono_neg [OF iffD2 [OF zero_less_int_conv]])
963   apply assumption
964   apply (simp add: mult_ac)
965   done
967 theorem number_of1: "(0::int) <= number_of n \<Longrightarrow> (0::int) <= number_of (n BIT b)"
968   by simp
970 theorem number_of2: "(0::int) <= Numeral0" by simp
972 theorem Suc_plus1: "Suc n = n + 1" by simp
974 text {*
975   \medskip Specific instances of congruence rules, to prevent
976   simplifier from looping. *}
978 theorem imp_le_cong: "(0 <= x \<Longrightarrow> P = P') \<Longrightarrow> (0 <= (x::int) \<longrightarrow> P) = (0 <= x \<longrightarrow> P')"
979   by simp
981 theorem conj_le_cong: "(0 <= x \<Longrightarrow> P = P') \<Longrightarrow> (0 <= (x::int) \<and> P) = (0 <= x \<and> P')"
982   by (simp cong: conj_cong)
984 use "cooper_dec.ML"
985 oracle
986   presburger_oracle = CooperDec.mk_presburger_oracle
988 use "cooper_proof.ML"
989 use "qelim.ML"
990 use "presburger.ML"
992 setup "Presburger.setup"
994 end