src/HOL/Presburger.thy
 author haftmann Fri Jul 18 18:25:53 2008 +0200 (2008-07-18) changeset 27651 16a26996c30e parent 27540 dc38e79f5a1c child 27668 6eb20b2cecf8 permissions -rw-r--r--
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
```     1 (* Title:      HOL/Presburger.thy
```
```     2    ID:         \$Id\$
```
```     3    Author:     Amine Chaieb, TU Muenchen
```
```     4 *)
```
```     5
```
```     6 header {* Decision Procedure for Presburger Arithmetic *}
```
```     7
```
```     8 theory Presburger
```
```     9 imports Arith_Tools SetInterval
```
```    10 uses
```
```    11   "Tools/Qelim/cooper_data.ML"
```
```    12   "Tools/Qelim/generated_cooper.ML"
```
```    13   "Tools/Qelim/qelim.ML"
```
```    14   ("Tools/Qelim/cooper.ML")
```
```    15   ("Tools/Qelim/presburger.ML")
```
```    16 begin
```
```    17
```
```    18 setup CooperData.setup
```
```    19
```
```    20 subsection{* The @{text "-\<infinity>"} and @{text "+\<infinity>"} Properties *}
```
```    21
```
```    22
```
```    23 lemma minf:
```
```    24   "\<lbrakk>\<exists>(z ::'a::linorder).\<forall>x<z. P x = P' x; \<exists>z.\<forall>x<z. Q x = Q' x\<rbrakk>
```
```    25      \<Longrightarrow> \<exists>z.\<forall>x<z. (P x \<and> Q x) = (P' x \<and> Q' x)"
```
```    26   "\<lbrakk>\<exists>(z ::'a::linorder).\<forall>x<z. P x = P' x; \<exists>z.\<forall>x<z. Q x = Q' x\<rbrakk>
```
```    27      \<Longrightarrow> \<exists>z.\<forall>x<z. (P x \<or> Q x) = (P' x \<or> Q' x)"
```
```    28   "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x = t) = False"
```
```    29   "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x \<noteq> t) = True"
```
```    30   "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x < t) = True"
```
```    31   "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x \<le> t) = True"
```
```    32   "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x > t) = False"
```
```    33   "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x \<ge> t) = False"
```
```    34   "\<exists>z.\<forall>(x::'a::{linorder,plus,Ring_and_Field.dvd})<z. (d dvd x + s) = (d dvd x + s)"
```
```    35   "\<exists>z.\<forall>(x::'a::{linorder,plus,Ring_and_Field.dvd})<z. (\<not> d dvd x + s) = (\<not> d dvd x + s)"
```
```    36   "\<exists>z.\<forall>x<z. F = F"
```
```    37   by ((erule exE, erule exE,rule_tac x="min z za" in exI,simp)+, (rule_tac x="t" in exI,fastsimp)+) simp_all
```
```    38
```
```    39 lemma pinf:
```
```    40   "\<lbrakk>\<exists>(z ::'a::linorder).\<forall>x>z. P x = P' x; \<exists>z.\<forall>x>z. Q x = Q' x\<rbrakk>
```
```    41      \<Longrightarrow> \<exists>z.\<forall>x>z. (P x \<and> Q x) = (P' x \<and> Q' x)"
```
```    42   "\<lbrakk>\<exists>(z ::'a::linorder).\<forall>x>z. P x = P' x; \<exists>z.\<forall>x>z. Q x = Q' x\<rbrakk>
```
```    43      \<Longrightarrow> \<exists>z.\<forall>x>z. (P x \<or> Q x) = (P' x \<or> Q' x)"
```
```    44   "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x = t) = False"
```
```    45   "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x \<noteq> t) = True"
```
```    46   "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x < t) = False"
```
```    47   "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x \<le> t) = False"
```
```    48   "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x > t) = True"
```
```    49   "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x \<ge> t) = True"
```
```    50   "\<exists>z.\<forall>(x::'a::{linorder,plus,Ring_and_Field.dvd})>z. (d dvd x + s) = (d dvd x + s)"
```
```    51   "\<exists>z.\<forall>(x::'a::{linorder,plus,Ring_and_Field.dvd})>z. (\<not> d dvd x + s) = (\<not> d dvd x + s)"
```
```    52   "\<exists>z.\<forall>x>z. F = F"
```
```    53   by ((erule exE, erule exE,rule_tac x="max z za" in exI,simp)+,(rule_tac x="t" in exI,fastsimp)+) simp_all
```
```    54
```
```    55 lemma inf_period:
```
```    56   "\<lbrakk>\<forall>x k. P x = P (x - k*D); \<forall>x k. Q x = Q (x - k*D)\<rbrakk>
```
```    57     \<Longrightarrow> \<forall>x k. (P x \<and> Q x) = (P (x - k*D) \<and> Q (x - k*D))"
```
```    58   "\<lbrakk>\<forall>x k. P x = P (x - k*D); \<forall>x k. Q x = Q (x - k*D)\<rbrakk>
```
```    59     \<Longrightarrow> \<forall>x k. (P x \<or> Q x) = (P (x - k*D) \<or> Q (x - k*D))"
```
```    60   "(d::'a::{comm_ring,Ring_and_Field.dvd}) dvd D \<Longrightarrow> \<forall>x k. (d dvd x + t) = (d dvd (x - k*D) + t)"
```
```    61   "(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   "\<forall>x k. F = F"
```
```    63 apply (auto elim!: dvdE simp add: ring_simps)
```
```    64 unfolding mult_assoc [symmetric] left_distrib [symmetric] left_diff_distrib [symmetric]
```
```    65 unfolding dvd_def mult_commute [of d] by auto
```
```    66
```
```    67 subsection{* The A and B sets *}
```
```    68 lemma bset:
```
```    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"
```
```    84 proof (blast, blast)
```
```    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     using dp tB by simp_all
```
```    89 next
```
```    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"])
```
```    93     using dp tB by simp_all
```
```    94 next
```
```    95   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
```
```    96 next
```
```    97   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
```
```    98 next
```
```    99   assume dp: "D > 0" and tB:"t \<in> B"
```
```   100   {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"
```
```   101     hence "x -t \<le> D" and "1 \<le> x - t" by simp+
```
```   102       hence "\<exists>j \<in> {1 .. D}. x - t = j" by auto
```
```   103       hence "\<exists>j \<in> {1 .. D}. x = t + j" by (simp add: ring_simps)
```
```   104       with nob tB have "False" by simp}
```
```   105   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
```
```   106 next
```
```   107   assume dp: "D > 0" and tB:"t - 1\<in> B"
```
```   108   {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"
```
```   109     hence "x - (t - 1) \<le> D" and "1 \<le> x - (t - 1)" by simp+
```
```   110       hence "\<exists>j \<in> {1 .. D}. x - (t - 1) = j" by auto
```
```   111       hence "\<exists>j \<in> {1 .. D}. x = (t - 1) + j" by (simp add: ring_simps)
```
```   112       with nob tB have "False" by simp}
```
```   113   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
```
```   114 next
```
```   115   assume d: "d dvd D"
```
```   116   {fix x assume H: "d dvd x + t" with d have "d dvd (x - D) + t"
```
```   117       by (auto elim!: dvdE simp add: ring_simps)
```
```   118         (auto simp only: left_diff_distrib [symmetric] dvd_def mult_commute)}
```
```   119   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
```
```   120 next
```
```   121   assume d: "d dvd D"
```
```   122   {fix x assume H: "\<not>(d dvd x + t)" with d have "\<not> d dvd (x - D) + t"
```
```   123       by (clarsimp simp add: dvd_def,erule_tac x= "ka + k" in allE,simp add: ring_simps)}
```
```   124   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
```
```   125 qed blast
```
```   126
```
```   127 lemma aset:
```
```   128   "\<lbrakk>\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> P x \<longrightarrow> P(x + D) ;
```
```   129      \<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> Q x \<longrightarrow> Q(x + D)\<rbrakk> \<Longrightarrow>
```
```   130   \<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))"
```
```   131   "\<lbrakk>\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> P x \<longrightarrow> P(x + D) ;
```
```   132      \<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> Q x \<longrightarrow> Q(x + D)\<rbrakk> \<Longrightarrow>
```
```   133   \<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))"
```
```   134   "\<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))"
```
```   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 \<noteq> t) \<longrightarrow> (x + D \<noteq> t))"
```
```   136   "\<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))"
```
```   137   "\<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))"
```
```   138   "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))"
```
```   139   "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))"
```
```   140   "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))"
```
```   141   "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))"
```
```   142   "\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j) \<longrightarrow> F \<longrightarrow> F"
```
```   143 proof (blast, blast)
```
```   144   assume dp: "D > 0" and tA: "t + 1 \<in> A"
```
```   145   show "(\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> (x = t) \<longrightarrow> (x + D = t))"
```
```   146     apply (rule allI, rule impI,erule ballE[where x="1"],erule ballE[where x="t + 1"])
```
```   147     using dp tA by simp_all
```
```   148 next
```
```   149   assume dp: "D > 0" and tA: "t \<in> A"
```
```   150   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))"
```
```   151     apply (rule allI, rule impI,erule ballE[where x="D"],erule ballE[where x="t"])
```
```   152     using dp tA by simp_all
```
```   153 next
```
```   154   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 next
```
```   156   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 next
```
```   158   assume dp: "D > 0" and tA:"t \<in> A"
```
```   159   {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"
```
```   160     hence "t - x \<le> D" and "1 \<le> t - x" by simp+
```
```   161       hence "\<exists>j \<in> {1 .. D}. t - x = j" by auto
```
```   162       hence "\<exists>j \<in> {1 .. D}. x = t - j" by (auto simp add: ring_simps)
```
```   163       with nob tA have "False" by simp}
```
```   164   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 next
```
```   166   assume dp: "D > 0" and tA:"t + 1\<in> A"
```
```   167   {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"
```
```   168     hence "(t + 1) - x \<le> D" and "1 \<le> (t + 1) - x" by (simp_all add: ring_simps)
```
```   169       hence "\<exists>j \<in> {1 .. D}. (t + 1) - x = j" by auto
```
```   170       hence "\<exists>j \<in> {1 .. D}. x = (t + 1) - j" by (auto simp add: ring_simps)
```
```   171       with nob tA have "False" by simp}
```
```   172   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
```
```   173 next
```
```   174   assume d: "d dvd D"
```
```   175   {fix x assume H: "d dvd x + t" with d have "d dvd (x + D) + t"
```
```   176       by (clarsimp simp add: dvd_def,rule_tac x= "ka + k" in exI,simp add: ring_simps)}
```
```   177   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
```
```   178 next
```
```   179   assume d: "d dvd D"
```
```   180   {fix x assume H: "\<not>(d dvd x + t)" with d have "\<not>d dvd (x + D) + t"
```
```   181       by (clarsimp simp add: dvd_def,erule_tac x= "ka - k" in allE,simp add: ring_simps)}
```
```   182   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
```
```   183 qed blast
```
```   184
```
```   185 subsection{* Cooper's Theorem @{text "-\<infinity>"} and @{text "+\<infinity>"} Version *}
```
```   186
```
```   187 subsubsection{* First some trivial facts about periodic sets or predicates *}
```
```   188 lemma periodic_finite_ex:
```
```   189   assumes dpos: "(0::int) < d" and modd: "ALL x k. P x = P(x - k*d)"
```
```   190   shows "(EX x. P x) = (EX j : {1..d}. P j)"
```
```   191   (is "?LHS = ?RHS")
```
```   192 proof
```
```   193   assume ?LHS
```
```   194   then obtain x where P: "P x" ..
```
```   195   have "x mod d = x - (x div d)*d" by(simp add:zmod_zdiv_equality mult_ac eq_diff_eq)
```
```   196   hence Pmod: "P x = P(x mod d)" using modd by simp
```
```   197   show ?RHS
```
```   198   proof (cases)
```
```   199     assume "x mod d = 0"
```
```   200     hence "P 0" using P Pmod by simp
```
```   201     moreover have "P 0 = P(0 - (-1)*d)" using modd by blast
```
```   202     ultimately have "P d" by simp
```
```   203     moreover have "d : {1..d}" using dpos by(simp add:atLeastAtMost_iff)
```
```   204     ultimately show ?RHS ..
```
```   205   next
```
```   206     assume not0: "x mod d \<noteq> 0"
```
```   207     have "P(x mod d)" using dpos P Pmod by(simp add:pos_mod_sign pos_mod_bound)
```
```   208     moreover have "x mod d : {1..d}"
```
```   209     proof -
```
```   210       from dpos have "0 \<le> x mod d" by(rule pos_mod_sign)
```
```   211       moreover from dpos have "x mod d < d" by(rule pos_mod_bound)
```
```   212       ultimately show ?thesis using not0 by(simp add:atLeastAtMost_iff)
```
```   213     qed
```
```   214     ultimately show ?RHS ..
```
```   215   qed
```
```   216 qed auto
```
```   217
```
```   218 subsubsection{* The @{text "-\<infinity>"} Version*}
```
```   219
```
```   220 lemma decr_lemma: "0 < (d::int) \<Longrightarrow> x - (abs(x-z)+1) * d < z"
```
```   221 by(induct rule: int_gr_induct,simp_all add:int_distrib)
```
```   222
```
```   223 lemma incr_lemma: "0 < (d::int) \<Longrightarrow> z < x + (abs(x-z)+1) * d"
```
```   224 by(induct rule: int_gr_induct, simp_all add:int_distrib)
```
```   225
```
```   226 theorem int_induct[case_names base step1 step2]:
```
```   227   assumes
```
```   228   base: "P(k::int)" and step1: "\<And>i. \<lbrakk>k \<le> i; P i\<rbrakk> \<Longrightarrow> P(i+1)" and
```
```   229   step2: "\<And>i. \<lbrakk>k \<ge> i; P i\<rbrakk> \<Longrightarrow> P(i - 1)"
```
```   230   shows "P i"
```
```   231 proof -
```
```   232   have "i \<le> k \<or> i\<ge> k" by arith
```
```   233   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
```
```   234 qed
```
```   235
```
```   236 lemma decr_mult_lemma:
```
```   237   assumes dpos: "(0::int) < d" and minus: "\<forall>x. P x \<longrightarrow> P(x - d)" and knneg: "0 <= k"
```
```   238   shows "ALL x. P x \<longrightarrow> P(x - k*d)"
```
```   239 using knneg
```
```   240 proof (induct rule:int_ge_induct)
```
```   241   case base thus ?case by simp
```
```   242 next
```
```   243   case (step i)
```
```   244   {fix x
```
```   245     have "P x \<longrightarrow> P (x - i * d)" using step.hyps by blast
```
```   246     also have "\<dots> \<longrightarrow> P(x - (i + 1) * d)" using minus[THEN spec, of "x - i * d"]
```
```   247       by (simp add:int_distrib OrderedGroup.diff_diff_eq[symmetric])
```
```   248     ultimately have "P x \<longrightarrow> P(x - (i + 1) * d)" by blast}
```
```   249   thus ?case ..
```
```   250 qed
```
```   251
```
```   252 lemma  minusinfinity:
```
```   253   assumes dpos: "0 < d" and
```
```   254     P1eqP1: "ALL x k. P1 x = P1(x - k*d)" and ePeqP1: "EX z::int. ALL x. x < z \<longrightarrow> (P x = P1 x)"
```
```   255   shows "(EX x. P1 x) \<longrightarrow> (EX x. P x)"
```
```   256 proof
```
```   257   assume eP1: "EX x. P1 x"
```
```   258   then obtain x where P1: "P1 x" ..
```
```   259   from ePeqP1 obtain z where P1eqP: "ALL x. x < z \<longrightarrow> (P x = P1 x)" ..
```
```   260   let ?w = "x - (abs(x-z)+1) * d"
```
```   261   from dpos have w: "?w < z" by(rule decr_lemma)
```
```   262   have "P1 x = P1 ?w" using P1eqP1 by blast
```
```   263   also have "\<dots> = P(?w)" using w P1eqP by blast
```
```   264   finally have "P ?w" using P1 by blast
```
```   265   thus "EX x. P x" ..
```
```   266 qed
```
```   267
```
```   268 lemma cpmi:
```
```   269   assumes dp: "0 < D" and p1:"\<exists>z. \<forall> x< z. P x = P' x"
```
```   270   and nb:"\<forall>x.(\<forall> j\<in> {1..D}. \<forall>(b::int) \<in> B. x \<noteq> b+j) --> P (x) --> P (x - D)"
```
```   271   and pd: "\<forall> x k. P' x = P' (x-k*D)"
```
```   272   shows "(\<exists>x. P x) = ((\<exists> j\<in> {1..D} . P' j) | (\<exists> j \<in> {1..D}.\<exists> b\<in> B. P (b+j)))"
```
```   273          (is "?L = (?R1 \<or> ?R2)")
```
```   274 proof-
```
```   275  {assume "?R2" hence "?L"  by blast}
```
```   276  moreover
```
```   277  {assume H:"?R1" hence "?L" using minusinfinity[OF dp pd p1] periodic_finite_ex[OF dp pd] by simp}
```
```   278  moreover
```
```   279  { fix x
```
```   280    assume P: "P x" and H: "\<not> ?R2"
```
```   281    {fix y assume "\<not> (\<exists>j\<in>{1..D}. \<exists>b\<in>B. P (b + j))" and P: "P y"
```
```   282      hence "~(EX (j::int) : {1..D}. EX (b::int) : B. y = b+j)" by auto
```
```   283      with nb P  have "P (y - D)" by auto }
```
```   284    hence "ALL x.~(EX (j::int) : {1..D}. EX (b::int) : B. P(b+j)) --> P (x) --> P (x - D)" by blast
```
```   285    with H P have th: " \<forall>x. P x \<longrightarrow> P (x - D)" by auto
```
```   286    from p1 obtain z where z: "ALL x. x < z --> (P x = P' x)" by blast
```
```   287    let ?y = "x - (\<bar>x - z\<bar> + 1)*D"
```
```   288    have zp: "0 <= (\<bar>x - z\<bar> + 1)" by arith
```
```   289    from dp have yz: "?y < z" using decr_lemma[OF dp] by simp
```
```   290    from z[rule_format, OF yz] decr_mult_lemma[OF dp th zp, rule_format, OF P] have th2: " P' ?y" by auto
```
```   291    with periodic_finite_ex[OF dp pd]
```
```   292    have "?R1" by blast}
```
```   293  ultimately show ?thesis by blast
```
```   294 qed
```
```   295
```
```   296 subsubsection {* The @{text "+\<infinity>"} Version*}
```
```   297
```
```   298 lemma  plusinfinity:
```
```   299   assumes dpos: "(0::int) < d" and
```
```   300     P1eqP1: "\<forall>x k. P' x = P'(x - k*d)" and ePeqP1: "\<exists> z. \<forall> x>z. P x = P' x"
```
```   301   shows "(\<exists> x. P' x) \<longrightarrow> (\<exists> x. P x)"
```
```   302 proof
```
```   303   assume eP1: "EX x. P' x"
```
```   304   then obtain x where P1: "P' x" ..
```
```   305   from ePeqP1 obtain z where P1eqP: "\<forall>x>z. P x = P' x" ..
```
```   306   let ?w' = "x + (abs(x-z)+1) * d"
```
```   307   let ?w = "x - (-(abs(x-z) + 1))*d"
```
```   308   have ww'[simp]: "?w = ?w'" by (simp add: ring_simps)
```
```   309   from dpos have w: "?w > z" by(simp only: ww' incr_lemma)
```
```   310   hence "P' x = P' ?w" using P1eqP1 by blast
```
```   311   also have "\<dots> = P(?w)" using w P1eqP by blast
```
```   312   finally have "P ?w" using P1 by blast
```
```   313   thus "EX x. P x" ..
```
```   314 qed
```
```   315
```
```   316 lemma incr_mult_lemma:
```
```   317   assumes dpos: "(0::int) < d" and plus: "ALL x::int. P x \<longrightarrow> P(x + d)" and knneg: "0 <= k"
```
```   318   shows "ALL x. P x \<longrightarrow> P(x + k*d)"
```
```   319 using knneg
```
```   320 proof (induct rule:int_ge_induct)
```
```   321   case base thus ?case by simp
```
```   322 next
```
```   323   case (step i)
```
```   324   {fix x
```
```   325     have "P x \<longrightarrow> P (x + i * d)" using step.hyps by blast
```
```   326     also have "\<dots> \<longrightarrow> P(x + (i + 1) * d)" using plus[THEN spec, of "x + i * d"]
```
```   327       by (simp add:int_distrib zadd_ac)
```
```   328     ultimately have "P x \<longrightarrow> P(x + (i + 1) * d)" by blast}
```
```   329   thus ?case ..
```
```   330 qed
```
```   331
```
```   332 lemma cppi:
```
```   333   assumes dp: "0 < D" and p1:"\<exists>z. \<forall> x> z. P x = P' x"
```
```   334   and nb:"\<forall>x.(\<forall> j\<in> {1..D}. \<forall>(b::int) \<in> A. x \<noteq> b - j) --> P (x) --> P (x + D)"
```
```   335   and pd: "\<forall> x k. P' x= P' (x-k*D)"
```
```   336   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 proof-
```
```   338  {assume "?R2" hence "?L"  by blast}
```
```   339  moreover
```
```   340  {assume H:"?R1" hence "?L" using plusinfinity[OF dp pd p1] periodic_finite_ex[OF dp pd] by simp}
```
```   341  moreover
```
```   342  { fix x
```
```   343    assume P: "P x" and H: "\<not> ?R2"
```
```   344    {fix y assume "\<not> (\<exists>j\<in>{1..D}. \<exists>b\<in>A. P (b - j))" and P: "P y"
```
```   345      hence "~(EX (j::int) : {1..D}. EX (b::int) : A. y = b - j)" by auto
```
```   346      with nb P  have "P (y + D)" by auto }
```
```   347    hence "ALL x.~(EX (j::int) : {1..D}. EX (b::int) : A. P(b-j)) --> P (x) --> P (x + D)" by blast
```
```   348    with H P have th: " \<forall>x. P x \<longrightarrow> P (x + D)" by auto
```
```   349    from p1 obtain z where z: "ALL x. x > z --> (P x = P' x)" by blast
```
```   350    let ?y = "x + (\<bar>x - z\<bar> + 1)*D"
```
```   351    have zp: "0 <= (\<bar>x - z\<bar> + 1)" by arith
```
```   352    from dp have yz: "?y > z" using incr_lemma[OF dp] by simp
```
```   353    from z[rule_format, OF yz] incr_mult_lemma[OF dp th zp, rule_format, OF P] have th2: " P' ?y" by auto
```
```   354    with periodic_finite_ex[OF dp pd]
```
```   355    have "?R1" by blast}
```
```   356  ultimately show ?thesis by blast
```
```   357 qed
```
```   358
```
```   359 lemma simp_from_to: "{i..j::int} = (if j < i then {} else insert i {i+1..j})"
```
```   360 apply(simp add:atLeastAtMost_def atLeast_def atMost_def)
```
```   361 apply(fastsimp)
```
```   362 done
```
```   363
```
```   364 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)"
```
```   365   apply (rule eq_reflection [symmetric])
```
```   366   apply (rule iffI)
```
```   367   defer
```
```   368   apply (erule exE)
```
```   369   apply (rule_tac x = "l * x" in exI)
```
```   370   apply (simp add: dvd_def)
```
```   371   apply (rule_tac x = x in exI, simp)
```
```   372   apply (erule exE)
```
```   373   apply (erule conjE)
```
```   374   apply simp
```
```   375   apply (erule dvdE)
```
```   376   apply (rule_tac x = k in exI)
```
```   377   apply simp
```
```   378   done
```
```   379
```
```   380 lemma zdvd_mono: assumes not0: "(k::int) \<noteq> 0"
```
```   381 shows "((m::int) dvd t) \<equiv> (k*m dvd k*t)"
```
```   382   using not0 by (simp add: dvd_def)
```
```   383
```
```   384 lemma uminus_dvd_conv: "(d dvd (t::int)) \<equiv> (-d dvd t)" "(d dvd (t::int)) \<equiv> (d dvd -t)"
```
```   385   by simp_all
```
```   386 text {* \bigskip Theorems for transforming predicates on nat to predicates on @{text int}*}
```
```   387 lemma all_nat: "(\<forall>x::nat. P x) = (\<forall>x::int. 0 <= x \<longrightarrow> P (nat x))"
```
```   388   by (simp split add: split_nat)
```
```   389
```
```   390 lemma ex_nat: "(\<exists>x::nat. P x) = (\<exists>x::int. 0 <= x \<and> P (nat x))"
```
```   391   apply (auto split add: split_nat)
```
```   392   apply (rule_tac x="int x" in exI, simp)
```
```   393   apply (rule_tac x = "nat x" in exI,erule_tac x = "nat x" in allE, simp)
```
```   394   done
```
```   395
```
```   396 lemma zdiff_int_split: "P (int (x - y)) =
```
```   397   ((y \<le> x \<longrightarrow> P (int x - int y)) \<and> (x < y \<longrightarrow> P 0))"
```
```   398   by (case_tac "y \<le> x", simp_all add: zdiff_int)
```
```   399
```
```   400 lemma number_of1: "(0::int) <= number_of n \<Longrightarrow> (0::int) <= number_of (Int.Bit0 n) \<and> (0::int) <= number_of (Int.Bit1 n)"
```
```   401 by simp
```
```   402 lemma number_of2: "(0::int) <= Numeral0" by simp
```
```   403 lemma Suc_plus1: "Suc n = n + 1" by simp
```
```   404
```
```   405 text {*
```
```   406   \medskip Specific instances of congruence rules, to prevent
```
```   407   simplifier from looping. *}
```
```   408
```
```   409 theorem imp_le_cong: "(0 <= x \<Longrightarrow> P = P') \<Longrightarrow> (0 <= (x::int) \<longrightarrow> P) = (0 <= x \<longrightarrow> P')" by simp
```
```   410
```
```   411 theorem conj_le_cong: "(0 <= x \<Longrightarrow> P = P') \<Longrightarrow> (0 <= (x::int) \<and> P) = (0 <= x \<and> P')"
```
```   412   by (simp cong: conj_cong)
```
```   413 lemma int_eq_number_of_eq:
```
```   414   "(((number_of v)::int) = (number_of w)) = iszero ((number_of (v + (uminus w)))::int)"
```
```   415   by simp
```
```   416
```
```   417 lemma mod_eq0_dvd_iff[presburger]: "(m::nat) mod n = 0 \<longleftrightarrow> n dvd m"
```
```   418 unfolding dvd_eq_mod_eq_0[symmetric] ..
```
```   419
```
```   420 lemma zmod_eq0_zdvd_iff[presburger]: "(m::int) mod n = 0 \<longleftrightarrow> n dvd m"
```
```   421 unfolding zdvd_iff_zmod_eq_0[symmetric] ..
```
```   422 declare mod_1[presburger]
```
```   423 declare mod_0[presburger]
```
```   424 declare zmod_1[presburger]
```
```   425 declare zmod_zero[presburger]
```
```   426 declare zmod_self[presburger]
```
```   427 declare mod_self[presburger]
```
```   428 declare mod_by_0[presburger]
```
```   429 declare nat_mod_div_trivial[presburger]
```
```   430 declare div_mod_equality2[presburger]
```
```   431 declare div_mod_equality[presburger]
```
```   432 declare mod_div_equality2[presburger]
```
```   433 declare mod_div_equality[presburger]
```
```   434 declare mod_mult_self1[presburger]
```
```   435 declare mod_mult_self2[presburger]
```
```   436 declare zdiv_zmod_equality2[presburger]
```
```   437 declare zdiv_zmod_equality[presburger]
```
```   438 declare mod2_Suc_Suc[presburger]
```
```   439 lemma [presburger]: "(a::int) div 0 = 0" and [presburger]: "a mod 0 = a"
```
```   440 by simp_all
```
```   441
```
```   442 use "Tools/Qelim/cooper.ML"
```
```   443 oracle linzqe_oracle ("term") = Coopereif.cooper_oracle
```
```   444
```
```   445 use "Tools/Qelim/presburger.ML"
```
```   446
```
```   447 declaration {* fn _ =>
```
```   448   arith_tactic_add
```
```   449     (mk_arith_tactic "presburger" (fn ctxt => fn i => fn st =>
```
```   450        (warning "Trying Presburger arithmetic ...";
```
```   451     Presburger.cooper_tac true [] [] ctxt i st)))
```
```   452 *}
```
```   453
```
```   454 method_setup presburger = {*
```
```   455 let
```
```   456  fun keyword k = Scan.lift (Args.\$\$\$ k -- Args.colon) >> K ()
```
```   457  fun simple_keyword k = Scan.lift (Args.\$\$\$ k) >> K ()
```
```   458  val addN = "add"
```
```   459  val delN = "del"
```
```   460  val elimN = "elim"
```
```   461  val any_keyword = keyword addN || keyword delN || simple_keyword elimN
```
```   462  val thms = Scan.repeat (Scan.unless any_keyword Attrib.multi_thm) >> flat;
```
```   463 in
```
```   464   fn src => Method.syntax
```
```   465    ((Scan.optional (simple_keyword elimN >> K false) true) --
```
```   466     (Scan.optional (keyword addN |-- thms) []) --
```
```   467     (Scan.optional (keyword delN |-- thms) [])) src
```
```   468   #> (fn (((elim, add_ths), del_ths),ctxt) =>
```
```   469          Method.SIMPLE_METHOD' (Presburger.cooper_tac elim add_ths del_ths ctxt))
```
```   470 end
```
```   471 *} "Cooper's algorithm for Presburger arithmetic"
```
```   472
```
```   473 lemma [presburger]: "m mod 2 = (1::nat) \<longleftrightarrow> \<not> 2 dvd m " by presburger
```
```   474 lemma [presburger]: "m mod 2 = Suc 0 \<longleftrightarrow> \<not> 2 dvd m " by presburger
```
```   475 lemma [presburger]: "m mod (Suc (Suc 0)) = (1::nat) \<longleftrightarrow> \<not> 2 dvd m " by presburger
```
```   476 lemma [presburger]: "m mod (Suc (Suc 0)) = Suc 0 \<longleftrightarrow> \<not> 2 dvd m " by presburger
```
```   477 lemma [presburger]: "m mod 2 = (1::int) \<longleftrightarrow> \<not> 2 dvd m " by presburger
```
```   478
```
```   479
```
```   480 lemma zdvd_period:
```
```   481   fixes a d :: int
```
```   482   assumes advdd: "a dvd d"
```
```   483   shows "a dvd (x + t) \<longleftrightarrow> a dvd ((x + c * d) + t)"
```
```   484 proof-
```
```   485   {
```
```   486     fix x k
```
```   487     from inf_period(3) [OF advdd, rule_format, where x=x and k="-k"]
```
```   488     have "a dvd (x + t) \<longleftrightarrow> a dvd (x + k * d + t)" by simp
```
```   489   }
```
```   490   hence "\<forall>x.\<forall>k. ((a::int) dvd (x + t)) = (a dvd (x+k*d + t))"  by simp
```
```   491   then show ?thesis by simp
```
```   492 qed
```
```   493
```
```   494 end
```