src/HOL/Presburger.thy
changeset 23465 8f8835aac299
child 23472 02099ea56555
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Presburger.thy	Thu Jun 21 20:48:47 2007 +0200
     1.3 @@ -0,0 +1,691 @@
     1.4 +(* Title:      HOL/Presburger.thy
     1.5 +   ID:         $Id$
     1.6 +   Author:     Amine Chaieb, TU Muenchen
     1.7 +*)
     1.8 +
     1.9 +theory Presburger
    1.10 +imports Arith_Tools SetInterval
    1.11 +uses
    1.12 +  "Tools/Qelim/cooper_data.ML"
    1.13 +  "Tools/Qelim/generated_cooper.ML"
    1.14 +  ("Tools/Qelim/cooper.ML")
    1.15 +  ("Tools/Qelim/presburger.ML")
    1.16 +begin
    1.17 +
    1.18 +subsection {* Decision Procedure for Presburger Arithmetic *}
    1.19 +
    1.20 +setup CooperData.setup
    1.21 +
    1.22 +subsection{* The @{text "-\<infinity>"} and @{text "+\<infinity>"} Properties *}
    1.23 +
    1.24 +lemma minf:
    1.25 +  "\<lbrakk>\<exists>(z ::'a::linorder).\<forall>x<z. P x = P' x; \<exists>z.\<forall>x<z. Q x = Q' x\<rbrakk> 
    1.26 +     \<Longrightarrow> \<exists>z.\<forall>x<z. (P x \<and> Q x) = (P' x \<and> Q' x)"
    1.27 +  "\<lbrakk>\<exists>(z ::'a::linorder).\<forall>x<z. P x = P' x; \<exists>z.\<forall>x<z. Q x = Q' x\<rbrakk> 
    1.28 +     \<Longrightarrow> \<exists>z.\<forall>x<z. (P x \<or> Q x) = (P' x \<or> Q' x)"
    1.29 +  "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x = t) = False"
    1.30 +  "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x \<noteq> t) = True"
    1.31 +  "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x < t) = True"
    1.32 +  "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x \<le> t) = True"
    1.33 +  "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x > t) = False"
    1.34 +  "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x \<ge> t) = False"
    1.35 +  "\<exists>z.\<forall>(x::'a::{linorder,plus,times})<z. (d dvd x + s) = (d dvd x + s)"
    1.36 +  "\<exists>z.\<forall>(x::'a::{linorder,plus,times})<z. (\<not> d dvd x + s) = (\<not> d dvd x + s)"
    1.37 +  "\<exists>z.\<forall>x<z. F = F"
    1.38 +  by ((erule exE, erule exE,rule_tac x="min z za" in exI,simp)+, (rule_tac x="t" in exI,fastsimp)+) simp_all
    1.39 +
    1.40 +lemma pinf:
    1.41 +  "\<lbrakk>\<exists>(z ::'a::linorder).\<forall>x>z. P x = P' x; \<exists>z.\<forall>x>z. Q x = Q' x\<rbrakk> 
    1.42 +     \<Longrightarrow> \<exists>z.\<forall>x>z. (P x \<and> Q x) = (P' x \<and> Q' x)"
    1.43 +  "\<lbrakk>\<exists>(z ::'a::linorder).\<forall>x>z. P x = P' x; \<exists>z.\<forall>x>z. Q x = Q' x\<rbrakk> 
    1.44 +     \<Longrightarrow> \<exists>z.\<forall>x>z. (P x \<or> Q x) = (P' x \<or> Q' x)"
    1.45 +  "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x = t) = False"
    1.46 +  "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x \<noteq> t) = True"
    1.47 +  "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x < t) = False"
    1.48 +  "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x \<le> t) = False"
    1.49 +  "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x > t) = True"
    1.50 +  "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x \<ge> t) = True"
    1.51 +  "\<exists>z.\<forall>(x::'a::{linorder,plus,times})>z. (d dvd x + s) = (d dvd x + s)"
    1.52 +  "\<exists>z.\<forall>(x::'a::{linorder,plus,times})>z. (\<not> d dvd x + s) = (\<not> d dvd x + s)"
    1.53 +  "\<exists>z.\<forall>x>z. F = F"
    1.54 +  by ((erule exE, erule exE,rule_tac x="max z za" in exI,simp)+,(rule_tac x="t" in exI,fastsimp)+) simp_all
    1.55 +
    1.56 +lemma inf_period:
    1.57 +  "\<lbrakk>\<forall>x k. P x = P (x - k*D); \<forall>x k. Q x = Q (x - k*D)\<rbrakk> 
    1.58 +    \<Longrightarrow> \<forall>x k. (P x \<and> Q x) = (P (x - k*D) \<and> Q (x - k*D))"
    1.59 +  "\<lbrakk>\<forall>x k. P x = P (x - k*D); \<forall>x k. Q x = Q (x - k*D)\<rbrakk> 
    1.60 +    \<Longrightarrow> \<forall>x k. (P x \<or> Q x) = (P (x - k*D) \<or> Q (x - k*D))"
    1.61 +  "(d::'a::{comm_ring}) dvd D \<Longrightarrow> \<forall>x k. (d dvd x + t) = (d dvd (x - k*D) + t)"
    1.62 +  "(d::'a::{comm_ring}) dvd D \<Longrightarrow> \<forall>x k. (\<not>d dvd x + t) = (\<not>d dvd (x - k*D) + t)"
    1.63 +  "\<forall>x k. F = F"
    1.64 +by simp_all
    1.65 +  (clarsimp simp add: dvd_def, rule iffI, clarsimp,rule_tac x = "kb - ka*k" in exI,
    1.66 +    simp add: ring_eq_simps, clarsimp,rule_tac x = "kb + ka*k" in exI,simp add: ring_eq_simps)+
    1.67 +
    1.68 +section{* The A and B sets *}
    1.69 +lemma bset:
    1.70 +  "\<lbrakk>\<forall>x.(\<forall>j \<in> {1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> P x \<longrightarrow> P(x - D) ;
    1.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> 
    1.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))"
    1.73 +  "\<lbrakk>\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> P x \<longrightarrow> P(x - D) ;
    1.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> 
    1.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))"
    1.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))"
    1.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))"
    1.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))"
    1.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))"
    1.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))"
    1.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))"
    1.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))"
    1.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))"
    1.84 +  "\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j) \<longrightarrow> F \<longrightarrow> F"
    1.85 +proof (blast, blast)
    1.86 +  assume dp: "D > 0" and tB: "t - 1\<in> B"
    1.87 +  show "(\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> (x = t) \<longrightarrow> (x - D = t))" 
    1.88 +    apply (rule allI, rule impI,erule ballE[where x="1"],erule ballE[where x="t - 1"])
    1.89 +    using dp tB by simp_all
    1.90 +next
    1.91 +  assume dp: "D > 0" and tB: "t \<in> B"
    1.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))" 
    1.93 +    apply (rule allI, rule impI,erule ballE[where x="D"],erule ballE[where x="t"])
    1.94 +    using dp tB by simp_all
    1.95 +next
    1.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
    1.97 +next
    1.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
    1.99 +next
   1.100 +  assume dp: "D > 0" and tB:"t \<in> B"
   1.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"
   1.102 +    hence "x -t \<le> D" and "1 \<le> x - t" by simp+
   1.103 +      hence "\<exists>j \<in> {1 .. D}. x - t = j" by auto
   1.104 +      hence "\<exists>j \<in> {1 .. D}. x = t + j" by (simp add: ring_eq_simps)
   1.105 +      with nob tB have "False" by simp}
   1.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
   1.107 +next
   1.108 +  assume dp: "D > 0" and tB:"t - 1\<in> B"
   1.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"
   1.110 +    hence "x - (t - 1) \<le> D" and "1 \<le> x - (t - 1)" by simp+
   1.111 +      hence "\<exists>j \<in> {1 .. D}. x - (t - 1) = j" by auto
   1.112 +      hence "\<exists>j \<in> {1 .. D}. x = (t - 1) + 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 \<ge> t) \<longrightarrow> (x - D \<ge> t)" by blast
   1.115 +next
   1.116 +  assume d: "d dvd D"
   1.117 +  {fix x assume H: "d dvd x + t" with d have "d dvd (x - D) + t"
   1.118 +      by (clarsimp simp add: dvd_def,rule_tac x= "ka - k" in exI,simp add: ring_eq_simps)}
   1.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
   1.120 +next
   1.121 +  assume d: "d dvd D"
   1.122 +  {fix x assume H: "\<not>(d dvd x + t)" with d have "\<not>d dvd (x - D) + t"
   1.123 +      by (clarsimp simp add: dvd_def,erule_tac x= "ka + k" in allE,simp add: ring_eq_simps)}
   1.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
   1.125 +qed blast
   1.126 +
   1.127 +lemma aset:
   1.128 +  "\<lbrakk>\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> P x \<longrightarrow> P(x + D) ;
   1.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> 
   1.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))"
   1.131 +  "\<lbrakk>\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> P x \<longrightarrow> P(x + D) ;
   1.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> 
   1.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))"
   1.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))"
   1.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))"
   1.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))"
   1.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))"
   1.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))"
   1.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))"
   1.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))"
   1.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))"
   1.142 +  "\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j) \<longrightarrow> F \<longrightarrow> F"
   1.143 +proof (blast, blast)
   1.144 +  assume dp: "D > 0" and tA: "t + 1 \<in> A"
   1.145 +  show "(\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> (x = t) \<longrightarrow> (x + D = t))" 
   1.146 +    apply (rule allI, rule impI,erule ballE[where x="1"],erule ballE[where x="t + 1"])
   1.147 +    using dp tA by simp_all
   1.148 +next
   1.149 +  assume dp: "D > 0" and tA: "t \<in> A"
   1.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))" 
   1.151 +    apply (rule allI, rule impI,erule ballE[where x="D"],erule ballE[where x="t"])
   1.152 +    using dp tA by simp_all
   1.153 +next
   1.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
   1.155 +next
   1.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
   1.157 +next
   1.158 +  assume dp: "D > 0" and tA:"t \<in> A"
   1.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"
   1.160 +    hence "t - x \<le> D" and "1 \<le> t - x" by simp+
   1.161 +      hence "\<exists>j \<in> {1 .. D}. t - x = j" by auto
   1.162 +      hence "\<exists>j \<in> {1 .. D}. x = t - j" by (auto simp add: ring_eq_simps) 
   1.163 +      with nob tA have "False" by simp}
   1.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
   1.165 +next
   1.166 +  assume dp: "D > 0" and tA:"t + 1\<in> A"
   1.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"
   1.168 +    hence "(t + 1) - x \<le> D" and "1 \<le> (t + 1) - x" by (simp_all add: ring_eq_simps)
   1.169 +      hence "\<exists>j \<in> {1 .. D}. (t + 1) - x = j" by auto
   1.170 +      hence "\<exists>j \<in> {1 .. D}. x = (t + 1) - 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 \<le> t) \<longrightarrow> (x + D \<le> t)" by blast
   1.173 +next
   1.174 +  assume d: "d dvd D"
   1.175 +  {fix x assume H: "d dvd x + t" with d have "d dvd (x + D) + t"
   1.176 +      by (clarsimp simp add: dvd_def,rule_tac x= "ka + k" in exI,simp add: ring_eq_simps)}
   1.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
   1.178 +next
   1.179 +  assume d: "d dvd D"
   1.180 +  {fix x assume H: "\<not>(d dvd x + t)" with d have "\<not>d dvd (x + D) + t"
   1.181 +      by (clarsimp simp add: dvd_def,erule_tac x= "ka - k" in allE,simp add: ring_eq_simps)}
   1.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
   1.183 +qed blast
   1.184 +
   1.185 +subsection{* Cooper's Theorem @{text "-\<infinity>"} and @{text "+\<infinity>"} Version *}
   1.186 +
   1.187 +subsubsection{* First some trivial facts about periodic sets or predicates *}
   1.188 +lemma periodic_finite_ex:
   1.189 +  assumes dpos: "(0::int) < d" and modd: "ALL x k. P x = P(x - k*d)"
   1.190 +  shows "(EX x. P x) = (EX j : {1..d}. P j)"
   1.191 +  (is "?LHS = ?RHS")
   1.192 +proof
   1.193 +  assume ?LHS
   1.194 +  then obtain x where P: "P x" ..
   1.195 +  have "x mod d = x - (x div d)*d" by(simp add:zmod_zdiv_equality mult_ac eq_diff_eq)
   1.196 +  hence Pmod: "P x = P(x mod d)" using modd by simp
   1.197 +  show ?RHS
   1.198 +  proof (cases)
   1.199 +    assume "x mod d = 0"
   1.200 +    hence "P 0" using P Pmod by simp
   1.201 +    moreover have "P 0 = P(0 - (-1)*d)" using modd by blast
   1.202 +    ultimately have "P d" by simp
   1.203 +    moreover have "d : {1..d}" using dpos by(simp add:atLeastAtMost_iff)
   1.204 +    ultimately show ?RHS ..
   1.205 +  next
   1.206 +    assume not0: "x mod d \<noteq> 0"
   1.207 +    have "P(x mod d)" using dpos P Pmod by(simp add:pos_mod_sign pos_mod_bound)
   1.208 +    moreover have "x mod d : {1..d}"
   1.209 +    proof -
   1.210 +      from dpos have "0 \<le> x mod d" by(rule pos_mod_sign)
   1.211 +      moreover from dpos have "x mod d < d" by(rule pos_mod_bound)
   1.212 +      ultimately show ?thesis using not0 by(simp add:atLeastAtMost_iff)
   1.213 +    qed
   1.214 +    ultimately show ?RHS ..
   1.215 +  qed
   1.216 +qed auto
   1.217 +
   1.218 +subsubsection{* The @{text "-\<infinity>"} Version*}
   1.219 +
   1.220 +lemma decr_lemma: "0 < (d::int) \<Longrightarrow> x - (abs(x-z)+1) * d < z"
   1.221 +by(induct rule: int_gr_induct,simp_all add:int_distrib)
   1.222 +
   1.223 +lemma incr_lemma: "0 < (d::int) \<Longrightarrow> z < x + (abs(x-z)+1) * d"
   1.224 +by(induct rule: int_gr_induct, simp_all add:int_distrib)
   1.225 +
   1.226 +theorem int_induct[case_names base step1 step2]:
   1.227 +  assumes 
   1.228 +  base: "P(k::int)" and step1: "\<And>i. \<lbrakk>k \<le> i; P i\<rbrakk> \<Longrightarrow> P(i+1)" and
   1.229 +  step2: "\<And>i. \<lbrakk>k \<ge> i; P i\<rbrakk> \<Longrightarrow> P(i - 1)"
   1.230 +  shows "P i"
   1.231 +proof -
   1.232 +  have "i \<le> k \<or> i\<ge> k" by arith
   1.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
   1.234 +qed
   1.235 +
   1.236 +lemma decr_mult_lemma:
   1.237 +  assumes dpos: "(0::int) < d" and minus: "\<forall>x. P x \<longrightarrow> P(x - d)" and knneg: "0 <= k"
   1.238 +  shows "ALL x. P x \<longrightarrow> P(x - k*d)"
   1.239 +using knneg
   1.240 +proof (induct rule:int_ge_induct)
   1.241 +  case base thus ?case by simp
   1.242 +next
   1.243 +  case (step i)
   1.244 +  {fix x
   1.245 +    have "P x \<longrightarrow> P (x - i * d)" using step.hyps by blast
   1.246 +    also have "\<dots> \<longrightarrow> P(x - (i + 1) * d)" using minus[THEN spec, of "x - i * d"]
   1.247 +      by (simp add:int_distrib OrderedGroup.diff_diff_eq[symmetric])
   1.248 +    ultimately have "P x \<longrightarrow> P(x - (i + 1) * d)" by blast}
   1.249 +  thus ?case ..
   1.250 +qed
   1.251 +
   1.252 +lemma  minusinfinity:
   1.253 +  assumes dpos: "0 < d" and
   1.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)"
   1.255 +  shows "(EX x. P1 x) \<longrightarrow> (EX x. P x)"
   1.256 +proof
   1.257 +  assume eP1: "EX x. P1 x"
   1.258 +  then obtain x where P1: "P1 x" ..
   1.259 +  from ePeqP1 obtain z where P1eqP: "ALL x. x < z \<longrightarrow> (P x = P1 x)" ..
   1.260 +  let ?w = "x - (abs(x-z)+1) * d"
   1.261 +  from dpos have w: "?w < z" by(rule decr_lemma)
   1.262 +  have "P1 x = P1 ?w" using P1eqP1 by blast
   1.263 +  also have "\<dots> = P(?w)" using w P1eqP by blast
   1.264 +  finally have "P ?w" using P1 by blast
   1.265 +  thus "EX x. P x" ..
   1.266 +qed
   1.267 +
   1.268 +lemma cpmi: 
   1.269 +  assumes dp: "0 < D" and p1:"\<exists>z. \<forall> x< z. P x = P' x"
   1.270 +  and nb:"\<forall>x.(\<forall> j\<in> {1..D}. \<forall>(b::int) \<in> B. x \<noteq> b+j) --> P (x) --> P (x - D)"
   1.271 +  and pd: "\<forall> x k. P' x = P' (x-k*D)"
   1.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)))" 
   1.273 +         (is "?L = (?R1 \<or> ?R2)")
   1.274 +proof-
   1.275 + {assume "?R2" hence "?L"  by blast}
   1.276 + moreover
   1.277 + {assume H:"?R1" hence "?L" using minusinfinity[OF dp pd p1] periodic_finite_ex[OF dp pd] by simp}
   1.278 + moreover 
   1.279 + { fix x
   1.280 +   assume P: "P x" and H: "\<not> ?R2"
   1.281 +   {fix y assume "\<not> (\<exists>j\<in>{1..D}. \<exists>b\<in>B. P (b + j))" and P: "P y"
   1.282 +     hence "~(EX (j::int) : {1..D}. EX (b::int) : B. y = b+j)" by auto
   1.283 +     with nb P  have "P (y - D)" by auto }
   1.284 +   hence "ALL x.~(EX (j::int) : {1..D}. EX (b::int) : B. P(b+j)) --> P (x) --> P (x - D)" by blast
   1.285 +   with H P have th: " \<forall>x. P x \<longrightarrow> P (x - D)" by auto
   1.286 +   from p1 obtain z where z: "ALL x. x < z --> (P x = P' x)" by blast
   1.287 +   let ?y = "x - (\<bar>x - z\<bar> + 1)*D"
   1.288 +   have zp: "0 <= (\<bar>x - z\<bar> + 1)" by arith
   1.289 +   from dp have yz: "?y < z" using decr_lemma[OF dp] by simp   
   1.290 +   from z[rule_format, OF yz] decr_mult_lemma[OF dp th zp, rule_format, OF P] have th2: " P' ?y" by auto
   1.291 +   with periodic_finite_ex[OF dp pd]
   1.292 +   have "?R1" by blast}
   1.293 + ultimately show ?thesis by blast
   1.294 +qed
   1.295 +
   1.296 +subsubsection {* The @{text "+\<infinity>"} Version*}
   1.297 +
   1.298 +lemma  plusinfinity:
   1.299 +  assumes dpos: "(0::int) < d" and
   1.300 +    P1eqP1: "\<forall>x k. P' x = P'(x - k*d)" and ePeqP1: "\<exists> z. \<forall> x>z. P x = P' x"
   1.301 +  shows "(\<exists> x. P' x) \<longrightarrow> (\<exists> x. P x)"
   1.302 +proof
   1.303 +  assume eP1: "EX x. P' x"
   1.304 +  then obtain x where P1: "P' x" ..
   1.305 +  from ePeqP1 obtain z where P1eqP: "\<forall>x>z. P x = P' x" ..
   1.306 +  let ?w' = "x + (abs(x-z)+1) * d"
   1.307 +  let ?w = "x - (-(abs(x-z) + 1))*d"
   1.308 +  have ww'[simp]: "?w = ?w'" by (simp add: ring_eq_simps)
   1.309 +  from dpos have w: "?w > z" by(simp only: ww' incr_lemma)
   1.310 +  hence "P' x = P' ?w" using P1eqP1 by blast
   1.311 +  also have "\<dots> = P(?w)" using w P1eqP by blast
   1.312 +  finally have "P ?w" using P1 by blast
   1.313 +  thus "EX x. P x" ..
   1.314 +qed
   1.315 +
   1.316 +lemma incr_mult_lemma:
   1.317 +  assumes dpos: "(0::int) < d" and plus: "ALL x::int. P x \<longrightarrow> P(x + d)" and knneg: "0 <= k"
   1.318 +  shows "ALL x. P x \<longrightarrow> P(x + k*d)"
   1.319 +using knneg
   1.320 +proof (induct rule:int_ge_induct)
   1.321 +  case base thus ?case by simp
   1.322 +next
   1.323 +  case (step i)
   1.324 +  {fix x
   1.325 +    have "P x \<longrightarrow> P (x + i * d)" using step.hyps by blast
   1.326 +    also have "\<dots> \<longrightarrow> P(x + (i + 1) * d)" using plus[THEN spec, of "x + i * d"]
   1.327 +      by (simp add:int_distrib zadd_ac)
   1.328 +    ultimately have "P x \<longrightarrow> P(x + (i + 1) * d)" by blast}
   1.329 +  thus ?case ..
   1.330 +qed
   1.331 +
   1.332 +lemma cppi: 
   1.333 +  assumes dp: "0 < D" and p1:"\<exists>z. \<forall> x> z. P x = P' x"
   1.334 +  and nb:"\<forall>x.(\<forall> j\<in> {1..D}. \<forall>(b::int) \<in> A. x \<noteq> b - j) --> P (x) --> P (x + D)"
   1.335 +  and pd: "\<forall> x k. P' x= P' (x-k*D)"
   1.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)")
   1.337 +proof-
   1.338 + {assume "?R2" hence "?L"  by blast}
   1.339 + moreover
   1.340 + {assume H:"?R1" hence "?L" using plusinfinity[OF dp pd p1] periodic_finite_ex[OF dp pd] by simp}
   1.341 + moreover 
   1.342 + { fix x
   1.343 +   assume P: "P x" and H: "\<not> ?R2"
   1.344 +   {fix y assume "\<not> (\<exists>j\<in>{1..D}. \<exists>b\<in>A. P (b - j))" and P: "P y"
   1.345 +     hence "~(EX (j::int) : {1..D}. EX (b::int) : A. y = b - j)" by auto
   1.346 +     with nb P  have "P (y + D)" by auto }
   1.347 +   hence "ALL x.~(EX (j::int) : {1..D}. EX (b::int) : A. P(b-j)) --> P (x) --> P (x + D)" by blast
   1.348 +   with H P have th: " \<forall>x. P x \<longrightarrow> P (x + D)" by auto
   1.349 +   from p1 obtain z where z: "ALL x. x > z --> (P x = P' x)" by blast
   1.350 +   let ?y = "x + (\<bar>x - z\<bar> + 1)*D"
   1.351 +   have zp: "0 <= (\<bar>x - z\<bar> + 1)" by arith
   1.352 +   from dp have yz: "?y > z" using incr_lemma[OF dp] by simp
   1.353 +   from z[rule_format, OF yz] incr_mult_lemma[OF dp th zp, rule_format, OF P] have th2: " P' ?y" by auto
   1.354 +   with periodic_finite_ex[OF dp pd]
   1.355 +   have "?R1" by blast}
   1.356 + ultimately show ?thesis by blast
   1.357 +qed
   1.358 +
   1.359 +lemma simp_from_to: "{i..j::int} = (if j < i then {} else insert i {i+1..j})"
   1.360 +apply(simp add:atLeastAtMost_def atLeast_def atMost_def)
   1.361 +apply(fastsimp)
   1.362 +done
   1.363 +
   1.364 +theorem unity_coeff_ex: "(\<exists>(x::'a::{semiring_0}). P (l * x)) \<equiv> (\<exists>x. l dvd (x + 0) \<and> P x)"
   1.365 +  apply (rule eq_reflection[symmetric])
   1.366 +  apply (rule iffI)
   1.367 +  defer
   1.368 +  apply (erule exE)
   1.369 +  apply (rule_tac x = "l * x" in exI)
   1.370 +  apply (simp add: dvd_def)
   1.371 +  apply (rule_tac x="x" in exI, simp)
   1.372 +  apply (erule exE)
   1.373 +  apply (erule conjE)
   1.374 +  apply (erule dvdE)
   1.375 +  apply (rule_tac x = k in exI)
   1.376 +  apply simp
   1.377 +  done
   1.378 +
   1.379 +lemma zdvd_mono: assumes not0: "(k::int) \<noteq> 0"
   1.380 +shows "((m::int) dvd t) \<equiv> (k*m dvd k*t)" 
   1.381 +  using not0 by (simp add: dvd_def)
   1.382 +
   1.383 +lemma uminus_dvd_conv: "(d dvd (t::int)) \<equiv> (-d dvd t)" "(d dvd (t::int)) \<equiv> (d dvd -t)"
   1.384 +  by simp_all
   1.385 +text {* \bigskip Theorems for transforming predicates on nat to predicates on @{text int}*}
   1.386 +lemma all_nat: "(\<forall>x::nat. P x) = (\<forall>x::int. 0 <= x \<longrightarrow> P (nat x))"
   1.387 +  by (simp split add: split_nat)
   1.388 +
   1.389 +lemma ex_nat: "(\<exists>x::nat. P x) = (\<exists>x::int. 0 <= x \<and> P (nat x))"
   1.390 +  apply (auto split add: split_nat)
   1.391 +  apply (rule_tac x="int x" in exI, simp)
   1.392 +  apply (rule_tac x = "nat x" in exI,erule_tac x = "nat x" in allE, simp)
   1.393 +  done
   1.394 +
   1.395 +lemma zdiff_int_split: "P (int (x - y)) =
   1.396 +  ((y \<le> x \<longrightarrow> P (int x - int y)) \<and> (x < y \<longrightarrow> P 0))"
   1.397 +  by (case_tac "y \<le> x", simp_all add: zdiff_int)
   1.398 +
   1.399 +lemma number_of1: "(0::int) <= number_of n \<Longrightarrow> (0::int) <= number_of (n BIT b)" by simp
   1.400 +lemma number_of2: "(0::int) <= Numeral0" by simp
   1.401 +lemma Suc_plus1: "Suc n = n + 1" by simp
   1.402 +
   1.403 +text {*
   1.404 +  \medskip Specific instances of congruence rules, to prevent
   1.405 +  simplifier from looping. *}
   1.406 +
   1.407 +theorem imp_le_cong: "(0 <= x \<Longrightarrow> P = P') \<Longrightarrow> (0 <= (x::int) \<longrightarrow> P) = (0 <= x \<longrightarrow> P')" by simp
   1.408 +
   1.409 +theorem conj_le_cong: "(0 <= x \<Longrightarrow> P = P') \<Longrightarrow> (0 <= (x::int) \<and> P) = (0 <= x \<and> P')" 
   1.410 +  by (simp cong: conj_cong)
   1.411 +lemma int_eq_number_of_eq:
   1.412 +  "(((number_of v)::int) = (number_of w)) = iszero ((number_of (v + (uminus w)))::int)"
   1.413 +  by simp
   1.414 +
   1.415 +lemma mod_eq0_dvd_iff[presburger]: "(m::nat) mod n = 0 \<longleftrightarrow> n dvd m"
   1.416 +unfolding dvd_eq_mod_eq_0[symmetric] ..
   1.417 +
   1.418 +lemma zmod_eq0_zdvd_iff[presburger]: "(m::int) mod n = 0 \<longleftrightarrow> n dvd m"
   1.419 +unfolding zdvd_iff_zmod_eq_0[symmetric] ..
   1.420 +declare mod_1[presburger]
   1.421 +declare mod_0[presburger]
   1.422 +declare zmod_1[presburger]
   1.423 +declare zmod_zero[presburger]
   1.424 +declare zmod_self[presburger]
   1.425 +declare mod_self[presburger]
   1.426 +declare DIVISION_BY_ZERO_MOD[presburger]
   1.427 +declare nat_mod_div_trivial[presburger]
   1.428 +declare div_mod_equality2[presburger]
   1.429 +declare div_mod_equality[presburger]
   1.430 +declare mod_div_equality2[presburger]
   1.431 +declare mod_div_equality[presburger]
   1.432 +declare mod_mult_self1[presburger]
   1.433 +declare mod_mult_self2[presburger]
   1.434 +declare zdiv_zmod_equality2[presburger]
   1.435 +declare zdiv_zmod_equality[presburger]
   1.436 +declare mod2_Suc_Suc[presburger]
   1.437 +lemma [presburger]: "(a::int) div 0 = 0" and [presburger]: "a mod 0 = a"
   1.438 +using IntDiv.DIVISION_BY_ZERO by blast+
   1.439 +
   1.440 +use "Tools/Qelim/cooper.ML"
   1.441 +oracle linzqe_oracle ("term") = Coopereif.cooper_oracle
   1.442 +
   1.443 +use "Tools/Qelim/presburger.ML"
   1.444 +
   1.445 +setup {* 
   1.446 +  arith_tactic_add 
   1.447 +    (mk_arith_tactic "presburger" (fn i => fn st =>
   1.448 +       (warning "Trying Presburger arithmetic ...";   
   1.449 +    Presburger.cooper_tac true [] [] ((ProofContext.init o theory_of_thm) st) i st)))
   1.450 +  (* FIXME!!!!!!! get the right context!!*)	
   1.451 +*}
   1.452 +
   1.453 +method_setup presburger = {*
   1.454 +let
   1.455 + fun keyword k = Scan.lift (Args.$$$ k -- Args.colon) >> K ()
   1.456 + fun simple_keyword k = Scan.lift (Args.$$$ k) >> K ()
   1.457 + val addN = "add"
   1.458 + val delN = "del"
   1.459 + val elimN = "elim"
   1.460 + val any_keyword = keyword addN || keyword delN || simple_keyword elimN
   1.461 + val thms = Scan.repeat (Scan.unless any_keyword Attrib.multi_thm) >> flat;
   1.462 +in
   1.463 +  fn src => Method.syntax 
   1.464 +   ((Scan.optional (simple_keyword elimN >> K false) true) -- 
   1.465 +    (Scan.optional (keyword addN |-- thms) []) -- 
   1.466 +    (Scan.optional (keyword delN |-- thms) [])) src 
   1.467 +  #> (fn (((elim, add_ths), del_ths),ctxt) => 
   1.468 +         Method.SIMPLE_METHOD' (Presburger.cooper_tac elim add_ths del_ths ctxt))
   1.469 +end
   1.470 +*} "Cooper's algorithm for Presburger arithmetic"
   1.471 +
   1.472 +lemma [presburger]: "m mod 2 = (1::nat) \<longleftrightarrow> \<not> 2 dvd m " by presburger
   1.473 +lemma [presburger]: "m mod 2 = Suc 0 \<longleftrightarrow> \<not> 2 dvd m " by presburger
   1.474 +lemma [presburger]: "m mod (Suc (Suc 0)) = (1::nat) \<longleftrightarrow> \<not> 2 dvd m " by presburger
   1.475 +lemma [presburger]: "m mod (Suc (Suc 0)) = Suc 0 \<longleftrightarrow> \<not> 2 dvd m " by presburger
   1.476 +lemma [presburger]: "m mod 2 = (1::int) \<longleftrightarrow> \<not> 2 dvd m " by presburger
   1.477 +
   1.478 +
   1.479 +subsection {* Code generator setup *}
   1.480 +
   1.481 +text {*
   1.482 +  Presburger arithmetic is convenient to prove some
   1.483 +  of the following code lemmas on integer numerals:
   1.484 +*}
   1.485 +
   1.486 +lemma eq_Pls_Pls:
   1.487 +  "Numeral.Pls = Numeral.Pls \<longleftrightarrow> True" by presburger
   1.488 +
   1.489 +lemma eq_Pls_Min:
   1.490 +  "Numeral.Pls = Numeral.Min \<longleftrightarrow> False"
   1.491 +  unfolding Pls_def Numeral.Min_def by presburger
   1.492 +
   1.493 +lemma eq_Pls_Bit0:
   1.494 +  "Numeral.Pls = Numeral.Bit k bit.B0 \<longleftrightarrow> Numeral.Pls = k"
   1.495 +  unfolding Pls_def Bit_def bit.cases by presburger
   1.496 +
   1.497 +lemma eq_Pls_Bit1:
   1.498 +  "Numeral.Pls = Numeral.Bit k bit.B1 \<longleftrightarrow> False"
   1.499 +  unfolding Pls_def Bit_def bit.cases by presburger
   1.500 +
   1.501 +lemma eq_Min_Pls:
   1.502 +  "Numeral.Min = Numeral.Pls \<longleftrightarrow> False"
   1.503 +  unfolding Pls_def Numeral.Min_def by presburger
   1.504 +
   1.505 +lemma eq_Min_Min:
   1.506 +  "Numeral.Min = Numeral.Min \<longleftrightarrow> True" by presburger
   1.507 +
   1.508 +lemma eq_Min_Bit0:
   1.509 +  "Numeral.Min = Numeral.Bit k bit.B0 \<longleftrightarrow> False"
   1.510 +  unfolding Numeral.Min_def Bit_def bit.cases by presburger
   1.511 +
   1.512 +lemma eq_Min_Bit1:
   1.513 +  "Numeral.Min = Numeral.Bit k bit.B1 \<longleftrightarrow> Numeral.Min = k"
   1.514 +  unfolding Numeral.Min_def Bit_def bit.cases by presburger
   1.515 +
   1.516 +lemma eq_Bit0_Pls:
   1.517 +  "Numeral.Bit k bit.B0 = Numeral.Pls \<longleftrightarrow> Numeral.Pls = k"
   1.518 +  unfolding Pls_def Bit_def bit.cases by presburger
   1.519 +
   1.520 +lemma eq_Bit1_Pls:
   1.521 +  "Numeral.Bit k bit.B1 = Numeral.Pls \<longleftrightarrow> False"
   1.522 +  unfolding Pls_def Bit_def bit.cases  by presburger
   1.523 +
   1.524 +lemma eq_Bit0_Min:
   1.525 +  "Numeral.Bit k bit.B0 = Numeral.Min \<longleftrightarrow> False"
   1.526 +  unfolding Numeral.Min_def Bit_def bit.cases  by presburger
   1.527 +
   1.528 +lemma eq_Bit1_Min:
   1.529 +  "(Numeral.Bit k bit.B1) = Numeral.Min \<longleftrightarrow> Numeral.Min = k"
   1.530 +  unfolding Numeral.Min_def Bit_def bit.cases  by presburger
   1.531 +
   1.532 +lemma eq_Bit_Bit:
   1.533 +  "Numeral.Bit k1 v1 = Numeral.Bit k2 v2 \<longleftrightarrow>
   1.534 +    v1 = v2 \<and> k1 = k2" 
   1.535 +  unfolding Bit_def
   1.536 +  apply (cases v1)
   1.537 +  apply (cases v2)
   1.538 +  apply auto
   1.539 +  apply presburger
   1.540 +  apply (cases v2)
   1.541 +  apply auto
   1.542 +  apply presburger
   1.543 +  apply (cases v2)
   1.544 +  apply auto
   1.545 +  done
   1.546 +
   1.547 +lemma eq_number_of:
   1.548 +  "(number_of k \<Colon> int) = number_of l \<longleftrightarrow> k = l" 
   1.549 +  unfolding number_of_is_id ..
   1.550 +
   1.551 +
   1.552 +lemma less_eq_Pls_Pls:
   1.553 +  "Numeral.Pls \<le> Numeral.Pls \<longleftrightarrow> True" by rule+
   1.554 +
   1.555 +lemma less_eq_Pls_Min:
   1.556 +  "Numeral.Pls \<le> Numeral.Min \<longleftrightarrow> False"
   1.557 +  unfolding Pls_def Numeral.Min_def by presburger
   1.558 +
   1.559 +lemma less_eq_Pls_Bit:
   1.560 +  "Numeral.Pls \<le> Numeral.Bit k v \<longleftrightarrow> Numeral.Pls \<le> k"
   1.561 +  unfolding Pls_def Bit_def by (cases v) auto
   1.562 +
   1.563 +lemma less_eq_Min_Pls:
   1.564 +  "Numeral.Min \<le> Numeral.Pls \<longleftrightarrow> True"
   1.565 +  unfolding Pls_def Numeral.Min_def by presburger
   1.566 +
   1.567 +lemma less_eq_Min_Min:
   1.568 +  "Numeral.Min \<le> Numeral.Min \<longleftrightarrow> True" by rule+
   1.569 +
   1.570 +lemma less_eq_Min_Bit0:
   1.571 +  "Numeral.Min \<le> Numeral.Bit k bit.B0 \<longleftrightarrow> Numeral.Min < k"
   1.572 +  unfolding Numeral.Min_def Bit_def by auto
   1.573 +
   1.574 +lemma less_eq_Min_Bit1:
   1.575 +  "Numeral.Min \<le> Numeral.Bit k bit.B1 \<longleftrightarrow> Numeral.Min \<le> k"
   1.576 +  unfolding Numeral.Min_def Bit_def by auto
   1.577 +
   1.578 +lemma less_eq_Bit0_Pls:
   1.579 +  "Numeral.Bit k bit.B0 \<le> Numeral.Pls \<longleftrightarrow> k \<le> Numeral.Pls"
   1.580 +  unfolding Pls_def Bit_def by simp
   1.581 +
   1.582 +lemma less_eq_Bit1_Pls:
   1.583 +  "Numeral.Bit k bit.B1 \<le> Numeral.Pls \<longleftrightarrow> k < Numeral.Pls"
   1.584 +  unfolding Pls_def Bit_def by auto
   1.585 +
   1.586 +lemma less_eq_Bit_Min:
   1.587 +  "Numeral.Bit k v \<le> Numeral.Min \<longleftrightarrow> k \<le> Numeral.Min"
   1.588 +  unfolding Numeral.Min_def Bit_def by (cases v) auto
   1.589 +
   1.590 +lemma less_eq_Bit0_Bit:
   1.591 +  "Numeral.Bit k1 bit.B0 \<le> Numeral.Bit k2 v \<longleftrightarrow> k1 \<le> k2"
   1.592 +  unfolding Bit_def bit.cases by (cases v) auto
   1.593 +
   1.594 +lemma less_eq_Bit_Bit1:
   1.595 +  "Numeral.Bit k1 v \<le> Numeral.Bit k2 bit.B1 \<longleftrightarrow> k1 \<le> k2"
   1.596 +  unfolding Bit_def bit.cases by (cases v) auto
   1.597 +
   1.598 +lemma less_eq_Bit1_Bit0:
   1.599 +  "Numeral.Bit k1 bit.B1 \<le> Numeral.Bit k2 bit.B0 \<longleftrightarrow> k1 < k2"
   1.600 +  unfolding Bit_def by (auto split: bit.split)
   1.601 +
   1.602 +lemma less_eq_number_of:
   1.603 +  "(number_of k \<Colon> int) \<le> number_of l \<longleftrightarrow> k \<le> l"
   1.604 +  unfolding number_of_is_id ..
   1.605 +
   1.606 +
   1.607 +lemma less_Pls_Pls:
   1.608 +  "Numeral.Pls < Numeral.Pls \<longleftrightarrow> False" by simp 
   1.609 +
   1.610 +lemma less_Pls_Min:
   1.611 +  "Numeral.Pls < Numeral.Min \<longleftrightarrow> False"
   1.612 +  unfolding Pls_def Numeral.Min_def  by presburger 
   1.613 +
   1.614 +lemma less_Pls_Bit0:
   1.615 +  "Numeral.Pls < Numeral.Bit k bit.B0 \<longleftrightarrow> Numeral.Pls < k"
   1.616 +  unfolding Pls_def Bit_def by auto
   1.617 +
   1.618 +lemma less_Pls_Bit1:
   1.619 +  "Numeral.Pls < Numeral.Bit k bit.B1 \<longleftrightarrow> Numeral.Pls \<le> k"
   1.620 +  unfolding Pls_def Bit_def by auto
   1.621 +
   1.622 +lemma less_Min_Pls:
   1.623 +  "Numeral.Min < Numeral.Pls \<longleftrightarrow> True"
   1.624 +  unfolding Pls_def Numeral.Min_def by presburger 
   1.625 +
   1.626 +lemma less_Min_Min:
   1.627 +  "Numeral.Min < Numeral.Min \<longleftrightarrow> False"  by simp
   1.628 +
   1.629 +lemma less_Min_Bit:
   1.630 +  "Numeral.Min < Numeral.Bit k v \<longleftrightarrow> Numeral.Min < k"
   1.631 +  unfolding Numeral.Min_def Bit_def by (auto split: bit.split)
   1.632 +
   1.633 +lemma less_Bit_Pls:
   1.634 +  "Numeral.Bit k v < Numeral.Pls \<longleftrightarrow> k < Numeral.Pls"
   1.635 +  unfolding Pls_def Bit_def by (auto split: bit.split)
   1.636 +
   1.637 +lemma less_Bit0_Min:
   1.638 +  "Numeral.Bit k bit.B0 < Numeral.Min \<longleftrightarrow> k \<le> Numeral.Min"
   1.639 +  unfolding Numeral.Min_def Bit_def by auto
   1.640 +
   1.641 +lemma less_Bit1_Min:
   1.642 +  "Numeral.Bit k bit.B1 < Numeral.Min \<longleftrightarrow> k < Numeral.Min"
   1.643 +  unfolding Numeral.Min_def Bit_def by auto
   1.644 +
   1.645 +lemma less_Bit_Bit0:
   1.646 +  "Numeral.Bit k1 v < Numeral.Bit k2 bit.B0 \<longleftrightarrow> k1 < k2"
   1.647 +  unfolding Bit_def by (auto split: bit.split)
   1.648 +
   1.649 +lemma less_Bit1_Bit:
   1.650 +  "Numeral.Bit k1 bit.B1 < Numeral.Bit k2 v \<longleftrightarrow> k1 < k2"
   1.651 +  unfolding Bit_def by (auto split: bit.split)
   1.652 +
   1.653 +lemma less_Bit0_Bit1:
   1.654 +  "Numeral.Bit k1 bit.B0 < Numeral.Bit k2 bit.B1 \<longleftrightarrow> k1 \<le> k2"
   1.655 +  unfolding Bit_def bit.cases  by arith
   1.656 +
   1.657 +lemma less_number_of:
   1.658 +  "(number_of k \<Colon> int) < number_of l \<longleftrightarrow> k < l"
   1.659 +  unfolding number_of_is_id ..
   1.660 +
   1.661 +lemmas pred_succ_numeral_code [code func] =
   1.662 +  arith_simps(5-12)
   1.663 +
   1.664 +lemmas plus_numeral_code [code func] =
   1.665 +  arith_simps(13-17)
   1.666 +  arith_simps(26-27)
   1.667 +  arith_extra_simps(1) [where 'a = int]
   1.668 +
   1.669 +lemmas minus_numeral_code [code func] =
   1.670 +  arith_simps(18-21)
   1.671 +  arith_extra_simps(2) [where 'a = int]
   1.672 +  arith_extra_simps(5) [where 'a = int]
   1.673 +
   1.674 +lemmas times_numeral_code [code func] =
   1.675 +  arith_simps(22-25)
   1.676 +  arith_extra_simps(4) [where 'a = int]
   1.677 +
   1.678 +lemmas eq_numeral_code [code func] =
   1.679 +  eq_Pls_Pls eq_Pls_Min eq_Pls_Bit0 eq_Pls_Bit1
   1.680 +  eq_Min_Pls eq_Min_Min eq_Min_Bit0 eq_Min_Bit1
   1.681 +  eq_Bit0_Pls eq_Bit1_Pls eq_Bit0_Min eq_Bit1_Min eq_Bit_Bit
   1.682 +  eq_number_of
   1.683 +
   1.684 +lemmas less_eq_numeral_code [code func] = less_eq_Pls_Pls less_eq_Pls_Min less_eq_Pls_Bit
   1.685 +  less_eq_Min_Pls less_eq_Min_Min less_eq_Min_Bit0 less_eq_Min_Bit1
   1.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
   1.687 +  less_eq_number_of
   1.688 +
   1.689 +lemmas less_numeral_code [code func] = less_Pls_Pls less_Pls_Min less_Pls_Bit0
   1.690 +  less_Pls_Bit1 less_Min_Pls less_Min_Min less_Min_Bit less_Bit_Pls
   1.691 +  less_Bit0_Min less_Bit1_Min less_Bit_Bit0 less_Bit1_Bit less_Bit0_Bit1
   1.692 +  less_number_of
   1.693 +
   1.694 +end