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