src/HOL/Presburger.thy
author haftmann
Mon May 10 14:55:06 2010 +0200 (2010-05-10)
changeset 36802 5f9fe7b3295d
parent 36800 59b50c691b75
child 36803 2cad8904c4ff
permissions -rw-r--r--
only one module fpr presburger method
     1 (* Title:      HOL/Presburger.thy
     2    Author:     Amine Chaieb, TU Muenchen
     3 *)
     4 
     5 header {* Decision Procedure for Presburger Arithmetic *}
     6 
     7 theory Presburger
     8 imports Groebner_Basis SetInterval
     9 uses
    10   "Tools/Qelim/qelim.ML"
    11   "Tools/Qelim/cooper_procedure.ML"
    12   ("Tools/Qelim/cooper.ML")
    13   ("Tools/Qelim/presburger.ML")
    14 begin
    15 
    16 subsection{* The @{text "-\<infinity>"} and @{text "+\<infinity>"} Properties *}
    17 
    18 lemma minf:
    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 \<and> Q x) = (P' x \<and> Q' x)"
    21   "\<lbrakk>\<exists>(z ::'a::linorder).\<forall>x<z. P x = P' x; \<exists>z.\<forall>x<z. Q x = Q' x\<rbrakk> 
    22      \<Longrightarrow> \<exists>z.\<forall>x<z. (P x \<or> Q x) = (P' x \<or> Q' x)"
    23   "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x = t) = False"
    24   "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x \<noteq> t) = True"
    25   "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x < t) = True"
    26   "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x \<le> t) = True"
    27   "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x > t) = False"
    28   "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x \<ge> t) = False"
    29   "\<exists>z.\<forall>(x::'a::{linorder,plus,Rings.dvd})<z. (d dvd x + s) = (d dvd x + s)"
    30   "\<exists>z.\<forall>(x::'a::{linorder,plus,Rings.dvd})<z. (\<not> d dvd x + s) = (\<not> d dvd x + s)"
    31   "\<exists>z.\<forall>x<z. F = F"
    32   by ((erule exE, erule exE,rule_tac x="min z za" in exI,simp)+, (rule_tac x="t" in exI,fastsimp)+) simp_all
    33 
    34 lemma pinf:
    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 \<and> Q x) = (P' x \<and> Q' x)"
    37   "\<lbrakk>\<exists>(z ::'a::linorder).\<forall>x>z. P x = P' x; \<exists>z.\<forall>x>z. Q x = Q' x\<rbrakk> 
    38      \<Longrightarrow> \<exists>z.\<forall>x>z. (P x \<or> Q x) = (P' x \<or> Q' x)"
    39   "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x = t) = False"
    40   "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x \<noteq> t) = True"
    41   "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x < t) = False"
    42   "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x \<le> t) = False"
    43   "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x > t) = True"
    44   "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x \<ge> t) = True"
    45   "\<exists>z.\<forall>(x::'a::{linorder,plus,Rings.dvd})>z. (d dvd x + s) = (d dvd x + s)"
    46   "\<exists>z.\<forall>(x::'a::{linorder,plus,Rings.dvd})>z. (\<not> d dvd x + s) = (\<not> d dvd x + s)"
    47   "\<exists>z.\<forall>x>z. F = F"
    48   by ((erule exE, erule exE,rule_tac x="max z za" in exI,simp)+,(rule_tac x="t" in exI,fastsimp)+) simp_all
    49 
    50 lemma inf_period:
    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 \<and> Q x) = (P (x - k*D) \<and> Q (x - k*D))"
    53   "\<lbrakk>\<forall>x k. P x = P (x - k*D); \<forall>x k. Q x = Q (x - k*D)\<rbrakk> 
    54     \<Longrightarrow> \<forall>x k. (P x \<or> Q x) = (P (x - k*D) \<or> Q (x - k*D))"
    55   "(d::'a::{comm_ring,Rings.dvd}) dvd D \<Longrightarrow> \<forall>x k. (d dvd x + t) = (d dvd (x - k*D) + t)"
    56   "(d::'a::{comm_ring,Rings.dvd}) dvd D \<Longrightarrow> \<forall>x k. (\<not>d dvd x + t) = (\<not>d dvd (x - k*D) + t)"
    57   "\<forall>x k. F = F"
    58 apply (auto elim!: dvdE simp add: algebra_simps)
    59 unfolding mult_assoc [symmetric] left_distrib [symmetric] left_diff_distrib [symmetric]
    60 unfolding dvd_def mult_commute [of d] 
    61 by auto
    62 
    63 subsection{* The A and B sets *}
    64 lemma bset:
    65   "\<lbrakk>\<forall>x.(\<forall>j \<in> {1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> P x \<longrightarrow> P(x - D) ;
    66      \<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> Q x \<longrightarrow> Q(x - D)\<rbrakk> \<Longrightarrow> 
    67   \<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))"
    68   "\<lbrakk>\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> P x \<longrightarrow> P(x - D) ;
    69      \<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> Q x \<longrightarrow> Q(x - D)\<rbrakk> \<Longrightarrow> 
    70   \<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))"
    71   "\<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))"
    72   "\<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))"
    73   "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))"
    74   "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))"
    75   "\<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))"
    76   "\<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))"
    77   "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))"
    78   "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))"
    79   "\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j) \<longrightarrow> F \<longrightarrow> F"
    80 proof (blast, blast)
    81   assume dp: "D > 0" and tB: "t - 1\<in> B"
    82   show "(\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> (x = t) \<longrightarrow> (x - D = t))" 
    83     apply (rule allI, rule impI,erule ballE[where x="1"],erule ballE[where x="t - 1"]) 
    84     apply algebra using dp tB by simp_all
    85 next
    86   assume dp: "D > 0" and tB: "t \<in> B"
    87   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))" 
    88     apply (rule allI, rule impI,erule ballE[where x="D"],erule ballE[where x="t"])
    89     apply algebra
    90     using dp tB by simp_all
    91 next
    92   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
    93 next
    94   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
    95 next
    96   assume dp: "D > 0" and tB:"t \<in> B"
    97   {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"
    98     hence "x -t \<le> D" and "1 \<le> x - t" by simp+
    99       hence "\<exists>j \<in> {1 .. D}. x - t = j" by auto
   100       hence "\<exists>j \<in> {1 .. D}. x = t + j" by (simp add: algebra_simps)
   101       with nob tB have "False" by simp}
   102   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
   103 next
   104   assume dp: "D > 0" and tB:"t - 1\<in> B"
   105   {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"
   106     hence "x - (t - 1) \<le> D" and "1 \<le> x - (t - 1)" by simp+
   107       hence "\<exists>j \<in> {1 .. D}. x - (t - 1) = j" by auto
   108       hence "\<exists>j \<in> {1 .. D}. x = (t - 1) + j" by (simp add: algebra_simps)
   109       with nob tB have "False" by simp}
   110   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 next
   112   assume d: "d dvd D"
   113   {fix x assume H: "d dvd x + t" with d have "d dvd (x - D) + t" by algebra}
   114   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 next
   116   assume d: "d dvd D"
   117   {fix x assume H: "\<not>(d dvd x + t)" with d have "\<not> d dvd (x - D) + t"
   118       by (clarsimp simp add: dvd_def,erule_tac x= "ka + k" in allE,simp add: algebra_simps)}
   119   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
   120 qed blast
   121 
   122 lemma aset:
   123   "\<lbrakk>\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> P x \<longrightarrow> P(x + D) ;
   124      \<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> Q x \<longrightarrow> Q(x + D)\<rbrakk> \<Longrightarrow> 
   125   \<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))"
   126   "\<lbrakk>\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> P x \<longrightarrow> P(x + D) ;
   127      \<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> Q x \<longrightarrow> Q(x + D)\<rbrakk> \<Longrightarrow> 
   128   \<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))"
   129   "\<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))"
   130   "\<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))"
   131   "\<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))"
   132   "\<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))"
   133   "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))"
   134   "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))"
   135   "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))"
   136   "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))"
   137   "\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j) \<longrightarrow> F \<longrightarrow> F"
   138 proof (blast, blast)
   139   assume dp: "D > 0" and tA: "t + 1 \<in> A"
   140   show "(\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> (x = t) \<longrightarrow> (x + D = t))" 
   141     apply (rule allI, rule impI,erule ballE[where x="1"],erule ballE[where x="t + 1"])
   142     using dp tA by simp_all
   143 next
   144   assume dp: "D > 0" and tA: "t \<in> A"
   145   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))" 
   146     apply (rule allI, rule impI,erule ballE[where x="D"],erule ballE[where x="t"])
   147     using dp tA by simp_all
   148 next
   149   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
   150 next
   151   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
   152 next
   153   assume dp: "D > 0" and tA:"t \<in> A"
   154   {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"
   155     hence "t - x \<le> D" and "1 \<le> t - x" by simp+
   156       hence "\<exists>j \<in> {1 .. D}. t - x = j" by auto
   157       hence "\<exists>j \<in> {1 .. D}. x = t - j" by (auto simp add: algebra_simps) 
   158       with nob tA have "False" by simp}
   159   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
   160 next
   161   assume dp: "D > 0" and tA:"t + 1\<in> A"
   162   {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"
   163     hence "(t + 1) - x \<le> D" and "1 \<le> (t + 1) - x" by (simp_all add: algebra_simps)
   164       hence "\<exists>j \<in> {1 .. D}. (t + 1) - x = j" by auto
   165       hence "\<exists>j \<in> {1 .. D}. x = (t + 1) - j" by (auto simp add: algebra_simps)
   166       with nob tA have "False" by simp}
   167   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 next
   169   assume d: "d dvd D"
   170   {fix x assume H: "d dvd x + t" with d have "d dvd (x + D) + t"
   171       by (clarsimp simp add: dvd_def,rule_tac x= "ka + k" in exI,simp add: algebra_simps)}
   172   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 next
   174   assume d: "d dvd D"
   175   {fix x assume H: "\<not>(d dvd x + t)" with d have "\<not>d dvd (x + D) + t"
   176       by (clarsimp simp add: dvd_def,erule_tac x= "ka - k" in allE,simp add: algebra_simps)}
   177   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 qed blast
   179 
   180 subsection{* Cooper's Theorem @{text "-\<infinity>"} and @{text "+\<infinity>"} Version *}
   181 
   182 subsubsection{* First some trivial facts about periodic sets or predicates *}
   183 lemma periodic_finite_ex:
   184   assumes dpos: "(0::int) < d" and modd: "ALL x k. P x = P(x - k*d)"
   185   shows "(EX x. P x) = (EX j : {1..d}. P j)"
   186   (is "?LHS = ?RHS")
   187 proof
   188   assume ?LHS
   189   then obtain x where P: "P x" ..
   190   have "x mod d = x - (x div d)*d" by(simp add:zmod_zdiv_equality mult_ac eq_diff_eq)
   191   hence Pmod: "P x = P(x mod d)" using modd by simp
   192   show ?RHS
   193   proof (cases)
   194     assume "x mod d = 0"
   195     hence "P 0" using P Pmod by simp
   196     moreover have "P 0 = P(0 - (-1)*d)" using modd by blast
   197     ultimately have "P d" by simp
   198     moreover have "d : {1..d}" using dpos by simp
   199     ultimately show ?RHS ..
   200   next
   201     assume not0: "x mod d \<noteq> 0"
   202     have "P(x mod d)" using dpos P Pmod by simp
   203     moreover have "x mod d : {1..d}"
   204     proof -
   205       from dpos have "0 \<le> x mod d" by(rule pos_mod_sign)
   206       moreover from dpos have "x mod d < d" by(rule pos_mod_bound)
   207       ultimately show ?thesis using not0 by simp
   208     qed
   209     ultimately show ?RHS ..
   210   qed
   211 qed auto
   212 
   213 subsubsection{* The @{text "-\<infinity>"} Version*}
   214 
   215 lemma decr_lemma: "0 < (d::int) \<Longrightarrow> x - (abs(x-z)+1) * d < z"
   216 by(induct rule: int_gr_induct,simp_all add:int_distrib)
   217 
   218 lemma incr_lemma: "0 < (d::int) \<Longrightarrow> z < x + (abs(x-z)+1) * d"
   219 by(induct rule: int_gr_induct, simp_all add:int_distrib)
   220 
   221 lemma decr_mult_lemma:
   222   assumes dpos: "(0::int) < d" and minus: "\<forall>x. P x \<longrightarrow> P(x - d)" and knneg: "0 <= k"
   223   shows "ALL x. P x \<longrightarrow> P(x - k*d)"
   224 using knneg
   225 proof (induct rule:int_ge_induct)
   226   case base thus ?case by simp
   227 next
   228   case (step i)
   229   {fix x
   230     have "P x \<longrightarrow> P (x - i * d)" using step.hyps by blast
   231     also have "\<dots> \<longrightarrow> P(x - (i + 1) * d)" using minus[THEN spec, of "x - i * d"]
   232       by (simp add: algebra_simps)
   233     ultimately have "P x \<longrightarrow> P(x - (i + 1) * d)" by blast}
   234   thus ?case ..
   235 qed
   236 
   237 lemma  minusinfinity:
   238   assumes dpos: "0 < d" and
   239     P1eqP1: "ALL x k. P1 x = P1(x - k*d)" and ePeqP1: "EX z::int. ALL x. x < z \<longrightarrow> (P x = P1 x)"
   240   shows "(EX x. P1 x) \<longrightarrow> (EX x. P x)"
   241 proof
   242   assume eP1: "EX x. P1 x"
   243   then obtain x where P1: "P1 x" ..
   244   from ePeqP1 obtain z where P1eqP: "ALL x. x < z \<longrightarrow> (P x = P1 x)" ..
   245   let ?w = "x - (abs(x-z)+1) * d"
   246   from dpos have w: "?w < z" by(rule decr_lemma)
   247   have "P1 x = P1 ?w" using P1eqP1 by blast
   248   also have "\<dots> = P(?w)" using w P1eqP by blast
   249   finally have "P ?w" using P1 by blast
   250   thus "EX x. P x" ..
   251 qed
   252 
   253 lemma cpmi: 
   254   assumes dp: "0 < D" and p1:"\<exists>z. \<forall> x< z. P x = P' x"
   255   and nb:"\<forall>x.(\<forall> j\<in> {1..D}. \<forall>(b::int) \<in> B. x \<noteq> b+j) --> P (x) --> P (x - D)"
   256   and pd: "\<forall> x k. P' x = P' (x-k*D)"
   257   shows "(\<exists>x. P x) = ((\<exists> j\<in> {1..D} . P' j) | (\<exists> j \<in> {1..D}.\<exists> b\<in> B. P (b+j)))" 
   258          (is "?L = (?R1 \<or> ?R2)")
   259 proof-
   260  {assume "?R2" hence "?L"  by blast}
   261  moreover
   262  {assume H:"?R1" hence "?L" using minusinfinity[OF dp pd p1] periodic_finite_ex[OF dp pd] by simp}
   263  moreover 
   264  { fix x
   265    assume P: "P x" and H: "\<not> ?R2"
   266    {fix y assume "\<not> (\<exists>j\<in>{1..D}. \<exists>b\<in>B. P (b + j))" and P: "P y"
   267      hence "~(EX (j::int) : {1..D}. EX (b::int) : B. y = b+j)" by auto
   268      with nb P  have "P (y - D)" by auto }
   269    hence "ALL x.~(EX (j::int) : {1..D}. EX (b::int) : B. P(b+j)) --> P (x) --> P (x - D)" by blast
   270    with H P have th: " \<forall>x. P x \<longrightarrow> P (x - D)" by auto
   271    from p1 obtain z where z: "ALL x. x < z --> (P x = P' x)" by blast
   272    let ?y = "x - (\<bar>x - z\<bar> + 1)*D"
   273    have zp: "0 <= (\<bar>x - z\<bar> + 1)" by arith
   274    from dp have yz: "?y < z" using decr_lemma[OF dp] by simp   
   275    from z[rule_format, OF yz] decr_mult_lemma[OF dp th zp, rule_format, OF P] have th2: " P' ?y" by auto
   276    with periodic_finite_ex[OF dp pd]
   277    have "?R1" by blast}
   278  ultimately show ?thesis by blast
   279 qed
   280 
   281 subsubsection {* The @{text "+\<infinity>"} Version*}
   282 
   283 lemma  plusinfinity:
   284   assumes dpos: "(0::int) < d" and
   285     P1eqP1: "\<forall>x k. P' x = P'(x - k*d)" and ePeqP1: "\<exists> z. \<forall> x>z. P x = P' x"
   286   shows "(\<exists> x. P' x) \<longrightarrow> (\<exists> x. P x)"
   287 proof
   288   assume eP1: "EX x. P' x"
   289   then obtain x where P1: "P' x" ..
   290   from ePeqP1 obtain z where P1eqP: "\<forall>x>z. P x = P' x" ..
   291   let ?w' = "x + (abs(x-z)+1) * d"
   292   let ?w = "x - (-(abs(x-z) + 1))*d"
   293   have ww'[simp]: "?w = ?w'" by (simp add: algebra_simps)
   294   from dpos have w: "?w > z" by(simp only: ww' incr_lemma)
   295   hence "P' x = P' ?w" using P1eqP1 by blast
   296   also have "\<dots> = P(?w)" using w P1eqP by blast
   297   finally have "P ?w" using P1 by blast
   298   thus "EX x. P x" ..
   299 qed
   300 
   301 lemma incr_mult_lemma:
   302   assumes dpos: "(0::int) < d" and plus: "ALL x::int. P x \<longrightarrow> P(x + d)" and knneg: "0 <= k"
   303   shows "ALL x. P x \<longrightarrow> P(x + k*d)"
   304 using knneg
   305 proof (induct rule:int_ge_induct)
   306   case base thus ?case by simp
   307 next
   308   case (step i)
   309   {fix x
   310     have "P x \<longrightarrow> P (x + i * d)" using step.hyps by blast
   311     also have "\<dots> \<longrightarrow> P(x + (i + 1) * d)" using plus[THEN spec, of "x + i * d"]
   312       by (simp add:int_distrib zadd_ac)
   313     ultimately have "P x \<longrightarrow> P(x + (i + 1) * d)" by blast}
   314   thus ?case ..
   315 qed
   316 
   317 lemma cppi: 
   318   assumes dp: "0 < D" and p1:"\<exists>z. \<forall> x> z. P x = P' x"
   319   and nb:"\<forall>x.(\<forall> j\<in> {1..D}. \<forall>(b::int) \<in> A. x \<noteq> b - j) --> P (x) --> P (x + D)"
   320   and pd: "\<forall> x k. P' x= P' (x-k*D)"
   321   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)")
   322 proof-
   323  {assume "?R2" hence "?L"  by blast}
   324  moreover
   325  {assume H:"?R1" hence "?L" using plusinfinity[OF dp pd p1] periodic_finite_ex[OF dp pd] by simp}
   326  moreover 
   327  { fix x
   328    assume P: "P x" and H: "\<not> ?R2"
   329    {fix y assume "\<not> (\<exists>j\<in>{1..D}. \<exists>b\<in>A. P (b - j))" and P: "P y"
   330      hence "~(EX (j::int) : {1..D}. EX (b::int) : A. y = b - j)" by auto
   331      with nb P  have "P (y + D)" by auto }
   332    hence "ALL x.~(EX (j::int) : {1..D}. EX (b::int) : A. P(b-j)) --> P (x) --> P (x + D)" by blast
   333    with H P have th: " \<forall>x. P x \<longrightarrow> P (x + D)" by auto
   334    from p1 obtain z where z: "ALL x. x > z --> (P x = P' x)" by blast
   335    let ?y = "x + (\<bar>x - z\<bar> + 1)*D"
   336    have zp: "0 <= (\<bar>x - z\<bar> + 1)" by arith
   337    from dp have yz: "?y > z" using incr_lemma[OF dp] by simp
   338    from z[rule_format, OF yz] incr_mult_lemma[OF dp th zp, rule_format, OF P] have th2: " P' ?y" by auto
   339    with periodic_finite_ex[OF dp pd]
   340    have "?R1" by blast}
   341  ultimately show ?thesis by blast
   342 qed
   343 
   344 lemma simp_from_to: "{i..j::int} = (if j < i then {} else insert i {i+1..j})"
   345 apply(simp add:atLeastAtMost_def atLeast_def atMost_def)
   346 apply(fastsimp)
   347 done
   348 
   349 theorem unity_coeff_ex: "(\<exists>(x::'a::{semiring_0,Rings.dvd}). P (l * x)) \<equiv> (\<exists>x. l dvd (x + 0) \<and> P x)"
   350   apply (rule eq_reflection [symmetric])
   351   apply (rule iffI)
   352   defer
   353   apply (erule exE)
   354   apply (rule_tac x = "l * x" in exI)
   355   apply (simp add: dvd_def)
   356   apply (rule_tac x = x in exI, simp)
   357   apply (erule exE)
   358   apply (erule conjE)
   359   apply simp
   360   apply (erule dvdE)
   361   apply (rule_tac x = k in exI)
   362   apply simp
   363   done
   364 
   365 lemma zdvd_mono: assumes not0: "(k::int) \<noteq> 0"
   366 shows "((m::int) dvd t) \<equiv> (k*m dvd k*t)" 
   367   using not0 by (simp add: dvd_def)
   368 
   369 lemma uminus_dvd_conv: "(d dvd (t::int)) \<equiv> (-d dvd t)" "(d dvd (t::int)) \<equiv> (d dvd -t)"
   370   by simp_all
   371 
   372 text {* \bigskip Theorems for transforming predicates on nat to predicates on @{text int}*}
   373 
   374 lemma zdiff_int_split: "P (int (x - y)) =
   375   ((y \<le> x \<longrightarrow> P (int x - int y)) \<and> (x < y \<longrightarrow> P 0))"
   376   by (cases "y \<le> x") (simp_all add: zdiff_int)
   377 
   378 lemma number_of1: "(0::int) <= number_of n \<Longrightarrow> (0::int) <= number_of (Int.Bit0 n) \<and> (0::int) <= number_of (Int.Bit1 n)"
   379 by simp
   380 
   381 lemma number_of2: "(0::int) <= Numeral0" by simp
   382 
   383 text {*
   384   \medskip Specific instances of congruence rules, to prevent
   385   simplifier from looping. *}
   386 
   387 theorem imp_le_cong: "(0 <= x \<Longrightarrow> P = P') \<Longrightarrow> (0 <= (x::int) \<longrightarrow> P) = (0 <= x \<longrightarrow> P')" by simp
   388 
   389 theorem conj_le_cong: "(0 <= x \<Longrightarrow> P = P') \<Longrightarrow> (0 <= (x::int) \<and> P) = (0 <= x \<and> P')" 
   390   by (simp cong: conj_cong)
   391 
   392 use "Tools/Qelim/cooper.ML"
   393 
   394 setup Cooper.setup
   395 
   396 method_setup presburger = "Cooper.cooper_method" "Cooper's algorithm for Presburger arithmetic"
   397 
   398 declare dvd_eq_mod_eq_0[symmetric, presburger]
   399 declare mod_1[presburger] 
   400 declare mod_0[presburger]
   401 declare mod_by_1[presburger]
   402 declare zmod_zero[presburger]
   403 declare zmod_self[presburger]
   404 declare mod_self[presburger]
   405 declare mod_by_0[presburger]
   406 declare mod_div_trivial[presburger]
   407 declare div_mod_equality2[presburger]
   408 declare div_mod_equality[presburger]
   409 declare mod_div_equality2[presburger]
   410 declare mod_div_equality[presburger]
   411 declare mod_mult_self1[presburger]
   412 declare mod_mult_self2[presburger]
   413 declare zdiv_zmod_equality2[presburger]
   414 declare zdiv_zmod_equality[presburger]
   415 declare mod2_Suc_Suc[presburger]
   416 lemma [presburger]: "(a::int) div 0 = 0" and [presburger]: "a mod 0 = a"
   417 by simp_all
   418 
   419 lemma [presburger, algebra]: "m mod 2 = (1::nat) \<longleftrightarrow> \<not> 2 dvd m " by presburger
   420 lemma [presburger, algebra]: "m mod 2 = Suc 0 \<longleftrightarrow> \<not> 2 dvd m " by presburger
   421 lemma [presburger, algebra]: "m mod (Suc (Suc 0)) = (1::nat) \<longleftrightarrow> \<not> 2 dvd m " by presburger
   422 lemma [presburger, algebra]: "m mod (Suc (Suc 0)) = Suc 0 \<longleftrightarrow> \<not> 2 dvd m " by presburger
   423 lemma [presburger, algebra]: "m mod 2 = (1::int) \<longleftrightarrow> \<not> 2 dvd m " by presburger
   424 
   425 end