src/HOL/Decision_Procs/Commutative_Ring.thy
author huffman
Sun Apr 01 16:09:58 2012 +0200 (2012-04-01)
changeset 47255 30a1692557b0
parent 41807 ab5d2d81f9fb
child 47432 e1576d13e933
permissions -rw-r--r--
removed Nat_Numeral.thy, moving all theorems elsewhere
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@33356
     9
imports Main Parity
haftmann@33356
    10
uses ("commutative_ring_tac.ML")
wenzelm@17516
    11
begin
wenzelm@17516
    12
wenzelm@17516
    13
text {* Syntax of multivariate polynomials (pol) and polynomial expressions. *}
wenzelm@17516
    14
wenzelm@17516
    15
datatype 'a pol =
wenzelm@17516
    16
    Pc 'a
wenzelm@17516
    17
  | Pinj nat "'a pol"
wenzelm@17516
    18
  | PX "'a pol" nat "'a pol"
wenzelm@17516
    19
wenzelm@17516
    20
datatype 'a polex =
wenzelm@20622
    21
    Pol "'a pol"
wenzelm@17516
    22
  | Add "'a polex" "'a polex"
wenzelm@17516
    23
  | Sub "'a polex" "'a polex"
wenzelm@17516
    24
  | Mul "'a polex" "'a polex"
wenzelm@17516
    25
  | Pow "'a polex" nat
wenzelm@17516
    26
  | Neg "'a polex"
wenzelm@17516
    27
wenzelm@17516
    28
text {* Interpretation functions for the shadow syntax. *}
wenzelm@17516
    29
haftmann@31021
    30
primrec
haftmann@31021
    31
  Ipol :: "'a::{comm_ring_1} list \<Rightarrow> 'a pol \<Rightarrow> 'a"
haftmann@22742
    32
where
haftmann@22742
    33
    "Ipol l (Pc c) = c"
haftmann@22742
    34
  | "Ipol l (Pinj i P) = Ipol (drop i l) P"
haftmann@22742
    35
  | "Ipol l (PX P x Q) = Ipol l P * (hd l)^x + Ipol (drop 1 l) Q"
wenzelm@17516
    36
haftmann@31021
    37
primrec
haftmann@31021
    38
  Ipolex :: "'a::{comm_ring_1} list \<Rightarrow> 'a polex \<Rightarrow> 'a"
haftmann@22742
    39
where
haftmann@22742
    40
    "Ipolex l (Pol P) = Ipol l P"
haftmann@22742
    41
  | "Ipolex l (Add P Q) = Ipolex l P + Ipolex l Q"
haftmann@22742
    42
  | "Ipolex l (Sub P Q) = Ipolex l P - Ipolex l Q"
haftmann@22742
    43
  | "Ipolex l (Mul P Q) = Ipolex l P * Ipolex l Q"
haftmann@22742
    44
  | "Ipolex l (Pow p n) = Ipolex l p ^ n"
haftmann@22742
    45
  | "Ipolex l (Neg P) = - Ipolex l P"
wenzelm@17516
    46
wenzelm@17516
    47
text {* Create polynomial normalized polynomials given normalized inputs. *}
wenzelm@17516
    48
wenzelm@19736
    49
definition
wenzelm@21404
    50
  mkPinj :: "nat \<Rightarrow> 'a pol \<Rightarrow> 'a pol" where
wenzelm@19736
    51
  "mkPinj x P = (case P of
wenzelm@17516
    52
    Pc c \<Rightarrow> Pc c |
wenzelm@17516
    53
    Pinj y P \<Rightarrow> Pinj (x + y) P |
wenzelm@17516
    54
    PX p1 y p2 \<Rightarrow> Pinj x P)"
wenzelm@17516
    55
wenzelm@19736
    56
definition
haftmann@31021
    57
  mkPX :: "'a::{comm_ring} pol \<Rightarrow> nat \<Rightarrow> 'a pol \<Rightarrow> 'a pol" where
wenzelm@19736
    58
  "mkPX P i Q = (case P of
wenzelm@17516
    59
    Pc c \<Rightarrow> (if (c = 0) then (mkPinj 1 Q) else (PX P i Q)) |
wenzelm@17516
    60
    Pinj j R \<Rightarrow> PX P i Q |
wenzelm@17516
    61
    PX P2 i2 Q2 \<Rightarrow> (if (Q2 = (Pc 0)) then (PX P2 (i+i2) Q) else (PX P i Q)) )"
wenzelm@17516
    62
wenzelm@17516
    63
text {* Defining the basic ring operations on normalized polynomials *}
wenzelm@17516
    64
haftmann@22742
    65
function
haftmann@31021
    66
  add :: "'a::{comm_ring} pol \<Rightarrow> 'a pol \<Rightarrow> 'a pol" (infixl "\<oplus>" 65)
haftmann@22742
    67
where
haftmann@22742
    68
    "Pc a \<oplus> Pc b = Pc (a + b)"
haftmann@22742
    69
  | "Pc c \<oplus> Pinj i P = Pinj i (P \<oplus> Pc c)"
haftmann@22742
    70
  | "Pinj i P \<oplus> Pc c = Pinj i (P \<oplus> Pc c)"
haftmann@22742
    71
  | "Pc c \<oplus> PX P i Q = PX P i (Q \<oplus> Pc c)"
haftmann@22742
    72
  | "PX P i Q \<oplus> Pc c = PX P i (Q \<oplus> Pc c)"
haftmann@22742
    73
  | "Pinj x P \<oplus> Pinj y Q =
haftmann@22742
    74
      (if x = y then mkPinj x (P \<oplus> Q)
haftmann@22742
    75
       else (if x > y then mkPinj y (Pinj (x - y) P \<oplus> Q)
haftmann@22742
    76
         else mkPinj x (Pinj (y - x) Q \<oplus> P)))"
haftmann@22742
    77
  | "Pinj x P \<oplus> PX Q y R =
haftmann@22742
    78
      (if x = 0 then P \<oplus> PX Q y R
haftmann@22742
    79
       else (if x = 1 then PX Q y (R \<oplus> P)
haftmann@22742
    80
         else PX Q y (R \<oplus> Pinj (x - 1) P)))"
haftmann@22742
    81
  | "PX P x R \<oplus> Pinj y Q =
haftmann@22742
    82
      (if y = 0 then PX P x R \<oplus> Q
haftmann@22742
    83
       else (if y = 1 then PX P x (R \<oplus> Q)
haftmann@22742
    84
         else PX P x (R \<oplus> Pinj (y - 1) Q)))"
haftmann@22742
    85
  | "PX P1 x P2 \<oplus> PX Q1 y Q2 =
haftmann@22742
    86
      (if x = y then mkPX (P1 \<oplus> Q1) x (P2 \<oplus> Q2)
haftmann@22742
    87
       else (if x > y then mkPX (PX P1 (x - y) (Pc 0) \<oplus> Q1) y (P2 \<oplus> Q2)
haftmann@22742
    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
haftmann@22742
    92
function
haftmann@31021
    93
  mul :: "'a::{comm_ring} pol \<Rightarrow> 'a pol \<Rightarrow> 'a pol" (infixl "\<otimes>" 70)
haftmann@22742
    94
where
haftmann@22742
    95
    "Pc a \<otimes> Pc b = Pc (a * b)"
haftmann@22742
    96
  | "Pc c \<otimes> Pinj i P =
haftmann@22742
    97
      (if c = 0 then Pc 0 else mkPinj i (P \<otimes> Pc c))"
haftmann@22742
    98
  | "Pinj i P \<otimes> Pc c =
haftmann@22742
    99
      (if c = 0 then Pc 0 else mkPinj i (P \<otimes> Pc c))"
haftmann@22742
   100
  | "Pc c \<otimes> PX P i Q =
haftmann@22742
   101
      (if c = 0 then Pc 0 else mkPX (P \<otimes> Pc c) i (Q \<otimes> Pc c))"
haftmann@22742
   102
  | "PX P i Q \<otimes> Pc c =
haftmann@22742
   103
      (if c = 0 then Pc 0 else mkPX (P \<otimes> Pc c) i (Q \<otimes> Pc c))"
haftmann@22742
   104
  | "Pinj x P \<otimes> Pinj y Q =
haftmann@22742
   105
      (if x = y then mkPinj x (P \<otimes> Q) else
haftmann@22742
   106
         (if x > y then mkPinj y (Pinj (x-y) P \<otimes> Q)
haftmann@22742
   107
           else mkPinj x (Pinj (y - x) Q \<otimes> P)))"
haftmann@22742
   108
  | "Pinj x P \<otimes> PX Q y R =
haftmann@22742
   109
      (if x = 0 then P \<otimes> PX Q y R else
haftmann@22742
   110
         (if x = 1 then mkPX (Pinj x P \<otimes> Q) y (R \<otimes> P)
haftmann@22742
   111
           else mkPX (Pinj x P \<otimes> Q) y (R \<otimes> Pinj (x - 1) P)))"
haftmann@22742
   112
  | "PX P x R \<otimes> Pinj y Q =
haftmann@22742
   113
      (if y = 0 then PX P x R \<otimes> Q else
haftmann@22742
   114
         (if y = 1 then mkPX (Pinj y Q \<otimes> P) x (R \<otimes> Q)
haftmann@22742
   115
           else mkPX (Pinj y Q \<otimes> P) x (R \<otimes> Pinj (y - 1) Q)))"
haftmann@22742
   116
  | "PX P1 x P2 \<otimes> PX Q1 y Q2 =
haftmann@22742
   117
      mkPX (P1 \<otimes> Q1) (x + y) (P2 \<otimes> Q2) \<oplus>
haftmann@22742
   118
        (mkPX (P1 \<otimes> mkPinj 1 Q2) x (Pc 0) \<oplus>
haftmann@22742
   119
          (mkPX (Q1 \<otimes> mkPinj 1 P2) y (Pc 0)))"
haftmann@22742
   120
by pat_completeness auto
haftmann@22742
   121
termination by (relation "measure (\<lambda>(x, y). size x + size y)")
haftmann@22742
   122
  (auto simp add: mkPinj_def split: pol.split)
wenzelm@17516
   123
wenzelm@17516
   124
text {* Negation*}
haftmann@31021
   125
primrec
haftmann@31021
   126
  neg :: "'a::{comm_ring} pol \<Rightarrow> 'a pol"
haftmann@22742
   127
where
haftmann@22742
   128
    "neg (Pc c) = Pc (-c)"
haftmann@22742
   129
  | "neg (Pinj i P) = Pinj i (neg P)"
haftmann@22742
   130
  | "neg (PX P x Q) = PX (neg P) x (neg Q)"
wenzelm@17516
   131
wenzelm@17516
   132
text {* Substraction *}
wenzelm@19736
   133
definition
haftmann@31021
   134
  sub :: "'a::{comm_ring} pol \<Rightarrow> 'a pol \<Rightarrow> 'a pol" (infixl "\<ominus>" 65)
haftmann@22742
   135
where
haftmann@22742
   136
  "sub P Q = P \<oplus> neg Q"
wenzelm@17516
   137
wenzelm@17516
   138
text {* Square for Fast Exponentation *}
haftmann@31021
   139
primrec
haftmann@31021
   140
  sqr :: "'a::{comm_ring_1} pol \<Rightarrow> 'a pol"
haftmann@22742
   141
where
haftmann@22742
   142
    "sqr (Pc c) = Pc (c * c)"
haftmann@22742
   143
  | "sqr (Pinj i P) = mkPinj i (sqr P)"
haftmann@22742
   144
  | "sqr (PX A x B) = mkPX (sqr A) (x + x) (sqr B) \<oplus>
haftmann@22742
   145
      mkPX (Pc (1 + 1) \<otimes> A \<otimes> mkPinj 1 B) x (Pc 0)"
wenzelm@17516
   146
wenzelm@17516
   147
text {* Fast Exponentation *}
haftmann@22742
   148
fun
haftmann@31021
   149
  pow :: "nat \<Rightarrow> 'a::{comm_ring_1} pol \<Rightarrow> 'a pol"
haftmann@22742
   150
where
haftmann@22742
   151
    "pow 0 P = Pc 1"
haftmann@22742
   152
  | "pow n P = (if even n then pow (n div 2) (sqr P)
haftmann@22742
   153
       else P \<otimes> pow (n div 2) (sqr P))"
haftmann@22742
   154
  
wenzelm@17516
   155
lemma pow_if:
haftmann@22742
   156
  "pow n P =
haftmann@22742
   157
   (if n = 0 then Pc 1 else if even n then pow (n div 2) (sqr P)
haftmann@22742
   158
    else P \<otimes> pow (n div 2) (sqr P))"
wenzelm@17516
   159
  by (cases n) simp_all
wenzelm@17516
   160
wenzelm@17516
   161
wenzelm@17516
   162
text {* Normalization of polynomial expressions *}
wenzelm@17516
   163
haftmann@31021
   164
primrec
haftmann@31021
   165
  norm :: "'a::{comm_ring_1} polex \<Rightarrow> 'a pol"
haftmann@22742
   166
where
haftmann@22742
   167
    "norm (Pol P) = P"
haftmann@22742
   168
  | "norm (Add P Q) = norm P \<oplus> norm Q"
haftmann@22742
   169
  | "norm (Sub P Q) = norm P \<ominus> norm Q"
haftmann@22742
   170
  | "norm (Mul P Q) = norm P \<otimes> norm Q"
haftmann@22742
   171
  | "norm (Pow P n) = pow n (norm P)"
haftmann@22742
   172
  | "norm (Neg P) = neg (norm P)"
wenzelm@17516
   173
wenzelm@17516
   174
text {* mkPinj preserve semantics *}
wenzelm@17516
   175
lemma mkPinj_ci: "Ipol l (mkPinj a B) = Ipol l (Pinj a B)"
nipkow@29667
   176
  by (induct B) (auto simp add: mkPinj_def algebra_simps)
wenzelm@17516
   177
wenzelm@17516
   178
text {* mkPX preserves semantics *}
wenzelm@17516
   179
lemma mkPX_ci: "Ipol l (mkPX A b C) = Ipol l (PX A b C)"
nipkow@29667
   180
  by (cases A) (auto simp add: mkPX_def mkPinj_ci power_add algebra_simps)
wenzelm@17516
   181
wenzelm@17516
   182
text {* Correctness theorems for the implemented operations *}
wenzelm@17516
   183
wenzelm@17516
   184
text {* Negation *}
wenzelm@20622
   185
lemma neg_ci: "Ipol l (neg P) = -(Ipol l P)"
wenzelm@20622
   186
  by (induct P arbitrary: l) auto
wenzelm@17516
   187
wenzelm@17516
   188
text {* Addition *}
haftmann@22742
   189
lemma add_ci: "Ipol l (P \<oplus> Q) = Ipol l P + Ipol l Q"
wenzelm@20622
   190
proof (induct P Q arbitrary: l rule: add.induct)
wenzelm@17516
   191
  case (6 x P y Q)
wenzelm@17516
   192
  show ?case
wenzelm@17516
   193
  proof (rule linorder_cases)
wenzelm@17516
   194
    assume "x < y"
nipkow@29667
   195
    with 6 show ?case by (simp add: mkPinj_ci algebra_simps)
wenzelm@17516
   196
  next
wenzelm@17516
   197
    assume "x = y"
wenzelm@17516
   198
    with 6 show ?case by (simp add: mkPinj_ci)
wenzelm@17516
   199
  next
wenzelm@17516
   200
    assume "x > y"
nipkow@29667
   201
    with 6 show ?case by (simp add: mkPinj_ci algebra_simps)
wenzelm@17516
   202
  qed
wenzelm@17516
   203
next
wenzelm@17516
   204
  case (7 x P Q y R)
wenzelm@17516
   205
  have "x = 0 \<or> x = 1 \<or> x > 1" by arith
wenzelm@17516
   206
  moreover
wenzelm@17516
   207
  { assume "x = 0" with 7 have ?case by simp }
wenzelm@17516
   208
  moreover
nipkow@29667
   209
  { assume "x = 1" with 7 have ?case by (simp add: algebra_simps) }
wenzelm@17516
   210
  moreover
wenzelm@17516
   211
  { assume "x > 1" from 7 have ?case by (cases x) simp_all }
wenzelm@17516
   212
  ultimately show ?case by blast
wenzelm@17516
   213
next
wenzelm@17516
   214
  case (8 P x R y Q)
wenzelm@17516
   215
  have "y = 0 \<or> y = 1 \<or> y > 1" by arith
wenzelm@17516
   216
  moreover
wenzelm@17516
   217
  { assume "y = 0" with 8 have ?case by simp }
wenzelm@17516
   218
  moreover
wenzelm@17516
   219
  { assume "y = 1" 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
  ultimately show ?case by blast
wenzelm@17516
   223
next
wenzelm@17516
   224
  case (9 P1 x P2 Q1 y Q2)
wenzelm@17516
   225
  show ?case
wenzelm@17516
   226
  proof (rule linorder_cases)
wenzelm@17516
   227
    assume a: "x < y" hence "EX d. d + x = y" by arith
nipkow@29667
   228
    with 9 a show ?case by (auto simp add: mkPX_ci power_add algebra_simps)
wenzelm@17516
   229
  next
wenzelm@17516
   230
    assume a: "y < x" hence "EX d. d + y = x" by arith
nipkow@29667
   231
    with 9 a show ?case by (auto simp add: power_add mkPX_ci algebra_simps)
wenzelm@17516
   232
  next
wenzelm@17516
   233
    assume "x = y"
nipkow@29667
   234
    with 9 show ?case by (simp add: mkPX_ci algebra_simps)
wenzelm@17516
   235
  qed
nipkow@29667
   236
qed (auto simp add: algebra_simps)
wenzelm@17516
   237
wenzelm@17516
   238
text {* Multiplication *}
haftmann@22742
   239
lemma mul_ci: "Ipol l (P \<otimes> Q) = Ipol l P * Ipol l Q"
wenzelm@20622
   240
  by (induct P Q arbitrary: l rule: mul.induct)
nipkow@29667
   241
    (simp_all add: mkPX_ci mkPinj_ci algebra_simps add_ci power_add)
wenzelm@17516
   242
wenzelm@17516
   243
text {* Substraction *}
haftmann@22742
   244
lemma sub_ci: "Ipol l (P \<ominus> Q) = Ipol l P - Ipol l Q"
wenzelm@17516
   245
  by (simp add: add_ci neg_ci sub_def)
wenzelm@17516
   246
wenzelm@17516
   247
text {* Square *}
haftmann@22742
   248
lemma sqr_ci: "Ipol ls (sqr P) = Ipol ls P * Ipol ls P"
haftmann@22742
   249
  by (induct P arbitrary: ls)
nipkow@29667
   250
    (simp_all add: add_ci mkPinj_ci mkPX_ci mul_ci algebra_simps power_add)
wenzelm@17516
   251
wenzelm@17516
   252
text {* Power *}
haftmann@22742
   253
lemma even_pow:"even n \<Longrightarrow> pow n P = pow (n div 2) (sqr P)"
wenzelm@20622
   254
  by (induct n) simp_all
wenzelm@17516
   255
haftmann@22742
   256
lemma pow_ci: "Ipol ls (pow n P) = Ipol ls P ^ n"
haftmann@22742
   257
proof (induct n arbitrary: P rule: nat_less_induct)
wenzelm@17516
   258
  case (1 k)
wenzelm@17516
   259
  show ?case
wenzelm@17516
   260
  proof (cases k)
wenzelm@20622
   261
    case 0
wenzelm@20622
   262
    then show ?thesis by simp
wenzelm@20622
   263
  next
wenzelm@17516
   264
    case (Suc l)
wenzelm@17516
   265
    show ?thesis
wenzelm@17516
   266
    proof cases
wenzelm@20622
   267
      assume "even l"
wenzelm@20622
   268
      then have "Suc l div 2 = l div 2"
nipkow@40077
   269
        by (simp add: eval_nat_numeral even_nat_plus_one_div_two)
wenzelm@17516
   270
      moreover
wenzelm@17516
   271
      from Suc have "l < k" by simp
haftmann@22742
   272
      with 1 have "\<And>P. Ipol ls (pow l P) = Ipol ls P ^ l" by simp
wenzelm@17516
   273
      moreover
wenzelm@20622
   274
      note Suc `even l` even_nat_plus_one_div_two
wenzelm@17516
   275
      ultimately show ?thesis by (auto simp add: mul_ci power_Suc even_pow)
wenzelm@17516
   276
    next
wenzelm@20622
   277
      assume "odd l"
wenzelm@20622
   278
      {
wenzelm@20622
   279
        fix p
haftmann@22742
   280
        have "Ipol ls (sqr P) ^ (Suc l div 2) = Ipol ls P ^ Suc l"
wenzelm@20622
   281
        proof (cases l)
wenzelm@20622
   282
          case 0
wenzelm@20622
   283
          with `odd l` show ?thesis by simp
wenzelm@20622
   284
        next
wenzelm@20622
   285
          case (Suc w)
wenzelm@20622
   286
          with `odd l` have "even w" by simp
wenzelm@20678
   287
          have two_times: "2 * (w div 2) = w"
wenzelm@20678
   288
            by (simp only: numerals even_nat_div_two_times_two [OF `even w`])
haftmann@22742
   289
          have "Ipol ls P * Ipol ls P = Ipol ls P ^ Suc (Suc 0)"
wenzelm@20622
   290
            by (simp add: power_Suc)
wenzelm@32960
   291
          then have "Ipol ls P * Ipol ls P = Ipol ls P ^ 2"
wenzelm@32960
   292
            by (simp add: numerals)
wenzelm@20622
   293
          with Suc show ?thesis
huffman@30273
   294
            by (auto simp add: power_mult [symmetric, of _ 2 _] two_times mul_ci sqr_ci
huffman@30273
   295
                     simp del: power_Suc)
wenzelm@20622
   296
        qed
wenzelm@20622
   297
      } with 1 Suc `odd l` show ?thesis by simp
wenzelm@17516
   298
    qed
wenzelm@17516
   299
  qed
wenzelm@17516
   300
qed
wenzelm@17516
   301
wenzelm@17516
   302
text {* Normalization preserves semantics  *}
wenzelm@20622
   303
lemma norm_ci: "Ipolex l Pe = Ipol l (norm Pe)"
wenzelm@17516
   304
  by (induct Pe) (simp_all add: add_ci sub_ci mul_ci neg_ci pow_ci)
wenzelm@17516
   305
wenzelm@17516
   306
text {* Reflection lemma: Key to the (incomplete) decision procedure *}
wenzelm@17516
   307
lemma norm_eq:
wenzelm@20622
   308
  assumes "norm P1 = norm P2"
wenzelm@17516
   309
  shows "Ipolex l P1 = Ipolex l P2"
wenzelm@17516
   310
proof -
wenzelm@41807
   311
  from assms have "Ipol l (norm P1) = Ipol l (norm P2)" by simp
wenzelm@20622
   312
  then show ?thesis by (simp only: norm_ci)
wenzelm@17516
   313
qed
wenzelm@17516
   314
wenzelm@17516
   315
haftmann@33356
   316
use "commutative_ring_tac.ML"
haftmann@33356
   317
setup Commutative_Ring_Tac.setup
wenzelm@17516
   318
wenzelm@17516
   319
end