src/HOL/Presburger.thy
author huffman
Tue, 27 Mar 2012 14:49:56 +0200
changeset 48013 d64fa2ca54b8
parent 47978 2a1953f0d20d
child 48036 9344891b504b
permissions -rw-r--r--
remove redundant lemmas
wenzelm@23465
     1
(* Title:      HOL/Presburger.thy
wenzelm@23465
     2
   Author:     Amine Chaieb, TU Muenchen
wenzelm@23465
     3
*)
wenzelm@23465
     4
huffman@23472
     5
header {* Decision Procedure for Presburger Arithmetic *}
huffman@23472
     6
wenzelm@23465
     7
theory Presburger
haftmann@28402
     8
imports Groebner_Basis SetInterval
wenzelm@23465
     9
uses
haftmann@30656
    10
  "Tools/Qelim/qelim.ML"
haftmann@36792
    11
  "Tools/Qelim/cooper_procedure.ML"
wenzelm@23465
    12
  ("Tools/Qelim/cooper.ML")
wenzelm@23465
    13
begin
wenzelm@23465
    14
wenzelm@23465
    15
subsection{* The @{text "-\<infinity>"} and @{text "+\<infinity>"} Properties *}
wenzelm@23465
    16
wenzelm@23465
    17
lemma minf:
wenzelm@23465
    18
  "\<lbrakk>\<exists>(z ::'a::linorder).\<forall>x<z. P x = P' x; \<exists>z.\<forall>x<z. Q x = Q' x\<rbrakk> 
wenzelm@23465
    19
     \<Longrightarrow> \<exists>z.\<forall>x<z. (P x \<and> Q x) = (P' x \<and> Q' x)"
wenzelm@23465
    20
  "\<lbrakk>\<exists>(z ::'a::linorder).\<forall>x<z. P x = P' x; \<exists>z.\<forall>x<z. Q x = Q' x\<rbrakk> 
wenzelm@23465
    21
     \<Longrightarrow> \<exists>z.\<forall>x<z. (P x \<or> Q x) = (P' x \<or> Q' x)"
wenzelm@23465
    22
  "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x = t) = False"
wenzelm@23465
    23
  "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x \<noteq> t) = True"
wenzelm@23465
    24
  "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x < t) = True"
wenzelm@23465
    25
  "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x \<le> t) = True"
wenzelm@23465
    26
  "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x > t) = False"
wenzelm@23465
    27
  "\<exists>(z ::'a::{linorder}).\<forall>x<z.(x \<ge> t) = False"
wenzelm@46296
    28
  "\<exists>z.\<forall>(x::'b::{linorder,plus,Rings.dvd})<z. (d dvd x + s) = (d dvd x + s)"
wenzelm@46296
    29
  "\<exists>z.\<forall>(x::'b::{linorder,plus,Rings.dvd})<z. (\<not> d dvd x + s) = (\<not> d dvd x + s)"
wenzelm@23465
    30
  "\<exists>z.\<forall>x<z. F = F"
nipkow@45761
    31
  by ((erule exE, erule exE,rule_tac x="min z za" in exI,simp)+, (rule_tac x="t" in exI,fastforce)+) simp_all
wenzelm@23465
    32
wenzelm@23465
    33
lemma pinf:
wenzelm@23465
    34
  "\<lbrakk>\<exists>(z ::'a::linorder).\<forall>x>z. P x = P' x; \<exists>z.\<forall>x>z. Q x = Q' x\<rbrakk> 
wenzelm@23465
    35
     \<Longrightarrow> \<exists>z.\<forall>x>z. (P x \<and> Q x) = (P' x \<and> Q' x)"
wenzelm@23465
    36
  "\<lbrakk>\<exists>(z ::'a::linorder).\<forall>x>z. P x = P' x; \<exists>z.\<forall>x>z. Q x = Q' x\<rbrakk> 
wenzelm@23465
    37
     \<Longrightarrow> \<exists>z.\<forall>x>z. (P x \<or> Q x) = (P' x \<or> Q' x)"
wenzelm@23465
    38
  "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x = t) = False"
wenzelm@23465
    39
  "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x \<noteq> t) = True"
wenzelm@23465
    40
  "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x < t) = False"
wenzelm@23465
    41
  "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x \<le> t) = False"
wenzelm@23465
    42
  "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x > t) = True"
wenzelm@23465
    43
  "\<exists>(z ::'a::{linorder}).\<forall>x>z.(x \<ge> t) = True"
wenzelm@46296
    44
  "\<exists>z.\<forall>(x::'b::{linorder,plus,Rings.dvd})>z. (d dvd x + s) = (d dvd x + s)"
wenzelm@46296
    45
  "\<exists>z.\<forall>(x::'b::{linorder,plus,Rings.dvd})>z. (\<not> d dvd x + s) = (\<not> d dvd x + s)"
wenzelm@23465
    46
  "\<exists>z.\<forall>x>z. F = F"
nipkow@45761
    47
  by ((erule exE, erule exE,rule_tac x="max z za" in exI,simp)+,(rule_tac x="t" in exI,fastforce)+) simp_all
wenzelm@23465
    48
wenzelm@23465
    49
lemma inf_period:
wenzelm@23465
    50
  "\<lbrakk>\<forall>x k. P x = P (x - k*D); \<forall>x k. Q x = Q (x - k*D)\<rbrakk> 
wenzelm@23465
    51
    \<Longrightarrow> \<forall>x k. (P x \<and> Q x) = (P (x - k*D) \<and> Q (x - k*D))"
wenzelm@23465
    52
  "\<lbrakk>\<forall>x k. P x = P (x - k*D); \<forall>x k. Q x = Q (x - k*D)\<rbrakk> 
wenzelm@23465
    53
    \<Longrightarrow> \<forall>x k. (P x \<or> Q x) = (P (x - k*D) \<or> Q (x - k*D))"
haftmann@35050
    54
  "(d::'a::{comm_ring,Rings.dvd}) dvd D \<Longrightarrow> \<forall>x k. (d dvd x + t) = (d dvd (x - k*D) + t)"
haftmann@35050
    55
  "(d::'a::{comm_ring,Rings.dvd}) dvd D \<Longrightarrow> \<forall>x k. (\<not>d dvd x + t) = (\<not>d dvd (x - k*D) + t)"
wenzelm@23465
    56
  "\<forall>x k. F = F"
nipkow@29667
    57
apply (auto elim!: dvdE simp add: algebra_simps)
haftmann@27651
    58
unfolding mult_assoc [symmetric] left_distrib [symmetric] left_diff_distrib [symmetric]
chaieb@27668
    59
unfolding dvd_def mult_commute [of d] 
chaieb@27668
    60
by auto
wenzelm@23465
    61
huffman@23472
    62
subsection{* The A and B sets *}
wenzelm@23465
    63
lemma bset:
wenzelm@23465
    64
  "\<lbrakk>\<forall>x.(\<forall>j \<in> {1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> P x \<longrightarrow> P(x - D) ;
wenzelm@23465
    65
     \<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> Q x \<longrightarrow> Q(x - D)\<rbrakk> \<Longrightarrow> 
wenzelm@23465
    66
  \<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))"
wenzelm@23465
    67
  "\<lbrakk>\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> P x \<longrightarrow> P(x - D) ;
wenzelm@23465
    68
     \<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> Q x \<longrightarrow> Q(x - D)\<rbrakk> \<Longrightarrow> 
wenzelm@23465
    69
  \<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))"
wenzelm@23465
    70
  "\<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))"
wenzelm@23465
    71
  "\<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))"
wenzelm@23465
    72
  "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))"
wenzelm@23465
    73
  "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))"
wenzelm@23465
    74
  "\<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))"
wenzelm@23465
    75
  "\<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))"
wenzelm@23465
    76
  "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))"
wenzelm@23465
    77
  "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))"
wenzelm@23465
    78
  "\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j) \<longrightarrow> F \<longrightarrow> F"
wenzelm@23465
    79
proof (blast, blast)
wenzelm@23465
    80
  assume dp: "D > 0" and tB: "t - 1\<in> B"
wenzelm@23465
    81
  show "(\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>B. x \<noteq> b + j)\<longrightarrow> (x = t) \<longrightarrow> (x - D = t))" 
chaieb@27668
    82
    apply (rule allI, rule impI,erule ballE[where x="1"],erule ballE[where x="t - 1"]) 
chaieb@27668
    83
    apply algebra using dp tB by simp_all
wenzelm@23465
    84
next
wenzelm@23465
    85
  assume dp: "D > 0" and tB: "t \<in> B"
wenzelm@23465
    86
  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))" 
wenzelm@23465
    87
    apply (rule allI, rule impI,erule ballE[where x="D"],erule ballE[where x="t"])
chaieb@27668
    88
    apply algebra
wenzelm@23465
    89
    using dp tB by simp_all
wenzelm@23465
    90
next
wenzelm@23465
    91
  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
wenzelm@23465
    92
next
wenzelm@23465
    93
  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
wenzelm@23465
    94
next
wenzelm@23465
    95
  assume dp: "D > 0" and tB:"t \<in> B"
wenzelm@23465
    96
  {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"
wenzelm@23465
    97
    hence "x -t \<le> D" and "1 \<le> x - t" by simp+
wenzelm@23465
    98
      hence "\<exists>j \<in> {1 .. D}. x - t = j" by auto
nipkow@29667
    99
      hence "\<exists>j \<in> {1 .. D}. x = t + j" by (simp add: algebra_simps)
wenzelm@23465
   100
      with nob tB have "False" by simp}
wenzelm@23465
   101
  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
wenzelm@23465
   102
next
wenzelm@23465
   103
  assume dp: "D > 0" and tB:"t - 1\<in> B"
wenzelm@23465
   104
  {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"
wenzelm@23465
   105
    hence "x - (t - 1) \<le> D" and "1 \<le> x - (t - 1)" by simp+
wenzelm@23465
   106
      hence "\<exists>j \<in> {1 .. D}. x - (t - 1) = j" by auto
nipkow@29667
   107
      hence "\<exists>j \<in> {1 .. D}. x = (t - 1) + j" by (simp add: algebra_simps)
wenzelm@23465
   108
      with nob tB have "False" by simp}
wenzelm@23465
   109
  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
wenzelm@23465
   110
next
wenzelm@23465
   111
  assume d: "d dvd D"
chaieb@27668
   112
  {fix x assume H: "d dvd x + t" with d have "d dvd (x - D) + t" by algebra}
wenzelm@23465
   113
  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
wenzelm@23465
   114
next
wenzelm@23465
   115
  assume d: "d dvd D"
haftmann@27651
   116
  {fix x assume H: "\<not>(d dvd x + t)" with d have "\<not> d dvd (x - D) + t"
nipkow@29667
   117
      by (clarsimp simp add: dvd_def,erule_tac x= "ka + k" in allE,simp add: algebra_simps)}
wenzelm@23465
   118
  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
wenzelm@23465
   119
qed blast
wenzelm@23465
   120
wenzelm@23465
   121
lemma aset:
wenzelm@23465
   122
  "\<lbrakk>\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> P x \<longrightarrow> P(x + D) ;
wenzelm@23465
   123
     \<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> Q x \<longrightarrow> Q(x + D)\<rbrakk> \<Longrightarrow> 
wenzelm@23465
   124
  \<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))"
wenzelm@23465
   125
  "\<lbrakk>\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> P x \<longrightarrow> P(x + D) ;
wenzelm@23465
   126
     \<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> Q x \<longrightarrow> Q(x + D)\<rbrakk> \<Longrightarrow> 
wenzelm@23465
   127
  \<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))"
wenzelm@23465
   128
  "\<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))"
wenzelm@23465
   129
  "\<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))"
wenzelm@23465
   130
  "\<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))"
wenzelm@23465
   131
  "\<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))"
wenzelm@23465
   132
  "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))"
wenzelm@23465
   133
  "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))"
wenzelm@23465
   134
  "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))"
wenzelm@23465
   135
  "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))"
wenzelm@23465
   136
  "\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j) \<longrightarrow> F \<longrightarrow> F"
wenzelm@23465
   137
proof (blast, blast)
wenzelm@23465
   138
  assume dp: "D > 0" and tA: "t + 1 \<in> A"
wenzelm@23465
   139
  show "(\<forall>x.(\<forall>j\<in>{1 .. D}. \<forall>b\<in>A. x \<noteq> b - j)\<longrightarrow> (x = t) \<longrightarrow> (x + D = t))" 
wenzelm@23465
   140
    apply (rule allI, rule impI,erule ballE[where x="1"],erule ballE[where x="t + 1"])
wenzelm@23465
   141
    using dp tA by simp_all
wenzelm@23465
   142
next
wenzelm@23465
   143
  assume dp: "D > 0" and tA: "t \<in> A"
wenzelm@23465
   144
  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))" 
wenzelm@23465
   145
    apply (rule allI, rule impI,erule ballE[where x="D"],erule ballE[where x="t"])
wenzelm@23465
   146
    using dp tA by simp_all
wenzelm@23465
   147
next
wenzelm@23465
   148
  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
wenzelm@23465
   149
next
wenzelm@23465
   150
  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
wenzelm@23465
   151
next
wenzelm@23465
   152
  assume dp: "D > 0" and tA:"t \<in> A"
wenzelm@23465
   153
  {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"
wenzelm@23465
   154
    hence "t - x \<le> D" and "1 \<le> t - x" by simp+
wenzelm@23465
   155
      hence "\<exists>j \<in> {1 .. D}. t - x = j" by auto
nipkow@29667
   156
      hence "\<exists>j \<in> {1 .. D}. x = t - j" by (auto simp add: algebra_simps) 
wenzelm@23465
   157
      with nob tA have "False" by simp}
wenzelm@23465
   158
  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
wenzelm@23465
   159
next
wenzelm@23465
   160
  assume dp: "D > 0" and tA:"t + 1\<in> A"
wenzelm@23465
   161
  {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"
nipkow@29667
   162
    hence "(t + 1) - x \<le> D" and "1 \<le> (t + 1) - x" by (simp_all add: algebra_simps)
wenzelm@23465
   163
      hence "\<exists>j \<in> {1 .. D}. (t + 1) - x = j" by auto
nipkow@29667
   164
      hence "\<exists>j \<in> {1 .. D}. x = (t + 1) - j" by (auto simp add: algebra_simps)
wenzelm@23465
   165
      with nob tA have "False" by simp}
wenzelm@23465
   166
  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
wenzelm@23465
   167
next
wenzelm@23465
   168
  assume d: "d dvd D"
wenzelm@23465
   169
  {fix x assume H: "d dvd x + t" with d have "d dvd (x + D) + t"
nipkow@29667
   170
      by (clarsimp simp add: dvd_def,rule_tac x= "ka + k" in exI,simp add: algebra_simps)}
wenzelm@23465
   171
  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
wenzelm@23465
   172
next
wenzelm@23465
   173
  assume d: "d dvd D"
wenzelm@23465
   174
  {fix x assume H: "\<not>(d dvd x + t)" with d have "\<not>d dvd (x + D) + t"
nipkow@29667
   175
      by (clarsimp simp add: dvd_def,erule_tac x= "ka - k" in allE,simp add: algebra_simps)}
wenzelm@23465
   176
  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
wenzelm@23465
   177
qed blast
wenzelm@23465
   178
wenzelm@23465
   179
subsection{* Cooper's Theorem @{text "-\<infinity>"} and @{text "+\<infinity>"} Version *}
wenzelm@23465
   180
wenzelm@23465
   181
subsubsection{* First some trivial facts about periodic sets or predicates *}
wenzelm@23465
   182
lemma periodic_finite_ex:
wenzelm@23465
   183
  assumes dpos: "(0::int) < d" and modd: "ALL x k. P x = P(x - k*d)"
wenzelm@23465
   184
  shows "(EX x. P x) = (EX j : {1..d}. P j)"
wenzelm@23465
   185
  (is "?LHS = ?RHS")
wenzelm@23465
   186
proof
wenzelm@23465
   187
  assume ?LHS
wenzelm@23465
   188
  then obtain x where P: "P x" ..
wenzelm@23465
   189
  have "x mod d = x - (x div d)*d" by(simp add:zmod_zdiv_equality mult_ac eq_diff_eq)
wenzelm@23465
   190
  hence Pmod: "P x = P(x mod d)" using modd by simp
wenzelm@23465
   191
  show ?RHS
wenzelm@23465
   192
  proof (cases)
wenzelm@23465
   193
    assume "x mod d = 0"
wenzelm@23465
   194
    hence "P 0" using P Pmod by simp
wenzelm@23465
   195
    moreover have "P 0 = P(0 - (-1)*d)" using modd by blast
wenzelm@23465
   196
    ultimately have "P d" by simp
huffman@35208
   197
    moreover have "d : {1..d}" using dpos by simp
wenzelm@23465
   198
    ultimately show ?RHS ..
wenzelm@23465
   199
  next
wenzelm@23465
   200
    assume not0: "x mod d \<noteq> 0"
huffman@35208
   201
    have "P(x mod d)" using dpos P Pmod by simp
wenzelm@23465
   202
    moreover have "x mod d : {1..d}"
wenzelm@23465
   203
    proof -
wenzelm@23465
   204
      from dpos have "0 \<le> x mod d" by(rule pos_mod_sign)
wenzelm@23465
   205
      moreover from dpos have "x mod d < d" by(rule pos_mod_bound)
huffman@35208
   206
      ultimately show ?thesis using not0 by simp
wenzelm@23465
   207
    qed
wenzelm@23465
   208
    ultimately show ?RHS ..
wenzelm@23465
   209
  qed
wenzelm@23465
   210
qed auto
wenzelm@23465
   211
wenzelm@23465
   212
subsubsection{* The @{text "-\<infinity>"} Version*}
wenzelm@23465
   213
wenzelm@23465
   214
lemma decr_lemma: "0 < (d::int) \<Longrightarrow> x - (abs(x-z)+1) * d < z"
wenzelm@23465
   215
by(induct rule: int_gr_induct,simp_all add:int_distrib)
wenzelm@23465
   216
wenzelm@23465
   217
lemma incr_lemma: "0 < (d::int) \<Longrightarrow> z < x + (abs(x-z)+1) * d"
wenzelm@23465
   218
by(induct rule: int_gr_induct, simp_all add:int_distrib)
wenzelm@23465
   219
wenzelm@23465
   220
lemma decr_mult_lemma:
wenzelm@23465
   221
  assumes dpos: "(0::int) < d" and minus: "\<forall>x. P x \<longrightarrow> P(x - d)" and knneg: "0 <= k"
wenzelm@23465
   222
  shows "ALL x. P x \<longrightarrow> P(x - k*d)"
wenzelm@23465
   223
using knneg
wenzelm@23465
   224
proof (induct rule:int_ge_induct)
wenzelm@23465
   225
  case base thus ?case by simp
wenzelm@23465
   226
next
wenzelm@23465
   227
  case (step i)
wenzelm@23465
   228
  {fix x
wenzelm@23465
   229
    have "P x \<longrightarrow> P (x - i * d)" using step.hyps by blast
wenzelm@23465
   230
    also have "\<dots> \<longrightarrow> P(x - (i + 1) * d)" using minus[THEN spec, of "x - i * d"]
haftmann@35050
   231
      by (simp add: algebra_simps)
wenzelm@23465
   232
    ultimately have "P x \<longrightarrow> P(x - (i + 1) * d)" by blast}
wenzelm@23465
   233
  thus ?case ..
wenzelm@23465
   234
qed
wenzelm@23465
   235
wenzelm@23465
   236
lemma  minusinfinity:
wenzelm@23465
   237
  assumes dpos: "0 < d" and
wenzelm@23465
   238
    P1eqP1: "ALL x k. P1 x = P1(x - k*d)" and ePeqP1: "EX z::int. ALL x. x < z \<longrightarrow> (P x = P1 x)"
wenzelm@23465
   239
  shows "(EX x. P1 x) \<longrightarrow> (EX x. P x)"
wenzelm@23465
   240
proof
wenzelm@23465
   241
  assume eP1: "EX x. P1 x"
wenzelm@23465
   242
  then obtain x where P1: "P1 x" ..
wenzelm@23465
   243
  from ePeqP1 obtain z where P1eqP: "ALL x. x < z \<longrightarrow> (P x = P1 x)" ..
wenzelm@23465
   244
  let ?w = "x - (abs(x-z)+1) * d"
wenzelm@23465
   245
  from dpos have w: "?w < z" by(rule decr_lemma)
wenzelm@23465
   246
  have "P1 x = P1 ?w" using P1eqP1 by blast
wenzelm@23465
   247
  also have "\<dots> = P(?w)" using w P1eqP by blast
wenzelm@23465
   248
  finally have "P ?w" using P1 by blast
wenzelm@23465
   249
  thus "EX x. P x" ..
wenzelm@23465
   250
qed
wenzelm@23465
   251
wenzelm@23465
   252
lemma cpmi: 
wenzelm@23465
   253
  assumes dp: "0 < D" and p1:"\<exists>z. \<forall> x< z. P x = P' x"
wenzelm@23465
   254
  and nb:"\<forall>x.(\<forall> j\<in> {1..D}. \<forall>(b::int) \<in> B. x \<noteq> b+j) --> P (x) --> P (x - D)"
wenzelm@23465
   255
  and pd: "\<forall> x k. P' x = P' (x-k*D)"
wenzelm@23465
   256
  shows "(\<exists>x. P x) = ((\<exists> j\<in> {1..D} . P' j) | (\<exists> j \<in> {1..D}.\<exists> b\<in> B. P (b+j)))" 
wenzelm@23465
   257
         (is "?L = (?R1 \<or> ?R2)")
wenzelm@23465
   258
proof-
wenzelm@23465
   259
 {assume "?R2" hence "?L"  by blast}
wenzelm@23465
   260
 moreover
wenzelm@23465
   261
 {assume H:"?R1" hence "?L" using minusinfinity[OF dp pd p1] periodic_finite_ex[OF dp pd] by simp}
wenzelm@23465
   262
 moreover 
wenzelm@23465
   263
 { fix x
wenzelm@23465
   264
   assume P: "P x" and H: "\<not> ?R2"
wenzelm@23465
   265
   {fix y assume "\<not> (\<exists>j\<in>{1..D}. \<exists>b\<in>B. P (b + j))" and P: "P y"
wenzelm@23465
   266
     hence "~(EX (j::int) : {1..D}. EX (b::int) : B. y = b+j)" by auto
wenzelm@23465
   267
     with nb P  have "P (y - D)" by auto }
wenzelm@23465
   268
   hence "ALL x.~(EX (j::int) : {1..D}. EX (b::int) : B. P(b+j)) --> P (x) --> P (x - D)" by blast
wenzelm@23465
   269
   with H P have th: " \<forall>x. P x \<longrightarrow> P (x - D)" by auto
wenzelm@23465
   270
   from p1 obtain z where z: "ALL x. x < z --> (P x = P' x)" by blast
wenzelm@23465
   271
   let ?y = "x - (\<bar>x - z\<bar> + 1)*D"
wenzelm@23465
   272
   have zp: "0 <= (\<bar>x - z\<bar> + 1)" by arith
wenzelm@23465
   273
   from dp have yz: "?y < z" using decr_lemma[OF dp] by simp   
wenzelm@23465
   274
   from z[rule_format, OF yz] decr_mult_lemma[OF dp th zp, rule_format, OF P] have th2: " P' ?y" by auto
wenzelm@23465
   275
   with periodic_finite_ex[OF dp pd]
wenzelm@23465
   276
   have "?R1" by blast}
wenzelm@23465
   277
 ultimately show ?thesis by blast
wenzelm@23465
   278
qed
wenzelm@23465
   279
wenzelm@23465
   280
subsubsection {* The @{text "+\<infinity>"} Version*}
wenzelm@23465
   281
wenzelm@23465
   282
lemma  plusinfinity:
wenzelm@23465
   283
  assumes dpos: "(0::int) < d" and
wenzelm@23465
   284
    P1eqP1: "\<forall>x k. P' x = P'(x - k*d)" and ePeqP1: "\<exists> z. \<forall> x>z. P x = P' x"
wenzelm@23465
   285
  shows "(\<exists> x. P' x) \<longrightarrow> (\<exists> x. P x)"
wenzelm@23465
   286
proof
wenzelm@23465
   287
  assume eP1: "EX x. P' x"
wenzelm@23465
   288
  then obtain x where P1: "P' x" ..
wenzelm@23465
   289
  from ePeqP1 obtain z where P1eqP: "\<forall>x>z. P x = P' x" ..
wenzelm@23465
   290
  let ?w' = "x + (abs(x-z)+1) * d"
wenzelm@23465
   291
  let ?w = "x - (-(abs(x-z) + 1))*d"
nipkow@29667
   292
  have ww'[simp]: "?w = ?w'" by (simp add: algebra_simps)
wenzelm@23465
   293
  from dpos have w: "?w > z" by(simp only: ww' incr_lemma)
wenzelm@23465
   294
  hence "P' x = P' ?w" using P1eqP1 by blast
wenzelm@23465
   295
  also have "\<dots> = P(?w)" using w P1eqP by blast
wenzelm@23465
   296
  finally have "P ?w" using P1 by blast
wenzelm@23465
   297
  thus "EX x. P x" ..
wenzelm@23465
   298
qed
wenzelm@23465
   299
wenzelm@23465
   300
lemma incr_mult_lemma:
wenzelm@23465
   301
  assumes dpos: "(0::int) < d" and plus: "ALL x::int. P x \<longrightarrow> P(x + d)" and knneg: "0 <= k"
wenzelm@23465
   302
  shows "ALL x. P x \<longrightarrow> P(x + k*d)"
wenzelm@23465
   303
using knneg
wenzelm@23465
   304
proof (induct rule:int_ge_induct)
wenzelm@23465
   305
  case base thus ?case by simp
wenzelm@23465
   306
next
wenzelm@23465
   307
  case (step i)
wenzelm@23465
   308
  {fix x
wenzelm@23465
   309
    have "P x \<longrightarrow> P (x + i * d)" using step.hyps by blast
wenzelm@23465
   310
    also have "\<dots> \<longrightarrow> P(x + (i + 1) * d)" using plus[THEN spec, of "x + i * d"]
huffman@45637
   311
      by (simp add:int_distrib add_ac)
wenzelm@23465
   312
    ultimately have "P x \<longrightarrow> P(x + (i + 1) * d)" by blast}
wenzelm@23465
   313
  thus ?case ..
wenzelm@23465
   314
qed
wenzelm@23465
   315
wenzelm@23465
   316
lemma cppi: 
wenzelm@23465
   317
  assumes dp: "0 < D" and p1:"\<exists>z. \<forall> x> z. P x = P' x"
wenzelm@23465
   318
  and nb:"\<forall>x.(\<forall> j\<in> {1..D}. \<forall>(b::int) \<in> A. x \<noteq> b - j) --> P (x) --> P (x + D)"
wenzelm@23465
   319
  and pd: "\<forall> x k. P' x= P' (x-k*D)"
wenzelm@23465
   320
  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)")
wenzelm@23465
   321
proof-
wenzelm@23465
   322
 {assume "?R2" hence "?L"  by blast}
wenzelm@23465
   323
 moreover
wenzelm@23465
   324
 {assume H:"?R1" hence "?L" using plusinfinity[OF dp pd p1] periodic_finite_ex[OF dp pd] by simp}
wenzelm@23465
   325
 moreover 
wenzelm@23465
   326
 { fix x
wenzelm@23465
   327
   assume P: "P x" and H: "\<not> ?R2"
wenzelm@23465
   328
   {fix y assume "\<not> (\<exists>j\<in>{1..D}. \<exists>b\<in>A. P (b - j))" and P: "P y"
wenzelm@23465
   329
     hence "~(EX (j::int) : {1..D}. EX (b::int) : A. y = b - j)" by auto
wenzelm@23465
   330
     with nb P  have "P (y + D)" by auto }
wenzelm@23465
   331
   hence "ALL x.~(EX (j::int) : {1..D}. EX (b::int) : A. P(b-j)) --> P (x) --> P (x + D)" by blast
wenzelm@23465
   332
   with H P have th: " \<forall>x. P x \<longrightarrow> P (x + D)" by auto
wenzelm@23465
   333
   from p1 obtain z where z: "ALL x. x > z --> (P x = P' x)" by blast
wenzelm@23465
   334
   let ?y = "x + (\<bar>x - z\<bar> + 1)*D"
wenzelm@23465
   335
   have zp: "0 <= (\<bar>x - z\<bar> + 1)" by arith
wenzelm@23465
   336
   from dp have yz: "?y > z" using incr_lemma[OF dp] by simp
wenzelm@23465
   337
   from z[rule_format, OF yz] incr_mult_lemma[OF dp th zp, rule_format, OF P] have th2: " P' ?y" by auto
wenzelm@23465
   338
   with periodic_finite_ex[OF dp pd]
wenzelm@23465
   339
   have "?R1" by blast}
wenzelm@23465
   340
 ultimately show ?thesis by blast
wenzelm@23465
   341
qed
wenzelm@23465
   342
wenzelm@23465
   343
lemma simp_from_to: "{i..j::int} = (if j < i then {} else insert i {i+1..j})"
wenzelm@23465
   344
apply(simp add:atLeastAtMost_def atLeast_def atMost_def)
nipkow@45761
   345
apply(fastforce)
wenzelm@23465
   346
done
wenzelm@23465
   347
haftmann@35050
   348
theorem unity_coeff_ex: "(\<exists>(x::'a::{semiring_0,Rings.dvd}). P (l * x)) \<equiv> (\<exists>x. l dvd (x + 0) \<and> P x)"
haftmann@27651
   349
  apply (rule eq_reflection [symmetric])
wenzelm@23465
   350
  apply (rule iffI)
wenzelm@23465
   351
  defer
wenzelm@23465
   352
  apply (erule exE)
wenzelm@23465
   353
  apply (rule_tac x = "l * x" in exI)
wenzelm@23465
   354
  apply (simp add: dvd_def)
haftmann@27651
   355
  apply (rule_tac x = x in exI, simp)
wenzelm@23465
   356
  apply (erule exE)
wenzelm@23465
   357
  apply (erule conjE)
haftmann@27651
   358
  apply simp
wenzelm@23465
   359
  apply (erule dvdE)
wenzelm@23465
   360
  apply (rule_tac x = k in exI)
wenzelm@23465
   361
  apply simp
wenzelm@23465
   362
  done
wenzelm@23465
   363
wenzelm@23465
   364
lemma zdvd_mono: assumes not0: "(k::int) \<noteq> 0"
wenzelm@23465
   365
shows "((m::int) dvd t) \<equiv> (k*m dvd k*t)" 
wenzelm@23465
   366
  using not0 by (simp add: dvd_def)
wenzelm@23465
   367
wenzelm@23465
   368
lemma uminus_dvd_conv: "(d dvd (t::int)) \<equiv> (-d dvd t)" "(d dvd (t::int)) \<equiv> (d dvd -t)"
wenzelm@23465
   369
  by simp_all
haftmann@32553
   370
wenzelm@23465
   371
text {* \bigskip Theorems for transforming predicates on nat to predicates on @{text int}*}
haftmann@32553
   372
wenzelm@23465
   373
lemma zdiff_int_split: "P (int (x - y)) =
wenzelm@23465
   374
  ((y \<le> x \<longrightarrow> P (int x - int y)) \<and> (x < y \<longrightarrow> P 0))"
haftmann@36794
   375
  by (cases "y \<le> x") (simp_all add: zdiff_int)
wenzelm@23465
   376
wenzelm@23465
   377
text {*
wenzelm@23465
   378
  \medskip Specific instances of congruence rules, to prevent
wenzelm@23465
   379
  simplifier from looping. *}
wenzelm@23465
   380
huffman@47978
   381
theorem imp_le_cong:
huffman@47978
   382
  "\<lbrakk>x = x'; 0 \<le> x' \<Longrightarrow> P = P'\<rbrakk> \<Longrightarrow> (0 \<le> (x::int) \<longrightarrow> P) = (0 \<le> x' \<longrightarrow> P')"
huffman@47978
   383
  by simp
wenzelm@23465
   384
huffman@47978
   385
theorem conj_le_cong:
huffman@47978
   386
  "\<lbrakk>x = x'; 0 \<le> x' \<Longrightarrow> P = P'\<rbrakk> \<Longrightarrow> (0 \<le> (x::int) \<and> P) = (0 \<le> x' \<and> P')"
wenzelm@23465
   387
  by (simp cong: conj_cong)
haftmann@36793
   388
haftmann@36792
   389
use "Tools/Qelim/cooper.ML"
wenzelm@23465
   390
haftmann@36793
   391
setup Cooper.setup
wenzelm@23465
   392
haftmann@36798
   393
method_setup presburger = "Cooper.method" "Cooper's algorithm for Presburger arithmetic"
wenzelm@23465
   394
haftmann@36792
   395
declare dvd_eq_mod_eq_0[symmetric, presburger]
haftmann@36792
   396
declare mod_1[presburger] 
haftmann@36792
   397
declare mod_0[presburger]
haftmann@36792
   398
declare mod_by_1[presburger]
haftmann@36792
   399
declare mod_self[presburger]
haftmann@36792
   400
declare mod_by_0[presburger]
haftmann@36792
   401
declare mod_div_trivial[presburger]
haftmann@36792
   402
declare div_mod_equality2[presburger]
haftmann@36792
   403
declare div_mod_equality[presburger]
haftmann@36792
   404
declare mod_div_equality2[presburger]
haftmann@36792
   405
declare mod_div_equality[presburger]
haftmann@36792
   406
declare mod_mult_self1[presburger]
haftmann@36792
   407
declare mod_mult_self2[presburger]
haftmann@36792
   408
declare zdiv_zmod_equality2[presburger]
haftmann@36792
   409
declare zdiv_zmod_equality[presburger]
haftmann@36792
   410
declare mod2_Suc_Suc[presburger]
haftmann@36792
   411
lemma [presburger]: "(a::int) div 0 = 0" and [presburger]: "a mod 0 = a"
haftmann@36792
   412
by simp_all
haftmann@36792
   413
chaieb@27668
   414
lemma [presburger, algebra]: "m mod 2 = (1::nat) \<longleftrightarrow> \<not> 2 dvd m " by presburger
chaieb@27668
   415
lemma [presburger, algebra]: "m mod 2 = Suc 0 \<longleftrightarrow> \<not> 2 dvd m " by presburger
chaieb@27668
   416
lemma [presburger, algebra]: "m mod (Suc (Suc 0)) = (1::nat) \<longleftrightarrow> \<not> 2 dvd m " by presburger
chaieb@27668
   417
lemma [presburger, algebra]: "m mod (Suc (Suc 0)) = Suc 0 \<longleftrightarrow> \<not> 2 dvd m " by presburger
chaieb@27668
   418
lemma [presburger, algebra]: "m mod 2 = (1::int) \<longleftrightarrow> \<not> 2 dvd m " by presburger
wenzelm@23465
   419
wenzelm@23465
   420
end