src/HOL/Presburger.thy
author hoelzl
Thu Jan 31 11:31:27 2013 +0100 (2013-01-31)
changeset 50999 3de230ed0547
parent 49962 a8cc904a6820
child 54227 63b441f49645
permissions -rw-r--r--
introduce order topology
     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 Set_Interval
     9 begin
    10 
    11 ML_file "Tools/Qelim/qelim.ML"
    12 ML_file "Tools/Qelim/cooper_procedure.ML"
    13 
    14 subsection{* The @{text "-\<infinity>"} and @{text "+\<infinity>"} Properties *}
    15 
    16 lemma minf:
    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
    31 
    32 lemma pinf:
    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
    47 
    48 lemma inf_period:
    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)"
    55   "\<forall>x k. F = F"
    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] 
    59 by auto
    60 
    61 subsection{* The A and B sets *}
    62 lemma bset:
    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"
    78 proof (blast, blast)
    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
    83 next
    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"])
    87     apply algebra
    88     using dp tB by simp_all
    89 next
    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
    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 \<le> t) \<longrightarrow> (x - D \<le> t)" by arith
    93 next
    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
   101 next
   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
   109 next
   110   assume d: "d dvd D"
   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
   113 next
   114   assume d: "d dvd D"
   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
   118 qed blast
   119 
   120 lemma aset:
   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"
   136 proof (blast, blast)
   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
   141 next
   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
   146 next
   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
   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 \<ge> t) \<longrightarrow> (x + D \<ge> t)" by arith
   150 next
   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
   158 next
   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
   166 next
   167   assume d: "d dvd D"
   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
   171 next
   172   assume d: "d dvd D"
   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
   176 qed blast
   177 
   178 subsection{* Cooper's Theorem @{text "-\<infinity>"} and @{text "+\<infinity>"} Version *}
   179 
   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)"
   184   (is "?LHS = ?RHS")
   185 proof
   186   assume ?LHS
   187   then obtain x where P: "P x" ..
   188   have "x mod d = x - (x div d)*d" by(simp add:zmod_zdiv_equality mult_ac eq_diff_eq)
   189   hence Pmod: "P x = P(x mod d)" using modd by simp
   190   show ?RHS
   191   proof (cases)
   192     assume "x mod d = 0"
   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 ..
   198   next
   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}"
   202     proof -
   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
   206     qed
   207     ultimately show ?RHS ..
   208   qed
   209 qed auto
   210 
   211 subsubsection{* The @{text "-\<infinity>"} Version*}
   212 
   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)
   215 
   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)
   218 
   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)"
   222 using knneg
   223 proof (induct rule:int_ge_induct)
   224   case base thus ?case by simp
   225 next
   226   case (step i)
   227   {fix x
   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}
   232   thus ?case ..
   233 qed
   234 
   235 lemma  minusinfinity:
   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)"
   239 proof
   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
   248   thus "EX x. P x" ..
   249 qed
   250 
   251 lemma cpmi: 
   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)")
   257 proof-
   258  {assume "?R2" hence "?L"  by blast}
   259  moreover
   260  {assume H:"?R1" hence "?L" using minusinfinity[OF dp pd p1] periodic_finite_ex[OF dp pd] by simp}
   261  moreover 
   262  { fix x
   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]
   275    have "?R1" by blast}
   276  ultimately show ?thesis by blast
   277 qed
   278 
   279 subsubsection {* The @{text "+\<infinity>"} Version*}
   280 
   281 lemma  plusinfinity:
   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)"
   285 proof
   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
   296   thus "EX x. P x" ..
   297 qed
   298 
   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)"
   302 using knneg
   303 proof (induct rule:int_ge_induct)
   304   case base thus ?case by simp
   305 next
   306   case (step i)
   307   {fix x
   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 add_ac)
   311     ultimately have "P x \<longrightarrow> P(x + (i + 1) * d)" by blast}
   312   thus ?case ..
   313 qed
   314 
   315 lemma cppi: 
   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)")
   320 proof-
   321  {assume "?R2" hence "?L"  by blast}
   322  moreover
   323  {assume H:"?R1" hence "?L" using plusinfinity[OF dp pd p1] periodic_finite_ex[OF dp pd] by simp}
   324  moreover 
   325  { fix x
   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]
   338    have "?R1" by blast}
   339  ultimately show ?thesis by blast
   340 qed
   341 
   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)
   344 apply(fastforce)
   345 done
   346 
   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])
   349   apply (rule iffI)
   350   defer
   351   apply (erule exE)
   352   apply (rule_tac x = "l * x" in exI)
   353   apply (simp add: dvd_def)
   354   apply (rule_tac x = x in exI, simp)
   355   apply (erule exE)
   356   apply (erule conjE)
   357   apply simp
   358   apply (erule dvdE)
   359   apply (rule_tac x = k in exI)
   360   apply simp
   361   done
   362 
   363 lemma zdvd_mono: assumes not0: "(k::int) \<noteq> 0"
   364 shows "((m::int) dvd t) \<equiv> (k*m dvd k*t)" 
   365   using not0 by (simp add: dvd_def)
   366 
   367 lemma uminus_dvd_conv: "(d dvd (t::int)) \<equiv> (-d dvd t)" "(d dvd (t::int)) \<equiv> (d dvd -t)"
   368   by simp_all
   369 
   370 text {* \bigskip Theorems for transforming predicates on nat to predicates on @{text int}*}
   371 
   372 lemma zdiff_int_split: "P (int (x - y)) =
   373   ((y \<le> x \<longrightarrow> P (int x - int y)) \<and> (x < y \<longrightarrow> P 0))"
   374   by (cases "y \<le> x") (simp_all add: zdiff_int)
   375 
   376 text {*
   377   \medskip Specific instances of congruence rules, to prevent
   378   simplifier from looping. *}
   379 
   380 theorem imp_le_cong:
   381   "\<lbrakk>x = x'; 0 \<le> x' \<Longrightarrow> P = P'\<rbrakk> \<Longrightarrow> (0 \<le> (x::int) \<longrightarrow> P) = (0 \<le> x' \<longrightarrow> P')"
   382   by simp
   383 
   384 theorem conj_le_cong:
   385   "\<lbrakk>x = x'; 0 \<le> x' \<Longrightarrow> P = P'\<rbrakk> \<Longrightarrow> (0 \<le> (x::int) \<and> P) = (0 \<le> x' \<and> P')"
   386   by (simp cong: conj_cong)
   387 
   388 ML_file "Tools/Qelim/cooper.ML"
   389 setup Cooper.setup
   390 
   391 method_setup presburger = {*
   392   let
   393     fun keyword k = Scan.lift (Args.$$$ k -- Args.colon) >> K ()
   394     fun simple_keyword k = Scan.lift (Args.$$$ k) >> K ()
   395     val addN = "add"
   396     val delN = "del"
   397     val elimN = "elim"
   398     val any_keyword = keyword addN || keyword delN || simple_keyword elimN
   399     val thms = Scan.repeat (Scan.unless any_keyword Attrib.multi_thm) >> flat;
   400   in
   401     Scan.optional (simple_keyword elimN >> K false) true --
   402     Scan.optional (keyword addN |-- thms) [] --
   403     Scan.optional (keyword delN |-- thms) [] >>
   404     (fn ((elim, add_ths), del_ths) => fn ctxt =>
   405       SIMPLE_METHOD' (Cooper.tac elim add_ths del_ths ctxt))
   406   end
   407 *} "Cooper's algorithm for Presburger arithmetic"
   408 
   409 declare dvd_eq_mod_eq_0[symmetric, presburger]
   410 declare mod_1[presburger] 
   411 declare mod_0[presburger]
   412 declare mod_by_1[presburger]
   413 declare mod_self[presburger]
   414 declare mod_by_0[presburger]
   415 declare mod_div_trivial[presburger]
   416 declare div_mod_equality2[presburger]
   417 declare div_mod_equality[presburger]
   418 declare mod_div_equality2[presburger]
   419 declare mod_div_equality[presburger]
   420 declare mod_mult_self1[presburger]
   421 declare mod_mult_self2[presburger]
   422 declare div_mod_equality[presburger]
   423 declare div_mod_equality2[presburger]
   424 declare mod2_Suc_Suc[presburger]
   425 lemma [presburger]: "(a::int) div 0 = 0" and [presburger]: "a mod 0 = a"
   426 by simp_all
   427 
   428 lemma [presburger, algebra]: "m mod 2 = (1::nat) \<longleftrightarrow> \<not> 2 dvd m " by presburger
   429 lemma [presburger, algebra]: "m mod 2 = Suc 0 \<longleftrightarrow> \<not> 2 dvd m " by presburger
   430 lemma [presburger, algebra]: "m mod (Suc (Suc 0)) = (1::nat) \<longleftrightarrow> \<not> 2 dvd m " by presburger
   431 lemma [presburger, algebra]: "m mod (Suc (Suc 0)) = Suc 0 \<longleftrightarrow> \<not> 2 dvd m " by presburger
   432 lemma [presburger, algebra]: "m mod 2 = (1::int) \<longleftrightarrow> \<not> 2 dvd m " by presburger
   433 
   434 end