moved Presburger setup back to Presburger.thy;
authorwenzelm
Thu Jun 21 20:48:47 2007 +0200 (2007-06-21)
changeset 234658f8835aac299
parent 23464 bc2563c37b1a
child 23466 886655a150f6
moved Presburger setup back to Presburger.thy;
src/HOL/Arith_Tools.thy
src/HOL/PreList.thy
src/HOL/Presburger.thy
     1.1 --- a/src/HOL/Arith_Tools.thy	Thu Jun 21 20:07:26 2007 +0200
     1.2 +++ b/src/HOL/Arith_Tools.thy	Thu Jun 21 20:48:47 2007 +0200
     1.3 @@ -7,16 +7,12 @@
     1.4  header {* Setup of arithmetic tools *}
     1.5  
     1.6  theory Arith_Tools
     1.7 -imports Groebner_Basis Dense_Linear_Order SetInterval
     1.8 +imports Groebner_Basis Dense_Linear_Order
     1.9  uses
    1.10    "~~/src/Provers/Arith/cancel_numeral_factor.ML"
    1.11    "~~/src/Provers/Arith/extract_common_term.ML"
    1.12    "int_factor_simprocs.ML"
    1.13    "nat_simprocs.ML"
    1.14 -  "Tools/Presburger/cooper_data.ML"
    1.15 -  "Tools/Presburger/generated_cooper.ML"
    1.16 -  ("Tools/Presburger/cooper.ML")
    1.17 -  ("Tools/Presburger/presburger.ML") 
    1.18  begin
    1.19  
    1.20  subsection {* Simprocs for the Naturals *}
    1.21 @@ -941,681 +937,4 @@
    1.22  end
    1.23  *}
    1.24  
    1.25 -
    1.26 -subsection {* Decision Procedure for Presburger Arithmetic *}
    1.27 -
    1.28 -setup CooperData.setup
    1.29 -
    1.30 -subsection{* The @{text "-\<infinity>"} and @{text "+\<infinity>"} Properties *}
    1.31 -
    1.32 -lemma minf:
    1.33 -  "\<lbrakk>\<exists>(z ::'a::linorder).\<forall>x<z. P x = P' x; \<exists>z.\<forall>x<z. Q x = Q' x\<rbrakk> 
    1.34 -     \<Longrightarrow> \<exists>z.\<forall>x<z. (P x \<and> Q x) = (P' x \<and> Q' x)"
    1.35 -  "\<lbrakk>\<exists>(z ::'a::linorder).\<forall>x<z. P x = P' x; \<exists>z.\<forall>x<z. Q x = Q' x\<rbrakk> 
    1.36 -     \<Longrightarrow> \<exists>z.\<forall>x<z. (P x \<or> Q x) = (P' x \<or> Q' x)"
    1.37 -  "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x = t) = False"
    1.38 -  "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x \<noteq> t) = True"
    1.39 -  "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x < t) = True"
    1.40 -  "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x \<le> t) = True"
    1.41 -  "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x > t) = False"
    1.42 -  "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x \<ge> t) = False"
    1.43 -  "\<exists>z.\<forall>(x::'a::{linorder,plus,times})<z. (d dvd x + s) = (d dvd x + s)"
    1.44 -  "\<exists>z.\<forall>(x::'a::{linorder,plus,times})<z. (\<not> d dvd x + s) = (\<not> d dvd x + s)"
    1.45 -  "\<exists>z.\<forall>x<z. F = F"
    1.46 -  by ((erule exE, erule exE,rule_tac x="min z za" in exI,simp)+, (rule_tac x="t" in exI,fastsimp)+) simp_all
    1.47 -
    1.48 -lemma pinf:
    1.49 -  "\<lbrakk>\<exists>(z ::'a::linorder).\<forall>x>z. P x = P' x; \<exists>z.\<forall>x>z. Q x = Q' x\<rbrakk> 
    1.50 -     \<Longrightarrow> \<exists>z.\<forall>x>z. (P x \<and> Q x) = (P' x \<and> Q' x)"
    1.51 -  "\<lbrakk>\<exists>(z ::'a::linorder).\<forall>x>z. P x = P' x; \<exists>z.\<forall>x>z. Q x = Q' x\<rbrakk> 
    1.52 -     \<Longrightarrow> \<exists>z.\<forall>x>z. (P x \<or> Q x) = (P' x \<or> Q' x)"
    1.53 -  "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x = t) = False"
    1.54 -  "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x \<noteq> t) = True"
    1.55 -  "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x < t) = False"
    1.56 -  "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x \<le> t) = False"
    1.57 -  "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x > t) = True"
    1.58 -  "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x \<ge> t) = True"
    1.59 -  "\<exists>z.\<forall>(x::'a::{linorder,plus,times})>z. (d dvd x + s) = (d dvd x + s)"
    1.60 -  "\<exists>z.\<forall>(x::'a::{linorder,plus,times})>z. (\<not> d dvd x + s) = (\<not> d dvd x + s)"
    1.61 -  "\<exists>z.\<forall>x>z. F = F"
    1.62 -  by ((erule exE, erule exE,rule_tac x="max z za" in exI,simp)+,(rule_tac x="t" in exI,fastsimp)+) simp_all
    1.63 -
    1.64 -lemma inf_period:
    1.65 -  "\<lbrakk>\<forall>x k. P x = P (x - k*D); \<forall>x k. Q x = Q (x - k*D)\<rbrakk> 
    1.66 -    \<Longrightarrow> \<forall>x k. (P x \<and> Q x) = (P (x - k*D) \<and> Q (x - k*D))"
    1.67 -  "\<lbrakk>\<forall>x k. P x = P (x - k*D); \<forall>x k. Q x = Q (x - k*D)\<rbrakk> 
    1.68 -    \<Longrightarrow> \<forall>x k. (P x \<or> Q x) = (P (x - k*D) \<or> Q (x - k*D))"
    1.69 -  "(d::'a::{comm_ring}) dvd D \<Longrightarrow> \<forall>x k. (d dvd x + t) = (d dvd (x - k*D) + t)"
    1.70 -  "(d::'a::{comm_ring}) dvd D \<Longrightarrow> \<forall>x k. (\<not>d dvd x + t) = (\<not>d dvd (x - k*D) + t)"
    1.71 -  "\<forall>x k. F = F"
    1.72 -by simp_all
    1.73 -  (clarsimp simp add: dvd_def, rule iffI, clarsimp,rule_tac x = "kb - ka*k" in exI,
    1.74 -    simp add: ring_eq_simps, clarsimp,rule_tac x = "kb + ka*k" in exI,simp add: ring_eq_simps)+
    1.75 -
    1.76 -section{* The A and B sets *}
    1.77 -lemma bset:
    1.78 -  "\<lbrakk>\<forall>x.(\<forall>j \<in> {1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> P x \<longrightarrow> P(x - D) ;
    1.79 -     \<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> Q x \<longrightarrow> Q(x - D)\<rbrakk> \<Longrightarrow> 
    1.80 -  \<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))"
    1.81 -  "\<lbrakk>\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> P x \<longrightarrow> P(x - D) ;
    1.82 -     \<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> Q x \<longrightarrow> Q(x - D)\<rbrakk> \<Longrightarrow> 
    1.83 -  \<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))"
    1.84 -  "\<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))"
    1.85 -  "\<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))"
    1.86 -  "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))"
    1.87 -  "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))"
    1.88 -  "\<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))"
    1.89 -  "\<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))"
    1.90 -  "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))"
    1.91 -  "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))"
    1.92 -  "\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j) \<longrightarrow> F \<longrightarrow> F"
    1.93 -proof (blast, blast)
    1.94 -  assume dp: "D > 0" and tB: "t - 1\<in> B"
    1.95 -  show "(\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> (x = t) \<longrightarrow> (x - D = t))" 
    1.96 -    apply (rule allI, rule impI,erule ballE[where x="1"],erule ballE[where x="t - 1"])
    1.97 -    using dp tB by simp_all
    1.98 -next
    1.99 -  assume dp: "D > 0" and tB: "t \<in> B"
   1.100 -  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))" 
   1.101 -    apply (rule allI, rule impI,erule ballE[where x="D"],erule ballE[where x="t"])
   1.102 -    using dp tB by simp_all
   1.103 -next
   1.104 -  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
   1.105 -next
   1.106 -  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
   1.107 -next
   1.108 -  assume dp: "D > 0" and tB:"t \<in> B"
   1.109 -  {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"
   1.110 -    hence "x -t \<le> D" and "1 \<le> x - t" by simp+
   1.111 -      hence "\<exists>j \<in> {1 .. D}. x - t = j" by auto
   1.112 -      hence "\<exists>j \<in> {1 .. D}. x = t + j" by (simp add: ring_eq_simps)
   1.113 -      with nob tB have "False" by simp}
   1.114 -  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
   1.115 -next
   1.116 -  assume dp: "D > 0" and tB:"t - 1\<in> B"
   1.117 -  {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"
   1.118 -    hence "x - (t - 1) \<le> D" and "1 \<le> x - (t - 1)" by simp+
   1.119 -      hence "\<exists>j \<in> {1 .. D}. x - (t - 1) = j" by auto
   1.120 -      hence "\<exists>j \<in> {1 .. D}. x = (t - 1) + j" by (simp add: ring_eq_simps)
   1.121 -      with nob tB have "False" by simp}
   1.122 -  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
   1.123 -next
   1.124 -  assume d: "d dvd D"
   1.125 -  {fix x assume H: "d dvd x + t" with d have "d dvd (x - D) + t"
   1.126 -      by (clarsimp simp add: dvd_def,rule_tac x= "ka - k" in exI,simp add: ring_eq_simps)}
   1.127 -  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
   1.128 -next
   1.129 -  assume d: "d dvd D"
   1.130 -  {fix x assume H: "\<not>(d dvd x + t)" with d have "\<not>d dvd (x - D) + t"
   1.131 -      by (clarsimp simp add: dvd_def,erule_tac x= "ka + k" in allE,simp add: ring_eq_simps)}
   1.132 -  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
   1.133 -qed blast
   1.134 -
   1.135 -lemma aset:
   1.136 -  "\<lbrakk>\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> P x \<longrightarrow> P(x + D) ;
   1.137 -     \<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> Q x \<longrightarrow> Q(x + D)\<rbrakk> \<Longrightarrow> 
   1.138 -  \<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))"
   1.139 -  "\<lbrakk>\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> P x \<longrightarrow> P(x + D) ;
   1.140 -     \<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> Q x \<longrightarrow> Q(x + D)\<rbrakk> \<Longrightarrow> 
   1.141 -  \<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))"
   1.142 -  "\<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))"
   1.143 -  "\<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))"
   1.144 -  "\<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))"
   1.145 -  "\<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))"
   1.146 -  "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))"
   1.147 -  "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))"
   1.148 -  "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))"
   1.149 -  "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))"
   1.150 -  "\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j) \<longrightarrow> F \<longrightarrow> F"
   1.151 -proof (blast, blast)
   1.152 -  assume dp: "D > 0" and tA: "t + 1 \<in> A"
   1.153 -  show "(\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> (x = t) \<longrightarrow> (x + D = t))" 
   1.154 -    apply (rule allI, rule impI,erule ballE[where x="1"],erule ballE[where x="t + 1"])
   1.155 -    using dp tA by simp_all
   1.156 -next
   1.157 -  assume dp: "D > 0" and tA: "t \<in> A"
   1.158 -  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))" 
   1.159 -    apply (rule allI, rule impI,erule ballE[where x="D"],erule ballE[where x="t"])
   1.160 -    using dp tA by simp_all
   1.161 -next
   1.162 -  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
   1.163 -next
   1.164 -  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
   1.165 -next
   1.166 -  assume dp: "D > 0" and tA:"t \<in> A"
   1.167 -  {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"
   1.168 -    hence "t - x \<le> D" and "1 \<le> t - x" by simp+
   1.169 -      hence "\<exists>j \<in> {1 .. D}. t - x = j" by auto
   1.170 -      hence "\<exists>j \<in> {1 .. D}. x = t - j" by (auto simp add: ring_eq_simps) 
   1.171 -      with nob tA have "False" by simp}
   1.172 -  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
   1.173 -next
   1.174 -  assume dp: "D > 0" and tA:"t + 1\<in> A"
   1.175 -  {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"
   1.176 -    hence "(t + 1) - x \<le> D" and "1 \<le> (t + 1) - x" by (simp_all add: ring_eq_simps)
   1.177 -      hence "\<exists>j \<in> {1 .. D}. (t + 1) - x = j" by auto
   1.178 -      hence "\<exists>j \<in> {1 .. D}. x = (t + 1) - j" by (auto simp add: ring_eq_simps)
   1.179 -      with nob tA have "False" by simp}
   1.180 -  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
   1.181 -next
   1.182 -  assume d: "d dvd D"
   1.183 -  {fix x assume H: "d dvd x + t" with d have "d dvd (x + D) + t"
   1.184 -      by (clarsimp simp add: dvd_def,rule_tac x= "ka + k" in exI,simp add: ring_eq_simps)}
   1.185 -  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
   1.186 -next
   1.187 -  assume d: "d dvd D"
   1.188 -  {fix x assume H: "\<not>(d dvd x + t)" with d have "\<not>d dvd (x + D) + t"
   1.189 -      by (clarsimp simp add: dvd_def,erule_tac x= "ka - k" in allE,simp add: ring_eq_simps)}
   1.190 -  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
   1.191 -qed blast
   1.192 -
   1.193 -subsection{* Cooper's Theorem @{text "-\<infinity>"} and @{text "+\<infinity>"} Version *}
   1.194 -
   1.195 -subsubsection{* First some trivial facts about periodic sets or predicates *}
   1.196 -lemma periodic_finite_ex:
   1.197 -  assumes dpos: "(0::int) < d" and modd: "ALL x k. P x = P(x - k*d)"
   1.198 -  shows "(EX x. P x) = (EX j : {1..d}. P j)"
   1.199 -  (is "?LHS = ?RHS")
   1.200 -proof
   1.201 -  assume ?LHS
   1.202 -  then obtain x where P: "P x" ..
   1.203 -  have "x mod d = x - (x div d)*d" by(simp add:zmod_zdiv_equality mult_ac eq_diff_eq)
   1.204 -  hence Pmod: "P x = P(x mod d)" using modd by simp
   1.205 -  show ?RHS
   1.206 -  proof (cases)
   1.207 -    assume "x mod d = 0"
   1.208 -    hence "P 0" using P Pmod by simp
   1.209 -    moreover have "P 0 = P(0 - (-1)*d)" using modd by blast
   1.210 -    ultimately have "P d" by simp
   1.211 -    moreover have "d : {1..d}" using dpos by(simp add:atLeastAtMost_iff)
   1.212 -    ultimately show ?RHS ..
   1.213 -  next
   1.214 -    assume not0: "x mod d \<noteq> 0"
   1.215 -    have "P(x mod d)" using dpos P Pmod by(simp add:pos_mod_sign pos_mod_bound)
   1.216 -    moreover have "x mod d : {1..d}"
   1.217 -    proof -
   1.218 -      from dpos have "0 \<le> x mod d" by(rule pos_mod_sign)
   1.219 -      moreover from dpos have "x mod d < d" by(rule pos_mod_bound)
   1.220 -      ultimately show ?thesis using not0 by(simp add:atLeastAtMost_iff)
   1.221 -    qed
   1.222 -    ultimately show ?RHS ..
   1.223 -  qed
   1.224 -qed auto
   1.225 -
   1.226 -subsubsection{* The @{text "-\<infinity>"} Version*}
   1.227 -
   1.228 -lemma decr_lemma: "0 < (d::int) \<Longrightarrow> x - (abs(x-z)+1) * d < z"
   1.229 -by(induct rule: int_gr_induct,simp_all add:int_distrib)
   1.230 -
   1.231 -lemma incr_lemma: "0 < (d::int) \<Longrightarrow> z < x + (abs(x-z)+1) * d"
   1.232 -by(induct rule: int_gr_induct, simp_all add:int_distrib)
   1.233 -
   1.234 -theorem int_induct[case_names base step1 step2]:
   1.235 -  assumes 
   1.236 -  base: "P(k::int)" and step1: "\<And>i. \<lbrakk>k \<le> i; P i\<rbrakk> \<Longrightarrow> P(i+1)" and
   1.237 -  step2: "\<And>i. \<lbrakk>k \<ge> i; P i\<rbrakk> \<Longrightarrow> P(i - 1)"
   1.238 -  shows "P i"
   1.239 -proof -
   1.240 -  have "i \<le> k \<or> i\<ge> k" by arith
   1.241 -  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
   1.242 -qed
   1.243 -
   1.244 -lemma decr_mult_lemma:
   1.245 -  assumes dpos: "(0::int) < d" and minus: "\<forall>x. P x \<longrightarrow> P(x - d)" and knneg: "0 <= k"
   1.246 -  shows "ALL x. P x \<longrightarrow> P(x - k*d)"
   1.247 -using knneg
   1.248 -proof (induct rule:int_ge_induct)
   1.249 -  case base thus ?case by simp
   1.250 -next
   1.251 -  case (step i)
   1.252 -  {fix x
   1.253 -    have "P x \<longrightarrow> P (x - i * d)" using step.hyps by blast
   1.254 -    also have "\<dots> \<longrightarrow> P(x - (i + 1) * d)" using minus[THEN spec, of "x - i * d"]
   1.255 -      by (simp add:int_distrib OrderedGroup.diff_diff_eq[symmetric])
   1.256 -    ultimately have "P x \<longrightarrow> P(x - (i + 1) * d)" by blast}
   1.257 -  thus ?case ..
   1.258 -qed
   1.259 -
   1.260 -lemma  minusinfinity:
   1.261 -  assumes dpos: "0 < d" and
   1.262 -    P1eqP1: "ALL x k. P1 x = P1(x - k*d)" and ePeqP1: "EX z::int. ALL x. x < z \<longrightarrow> (P x = P1 x)"
   1.263 -  shows "(EX x. P1 x) \<longrightarrow> (EX x. P x)"
   1.264 -proof
   1.265 -  assume eP1: "EX x. P1 x"
   1.266 -  then obtain x where P1: "P1 x" ..
   1.267 -  from ePeqP1 obtain z where P1eqP: "ALL x. x < z \<longrightarrow> (P x = P1 x)" ..
   1.268 -  let ?w = "x - (abs(x-z)+1) * d"
   1.269 -  from dpos have w: "?w < z" by(rule decr_lemma)
   1.270 -  have "P1 x = P1 ?w" using P1eqP1 by blast
   1.271 -  also have "\<dots> = P(?w)" using w P1eqP by blast
   1.272 -  finally have "P ?w" using P1 by blast
   1.273 -  thus "EX x. P x" ..
   1.274 -qed
   1.275 -
   1.276 -lemma cpmi: 
   1.277 -  assumes dp: "0 < D" and p1:"\<exists>z. \<forall> x< z. P x = P' x"
   1.278 -  and nb:"\<forall>x.(\<forall> j\<in> {1..D}. \<forall>(b::int) \<in> B. x \<noteq> b+j) --> P (x) --> P (x - D)"
   1.279 -  and pd: "\<forall> x k. P' x = P' (x-k*D)"
   1.280 -  shows "(\<exists>x. P x) = ((\<exists> j\<in> {1..D} . P' j) | (\<exists> j \<in> {1..D}.\<exists> b\<in> B. P (b+j)))" 
   1.281 -         (is "?L = (?R1 \<or> ?R2)")
   1.282 -proof-
   1.283 - {assume "?R2" hence "?L"  by blast}
   1.284 - moreover
   1.285 - {assume H:"?R1" hence "?L" using minusinfinity[OF dp pd p1] periodic_finite_ex[OF dp pd] by simp}
   1.286 - moreover 
   1.287 - { fix x
   1.288 -   assume P: "P x" and H: "\<not> ?R2"
   1.289 -   {fix y assume "\<not> (\<exists>j\<in>{1..D}. \<exists>b\<in>B. P (b + j))" and P: "P y"
   1.290 -     hence "~(EX (j::int) : {1..D}. EX (b::int) : B. y = b+j)" by auto
   1.291 -     with nb P  have "P (y - D)" by auto }
   1.292 -   hence "ALL x.~(EX (j::int) : {1..D}. EX (b::int) : B. P(b+j)) --> P (x) --> P (x - D)" by blast
   1.293 -   with H P have th: " \<forall>x. P x \<longrightarrow> P (x - D)" by auto
   1.294 -   from p1 obtain z where z: "ALL x. x < z --> (P x = P' x)" by blast
   1.295 -   let ?y = "x - (\<bar>x - z\<bar> + 1)*D"
   1.296 -   have zp: "0 <= (\<bar>x - z\<bar> + 1)" by arith
   1.297 -   from dp have yz: "?y < z" using decr_lemma[OF dp] by simp   
   1.298 -   from z[rule_format, OF yz] decr_mult_lemma[OF dp th zp, rule_format, OF P] have th2: " P' ?y" by auto
   1.299 -   with periodic_finite_ex[OF dp pd]
   1.300 -   have "?R1" by blast}
   1.301 - ultimately show ?thesis by blast
   1.302 -qed
   1.303 -
   1.304 -subsubsection {* The @{text "+\<infinity>"} Version*}
   1.305 -
   1.306 -lemma  plusinfinity:
   1.307 -  assumes dpos: "(0::int) < d" and
   1.308 -    P1eqP1: "\<forall>x k. P' x = P'(x - k*d)" and ePeqP1: "\<exists> z. \<forall> x>z. P x = P' x"
   1.309 -  shows "(\<exists> x. P' x) \<longrightarrow> (\<exists> x. P x)"
   1.310 -proof
   1.311 -  assume eP1: "EX x. P' x"
   1.312 -  then obtain x where P1: "P' x" ..
   1.313 -  from ePeqP1 obtain z where P1eqP: "\<forall>x>z. P x = P' x" ..
   1.314 -  let ?w' = "x + (abs(x-z)+1) * d"
   1.315 -  let ?w = "x - (-(abs(x-z) + 1))*d"
   1.316 -  have ww'[simp]: "?w = ?w'" by (simp add: ring_eq_simps)
   1.317 -  from dpos have w: "?w > z" by(simp only: ww' incr_lemma)
   1.318 -  hence "P' x = P' ?w" using P1eqP1 by blast
   1.319 -  also have "\<dots> = P(?w)" using w P1eqP by blast
   1.320 -  finally have "P ?w" using P1 by blast
   1.321 -  thus "EX x. P x" ..
   1.322 -qed
   1.323 -
   1.324 -lemma incr_mult_lemma:
   1.325 -  assumes dpos: "(0::int) < d" and plus: "ALL x::int. P x \<longrightarrow> P(x + d)" and knneg: "0 <= k"
   1.326 -  shows "ALL x. P x \<longrightarrow> P(x + k*d)"
   1.327 -using knneg
   1.328 -proof (induct rule:int_ge_induct)
   1.329 -  case base thus ?case by simp
   1.330 -next
   1.331 -  case (step i)
   1.332 -  {fix x
   1.333 -    have "P x \<longrightarrow> P (x + i * d)" using step.hyps by blast
   1.334 -    also have "\<dots> \<longrightarrow> P(x + (i + 1) * d)" using plus[THEN spec, of "x + i * d"]
   1.335 -      by (simp add:int_distrib zadd_ac)
   1.336 -    ultimately have "P x \<longrightarrow> P(x + (i + 1) * d)" by blast}
   1.337 -  thus ?case ..
   1.338 -qed
   1.339 -
   1.340 -lemma cppi: 
   1.341 -  assumes dp: "0 < D" and p1:"\<exists>z. \<forall> x> z. P x = P' x"
   1.342 -  and nb:"\<forall>x.(\<forall> j\<in> {1..D}. \<forall>(b::int) \<in> A. x \<noteq> b - j) --> P (x) --> P (x + D)"
   1.343 -  and pd: "\<forall> x k. P' x= P' (x-k*D)"
   1.344 -  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)")
   1.345 -proof-
   1.346 - {assume "?R2" hence "?L"  by blast}
   1.347 - moreover
   1.348 - {assume H:"?R1" hence "?L" using plusinfinity[OF dp pd p1] periodic_finite_ex[OF dp pd] by simp}
   1.349 - moreover 
   1.350 - { fix x
   1.351 -   assume P: "P x" and H: "\<not> ?R2"
   1.352 -   {fix y assume "\<not> (\<exists>j\<in>{1..D}. \<exists>b\<in>A. P (b - j))" and P: "P y"
   1.353 -     hence "~(EX (j::int) : {1..D}. EX (b::int) : A. y = b - j)" by auto
   1.354 -     with nb P  have "P (y + D)" by auto }
   1.355 -   hence "ALL x.~(EX (j::int) : {1..D}. EX (b::int) : A. P(b-j)) --> P (x) --> P (x + D)" by blast
   1.356 -   with H P have th: " \<forall>x. P x \<longrightarrow> P (x + D)" by auto
   1.357 -   from p1 obtain z where z: "ALL x. x > z --> (P x = P' x)" by blast
   1.358 -   let ?y = "x + (\<bar>x - z\<bar> + 1)*D"
   1.359 -   have zp: "0 <= (\<bar>x - z\<bar> + 1)" by arith
   1.360 -   from dp have yz: "?y > z" using incr_lemma[OF dp] by simp
   1.361 -   from z[rule_format, OF yz] incr_mult_lemma[OF dp th zp, rule_format, OF P] have th2: " P' ?y" by auto
   1.362 -   with periodic_finite_ex[OF dp pd]
   1.363 -   have "?R1" by blast}
   1.364 - ultimately show ?thesis by blast
   1.365 -qed
   1.366 -
   1.367 -lemma simp_from_to: "{i..j::int} = (if j < i then {} else insert i {i+1..j})"
   1.368 -apply(simp add:atLeastAtMost_def atLeast_def atMost_def)
   1.369 -apply(fastsimp)
   1.370 -done
   1.371 -
   1.372 -theorem unity_coeff_ex: "(\<exists>(x::'a::{semiring_0}). P (l * x)) \<equiv> (\<exists>x. l dvd (x + 0) \<and> P x)"
   1.373 -  apply (rule eq_reflection[symmetric])
   1.374 -  apply (rule iffI)
   1.375 -  defer
   1.376 -  apply (erule exE)
   1.377 -  apply (rule_tac x = "l * x" in exI)
   1.378 -  apply (simp add: dvd_def)
   1.379 -  apply (rule_tac x="x" in exI, simp)
   1.380 -  apply (erule exE)
   1.381 -  apply (erule conjE)
   1.382 -  apply (erule dvdE)
   1.383 -  apply (rule_tac x = k in exI)
   1.384 -  apply simp
   1.385 -  done
   1.386 -
   1.387 -lemma zdvd_mono: assumes not0: "(k::int) \<noteq> 0"
   1.388 -shows "((m::int) dvd t) \<equiv> (k*m dvd k*t)" 
   1.389 -  using not0 by (simp add: dvd_def)
   1.390 -
   1.391 -lemma uminus_dvd_conv: "(d dvd (t::int)) \<equiv> (-d dvd t)" "(d dvd (t::int)) \<equiv> (d dvd -t)"
   1.392 -  by simp_all
   1.393 -text {* \bigskip Theorems for transforming predicates on nat to predicates on @{text int}*}
   1.394 -lemma all_nat: "(\<forall>x::nat. P x) = (\<forall>x::int. 0 <= x \<longrightarrow> P (nat x))"
   1.395 -  by (simp split add: split_nat)
   1.396 -
   1.397 -lemma ex_nat: "(\<exists>x::nat. P x) = (\<exists>x::int. 0 <= x \<and> P (nat x))"
   1.398 -  apply (auto split add: split_nat)
   1.399 -  apply (rule_tac x="int x" in exI, simp)
   1.400 -  apply (rule_tac x = "nat x" in exI,erule_tac x = "nat x" in allE, simp)
   1.401 -  done
   1.402 -
   1.403 -lemma zdiff_int_split: "P (int (x - y)) =
   1.404 -  ((y \<le> x \<longrightarrow> P (int x - int y)) \<and> (x < y \<longrightarrow> P 0))"
   1.405 -  by (case_tac "y \<le> x", simp_all add: zdiff_int)
   1.406 -
   1.407 -lemma number_of1: "(0::int) <= number_of n \<Longrightarrow> (0::int) <= number_of (n BIT b)" by simp
   1.408 -lemma number_of2: "(0::int) <= Numeral0" by simp
   1.409 -lemma Suc_plus1: "Suc n = n + 1" by simp
   1.410 -
   1.411 -text {*
   1.412 -  \medskip Specific instances of congruence rules, to prevent
   1.413 -  simplifier from looping. *}
   1.414 -
   1.415 -theorem imp_le_cong: "(0 <= x \<Longrightarrow> P = P') \<Longrightarrow> (0 <= (x::int) \<longrightarrow> P) = (0 <= x \<longrightarrow> P')" by simp
   1.416 -
   1.417 -theorem conj_le_cong: "(0 <= x \<Longrightarrow> P = P') \<Longrightarrow> (0 <= (x::int) \<and> P) = (0 <= x \<and> P')" 
   1.418 -  by (simp cong: conj_cong)
   1.419 -lemma int_eq_number_of_eq:
   1.420 -  "(((number_of v)::int) = (number_of w)) = iszero ((number_of (v + (uminus w)))::int)"
   1.421 -  by simp
   1.422 -
   1.423 -lemma mod_eq0_dvd_iff[presburger]: "(m::nat) mod n = 0 \<longleftrightarrow> n dvd m"
   1.424 -unfolding dvd_eq_mod_eq_0[symmetric] ..
   1.425 -
   1.426 -lemma zmod_eq0_zdvd_iff[presburger]: "(m::int) mod n = 0 \<longleftrightarrow> n dvd m"
   1.427 -unfolding zdvd_iff_zmod_eq_0[symmetric] ..
   1.428 -declare mod_1[presburger]
   1.429 -declare mod_0[presburger]
   1.430 -declare zmod_1[presburger]
   1.431 -declare zmod_zero[presburger]
   1.432 -declare zmod_self[presburger]
   1.433 -declare mod_self[presburger]
   1.434 -declare DIVISION_BY_ZERO_MOD[presburger]
   1.435 -declare nat_mod_div_trivial[presburger]
   1.436 -declare div_mod_equality2[presburger]
   1.437 -declare div_mod_equality[presburger]
   1.438 -declare mod_div_equality2[presburger]
   1.439 -declare mod_div_equality[presburger]
   1.440 -declare mod_mult_self1[presburger]
   1.441 -declare mod_mult_self2[presburger]
   1.442 -declare zdiv_zmod_equality2[presburger]
   1.443 -declare zdiv_zmod_equality[presburger]
   1.444 -declare mod2_Suc_Suc[presburger]
   1.445 -lemma [presburger]: "(a::int) div 0 = 0" and [presburger]: "a mod 0 = a"
   1.446 -using IntDiv.DIVISION_BY_ZERO by blast+
   1.447 -
   1.448 -use "Tools/Presburger/cooper.ML"
   1.449 -oracle linzqe_oracle ("term") = Coopereif.cooper_oracle
   1.450 -
   1.451 -use "Tools/Presburger/presburger.ML"
   1.452 -
   1.453 -setup {* 
   1.454 -  arith_tactic_add 
   1.455 -    (mk_arith_tactic "presburger" (fn i => fn st =>
   1.456 -       (warning "Trying Presburger arithmetic ...";   
   1.457 -    Presburger.cooper_tac true [] [] ((ProofContext.init o theory_of_thm) st) i st)))
   1.458 -  (* FIXME!!!!!!! get the right context!!*)	
   1.459 -*}
   1.460 -
   1.461 -method_setup presburger = {*
   1.462 -let
   1.463 - fun keyword k = Scan.lift (Args.$$$ k -- Args.colon) >> K ()
   1.464 - fun simple_keyword k = Scan.lift (Args.$$$ k) >> K ()
   1.465 - val addN = "add"
   1.466 - val delN = "del"
   1.467 - val elimN = "elim"
   1.468 - val any_keyword = keyword addN || keyword delN || simple_keyword elimN
   1.469 - val thms = Scan.repeat (Scan.unless any_keyword Attrib.multi_thm) >> flat;
   1.470 -in
   1.471 -  fn src => Method.syntax 
   1.472 -   ((Scan.optional (simple_keyword elimN >> K false) true) -- 
   1.473 -    (Scan.optional (keyword addN |-- thms) []) -- 
   1.474 -    (Scan.optional (keyword delN |-- thms) [])) src 
   1.475 -  #> (fn (((elim, add_ths), del_ths),ctxt) => 
   1.476 -         Method.SIMPLE_METHOD' (Presburger.cooper_tac elim add_ths del_ths ctxt))
   1.477  end
   1.478 -*} "Cooper's algorithm for Presburger arithmetic"
   1.479 -
   1.480 -lemma [presburger]: "m mod 2 = (1::nat) \<longleftrightarrow> \<not> 2 dvd m " by presburger
   1.481 -lemma [presburger]: "m mod 2 = Suc 0 \<longleftrightarrow> \<not> 2 dvd m " by presburger
   1.482 -lemma [presburger]: "m mod (Suc (Suc 0)) = (1::nat) \<longleftrightarrow> \<not> 2 dvd m " by presburger
   1.483 -lemma [presburger]: "m mod (Suc (Suc 0)) = Suc 0 \<longleftrightarrow> \<not> 2 dvd m " by presburger
   1.484 -lemma [presburger]: "m mod 2 = (1::int) \<longleftrightarrow> \<not> 2 dvd m " by presburger
   1.485 -
   1.486 -
   1.487 -subsection {* Code generator setup *}
   1.488 -
   1.489 -text {*
   1.490 -  Presburger arithmetic is convenient to prove some
   1.491 -  of the following code lemmas on integer numerals:
   1.492 -*}
   1.493 -
   1.494 -lemma eq_Pls_Pls:
   1.495 -  "Numeral.Pls = Numeral.Pls \<longleftrightarrow> True" by presburger
   1.496 -
   1.497 -lemma eq_Pls_Min:
   1.498 -  "Numeral.Pls = Numeral.Min \<longleftrightarrow> False"
   1.499 -  unfolding Pls_def Numeral.Min_def by presburger
   1.500 -
   1.501 -lemma eq_Pls_Bit0:
   1.502 -  "Numeral.Pls = Numeral.Bit k bit.B0 \<longleftrightarrow> Numeral.Pls = k"
   1.503 -  unfolding Pls_def Bit_def bit.cases by presburger
   1.504 -
   1.505 -lemma eq_Pls_Bit1:
   1.506 -  "Numeral.Pls = Numeral.Bit k bit.B1 \<longleftrightarrow> False"
   1.507 -  unfolding Pls_def Bit_def bit.cases by presburger
   1.508 -
   1.509 -lemma eq_Min_Pls:
   1.510 -  "Numeral.Min = Numeral.Pls \<longleftrightarrow> False"
   1.511 -  unfolding Pls_def Numeral.Min_def by presburger
   1.512 -
   1.513 -lemma eq_Min_Min:
   1.514 -  "Numeral.Min = Numeral.Min \<longleftrightarrow> True" by presburger
   1.515 -
   1.516 -lemma eq_Min_Bit0:
   1.517 -  "Numeral.Min = Numeral.Bit k bit.B0 \<longleftrightarrow> False"
   1.518 -  unfolding Numeral.Min_def Bit_def bit.cases by presburger
   1.519 -
   1.520 -lemma eq_Min_Bit1:
   1.521 -  "Numeral.Min = Numeral.Bit k bit.B1 \<longleftrightarrow> Numeral.Min = k"
   1.522 -  unfolding Numeral.Min_def Bit_def bit.cases by presburger
   1.523 -
   1.524 -lemma eq_Bit0_Pls:
   1.525 -  "Numeral.Bit k bit.B0 = Numeral.Pls \<longleftrightarrow> Numeral.Pls = k"
   1.526 -  unfolding Pls_def Bit_def bit.cases by presburger
   1.527 -
   1.528 -lemma eq_Bit1_Pls:
   1.529 -  "Numeral.Bit k bit.B1 = Numeral.Pls \<longleftrightarrow> False"
   1.530 -  unfolding Pls_def Bit_def bit.cases  by presburger
   1.531 -
   1.532 -lemma eq_Bit0_Min:
   1.533 -  "Numeral.Bit k bit.B0 = Numeral.Min \<longleftrightarrow> False"
   1.534 -  unfolding Numeral.Min_def Bit_def bit.cases  by presburger
   1.535 -
   1.536 -lemma eq_Bit1_Min:
   1.537 -  "(Numeral.Bit k bit.B1) = Numeral.Min \<longleftrightarrow> Numeral.Min = k"
   1.538 -  unfolding Numeral.Min_def Bit_def bit.cases  by presburger
   1.539 -
   1.540 -lemma eq_Bit_Bit:
   1.541 -  "Numeral.Bit k1 v1 = Numeral.Bit k2 v2 \<longleftrightarrow>
   1.542 -    v1 = v2 \<and> k1 = k2" 
   1.543 -  unfolding Bit_def
   1.544 -  apply (cases v1)
   1.545 -  apply (cases v2)
   1.546 -  apply auto
   1.547 -  apply presburger
   1.548 -  apply (cases v2)
   1.549 -  apply auto
   1.550 -  apply presburger
   1.551 -  apply (cases v2)
   1.552 -  apply auto
   1.553 -  done
   1.554 -
   1.555 -lemma eq_number_of:
   1.556 -  "(number_of k \<Colon> int) = number_of l \<longleftrightarrow> k = l" 
   1.557 -  unfolding number_of_is_id ..
   1.558 -
   1.559 -
   1.560 -lemma less_eq_Pls_Pls:
   1.561 -  "Numeral.Pls \<le> Numeral.Pls \<longleftrightarrow> True" by rule+
   1.562 -
   1.563 -lemma less_eq_Pls_Min:
   1.564 -  "Numeral.Pls \<le> Numeral.Min \<longleftrightarrow> False"
   1.565 -  unfolding Pls_def Numeral.Min_def by presburger
   1.566 -
   1.567 -lemma less_eq_Pls_Bit:
   1.568 -  "Numeral.Pls \<le> Numeral.Bit k v \<longleftrightarrow> Numeral.Pls \<le> k"
   1.569 -  unfolding Pls_def Bit_def by (cases v) auto
   1.570 -
   1.571 -lemma less_eq_Min_Pls:
   1.572 -  "Numeral.Min \<le> Numeral.Pls \<longleftrightarrow> True"
   1.573 -  unfolding Pls_def Numeral.Min_def by presburger
   1.574 -
   1.575 -lemma less_eq_Min_Min:
   1.576 -  "Numeral.Min \<le> Numeral.Min \<longleftrightarrow> True" by rule+
   1.577 -
   1.578 -lemma less_eq_Min_Bit0:
   1.579 -  "Numeral.Min \<le> Numeral.Bit k bit.B0 \<longleftrightarrow> Numeral.Min < k"
   1.580 -  unfolding Numeral.Min_def Bit_def by auto
   1.581 -
   1.582 -lemma less_eq_Min_Bit1:
   1.583 -  "Numeral.Min \<le> Numeral.Bit k bit.B1 \<longleftrightarrow> Numeral.Min \<le> k"
   1.584 -  unfolding Numeral.Min_def Bit_def by auto
   1.585 -
   1.586 -lemma less_eq_Bit0_Pls:
   1.587 -  "Numeral.Bit k bit.B0 \<le> Numeral.Pls \<longleftrightarrow> k \<le> Numeral.Pls"
   1.588 -  unfolding Pls_def Bit_def by simp
   1.589 -
   1.590 -lemma less_eq_Bit1_Pls:
   1.591 -  "Numeral.Bit k bit.B1 \<le> Numeral.Pls \<longleftrightarrow> k < Numeral.Pls"
   1.592 -  unfolding Pls_def Bit_def by auto
   1.593 -
   1.594 -lemma less_eq_Bit_Min:
   1.595 -  "Numeral.Bit k v \<le> Numeral.Min \<longleftrightarrow> k \<le> Numeral.Min"
   1.596 -  unfolding Numeral.Min_def Bit_def by (cases v) auto
   1.597 -
   1.598 -lemma less_eq_Bit0_Bit:
   1.599 -  "Numeral.Bit k1 bit.B0 \<le> Numeral.Bit k2 v \<longleftrightarrow> k1 \<le> k2"
   1.600 -  unfolding Bit_def bit.cases by (cases v) auto
   1.601 -
   1.602 -lemma less_eq_Bit_Bit1:
   1.603 -  "Numeral.Bit k1 v \<le> Numeral.Bit k2 bit.B1 \<longleftrightarrow> k1 \<le> k2"
   1.604 -  unfolding Bit_def bit.cases by (cases v) auto
   1.605 -
   1.606 -lemma less_eq_Bit1_Bit0:
   1.607 -  "Numeral.Bit k1 bit.B1 \<le> Numeral.Bit k2 bit.B0 \<longleftrightarrow> k1 < k2"
   1.608 -  unfolding Bit_def by (auto split: bit.split)
   1.609 -
   1.610 -lemma less_eq_number_of:
   1.611 -  "(number_of k \<Colon> int) \<le> number_of l \<longleftrightarrow> k \<le> l"
   1.612 -  unfolding number_of_is_id ..
   1.613 -
   1.614 -
   1.615 -lemma less_Pls_Pls:
   1.616 -  "Numeral.Pls < Numeral.Pls \<longleftrightarrow> False" by simp 
   1.617 -
   1.618 -lemma less_Pls_Min:
   1.619 -  "Numeral.Pls < Numeral.Min \<longleftrightarrow> False"
   1.620 -  unfolding Pls_def Numeral.Min_def  by presburger 
   1.621 -
   1.622 -lemma less_Pls_Bit0:
   1.623 -  "Numeral.Pls < Numeral.Bit k bit.B0 \<longleftrightarrow> Numeral.Pls < k"
   1.624 -  unfolding Pls_def Bit_def by auto
   1.625 -
   1.626 -lemma less_Pls_Bit1:
   1.627 -  "Numeral.Pls < Numeral.Bit k bit.B1 \<longleftrightarrow> Numeral.Pls \<le> k"
   1.628 -  unfolding Pls_def Bit_def by auto
   1.629 -
   1.630 -lemma less_Min_Pls:
   1.631 -  "Numeral.Min < Numeral.Pls \<longleftrightarrow> True"
   1.632 -  unfolding Pls_def Numeral.Min_def by presburger 
   1.633 -
   1.634 -lemma less_Min_Min:
   1.635 -  "Numeral.Min < Numeral.Min \<longleftrightarrow> False"  by simp
   1.636 -
   1.637 -lemma less_Min_Bit:
   1.638 -  "Numeral.Min < Numeral.Bit k v \<longleftrightarrow> Numeral.Min < k"
   1.639 -  unfolding Numeral.Min_def Bit_def by (auto split: bit.split)
   1.640 -
   1.641 -lemma less_Bit_Pls:
   1.642 -  "Numeral.Bit k v < Numeral.Pls \<longleftrightarrow> k < Numeral.Pls"
   1.643 -  unfolding Pls_def Bit_def by (auto split: bit.split)
   1.644 -
   1.645 -lemma less_Bit0_Min:
   1.646 -  "Numeral.Bit k bit.B0 < Numeral.Min \<longleftrightarrow> k \<le> Numeral.Min"
   1.647 -  unfolding Numeral.Min_def Bit_def by auto
   1.648 -
   1.649 -lemma less_Bit1_Min:
   1.650 -  "Numeral.Bit k bit.B1 < Numeral.Min \<longleftrightarrow> k < Numeral.Min"
   1.651 -  unfolding Numeral.Min_def Bit_def by auto
   1.652 -
   1.653 -lemma less_Bit_Bit0:
   1.654 -  "Numeral.Bit k1 v < Numeral.Bit k2 bit.B0 \<longleftrightarrow> k1 < k2"
   1.655 -  unfolding Bit_def by (auto split: bit.split)
   1.656 -
   1.657 -lemma less_Bit1_Bit:
   1.658 -  "Numeral.Bit k1 bit.B1 < Numeral.Bit k2 v \<longleftrightarrow> k1 < k2"
   1.659 -  unfolding Bit_def by (auto split: bit.split)
   1.660 -
   1.661 -lemma less_Bit0_Bit1:
   1.662 -  "Numeral.Bit k1 bit.B0 < Numeral.Bit k2 bit.B1 \<longleftrightarrow> k1 \<le> k2"
   1.663 -  unfolding Bit_def bit.cases  by arith
   1.664 -
   1.665 -lemma less_number_of:
   1.666 -  "(number_of k \<Colon> int) < number_of l \<longleftrightarrow> k < l"
   1.667 -  unfolding number_of_is_id ..
   1.668 -
   1.669 -lemmas pred_succ_numeral_code [code func] =
   1.670 -  arith_simps(5-12)
   1.671 -
   1.672 -lemmas plus_numeral_code [code func] =
   1.673 -  arith_simps(13-17)
   1.674 -  arith_simps(26-27)
   1.675 -  arith_extra_simps(1) [where 'a = int]
   1.676 -
   1.677 -lemmas minus_numeral_code [code func] =
   1.678 -  arith_simps(18-21)
   1.679 -  arith_extra_simps(2) [where 'a = int]
   1.680 -  arith_extra_simps(5) [where 'a = int]
   1.681 -
   1.682 -lemmas times_numeral_code [code func] =
   1.683 -  arith_simps(22-25)
   1.684 -  arith_extra_simps(4) [where 'a = int]
   1.685 -
   1.686 -lemmas eq_numeral_code [code func] =
   1.687 -  eq_Pls_Pls eq_Pls_Min eq_Pls_Bit0 eq_Pls_Bit1
   1.688 -  eq_Min_Pls eq_Min_Min eq_Min_Bit0 eq_Min_Bit1
   1.689 -  eq_Bit0_Pls eq_Bit1_Pls eq_Bit0_Min eq_Bit1_Min eq_Bit_Bit
   1.690 -  eq_number_of
   1.691 -
   1.692 -lemmas less_eq_numeral_code [code func] = less_eq_Pls_Pls less_eq_Pls_Min less_eq_Pls_Bit
   1.693 -  less_eq_Min_Pls less_eq_Min_Min less_eq_Min_Bit0 less_eq_Min_Bit1
   1.694 -  less_eq_Bit0_Pls less_eq_Bit1_Pls less_eq_Bit_Min less_eq_Bit0_Bit less_eq_Bit_Bit1 less_eq_Bit1_Bit0
   1.695 -  less_eq_number_of
   1.696 -
   1.697 -lemmas less_numeral_code [code func] = less_Pls_Pls less_Pls_Min less_Pls_Bit0
   1.698 -  less_Pls_Bit1 less_Min_Pls less_Min_Min less_Min_Bit less_Bit_Pls
   1.699 -  less_Bit0_Min less_Bit1_Min less_Bit_Bit0 less_Bit1_Bit less_Bit0_Bit1
   1.700 -  less_number_of
   1.701 -
   1.702 -end
     2.1 --- a/src/HOL/PreList.thy	Thu Jun 21 20:07:26 2007 +0200
     2.2 +++ b/src/HOL/PreList.thy	Thu Jun 21 20:48:47 2007 +0200
     2.3 @@ -7,7 +7,7 @@
     2.4  header {* A Basis for Building the Theory of Lists *}
     2.5  
     2.6  theory PreList
     2.7 -imports Wellfounded_Relations Arith_Tools Relation_Power SAT
     2.8 +imports Wellfounded_Relations Presburger Relation_Power SAT
     2.9    FunDef Recdef Extraction
    2.10  begin
    2.11  
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/HOL/Presburger.thy	Thu Jun 21 20:48:47 2007 +0200
     3.3 @@ -0,0 +1,691 @@
     3.4 +(* Title:      HOL/Presburger.thy
     3.5 +   ID:         $Id$
     3.6 +   Author:     Amine Chaieb, TU Muenchen
     3.7 +*)
     3.8 +
     3.9 +theory Presburger
    3.10 +imports Arith_Tools SetInterval
    3.11 +uses
    3.12 +  "Tools/Qelim/cooper_data.ML"
    3.13 +  "Tools/Qelim/generated_cooper.ML"
    3.14 +  ("Tools/Qelim/cooper.ML")
    3.15 +  ("Tools/Qelim/presburger.ML")
    3.16 +begin
    3.17 +
    3.18 +subsection {* Decision Procedure for Presburger Arithmetic *}
    3.19 +
    3.20 +setup CooperData.setup
    3.21 +
    3.22 +subsection{* The @{text "-\<infinity>"} and @{text "+\<infinity>"} Properties *}
    3.23 +
    3.24 +lemma minf:
    3.25 +  "\<lbrakk>\<exists>(z ::'a::linorder).\<forall>x<z. P x = P' x; \<exists>z.\<forall>x<z. Q x = Q' x\<rbrakk> 
    3.26 +     \<Longrightarrow> \<exists>z.\<forall>x<z. (P x \<and> Q x) = (P' x \<and> Q' x)"
    3.27 +  "\<lbrakk>\<exists>(z ::'a::linorder).\<forall>x<z. P x = P' x; \<exists>z.\<forall>x<z. Q x = Q' x\<rbrakk> 
    3.28 +     \<Longrightarrow> \<exists>z.\<forall>x<z. (P x \<or> Q x) = (P' x \<or> Q' x)"
    3.29 +  "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x = t) = False"
    3.30 +  "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x \<noteq> t) = True"
    3.31 +  "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x < t) = True"
    3.32 +  "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x \<le> t) = True"
    3.33 +  "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x > t) = False"
    3.34 +  "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x \<ge> t) = False"
    3.35 +  "\<exists>z.\<forall>(x::'a::{linorder,plus,times})<z. (d dvd x + s) = (d dvd x + s)"
    3.36 +  "\<exists>z.\<forall>(x::'a::{linorder,plus,times})<z. (\<not> d dvd x + s) = (\<not> d dvd x + s)"
    3.37 +  "\<exists>z.\<forall>x<z. F = F"
    3.38 +  by ((erule exE, erule exE,rule_tac x="min z za" in exI,simp)+, (rule_tac x="t" in exI,fastsimp)+) simp_all
    3.39 +
    3.40 +lemma pinf:
    3.41 +  "\<lbrakk>\<exists>(z ::'a::linorder).\<forall>x>z. P x = P' x; \<exists>z.\<forall>x>z. Q x = Q' x\<rbrakk> 
    3.42 +     \<Longrightarrow> \<exists>z.\<forall>x>z. (P x \<and> Q x) = (P' x \<and> Q' x)"
    3.43 +  "\<lbrakk>\<exists>(z ::'a::linorder).\<forall>x>z. P x = P' x; \<exists>z.\<forall>x>z. Q x = Q' x\<rbrakk> 
    3.44 +     \<Longrightarrow> \<exists>z.\<forall>x>z. (P x \<or> Q x) = (P' x \<or> Q' x)"
    3.45 +  "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x = t) = False"
    3.46 +  "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x \<noteq> t) = True"
    3.47 +  "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x < t) = False"
    3.48 +  "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x \<le> t) = False"
    3.49 +  "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x > t) = True"
    3.50 +  "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x \<ge> t) = True"
    3.51 +  "\<exists>z.\<forall>(x::'a::{linorder,plus,times})>z. (d dvd x + s) = (d dvd x + s)"
    3.52 +  "\<exists>z.\<forall>(x::'a::{linorder,plus,times})>z. (\<not> d dvd x + s) = (\<not> d dvd x + s)"
    3.53 +  "\<exists>z.\<forall>x>z. F = F"
    3.54 +  by ((erule exE, erule exE,rule_tac x="max z za" in exI,simp)+,(rule_tac x="t" in exI,fastsimp)+) simp_all
    3.55 +
    3.56 +lemma inf_period:
    3.57 +  "\<lbrakk>\<forall>x k. P x = P (x - k*D); \<forall>x k. Q x = Q (x - k*D)\<rbrakk> 
    3.58 +    \<Longrightarrow> \<forall>x k. (P x \<and> Q x) = (P (x - k*D) \<and> Q (x - k*D))"
    3.59 +  "\<lbrakk>\<forall>x k. P x = P (x - k*D); \<forall>x k. Q x = Q (x - k*D)\<rbrakk> 
    3.60 +    \<Longrightarrow> \<forall>x k. (P x \<or> Q x) = (P (x - k*D) \<or> Q (x - k*D))"
    3.61 +  "(d::'a::{comm_ring}) dvd D \<Longrightarrow> \<forall>x k. (d dvd x + t) = (d dvd (x - k*D) + t)"
    3.62 +  "(d::'a::{comm_ring}) dvd D \<Longrightarrow> \<forall>x k. (\<not>d dvd x + t) = (\<not>d dvd (x - k*D) + t)"
    3.63 +  "\<forall>x k. F = F"
    3.64 +by simp_all
    3.65 +  (clarsimp simp add: dvd_def, rule iffI, clarsimp,rule_tac x = "kb - ka*k" in exI,
    3.66 +    simp add: ring_eq_simps, clarsimp,rule_tac x = "kb + ka*k" in exI,simp add: ring_eq_simps)+
    3.67 +
    3.68 +section{* The A and B sets *}
    3.69 +lemma bset:
    3.70 +  "\<lbrakk>\<forall>x.(\<forall>j \<in> {1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> P x \<longrightarrow> P(x - D) ;
    3.71 +     \<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> Q x \<longrightarrow> Q(x - D)\<rbrakk> \<Longrightarrow> 
    3.72 +  \<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))"
    3.73 +  "\<lbrakk>\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> P x \<longrightarrow> P(x - D) ;
    3.74 +     \<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> Q x \<longrightarrow> Q(x - D)\<rbrakk> \<Longrightarrow> 
    3.75 +  \<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))"
    3.76 +  "\<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))"
    3.77 +  "\<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))"
    3.78 +  "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))"
    3.79 +  "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))"
    3.80 +  "\<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))"
    3.81 +  "\<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))"
    3.82 +  "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))"
    3.83 +  "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))"
    3.84 +  "\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j) \<longrightarrow> F \<longrightarrow> F"
    3.85 +proof (blast, blast)
    3.86 +  assume dp: "D > 0" and tB: "t - 1\<in> B"
    3.87 +  show "(\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> (x = t) \<longrightarrow> (x - D = t))" 
    3.88 +    apply (rule allI, rule impI,erule ballE[where x="1"],erule ballE[where x="t - 1"])
    3.89 +    using dp tB by simp_all
    3.90 +next
    3.91 +  assume dp: "D > 0" and tB: "t \<in> B"
    3.92 +  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))" 
    3.93 +    apply (rule allI, rule impI,erule ballE[where x="D"],erule ballE[where x="t"])
    3.94 +    using dp tB by simp_all
    3.95 +next
    3.96 +  assume dp: "D > 0" thus "(\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> (x < t) \<longrightarrow> (x - D < t))" by arith
    3.97 +next
    3.98 +  assume dp: "D > 0" thus "\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> (x \<le> t) \<longrightarrow> (x - D \<le> t)" by arith
    3.99 +next
   3.100 +  assume dp: "D > 0" and tB:"t \<in> B"
   3.101 +  {fix x assume nob: "\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j" and g: "x > t" and ng: "\<not> (x - D) > t"
   3.102 +    hence "x -t \<le> D" and "1 \<le> x - t" by simp+
   3.103 +      hence "\<exists>j \<in> {1 .. D}. x - t = j" by auto
   3.104 +      hence "\<exists>j \<in> {1 .. D}. x = t + j" by (simp add: ring_eq_simps)
   3.105 +      with nob tB have "False" by simp}
   3.106 +  thus "\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> (x > t) \<longrightarrow> (x - D > t)" by blast
   3.107 +next
   3.108 +  assume dp: "D > 0" and tB:"t - 1\<in> B"
   3.109 +  {fix x assume nob: "\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j" and g: "x \<ge> t" and ng: "\<not> (x - D) \<ge> t"
   3.110 +    hence "x - (t - 1) \<le> D" and "1 \<le> x - (t - 1)" by simp+
   3.111 +      hence "\<exists>j \<in> {1 .. D}. x - (t - 1) = j" by auto
   3.112 +      hence "\<exists>j \<in> {1 .. D}. x = (t - 1) + j" by (simp add: ring_eq_simps)
   3.113 +      with nob tB have "False" by simp}
   3.114 +  thus "\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> (x \<ge> t) \<longrightarrow> (x - D \<ge> t)" by blast
   3.115 +next
   3.116 +  assume d: "d dvd D"
   3.117 +  {fix x assume H: "d dvd x + t" with d have "d dvd (x - D) + t"
   3.118 +      by (clarsimp simp add: dvd_def,rule_tac x= "ka - k" in exI,simp add: ring_eq_simps)}
   3.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
   3.120 +next
   3.121 +  assume d: "d dvd D"
   3.122 +  {fix x assume H: "\<not>(d dvd x + t)" with d have "\<not>d dvd (x - D) + t"
   3.123 +      by (clarsimp simp add: dvd_def,erule_tac x= "ka + k" in allE,simp add: ring_eq_simps)}
   3.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
   3.125 +qed blast
   3.126 +
   3.127 +lemma aset:
   3.128 +  "\<lbrakk>\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> P x \<longrightarrow> P(x + D) ;
   3.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> 
   3.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))"
   3.131 +  "\<lbrakk>\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> P x \<longrightarrow> P(x + D) ;
   3.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> 
   3.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))"
   3.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))"
   3.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))"
   3.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))"
   3.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))"
   3.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))"
   3.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))"
   3.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))"
   3.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))"
   3.142 +  "\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j) \<longrightarrow> F \<longrightarrow> F"
   3.143 +proof (blast, blast)
   3.144 +  assume dp: "D > 0" and tA: "t + 1 \<in> A"
   3.145 +  show "(\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> (x = t) \<longrightarrow> (x + D = t))" 
   3.146 +    apply (rule allI, rule impI,erule ballE[where x="1"],erule ballE[where x="t + 1"])
   3.147 +    using dp tA by simp_all
   3.148 +next
   3.149 +  assume dp: "D > 0" and tA: "t \<in> A"
   3.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))" 
   3.151 +    apply (rule allI, rule impI,erule ballE[where x="D"],erule ballE[where x="t"])
   3.152 +    using dp tA by simp_all
   3.153 +next
   3.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
   3.155 +next
   3.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
   3.157 +next
   3.158 +  assume dp: "D > 0" and tA:"t \<in> A"
   3.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"
   3.160 +    hence "t - x \<le> D" and "1 \<le> t - x" by simp+
   3.161 +      hence "\<exists>j \<in> {1 .. D}. t - x = j" by auto
   3.162 +      hence "\<exists>j \<in> {1 .. D}. x = t - j" by (auto simp add: ring_eq_simps) 
   3.163 +      with nob tA have "False" by simp}
   3.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
   3.165 +next
   3.166 +  assume dp: "D > 0" and tA:"t + 1\<in> A"
   3.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"
   3.168 +    hence "(t + 1) - x \<le> D" and "1 \<le> (t + 1) - x" by (simp_all add: ring_eq_simps)
   3.169 +      hence "\<exists>j \<in> {1 .. D}. (t + 1) - x = j" by auto
   3.170 +      hence "\<exists>j \<in> {1 .. D}. x = (t + 1) - j" by (auto simp add: ring_eq_simps)
   3.171 +      with nob tA have "False" by simp}
   3.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
   3.173 +next
   3.174 +  assume d: "d dvd D"
   3.175 +  {fix x assume H: "d dvd x + t" with d have "d dvd (x + D) + t"
   3.176 +      by (clarsimp simp add: dvd_def,rule_tac x= "ka + k" in exI,simp add: ring_eq_simps)}
   3.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
   3.178 +next
   3.179 +  assume d: "d dvd D"
   3.180 +  {fix x assume H: "\<not>(d dvd x + t)" with d have "\<not>d dvd (x + D) + t"
   3.181 +      by (clarsimp simp add: dvd_def,erule_tac x= "ka - k" in allE,simp add: ring_eq_simps)}
   3.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
   3.183 +qed blast
   3.184 +
   3.185 +subsection{* Cooper's Theorem @{text "-\<infinity>"} and @{text "+\<infinity>"} Version *}
   3.186 +
   3.187 +subsubsection{* First some trivial facts about periodic sets or predicates *}
   3.188 +lemma periodic_finite_ex:
   3.189 +  assumes dpos: "(0::int) < d" and modd: "ALL x k. P x = P(x - k*d)"
   3.190 +  shows "(EX x. P x) = (EX j : {1..d}. P j)"
   3.191 +  (is "?LHS = ?RHS")
   3.192 +proof
   3.193 +  assume ?LHS
   3.194 +  then obtain x where P: "P x" ..
   3.195 +  have "x mod d = x - (x div d)*d" by(simp add:zmod_zdiv_equality mult_ac eq_diff_eq)
   3.196 +  hence Pmod: "P x = P(x mod d)" using modd by simp
   3.197 +  show ?RHS
   3.198 +  proof (cases)
   3.199 +    assume "x mod d = 0"
   3.200 +    hence "P 0" using P Pmod by simp
   3.201 +    moreover have "P 0 = P(0 - (-1)*d)" using modd by blast
   3.202 +    ultimately have "P d" by simp
   3.203 +    moreover have "d : {1..d}" using dpos by(simp add:atLeastAtMost_iff)
   3.204 +    ultimately show ?RHS ..
   3.205 +  next
   3.206 +    assume not0: "x mod d \<noteq> 0"
   3.207 +    have "P(x mod d)" using dpos P Pmod by(simp add:pos_mod_sign pos_mod_bound)
   3.208 +    moreover have "x mod d : {1..d}"
   3.209 +    proof -
   3.210 +      from dpos have "0 \<le> x mod d" by(rule pos_mod_sign)
   3.211 +      moreover from dpos have "x mod d < d" by(rule pos_mod_bound)
   3.212 +      ultimately show ?thesis using not0 by(simp add:atLeastAtMost_iff)
   3.213 +    qed
   3.214 +    ultimately show ?RHS ..
   3.215 +  qed
   3.216 +qed auto
   3.217 +
   3.218 +subsubsection{* The @{text "-\<infinity>"} Version*}
   3.219 +
   3.220 +lemma decr_lemma: "0 < (d::int) \<Longrightarrow> x - (abs(x-z)+1) * d < z"
   3.221 +by(induct rule: int_gr_induct,simp_all add:int_distrib)
   3.222 +
   3.223 +lemma incr_lemma: "0 < (d::int) \<Longrightarrow> z < x + (abs(x-z)+1) * d"
   3.224 +by(induct rule: int_gr_induct, simp_all add:int_distrib)
   3.225 +
   3.226 +theorem int_induct[case_names base step1 step2]:
   3.227 +  assumes 
   3.228 +  base: "P(k::int)" and step1: "\<And>i. \<lbrakk>k \<le> i; P i\<rbrakk> \<Longrightarrow> P(i+1)" and
   3.229 +  step2: "\<And>i. \<lbrakk>k \<ge> i; P i\<rbrakk> \<Longrightarrow> P(i - 1)"
   3.230 +  shows "P i"
   3.231 +proof -
   3.232 +  have "i \<le> k \<or> i\<ge> k" by arith
   3.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
   3.234 +qed
   3.235 +
   3.236 +lemma decr_mult_lemma:
   3.237 +  assumes dpos: "(0::int) < d" and minus: "\<forall>x. P x \<longrightarrow> P(x - d)" and knneg: "0 <= k"
   3.238 +  shows "ALL x. P x \<longrightarrow> P(x - k*d)"
   3.239 +using knneg
   3.240 +proof (induct rule:int_ge_induct)
   3.241 +  case base thus ?case by simp
   3.242 +next
   3.243 +  case (step i)
   3.244 +  {fix x
   3.245 +    have "P x \<longrightarrow> P (x - i * d)" using step.hyps by blast
   3.246 +    also have "\<dots> \<longrightarrow> P(x - (i + 1) * d)" using minus[THEN spec, of "x - i * d"]
   3.247 +      by (simp add:int_distrib OrderedGroup.diff_diff_eq[symmetric])
   3.248 +    ultimately have "P x \<longrightarrow> P(x - (i + 1) * d)" by blast}
   3.249 +  thus ?case ..
   3.250 +qed
   3.251 +
   3.252 +lemma  minusinfinity:
   3.253 +  assumes dpos: "0 < d" and
   3.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)"
   3.255 +  shows "(EX x. P1 x) \<longrightarrow> (EX x. P x)"
   3.256 +proof
   3.257 +  assume eP1: "EX x. P1 x"
   3.258 +  then obtain x where P1: "P1 x" ..
   3.259 +  from ePeqP1 obtain z where P1eqP: "ALL x. x < z \<longrightarrow> (P x = P1 x)" ..
   3.260 +  let ?w = "x - (abs(x-z)+1) * d"
   3.261 +  from dpos have w: "?w < z" by(rule decr_lemma)
   3.262 +  have "P1 x = P1 ?w" using P1eqP1 by blast
   3.263 +  also have "\<dots> = P(?w)" using w P1eqP by blast
   3.264 +  finally have "P ?w" using P1 by blast
   3.265 +  thus "EX x. P x" ..
   3.266 +qed
   3.267 +
   3.268 +lemma cpmi: 
   3.269 +  assumes dp: "0 < D" and p1:"\<exists>z. \<forall> x< z. P x = P' x"
   3.270 +  and nb:"\<forall>x.(\<forall> j\<in> {1..D}. \<forall>(b::int) \<in> B. x \<noteq> b+j) --> P (x) --> P (x - D)"
   3.271 +  and pd: "\<forall> x k. P' x = P' (x-k*D)"
   3.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)))" 
   3.273 +         (is "?L = (?R1 \<or> ?R2)")
   3.274 +proof-
   3.275 + {assume "?R2" hence "?L"  by blast}
   3.276 + moreover
   3.277 + {assume H:"?R1" hence "?L" using minusinfinity[OF dp pd p1] periodic_finite_ex[OF dp pd] by simp}
   3.278 + moreover 
   3.279 + { fix x
   3.280 +   assume P: "P x" and H: "\<not> ?R2"
   3.281 +   {fix y assume "\<not> (\<exists>j\<in>{1..D}. \<exists>b\<in>B. P (b + j))" and P: "P y"
   3.282 +     hence "~(EX (j::int) : {1..D}. EX (b::int) : B. y = b+j)" by auto
   3.283 +     with nb P  have "P (y - D)" by auto }
   3.284 +   hence "ALL x.~(EX (j::int) : {1..D}. EX (b::int) : B. P(b+j)) --> P (x) --> P (x - D)" by blast
   3.285 +   with H P have th: " \<forall>x. P x \<longrightarrow> P (x - D)" by auto
   3.286 +   from p1 obtain z where z: "ALL x. x < z --> (P x = P' x)" by blast
   3.287 +   let ?y = "x - (\<bar>x - z\<bar> + 1)*D"
   3.288 +   have zp: "0 <= (\<bar>x - z\<bar> + 1)" by arith
   3.289 +   from dp have yz: "?y < z" using decr_lemma[OF dp] by simp   
   3.290 +   from z[rule_format, OF yz] decr_mult_lemma[OF dp th zp, rule_format, OF P] have th2: " P' ?y" by auto
   3.291 +   with periodic_finite_ex[OF dp pd]
   3.292 +   have "?R1" by blast}
   3.293 + ultimately show ?thesis by blast
   3.294 +qed
   3.295 +
   3.296 +subsubsection {* The @{text "+\<infinity>"} Version*}
   3.297 +
   3.298 +lemma  plusinfinity:
   3.299 +  assumes dpos: "(0::int) < d" and
   3.300 +    P1eqP1: "\<forall>x k. P' x = P'(x - k*d)" and ePeqP1: "\<exists> z. \<forall> x>z. P x = P' x"
   3.301 +  shows "(\<exists> x. P' x) \<longrightarrow> (\<exists> x. P x)"
   3.302 +proof
   3.303 +  assume eP1: "EX x. P' x"
   3.304 +  then obtain x where P1: "P' x" ..
   3.305 +  from ePeqP1 obtain z where P1eqP: "\<forall>x>z. P x = P' x" ..
   3.306 +  let ?w' = "x + (abs(x-z)+1) * d"
   3.307 +  let ?w = "x - (-(abs(x-z) + 1))*d"
   3.308 +  have ww'[simp]: "?w = ?w'" by (simp add: ring_eq_simps)
   3.309 +  from dpos have w: "?w > z" by(simp only: ww' incr_lemma)
   3.310 +  hence "P' x = P' ?w" using P1eqP1 by blast
   3.311 +  also have "\<dots> = P(?w)" using w P1eqP by blast
   3.312 +  finally have "P ?w" using P1 by blast
   3.313 +  thus "EX x. P x" ..
   3.314 +qed
   3.315 +
   3.316 +lemma incr_mult_lemma:
   3.317 +  assumes dpos: "(0::int) < d" and plus: "ALL x::int. P x \<longrightarrow> P(x + d)" and knneg: "0 <= k"
   3.318 +  shows "ALL x. P x \<longrightarrow> P(x + k*d)"
   3.319 +using knneg
   3.320 +proof (induct rule:int_ge_induct)
   3.321 +  case base thus ?case by simp
   3.322 +next
   3.323 +  case (step i)
   3.324 +  {fix x
   3.325 +    have "P x \<longrightarrow> P (x + i * d)" using step.hyps by blast
   3.326 +    also have "\<dots> \<longrightarrow> P(x + (i + 1) * d)" using plus[THEN spec, of "x + i * d"]
   3.327 +      by (simp add:int_distrib zadd_ac)
   3.328 +    ultimately have "P x \<longrightarrow> P(x + (i + 1) * d)" by blast}
   3.329 +  thus ?case ..
   3.330 +qed
   3.331 +
   3.332 +lemma cppi: 
   3.333 +  assumes dp: "0 < D" and p1:"\<exists>z. \<forall> x> z. P x = P' x"
   3.334 +  and nb:"\<forall>x.(\<forall> j\<in> {1..D}. \<forall>(b::int) \<in> A. x \<noteq> b - j) --> P (x) --> P (x + D)"
   3.335 +  and pd: "\<forall> x k. P' x= P' (x-k*D)"
   3.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)")
   3.337 +proof-
   3.338 + {assume "?R2" hence "?L"  by blast}
   3.339 + moreover
   3.340 + {assume H:"?R1" hence "?L" using plusinfinity[OF dp pd p1] periodic_finite_ex[OF dp pd] by simp}
   3.341 + moreover 
   3.342 + { fix x
   3.343 +   assume P: "P x" and H: "\<not> ?R2"
   3.344 +   {fix y assume "\<not> (\<exists>j\<in>{1..D}. \<exists>b\<in>A. P (b - j))" and P: "P y"
   3.345 +     hence "~(EX (j::int) : {1..D}. EX (b::int) : A. y = b - j)" by auto
   3.346 +     with nb P  have "P (y + D)" by auto }
   3.347 +   hence "ALL x.~(EX (j::int) : {1..D}. EX (b::int) : A. P(b-j)) --> P (x) --> P (x + D)" by blast
   3.348 +   with H P have th: " \<forall>x. P x \<longrightarrow> P (x + D)" by auto
   3.349 +   from p1 obtain z where z: "ALL x. x > z --> (P x = P' x)" by blast
   3.350 +   let ?y = "x + (\<bar>x - z\<bar> + 1)*D"
   3.351 +   have zp: "0 <= (\<bar>x - z\<bar> + 1)" by arith
   3.352 +   from dp have yz: "?y > z" using incr_lemma[OF dp] by simp
   3.353 +   from z[rule_format, OF yz] incr_mult_lemma[OF dp th zp, rule_format, OF P] have th2: " P' ?y" by auto
   3.354 +   with periodic_finite_ex[OF dp pd]
   3.355 +   have "?R1" by blast}
   3.356 + ultimately show ?thesis by blast
   3.357 +qed
   3.358 +
   3.359 +lemma simp_from_to: "{i..j::int} = (if j < i then {} else insert i {i+1..j})"
   3.360 +apply(simp add:atLeastAtMost_def atLeast_def atMost_def)
   3.361 +apply(fastsimp)
   3.362 +done
   3.363 +
   3.364 +theorem unity_coeff_ex: "(\<exists>(x::'a::{semiring_0}). P (l * x)) \<equiv> (\<exists>x. l dvd (x + 0) \<and> P x)"
   3.365 +  apply (rule eq_reflection[symmetric])
   3.366 +  apply (rule iffI)
   3.367 +  defer
   3.368 +  apply (erule exE)
   3.369 +  apply (rule_tac x = "l * x" in exI)
   3.370 +  apply (simp add: dvd_def)
   3.371 +  apply (rule_tac x="x" in exI, simp)
   3.372 +  apply (erule exE)
   3.373 +  apply (erule conjE)
   3.374 +  apply (erule dvdE)
   3.375 +  apply (rule_tac x = k in exI)
   3.376 +  apply simp
   3.377 +  done
   3.378 +
   3.379 +lemma zdvd_mono: assumes not0: "(k::int) \<noteq> 0"
   3.380 +shows "((m::int) dvd t) \<equiv> (k*m dvd k*t)" 
   3.381 +  using not0 by (simp add: dvd_def)
   3.382 +
   3.383 +lemma uminus_dvd_conv: "(d dvd (t::int)) \<equiv> (-d dvd t)" "(d dvd (t::int)) \<equiv> (d dvd -t)"
   3.384 +  by simp_all
   3.385 +text {* \bigskip Theorems for transforming predicates on nat to predicates on @{text int}*}
   3.386 +lemma all_nat: "(\<forall>x::nat. P x) = (\<forall>x::int. 0 <= x \<longrightarrow> P (nat x))"
   3.387 +  by (simp split add: split_nat)
   3.388 +
   3.389 +lemma ex_nat: "(\<exists>x::nat. P x) = (\<exists>x::int. 0 <= x \<and> P (nat x))"
   3.390 +  apply (auto split add: split_nat)
   3.391 +  apply (rule_tac x="int x" in exI, simp)
   3.392 +  apply (rule_tac x = "nat x" in exI,erule_tac x = "nat x" in allE, simp)
   3.393 +  done
   3.394 +
   3.395 +lemma zdiff_int_split: "P (int (x - y)) =
   3.396 +  ((y \<le> x \<longrightarrow> P (int x - int y)) \<and> (x < y \<longrightarrow> P 0))"
   3.397 +  by (case_tac "y \<le> x", simp_all add: zdiff_int)
   3.398 +
   3.399 +lemma number_of1: "(0::int) <= number_of n \<Longrightarrow> (0::int) <= number_of (n BIT b)" by simp
   3.400 +lemma number_of2: "(0::int) <= Numeral0" by simp
   3.401 +lemma Suc_plus1: "Suc n = n + 1" by simp
   3.402 +
   3.403 +text {*
   3.404 +  \medskip Specific instances of congruence rules, to prevent
   3.405 +  simplifier from looping. *}
   3.406 +
   3.407 +theorem imp_le_cong: "(0 <= x \<Longrightarrow> P = P') \<Longrightarrow> (0 <= (x::int) \<longrightarrow> P) = (0 <= x \<longrightarrow> P')" by simp
   3.408 +
   3.409 +theorem conj_le_cong: "(0 <= x \<Longrightarrow> P = P') \<Longrightarrow> (0 <= (x::int) \<and> P) = (0 <= x \<and> P')" 
   3.410 +  by (simp cong: conj_cong)
   3.411 +lemma int_eq_number_of_eq:
   3.412 +  "(((number_of v)::int) = (number_of w)) = iszero ((number_of (v + (uminus w)))::int)"
   3.413 +  by simp
   3.414 +
   3.415 +lemma mod_eq0_dvd_iff[presburger]: "(m::nat) mod n = 0 \<longleftrightarrow> n dvd m"
   3.416 +unfolding dvd_eq_mod_eq_0[symmetric] ..
   3.417 +
   3.418 +lemma zmod_eq0_zdvd_iff[presburger]: "(m::int) mod n = 0 \<longleftrightarrow> n dvd m"
   3.419 +unfolding zdvd_iff_zmod_eq_0[symmetric] ..
   3.420 +declare mod_1[presburger]
   3.421 +declare mod_0[presburger]
   3.422 +declare zmod_1[presburger]
   3.423 +declare zmod_zero[presburger]
   3.424 +declare zmod_self[presburger]
   3.425 +declare mod_self[presburger]
   3.426 +declare DIVISION_BY_ZERO_MOD[presburger]
   3.427 +declare nat_mod_div_trivial[presburger]
   3.428 +declare div_mod_equality2[presburger]
   3.429 +declare div_mod_equality[presburger]
   3.430 +declare mod_div_equality2[presburger]
   3.431 +declare mod_div_equality[presburger]
   3.432 +declare mod_mult_self1[presburger]
   3.433 +declare mod_mult_self2[presburger]
   3.434 +declare zdiv_zmod_equality2[presburger]
   3.435 +declare zdiv_zmod_equality[presburger]
   3.436 +declare mod2_Suc_Suc[presburger]
   3.437 +lemma [presburger]: "(a::int) div 0 = 0" and [presburger]: "a mod 0 = a"
   3.438 +using IntDiv.DIVISION_BY_ZERO by blast+
   3.439 +
   3.440 +use "Tools/Qelim/cooper.ML"
   3.441 +oracle linzqe_oracle ("term") = Coopereif.cooper_oracle
   3.442 +
   3.443 +use "Tools/Qelim/presburger.ML"
   3.444 +
   3.445 +setup {* 
   3.446 +  arith_tactic_add 
   3.447 +    (mk_arith_tactic "presburger" (fn i => fn st =>
   3.448 +       (warning "Trying Presburger arithmetic ...";   
   3.449 +    Presburger.cooper_tac true [] [] ((ProofContext.init o theory_of_thm) st) i st)))
   3.450 +  (* FIXME!!!!!!! get the right context!!*)	
   3.451 +*}
   3.452 +
   3.453 +method_setup presburger = {*
   3.454 +let
   3.455 + fun keyword k = Scan.lift (Args.$$$ k -- Args.colon) >> K ()
   3.456 + fun simple_keyword k = Scan.lift (Args.$$$ k) >> K ()
   3.457 + val addN = "add"
   3.458 + val delN = "del"
   3.459 + val elimN = "elim"
   3.460 + val any_keyword = keyword addN || keyword delN || simple_keyword elimN
   3.461 + val thms = Scan.repeat (Scan.unless any_keyword Attrib.multi_thm) >> flat;
   3.462 +in
   3.463 +  fn src => Method.syntax 
   3.464 +   ((Scan.optional (simple_keyword elimN >> K false) true) -- 
   3.465 +    (Scan.optional (keyword addN |-- thms) []) -- 
   3.466 +    (Scan.optional (keyword delN |-- thms) [])) src 
   3.467 +  #> (fn (((elim, add_ths), del_ths),ctxt) => 
   3.468 +         Method.SIMPLE_METHOD' (Presburger.cooper_tac elim add_ths del_ths ctxt))
   3.469 +end
   3.470 +*} "Cooper's algorithm for Presburger arithmetic"
   3.471 +
   3.472 +lemma [presburger]: "m mod 2 = (1::nat) \<longleftrightarrow> \<not> 2 dvd m " by presburger
   3.473 +lemma [presburger]: "m mod 2 = Suc 0 \<longleftrightarrow> \<not> 2 dvd m " by presburger
   3.474 +lemma [presburger]: "m mod (Suc (Suc 0)) = (1::nat) \<longleftrightarrow> \<not> 2 dvd m " by presburger
   3.475 +lemma [presburger]: "m mod (Suc (Suc 0)) = Suc 0 \<longleftrightarrow> \<not> 2 dvd m " by presburger
   3.476 +lemma [presburger]: "m mod 2 = (1::int) \<longleftrightarrow> \<not> 2 dvd m " by presburger
   3.477 +
   3.478 +
   3.479 +subsection {* Code generator setup *}
   3.480 +
   3.481 +text {*
   3.482 +  Presburger arithmetic is convenient to prove some
   3.483 +  of the following code lemmas on integer numerals:
   3.484 +*}
   3.485 +
   3.486 +lemma eq_Pls_Pls:
   3.487 +  "Numeral.Pls = Numeral.Pls \<longleftrightarrow> True" by presburger
   3.488 +
   3.489 +lemma eq_Pls_Min:
   3.490 +  "Numeral.Pls = Numeral.Min \<longleftrightarrow> False"
   3.491 +  unfolding Pls_def Numeral.Min_def by presburger
   3.492 +
   3.493 +lemma eq_Pls_Bit0:
   3.494 +  "Numeral.Pls = Numeral.Bit k bit.B0 \<longleftrightarrow> Numeral.Pls = k"
   3.495 +  unfolding Pls_def Bit_def bit.cases by presburger
   3.496 +
   3.497 +lemma eq_Pls_Bit1:
   3.498 +  "Numeral.Pls = Numeral.Bit k bit.B1 \<longleftrightarrow> False"
   3.499 +  unfolding Pls_def Bit_def bit.cases by presburger
   3.500 +
   3.501 +lemma eq_Min_Pls:
   3.502 +  "Numeral.Min = Numeral.Pls \<longleftrightarrow> False"
   3.503 +  unfolding Pls_def Numeral.Min_def by presburger
   3.504 +
   3.505 +lemma eq_Min_Min:
   3.506 +  "Numeral.Min = Numeral.Min \<longleftrightarrow> True" by presburger
   3.507 +
   3.508 +lemma eq_Min_Bit0:
   3.509 +  "Numeral.Min = Numeral.Bit k bit.B0 \<longleftrightarrow> False"
   3.510 +  unfolding Numeral.Min_def Bit_def bit.cases by presburger
   3.511 +
   3.512 +lemma eq_Min_Bit1:
   3.513 +  "Numeral.Min = Numeral.Bit k bit.B1 \<longleftrightarrow> Numeral.Min = k"
   3.514 +  unfolding Numeral.Min_def Bit_def bit.cases by presburger
   3.515 +
   3.516 +lemma eq_Bit0_Pls:
   3.517 +  "Numeral.Bit k bit.B0 = Numeral.Pls \<longleftrightarrow> Numeral.Pls = k"
   3.518 +  unfolding Pls_def Bit_def bit.cases by presburger
   3.519 +
   3.520 +lemma eq_Bit1_Pls:
   3.521 +  "Numeral.Bit k bit.B1 = Numeral.Pls \<longleftrightarrow> False"
   3.522 +  unfolding Pls_def Bit_def bit.cases  by presburger
   3.523 +
   3.524 +lemma eq_Bit0_Min:
   3.525 +  "Numeral.Bit k bit.B0 = Numeral.Min \<longleftrightarrow> False"
   3.526 +  unfolding Numeral.Min_def Bit_def bit.cases  by presburger
   3.527 +
   3.528 +lemma eq_Bit1_Min:
   3.529 +  "(Numeral.Bit k bit.B1) = Numeral.Min \<longleftrightarrow> Numeral.Min = k"
   3.530 +  unfolding Numeral.Min_def Bit_def bit.cases  by presburger
   3.531 +
   3.532 +lemma eq_Bit_Bit:
   3.533 +  "Numeral.Bit k1 v1 = Numeral.Bit k2 v2 \<longleftrightarrow>
   3.534 +    v1 = v2 \<and> k1 = k2" 
   3.535 +  unfolding Bit_def
   3.536 +  apply (cases v1)
   3.537 +  apply (cases v2)
   3.538 +  apply auto
   3.539 +  apply presburger
   3.540 +  apply (cases v2)
   3.541 +  apply auto
   3.542 +  apply presburger
   3.543 +  apply (cases v2)
   3.544 +  apply auto
   3.545 +  done
   3.546 +
   3.547 +lemma eq_number_of:
   3.548 +  "(number_of k \<Colon> int) = number_of l \<longleftrightarrow> k = l" 
   3.549 +  unfolding number_of_is_id ..
   3.550 +
   3.551 +
   3.552 +lemma less_eq_Pls_Pls:
   3.553 +  "Numeral.Pls \<le> Numeral.Pls \<longleftrightarrow> True" by rule+
   3.554 +
   3.555 +lemma less_eq_Pls_Min:
   3.556 +  "Numeral.Pls \<le> Numeral.Min \<longleftrightarrow> False"
   3.557 +  unfolding Pls_def Numeral.Min_def by presburger
   3.558 +
   3.559 +lemma less_eq_Pls_Bit:
   3.560 +  "Numeral.Pls \<le> Numeral.Bit k v \<longleftrightarrow> Numeral.Pls \<le> k"
   3.561 +  unfolding Pls_def Bit_def by (cases v) auto
   3.562 +
   3.563 +lemma less_eq_Min_Pls:
   3.564 +  "Numeral.Min \<le> Numeral.Pls \<longleftrightarrow> True"
   3.565 +  unfolding Pls_def Numeral.Min_def by presburger
   3.566 +
   3.567 +lemma less_eq_Min_Min:
   3.568 +  "Numeral.Min \<le> Numeral.Min \<longleftrightarrow> True" by rule+
   3.569 +
   3.570 +lemma less_eq_Min_Bit0:
   3.571 +  "Numeral.Min \<le> Numeral.Bit k bit.B0 \<longleftrightarrow> Numeral.Min < k"
   3.572 +  unfolding Numeral.Min_def Bit_def by auto
   3.573 +
   3.574 +lemma less_eq_Min_Bit1:
   3.575 +  "Numeral.Min \<le> Numeral.Bit k bit.B1 \<longleftrightarrow> Numeral.Min \<le> k"
   3.576 +  unfolding Numeral.Min_def Bit_def by auto
   3.577 +
   3.578 +lemma less_eq_Bit0_Pls:
   3.579 +  "Numeral.Bit k bit.B0 \<le> Numeral.Pls \<longleftrightarrow> k \<le> Numeral.Pls"
   3.580 +  unfolding Pls_def Bit_def by simp
   3.581 +
   3.582 +lemma less_eq_Bit1_Pls:
   3.583 +  "Numeral.Bit k bit.B1 \<le> Numeral.Pls \<longleftrightarrow> k < Numeral.Pls"
   3.584 +  unfolding Pls_def Bit_def by auto
   3.585 +
   3.586 +lemma less_eq_Bit_Min:
   3.587 +  "Numeral.Bit k v \<le> Numeral.Min \<longleftrightarrow> k \<le> Numeral.Min"
   3.588 +  unfolding Numeral.Min_def Bit_def by (cases v) auto
   3.589 +
   3.590 +lemma less_eq_Bit0_Bit:
   3.591 +  "Numeral.Bit k1 bit.B0 \<le> Numeral.Bit k2 v \<longleftrightarrow> k1 \<le> k2"
   3.592 +  unfolding Bit_def bit.cases by (cases v) auto
   3.593 +
   3.594 +lemma less_eq_Bit_Bit1:
   3.595 +  "Numeral.Bit k1 v \<le> Numeral.Bit k2 bit.B1 \<longleftrightarrow> k1 \<le> k2"
   3.596 +  unfolding Bit_def bit.cases by (cases v) auto
   3.597 +
   3.598 +lemma less_eq_Bit1_Bit0:
   3.599 +  "Numeral.Bit k1 bit.B1 \<le> Numeral.Bit k2 bit.B0 \<longleftrightarrow> k1 < k2"
   3.600 +  unfolding Bit_def by (auto split: bit.split)
   3.601 +
   3.602 +lemma less_eq_number_of:
   3.603 +  "(number_of k \<Colon> int) \<le> number_of l \<longleftrightarrow> k \<le> l"
   3.604 +  unfolding number_of_is_id ..
   3.605 +
   3.606 +
   3.607 +lemma less_Pls_Pls:
   3.608 +  "Numeral.Pls < Numeral.Pls \<longleftrightarrow> False" by simp 
   3.609 +
   3.610 +lemma less_Pls_Min:
   3.611 +  "Numeral.Pls < Numeral.Min \<longleftrightarrow> False"
   3.612 +  unfolding Pls_def Numeral.Min_def  by presburger 
   3.613 +
   3.614 +lemma less_Pls_Bit0:
   3.615 +  "Numeral.Pls < Numeral.Bit k bit.B0 \<longleftrightarrow> Numeral.Pls < k"
   3.616 +  unfolding Pls_def Bit_def by auto
   3.617 +
   3.618 +lemma less_Pls_Bit1:
   3.619 +  "Numeral.Pls < Numeral.Bit k bit.B1 \<longleftrightarrow> Numeral.Pls \<le> k"
   3.620 +  unfolding Pls_def Bit_def by auto
   3.621 +
   3.622 +lemma less_Min_Pls:
   3.623 +  "Numeral.Min < Numeral.Pls \<longleftrightarrow> True"
   3.624 +  unfolding Pls_def Numeral.Min_def by presburger 
   3.625 +
   3.626 +lemma less_Min_Min:
   3.627 +  "Numeral.Min < Numeral.Min \<longleftrightarrow> False"  by simp
   3.628 +
   3.629 +lemma less_Min_Bit:
   3.630 +  "Numeral.Min < Numeral.Bit k v \<longleftrightarrow> Numeral.Min < k"
   3.631 +  unfolding Numeral.Min_def Bit_def by (auto split: bit.split)
   3.632 +
   3.633 +lemma less_Bit_Pls:
   3.634 +  "Numeral.Bit k v < Numeral.Pls \<longleftrightarrow> k < Numeral.Pls"
   3.635 +  unfolding Pls_def Bit_def by (auto split: bit.split)
   3.636 +
   3.637 +lemma less_Bit0_Min:
   3.638 +  "Numeral.Bit k bit.B0 < Numeral.Min \<longleftrightarrow> k \<le> Numeral.Min"
   3.639 +  unfolding Numeral.Min_def Bit_def by auto
   3.640 +
   3.641 +lemma less_Bit1_Min:
   3.642 +  "Numeral.Bit k bit.B1 < Numeral.Min \<longleftrightarrow> k < Numeral.Min"
   3.643 +  unfolding Numeral.Min_def Bit_def by auto
   3.644 +
   3.645 +lemma less_Bit_Bit0:
   3.646 +  "Numeral.Bit k1 v < Numeral.Bit k2 bit.B0 \<longleftrightarrow> k1 < k2"
   3.647 +  unfolding Bit_def by (auto split: bit.split)
   3.648 +
   3.649 +lemma less_Bit1_Bit:
   3.650 +  "Numeral.Bit k1 bit.B1 < Numeral.Bit k2 v \<longleftrightarrow> k1 < k2"
   3.651 +  unfolding Bit_def by (auto split: bit.split)
   3.652 +
   3.653 +lemma less_Bit0_Bit1:
   3.654 +  "Numeral.Bit k1 bit.B0 < Numeral.Bit k2 bit.B1 \<longleftrightarrow> k1 \<le> k2"
   3.655 +  unfolding Bit_def bit.cases  by arith
   3.656 +
   3.657 +lemma less_number_of:
   3.658 +  "(number_of k \<Colon> int) < number_of l \<longleftrightarrow> k < l"
   3.659 +  unfolding number_of_is_id ..
   3.660 +
   3.661 +lemmas pred_succ_numeral_code [code func] =
   3.662 +  arith_simps(5-12)
   3.663 +
   3.664 +lemmas plus_numeral_code [code func] =
   3.665 +  arith_simps(13-17)
   3.666 +  arith_simps(26-27)
   3.667 +  arith_extra_simps(1) [where 'a = int]
   3.668 +
   3.669 +lemmas minus_numeral_code [code func] =
   3.670 +  arith_simps(18-21)
   3.671 +  arith_extra_simps(2) [where 'a = int]
   3.672 +  arith_extra_simps(5) [where 'a = int]
   3.673 +
   3.674 +lemmas times_numeral_code [code func] =
   3.675 +  arith_simps(22-25)
   3.676 +  arith_extra_simps(4) [where 'a = int]
   3.677 +
   3.678 +lemmas eq_numeral_code [code func] =
   3.679 +  eq_Pls_Pls eq_Pls_Min eq_Pls_Bit0 eq_Pls_Bit1
   3.680 +  eq_Min_Pls eq_Min_Min eq_Min_Bit0 eq_Min_Bit1
   3.681 +  eq_Bit0_Pls eq_Bit1_Pls eq_Bit0_Min eq_Bit1_Min eq_Bit_Bit
   3.682 +  eq_number_of
   3.683 +
   3.684 +lemmas less_eq_numeral_code [code func] = less_eq_Pls_Pls less_eq_Pls_Min less_eq_Pls_Bit
   3.685 +  less_eq_Min_Pls less_eq_Min_Min less_eq_Min_Bit0 less_eq_Min_Bit1
   3.686 +  less_eq_Bit0_Pls less_eq_Bit1_Pls less_eq_Bit_Min less_eq_Bit0_Bit less_eq_Bit_Bit1 less_eq_Bit1_Bit0
   3.687 +  less_eq_number_of
   3.688 +
   3.689 +lemmas less_numeral_code [code func] = less_Pls_Pls less_Pls_Min less_Pls_Bit0
   3.690 +  less_Pls_Bit1 less_Min_Pls less_Min_Min less_Min_Bit less_Bit_Pls
   3.691 +  less_Bit0_Min less_Bit1_Min less_Bit_Bit0 less_Bit1_Bit less_Bit0_Bit1
   3.692 +  less_number_of
   3.693 +
   3.694 +end