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