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