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