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