src/HOL/Decision_Procs/Commutative_Ring.thy
author haftmann
Thu Oct 30 21:02:01 2014 +0100 (2014-10-30)
changeset 58834 773b378d9313
parent 58770 ae5e9b4f8daf
child 58889 5b7a9633cfa8
permissions -rw-r--r--
more simp rules concerning dvd and even/odd
haftmann@28952
     1
(*  Author:     Bernhard Haeupler
wenzelm@17516
     2
wenzelm@17516
     3
Proving equalities in commutative rings done "right" in Isabelle/HOL.
wenzelm@17516
     4
*)
wenzelm@17516
     5
wenzelm@17516
     6
header {* Proving equalities in commutative rings *}
wenzelm@17516
     7
wenzelm@17516
     8
theory Commutative_Ring
haftmann@58770
     9
imports Main
wenzelm@17516
    10
begin
wenzelm@17516
    11
wenzelm@17516
    12
text {* Syntax of multivariate polynomials (pol) and polynomial expressions. *}
wenzelm@17516
    13
blanchet@58310
    14
datatype 'a pol =
wenzelm@17516
    15
    Pc 'a
wenzelm@17516
    16
  | Pinj nat "'a pol"
wenzelm@17516
    17
  | PX "'a pol" nat "'a pol"
wenzelm@17516
    18
blanchet@58310
    19
datatype 'a polex =
wenzelm@20622
    20
    Pol "'a pol"
wenzelm@17516
    21
  | Add "'a polex" "'a polex"
wenzelm@17516
    22
  | Sub "'a polex" "'a polex"
wenzelm@17516
    23
  | Mul "'a polex" "'a polex"
wenzelm@17516
    24
  | Pow "'a polex" nat
wenzelm@17516
    25
  | Neg "'a polex"
wenzelm@17516
    26
wenzelm@17516
    27
text {* Interpretation functions for the shadow syntax. *}
wenzelm@17516
    28
wenzelm@55754
    29
primrec Ipol :: "'a::{comm_ring_1} list \<Rightarrow> 'a pol \<Rightarrow> 'a"
haftmann@22742
    30
where
haftmann@22742
    31
    "Ipol l (Pc c) = c"
haftmann@22742
    32
  | "Ipol l (Pinj i P) = Ipol (drop i l) P"
haftmann@22742
    33
  | "Ipol l (PX P x Q) = Ipol l P * (hd l)^x + Ipol (drop 1 l) Q"
wenzelm@17516
    34
wenzelm@55754
    35
primrec Ipolex :: "'a::{comm_ring_1} list \<Rightarrow> 'a polex \<Rightarrow> 'a"
haftmann@22742
    36
where
haftmann@22742
    37
    "Ipolex l (Pol P) = Ipol l P"
haftmann@22742
    38
  | "Ipolex l (Add P Q) = Ipolex l P + Ipolex l Q"
haftmann@22742
    39
  | "Ipolex l (Sub P Q) = Ipolex l P - Ipolex l Q"
haftmann@22742
    40
  | "Ipolex l (Mul P Q) = Ipolex l P * Ipolex l Q"
haftmann@22742
    41
  | "Ipolex l (Pow p n) = Ipolex l p ^ n"
haftmann@22742
    42
  | "Ipolex l (Neg P) = - Ipolex l P"
wenzelm@17516
    43
wenzelm@17516
    44
text {* Create polynomial normalized polynomials given normalized inputs. *}
wenzelm@17516
    45
wenzelm@55754
    46
definition mkPinj :: "nat \<Rightarrow> 'a pol \<Rightarrow> 'a pol"
wenzelm@55754
    47
where
wenzelm@19736
    48
  "mkPinj x P = (case P of
wenzelm@17516
    49
    Pc c \<Rightarrow> Pc c |
wenzelm@17516
    50
    Pinj y P \<Rightarrow> Pinj (x + y) P |
wenzelm@17516
    51
    PX p1 y p2 \<Rightarrow> Pinj x P)"
wenzelm@17516
    52
wenzelm@55754
    53
definition mkPX :: "'a::comm_ring pol \<Rightarrow> nat \<Rightarrow> 'a pol \<Rightarrow> 'a pol"
wenzelm@55754
    54
where
wenzelm@55754
    55
  "mkPX P i Q =
wenzelm@55754
    56
    (case P of
wenzelm@55754
    57
      Pc c \<Rightarrow> if c = 0 then mkPinj 1 Q else PX P i Q
wenzelm@55754
    58
    | Pinj j R \<Rightarrow> PX P i Q
wenzelm@55754
    59
    | PX P2 i2 Q2 \<Rightarrow> if Q2 = Pc 0 then PX P2 (i + i2) Q else PX P i Q)"
wenzelm@17516
    60
wenzelm@17516
    61
text {* Defining the basic ring operations on normalized polynomials *}
wenzelm@17516
    62
blanchet@58259
    63
lemma pol_size_nz[simp]: "size (p :: 'a pol) \<noteq> 0"
blanchet@58259
    64
  by (cases p) simp_all
blanchet@58259
    65
wenzelm@55754
    66
function add :: "'a::comm_ring pol \<Rightarrow> 'a pol \<Rightarrow> 'a pol"  (infixl "\<oplus>" 65)
haftmann@22742
    67
where
wenzelm@55754
    68
  "Pc a \<oplus> Pc b = Pc (a + b)"
wenzelm@55754
    69
| "Pc c \<oplus> Pinj i P = Pinj i (P \<oplus> Pc c)"
wenzelm@55754
    70
| "Pinj i P \<oplus> Pc c = Pinj i (P \<oplus> Pc c)"
wenzelm@55754
    71
| "Pc c \<oplus> PX P i Q = PX P i (Q \<oplus> Pc c)"
wenzelm@55754
    72
| "PX P i Q \<oplus> Pc c = PX P i (Q \<oplus> Pc c)"
wenzelm@55754
    73
| "Pinj x P \<oplus> Pinj y Q =
wenzelm@55754
    74
    (if x = y then mkPinj x (P \<oplus> Q)
wenzelm@55754
    75
     else (if x > y then mkPinj y (Pinj (x - y) P \<oplus> Q)
wenzelm@55754
    76
       else mkPinj x (Pinj (y - x) Q \<oplus> P)))"
wenzelm@55754
    77
| "Pinj x P \<oplus> PX Q y R =
wenzelm@55754
    78
    (if x = 0 then P \<oplus> PX Q y R
wenzelm@55754
    79
     else (if x = 1 then PX Q y (R \<oplus> P)
wenzelm@55754
    80
       else PX Q y (R \<oplus> Pinj (x - 1) P)))"
wenzelm@55754
    81
| "PX P x R \<oplus> Pinj y Q =
wenzelm@55754
    82
    (if y = 0 then PX P x R \<oplus> Q
wenzelm@55754
    83
     else (if y = 1 then PX P x (R \<oplus> Q)
wenzelm@55754
    84
       else PX P x (R \<oplus> Pinj (y - 1) Q)))"
wenzelm@55754
    85
| "PX P1 x P2 \<oplus> PX Q1 y Q2 =
wenzelm@55754
    86
    (if x = y then mkPX (P1 \<oplus> Q1) x (P2 \<oplus> Q2)
wenzelm@55754
    87
     else (if x > y then mkPX (PX P1 (x - y) (Pc 0) \<oplus> Q1) y (P2 \<oplus> Q2)
wenzelm@55754
    88
       else mkPX (PX Q1 (y-x) (Pc 0) \<oplus> P1) x (P2 \<oplus> Q2)))"
haftmann@22742
    89
by pat_completeness auto
haftmann@22742
    90
termination by (relation "measure (\<lambda>(x, y). size x + size y)") auto
wenzelm@17516
    91
wenzelm@55754
    92
function mul :: "'a::{comm_ring} pol \<Rightarrow> 'a pol \<Rightarrow> 'a pol"  (infixl "\<otimes>" 70)
haftmann@22742
    93
where
wenzelm@55754
    94
  "Pc a \<otimes> Pc b = Pc (a * b)"
wenzelm@55754
    95
| "Pc c \<otimes> Pinj i P =
wenzelm@55754
    96
    (if c = 0 then Pc 0 else mkPinj i (P \<otimes> Pc c))"
wenzelm@55754
    97
| "Pinj i P \<otimes> Pc c =
wenzelm@55754
    98
    (if c = 0 then Pc 0 else mkPinj i (P \<otimes> Pc c))"
wenzelm@55754
    99
| "Pc c \<otimes> PX P i Q =
wenzelm@55754
   100
    (if c = 0 then Pc 0 else mkPX (P \<otimes> Pc c) i (Q \<otimes> Pc c))"
wenzelm@55754
   101
| "PX P i Q \<otimes> Pc c =
wenzelm@55754
   102
    (if c = 0 then Pc 0 else mkPX (P \<otimes> Pc c) i (Q \<otimes> Pc c))"
wenzelm@55754
   103
| "Pinj x P \<otimes> Pinj y Q =
wenzelm@55754
   104
    (if x = y then mkPinj x (P \<otimes> Q) else
wenzelm@55754
   105
       (if x > y then mkPinj y (Pinj (x-y) P \<otimes> Q)
wenzelm@55754
   106
         else mkPinj x (Pinj (y - x) Q \<otimes> P)))"
wenzelm@55754
   107
| "Pinj x P \<otimes> PX Q y R =
wenzelm@55754
   108
    (if x = 0 then P \<otimes> PX Q y R else
wenzelm@55754
   109
       (if x = 1 then mkPX (Pinj x P \<otimes> Q) y (R \<otimes> P)
wenzelm@55754
   110
         else mkPX (Pinj x P \<otimes> Q) y (R \<otimes> Pinj (x - 1) P)))"
wenzelm@55754
   111
| "PX P x R \<otimes> Pinj y Q =
wenzelm@55754
   112
    (if y = 0 then PX P x R \<otimes> Q else
wenzelm@55754
   113
       (if y = 1 then mkPX (Pinj y Q \<otimes> P) x (R \<otimes> Q)
wenzelm@55754
   114
         else mkPX (Pinj y Q \<otimes> P) x (R \<otimes> Pinj (y - 1) Q)))"
wenzelm@55754
   115
| "PX P1 x P2 \<otimes> PX Q1 y Q2 =
wenzelm@55754
   116
    mkPX (P1 \<otimes> Q1) (x + y) (P2 \<otimes> Q2) \<oplus>
wenzelm@55754
   117
      (mkPX (P1 \<otimes> mkPinj 1 Q2) x (Pc 0) \<oplus>
wenzelm@55754
   118
        (mkPX (Q1 \<otimes> mkPinj 1 P2) y (Pc 0)))"
haftmann@22742
   119
by pat_completeness auto
haftmann@22742
   120
termination by (relation "measure (\<lambda>(x, y). size x + size y)")
haftmann@22742
   121
  (auto simp add: mkPinj_def split: pol.split)
wenzelm@17516
   122
wenzelm@17516
   123
text {* Negation*}
wenzelm@55754
   124
primrec neg :: "'a::{comm_ring} pol \<Rightarrow> 'a pol"
haftmann@22742
   125
where
wenzelm@55754
   126
  "neg (Pc c) = Pc (-c)"
wenzelm@55754
   127
| "neg (Pinj i P) = Pinj i (neg P)"
wenzelm@55754
   128
| "neg (PX P x Q) = PX (neg P) x (neg Q)"
wenzelm@17516
   129
wenzelm@17516
   130
text {* Substraction *}
wenzelm@55754
   131
definition sub :: "'a::{comm_ring} pol \<Rightarrow> 'a pol \<Rightarrow> 'a pol"  (infixl "\<ominus>" 65)
wenzelm@55754
   132
  where "sub P Q = P \<oplus> neg Q"
wenzelm@17516
   133
wenzelm@17516
   134
text {* Square for Fast Exponentation *}
wenzelm@55754
   135
primrec sqr :: "'a::{comm_ring_1} pol \<Rightarrow> 'a pol"
haftmann@22742
   136
where
wenzelm@55754
   137
  "sqr (Pc c) = Pc (c * c)"
wenzelm@55754
   138
| "sqr (Pinj i P) = mkPinj i (sqr P)"
wenzelm@55754
   139
| "sqr (PX A x B) =
wenzelm@55754
   140
    mkPX (sqr A) (x + x) (sqr B) \<oplus> mkPX (Pc (1 + 1) \<otimes> A \<otimes> mkPinj 1 B) x (Pc 0)"
wenzelm@17516
   141
wenzelm@17516
   142
text {* Fast Exponentation *}
haftmann@58710
   143
wenzelm@55754
   144
fun pow :: "nat \<Rightarrow> 'a::{comm_ring_1} pol \<Rightarrow> 'a pol"
haftmann@22742
   145
where
haftmann@58710
   146
  pow_if [simp del]: "pow n P =
haftmann@22742
   147
   (if n = 0 then Pc 1 else if even n then pow (n div 2) (sqr P)
haftmann@22742
   148
    else P \<otimes> pow (n div 2) (sqr P))"
wenzelm@17516
   149
haftmann@58710
   150
lemma pow_simps [simp]:    
haftmann@58710
   151
  "pow 0 P = Pc 1"
haftmann@58712
   152
  "pow (2 * n) P = pow n (sqr P)"
haftmann@58712
   153
  "pow (Suc (2 * n)) P = P \<otimes> pow n (sqr P)"
haftmann@58710
   154
  by (simp_all add: pow_if)
wenzelm@17516
   155
haftmann@58712
   156
lemma even_pow:
haftmann@58712
   157
  "even n \<Longrightarrow> pow n P = pow (n div 2) (sqr P)"
haftmann@58712
   158
  by (erule evenE) simp
haftmann@58712
   159
haftmann@58712
   160
lemma odd_pow:
haftmann@58712
   161
  "odd n \<Longrightarrow> pow n P = P \<otimes> pow (n div 2) (sqr P)"
haftmann@58712
   162
  by (erule oddE) simp
haftmann@58712
   163
haftmann@58712
   164
  
wenzelm@17516
   165
text {* Normalization of polynomial expressions *}
wenzelm@17516
   166
wenzelm@55754
   167
primrec norm :: "'a::{comm_ring_1} polex \<Rightarrow> 'a pol"
haftmann@22742
   168
where
wenzelm@55754
   169
  "norm (Pol P) = P"
wenzelm@55754
   170
| "norm (Add P Q) = norm P \<oplus> norm Q"
wenzelm@55754
   171
| "norm (Sub P Q) = norm P \<ominus> norm Q"
wenzelm@55754
   172
| "norm (Mul P Q) = norm P \<otimes> norm Q"
wenzelm@55754
   173
| "norm (Pow P n) = pow n (norm P)"
wenzelm@55754
   174
| "norm (Neg P) = neg (norm P)"
wenzelm@17516
   175
wenzelm@17516
   176
text {* mkPinj preserve semantics *}
wenzelm@17516
   177
lemma mkPinj_ci: "Ipol l (mkPinj a B) = Ipol l (Pinj a B)"
nipkow@29667
   178
  by (induct B) (auto simp add: mkPinj_def algebra_simps)
wenzelm@17516
   179
wenzelm@17516
   180
text {* mkPX preserves semantics *}
wenzelm@17516
   181
lemma mkPX_ci: "Ipol l (mkPX A b C) = Ipol l (PX A b C)"
nipkow@29667
   182
  by (cases A) (auto simp add: mkPX_def mkPinj_ci power_add algebra_simps)
wenzelm@17516
   183
wenzelm@17516
   184
text {* Correctness theorems for the implemented operations *}
wenzelm@17516
   185
wenzelm@17516
   186
text {* Negation *}
wenzelm@20622
   187
lemma neg_ci: "Ipol l (neg P) = -(Ipol l P)"
wenzelm@20622
   188
  by (induct P arbitrary: l) auto
wenzelm@17516
   189
wenzelm@17516
   190
text {* Addition *}
haftmann@22742
   191
lemma add_ci: "Ipol l (P \<oplus> Q) = Ipol l P + Ipol l Q"
wenzelm@20622
   192
proof (induct P Q arbitrary: l rule: add.induct)
wenzelm@17516
   193
  case (6 x P y Q)
wenzelm@17516
   194
  show ?case
wenzelm@17516
   195
  proof (rule linorder_cases)
wenzelm@17516
   196
    assume "x < y"
nipkow@29667
   197
    with 6 show ?case by (simp add: mkPinj_ci algebra_simps)
wenzelm@17516
   198
  next
wenzelm@17516
   199
    assume "x = y"
wenzelm@17516
   200
    with 6 show ?case by (simp add: mkPinj_ci)
wenzelm@17516
   201
  next
wenzelm@17516
   202
    assume "x > y"
nipkow@29667
   203
    with 6 show ?case by (simp add: mkPinj_ci algebra_simps)
wenzelm@17516
   204
  qed
wenzelm@17516
   205
next
wenzelm@17516
   206
  case (7 x P Q y R)
wenzelm@17516
   207
  have "x = 0 \<or> x = 1 \<or> x > 1" by arith
wenzelm@17516
   208
  moreover
wenzelm@17516
   209
  { assume "x = 0" with 7 have ?case by simp }
wenzelm@17516
   210
  moreover
nipkow@29667
   211
  { assume "x = 1" with 7 have ?case by (simp add: algebra_simps) }
wenzelm@17516
   212
  moreover
wenzelm@17516
   213
  { assume "x > 1" from 7 have ?case by (cases x) simp_all }
wenzelm@17516
   214
  ultimately show ?case by blast
wenzelm@17516
   215
next
wenzelm@17516
   216
  case (8 P x R y Q)
wenzelm@17516
   217
  have "y = 0 \<or> y = 1 \<or> y > 1" by arith
wenzelm@17516
   218
  moreover
wenzelm@17516
   219
  { assume "y = 0" with 8 have ?case by simp }
wenzelm@17516
   220
  moreover
wenzelm@17516
   221
  { assume "y = 1" with 8 have ?case by simp }
wenzelm@17516
   222
  moreover
wenzelm@17516
   223
  { assume "y > 1" with 8 have ?case by simp }
wenzelm@17516
   224
  ultimately show ?case by blast
wenzelm@17516
   225
next
wenzelm@17516
   226
  case (9 P1 x P2 Q1 y Q2)
wenzelm@17516
   227
  show ?case
wenzelm@17516
   228
  proof (rule linorder_cases)
wenzelm@17516
   229
    assume a: "x < y" hence "EX d. d + x = y" by arith
nipkow@29667
   230
    with 9 a show ?case by (auto simp add: mkPX_ci power_add algebra_simps)
wenzelm@17516
   231
  next
wenzelm@17516
   232
    assume a: "y < x" hence "EX d. d + y = x" by arith
nipkow@29667
   233
    with 9 a show ?case by (auto simp add: power_add mkPX_ci algebra_simps)
wenzelm@17516
   234
  next
wenzelm@17516
   235
    assume "x = y"
nipkow@29667
   236
    with 9 show ?case by (simp add: mkPX_ci algebra_simps)
wenzelm@17516
   237
  qed
nipkow@29667
   238
qed (auto simp add: algebra_simps)
wenzelm@17516
   239
wenzelm@17516
   240
text {* Multiplication *}
haftmann@22742
   241
lemma mul_ci: "Ipol l (P \<otimes> Q) = Ipol l P * Ipol l Q"
wenzelm@20622
   242
  by (induct P Q arbitrary: l rule: mul.induct)
nipkow@29667
   243
    (simp_all add: mkPX_ci mkPinj_ci algebra_simps add_ci power_add)
wenzelm@17516
   244
wenzelm@17516
   245
text {* Substraction *}
haftmann@22742
   246
lemma sub_ci: "Ipol l (P \<ominus> Q) = Ipol l P - Ipol l Q"
wenzelm@17516
   247
  by (simp add: add_ci neg_ci sub_def)
wenzelm@17516
   248
wenzelm@17516
   249
text {* Square *}
haftmann@22742
   250
lemma sqr_ci: "Ipol ls (sqr P) = Ipol ls P * Ipol ls P"
haftmann@22742
   251
  by (induct P arbitrary: ls)
nipkow@29667
   252
    (simp_all add: add_ci mkPinj_ci mkPX_ci mul_ci algebra_simps power_add)
wenzelm@17516
   253
wenzelm@17516
   254
text {* Power *}
haftmann@22742
   255
lemma pow_ci: "Ipol ls (pow n P) = Ipol ls P ^ n"
haftmann@58712
   256
proof (induct n arbitrary: P rule: less_induct)
haftmann@58712
   257
  case (less k)
wenzelm@17516
   258
  show ?case
haftmann@58712
   259
  proof (cases "k = 0")
haftmann@58712
   260
    case True then show ?thesis by simp
wenzelm@20622
   261
  next
haftmann@58712
   262
    case False then have "k > 0" by simp
haftmann@58712
   263
    then have "k div 2 < k" by arith
haftmann@58712
   264
    with less have *: "Ipol ls (pow (k div 2) (sqr P)) = Ipol ls (sqr P) ^ (k div 2)"
haftmann@58712
   265
      by simp
wenzelm@17516
   266
    show ?thesis
haftmann@58712
   267
    proof (cases "even k")
haftmann@58712
   268
      case True with * show ?thesis
haftmann@58712
   269
        by (simp add: even_pow sqr_ci power_mult_distrib power_add [symmetric] mult_2 [symmetric] even_two_times_div_two)
wenzelm@17516
   270
    next
haftmann@58712
   271
      case False with * show ?thesis
haftmann@58834
   272
        by (simp add: odd_pow mul_ci sqr_ci power_mult_distrib power_add [symmetric] mult_2 [symmetric] power_Suc [symmetric])
wenzelm@17516
   273
    qed
wenzelm@17516
   274
  qed
wenzelm@17516
   275
qed
wenzelm@17516
   276
wenzelm@17516
   277
text {* Normalization preserves semantics  *}
wenzelm@20622
   278
lemma norm_ci: "Ipolex l Pe = Ipol l (norm Pe)"
wenzelm@17516
   279
  by (induct Pe) (simp_all add: add_ci sub_ci mul_ci neg_ci pow_ci)
wenzelm@17516
   280
wenzelm@17516
   281
text {* Reflection lemma: Key to the (incomplete) decision procedure *}
wenzelm@17516
   282
lemma norm_eq:
wenzelm@20622
   283
  assumes "norm P1 = norm P2"
wenzelm@17516
   284
  shows "Ipolex l P1 = Ipolex l P2"
wenzelm@17516
   285
proof -
wenzelm@41807
   286
  from assms have "Ipol l (norm P1) = Ipol l (norm P2)" by simp
wenzelm@20622
   287
  then show ?thesis by (simp only: norm_ci)
wenzelm@17516
   288
qed
wenzelm@17516
   289
wenzelm@17516
   290
wenzelm@48891
   291
ML_file "commutative_ring_tac.ML"
wenzelm@47432
   292
wenzelm@47432
   293
method_setup comm_ring = {*
wenzelm@47432
   294
  Scan.succeed (SIMPLE_METHOD' o Commutative_Ring_Tac.tac)
wenzelm@47432
   295
*} "reflective decision procedure for equalities over commutative rings"
wenzelm@17516
   296
wenzelm@17516
   297
end