1 (* Title: HOL/Presburger.thy
2 Author: Amine Chaieb, TU Muenchen
5 header {* Decision Procedure for Presburger Arithmetic *}
8 imports Groebner_Basis SetInterval
10 "Tools/Qelim/qelim.ML"
11 "Tools/Qelim/cooper_data.ML"
12 "Tools/Qelim/generated_cooper.ML"
13 ("Tools/Qelim/cooper.ML")
14 ("Tools/Qelim/presburger.ML")
17 setup CooperData.setup
19 subsection{* The @{text "-\<infinity>"} and @{text "+\<infinity>"} Properties *}
23 "\<lbrakk>\<exists>(z ::'a::linorder).\<forall>x<z. P x = P' x; \<exists>z.\<forall>x<z. Q x = Q' x\<rbrakk>
24 \<Longrightarrow> \<exists>z.\<forall>x<z. (P x \<and> Q x) = (P' x \<and> Q' x)"
25 "\<lbrakk>\<exists>(z ::'a::linorder).\<forall>x<z. P x = P' x; \<exists>z.\<forall>x<z. Q x = Q' x\<rbrakk>
26 \<Longrightarrow> \<exists>z.\<forall>x<z. (P x \<or> Q x) = (P' x \<or> Q' x)"
27 "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x = t) = False"
28 "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x \<noteq> t) = True"
29 "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x < t) = True"
30 "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x \<le> t) = True"
31 "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x > t) = False"
32 "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x \<ge> t) = False"
33 "\<exists>z.\<forall>(x::'a::{linorder,plus,Ring_and_Field.dvd})<z. (d dvd x + s) = (d dvd x + s)"
34 "\<exists>z.\<forall>(x::'a::{linorder,plus,Ring_and_Field.dvd})<z. (\<not> d dvd x + s) = (\<not> d dvd x + s)"
35 "\<exists>z.\<forall>x<z. F = F"
36 by ((erule exE, erule exE,rule_tac x="min z za" in exI,simp)+, (rule_tac x="t" in exI,fastsimp)+) simp_all
39 "\<lbrakk>\<exists>(z ::'a::linorder).\<forall>x>z. P x = P' x; \<exists>z.\<forall>x>z. Q x = Q' x\<rbrakk>
40 \<Longrightarrow> \<exists>z.\<forall>x>z. (P x \<and> Q x) = (P' x \<and> Q' x)"
41 "\<lbrakk>\<exists>(z ::'a::linorder).\<forall>x>z. P x = P' x; \<exists>z.\<forall>x>z. Q x = Q' x\<rbrakk>
42 \<Longrightarrow> \<exists>z.\<forall>x>z. (P x \<or> Q x) = (P' x \<or> Q' x)"
43 "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x = t) = False"
44 "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x \<noteq> t) = True"
45 "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x < t) = False"
46 "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x \<le> t) = False"
47 "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x > t) = True"
48 "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x \<ge> t) = True"
49 "\<exists>z.\<forall>(x::'a::{linorder,plus,Ring_and_Field.dvd})>z. (d dvd x + s) = (d dvd x + s)"
50 "\<exists>z.\<forall>(x::'a::{linorder,plus,Ring_and_Field.dvd})>z. (\<not> d dvd x + s) = (\<not> d dvd x + s)"
51 "\<exists>z.\<forall>x>z. F = F"
52 by ((erule exE, erule exE,rule_tac x="max z za" in exI,simp)+,(rule_tac x="t" in exI,fastsimp)+) simp_all
55 "\<lbrakk>\<forall>x k. P x = P (x - k*D); \<forall>x k. Q x = Q (x - k*D)\<rbrakk>
56 \<Longrightarrow> \<forall>x k. (P x \<and> Q x) = (P (x - k*D) \<and> Q (x - k*D))"
57 "\<lbrakk>\<forall>x k. P x = P (x - k*D); \<forall>x k. Q x = Q (x - k*D)\<rbrakk>
58 \<Longrightarrow> \<forall>x k. (P x \<or> Q x) = (P (x - k*D) \<or> Q (x - k*D))"
59 "(d::'a::{comm_ring,Ring_and_Field.dvd}) dvd D \<Longrightarrow> \<forall>x k. (d dvd x + t) = (d dvd (x - k*D) + t)"
60 "(d::'a::{comm_ring,Ring_and_Field.dvd}) dvd D \<Longrightarrow> \<forall>x k. (\<not>d dvd x + t) = (\<not>d dvd (x - k*D) + t)"
62 apply (auto elim!: dvdE simp add: algebra_simps)
63 unfolding mult_assoc [symmetric] left_distrib [symmetric] left_diff_distrib [symmetric]
64 unfolding dvd_def mult_commute [of d]
67 subsection{* The A and B sets *}
69 "\<lbrakk>\<forall>x.(\<forall>j \<in> {1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> P x \<longrightarrow> P(x - D) ;
70 \<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> Q x \<longrightarrow> Q(x - D)\<rbrakk> \<Longrightarrow>
71 \<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))"
72 "\<lbrakk>\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> P x \<longrightarrow> P(x - D) ;
73 \<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> Q x \<longrightarrow> Q(x - D)\<rbrakk> \<Longrightarrow>
74 \<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))"
75 "\<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))"
76 "\<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))"
77 "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))"
78 "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))"
79 "\<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))"
80 "\<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))"
81 "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))"
82 "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))"
83 "\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j) \<longrightarrow> F \<longrightarrow> F"
85 assume dp: "D > 0" and tB: "t - 1\<in> B"
86 show "(\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> (x = t) \<longrightarrow> (x - D = t))"
87 apply (rule allI, rule impI,erule ballE[where x="1"],erule ballE[where x="t - 1"])
88 apply algebra using dp tB by simp_all
90 assume dp: "D > 0" and tB: "t \<in> B"
91 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))"
92 apply (rule allI, rule impI,erule ballE[where x="D"],erule ballE[where x="t"])
94 using dp tB by simp_all
96 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
98 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
100 assume dp: "D > 0" and tB:"t \<in> B"
101 {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"
102 hence "x -t \<le> D" and "1 \<le> x - t" by simp+
103 hence "\<exists>j \<in> {1 .. D}. x - t = j" by auto
104 hence "\<exists>j \<in> {1 .. D}. x = t + j" by (simp add: algebra_simps)
105 with nob tB have "False" by simp}
106 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
108 assume dp: "D > 0" and tB:"t - 1\<in> B"
109 {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"
110 hence "x - (t - 1) \<le> D" and "1 \<le> x - (t - 1)" by simp+
111 hence "\<exists>j \<in> {1 .. D}. x - (t - 1) = j" by auto
112 hence "\<exists>j \<in> {1 .. D}. x = (t - 1) + j" by (simp add: algebra_simps)
113 with nob tB have "False" by simp}
114 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
117 {fix x assume H: "d dvd x + t" with d have "d dvd (x - D) + t" by algebra}
118 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
121 {fix x assume H: "\<not>(d dvd x + t)" with d have "\<not> d dvd (x - D) + t"
122 by (clarsimp simp add: dvd_def,erule_tac x= "ka + k" in allE,simp add: algebra_simps)}
123 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
127 "\<lbrakk>\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> P x \<longrightarrow> P(x + D) ;
128 \<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> Q x \<longrightarrow> Q(x + D)\<rbrakk> \<Longrightarrow>
129 \<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))"
130 "\<lbrakk>\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> P x \<longrightarrow> P(x + D) ;
131 \<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> Q x \<longrightarrow> Q(x + D)\<rbrakk> \<Longrightarrow>
132 \<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))"
133 "\<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))"
134 "\<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))"
135 "\<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))"
136 "\<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))"
137 "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))"
138 "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))"
139 "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))"
140 "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))"
141 "\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j) \<longrightarrow> F \<longrightarrow> F"
143 assume dp: "D > 0" and tA: "t + 1 \<in> A"
144 show "(\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> (x = t) \<longrightarrow> (x + D = t))"
145 apply (rule allI, rule impI,erule ballE[where x="1"],erule ballE[where x="t + 1"])
146 using dp tA by simp_all
148 assume dp: "D > 0" and tA: "t \<in> A"
149 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))"
150 apply (rule allI, rule impI,erule ballE[where x="D"],erule ballE[where x="t"])
151 using dp tA by simp_all
153 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
155 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
157 assume dp: "D > 0" and tA:"t \<in> A"
158 {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"
159 hence "t - x \<le> D" and "1 \<le> t - x" by simp+
160 hence "\<exists>j \<in> {1 .. D}. t - x = j" by auto
161 hence "\<exists>j \<in> {1 .. D}. x = t - j" by (auto simp add: algebra_simps)
162 with nob tA have "False" by simp}
163 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
165 assume dp: "D > 0" and tA:"t + 1\<in> A"
166 {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"
167 hence "(t + 1) - x \<le> D" and "1 \<le> (t + 1) - x" by (simp_all add: algebra_simps)
168 hence "\<exists>j \<in> {1 .. D}. (t + 1) - x = j" by auto
169 hence "\<exists>j \<in> {1 .. D}. x = (t + 1) - j" by (auto simp add: algebra_simps)
170 with nob tA have "False" by simp}
171 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
174 {fix x assume H: "d dvd x + t" with d have "d dvd (x + D) + t"
175 by (clarsimp simp add: dvd_def,rule_tac x= "ka + k" in exI,simp add: algebra_simps)}
176 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
179 {fix x assume H: "\<not>(d dvd x + t)" with d have "\<not>d dvd (x + D) + t"
180 by (clarsimp simp add: dvd_def,erule_tac x= "ka - k" in allE,simp add: algebra_simps)}
181 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
184 subsection{* Cooper's Theorem @{text "-\<infinity>"} and @{text "+\<infinity>"} Version *}
186 subsubsection{* First some trivial facts about periodic sets or predicates *}
187 lemma periodic_finite_ex:
188 assumes dpos: "(0::int) < d" and modd: "ALL x k. P x = P(x - k*d)"
189 shows "(EX x. P x) = (EX j : {1..d}. P j)"
193 then obtain x where P: "P x" ..
194 have "x mod d = x - (x div d)*d" by(simp add:zmod_zdiv_equality mult_ac eq_diff_eq)
195 hence Pmod: "P x = P(x mod d)" using modd by simp
199 hence "P 0" using P Pmod by simp
200 moreover have "P 0 = P(0 - (-1)*d)" using modd by blast
201 ultimately have "P d" by simp
202 moreover have "d : {1..d}" using dpos by(simp add:atLeastAtMost_iff)
203 ultimately show ?RHS ..
205 assume not0: "x mod d \<noteq> 0"
206 have "P(x mod d)" using dpos P Pmod by(simp add:pos_mod_sign pos_mod_bound)
207 moreover have "x mod d : {1..d}"
209 from dpos have "0 \<le> x mod d" by(rule pos_mod_sign)
210 moreover from dpos have "x mod d < d" by(rule pos_mod_bound)
211 ultimately show ?thesis using not0 by(simp add:atLeastAtMost_iff)
213 ultimately show ?RHS ..
217 subsubsection{* The @{text "-\<infinity>"} Version*}
219 lemma decr_lemma: "0 < (d::int) \<Longrightarrow> x - (abs(x-z)+1) * d < z"
220 by(induct rule: int_gr_induct,simp_all add:int_distrib)
222 lemma incr_lemma: "0 < (d::int) \<Longrightarrow> z < x + (abs(x-z)+1) * d"
223 by(induct rule: int_gr_induct, simp_all add:int_distrib)
225 theorem int_induct[case_names base step1 step2]:
227 base: "P(k::int)" and step1: "\<And>i. \<lbrakk>k \<le> i; P i\<rbrakk> \<Longrightarrow> P(i+1)" and
228 step2: "\<And>i. \<lbrakk>k \<ge> i; P i\<rbrakk> \<Longrightarrow> P(i - 1)"
231 have "i \<le> k \<or> i\<ge> k" by arith
232 thus ?thesis using prems int_ge_induct[where P="P" and k="k" and i="i"] int_le_induct[where P="P" and k="k" and i="i"] by blast
235 lemma decr_mult_lemma:
236 assumes dpos: "(0::int) < d" and minus: "\<forall>x. P x \<longrightarrow> P(x - d)" and knneg: "0 <= k"
237 shows "ALL x. P x \<longrightarrow> P(x - k*d)"
239 proof (induct rule:int_ge_induct)
240 case base thus ?case by simp
244 have "P x \<longrightarrow> P (x - i * d)" using step.hyps by blast
245 also have "\<dots> \<longrightarrow> P(x - (i + 1) * d)" using minus[THEN spec, of "x - i * d"]
246 by (simp add:int_distrib OrderedGroup.diff_diff_eq[symmetric])
247 ultimately have "P x \<longrightarrow> P(x - (i + 1) * d)" by blast}
252 assumes dpos: "0 < d" and
253 P1eqP1: "ALL x k. P1 x = P1(x - k*d)" and ePeqP1: "EX z::int. ALL x. x < z \<longrightarrow> (P x = P1 x)"
254 shows "(EX x. P1 x) \<longrightarrow> (EX x. P x)"
256 assume eP1: "EX x. P1 x"
257 then obtain x where P1: "P1 x" ..
258 from ePeqP1 obtain z where P1eqP: "ALL x. x < z \<longrightarrow> (P x = P1 x)" ..
259 let ?w = "x - (abs(x-z)+1) * d"
260 from dpos have w: "?w < z" by(rule decr_lemma)
261 have "P1 x = P1 ?w" using P1eqP1 by blast
262 also have "\<dots> = P(?w)" using w P1eqP by blast
263 finally have "P ?w" using P1 by blast
268 assumes dp: "0 < D" and p1:"\<exists>z. \<forall> x< z. P x = P' x"
269 and nb:"\<forall>x.(\<forall> j\<in> {1..D}. \<forall>(b::int) \<in> B. x \<noteq> b+j) --> P (x) --> P (x - D)"
270 and pd: "\<forall> x k. P' x = P' (x-k*D)"
271 shows "(\<exists>x. P x) = ((\<exists> j\<in> {1..D} . P' j) | (\<exists> j \<in> {1..D}.\<exists> b\<in> B. P (b+j)))"
272 (is "?L = (?R1 \<or> ?R2)")
274 {assume "?R2" hence "?L" by blast}
276 {assume H:"?R1" hence "?L" using minusinfinity[OF dp pd p1] periodic_finite_ex[OF dp pd] by simp}
279 assume P: "P x" and H: "\<not> ?R2"
280 {fix y assume "\<not> (\<exists>j\<in>{1..D}. \<exists>b\<in>B. P (b + j))" and P: "P y"
281 hence "~(EX (j::int) : {1..D}. EX (b::int) : B. y = b+j)" by auto
282 with nb P have "P (y - D)" by auto }
283 hence "ALL x.~(EX (j::int) : {1..D}. EX (b::int) : B. P(b+j)) --> P (x) --> P (x - D)" by blast
284 with H P have th: " \<forall>x. P x \<longrightarrow> P (x - D)" by auto
285 from p1 obtain z where z: "ALL x. x < z --> (P x = P' x)" by blast
286 let ?y = "x - (\<bar>x - z\<bar> + 1)*D"
287 have zp: "0 <= (\<bar>x - z\<bar> + 1)" by arith
288 from dp have yz: "?y < z" using decr_lemma[OF dp] by simp
289 from z[rule_format, OF yz] decr_mult_lemma[OF dp th zp, rule_format, OF P] have th2: " P' ?y" by auto
290 with periodic_finite_ex[OF dp pd]
292 ultimately show ?thesis by blast
295 subsubsection {* The @{text "+\<infinity>"} Version*}
298 assumes dpos: "(0::int) < d" and
299 P1eqP1: "\<forall>x k. P' x = P'(x - k*d)" and ePeqP1: "\<exists> z. \<forall> x>z. P x = P' x"
300 shows "(\<exists> x. P' x) \<longrightarrow> (\<exists> x. P x)"
302 assume eP1: "EX x. P' x"
303 then obtain x where P1: "P' x" ..
304 from ePeqP1 obtain z where P1eqP: "\<forall>x>z. P x = P' x" ..
305 let ?w' = "x + (abs(x-z)+1) * d"
306 let ?w = "x - (-(abs(x-z) + 1))*d"
307 have ww'[simp]: "?w = ?w'" by (simp add: algebra_simps)
308 from dpos have w: "?w > z" by(simp only: ww' incr_lemma)
309 hence "P' x = P' ?w" using P1eqP1 by blast
310 also have "\<dots> = P(?w)" using w P1eqP by blast
311 finally have "P ?w" using P1 by blast
315 lemma incr_mult_lemma:
316 assumes dpos: "(0::int) < d" and plus: "ALL x::int. P x \<longrightarrow> P(x + d)" and knneg: "0 <= k"
317 shows "ALL x. P x \<longrightarrow> P(x + k*d)"
319 proof (induct rule:int_ge_induct)
320 case base thus ?case by simp
324 have "P x \<longrightarrow> P (x + i * d)" using step.hyps by blast
325 also have "\<dots> \<longrightarrow> P(x + (i + 1) * d)" using plus[THEN spec, of "x + i * d"]
326 by (simp add:int_distrib zadd_ac)
327 ultimately have "P x \<longrightarrow> P(x + (i + 1) * d)" by blast}
332 assumes dp: "0 < D" and p1:"\<exists>z. \<forall> x> z. P x = P' x"
333 and nb:"\<forall>x.(\<forall> j\<in> {1..D}. \<forall>(b::int) \<in> A. x \<noteq> b - j) --> P (x) --> P (x + D)"
334 and pd: "\<forall> x k. P' x= P' (x-k*D)"
335 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)")
337 {assume "?R2" hence "?L" by blast}
339 {assume H:"?R1" hence "?L" using plusinfinity[OF dp pd p1] periodic_finite_ex[OF dp pd] by simp}
342 assume P: "P x" and H: "\<not> ?R2"
343 {fix y assume "\<not> (\<exists>j\<in>{1..D}. \<exists>b\<in>A. P (b - j))" and P: "P y"
344 hence "~(EX (j::int) : {1..D}. EX (b::int) : A. y = b - j)" by auto
345 with nb P have "P (y + D)" by auto }
346 hence "ALL x.~(EX (j::int) : {1..D}. EX (b::int) : A. P(b-j)) --> P (x) --> P (x + D)" by blast
347 with H P have th: " \<forall>x. P x \<longrightarrow> P (x + D)" by auto
348 from p1 obtain z where z: "ALL x. x > z --> (P x = P' x)" by blast
349 let ?y = "x + (\<bar>x - z\<bar> + 1)*D"
350 have zp: "0 <= (\<bar>x - z\<bar> + 1)" by arith
351 from dp have yz: "?y > z" using incr_lemma[OF dp] by simp
352 from z[rule_format, OF yz] incr_mult_lemma[OF dp th zp, rule_format, OF P] have th2: " P' ?y" by auto
353 with periodic_finite_ex[OF dp pd]
355 ultimately show ?thesis by blast
358 lemma simp_from_to: "{i..j::int} = (if j < i then {} else insert i {i+1..j})"
359 apply(simp add:atLeastAtMost_def atLeast_def atMost_def)
363 theorem unity_coeff_ex: "(\<exists>(x::'a::{semiring_0,Ring_and_Field.dvd}). P (l * x)) \<equiv> (\<exists>x. l dvd (x + 0) \<and> P x)"
364 apply (rule eq_reflection [symmetric])
368 apply (rule_tac x = "l * x" in exI)
369 apply (simp add: dvd_def)
370 apply (rule_tac x = x in exI, simp)
375 apply (rule_tac x = k in exI)
379 lemma zdvd_mono: assumes not0: "(k::int) \<noteq> 0"
380 shows "((m::int) dvd t) \<equiv> (k*m dvd k*t)"
381 using not0 by (simp add: dvd_def)
383 lemma uminus_dvd_conv: "(d dvd (t::int)) \<equiv> (-d dvd t)" "(d dvd (t::int)) \<equiv> (d dvd -t)"
386 text {* \bigskip Theorems for transforming predicates on nat to predicates on @{text int}*}
388 lemma zdiff_int_split: "P (int (x - y)) =
389 ((y \<le> x \<longrightarrow> P (int x - int y)) \<and> (x < y \<longrightarrow> P 0))"
390 by (case_tac "y \<le> x", simp_all add: zdiff_int)
392 lemma number_of1: "(0::int) <= number_of n \<Longrightarrow> (0::int) <= number_of (Int.Bit0 n) \<and> (0::int) <= number_of (Int.Bit1 n)"
394 lemma number_of2: "(0::int) <= Numeral0" by simp
397 \medskip Specific instances of congruence rules, to prevent
398 simplifier from looping. *}
400 theorem imp_le_cong: "(0 <= x \<Longrightarrow> P = P') \<Longrightarrow> (0 <= (x::int) \<longrightarrow> P) = (0 <= x \<longrightarrow> P')" by simp
402 theorem conj_le_cong: "(0 <= x \<Longrightarrow> P = P') \<Longrightarrow> (0 <= (x::int) \<and> P) = (0 <= x \<and> P')"
403 by (simp cong: conj_cong)
404 lemma int_eq_number_of_eq:
405 "(((number_of v)::int) = (number_of w)) = iszero ((number_of (v + (uminus w)))::int)"
406 by (rule eq_number_of_eq)
408 declare dvd_eq_mod_eq_0[symmetric, presburger]
409 declare mod_1[presburger]
410 declare mod_0[presburger]
411 declare mod_by_1[presburger]
412 declare zmod_zero[presburger]
413 declare zmod_self[presburger]
414 declare mod_self[presburger]
415 declare mod_by_0[presburger]
416 declare mod_div_trivial[presburger]
417 declare div_mod_equality2[presburger]
418 declare div_mod_equality[presburger]
419 declare mod_div_equality2[presburger]
420 declare mod_div_equality[presburger]
421 declare mod_mult_self1[presburger]
422 declare mod_mult_self2[presburger]
423 declare zdiv_zmod_equality2[presburger]
424 declare zdiv_zmod_equality[presburger]
425 declare mod2_Suc_Suc[presburger]
426 lemma [presburger]: "(a::int) div 0 = 0" and [presburger]: "a mod 0 = a"
429 use "Tools/Qelim/cooper.ML"
430 oracle linzqe_oracle = Coopereif.cooper_oracle
432 use "Tools/Qelim/presburger.ML"
434 setup {* Arith_Data.add_tactic "Presburger arithmetic" (K (Presburger.cooper_tac true [] [])) *}
436 method_setup presburger = {*
438 fun keyword k = Scan.lift (Args.$$$ k -- Args.colon) >> K ()
439 fun simple_keyword k = Scan.lift (Args.$$$ k) >> K ()
443 val any_keyword = keyword addN || keyword delN || simple_keyword elimN
444 val thms = Scan.repeat (Scan.unless any_keyword Attrib.multi_thm) >> flat;
446 Scan.optional (simple_keyword elimN >> K false) true --
447 Scan.optional (keyword addN |-- thms) [] --
448 Scan.optional (keyword delN |-- thms) [] >>
449 (fn ((elim, add_ths), del_ths) => fn ctxt =>
450 SIMPLE_METHOD' (Presburger.cooper_tac elim add_ths del_ths ctxt))
452 *} "Cooper's algorithm for Presburger arithmetic"
454 lemma [presburger, algebra]: "m mod 2 = (1::nat) \<longleftrightarrow> \<not> 2 dvd m " by presburger
455 lemma [presburger, algebra]: "m mod 2 = Suc 0 \<longleftrightarrow> \<not> 2 dvd m " by presburger
456 lemma [presburger, algebra]: "m mod (Suc (Suc 0)) = (1::nat) \<longleftrightarrow> \<not> 2 dvd m " by presburger
457 lemma [presburger, algebra]: "m mod (Suc (Suc 0)) = Suc 0 \<longleftrightarrow> \<not> 2 dvd m " by presburger
458 lemma [presburger, algebra]: "m mod 2 = (1::int) \<longleftrightarrow> \<not> 2 dvd m " by presburger
463 assumes advdd: "a dvd d"
464 shows "a dvd (x + t) \<longleftrightarrow> a dvd ((x + c * d) + t)"