1 (* Title: HOL/Presburger.thy
2 Author: Amine Chaieb, TU Muenchen
5 header {* Decision Procedure for Presburger Arithmetic *}
8 imports Groebner_Basis Set_Interval
11 ML_file "Tools/Qelim/qelim.ML"
12 ML_file "Tools/Qelim/cooper_procedure.ML"
14 subsection{* The @{text "-\<infinity>"} and @{text "+\<infinity>"} Properties *}
17 "\<lbrakk>\<exists>(z ::'a::linorder).\<forall>x<z. P x = P' x; \<exists>z.\<forall>x<z. Q x = Q' x\<rbrakk>
18 \<Longrightarrow> \<exists>z.\<forall>x<z. (P x \<and> Q x) = (P' x \<and> Q' x)"
19 "\<lbrakk>\<exists>(z ::'a::linorder).\<forall>x<z. P x = P' x; \<exists>z.\<forall>x<z. Q x = Q' x\<rbrakk>
20 \<Longrightarrow> \<exists>z.\<forall>x<z. (P x \<or> Q x) = (P' x \<or> Q' x)"
21 "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x = t) = False"
22 "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x \<noteq> t) = True"
23 "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x < t) = True"
24 "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x \<le> t) = True"
25 "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x > t) = False"
26 "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x \<ge> t) = False"
27 "\<exists>z.\<forall>(x::'b::{linorder,plus,Rings.dvd})<z. (d dvd x + s) = (d dvd x + s)"
28 "\<exists>z.\<forall>(x::'b::{linorder,plus,Rings.dvd})<z. (\<not> d dvd x + s) = (\<not> d dvd x + s)"
29 "\<exists>z.\<forall>x<z. F = F"
30 by ((erule exE, erule exE,rule_tac x="min z za" in exI,simp)+, (rule_tac x="t" in exI,fastforce)+) simp_all
33 "\<lbrakk>\<exists>(z ::'a::linorder).\<forall>x>z. P x = P' x; \<exists>z.\<forall>x>z. Q x = Q' x\<rbrakk>
34 \<Longrightarrow> \<exists>z.\<forall>x>z. (P x \<and> Q x) = (P' x \<and> Q' x)"
35 "\<lbrakk>\<exists>(z ::'a::linorder).\<forall>x>z. P x = P' x; \<exists>z.\<forall>x>z. Q x = Q' x\<rbrakk>
36 \<Longrightarrow> \<exists>z.\<forall>x>z. (P x \<or> Q x) = (P' x \<or> Q' x)"
37 "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x = t) = False"
38 "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x \<noteq> t) = True"
39 "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x < t) = False"
40 "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x \<le> t) = False"
41 "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x > t) = True"
42 "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x \<ge> t) = True"
43 "\<exists>z.\<forall>(x::'b::{linorder,plus,Rings.dvd})>z. (d dvd x + s) = (d dvd x + s)"
44 "\<exists>z.\<forall>(x::'b::{linorder,plus,Rings.dvd})>z. (\<not> d dvd x + s) = (\<not> d dvd x + s)"
45 "\<exists>z.\<forall>x>z. F = F"
46 by ((erule exE, erule exE,rule_tac x="max z za" in exI,simp)+,(rule_tac x="t" in exI,fastforce)+) simp_all
49 "\<lbrakk>\<forall>x k. P x = P (x - k*D); \<forall>x k. Q x = Q (x - k*D)\<rbrakk>
50 \<Longrightarrow> \<forall>x k. (P x \<and> Q x) = (P (x - k*D) \<and> Q (x - k*D))"
51 "\<lbrakk>\<forall>x k. P x = P (x - k*D); \<forall>x k. Q x = Q (x - k*D)\<rbrakk>
52 \<Longrightarrow> \<forall>x k. (P x \<or> Q x) = (P (x - k*D) \<or> Q (x - k*D))"
53 "(d::'a::{comm_ring,Rings.dvd}) dvd D \<Longrightarrow> \<forall>x k. (d dvd x + t) = (d dvd (x - k*D) + t)"
54 "(d::'a::{comm_ring,Rings.dvd}) dvd D \<Longrightarrow> \<forall>x k. (\<not>d dvd x + t) = (\<not>d dvd (x - k*D) + t)"
56 apply (auto elim!: dvdE simp add: algebra_simps)
57 unfolding mult.assoc [symmetric] distrib_right [symmetric] left_diff_distrib [symmetric]
58 unfolding dvd_def mult.commute [of d]
61 subsection{* The A and B sets *}
63 "\<lbrakk>\<forall>x.(\<forall>j \<in> {1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> P x \<longrightarrow> P(x - D) ;
64 \<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> Q x \<longrightarrow> Q(x - D)\<rbrakk> \<Longrightarrow>
65 \<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j) \<longrightarrow> (P x \<and> Q x) \<longrightarrow> (P(x - D) \<and> Q (x - D))"
66 "\<lbrakk>\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> P x \<longrightarrow> P(x - D) ;
67 \<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> Q x \<longrightarrow> Q(x - D)\<rbrakk> \<Longrightarrow>
68 \<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> (P x \<or> Q x) \<longrightarrow> (P(x - D) \<or> Q (x - D))"
69 "\<lbrakk>D>0; t - 1\<in> B\<rbrakk> \<Longrightarrow> (\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> (x = t) \<longrightarrow> (x - D = t))"
70 "\<lbrakk>D>0 ; t \<in> B\<rbrakk> \<Longrightarrow>(\<forall>(x::int).(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> (x \<noteq> t) \<longrightarrow> (x - D \<noteq> t))"
71 "D>0 \<Longrightarrow> (\<forall>(x::int).(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> (x < t) \<longrightarrow> (x - D < t))"
72 "D>0 \<Longrightarrow> (\<forall>(x::int).(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> (x \<le> t) \<longrightarrow> (x - D \<le> t))"
73 "\<lbrakk>D>0 ; t \<in> B\<rbrakk> \<Longrightarrow>(\<forall>(x::int).(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> (x > t) \<longrightarrow> (x - D > t))"
74 "\<lbrakk>D>0 ; t - 1 \<in> B\<rbrakk> \<Longrightarrow>(\<forall>(x::int).(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> (x \<ge> t) \<longrightarrow> (x - D \<ge> t))"
75 "d dvd D \<Longrightarrow>(\<forall>(x::int).(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> (d dvd x+t) \<longrightarrow> (d dvd (x - D) + t))"
76 "d dvd D \<Longrightarrow>(\<forall>(x::int).(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> (\<not>d dvd x+t) \<longrightarrow> (\<not> d dvd (x - D) + t))"
77 "\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j) \<longrightarrow> F \<longrightarrow> F"
79 assume dp: "D > 0" and tB: "t - 1\<in> B"
80 show "(\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> (x = t) \<longrightarrow> (x - D = t))"
81 apply (rule allI, rule impI,erule ballE[where x="1"],erule ballE[where x="t - 1"])
82 apply algebra using dp tB by simp_all
84 assume dp: "D > 0" and tB: "t \<in> B"
85 show "(\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> (x \<noteq> t) \<longrightarrow> (x - D \<noteq> t))"
86 apply (rule allI, rule impI,erule ballE[where x="D"],erule ballE[where x="t"])
88 using dp tB by simp_all
90 assume dp: "D > 0" thus "(\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> (x < t) \<longrightarrow> (x - D < t))" by arith
92 assume dp: "D > 0" thus "\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> (x \<le> t) \<longrightarrow> (x - D \<le> t)" by arith
94 assume dp: "D > 0" and tB:"t \<in> B"
95 {fix x assume nob: "\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j" and g: "x > t" and ng: "\<not> (x - D) > t"
96 hence "x -t \<le> D" and "1 \<le> x - t" by simp+
97 hence "\<exists>j \<in> {1 .. D}. x - t = j" by auto
98 hence "\<exists>j \<in> {1 .. D}. x = t + j" by (simp add: algebra_simps)
99 with nob tB have "False" by simp}
100 thus "\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> (x > t) \<longrightarrow> (x - D > t)" by blast
102 assume dp: "D > 0" and tB:"t - 1\<in> B"
103 {fix x assume nob: "\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j" and g: "x \<ge> t" and ng: "\<not> (x - D) \<ge> t"
104 hence "x - (t - 1) \<le> D" and "1 \<le> x - (t - 1)" by simp+
105 hence "\<exists>j \<in> {1 .. D}. x - (t - 1) = j" by auto
106 hence "\<exists>j \<in> {1 .. D}. x = (t - 1) + j" by (simp add: algebra_simps)
107 with nob tB have "False" by simp}
108 thus "\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> (x \<ge> t) \<longrightarrow> (x - D \<ge> t)" by blast
111 {fix x assume H: "d dvd x + t" with d have "d dvd (x - D) + t" by algebra}
112 thus "\<forall>(x::int).(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> (d dvd x+t) \<longrightarrow> (d dvd (x - D) + t)" by simp
115 {fix x assume H: "\<not>(d dvd x + t)" with d have "\<not> d dvd (x - D) + t"
116 by (clarsimp simp add: dvd_def,erule_tac x= "ka + k" in allE,simp add: algebra_simps)}
117 thus "\<forall>(x::int).(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> (\<not>d dvd x+t) \<longrightarrow> (\<not>d dvd (x - D) + t)" by auto
121 "\<lbrakk>\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> P x \<longrightarrow> P(x + D) ;
122 \<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> Q x \<longrightarrow> Q(x + D)\<rbrakk> \<Longrightarrow>
123 \<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j) \<longrightarrow> (P x \<and> Q x) \<longrightarrow> (P(x + D) \<and> Q (x + D))"
124 "\<lbrakk>\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> P x \<longrightarrow> P(x + D) ;
125 \<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> Q x \<longrightarrow> Q(x + D)\<rbrakk> \<Longrightarrow>
126 \<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> (P x \<or> Q x) \<longrightarrow> (P(x + D) \<or> Q (x + D))"
127 "\<lbrakk>D>0; t + 1\<in> A\<rbrakk> \<Longrightarrow> (\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> (x = t) \<longrightarrow> (x + D = t))"
128 "\<lbrakk>D>0 ; t \<in> A\<rbrakk> \<Longrightarrow>(\<forall>(x::int).(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> (x \<noteq> t) \<longrightarrow> (x + D \<noteq> t))"
129 "\<lbrakk>D>0; t\<in> A\<rbrakk> \<Longrightarrow>(\<forall>(x::int). (\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> (x < t) \<longrightarrow> (x + D < t))"
130 "\<lbrakk>D>0; t + 1 \<in> A\<rbrakk> \<Longrightarrow> (\<forall>(x::int).(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> (x \<le> t) \<longrightarrow> (x + D \<le> t))"
131 "D>0 \<Longrightarrow>(\<forall>(x::int).(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> (x > t) \<longrightarrow> (x + D > t))"
132 "D>0 \<Longrightarrow>(\<forall>(x::int).(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> (x \<ge> t) \<longrightarrow> (x + D \<ge> t))"
133 "d dvd D \<Longrightarrow>(\<forall>(x::int).(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> (d dvd x+t) \<longrightarrow> (d dvd (x + D) + t))"
134 "d dvd D \<Longrightarrow>(\<forall>(x::int).(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> (\<not>d dvd x+t) \<longrightarrow> (\<not> d dvd (x + D) + t))"
135 "\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j) \<longrightarrow> F \<longrightarrow> F"
137 assume dp: "D > 0" and tA: "t + 1 \<in> A"
138 show "(\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> (x = t) \<longrightarrow> (x + D = t))"
139 apply (rule allI, rule impI,erule ballE[where x="1"],erule ballE[where x="t + 1"])
140 using dp tA by simp_all
142 assume dp: "D > 0" and tA: "t \<in> A"
143 show "(\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> (x \<noteq> t) \<longrightarrow> (x + D \<noteq> t))"
144 apply (rule allI, rule impI,erule ballE[where x="D"],erule ballE[where x="t"])
145 using dp tA by simp_all
147 assume dp: "D > 0" thus "(\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> (x > t) \<longrightarrow> (x + D > t))" by arith
149 assume dp: "D > 0" thus "\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> (x \<ge> t) \<longrightarrow> (x + D \<ge> t)" by arith
151 assume dp: "D > 0" and tA:"t \<in> A"
152 {fix x assume nob: "\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j" and g: "x < t" and ng: "\<not> (x + D) < t"
153 hence "t - x \<le> D" and "1 \<le> t - x" by simp+
154 hence "\<exists>j \<in> {1 .. D}. t - x = j" by auto
155 hence "\<exists>j \<in> {1 .. D}. x = t - j" by (auto simp add: algebra_simps)
156 with nob tA have "False" by simp}
157 thus "\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> (x < t) \<longrightarrow> (x + D < t)" by blast
159 assume dp: "D > 0" and tA:"t + 1\<in> A"
160 {fix x assume nob: "\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j" and g: "x \<le> t" and ng: "\<not> (x + D) \<le> t"
161 hence "(t + 1) - x \<le> D" and "1 \<le> (t + 1) - x" by (simp_all add: algebra_simps)
162 hence "\<exists>j \<in> {1 .. D}. (t + 1) - x = j" by auto
163 hence "\<exists>j \<in> {1 .. D}. x = (t + 1) - j" by (auto simp add: algebra_simps)
164 with nob tA have "False" by simp}
165 thus "\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> (x \<le> t) \<longrightarrow> (x + D \<le> t)" by blast
168 {fix x assume H: "d dvd x + t" with d have "d dvd (x + D) + t"
169 by (clarsimp simp add: dvd_def,rule_tac x= "ka + k" in exI,simp add: algebra_simps)}
170 thus "\<forall>(x::int).(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> (d dvd x+t) \<longrightarrow> (d dvd (x + D) + t)" by simp
173 {fix x assume H: "\<not>(d dvd x + t)" with d have "\<not>d dvd (x + D) + t"
174 by (clarsimp simp add: dvd_def,erule_tac x= "ka - k" in allE,simp add: algebra_simps)}
175 thus "\<forall>(x::int).(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> (\<not>d dvd x+t) \<longrightarrow> (\<not>d dvd (x + D) + t)" by auto
178 subsection{* Cooper's Theorem @{text "-\<infinity>"} and @{text "+\<infinity>"} Version *}
180 subsubsection{* First some trivial facts about periodic sets or predicates *}
181 lemma periodic_finite_ex:
182 assumes dpos: "(0::int) < d" and modd: "ALL x k. P x = P(x - k*d)"
183 shows "(EX x. P x) = (EX j : {1..d}. P j)"
187 then obtain x where P: "P x" ..
188 have "x mod d = x - (x div d)*d" by(simp add:zmod_zdiv_equality ac_simps eq_diff_eq)
189 hence Pmod: "P x = P(x mod d)" using modd by simp
193 hence "P 0" using P Pmod by simp
194 moreover have "P 0 = P(0 - (-1)*d)" using modd by blast
195 ultimately have "P d" by simp
196 moreover have "d : {1..d}" using dpos by simp
197 ultimately show ?RHS ..
199 assume not0: "x mod d \<noteq> 0"
200 have "P(x mod d)" using dpos P Pmod by simp
201 moreover have "x mod d : {1..d}"
203 from dpos have "0 \<le> x mod d" by(rule pos_mod_sign)
204 moreover from dpos have "x mod d < d" by(rule pos_mod_bound)
205 ultimately show ?thesis using not0 by simp
207 ultimately show ?RHS ..
211 subsubsection{* The @{text "-\<infinity>"} Version*}
213 lemma decr_lemma: "0 < (d::int) \<Longrightarrow> x - (abs(x-z)+1) * d < z"
214 by(induct rule: int_gr_induct,simp_all add:int_distrib)
216 lemma incr_lemma: "0 < (d::int) \<Longrightarrow> z < x + (abs(x-z)+1) * d"
217 by(induct rule: int_gr_induct, simp_all add:int_distrib)
219 lemma decr_mult_lemma:
220 assumes dpos: "(0::int) < d" and minus: "\<forall>x. P x \<longrightarrow> P(x - d)" and knneg: "0 <= k"
221 shows "ALL x. P x \<longrightarrow> P(x - k*d)"
223 proof (induct rule:int_ge_induct)
224 case base thus ?case by simp
228 have "P x \<longrightarrow> P (x - i * d)" using step.hyps by blast
229 also have "\<dots> \<longrightarrow> P(x - (i + 1) * d)" using minus[THEN spec, of "x - i * d"]
230 by (simp add: algebra_simps)
231 ultimately have "P x \<longrightarrow> P(x - (i + 1) * d)" by blast}
236 assumes dpos: "0 < d" and
237 P1eqP1: "ALL x k. P1 x = P1(x - k*d)" and ePeqP1: "EX z::int. ALL x. x < z \<longrightarrow> (P x = P1 x)"
238 shows "(EX x. P1 x) \<longrightarrow> (EX x. P x)"
240 assume eP1: "EX x. P1 x"
241 then obtain x where P1: "P1 x" ..
242 from ePeqP1 obtain z where P1eqP: "ALL x. x < z \<longrightarrow> (P x = P1 x)" ..
243 let ?w = "x - (abs(x-z)+1) * d"
244 from dpos have w: "?w < z" by(rule decr_lemma)
245 have "P1 x = P1 ?w" using P1eqP1 by blast
246 also have "\<dots> = P(?w)" using w P1eqP by blast
247 finally have "P ?w" using P1 by blast
252 assumes dp: "0 < D" and p1:"\<exists>z. \<forall> x< z. P x = P' x"
253 and nb:"\<forall>x.(\<forall> j\<in> {1..D}. \<forall>(b::int) \<in> B. x \<noteq> b+j) --> P (x) --> P (x - D)"
254 and pd: "\<forall> x k. P' x = P' (x-k*D)"
255 shows "(\<exists>x. P x) = ((\<exists> j\<in> {1..D} . P' j) | (\<exists> j \<in> {1..D}.\<exists> b\<in> B. P (b+j)))"
256 (is "?L = (?R1 \<or> ?R2)")
258 {assume "?R2" hence "?L" by blast}
260 {assume H:"?R1" hence "?L" using minusinfinity[OF dp pd p1] periodic_finite_ex[OF dp pd] by simp}
263 assume P: "P x" and H: "\<not> ?R2"
264 {fix y assume "\<not> (\<exists>j\<in>{1..D}. \<exists>b\<in>B. P (b + j))" and P: "P y"
265 hence "~(EX (j::int) : {1..D}. EX (b::int) : B. y = b+j)" by auto
266 with nb P have "P (y - D)" by auto }
267 hence "ALL x.~(EX (j::int) : {1..D}. EX (b::int) : B. P(b+j)) --> P (x) --> P (x - D)" by blast
268 with H P have th: " \<forall>x. P x \<longrightarrow> P (x - D)" by auto
269 from p1 obtain z where z: "ALL x. x < z --> (P x = P' x)" by blast
270 let ?y = "x - (\<bar>x - z\<bar> + 1)*D"
271 have zp: "0 <= (\<bar>x - z\<bar> + 1)" by arith
272 from dp have yz: "?y < z" using decr_lemma[OF dp] by simp
273 from z[rule_format, OF yz] decr_mult_lemma[OF dp th zp, rule_format, OF P] have th2: " P' ?y" by auto
274 with periodic_finite_ex[OF dp pd]
276 ultimately show ?thesis by blast
279 subsubsection {* The @{text "+\<infinity>"} Version*}
282 assumes dpos: "(0::int) < d" and
283 P1eqP1: "\<forall>x k. P' x = P'(x - k*d)" and ePeqP1: "\<exists> z. \<forall> x>z. P x = P' x"
284 shows "(\<exists> x. P' x) \<longrightarrow> (\<exists> x. P x)"
286 assume eP1: "EX x. P' x"
287 then obtain x where P1: "P' x" ..
288 from ePeqP1 obtain z where P1eqP: "\<forall>x>z. P x = P' x" ..
289 let ?w' = "x + (abs(x-z)+1) * d"
290 let ?w = "x - (-(abs(x-z) + 1))*d"
291 have ww'[simp]: "?w = ?w'" by (simp add: algebra_simps)
292 from dpos have w: "?w > z" by(simp only: ww' incr_lemma)
293 hence "P' x = P' ?w" using P1eqP1 by blast
294 also have "\<dots> = P(?w)" using w P1eqP by blast
295 finally have "P ?w" using P1 by blast
299 lemma incr_mult_lemma:
300 assumes dpos: "(0::int) < d" and plus: "ALL x::int. P x \<longrightarrow> P(x + d)" and knneg: "0 <= k"
301 shows "ALL x. P x \<longrightarrow> P(x + k*d)"
303 proof (induct rule:int_ge_induct)
304 case base thus ?case by simp
308 have "P x \<longrightarrow> P (x + i * d)" using step.hyps by blast
309 also have "\<dots> \<longrightarrow> P(x + (i + 1) * d)" using plus[THEN spec, of "x + i * d"]
310 by (simp add:int_distrib ac_simps)
311 ultimately have "P x \<longrightarrow> P(x + (i + 1) * d)" by blast}
316 assumes dp: "0 < D" and p1:"\<exists>z. \<forall> x> z. P x = P' x"
317 and nb:"\<forall>x.(\<forall> j\<in> {1..D}. \<forall>(b::int) \<in> A. x \<noteq> b - j) --> P (x) --> P (x + D)"
318 and pd: "\<forall> x k. P' x= P' (x-k*D)"
319 shows "(\<exists>x. P x) = ((\<exists> j\<in> {1..D} . P' j) | (\<exists> j \<in> {1..D}.\<exists> b\<in> A. P (b - j)))" (is "?L = (?R1 \<or> ?R2)")
321 {assume "?R2" hence "?L" by blast}
323 {assume H:"?R1" hence "?L" using plusinfinity[OF dp pd p1] periodic_finite_ex[OF dp pd] by simp}
326 assume P: "P x" and H: "\<not> ?R2"
327 {fix y assume "\<not> (\<exists>j\<in>{1..D}. \<exists>b\<in>A. P (b - j))" and P: "P y"
328 hence "~(EX (j::int) : {1..D}. EX (b::int) : A. y = b - j)" by auto
329 with nb P have "P (y + D)" by auto }
330 hence "ALL x.~(EX (j::int) : {1..D}. EX (b::int) : A. P(b-j)) --> P (x) --> P (x + D)" by blast
331 with H P have th: " \<forall>x. P x \<longrightarrow> P (x + D)" by auto
332 from p1 obtain z where z: "ALL x. x > z --> (P x = P' x)" by blast
333 let ?y = "x + (\<bar>x - z\<bar> + 1)*D"
334 have zp: "0 <= (\<bar>x - z\<bar> + 1)" by arith
335 from dp have yz: "?y > z" using incr_lemma[OF dp] by simp
336 from z[rule_format, OF yz] incr_mult_lemma[OF dp th zp, rule_format, OF P] have th2: " P' ?y" by auto
337 with periodic_finite_ex[OF dp pd]
339 ultimately show ?thesis by blast
342 lemma simp_from_to: "{i..j::int} = (if j < i then {} else insert i {i+1..j})"
343 apply(simp add:atLeastAtMost_def atLeast_def atMost_def)
347 theorem unity_coeff_ex: "(\<exists>(x::'a::{semiring_0,Rings.dvd}). P (l * x)) \<equiv> (\<exists>x. l dvd (x + 0) \<and> P x)"
348 apply (rule eq_reflection [symmetric])
352 apply (rule_tac x = "l * x" in exI)
353 apply (simp add: dvd_def)
354 apply (rule_tac x = x in exI, simp)
359 apply (rule_tac x = k in exI)
365 assumes "k \<noteq> 0"
366 shows "m dvd t \<equiv> k * m dvd k * t"
369 lemma uminus_dvd_conv:
371 shows "d dvd t \<equiv> - d dvd t" and "d dvd t \<equiv> d dvd - t"
374 text {* \bigskip Theorems for transforming predicates on nat to predicates on @{text int}*}
376 lemma zdiff_int_split: "P (int (x - y)) =
377 ((y \<le> x \<longrightarrow> P (int x - int y)) \<and> (x < y \<longrightarrow> P 0))"
378 by (cases "y \<le> x") (simp_all add: zdiff_int)
381 \medskip Specific instances of congruence rules, to prevent
382 simplifier from looping. *}
385 "\<lbrakk>x = x'; 0 \<le> x' \<Longrightarrow> P = P'\<rbrakk> \<Longrightarrow> (0 \<le> (x::int) \<longrightarrow> P) = (0 \<le> x' \<longrightarrow> P')"
388 theorem conj_le_cong:
389 "\<lbrakk>x = x'; 0 \<le> x' \<Longrightarrow> P = P'\<rbrakk> \<Longrightarrow> (0 \<le> (x::int) \<and> P) = (0 \<le> x' \<and> P')"
390 by (simp cong: conj_cong)
392 ML_file "Tools/Qelim/cooper.ML"
395 method_setup presburger = {*
397 fun keyword k = Scan.lift (Args.$$$ k -- Args.colon) >> K ()
398 fun simple_keyword k = Scan.lift (Args.$$$ k) >> K ()
402 val any_keyword = keyword addN || keyword delN || simple_keyword elimN
403 val thms = Scan.repeat (Scan.unless any_keyword Attrib.multi_thm) >> flat;
405 Scan.optional (simple_keyword elimN >> K false) true --
406 Scan.optional (keyword addN |-- thms) [] --
407 Scan.optional (keyword delN |-- thms) [] >>
408 (fn ((elim, add_ths), del_ths) => fn ctxt =>
409 SIMPLE_METHOD' (Cooper.tac elim add_ths del_ths ctxt))
411 *} "Cooper's algorithm for Presburger arithmetic"
413 declare dvd_eq_mod_eq_0 [symmetric, presburger]
414 declare mod_1 [presburger]
415 declare mod_0 [presburger]
416 declare mod_by_1 [presburger]
417 declare mod_self [presburger]
418 declare div_by_0 [presburger]
419 declare mod_by_0 [presburger]
420 declare mod_div_trivial [presburger]
421 declare div_mod_equality2 [presburger]
422 declare div_mod_equality [presburger]
423 declare mod_div_equality2 [presburger]
424 declare mod_div_equality [presburger]
425 declare mod_mult_self1 [presburger]
426 declare mod_mult_self2 [presburger]
427 declare mod2_Suc_Suc[presburger]
428 declare not_mod_2_eq_0_eq_1 [presburger]
429 declare nat_zero_less_power_iff [presburger]
431 lemma [presburger, algebra]: "m mod 2 = (1::nat) \<longleftrightarrow> \<not> 2 dvd m " by presburger
432 lemma [presburger, algebra]: "m mod 2 = Suc 0 \<longleftrightarrow> \<not> 2 dvd m " by presburger
433 lemma [presburger, algebra]: "m mod (Suc (Suc 0)) = (1::nat) \<longleftrightarrow> \<not> 2 dvd m " by presburger
434 lemma [presburger, algebra]: "m mod (Suc (Suc 0)) = Suc 0 \<longleftrightarrow> \<not> 2 dvd m " by presburger
435 lemma [presburger, algebra]: "m mod 2 = (1::int) \<longleftrightarrow> \<not> 2 dvd m " by presburger
437 context semiring_parity
440 declare even_times_iff [presburger]
442 declare even_power [presburger]
445 "even (a + b) \<longleftrightarrow> even a \<and> even b \<or> odd a \<and> odd b"
453 declare even_minus [presburger]
457 context linordered_idom
460 declare zero_le_power_iff [presburger]
462 declare zero_le_power_eq [presburger]
464 declare zero_less_power_eq [presburger]
466 declare power_less_zero_eq [presburger]
468 declare power_le_zero_eq [presburger]
472 declare even_Suc [presburger]
475 "Suc n div Suc (Suc 0) = n div Suc (Suc 0) \<longleftrightarrow> even n"
478 declare even_diff_nat [presburger]
482 shows "(k + 1) div 2 = k div 2 \<longleftrightarrow> even k"
487 shows "(k + 1) div 2 = k div 2 + 1 \<longleftrightarrow> odd k"
491 "even n \<longleftrightarrow> even (int n)"
492 using even_int_iff [of n] by simp
495 subsection {* Nice facts about division by @{term 4} *}
497 lemma even_even_mod_4_iff:
498 "even (n::nat) \<longleftrightarrow> even (n mod 4)"
501 lemma odd_mod_4_div_2:
502 "n mod 4 = (3::nat) \<Longrightarrow> odd ((n - 1) div 2)"
505 lemma even_mod_4_div_2:
506 "n mod 4 = (1::nat) \<Longrightarrow> even ((n - 1) div 2)"
510 subsection {* Try0 *}
512 ML_file "Tools/try0.ML"