
wenzelm@23148: (*  Title:      HOL/Presburger.thy
berghofe@13876:     ID:         $Id$
chaieb@23314:     Author:     Amine Chaieb, TU Muenchen
berghofe@13876: *)
berghofe@13876: 
nipkow@15131: theory Presburger
wenzelm@23164: imports NatSimprocs SetInterval
chaieb@23314:   uses "Tools/Presburger/cooper_data" "Tools/Presburger/qelim" 
chaieb@23314:        "Tools/Presburger/generated_cooper.ML"
chaieb@23314:        ("Tools/Presburger/cooper.ML") ("Tools/Presburger/presburger.ML") 
chaieb@23314:        
nipkow@15131: begin
chaieb@23314: setup {* Cooper_Data.setup*}
berghofe@13876: 
chaieb@23314: section{* The @{text "-\<infinity>"} and @{text "+\<infinity>"} Properties *}
berghofe@13876: 
chaieb@23314: lemma minf:
chaieb@23314:   "\<lbrakk>\<exists>(z ::'a::linorder).\<forall>x<z. P x = P' x; \<exists>z.\<forall>x<z. Q x = Q' x\<rbrakk> 
chaieb@23314:      \<Longrightarrow> \<exists>z.\<forall>x<z. (P x \<and> Q x) = (P' x \<and> Q' x)"
chaieb@23314:   "\<lbrakk>\<exists>(z ::'a::linorder).\<forall>x<z. P x = P' x; \<exists>z.\<forall>x<z. Q x = Q' x\<rbrakk> 
chaieb@23314:      \<Longrightarrow> \<exists>z.\<forall>x<z. (P x \<or> Q x) = (P' x \<or> Q' x)"
chaieb@23314:   "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x = t) = False"
chaieb@23314:   "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x \<noteq> t) = True"
chaieb@23314:   "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x < t) = True"
chaieb@23314:   "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x \<le> t) = True"
chaieb@23314:   "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x > t) = False"
chaieb@23314:   "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x \<ge> t) = False"
chaieb@23314:   "\<exists>z.\<forall>(x::'a::{linorder,plus,times})<z. (d dvd x + s) = (d dvd x + s)"
chaieb@23314:   "\<exists>z.\<forall>(x::'a::{linorder,plus,times})<z. (\<not> d dvd x + s) = (\<not> d dvd x + s)"
chaieb@23314:   "\<exists>z.\<forall>x<z. F = F"
chaieb@23314:   by ((erule exE, erule exE,rule_tac x="min z za" in exI,simp)+, (rule_tac x="t" in exI,fastsimp)+) simp_all
berghofe@13876: 
chaieb@23314: lemma pinf:
chaieb@23314:   "\<lbrakk>\<exists>(z ::'a::linorder).\<forall>x>z. P x = P' x; \<exists>z.\<forall>x>z. Q x = Q' x\<rbrakk> 
chaieb@23314:      \<Longrightarrow> \<exists>z.\<forall>x>z. (P x \<and> Q x) = (P' x \<and> Q' x)"
chaieb@23314:   "\<lbrakk>\<exists>(z ::'a::linorder).\<forall>x>z. P x = P' x; \<exists>z.\<forall>x>z. Q x = Q' x\<rbrakk> 
chaieb@23314:      \<Longrightarrow> \<exists>z.\<forall>x>z. (P x \<or> Q x) = (P' x \<or> Q' x)"
chaieb@23314:   "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x = t) = False"
chaieb@23314:   "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x \<noteq> t) = True"
chaieb@23314:   "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x < t) = False"
chaieb@23314:   "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x \<le> t) = False"
chaieb@23314:   "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x > t) = True"
chaieb@23314:   "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x \<ge> t) = True"
chaieb@23314:   "\<exists>z.\<forall>(x::'a::{linorder,plus,times})>z. (d dvd x + s) = (d dvd x + s)"
chaieb@23314:   "\<exists>z.\<forall>(x::'a::{linorder,plus,times})>z. (\<not> d dvd x + s) = (\<not> d dvd x + s)"
chaieb@23314:   "\<exists>z.\<forall>x>z. F = F"
chaieb@23314:   by ((erule exE, erule exE,rule_tac x="max z za" in exI,simp)+,(rule_tac x="t" in exI,fastsimp)+) simp_all
berghofe@13876: 
chaieb@23314: lemma inf_period:
chaieb@23314:   "\<lbrakk>\<forall>x k. P x = P (x - k*D); \<forall>x k. Q x = Q (x - k*D)\<rbrakk> 
chaieb@23314:     \<Longrightarrow> \<forall>x k. (P x \<and> Q x) = (P (x - k*D) \<and> Q (x - k*D))"
chaieb@23314:   "\<lbrakk>\<forall>x k. P x = P (x - k*D); \<forall>x k. Q x = Q (x - k*D)\<rbrakk> 
chaieb@23314:     \<Longrightarrow> \<forall>x k. (P x \<or> Q x) = (P (x - k*D) \<or> Q (x - k*D))"
chaieb@23314:   "(d::'a::{comm_ring}) dvd D \<Longrightarrow> \<forall>x k. (d dvd x + t) = (d dvd (x - k*D) + t)"
chaieb@23314:   "(d::'a::{comm_ring}) dvd D \<Longrightarrow> \<forall>x k. (\<not>d dvd x + t) = (\<not>d dvd (x - k*D) + t)"
chaieb@23314:   "\<forall>x k. F = F"
chaieb@23314: by simp_all
chaieb@23314:   (clarsimp simp add: dvd_def, rule iffI, clarsimp,rule_tac x = "kb - ka*k" in exI,
chaieb@23314:     simp add: ring_eq_simps, clarsimp,rule_tac x = "kb + ka*k" in exI,simp add: ring_eq_simps)+
berghofe@13876: 
chaieb@23314: section{* The A and B sets *}
chaieb@23314: lemma bset:
chaieb@23314:   "\<lbrakk>\<forall>x.(\<forall>j \<in> {1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> P x \<longrightarrow> P(x - D) ;
chaieb@23314:      \<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> Q x \<longrightarrow> Q(x - D)\<rbrakk> \<Longrightarrow> 
chaieb@23314:   \<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))"
chaieb@23314:   "\<lbrakk>\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> P x \<longrightarrow> P(x - D) ;
chaieb@23314:      \<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> Q x \<longrightarrow> Q(x - D)\<rbrakk> \<Longrightarrow> 
chaieb@23314:   \<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))"
chaieb@23314:   "\<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))"
chaieb@23314:   "\<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))"
chaieb@23314:   "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))"
chaieb@23314:   "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))"
chaieb@23314:   "\<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))"
chaieb@23314:   "\<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))"
chaieb@23314:   "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))"
chaieb@23314:   "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))"
chaieb@23314:   "\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j) \<longrightarrow> F \<longrightarrow> F"
chaieb@23314: proof (blast, blast)
chaieb@23314:   assume dp: "D > 0" and tB: "t - 1\<in> B"
chaieb@23314:   show "(\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> (x = t) \<longrightarrow> (x - D = t))" 
chaieb@23314:     apply (rule allI, rule impI,erule ballE[where x="1"],erule ballE[where x="t - 1"])
chaieb@23314:     using dp tB by simp_all
chaieb@23314: next
chaieb@23314:   assume dp: "D > 0" and tB: "t \<in> B"
chaieb@23314:   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))" 
chaieb@23314:     apply (rule allI, rule impI,erule ballE[where x="D"],erule ballE[where x="t"])
chaieb@23314:     using dp tB by simp_all
chaieb@23314: next
chaieb@23314:   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
chaieb@23314: next
chaieb@23314:   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
chaieb@23314: next
chaieb@23314:   assume dp: "D > 0" and tB:"t \<in> B"
chaieb@23314:   {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"
chaieb@23314:     hence "x -t \<le> D" and "1 \<le> x - t" by simp+
chaieb@23314:       hence "\<exists>j \<in> {1 .. D}. x - t = j" by auto
chaieb@23314:       hence "\<exists>j \<in> {1 .. D}. x = t + j" by (simp add: ring_eq_simps)
chaieb@23314:       with nob tB have "False" by simp}
chaieb@23314:   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
chaieb@23314: next
chaieb@23314:   assume dp: "D > 0" and tB:"t - 1\<in> B"
chaieb@23314:   {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"
chaieb@23314:     hence "x - (t - 1) \<le> D" and "1 \<le> x - (t - 1)" by simp+
chaieb@23314:       hence "\<exists>j \<in> {1 .. D}. x - (t - 1) = j" by auto
chaieb@23314:       hence "\<exists>j \<in> {1 .. D}. x = (t - 1) + j" by (simp add: ring_eq_simps)
chaieb@23314:       with nob tB have "False" by simp}
chaieb@23314:   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
chaieb@23314: next
chaieb@23314:   assume d: "d dvd D"
chaieb@23314:   {fix x assume H: "d dvd x + t" with d have "d dvd (x - D) + t"
chaieb@23314:       by (clarsimp simp add: dvd_def,rule_tac x= "ka - k" in exI,simp add: ring_eq_simps)}
chaieb@23314:   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
chaieb@23314: next
chaieb@23314:   assume d: "d dvd D"
chaieb@23314:   {fix x assume H: "\<not>(d dvd x + t)" with d have "\<not>d dvd (x - D) + t"
chaieb@23314:       by (clarsimp simp add: dvd_def,erule_tac x= "ka + k" in allE,simp add: ring_eq_simps)}
chaieb@23314:   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
chaieb@23314: qed blast
berghofe@13876: 
chaieb@23314: lemma aset:
chaieb@23314:   "\<lbrakk>\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> P x \<longrightarrow> P(x + D) ;
chaieb@23314:      \<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> Q x \<longrightarrow> Q(x + D)\<rbrakk> \<Longrightarrow> 
chaieb@23314:   \<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))"
chaieb@23314:   "\<lbrakk>\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> P x \<longrightarrow> P(x + D) ;
chaieb@23314:      \<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> Q x \<longrightarrow> Q(x + D)\<rbrakk> \<Longrightarrow> 
chaieb@23314:   \<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))"
chaieb@23314:   "\<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))"
chaieb@23314:   "\<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))"
chaieb@23314:   "\<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))"
chaieb@23314:   "\<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))"
chaieb@23314:   "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))"
chaieb@23314:   "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))"
chaieb@23314:   "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))"
chaieb@23314:   "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))"
chaieb@23314:   "\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j) \<longrightarrow> F \<longrightarrow> F"
chaieb@23314: proof (blast, blast)
chaieb@23314:   assume dp: "D > 0" and tA: "t + 1 \<in> A"
chaieb@23314:   show "(\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> (x = t) \<longrightarrow> (x + D = t))" 
chaieb@23314:     apply (rule allI, rule impI,erule ballE[where x="1"],erule ballE[where x="t + 1"])
chaieb@23314:     using dp tA by simp_all
chaieb@23314: next
chaieb@23314:   assume dp: "D > 0" and tA: "t \<in> A"
chaieb@23314:   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))" 
chaieb@23314:     apply (rule allI, rule impI,erule ballE[where x="D"],erule ballE[where x="t"])
chaieb@23314:     using dp tA by simp_all
chaieb@23314: next
chaieb@23314:   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
chaieb@23314: next
chaieb@23314:   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
chaieb@23314: next
chaieb@23314:   assume dp: "D > 0" and tA:"t \<in> A"
chaieb@23314:   {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"
chaieb@23314:     hence "t - x \<le> D" and "1 \<le> t - x" by simp+
chaieb@23314:       hence "\<exists>j \<in> {1 .. D}. t - x = j" by auto
chaieb@23314:       hence "\<exists>j \<in> {1 .. D}. x = t - j" by (auto simp add: ring_eq_simps) 
chaieb@23314:       with nob tA have "False" by simp}
chaieb@23314:   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
chaieb@23314: next
chaieb@23314:   assume dp: "D > 0" and tA:"t + 1\<in> A"
chaieb@23314:   {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"
chaieb@23314:     hence "(t + 1) - x \<le> D" and "1 \<le> (t + 1) - x" by (simp_all add: ring_eq_simps)
chaieb@23314:       hence "\<exists>j \<in> {1 .. D}. (t + 1) - x = j" by auto
chaieb@23314:       hence "\<exists>j \<in> {1 .. D}. x = (t + 1) - j" by (auto simp add: ring_eq_simps)
chaieb@23314:       with nob tA have "False" by simp}
chaieb@23314:   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
chaieb@23314: next
chaieb@23314:   assume d: "d dvd D"
chaieb@23314:   {fix x assume H: "d dvd x + t" with d have "d dvd (x + D) + t"
chaieb@23314:       by (clarsimp simp add: dvd_def,rule_tac x= "ka + k" in exI,simp add: ring_eq_simps)}
chaieb@23314:   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
chaieb@23314: next
chaieb@23314:   assume d: "d dvd D"
chaieb@23314:   {fix x assume H: "\<not>(d dvd x + t)" with d have "\<not>d dvd (x + D) + t"
chaieb@23314:       by (clarsimp simp add: dvd_def,erule_tac x= "ka - k" in allE,simp add: ring_eq_simps)}
chaieb@23314:   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
chaieb@23314: qed blast
wenzelm@14577: 
chaieb@23314: section{* Cooper's Theorem @{text "-\<infinity>"} and @{text "+\<infinity>"} Version *}
berghofe@13876: 
chaieb@23314: subsection{* First some trivial facts about periodic sets or predicates *}
chaieb@23314: lemma periodic_finite_ex:
berghofe@13876:   assumes dpos: "(0::int) < d" and modd: "ALL x k. P x = P(x - k*d)"
berghofe@13876:   shows "(EX x. P x) = (EX j : {1..d}. P j)"
berghofe@13876:   (is "?LHS = ?RHS")
berghofe@13876: proof
berghofe@13876:   assume ?LHS
berghofe@13876:   then obtain x where P: "P x" ..
chaieb@23314:   have "x mod d = x - (x div d)*d" by(simp add:zmod_zdiv_equality mult_ac eq_diff_eq)
berghofe@13876:   hence Pmod: "P x = P(x mod d)" using modd by simp
berghofe@13876:   show ?RHS
berghofe@13876:   proof (cases)
berghofe@13876:     assume "x mod d = 0"
berghofe@13876:     hence "P 0" using P Pmod by simp
berghofe@13876:     moreover have "P 0 = P(0 - (-1)*d)" using modd by blast
berghofe@13876:     ultimately have "P d" by simp
berghofe@13876:     moreover have "d : {1..d}" using dpos by(simp add:atLeastAtMost_iff)
berghofe@13876:     ultimately show ?RHS ..
berghofe@13876:   next
berghofe@13876:     assume not0: "x mod d \<noteq> 0"
berghofe@13876:     have "P(x mod d)" using dpos P Pmod by(simp add:pos_mod_sign pos_mod_bound)
berghofe@13876:     moreover have "x mod d : {1..d}"
berghofe@13876:     proof -
wenzelm@23389:       from dpos have "0 \<le> x mod d" by(rule pos_mod_sign)
wenzelm@23389:       moreover from dpos have "x mod d < d" by(rule pos_mod_bound)
berghofe@13876:       ultimately show ?thesis using not0 by(simp add:atLeastAtMost_iff)
berghofe@13876:     qed
berghofe@13876:     ultimately show ?RHS ..
berghofe@13876:   qed
chaieb@23314: qed auto
berghofe@13876: 
chaieb@23314: subsection{* The @{text "-\<infinity>"} Version*}
chaieb@23314: 
chaieb@23314: lemma decr_lemma: "0 < (d::int) \<Longrightarrow> x - (abs(x-z)+1) * d < z"
chaieb@23314: by(induct rule: int_gr_induct,simp_all add:int_distrib)
wenzelm@14577: 
chaieb@23314: lemma incr_lemma: "0 < (d::int) \<Longrightarrow> z < x + (abs(x-z)+1) * d"
chaieb@23314: by(induct rule: int_gr_induct, simp_all add:int_distrib)
chaieb@23314: 
chaieb@23314: theorem int_induct[case_names base step1 step2]:
chaieb@23314:   assumes 
chaieb@23314:   base: "P(k::int)" and step1: "\<And>i. \<lbrakk>k \<le> i; P i\<rbrakk> \<Longrightarrow> P(i+1)" and
chaieb@23314:   step2: "\<And>i. \<lbrakk>k \<ge> i; P i\<rbrakk> \<Longrightarrow> P(i - 1)"
chaieb@23314:   shows "P i"
chaieb@23314: proof -
chaieb@23314:   have "i \<le> k \<or> i\<ge> k" by arith
chaieb@23314:   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
berghofe@13876: qed
berghofe@13876: 
berghofe@13876: lemma decr_mult_lemma:
chaieb@23314:   assumes dpos: "(0::int) < d" and minus: "\<forall>x. P x \<longrightarrow> P(x - d)" and knneg: "0 <= k"
berghofe@13876:   shows "ALL x. P x \<longrightarrow> P(x - k*d)"
berghofe@13876: using knneg
berghofe@13876: proof (induct rule:int_ge_induct)
berghofe@13876:   case base thus ?case by simp
berghofe@13876: next
berghofe@13876:   case (step i)
chaieb@23314:   {fix x
berghofe@13876:     have "P x \<longrightarrow> P (x - i * d)" using step.hyps by blast
chaieb@23314:     also have "\<dots> \<longrightarrow> P(x - (i + 1) * d)" using minus[THEN spec, of "x - i * d"]
obua@14738:       by (simp add:int_distrib OrderedGroup.diff_diff_eq[symmetric])
chaieb@23314:     ultimately have "P x \<longrightarrow> P(x - (i + 1) * d)" by blast}
chaieb@23314:   thus ?case ..
chaieb@23314: qed
chaieb@23314: 
chaieb@23314: lemma  minusinfinity:
wenzelm@23389:   assumes dpos: "0 < d" and
chaieb@23314:     P1eqP1: "ALL x k. P1 x = P1(x - k*d)" and ePeqP1: "EX z::int. ALL x. x < z \<longrightarrow> (P x = P1 x)"
chaieb@23314:   shows "(EX x. P1 x) \<longrightarrow> (EX x. P x)"
chaieb@23314: proof
chaieb@23314:   assume eP1: "EX x. P1 x"
chaieb@23314:   then obtain x where P1: "P1 x" ..
chaieb@23314:   from ePeqP1 obtain z where P1eqP: "ALL x. x < z \<longrightarrow> (P x = P1 x)" ..
chaieb@23314:   let ?w = "x - (abs(x-z)+1) * d"
wenzelm@23389:   from dpos have w: "?w < z" by(rule decr_lemma)
chaieb@23314:   have "P1 x = P1 ?w" using P1eqP1 by blast
chaieb@23314:   also have "\<dots> = P(?w)" using w P1eqP by blast
chaieb@23314:   finally have "P ?w" using P1 by blast
chaieb@23314:   thus "EX x. P x" ..
chaieb@23314: qed
chaieb@23314: 
chaieb@23314: lemma cpmi: 
chaieb@23314:   assumes dp: "0 < D" and p1:"\<exists>z. \<forall> x< z. P x = P' x"
chaieb@23314:   and nb:"\<forall>x.(\<forall> j\<in> {1..D}. \<forall>(b::int) \<in> B. x \<noteq> b+j) --> P (x) --> P (x - D)"
chaieb@23314:   and pd: "\<forall> x k. P' x = P' (x-k*D)"
chaieb@23314:   shows "(\<exists>x. P x) = ((\<exists> j\<in> {1..D} . P' j) | (\<exists> j \<in> {1..D}.\<exists> b\<in> B. P (b+j)))" 
chaieb@23314:          (is "?L = (?R1 \<or> ?R2)")
chaieb@23314: proof-
chaieb@23314:  {assume "?R2" hence "?L"  by blast}
chaieb@23314:  moreover
chaieb@23314:  {assume H:"?R1" hence "?L" using minusinfinity[OF dp pd p1] periodic_finite_ex[OF dp pd] by simp}
chaieb@23314:  moreover 
chaieb@23314:  { fix x
chaieb@23314:    assume P: "P x" and H: "\<not> ?R2"
chaieb@23314:    {fix y assume "\<not> (\<exists>j\<in>{1..D}. \<exists>b\<in>B. P (b + j))" and P: "P y"
chaieb@23314:      hence "~(EX (j::int) : {1..D}. EX (b::int) : B. y = b+j)" by auto
chaieb@23314:      with nb P  have "P (y - D)" by auto }
chaieb@23314:    hence "ALL x.~(EX (j::int) : {1..D}. EX (b::int) : B. P(b+j)) --> P (x) --> P (x - D)" by blast
chaieb@23314:    with H P have th: " \<forall>x. P x \<longrightarrow> P (x - D)" by auto
chaieb@23314:    from p1 obtain z where z: "ALL x. x < z --> (P x = P' x)" by blast
chaieb@23314:    let ?y = "x - (\<bar>x - z\<bar> + 1)*D"
chaieb@23314:    have zp: "0 <= (\<bar>x - z\<bar> + 1)" by arith
chaieb@23314:    from dp have yz: "?y < z" using decr_lemma[OF dp] by simp   
chaieb@23314:    from z[rule_format, OF yz] decr_mult_lemma[OF dp th zp, rule_format, OF P] have th2: " P' ?y" by auto
chaieb@23314:    with periodic_finite_ex[OF dp pd]
chaieb@23314:    have "?R1" by blast}
chaieb@23314:  ultimately show ?thesis by blast
chaieb@23314: qed
chaieb@23314: 
chaieb@23314: subsection {* The @{text "+\<infinity>"} Version*}
chaieb@23314: 
chaieb@23314: lemma  plusinfinity:
wenzelm@23389:   assumes dpos: "(0::int) < d" and
chaieb@23314:     P1eqP1: "\<forall>x k. P' x = P'(x - k*d)" and ePeqP1: "\<exists> z. \<forall> x>z. P x = P' x"
chaieb@23314:   shows "(\<exists> x. P' x) \<longrightarrow> (\<exists> x. P x)"
chaieb@23314: proof
chaieb@23314:   assume eP1: "EX x. P' x"
chaieb@23314:   then obtain x where P1: "P' x" ..
chaieb@23314:   from ePeqP1 obtain z where P1eqP: "\<forall>x>z. P x = P' x" ..
chaieb@23314:   let ?w' = "x + (abs(x-z)+1) * d"
chaieb@23314:   let ?w = "x - (-(abs(x-z) + 1))*d"
chaieb@23314:   have ww'[simp]: "?w = ?w'" by (simp add: ring_eq_simps)
wenzelm@23389:   from dpos have w: "?w > z" by(simp only: ww' incr_lemma)
chaieb@23314:   hence "P' x = P' ?w" using P1eqP1 by blast
chaieb@23314:   also have "\<dots> = P(?w)" using w P1eqP by blast
chaieb@23314:   finally have "P ?w" using P1 by blast
chaieb@23314:   thus "EX x. P x" ..
berghofe@13876: qed
berghofe@13876: 
berghofe@13876: lemma incr_mult_lemma:
chaieb@23314:   assumes dpos: "(0::int) < d" and plus: "ALL x::int. P x \<longrightarrow> P(x + d)" and knneg: "0 <= k"
berghofe@13876:   shows "ALL x. P x \<longrightarrow> P(x + k*d)"
berghofe@13876: using knneg
berghofe@13876: proof (induct rule:int_ge_induct)
berghofe@13876:   case base thus ?case by simp
berghofe@13876: next
berghofe@13876:   case (step i)
chaieb@23314:   {fix x
berghofe@13876:     have "P x \<longrightarrow> P (x + i * d)" using step.hyps by blast
chaieb@23314:     also have "\<dots> \<longrightarrow> P(x + (i + 1) * d)" using plus[THEN spec, of "x + i * d"]
berghofe@13876:       by (simp add:int_distrib zadd_ac)
chaieb@23314:     ultimately have "P x \<longrightarrow> P(x + (i + 1) * d)" by blast}
chaieb@23314:   thus ?case ..
berghofe@13876: qed
berghofe@13876: 
chaieb@23314: lemma cppi: 
chaieb@23314:   assumes dp: "0 < D" and p1:"\<exists>z. \<forall> x> z. P x = P' x"
chaieb@23314:   and nb:"\<forall>x.(\<forall> j\<in> {1..D}. \<forall>(b::int) \<in> A. x \<noteq> b - j) --> P (x) --> P (x + D)"
chaieb@23314:   and pd: "\<forall> x k. P' x= P' (x-k*D)"
chaieb@23314:   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)")
chaieb@23314: proof-
chaieb@23314:  {assume "?R2" hence "?L"  by blast}
chaieb@23314:  moreover
chaieb@23314:  {assume H:"?R1" hence "?L" using plusinfinity[OF dp pd p1] periodic_finite_ex[OF dp pd] by simp}
chaieb@23314:  moreover 
chaieb@23314:  { fix x
chaieb@23314:    assume P: "P x" and H: "\<not> ?R2"
chaieb@23314:    {fix y assume "\<not> (\<exists>j\<in>{1..D}. \<exists>b\<in>A. P (b - j))" and P: "P y"
chaieb@23314:      hence "~(EX (j::int) : {1..D}. EX (b::int) : A. y = b - j)" by auto
chaieb@23314:      with nb P  have "P (y + D)" by auto }
chaieb@23314:    hence "ALL x.~(EX (j::int) : {1..D}. EX (b::int) : A. P(b-j)) --> P (x) --> P (x + D)" by blast
chaieb@23314:    with H P have th: " \<forall>x. P x \<longrightarrow> P (x + D)" by auto
chaieb@23314:    from p1 obtain z where z: "ALL x. x > z --> (P x = P' x)" by blast
chaieb@23314:    let ?y = "x + (\<bar>x - z\<bar> + 1)*D"
chaieb@23314:    have zp: "0 <= (\<bar>x - z\<bar> + 1)" by arith
chaieb@23314:    from dp have yz: "?y > z" using incr_lemma[OF dp] by simp
chaieb@23314:    from z[rule_format, OF yz] incr_mult_lemma[OF dp th zp, rule_format, OF P] have th2: " P' ?y" by auto
chaieb@23314:    with periodic_finite_ex[OF dp pd]
chaieb@23314:    have "?R1" by blast}
chaieb@23314:  ultimately show ?thesis by blast
chaieb@23314: qed
berghofe@13876: 
berghofe@13876: lemma simp_from_to: "{i..j::int} = (if j < i then {} else insert i {i+1..j})"
berghofe@13876: apply(simp add:atLeastAtMost_def atLeast_def atMost_def)
berghofe@13876: apply(fastsimp)
berghofe@13876: done
berghofe@13876: 
chaieb@23314: theorem unity_coeff_ex: "(\<exists>(x::'a::{semiring_0}). P (l * x)) \<equiv> (\<exists>x. l dvd (x + 0) \<and> P x)"
chaieb@23314:   apply (rule eq_reflection[symmetric])
chaieb@23314:   apply (rule iffI)
chaieb@23314:   defer
chaieb@23314:   apply (erule exE)
chaieb@23314:   apply (rule_tac x = "l * x" in exI)
chaieb@23314:   apply (simp add: dvd_def)
chaieb@23314:   apply (rule_tac x="x" in exI, simp)
chaieb@23314:   apply (erule exE)
chaieb@23314:   apply (erule conjE)
chaieb@23314:   apply (erule dvdE)
chaieb@23314:   apply (rule_tac x = k in exI)
chaieb@23314:   apply simp
chaieb@23314:   done
berghofe@13876: 
chaieb@23314: lemma zdvd_mono: assumes not0: "(k::int) \<noteq> 0"
chaieb@23314: shows "((m::int) dvd t) \<equiv> (k*m dvd k*t)" 
chaieb@23314:   using not0 by (simp add: dvd_def)
berghofe@13876: 
chaieb@23314: lemma uminus_dvd_conv: "(d dvd (t::int)) \<equiv> (-d dvd t)" "(d dvd (t::int)) \<equiv> (d dvd -t)"
chaieb@23314:   by simp_all
wenzelm@14577: text {* \bigskip Theorems for transforming predicates on nat to predicates on @{text int}*}
chaieb@23314: lemma all_nat: "(\<forall>x::nat. P x) = (\<forall>x::int. 0 <= x \<longrightarrow> P (nat x))"
berghofe@13876:   by (simp split add: split_nat)
berghofe@13876: 
chaieb@23314: lemma ex_nat: "(\<exists>x::nat. P x) = (\<exists>x::int. 0 <= x \<and> P (nat x))"
huffman@23365:   apply (auto split add: split_nat)
huffman@23365:   apply (rule_tac x="int x" in exI, simp)
huffman@23365:   apply (rule_tac x = "nat x" in exI,erule_tac x = "nat x" in allE, simp)
huffman@23365:   done
berghofe@13876: 
chaieb@23314: lemma zdiff_int_split: "P (int (x - y)) =
berghofe@13876:   ((y \<le> x \<longrightarrow> P (int x - int y)) \<and> (x < y \<longrightarrow> P 0))"
huffman@23365:   by (case_tac "y \<le> x", simp_all)
berghofe@13876: 
chaieb@23314: lemma number_of1: "(0::int) <= number_of n \<Longrightarrow> (0::int) <= number_of (n BIT b)" by simp
chaieb@23314: lemma number_of2: "(0::int) <= Numeral0" by simp
chaieb@23314: lemma Suc_plus1: "Suc n = n + 1" by simp
berghofe@13876: 
wenzelm@14577: text {*
wenzelm@14577:   \medskip Specific instances of congruence rules, to prevent
wenzelm@14577:   simplifier from looping. *}
berghofe@13876: 
chaieb@23314: theorem imp_le_cong: "(0 <= x \<Longrightarrow> P = P') \<Longrightarrow> (0 <= (x::int) \<longrightarrow> P) = (0 <= x \<longrightarrow> P')" by simp
chaieb@18202: 
chaieb@23314: theorem conj_le_cong: "(0 <= x \<Longrightarrow> P = P') \<Longrightarrow> (0 <= (x::int) \<and> P) = (0 <= x \<and> P')" 
chaieb@23314:   by (simp cong: conj_cong)
haftmann@20485: lemma int_eq_number_of_eq:
haftmann@20485:   "(((number_of v)::int) = (number_of w)) = iszero ((number_of (v + (uminus w)))::int)"
chaieb@18202:   by simp
chaieb@18202: 
chaieb@23390: lemma mod_eq0_dvd_iff[presburger]: "(m::nat) mod n = 0 \<longleftrightarrow> n dvd m"
chaieb@23390: unfolding dvd_eq_mod_eq_0[symmetric] ..
chaieb@23390: 
chaieb@23390: lemma zmod_eq0_zdvd_iff[presburger]: "(m::int) mod n = 0 \<longleftrightarrow> n dvd m"
chaieb@23390: unfolding zdvd_iff_zmod_eq_0[symmetric] ..
chaieb@23390: declare mod_1[presburger]
chaieb@23390: declare mod_0[presburger]
chaieb@23390: declare zmod_1[presburger]
chaieb@23390: declare zmod_zero[presburger]
chaieb@23390: declare zmod_self[presburger]
chaieb@23390: declare mod_self[presburger]
chaieb@23390: declare DIVISION_BY_ZERO_MOD[presburger]
chaieb@23390: declare nat_mod_div_trivial[presburger]
chaieb@23390: declare div_mod_equality2[presburger]
chaieb@23390: declare div_mod_equality[presburger]
chaieb@23390: declare mod_div_equality2[presburger]
chaieb@23390: declare mod_div_equality[presburger]
chaieb@23390: declare mod_mult_self1[presburger]
chaieb@23390: declare mod_mult_self2[presburger]
chaieb@23390: declare zdiv_zmod_equality2[presburger]
chaieb@23390: declare zdiv_zmod_equality[presburger]
chaieb@23390: declare mod2_Suc_Suc[presburger]
chaieb@23390: lemma [presburger]: "(a::int) div 0 = 0" and [presburger]: "a mod 0 = a"
chaieb@23390: using IntDiv.DIVISION_BY_ZERO by blast+
chaieb@18202: 
chaieb@23314: use "Tools/Presburger/cooper.ML"
chaieb@23314: oracle linzqe_oracle ("term") = Coopereif.cooper_oracle
chaieb@18202: 
wenzelm@23146: use "Tools/Presburger/presburger.ML"
berghofe@13876: 
chaieb@23314: setup {* 
chaieb@23314:   arith_tactic_add 
chaieb@23314:     (mk_arith_tactic "presburger" (fn i => fn st =>
chaieb@23314:        (warning "Trying Presburger arithmetic ...";   
chaieb@23333:     Presburger.cooper_tac true [] [] ((ProofContext.init o theory_of_thm) st) i st)))
chaieb@23314:   (* FIXME!!!!!!! get the right context!!*)	
chaieb@23314: *}
chaieb@23333: 
chaieb@23314: method_setup presburger = {*
chaieb@23333: let
chaieb@23333:  fun keyword k = Scan.lift (Args.$$$ k -- Args.colon) >> K ()
chaieb@23333:  fun simple_keyword k = Scan.lift (Args.$$$ k) >> K ()
chaieb@23333:  val addN = "add"
chaieb@23333:  val delN = "del"
chaieb@23333:  val elimN = "elim"
chaieb@23333:  val any_keyword = keyword addN || keyword delN || simple_keyword elimN
chaieb@23333:  val thms = Scan.repeat (Scan.unless any_keyword Attrib.multi_thm) >> flat;
chaieb@23333: in
chaieb@23333:   fn src => Method.syntax 
chaieb@23333:    ((Scan.optional (simple_keyword elimN >> K false) true) -- 
chaieb@23333:     (Scan.optional (keyword addN |-- thms) []) -- 
chaieb@23333:     (Scan.optional (keyword delN |-- thms) [])) src 
chaieb@23333:   #> (fn (((elim, add_ths), del_ths),ctxt) => 
chaieb@23333:          Method.SIMPLE_METHOD' (Presburger.cooper_tac elim add_ths del_ths ctxt))
chaieb@23333: end
chaieb@23314: *} ""
haftmann@22801: 
chaieb@23390: lemma [presburger]: "m mod 2 = (1::nat) \<longleftrightarrow> \<not> 2 dvd m " by presburger
chaieb@23390: lemma [presburger]: "m mod 2 = Suc 0 \<longleftrightarrow> \<not> 2 dvd m " by presburger
chaieb@23390: lemma [presburger]: "m mod (Suc (Suc 0)) = (1::nat) \<longleftrightarrow> \<not> 2 dvd m " by presburger
chaieb@23390: lemma [presburger]: "m mod (Suc (Suc 0)) = Suc 0 \<longleftrightarrow> \<not> 2 dvd m " by presburger
chaieb@23390: lemma [presburger]: "m mod 2 = (1::int) \<longleftrightarrow> \<not> 2 dvd m " by presburger
chaieb@23390: 
haftmann@22801: subsection {* Code generator setup *}
haftmann@20595: text {*
haftmann@22801:   Presburger arithmetic is convenient to prove some
haftmann@22801:   of the following code lemmas on integer numerals:
haftmann@20595: *}
haftmann@20595: 
haftmann@20595: lemma eq_Pls_Pls:
chaieb@23390:   "Numeral.Pls = Numeral.Pls \<longleftrightarrow> True" by presburger
haftmann@20595: 
haftmann@20595: lemma eq_Pls_Min:
haftmann@22744:   "Numeral.Pls = Numeral.Min \<longleftrightarrow> False"
chaieb@23390:   unfolding Pls_def Min_def by presburger
haftmann@20595: 
haftmann@20595: lemma eq_Pls_Bit0:
haftmann@21454:   "Numeral.Pls = Numeral.Bit k bit.B0 \<longleftrightarrow> Numeral.Pls = k"
chaieb@23390:   unfolding Pls_def Bit_def bit.cases by presburger
haftmann@20595: 
haftmann@20595: lemma eq_Pls_Bit1:
haftmann@22744:   "Numeral.Pls = Numeral.Bit k bit.B1 \<longleftrightarrow> False"
chaieb@23390:   unfolding Pls_def Bit_def bit.cases by presburger
haftmann@20595: 
haftmann@20595: lemma eq_Min_Pls:
haftmann@22744:   "Numeral.Min = Numeral.Pls \<longleftrightarrow> False"
chaieb@23390:   unfolding Pls_def Min_def by presburger
haftmann@20595: 
haftmann@20595: lemma eq_Min_Min:
chaieb@23390:   "Numeral.Min = Numeral.Min \<longleftrightarrow> True" by presburger
haftmann@20595: 
haftmann@20595: lemma eq_Min_Bit0:
haftmann@22744:   "Numeral.Min = Numeral.Bit k bit.B0 \<longleftrightarrow> False"
chaieb@23390:   unfolding Min_def Bit_def bit.cases by presburger
haftmann@20595: 
haftmann@20595: lemma eq_Min_Bit1:
haftmann@21454:   "Numeral.Min = Numeral.Bit k bit.B1 \<longleftrightarrow> Numeral.Min = k"
chaieb@23390:   unfolding Min_def Bit_def bit.cases by presburger
haftmann@20595: 
haftmann@20595: lemma eq_Bit0_Pls:
haftmann@21454:   "Numeral.Bit k bit.B0 = Numeral.Pls \<longleftrightarrow> Numeral.Pls = k"
chaieb@23390:   unfolding Pls_def Bit_def bit.cases by presburger
haftmann@20595: 
haftmann@20595: lemma eq_Bit1_Pls:
haftmann@22744:   "Numeral.Bit k bit.B1 = Numeral.Pls \<longleftrightarrow> False"
chaieb@23390:   unfolding Pls_def Bit_def bit.cases  by presburger
haftmann@20595: 
haftmann@20595: lemma eq_Bit0_Min:
haftmann@22744:   "Numeral.Bit k bit.B0 = Numeral.Min \<longleftrightarrow> False"
chaieb@23390:   unfolding Min_def Bit_def bit.cases  by presburger
haftmann@20595: 
haftmann@20595: lemma eq_Bit1_Min:
haftmann@21454:   "(Numeral.Bit k bit.B1) = Numeral.Min \<longleftrightarrow> Numeral.Min = k"
chaieb@23390:   unfolding Min_def Bit_def bit.cases  by presburger
haftmann@20595: 
haftmann@20595: lemma eq_Bit_Bit:
haftmann@21454:   "Numeral.Bit k1 v1 = Numeral.Bit k2 v2 \<longleftrightarrow>
chaieb@23390:     v1 = v2 \<and> k1 = k2" 
haftmann@21454:   unfolding Bit_def
haftmann@20595:   apply (cases v1)
haftmann@20595:   apply (cases v2)
haftmann@20595:   apply auto
chaieb@23390:   apply presburger
haftmann@20595:   apply (cases v2)
haftmann@20595:   apply auto
chaieb@23390:   apply presburger
haftmann@20595:   apply (cases v2)
haftmann@20595:   apply auto
haftmann@20595: done
haftmann@20595: 
haftmann@22801: lemma eq_number_of:
chaieb@23390:   "(number_of k \<Colon> int) = number_of l \<longleftrightarrow> k = l" 
haftmann@22801:   unfolding number_of_is_id ..
haftmann@20595: 
haftmann@22394: 
haftmann@20595: lemma less_eq_Pls_Pls:
haftmann@22744:   "Numeral.Pls \<le> Numeral.Pls \<longleftrightarrow> True" by rule+
haftmann@20595: 
haftmann@20595: lemma less_eq_Pls_Min:
haftmann@22744:   "Numeral.Pls \<le> Numeral.Min \<longleftrightarrow> False"
chaieb@23390:   unfolding Pls_def Min_def by presburger
haftmann@20595: 
haftmann@20595: lemma less_eq_Pls_Bit:
haftmann@20595:   "Numeral.Pls \<le> Numeral.Bit k v \<longleftrightarrow> Numeral.Pls \<le> k"
haftmann@20595:   unfolding Pls_def Bit_def by (cases v) auto
haftmann@20595: 
haftmann@20595: lemma less_eq_Min_Pls:
haftmann@22744:   "Numeral.Min \<le> Numeral.Pls \<longleftrightarrow> True"
chaieb@23390:   unfolding Pls_def Min_def by presburger
haftmann@20595: 
haftmann@20595: lemma less_eq_Min_Min:
haftmann@22744:   "Numeral.Min \<le> Numeral.Min \<longleftrightarrow> True" by rule+
haftmann@20595: 
haftmann@20595: lemma less_eq_Min_Bit0:
haftmann@20595:   "Numeral.Min \<le> Numeral.Bit k bit.B0 \<longleftrightarrow> Numeral.Min < k"
haftmann@20595:   unfolding Min_def Bit_def by auto
haftmann@20595: 
haftmann@20595: lemma less_eq_Min_Bit1:
haftmann@20595:   "Numeral.Min \<le> Numeral.Bit k bit.B1 \<longleftrightarrow> Numeral.Min \<le> k"
haftmann@20595:   unfolding Min_def Bit_def by auto
haftmann@20595: 
haftmann@20595: lemma less_eq_Bit0_Pls:
haftmann@20595:   "Numeral.Bit k bit.B0 \<le> Numeral.Pls \<longleftrightarrow> k \<le> Numeral.Pls"
haftmann@20595:   unfolding Pls_def Bit_def by simp
haftmann@20595: 
haftmann@20595: lemma less_eq_Bit1_Pls:
haftmann@20595:   "Numeral.Bit k bit.B1 \<le> Numeral.Pls \<longleftrightarrow> k < Numeral.Pls"
haftmann@20595:   unfolding Pls_def Bit_def by auto
haftmann@20595: 
haftmann@20595: lemma less_eq_Bit_Min:
haftmann@20595:   "Numeral.Bit k v \<le> Numeral.Min \<longleftrightarrow> k \<le> Numeral.Min"
haftmann@20595:   unfolding Min_def Bit_def by (cases v) auto
haftmann@20595: 
haftmann@20595: lemma less_eq_Bit0_Bit:
haftmann@20595:   "Numeral.Bit k1 bit.B0 \<le> Numeral.Bit k2 v \<longleftrightarrow> k1 \<le> k2"
haftmann@22394:   unfolding Bit_def bit.cases by (cases v) auto
haftmann@20595: 
haftmann@20595: lemma less_eq_Bit_Bit1:
haftmann@20595:   "Numeral.Bit k1 v \<le> Numeral.Bit k2 bit.B1 \<longleftrightarrow> k1 \<le> k2"
haftmann@22394:   unfolding Bit_def bit.cases by (cases v) auto
haftmann@22394: 
haftmann@22394: lemma less_eq_Bit1_Bit0:
haftmann@22394:   "Numeral.Bit k1 bit.B1 \<le> Numeral.Bit k2 bit.B0 \<longleftrightarrow> k1 < k2"
haftmann@22394:   unfolding Bit_def by (auto split: bit.split)
haftmann@20595: 
haftmann@22801: lemma less_eq_number_of:
haftmann@22801:   "(number_of k \<Colon> int) \<le> number_of l \<longleftrightarrow> k \<le> l"
haftmann@22801:   unfolding number_of_is_id ..
haftmann@22394: 
haftmann@22394: 
haftmann@22394: lemma less_Pls_Pls:
chaieb@23405:   "Numeral.Pls < Numeral.Pls \<longleftrightarrow> False" by simp 
haftmann@22394: 
haftmann@22394: lemma less_Pls_Min:
haftmann@22744:   "Numeral.Pls < Numeral.Min \<longleftrightarrow> False"
chaieb@23390:   unfolding Pls_def Min_def  by presburger 
haftmann@22394: 
haftmann@22394: lemma less_Pls_Bit0:
haftmann@22394:   "Numeral.Pls < Numeral.Bit k bit.B0 \<longleftrightarrow> Numeral.Pls < k"
haftmann@22394:   unfolding Pls_def Bit_def by auto
haftmann@22394: 
haftmann@22394: lemma less_Pls_Bit1:
haftmann@22394:   "Numeral.Pls < Numeral.Bit k bit.B1 \<longleftrightarrow> Numeral.Pls \<le> k"
haftmann@22394:   unfolding Pls_def Bit_def by auto
haftmann@22394: 
haftmann@22394: lemma less_Min_Pls:
haftmann@22744:   "Numeral.Min < Numeral.Pls \<longleftrightarrow> True"
chaieb@23390:   unfolding Pls_def Min_def by presburger 
haftmann@22394: 
haftmann@22394: lemma less_Min_Min:
chaieb@23405:   "Numeral.Min < Numeral.Min \<longleftrightarrow> False"  by simp
haftmann@22394: 
haftmann@22394: lemma less_Min_Bit:
haftmann@22394:   "Numeral.Min < Numeral.Bit k v \<longleftrightarrow> Numeral.Min < k"
haftmann@22394:   unfolding Min_def Bit_def by (auto split: bit.split)
haftmann@22394: 
haftmann@22394: lemma less_Bit_Pls:
haftmann@22394:   "Numeral.Bit k v < Numeral.Pls \<longleftrightarrow> k < Numeral.Pls"
haftmann@22394:   unfolding Pls_def Bit_def by (auto split: bit.split)
haftmann@22394: 
haftmann@22394: lemma less_Bit0_Min:
haftmann@22394:   "Numeral.Bit k bit.B0 < Numeral.Min \<longleftrightarrow> k \<le> Numeral.Min"
haftmann@22394:   unfolding Min_def Bit_def by auto
haftmann@22394: 
haftmann@22394: lemma less_Bit1_Min:
haftmann@22394:   "Numeral.Bit k bit.B1 < Numeral.Min \<longleftrightarrow> k < Numeral.Min"
haftmann@22394:   unfolding Min_def Bit_def by auto
haftmann@22394: 
haftmann@22394: lemma less_Bit_Bit0:
haftmann@22394:   "Numeral.Bit k1 v < Numeral.Bit k2 bit.B0 \<longleftrightarrow> k1 < k2"
haftmann@22394:   unfolding Bit_def by (auto split: bit.split)
haftmann@22394: 
haftmann@22394: lemma less_Bit1_Bit:
haftmann@22394:   "Numeral.Bit k1 bit.B1 < Numeral.Bit k2 v \<longleftrightarrow> k1 < k2"
haftmann@22394:   unfolding Bit_def by (auto split: bit.split)
haftmann@22394: 
haftmann@22394: lemma less_Bit0_Bit1:
haftmann@22394:   "Numeral.Bit k1 bit.B0 < Numeral.Bit k2 bit.B1 \<longleftrightarrow> k1 \<le> k2"
chaieb@23390:   unfolding Bit_def bit.cases  by arith
haftmann@22394: 
haftmann@22801: lemma less_number_of:
haftmann@22801:   "(number_of k \<Colon> int) < number_of l \<longleftrightarrow> k < l"
haftmann@22801:   unfolding number_of_is_id ..
haftmann@22801: 
haftmann@22801: lemmas pred_succ_numeral_code [code func] =
haftmann@22801:   arith_simps(5-12)
haftmann@22801: 
haftmann@22801: lemmas plus_numeral_code [code func] =
haftmann@22801:   arith_simps(13-17)
haftmann@22801:   arith_simps(26-27)
haftmann@22801:   arith_extra_simps(1) [where 'a = int]
haftmann@22801: 
haftmann@22801: lemmas minus_numeral_code [code func] =
haftmann@22801:   arith_simps(18-21)
haftmann@22801:   arith_extra_simps(2) [where 'a = int]
haftmann@22801:   arith_extra_simps(5) [where 'a = int]
haftmann@22801: 
haftmann@22801: lemmas times_numeral_code [code func] =
haftmann@22801:   arith_simps(22-25)
haftmann@22801:   arith_extra_simps(4) [where 'a = int]
haftmann@22801: 
haftmann@22801: lemmas eq_numeral_code [code func] =
haftmann@22801:   eq_Pls_Pls eq_Pls_Min eq_Pls_Bit0 eq_Pls_Bit1
haftmann@22801:   eq_Min_Pls eq_Min_Min eq_Min_Bit0 eq_Min_Bit1
haftmann@22801:   eq_Bit0_Pls eq_Bit1_Pls eq_Bit0_Min eq_Bit1_Min eq_Bit_Bit
haftmann@22801:   eq_number_of
haftmann@22801: 
haftmann@22801: lemmas less_eq_numeral_code [code func] = less_eq_Pls_Pls less_eq_Pls_Min less_eq_Pls_Bit
haftmann@22801:   less_eq_Min_Pls less_eq_Min_Min less_eq_Min_Bit0 less_eq_Min_Bit1
haftmann@22801:   less_eq_Bit0_Pls less_eq_Bit1_Pls less_eq_Bit_Min less_eq_Bit0_Bit less_eq_Bit_Bit1 less_eq_Bit1_Bit0
haftmann@22801:   less_eq_number_of
haftmann@22801: 
haftmann@22394: lemmas less_numeral_code [code func] = less_Pls_Pls less_Pls_Min less_Pls_Bit0
haftmann@22394:   less_Pls_Bit1 less_Min_Pls less_Min_Min less_Min_Bit less_Bit_Pls
haftmann@22394:   less_Bit0_Min less_Bit1_Min less_Bit_Bit0 less_Bit1_Bit less_Bit0_Bit1
haftmann@22801:   less_number_of
haftmann@20595: 
huffman@23365: end




