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