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