src/HOL/Presburger.thy
author wenzelm
Fri Mar 28 19:43:54 2008 +0100 (2008-03-28)
changeset 26462 dac4e2bce00d
parent 26156 420c1947511c
child 26508 4cd7c4f936bb
permissions -rw-r--r--
avoid rebinding of existing facts;
     1 (* Title:      HOL/Presburger.thy
     2    ID:         $Id$
     3    Author:     Amine Chaieb, TU Muenchen
     4 *)
     5 
     6 header {* Decision Procedure for Presburger Arithmetic *}
     7 
     8 theory Presburger
     9 imports Arith_Tools SetInterval
    10 uses
    11   "Tools/Qelim/cooper_data.ML"
    12   "Tools/Qelim/generated_cooper.ML"
    13   "Tools/Qelim/qelim.ML"
    14   ("Tools/Qelim/cooper.ML")
    15   ("Tools/Qelim/presburger.ML")
    16 begin
    17 
    18 setup CooperData.setup
    19 
    20 subsection{* The @{text "-\<infinity>"} and @{text "+\<infinity>"} Properties *}
    21 
    22 
    23 lemma minf:
    24   "\<lbrakk>\<exists>(z ::'a::linorder).\<forall>x<z. P x = P' x; \<exists>z.\<forall>x<z. Q x = Q' x\<rbrakk> 
    25      \<Longrightarrow> \<exists>z.\<forall>x<z. (P x \<and> Q x) = (P' x \<and> Q' x)"
    26   "\<lbrakk>\<exists>(z ::'a::linorder).\<forall>x<z. P x = P' x; \<exists>z.\<forall>x<z. Q x = Q' x\<rbrakk> 
    27      \<Longrightarrow> \<exists>z.\<forall>x<z. (P x \<or> Q x) = (P' x \<or> Q' x)"
    28   "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x = t) = False"
    29   "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x \<noteq> t) = True"
    30   "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x < t) = True"
    31   "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x \<le> t) = True"
    32   "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x > t) = False"
    33   "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x \<ge> t) = False"
    34   "\<exists>z.\<forall>(x::'a::{linorder,plus,Divides.div})<z. (d dvd x + s) = (d dvd x + s)"
    35   "\<exists>z.\<forall>(x::'a::{linorder,plus,Divides.div})<z. (\<not> d dvd x + s) = (\<not> d dvd x + s)"
    36   "\<exists>z.\<forall>x<z. F = F"
    37   by ((erule exE, erule exE,rule_tac x="min z za" in exI,simp)+, (rule_tac x="t" in exI,fastsimp)+) simp_all
    38 
    39 lemma pinf:
    40   "\<lbrakk>\<exists>(z ::'a::linorder).\<forall>x>z. P x = P' x; \<exists>z.\<forall>x>z. Q x = Q' x\<rbrakk> 
    41      \<Longrightarrow> \<exists>z.\<forall>x>z. (P x \<and> Q x) = (P' x \<and> Q' x)"
    42   "\<lbrakk>\<exists>(z ::'a::linorder).\<forall>x>z. P x = P' x; \<exists>z.\<forall>x>z. Q x = Q' x\<rbrakk> 
    43      \<Longrightarrow> \<exists>z.\<forall>x>z. (P x \<or> Q x) = (P' x \<or> Q' x)"
    44   "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x = t) = False"
    45   "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x \<noteq> t) = True"
    46   "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x < t) = False"
    47   "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x \<le> t) = False"
    48   "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x > t) = True"
    49   "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x \<ge> t) = True"
    50   "\<exists>z.\<forall>(x::'a::{linorder,plus,Divides.div})>z. (d dvd x + s) = (d dvd x + s)"
    51   "\<exists>z.\<forall>(x::'a::{linorder,plus,Divides.div})>z. (\<not> d dvd x + s) = (\<not> d dvd x + s)"
    52   "\<exists>z.\<forall>x>z. F = F"
    53   by ((erule exE, erule exE,rule_tac x="max z za" in exI,simp)+,(rule_tac x="t" in exI,fastsimp)+) simp_all
    54 
    55 lemma inf_period:
    56   "\<lbrakk>\<forall>x k. P x = P (x - k*D); \<forall>x k. Q x = Q (x - k*D)\<rbrakk> 
    57     \<Longrightarrow> \<forall>x k. (P x \<and> Q x) = (P (x - k*D) \<and> Q (x - k*D))"
    58   "\<lbrakk>\<forall>x k. P x = P (x - k*D); \<forall>x k. Q x = Q (x - k*D)\<rbrakk> 
    59     \<Longrightarrow> \<forall>x k. (P x \<or> Q x) = (P (x - k*D) \<or> Q (x - k*D))"
    60   "(d::'a::{comm_ring,Divides.div}) dvd D \<Longrightarrow> \<forall>x k. (d dvd x + t) = (d dvd (x - k*D) + t)"
    61   "(d::'a::{comm_ring,Divides.div}) dvd D \<Longrightarrow> \<forall>x k. (\<not>d dvd x + t) = (\<not>d dvd (x - k*D) + t)"
    62   "\<forall>x k. F = F"
    63 by simp_all
    64   (clarsimp simp add: dvd_def, rule iffI, clarsimp,rule_tac x = "kb - ka*k" in exI,
    65     simp add: ring_simps, clarsimp,rule_tac x = "kb + ka*k" in exI,simp add: ring_simps)+
    66 
    67 subsection{* The A and B sets *}
    68 lemma bset:
    69   "\<lbrakk>\<forall>x.(\<forall>j \<in> {1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> P x \<longrightarrow> P(x - D) ;
    70      \<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> Q x \<longrightarrow> Q(x - D)\<rbrakk> \<Longrightarrow> 
    71   \<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j) \<longrightarrow> (P x \<and> Q x) \<longrightarrow> (P(x - D) \<and> Q (x - D))"
    72   "\<lbrakk>\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> P x \<longrightarrow> P(x - D) ;
    73      \<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> Q x \<longrightarrow> Q(x - D)\<rbrakk> \<Longrightarrow> 
    74   \<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))"
    75   "\<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))"
    76   "\<lbrakk>D>0 ; t \<in> B\<rbrakk> \<Longrightarrow>(\<forall>(x::int).(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> (x \<noteq> t) \<longrightarrow> (x - D \<noteq> t))"
    77   "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))"
    78   "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))"
    79   "\<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))"
    80   "\<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))"
    81   "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))"
    82   "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))"
    83   "\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j) \<longrightarrow> F \<longrightarrow> F"
    84 proof (blast, blast)
    85   assume dp: "D > 0" and tB: "t - 1\<in> B"
    86   show "(\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> (x = t) \<longrightarrow> (x - D = t))" 
    87     apply (rule allI, rule impI,erule ballE[where x="1"],erule ballE[where x="t - 1"])
    88     using dp tB by simp_all
    89 next
    90   assume dp: "D > 0" and tB: "t \<in> B"
    91   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))" 
    92     apply (rule allI, rule impI,erule ballE[where x="D"],erule ballE[where x="t"])
    93     using dp tB by simp_all
    94 next
    95   assume dp: "D > 0" thus "(\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> (x < t) \<longrightarrow> (x - D < t))" by arith
    96 next
    97   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
    98 next
    99   assume dp: "D > 0" and tB:"t \<in> B"
   100   {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"
   101     hence "x -t \<le> D" and "1 \<le> x - t" by simp+
   102       hence "\<exists>j \<in> {1 .. D}. x - t = j" by auto
   103       hence "\<exists>j \<in> {1 .. D}. x = t + j" by (simp add: ring_simps)
   104       with nob tB have "False" by simp}
   105   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
   106 next
   107   assume dp: "D > 0" and tB:"t - 1\<in> B"
   108   {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"
   109     hence "x - (t - 1) \<le> D" and "1 \<le> x - (t - 1)" by simp+
   110       hence "\<exists>j \<in> {1 .. D}. x - (t - 1) = j" by auto
   111       hence "\<exists>j \<in> {1 .. D}. x = (t - 1) + j" by (simp add: ring_simps)
   112       with nob tB have "False" by simp}
   113   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
   114 next
   115   assume d: "d dvd D"
   116   {fix x assume H: "d dvd x + t" with d have "d dvd (x - D) + t"
   117       by (clarsimp simp add: dvd_def,rule_tac x= "ka - k" in exI,simp add: ring_simps)}
   118   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
   119 next
   120   assume d: "d dvd D"
   121   {fix x assume H: "\<not>(d dvd x + t)" with d have "\<not>d dvd (x - D) + t"
   122       by (clarsimp simp add: dvd_def,erule_tac x= "ka + k" in allE,simp add: ring_simps)}
   123   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
   124 qed blast
   125 
   126 lemma aset:
   127   "\<lbrakk>\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> P x \<longrightarrow> P(x + D) ;
   128      \<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> Q x \<longrightarrow> Q(x + D)\<rbrakk> \<Longrightarrow> 
   129   \<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j) \<longrightarrow> (P x \<and> Q x) \<longrightarrow> (P(x + D) \<and> Q (x + D))"
   130   "\<lbrakk>\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> P x \<longrightarrow> P(x + D) ;
   131      \<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> Q x \<longrightarrow> Q(x + D)\<rbrakk> \<Longrightarrow> 
   132   \<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))"
   133   "\<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))"
   134   "\<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))"
   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 < t) \<longrightarrow> (x + D < t))"
   136   "\<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))"
   137   "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))"
   138   "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))"
   139   "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))"
   140   "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))"
   141   "\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j) \<longrightarrow> F \<longrightarrow> F"
   142 proof (blast, blast)
   143   assume dp: "D > 0" and tA: "t + 1 \<in> A"
   144   show "(\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> (x = t) \<longrightarrow> (x + D = t))" 
   145     apply (rule allI, rule impI,erule ballE[where x="1"],erule ballE[where x="t + 1"])
   146     using dp tA by simp_all
   147 next
   148   assume dp: "D > 0" and tA: "t \<in> A"
   149   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))" 
   150     apply (rule allI, rule impI,erule ballE[where x="D"],erule ballE[where x="t"])
   151     using dp tA by simp_all
   152 next
   153   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
   154 next
   155   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
   156 next
   157   assume dp: "D > 0" and tA:"t \<in> A"
   158   {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"
   159     hence "t - x \<le> D" and "1 \<le> t - x" by simp+
   160       hence "\<exists>j \<in> {1 .. D}. t - x = j" by auto
   161       hence "\<exists>j \<in> {1 .. D}. x = t - j" by (auto simp add: ring_simps) 
   162       with nob tA have "False" by simp}
   163   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
   164 next
   165   assume dp: "D > 0" and tA:"t + 1\<in> A"
   166   {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"
   167     hence "(t + 1) - x \<le> D" and "1 \<le> (t + 1) - x" by (simp_all add: ring_simps)
   168       hence "\<exists>j \<in> {1 .. D}. (t + 1) - x = j" by auto
   169       hence "\<exists>j \<in> {1 .. D}. x = (t + 1) - j" by (auto simp add: ring_simps)
   170       with nob tA have "False" by simp}
   171   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
   172 next
   173   assume d: "d dvd D"
   174   {fix x assume H: "d dvd x + t" with d have "d dvd (x + D) + t"
   175       by (clarsimp simp add: dvd_def,rule_tac x= "ka + k" in exI,simp add: ring_simps)}
   176   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
   177 next
   178   assume d: "d dvd D"
   179   {fix x assume H: "\<not>(d dvd x + t)" with d have "\<not>d dvd (x + D) + t"
   180       by (clarsimp simp add: dvd_def,erule_tac x= "ka - k" in allE,simp add: ring_simps)}
   181   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
   182 qed blast
   183 
   184 subsection{* Cooper's Theorem @{text "-\<infinity>"} and @{text "+\<infinity>"} Version *}
   185 
   186 subsubsection{* First some trivial facts about periodic sets or predicates *}
   187 lemma periodic_finite_ex:
   188   assumes dpos: "(0::int) < d" and modd: "ALL x k. P x = P(x - k*d)"
   189   shows "(EX x. P x) = (EX j : {1..d}. P j)"
   190   (is "?LHS = ?RHS")
   191 proof
   192   assume ?LHS
   193   then obtain x where P: "P x" ..
   194   have "x mod d = x - (x div d)*d" by(simp add:zmod_zdiv_equality mult_ac eq_diff_eq)
   195   hence Pmod: "P x = P(x mod d)" using modd by simp
   196   show ?RHS
   197   proof (cases)
   198     assume "x mod d = 0"
   199     hence "P 0" using P Pmod by simp
   200     moreover have "P 0 = P(0 - (-1)*d)" using modd by blast
   201     ultimately have "P d" by simp
   202     moreover have "d : {1..d}" using dpos by(simp add:atLeastAtMost_iff)
   203     ultimately show ?RHS ..
   204   next
   205     assume not0: "x mod d \<noteq> 0"
   206     have "P(x mod d)" using dpos P Pmod by(simp add:pos_mod_sign pos_mod_bound)
   207     moreover have "x mod d : {1..d}"
   208     proof -
   209       from dpos have "0 \<le> x mod d" by(rule pos_mod_sign)
   210       moreover from dpos have "x mod d < d" by(rule pos_mod_bound)
   211       ultimately show ?thesis using not0 by(simp add:atLeastAtMost_iff)
   212     qed
   213     ultimately show ?RHS ..
   214   qed
   215 qed auto
   216 
   217 subsubsection{* The @{text "-\<infinity>"} Version*}
   218 
   219 lemma decr_lemma: "0 < (d::int) \<Longrightarrow> x - (abs(x-z)+1) * d < z"
   220 by(induct rule: int_gr_induct,simp_all add:int_distrib)
   221 
   222 lemma incr_lemma: "0 < (d::int) \<Longrightarrow> z < x + (abs(x-z)+1) * d"
   223 by(induct rule: int_gr_induct, simp_all add:int_distrib)
   224 
   225 theorem int_induct[case_names base step1 step2]:
   226   assumes 
   227   base: "P(k::int)" and step1: "\<And>i. \<lbrakk>k \<le> i; P i\<rbrakk> \<Longrightarrow> P(i+1)" and
   228   step2: "\<And>i. \<lbrakk>k \<ge> i; P i\<rbrakk> \<Longrightarrow> P(i - 1)"
   229   shows "P i"
   230 proof -
   231   have "i \<le> k \<or> i\<ge> k" by arith
   232   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
   233 qed
   234 
   235 lemma decr_mult_lemma:
   236   assumes dpos: "(0::int) < d" and minus: "\<forall>x. P x \<longrightarrow> P(x - d)" and knneg: "0 <= k"
   237   shows "ALL x. P x \<longrightarrow> P(x - k*d)"
   238 using knneg
   239 proof (induct rule:int_ge_induct)
   240   case base thus ?case by simp
   241 next
   242   case (step i)
   243   {fix x
   244     have "P x \<longrightarrow> P (x - i * d)" using step.hyps by blast
   245     also have "\<dots> \<longrightarrow> P(x - (i + 1) * d)" using minus[THEN spec, of "x - i * d"]
   246       by (simp add:int_distrib OrderedGroup.diff_diff_eq[symmetric])
   247     ultimately have "P x \<longrightarrow> P(x - (i + 1) * d)" by blast}
   248   thus ?case ..
   249 qed
   250 
   251 lemma  minusinfinity:
   252   assumes dpos: "0 < d" and
   253     P1eqP1: "ALL x k. P1 x = P1(x - k*d)" and ePeqP1: "EX z::int. ALL x. x < z \<longrightarrow> (P x = P1 x)"
   254   shows "(EX x. P1 x) \<longrightarrow> (EX x. P x)"
   255 proof
   256   assume eP1: "EX x. P1 x"
   257   then obtain x where P1: "P1 x" ..
   258   from ePeqP1 obtain z where P1eqP: "ALL x. x < z \<longrightarrow> (P x = P1 x)" ..
   259   let ?w = "x - (abs(x-z)+1) * d"
   260   from dpos have w: "?w < z" by(rule decr_lemma)
   261   have "P1 x = P1 ?w" using P1eqP1 by blast
   262   also have "\<dots> = P(?w)" using w P1eqP by blast
   263   finally have "P ?w" using P1 by blast
   264   thus "EX x. P x" ..
   265 qed
   266 
   267 lemma cpmi: 
   268   assumes dp: "0 < D" and p1:"\<exists>z. \<forall> x< z. P x = P' x"
   269   and nb:"\<forall>x.(\<forall> j\<in> {1..D}. \<forall>(b::int) \<in> B. x \<noteq> b+j) --> P (x) --> P (x - D)"
   270   and pd: "\<forall> x k. P' x = P' (x-k*D)"
   271   shows "(\<exists>x. P x) = ((\<exists> j\<in> {1..D} . P' j) | (\<exists> j \<in> {1..D}.\<exists> b\<in> B. P (b+j)))" 
   272          (is "?L = (?R1 \<or> ?R2)")
   273 proof-
   274  {assume "?R2" hence "?L"  by blast}
   275  moreover
   276  {assume H:"?R1" hence "?L" using minusinfinity[OF dp pd p1] periodic_finite_ex[OF dp pd] by simp}
   277  moreover 
   278  { fix x
   279    assume P: "P x" and H: "\<not> ?R2"
   280    {fix y assume "\<not> (\<exists>j\<in>{1..D}. \<exists>b\<in>B. P (b + j))" and P: "P y"
   281      hence "~(EX (j::int) : {1..D}. EX (b::int) : B. y = b+j)" by auto
   282      with nb P  have "P (y - D)" by auto }
   283    hence "ALL x.~(EX (j::int) : {1..D}. EX (b::int) : B. P(b+j)) --> P (x) --> P (x - D)" by blast
   284    with H P have th: " \<forall>x. P x \<longrightarrow> P (x - D)" by auto
   285    from p1 obtain z where z: "ALL x. x < z --> (P x = P' x)" by blast
   286    let ?y = "x - (\<bar>x - z\<bar> + 1)*D"
   287    have zp: "0 <= (\<bar>x - z\<bar> + 1)" by arith
   288    from dp have yz: "?y < z" using decr_lemma[OF dp] by simp   
   289    from z[rule_format, OF yz] decr_mult_lemma[OF dp th zp, rule_format, OF P] have th2: " P' ?y" by auto
   290    with periodic_finite_ex[OF dp pd]
   291    have "?R1" by blast}
   292  ultimately show ?thesis by blast
   293 qed
   294 
   295 subsubsection {* The @{text "+\<infinity>"} Version*}
   296 
   297 lemma  plusinfinity:
   298   assumes dpos: "(0::int) < d" and
   299     P1eqP1: "\<forall>x k. P' x = P'(x - k*d)" and ePeqP1: "\<exists> z. \<forall> x>z. P x = P' x"
   300   shows "(\<exists> x. P' x) \<longrightarrow> (\<exists> x. P x)"
   301 proof
   302   assume eP1: "EX x. P' x"
   303   then obtain x where P1: "P' x" ..
   304   from ePeqP1 obtain z where P1eqP: "\<forall>x>z. P x = P' x" ..
   305   let ?w' = "x + (abs(x-z)+1) * d"
   306   let ?w = "x - (-(abs(x-z) + 1))*d"
   307   have ww'[simp]: "?w = ?w'" by (simp add: ring_simps)
   308   from dpos have w: "?w > z" by(simp only: ww' incr_lemma)
   309   hence "P' x = P' ?w" using P1eqP1 by blast
   310   also have "\<dots> = P(?w)" using w P1eqP by blast
   311   finally have "P ?w" using P1 by blast
   312   thus "EX x. P x" ..
   313 qed
   314 
   315 lemma incr_mult_lemma:
   316   assumes dpos: "(0::int) < d" and plus: "ALL x::int. P x \<longrightarrow> P(x + d)" and knneg: "0 <= k"
   317   shows "ALL x. P x \<longrightarrow> P(x + k*d)"
   318 using knneg
   319 proof (induct rule:int_ge_induct)
   320   case base thus ?case by simp
   321 next
   322   case (step i)
   323   {fix x
   324     have "P x \<longrightarrow> P (x + i * d)" using step.hyps by blast
   325     also have "\<dots> \<longrightarrow> P(x + (i + 1) * d)" using plus[THEN spec, of "x + i * d"]
   326       by (simp add:int_distrib zadd_ac)
   327     ultimately have "P x \<longrightarrow> P(x + (i + 1) * d)" by blast}
   328   thus ?case ..
   329 qed
   330 
   331 lemma cppi: 
   332   assumes dp: "0 < D" and p1:"\<exists>z. \<forall> x> z. P x = P' x"
   333   and nb:"\<forall>x.(\<forall> j\<in> {1..D}. \<forall>(b::int) \<in> A. x \<noteq> b - j) --> P (x) --> P (x + D)"
   334   and pd: "\<forall> x k. P' x= P' (x-k*D)"
   335   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)")
   336 proof-
   337  {assume "?R2" hence "?L"  by blast}
   338  moreover
   339  {assume H:"?R1" hence "?L" using plusinfinity[OF dp pd p1] periodic_finite_ex[OF dp pd] by simp}
   340  moreover 
   341  { fix x
   342    assume P: "P x" and H: "\<not> ?R2"
   343    {fix y assume "\<not> (\<exists>j\<in>{1..D}. \<exists>b\<in>A. P (b - j))" and P: "P y"
   344      hence "~(EX (j::int) : {1..D}. EX (b::int) : A. y = b - j)" by auto
   345      with nb P  have "P (y + D)" by auto }
   346    hence "ALL x.~(EX (j::int) : {1..D}. EX (b::int) : A. P(b-j)) --> P (x) --> P (x + D)" by blast
   347    with H P have th: " \<forall>x. P x \<longrightarrow> P (x + D)" by auto
   348    from p1 obtain z where z: "ALL x. x > z --> (P x = P' x)" by blast
   349    let ?y = "x + (\<bar>x - z\<bar> + 1)*D"
   350    have zp: "0 <= (\<bar>x - z\<bar> + 1)" by arith
   351    from dp have yz: "?y > z" using incr_lemma[OF dp] by simp
   352    from z[rule_format, OF yz] incr_mult_lemma[OF dp th zp, rule_format, OF P] have th2: " P' ?y" by auto
   353    with periodic_finite_ex[OF dp pd]
   354    have "?R1" by blast}
   355  ultimately show ?thesis by blast
   356 qed
   357 
   358 lemma simp_from_to: "{i..j::int} = (if j < i then {} else insert i {i+1..j})"
   359 apply(simp add:atLeastAtMost_def atLeast_def atMost_def)
   360 apply(fastsimp)
   361 done
   362 
   363 theorem unity_coeff_ex: "(\<exists>(x::'a::{semiring_0,Divides.div}). P (l * x)) \<equiv> (\<exists>x. l dvd (x + 0) \<and> P x)"
   364   apply (rule eq_reflection[symmetric])
   365   apply (rule iffI)
   366   defer
   367   apply (erule exE)
   368   apply (rule_tac x = "l * x" in exI)
   369   apply (simp add: dvd_def)
   370   apply (rule_tac x="x" in exI, simp)
   371   apply (erule exE)
   372   apply (erule conjE)
   373   apply (erule dvdE)
   374   apply (rule_tac x = k in exI)
   375   apply simp
   376   done
   377 
   378 lemma zdvd_mono: assumes not0: "(k::int) \<noteq> 0"
   379 shows "((m::int) dvd t) \<equiv> (k*m dvd k*t)" 
   380   using not0 by (simp add: dvd_def)
   381 
   382 lemma uminus_dvd_conv: "(d dvd (t::int)) \<equiv> (-d dvd t)" "(d dvd (t::int)) \<equiv> (d dvd -t)"
   383   by simp_all
   384 text {* \bigskip Theorems for transforming predicates on nat to predicates on @{text int}*}
   385 lemma all_nat: "(\<forall>x::nat. P x) = (\<forall>x::int. 0 <= x \<longrightarrow> P (nat x))"
   386   by (simp split add: split_nat)
   387 
   388 lemma ex_nat: "(\<exists>x::nat. P x) = (\<exists>x::int. 0 <= x \<and> P (nat x))"
   389   apply (auto split add: split_nat)
   390   apply (rule_tac x="int x" in exI, simp)
   391   apply (rule_tac x = "nat x" in exI,erule_tac x = "nat x" in allE, simp)
   392   done
   393 
   394 lemma zdiff_int_split: "P (int (x - y)) =
   395   ((y \<le> x \<longrightarrow> P (int x - int y)) \<and> (x < y \<longrightarrow> P 0))"
   396   by (case_tac "y \<le> x", simp_all add: zdiff_int)
   397 
   398 lemma number_of1: "(0::int) <= number_of n \<Longrightarrow> (0::int) <= number_of (Int.Bit0 n) \<and> (0::int) <= number_of (Int.Bit1 n)"
   399 by simp
   400 lemma number_of2: "(0::int) <= Numeral0" by simp
   401 lemma Suc_plus1: "Suc n = n + 1" by simp
   402 
   403 text {*
   404   \medskip Specific instances of congruence rules, to prevent
   405   simplifier from looping. *}
   406 
   407 theorem imp_le_cong: "(0 <= x \<Longrightarrow> P = P') \<Longrightarrow> (0 <= (x::int) \<longrightarrow> P) = (0 <= x \<longrightarrow> P')" by simp
   408 
   409 theorem conj_le_cong: "(0 <= x \<Longrightarrow> P = P') \<Longrightarrow> (0 <= (x::int) \<and> P) = (0 <= x \<and> P')" 
   410   by (simp cong: conj_cong)
   411 lemma int_eq_number_of_eq:
   412   "(((number_of v)::int) = (number_of w)) = iszero ((number_of (v + (uminus w)))::int)"
   413   by simp
   414 
   415 lemma mod_eq0_dvd_iff[presburger]: "(m::nat) mod n = 0 \<longleftrightarrow> n dvd m"
   416 unfolding dvd_eq_mod_eq_0[symmetric] ..
   417 
   418 lemma zmod_eq0_zdvd_iff[presburger]: "(m::int) mod n = 0 \<longleftrightarrow> n dvd m"
   419 unfolding zdvd_iff_zmod_eq_0[symmetric] ..
   420 declare mod_1[presburger]
   421 declare mod_0[presburger]
   422 declare zmod_1[presburger]
   423 declare zmod_zero[presburger]
   424 declare zmod_self[presburger]
   425 declare mod_self[presburger]
   426 declare DIVISION_BY_ZERO_MOD[presburger]
   427 declare nat_mod_div_trivial[presburger]
   428 declare div_mod_equality2[presburger]
   429 declare div_mod_equality[presburger]
   430 declare mod_div_equality2[presburger]
   431 declare mod_div_equality[presburger]
   432 declare mod_mult_self1[presburger]
   433 declare mod_mult_self2[presburger]
   434 declare zdiv_zmod_equality2[presburger]
   435 declare zdiv_zmod_equality[presburger]
   436 declare mod2_Suc_Suc[presburger]
   437 lemma [presburger]: "(a::int) div 0 = 0" and [presburger]: "a mod 0 = a"
   438 using IntDiv.DIVISION_BY_ZERO by blast+
   439 
   440 use "Tools/Qelim/cooper.ML"
   441 oracle linzqe_oracle ("term") = Coopereif.cooper_oracle
   442 
   443 use "Tools/Qelim/presburger.ML"
   444 
   445 declaration {* fn _ =>
   446   arith_tactic_add
   447     (mk_arith_tactic "presburger" (fn ctxt => fn i => fn st =>
   448        (warning "Trying Presburger arithmetic ...";   
   449     Presburger.cooper_tac true [] [] ctxt i st)))
   450 *}
   451 
   452 method_setup presburger = {*
   453 let
   454  fun keyword k = Scan.lift (Args.$$$ k -- Args.colon) >> K ()
   455  fun simple_keyword k = Scan.lift (Args.$$$ k) >> K ()
   456  val addN = "add"
   457  val delN = "del"
   458  val elimN = "elim"
   459  val any_keyword = keyword addN || keyword delN || simple_keyword elimN
   460  val thms = Scan.repeat (Scan.unless any_keyword Attrib.multi_thm) >> flat;
   461 in
   462   fn src => Method.syntax 
   463    ((Scan.optional (simple_keyword elimN >> K false) true) -- 
   464     (Scan.optional (keyword addN |-- thms) []) -- 
   465     (Scan.optional (keyword delN |-- thms) [])) src 
   466   #> (fn (((elim, add_ths), del_ths),ctxt) => 
   467          Method.SIMPLE_METHOD' (Presburger.cooper_tac elim add_ths del_ths ctxt))
   468 end
   469 *} "Cooper's algorithm for Presburger arithmetic"
   470 
   471 lemma [presburger]: "m mod 2 = (1::nat) \<longleftrightarrow> \<not> 2 dvd m " by presburger
   472 lemma [presburger]: "m mod 2 = Suc 0 \<longleftrightarrow> \<not> 2 dvd m " by presburger
   473 lemma [presburger]: "m mod (Suc (Suc 0)) = (1::nat) \<longleftrightarrow> \<not> 2 dvd m " by presburger
   474 lemma [presburger]: "m mod (Suc (Suc 0)) = Suc 0 \<longleftrightarrow> \<not> 2 dvd m " by presburger
   475 lemma [presburger]: "m mod 2 = (1::int) \<longleftrightarrow> \<not> 2 dvd m " by presburger
   476 
   477 
   478 lemma zdvd_period:
   479   fixes a d :: int
   480   assumes advdd: "a dvd d"
   481   shows "a dvd (x + t) \<longleftrightarrow> a dvd ((x + c * d) + t)"
   482 proof-
   483   {
   484     fix x k
   485     from inf_period(3) [OF advdd, rule_format, where x=x and k="-k"]  
   486     have "a dvd (x + t) \<longleftrightarrow> a dvd (x + k * d + t)" by simp
   487   }
   488   hence "\<forall>x.\<forall>k. ((a::int) dvd (x + t)) = (a dvd (x+k*d + t))"  by simp
   489   then show ?thesis by simp
   490 qed
   491 
   492 
   493 subsection {* Code generator setup *}
   494 
   495 text {*
   496   Presburger arithmetic is convenient to prove some
   497   of the following code lemmas on integer numerals:
   498 *}
   499 
   500 lemma eq_Pls_Pls:
   501   "Int.Pls = Int.Pls \<longleftrightarrow> True" by presburger
   502 
   503 lemma eq_Pls_Min:
   504   "Int.Pls = Int.Min \<longleftrightarrow> False"
   505   unfolding Pls_def Int.Min_def by presburger
   506 
   507 lemma eq_Pls_Bit0:
   508   "Int.Pls = Int.Bit0 k \<longleftrightarrow> Int.Pls = k"
   509   unfolding Pls_def Bit0_def by presburger
   510 
   511 lemma eq_Pls_Bit1:
   512   "Int.Pls = Int.Bit1 k \<longleftrightarrow> False"
   513   unfolding Pls_def Bit1_def by presburger
   514 
   515 lemma eq_Min_Pls:
   516   "Int.Min = Int.Pls \<longleftrightarrow> False"
   517   unfolding Pls_def Int.Min_def by presburger
   518 
   519 lemma eq_Min_Min:
   520   "Int.Min = Int.Min \<longleftrightarrow> True" by presburger
   521 
   522 lemma eq_Min_Bit0:
   523   "Int.Min = Int.Bit0 k \<longleftrightarrow> False"
   524   unfolding Int.Min_def Bit0_def by presburger
   525 
   526 lemma eq_Min_Bit1:
   527   "Int.Min = Int.Bit1 k \<longleftrightarrow> Int.Min = k"
   528   unfolding Int.Min_def Bit1_def by presburger
   529 
   530 lemma eq_Bit0_Pls:
   531   "Int.Bit0 k = Int.Pls \<longleftrightarrow> Int.Pls = k"
   532   unfolding Pls_def Bit0_def by presburger
   533 
   534 lemma eq_Bit1_Pls:
   535   "Int.Bit1 k = Int.Pls \<longleftrightarrow> False"
   536   unfolding Pls_def Bit1_def by presburger
   537 
   538 lemma eq_Bit0_Min:
   539   "Int.Bit0 k = Int.Min \<longleftrightarrow> False"
   540   unfolding Int.Min_def Bit0_def by presburger
   541 
   542 lemma eq_Bit1_Min:
   543   "Int.Bit1 k = Int.Min \<longleftrightarrow> Int.Min = k"
   544   unfolding Int.Min_def Bit1_def by presburger
   545 
   546 lemma eq_Bit0_Bit0:
   547   "Int.Bit0 k1 = Int.Bit0 k2 \<longleftrightarrow> k1 = k2"
   548   unfolding Bit0_def by presburger
   549 
   550 lemma eq_Bit0_Bit1:
   551   "Int.Bit0 k1 = Int.Bit1 k2 \<longleftrightarrow> False"
   552   unfolding Bit0_def Bit1_def by presburger
   553 
   554 lemma eq_Bit1_Bit0:
   555   "Int.Bit1 k1 = Int.Bit0 k2 \<longleftrightarrow> False"
   556   unfolding Bit0_def Bit1_def by presburger
   557 
   558 lemma eq_Bit1_Bit1:
   559   "Int.Bit1 k1 = Int.Bit1 k2 \<longleftrightarrow> k1 = k2"
   560   unfolding Bit1_def by presburger
   561 
   562 lemma eq_number_of:
   563   "(number_of k \<Colon> int) = number_of l \<longleftrightarrow> k = l" 
   564   unfolding number_of_is_id ..
   565 
   566 
   567 lemma less_eq_Pls_Pls:
   568   "Int.Pls \<le> Int.Pls \<longleftrightarrow> True" by rule+
   569 
   570 lemma less_eq_Pls_Min:
   571   "Int.Pls \<le> Int.Min \<longleftrightarrow> False"
   572   unfolding Pls_def Int.Min_def by presburger
   573 
   574 lemma less_eq_Pls_Bit0:
   575   "Int.Pls \<le> Int.Bit0 k \<longleftrightarrow> Int.Pls \<le> k"
   576   unfolding Pls_def Bit0_def by auto
   577 
   578 lemma less_eq_Pls_Bit1:
   579   "Int.Pls \<le> Int.Bit1 k \<longleftrightarrow> Int.Pls \<le> k"
   580   unfolding Pls_def Bit1_def by auto
   581 
   582 lemma less_eq_Min_Pls:
   583   "Int.Min \<le> Int.Pls \<longleftrightarrow> True"
   584   unfolding Pls_def Int.Min_def by presburger
   585 
   586 lemma less_eq_Min_Min:
   587   "Int.Min \<le> Int.Min \<longleftrightarrow> True" by rule+
   588 
   589 lemma less_eq_Min_Bit0:
   590   "Int.Min \<le> Int.Bit0 k \<longleftrightarrow> Int.Min < k"
   591   unfolding Int.Min_def Bit0_def by auto
   592 
   593 lemma less_eq_Min_Bit1:
   594   "Int.Min \<le> Int.Bit1 k \<longleftrightarrow> Int.Min \<le> k"
   595   unfolding Int.Min_def Bit1_def by auto
   596 
   597 lemma less_eq_Bit0_Pls:
   598   "Int.Bit0 k \<le> Int.Pls \<longleftrightarrow> k \<le> Int.Pls"
   599   unfolding Pls_def Bit0_def by simp
   600 
   601 lemma less_eq_Bit1_Pls:
   602   "Int.Bit1 k \<le> Int.Pls \<longleftrightarrow> k < Int.Pls"
   603   unfolding Pls_def Bit1_def by auto
   604 
   605 lemma less_eq_Bit0_Min:
   606   "Int.Bit0 k \<le> Int.Min \<longleftrightarrow> k \<le> Int.Min"
   607   unfolding Int.Min_def Bit0_def by auto
   608 
   609 lemma less_eq_Bit1_Min:
   610   "Int.Bit1 k \<le> Int.Min \<longleftrightarrow> k \<le> Int.Min"
   611   unfolding Int.Min_def Bit1_def by auto
   612 
   613 lemma less_eq_Bit0_Bit0:
   614   "Int.Bit0 k1 \<le> Int.Bit0 k2 \<longleftrightarrow> k1 \<le> k2"
   615   unfolding Bit0_def by auto
   616 
   617 lemma less_eq_Bit0_Bit1:
   618   "Int.Bit0 k1 \<le> Int.Bit1 k2 \<longleftrightarrow> k1 \<le> k2"
   619   unfolding Bit0_def Bit1_def by auto
   620 
   621 lemma less_eq_Bit1_Bit0:
   622   "Int.Bit1 k1 \<le> Int.Bit0 k2 \<longleftrightarrow> k1 < k2"
   623   unfolding Bit0_def Bit1_def by auto
   624 
   625 lemma less_eq_Bit1_Bit1:
   626   "Int.Bit1 k1 \<le> Int.Bit1 k2 \<longleftrightarrow> k1 \<le> k2"
   627   unfolding Bit1_def by auto
   628 
   629 lemma less_eq_number_of:
   630   "(number_of k \<Colon> int) \<le> number_of l \<longleftrightarrow> k \<le> l"
   631   unfolding number_of_is_id ..
   632 
   633 
   634 lemma less_Pls_Pls:
   635   "Int.Pls < Int.Pls \<longleftrightarrow> False" by simp 
   636 
   637 lemma less_Pls_Min:
   638   "Int.Pls < Int.Min \<longleftrightarrow> False"
   639   unfolding Pls_def Int.Min_def  by presburger 
   640 
   641 lemma less_Pls_Bit0:
   642   "Int.Pls < Int.Bit0 k \<longleftrightarrow> Int.Pls < k"
   643   unfolding Pls_def Bit0_def by auto
   644 
   645 lemma less_Pls_Bit1:
   646   "Int.Pls < Int.Bit1 k \<longleftrightarrow> Int.Pls \<le> k"
   647   unfolding Pls_def Bit1_def by auto
   648 
   649 lemma less_Min_Pls:
   650   "Int.Min < Int.Pls \<longleftrightarrow> True"
   651   unfolding Pls_def Int.Min_def by presburger 
   652 
   653 lemma less_Min_Min:
   654   "Int.Min < Int.Min \<longleftrightarrow> False"  by simp
   655 
   656 lemma less_Min_Bit0:
   657   "Int.Min < Int.Bit0 k \<longleftrightarrow> Int.Min < k"
   658   unfolding Int.Min_def Bit0_def by auto
   659 
   660 lemma less_Min_Bit1:
   661   "Int.Min < Int.Bit1 k \<longleftrightarrow> Int.Min < k"
   662   unfolding Int.Min_def Bit1_def by auto
   663 
   664 lemma less_Bit0_Pls:
   665   "Int.Bit0 k < Int.Pls \<longleftrightarrow> k < Int.Pls"
   666   unfolding Pls_def Bit0_def by auto
   667 
   668 lemma less_Bit1_Pls:
   669   "Int.Bit1 k < Int.Pls \<longleftrightarrow> k < Int.Pls"
   670   unfolding Pls_def Bit1_def by auto
   671 
   672 lemma less_Bit0_Min:
   673   "Int.Bit0 k < Int.Min \<longleftrightarrow> k \<le> Int.Min"
   674   unfolding Int.Min_def Bit0_def by auto
   675 
   676 lemma less_Bit1_Min:
   677   "Int.Bit1 k < Int.Min \<longleftrightarrow> k < Int.Min"
   678   unfolding Int.Min_def Bit1_def by auto
   679 
   680 lemma less_Bit0_Bit0:
   681   "Int.Bit0 k1 < Int.Bit0 k2 \<longleftrightarrow> k1 < k2"
   682   unfolding Bit0_def by auto
   683 
   684 lemma less_Bit0_Bit1:
   685   "Int.Bit0 k1 < Int.Bit1 k2 \<longleftrightarrow> k1 \<le> k2"
   686   unfolding Bit0_def Bit1_def by auto
   687 
   688 lemma less_Bit1_Bit0:
   689   "Int.Bit1 k1 < Int.Bit0 k2 \<longleftrightarrow> k1 < k2"
   690   unfolding Bit0_def Bit1_def by auto
   691 
   692 lemma less_Bit1_Bit1:
   693   "Int.Bit1 k1 < Int.Bit1 k2 \<longleftrightarrow> k1 < k2"
   694   unfolding Bit1_def by auto
   695 
   696 lemma less_number_of:
   697   "(number_of k \<Colon> int) < number_of l \<longleftrightarrow> k < l"
   698   unfolding number_of_is_id ..
   699 
   700 lemmas pred_succ_numeral_code [code func] =
   701   pred_bin_simps succ_bin_simps
   702 
   703 lemmas plus_numeral_code [code func] =
   704   add_bin_simps
   705   arith_extra_simps(1) [where 'a = int]
   706 
   707 lemmas minus_numeral_code [code func] =
   708   minus_bin_simps
   709   arith_extra_simps(2) [where 'a = int]
   710   arith_extra_simps(5) [where 'a = int]
   711 
   712 lemmas times_numeral_code [code func] =
   713   mult_bin_simps
   714   arith_extra_simps(4) [where 'a = int]
   715 
   716 lemmas eq_numeral_code [code func] =
   717   eq_Pls_Pls eq_Pls_Min eq_Pls_Bit0 eq_Pls_Bit1
   718   eq_Min_Pls eq_Min_Min eq_Min_Bit0 eq_Min_Bit1
   719   eq_Bit0_Pls eq_Bit1_Pls eq_Bit0_Min eq_Bit1_Min
   720   eq_Bit0_Bit0 eq_Bit0_Bit1 eq_Bit1_Bit0 eq_Bit1_Bit1
   721   eq_number_of
   722 
   723 lemmas less_eq_numeral_code [code func] =
   724   less_eq_Pls_Pls less_eq_Pls_Min less_eq_Pls_Bit0 less_eq_Pls_Bit1
   725   less_eq_Min_Pls less_eq_Min_Min less_eq_Min_Bit0 less_eq_Min_Bit1
   726   less_eq_Bit0_Pls less_eq_Bit1_Pls less_eq_Bit0_Min less_eq_Bit1_Min
   727   less_eq_Bit0_Bit0 less_eq_Bit0_Bit1 less_eq_Bit1_Bit0 less_eq_Bit1_Bit1
   728   less_eq_number_of
   729 
   730 lemmas less_numeral_code [code func] =
   731   less_Pls_Pls less_Pls_Min less_Pls_Bit0 less_Pls_Bit1
   732   less_Min_Pls less_Min_Min less_Min_Bit0 less_Min_Bit1
   733   less_Bit0_Pls less_Bit1_Pls less_Bit0_Min less_Bit1_Min
   734   less_Bit0_Bit0 less_Bit0_Bit1 less_Bit1_Bit0 less_Bit1_Bit1
   735   less_number_of
   736 
   737 context ring_1
   738 begin
   739 
   740 lemma of_int_num [code func]:
   741   "of_int k = (if k = 0 then 0 else if k < 0 then
   742      - of_int (- k) else let
   743        (l, m) = divAlg (k, 2);
   744        l' = of_int l
   745      in if m = 0 then l' + l' else l' + l' + 1)"
   746 proof -
   747   have aux1: "k mod (2\<Colon>int) \<noteq> (0\<Colon>int) \<Longrightarrow> 
   748     of_int k = of_int (k div 2 * 2 + 1)"
   749   proof -
   750     assume "k mod 2 \<noteq> 0"
   751     then have "k mod 2 = 1" by arith
   752     moreover have "of_int k = of_int (k div 2 * 2 + k mod 2)" by simp
   753     ultimately show ?thesis by auto
   754   qed
   755   have aux2: "\<And>x. of_int 2 * x = x + x"
   756   proof -
   757     fix x
   758     have int2: "(2::int) = 1 + 1" by arith
   759     show "of_int 2 * x = x + x"
   760     unfolding int2 of_int_add left_distrib by simp
   761   qed
   762   have aux3: "\<And>x. x * of_int 2 = x + x"
   763   proof -
   764     fix x
   765     have int2: "(2::int) = 1 + 1" by arith
   766     show "x * of_int 2 = x + x" 
   767     unfolding int2 of_int_add right_distrib by simp
   768   qed
   769   from aux1 show ?thesis by (auto simp add: divAlg_mod_div Let_def aux2 aux3)
   770 qed
   771 
   772 end
   773 
   774 end