author chaieb Sun Oct 25 08:57:36 2009 +0100 (2009-10-25) changeset 33154 daa6ddece9f0 parent 33153 92080294beb8 child 33155 78c10ce27f09
Multivariate polynomials library over fields
```     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/HOL/Decision_Procs/Reflected_Multivariate_Polynomial.thy	Sun Oct 25 08:57:36 2009 +0100
1.3 @@ -0,0 +1,1743 @@
1.4 +(*  Title:      HOL/Decision_Procs/Reflected_Multivariate_Polynomial.thy
1.5 +    Author:     Amine Chaieb
1.6 +*)
1.7 +
1.8 +header {* Implementation and verification of mutivariate polynomials Library *}
1.9 +
1.10 +
1.11 +theory Reflected_Multivariate_Polynomial
1.12 +imports Parity Abstract_Rat Efficient_Nat List Polynomial_List
1.13 +begin
1.14 +
1.15 +  (* Impelementation *)
1.16 +
1.17 +subsection{* Datatype of polynomial expressions *}
1.18 +
1.19 +datatype poly = C Num| Bound nat| Add poly poly|Sub poly poly
1.20 +  | Mul poly poly| Neg poly| Pw poly nat| CN poly nat poly
1.21 +
1.22 +ML{* @{term "Add"}*}
1.23 +syntax "_poly0" :: "poly" ("0\<^sub>p")
1.24 +translations "0\<^sub>p" \<rightleftharpoons> "C (0\<^sub>N)"
1.25 +syntax "_poly" :: "int \<Rightarrow> poly" ("_\<^sub>p")
1.26 +translations "i\<^sub>p" \<rightleftharpoons> "C (i\<^sub>N)"
1.27 +
1.28 +subsection{* Boundedness, substitution and all that *}
1.29 +consts polysize:: "poly \<Rightarrow> nat"
1.30 +primrec
1.31 +  "polysize (C c) = 1"
1.32 +  "polysize (Bound n) = 1"
1.33 +  "polysize (Neg p) = 1 + polysize p"
1.34 +  "polysize (Add p q) = 1 + polysize p + polysize q"
1.35 +  "polysize (Sub p q) = 1 + polysize p + polysize q"
1.36 +  "polysize (Mul p q) = 1 + polysize p + polysize q"
1.37 +  "polysize (Pw p n) = 1 + polysize p"
1.38 +  "polysize (CN c n p) = 4 + polysize c + polysize p"
1.39 +
1.40 +consts
1.41 +  polybound0:: "poly \<Rightarrow> bool" (* a poly is INDEPENDENT of Bound 0 *)
1.42 +  polysubst0:: "poly \<Rightarrow> poly \<Rightarrow> poly" (* substitute a poly into a poly for Bound 0 *)
1.43 +primrec
1.44 +  "polybound0 (C c) = True"
1.45 +  "polybound0 (Bound n) = (n>0)"
1.46 +  "polybound0 (Neg a) = polybound0 a"
1.47 +  "polybound0 (Add a b) = (polybound0 a \<and> polybound0 b)"
1.48 +  "polybound0 (Sub a b) = (polybound0 a \<and> polybound0 b)"
1.49 +  "polybound0 (Mul a b) = (polybound0 a \<and> polybound0 b)"
1.50 +  "polybound0 (Pw p n) = (polybound0 p)"
1.51 +  "polybound0 (CN c n p) = (n \<noteq> 0 \<and> polybound0 c \<and> polybound0 p)"
1.52 +primrec
1.53 +  "polysubst0 t (C c) = (C c)"
1.54 +  "polysubst0 t (Bound n) = (if n=0 then t else Bound n)"
1.55 +  "polysubst0 t (Neg a) = Neg (polysubst0 t a)"
1.56 +  "polysubst0 t (Add a b) = Add (polysubst0 t a) (polysubst0 t b)"
1.57 +  "polysubst0 t (Sub a b) = Sub (polysubst0 t a) (polysubst0 t b)"
1.58 +  "polysubst0 t (Mul a b) = Mul (polysubst0 t a) (polysubst0 t b)"
1.59 +  "polysubst0 t (Pw p n) = Pw (polysubst0 t p) n"
1.60 +  "polysubst0 t (CN c n p) = (if n=0 then Add (polysubst0 t c) (Mul t (polysubst0 t p))
1.61 +                             else CN (polysubst0 t c) n (polysubst0 t p))"
1.62 +
1.63 +consts
1.64 +  decrpoly:: "poly \<Rightarrow> poly"
1.65 +recdef decrpoly "measure polysize"
1.66 +  "decrpoly (Bound n) = Bound (n - 1)"
1.67 +  "decrpoly (Neg a) = Neg (decrpoly a)"
1.68 +  "decrpoly (Add a b) = Add (decrpoly a) (decrpoly b)"
1.69 +  "decrpoly (Sub a b) = Sub (decrpoly a) (decrpoly b)"
1.70 +  "decrpoly (Mul a b) = Mul (decrpoly a) (decrpoly b)"
1.71 +  "decrpoly (Pw p n) = Pw (decrpoly p) n"
1.72 +  "decrpoly (CN c n p) = CN (decrpoly c) (n - 1) (decrpoly p)"
1.73 +  "decrpoly a = a"
1.74 +
1.75 +subsection{* Degrees and heads and coefficients *}
1.76 +
1.77 +consts degree:: "poly \<Rightarrow> nat"
1.78 +recdef degree "measure size"
1.79 +  "degree (CN c 0 p) = 1 + degree p"
1.80 +  "degree p = 0"
1.81 +consts head:: "poly \<Rightarrow> poly"
1.82 +
1.83 +recdef head "measure size"
1.84 +  "head (CN c 0 p) = head p"
1.85 +  "head p = p"
1.86 +  (* More general notions of degree and head *)
1.87 +consts degreen:: "poly \<Rightarrow> nat \<Rightarrow> nat"
1.88 +recdef degreen "measure size"
1.89 +  "degreen (CN c n p) = (\<lambda>m. if n=m then 1 + degreen p n else 0)"
1.90 +  "degreen p = (\<lambda>m. 0)"
1.91 +
1.92 +consts headn:: "poly \<Rightarrow> nat \<Rightarrow> poly"
1.93 +recdef headn "measure size"
1.94 +  "headn (CN c n p) = (\<lambda>m. if n \<le> m then headn p m else CN c n p)"
1.95 +  "headn p = (\<lambda>m. p)"
1.96 +
1.97 +consts coefficients:: "poly \<Rightarrow> poly list"
1.98 +recdef coefficients "measure size"
1.99 +  "coefficients (CN c 0 p) = c#(coefficients p)"
1.100 +  "coefficients p = [p]"
1.101 +
1.102 +consts isconstant:: "poly \<Rightarrow> bool"
1.103 +recdef isconstant "measure size"
1.104 +  "isconstant (CN c 0 p) = False"
1.105 +  "isconstant p = True"
1.106 +
1.107 +consts behead:: "poly \<Rightarrow> poly"
1.108 +recdef behead "measure size"
1.109 +  "behead (CN c 0 p) = (let p' = behead p in if p' = 0\<^sub>p then c else CN c 0 p')"
1.110 +  "behead p = 0\<^sub>p"
1.111 +
1.112 +consts headconst:: "poly \<Rightarrow> Num"
1.113 +recdef headconst "measure size"
1.114 +  "headconst (CN c n p) = headconst p"
1.115 +  "headconst (C n) = n"
1.116 +
1.117 +subsection{* Operations for normalization *}
1.118 +consts
1.119 +  polyadd :: "poly\<times>poly \<Rightarrow> poly"
1.120 +  polyneg :: "poly \<Rightarrow> poly" ("~\<^sub>p")
1.121 +  polysub :: "poly\<times>poly \<Rightarrow> poly"
1.122 +  polymul :: "poly\<times>poly \<Rightarrow> poly"
1.123 +  polypow :: "nat \<Rightarrow> poly \<Rightarrow> poly"
1.124 +syntax "_polyadd" :: "poly \<Rightarrow> poly \<Rightarrow> poly" (infixl "+\<^sub>p" 60)
1.125 +translations "a +\<^sub>p b" \<rightleftharpoons> "polyadd (a,b)"
1.126 +syntax "_polymul" :: "poly \<Rightarrow> poly \<Rightarrow> poly" (infixl "*\<^sub>p" 60)
1.127 +translations "a *\<^sub>p b" \<rightleftharpoons> "polymul (a,b)"
1.128 +syntax "_polysub" :: "poly \<Rightarrow> poly \<Rightarrow> poly" (infixl "-\<^sub>p" 60)
1.129 +translations "a -\<^sub>p b" \<rightleftharpoons> "polysub (a,b)"
1.130 +syntax "_polypow" :: "nat \<Rightarrow> poly \<Rightarrow> poly" (infixl "^\<^sub>p" 60)
1.131 +translations "a ^\<^sub>p k" \<rightleftharpoons> "polypow k a"
1.132 +
1.133 +recdef polyadd "measure (\<lambda> (a,b). polysize a + polysize b)"
1.134 +  "polyadd (C c, C c') = C (c+\<^sub>Nc')"
1.135 +  "polyadd (C c, CN c' n' p') = CN (polyadd (C c, c')) n' p'"
1.136 +  "polyadd (CN c n p, C c') = CN (polyadd (c, C c')) n p"
1.137 +stupid:  "polyadd (CN c n p, CN c' n' p') =
1.138 +    (if n < n' then CN (polyadd(c,CN c' n' p')) n p
1.139 +     else if n'<n then CN (polyadd(CN c n p, c')) n' p'
1.140 +     else (let cc' = polyadd (c,c') ;
1.141 +               pp' = polyadd (p,p')
1.142 +           in (if pp' = 0\<^sub>p then cc' else CN cc' n pp')))"
1.143 +  "polyadd (a, b) = Add a b"
1.144 +(hints recdef_simp add: Let_def measure_def split_def inv_image_def)
1.145 +
1.146 +(*
1.147 +declare stupid [simp del, code del]
1.148 +
1.149 +lemma [simp,code]: "polyadd (CN c n p, CN c' n' p') =
1.150 +    (if n < n' then CN (polyadd(c,CN c' n' p')) n p
1.151 +     else if n'<n then CN (polyadd(CN c n p, c')) n' p'
1.152 +     else (let cc' = polyadd (c,c') ;
1.153 +               pp' = polyadd (p,p')
1.154 +           in (if pp' = 0\<^sub>p then cc' else CN cc' n pp')))"
1.155 +  by (simp add: Let_def stupid)
1.156 +*)
1.157 +
1.158 +recdef polyneg "measure size"
1.159 +  "polyneg (C c) = C (~\<^sub>N c)"
1.160 +  "polyneg (CN c n p) = CN (polyneg c) n (polyneg p)"
1.161 +  "polyneg a = Neg a"
1.162 +
1.163 +defs polysub_def[code]: "polysub \<equiv> \<lambda> (p,q). polyadd (p,polyneg q)"
1.164 +
1.165 +recdef polymul "measure (\<lambda>(a,b). size a + size b)"
1.166 +  "polymul(C c, C c') = C (c*\<^sub>Nc')"
1.167 +  "polymul(C c, CN c' n' p') =
1.168 +      (if c = 0\<^sub>N then 0\<^sub>p else CN (polymul(C c,c')) n' (polymul(C c, p')))"
1.169 +  "polymul(CN c n p, C c') =
1.170 +      (if c' = 0\<^sub>N  then 0\<^sub>p else CN (polymul(c,C c')) n (polymul(p, C c')))"
1.171 +  "polymul(CN c n p, CN c' n' p') =
1.172 +  (if n<n' then CN (polymul(c,CN c' n' p')) n (polymul(p,CN c' n' p'))
1.173 +  else if n' < n
1.174 +  then CN (polymul(CN c n p,c')) n' (polymul(CN c n p,p'))
1.175 +  else polyadd(polymul(CN c n p, c'),CN 0\<^sub>p n' (polymul(CN c n p, p'))))"
1.176 +  "polymul (a,b) = Mul a b"
1.177 +recdef polypow "measure id"
1.178 +  "polypow 0 = (\<lambda>p. 1\<^sub>p)"
1.179 +  "polypow n = (\<lambda>p. let q = polypow (n div 2) p ; d = polymul(q,q) in
1.180 +                    if even n then d else polymul(p,d))"
1.181 +
1.182 +consts polynate :: "poly \<Rightarrow> poly"
1.183 +recdef polynate "measure polysize"
1.184 +  "polynate (Bound n) = CN 0\<^sub>p n 1\<^sub>p"
1.185 +  "polynate (Add p q) = (polynate p +\<^sub>p polynate q)"
1.186 +  "polynate (Sub p q) = (polynate p -\<^sub>p polynate q)"
1.187 +  "polynate (Mul p q) = (polynate p *\<^sub>p polynate q)"
1.188 +  "polynate (Neg p) = (~\<^sub>p (polynate p))"
1.189 +  "polynate (Pw p n) = ((polynate p) ^\<^sub>p n)"
1.190 +  "polynate (CN c n p) = polynate (Add c (Mul (Bound n) p))"
1.191 +  "polynate (C c) = C (normNum c)"
1.192 +
1.193 +fun poly_cmul :: "Num \<Rightarrow> poly \<Rightarrow> poly" where
1.194 +  "poly_cmul y (C x) = C (y *\<^sub>N x)"
1.195 +| "poly_cmul y (CN c n p) = CN (poly_cmul y c) n (poly_cmul y p)"
1.196 +| "poly_cmul y p = C y *\<^sub>p p"
1.197 +
1.198 +constdefs monic:: "poly \<Rightarrow> (poly \<times> bool)"
1.199 +  "monic p \<equiv> (let h = headconst p in if h = 0\<^sub>N then (p,False) else ((C (Ninv h)) *\<^sub>p p, 0>\<^sub>N h))"
1.200 +
1.201 +subsection{* Pseudo-division *}
1.202 +
1.203 +constdefs shift1:: "poly \<Rightarrow> poly"
1.204 +  "shift1 p \<equiv> CN 0\<^sub>p 0 p"
1.205 +consts funpow :: "nat \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a"
1.206 +
1.207 +primrec
1.208 +  "funpow 0 f x = x"
1.209 +  "funpow (Suc n) f x = funpow n f (f x)"
1.210 +function (tailrec) polydivide_aux :: "(poly \<times> nat \<times> poly \<times> nat \<times> poly) \<Rightarrow> (nat \<times> poly)"
1.211 +  where
1.212 +  "polydivide_aux (a,n,p,k,s) =
1.213 +  (if s = 0\<^sub>p then (k,s)
1.214 +  else (let b = head s; m = degree s in
1.215 +  (if m < n then (k,s) else
1.216 +  (let p'= funpow (m - n) shift1 p in
1.217 +  (if a = b then polydivide_aux (a,n,p,k,s -\<^sub>p p')
1.218 +  else polydivide_aux (a,n,p,Suc k, (a *\<^sub>p s) -\<^sub>p (b *\<^sub>p p')))))))"
1.219 +  by pat_completeness auto
1.220 +
1.221 +
1.222 +constdefs polydivide:: "poly \<Rightarrow> poly \<Rightarrow> (nat \<times> poly)"
1.223 +  "polydivide s p \<equiv> polydivide_aux (head p,degree p,p,0, s)"
1.224 +
1.225 +fun poly_deriv_aux :: "nat \<Rightarrow> poly \<Rightarrow> poly" where
1.226 +  "poly_deriv_aux n (CN c 0 p) = CN (poly_cmul ((int n)\<^sub>N) c) 0 (poly_deriv_aux (n + 1) p)"
1.227 +| "poly_deriv_aux n p = poly_cmul ((int n)\<^sub>N) p"
1.228 +
1.229 +fun poly_deriv :: "poly \<Rightarrow> poly" where
1.230 +  "poly_deriv (CN c 0 p) = poly_deriv_aux 1 p"
1.231 +| "poly_deriv p = 0\<^sub>p"
1.232 +
1.233 +  (* Verification *)
1.234 +lemma nth_pos2[simp]: "0 < n \<Longrightarrow> (x#xs) ! n = xs ! (n - 1)"
1.235 +using Nat.gr0_conv_Suc
1.236 +by clarsimp
1.237 +
1.238 +subsection{* Semantics of the polynomial representation *}
1.239 +
1.240 +consts Ipoly :: "'a list \<Rightarrow> poly \<Rightarrow> 'a::{ring_char_0,power,division_by_zero,field}"
1.241 +primrec
1.242 +  "Ipoly bs (C c) = INum c"
1.243 +  "Ipoly bs (Bound n) = bs!n"
1.244 +  "Ipoly bs (Neg a) = - Ipoly bs a"
1.245 +  "Ipoly bs (Add a b) = Ipoly bs a + Ipoly bs b"
1.246 +  "Ipoly bs (Sub a b) = Ipoly bs a - Ipoly bs b"
1.247 +  "Ipoly bs (Mul a b) = Ipoly bs a * Ipoly bs b"
1.248 +  "Ipoly bs (Pw t n) = (Ipoly bs t) ^ n"
1.249 +  "Ipoly bs (CN c n p) = (Ipoly bs c) + (bs!n)*(Ipoly bs p)"
1.250 +syntax "_Ipoly" :: "poly \<Rightarrow> 'a list \<Rightarrow>'a::{ring_char_0,power,division_by_zero,field}" ("\<lparr>_\<rparr>\<^sub>p\<^bsup>_\<^esup>")
1.251 +translations "\<lparr>p\<rparr>\<^sub>p\<^bsup>bs\<^esup>" \<rightleftharpoons> "Ipoly bs p"
1.252 +
1.253 +lemma Ipoly_CInt: "Ipoly bs (C (i,1)) = of_int i"
1.254 +  by (simp add: INum_def)
1.255 +lemma Ipoly_CRat: "Ipoly bs (C (i, j)) = of_int i / of_int j"
1.256 +  by (simp  add: INum_def)
1.257 +
1.258 +lemmas RIpoly_eqs = Ipoly.simps(2-7) Ipoly_CInt Ipoly_CRat
1.259 +
1.260 +subsection {* Normal form and normalization *}
1.261 +
1.262 +consts isnpolyh:: "poly \<Rightarrow> nat \<Rightarrow> bool"
1.263 +recdef isnpolyh "measure size"
1.264 +  "isnpolyh (C c) = (\<lambda>k. isnormNum c)"
1.265 +  "isnpolyh (CN c n p) = (\<lambda>k. n\<ge> k \<and> (isnpolyh c (Suc n)) \<and> (isnpolyh p n) \<and> (p \<noteq> 0\<^sub>p))"
1.266 +  "isnpolyh p = (\<lambda>k. False)"
1.267 +
1.268 +lemma isnpolyh_mono: "\<lbrakk>n' \<le> n ; isnpolyh p n\<rbrakk> \<Longrightarrow> isnpolyh p n'"
1.269 +by (induct p rule: isnpolyh.induct, auto)
1.270 +
1.271 +constdefs isnpoly:: "poly \<Rightarrow> bool"
1.272 +  "isnpoly p \<equiv> isnpolyh p 0"
1.273 +
1.274 +text{* polyadd preserves normal forms *}
1.275 +
1.276 +lemma polyadd_normh: "\<lbrakk>isnpolyh p n0 ; isnpolyh q n1\<rbrakk>
1.277 +      \<Longrightarrow> isnpolyh (polyadd(p,q)) (min n0 n1)"
1.278 +proof(induct p q arbitrary: n0 n1 rule: polyadd.induct)
1.279 +  case (2 a b c' n' p' n0 n1)
1.280 +  from prems have  th1: "isnpolyh (C (a,b)) (Suc n')" by simp
1.281 +  from prems(3) have th2: "isnpolyh c' (Suc n')"  and nplen1: "n' \<ge> n1" by simp_all
1.282 +  with isnpolyh_mono have cp: "isnpolyh c' (Suc n')" by simp
1.283 +  with prems(1)[OF th1 th2] have th3:"isnpolyh (C (a,b) +\<^sub>p c') (Suc n')" by simp
1.284 +  from nplen1 have n01len1: "min n0 n1 \<le> n'" by simp
1.285 +  thus ?case using prems th3 by simp
1.286 +next
1.287 +  case (3 c' n' p' a b n1 n0)
1.288 +  from prems have  th1: "isnpolyh (C (a,b)) (Suc n')" by simp
1.289 +  from prems(2) have th2: "isnpolyh c' (Suc n')"  and nplen1: "n' \<ge> n1" by simp_all
1.290 +  with isnpolyh_mono have cp: "isnpolyh c' (Suc n')" by simp
1.291 +  with prems(1)[OF th2 th1] have th3:"isnpolyh (c' +\<^sub>p C (a,b)) (Suc n')" by simp
1.292 +  from nplen1 have n01len1: "min n0 n1 \<le> n'" by simp
1.293 +  thus ?case using prems th3 by simp
1.294 +next
1.295 +  case (4 c n p c' n' p' n0 n1)
1.296 +  hence nc: "isnpolyh c (Suc n)" and np: "isnpolyh p n" by simp_all
1.297 +  from prems have nc': "isnpolyh c' (Suc n')" and np': "isnpolyh p' n'" by simp_all
1.298 +  from prems have ngen0: "n \<ge> n0" by simp
1.299 +  from prems have n'gen1: "n' \<ge> n1" by simp
1.300 +  have "n < n' \<or> n' < n \<or> n = n'" by auto
1.301 +  moreover {assume eq: "n = n'" hence eq': "\<not> n' < n \<and> \<not> n < n'" by simp
1.302 +    with prems(2)[rule_format, OF eq' nc nc']
1.303 +    have ncc':"isnpolyh (c +\<^sub>p c') (Suc n)" by auto
1.304 +    hence ncc'n01: "isnpolyh (c +\<^sub>p c') (min n0 n1)"
1.305 +      using isnpolyh_mono[where n'="min n0 n1" and n="Suc n"] ngen0 n'gen1 by auto
1.306 +    from eq prems(1)[rule_format, OF eq' np np'] have npp': "isnpolyh (p +\<^sub>p p') n" by simp
1.307 +    have minle: "min n0 n1 \<le> n'" using ngen0 n'gen1 eq by simp
1.308 +    from minle npp' ncc'n01 prems ngen0 n'gen1 ncc' have ?case by (simp add: Let_def)}
1.309 +  moreover {assume lt: "n < n'"
1.310 +    have "min n0 n1 \<le> n0" by simp
1.311 +    with prems have th1:"min n0 n1 \<le> n" by auto
1.312 +    from prems have th21: "isnpolyh c (Suc n)" by simp
1.313 +    from prems have th22: "isnpolyh (CN c' n' p') n'" by simp
1.314 +    from lt have th23: "min (Suc n) n' = Suc n" by arith
1.315 +    from prems(4)[rule_format, OF lt th21 th22]
1.316 +    have "isnpolyh (polyadd (c, CN c' n' p')) (Suc n)" using th23 by simp
1.317 +    with prems th1 have ?case by simp }
1.318 +  moreover {assume gt: "n' < n" hence gt': "n' < n \<and> \<not> n < n'" by simp
1.319 +    have "min n0 n1 \<le> n1"  by simp
1.320 +    with prems have th1:"min n0 n1 \<le> n'" by auto
1.321 +    from prems have th21: "isnpolyh c' (Suc n')" by simp_all
1.322 +    from prems have th22: "isnpolyh (CN c n p) n" by simp
1.323 +    from gt have th23: "min n (Suc n') = Suc n'" by arith
1.324 +    from prems(3)[rule_format, OF  gt' th22 th21]
1.325 +    have "isnpolyh (polyadd (CN c n p,c')) (Suc n')" using th23 by simp
1.326 +    with prems th1 have ?case by simp}
1.327 +      ultimately show ?case by blast
1.328 +qed auto
1.329 +
1.330 +lemma polyadd[simp]: "Ipoly bs (polyadd (p,q)) = (Ipoly bs p) + (Ipoly bs q)"
1.331 +by (induct p q rule: polyadd.induct, auto simp add: Let_def ring_simps right_distrib[symmetric] simp del: right_distrib)
1.332 +
1.333 +lemma polyadd_norm: "\<lbrakk> isnpoly p ; isnpoly q\<rbrakk> \<Longrightarrow> isnpoly (polyadd(p,q))"
1.334 +  using polyadd_normh[of "p" "0" "q" "0"] isnpoly_def by simp
1.335 +
1.336 +text{* The degree of addition and other general lemmas needed for the normal form of polymul*}
1.337 +
1.338 +lemma polyadd_different_degreen:
1.339 +  "\<lbrakk>isnpolyh p n0 ; isnpolyh q n1; degreen p m \<noteq> degreen q m ; m \<le> min n0 n1\<rbrakk> \<Longrightarrow>
1.340 +  degreen (polyadd(p,q)) m = max (degreen p m) (degreen q m)"
1.341 +proof (induct p q arbitrary: m n0 n1 rule: polyadd.induct)
1.342 +  case (4 c n p c' n' p' m n0 n1)
1.343 +  thus ?case
1.344 +    apply (cases "n' < n", simp_all add: Let_def)
1.345 +    apply (cases "n = n'", simp_all)
1.346 +    apply (cases "n' = m", simp_all add: Let_def)
1.347 +    by (erule allE[where x="m"], erule allE[where x="Suc m"],
1.348 +           erule allE[where x="m"], erule allE[where x="Suc m"],
1.349 +           clarsimp,erule allE[where x="m"],erule allE[where x="Suc m"], simp)
1.350 +qed simp_all
1.351 +
1.352 +lemma headnz[simp]: "\<lbrakk>isnpolyh p n ; p \<noteq> 0\<^sub>p\<rbrakk> \<Longrightarrow> headn p m \<noteq> 0\<^sub>p"
1.353 +  by (induct p arbitrary: n rule: headn.induct, auto)
1.354 +lemma degree_isnpolyh_Suc[simp]: "isnpolyh p (Suc n) \<Longrightarrow> degree p = 0"
1.355 +  by (induct p arbitrary: n rule: degree.induct, auto)
1.356 +lemma degreen_0[simp]: "isnpolyh p n \<Longrightarrow> m < n \<Longrightarrow> degreen p m = 0"
1.357 +  by (induct p arbitrary: n rule: degreen.induct, auto)
1.358 +
1.359 +lemma degree_isnpolyh_Suc': "n > 0 \<Longrightarrow> isnpolyh p n \<Longrightarrow> degree p = 0"
1.360 +  by (induct p arbitrary: n rule: degree.induct, auto)
1.361 +
1.362 +lemma degree_npolyhCN[simp]: "isnpolyh (CN c n p) n0 \<Longrightarrow> degree c = 0"
1.363 +  using degree_isnpolyh_Suc by auto
1.364 +lemma degreen_npolyhCN[simp]: "isnpolyh (CN c n p) n0 \<Longrightarrow> degreen c n = 0"
1.365 +  using degreen_0 by auto
1.366 +
1.367 +
1.368 +lemma degreen_polyadd:
1.369 +  assumes np: "isnpolyh p n0" and nq: "isnpolyh q n1" and m: "m \<le> max n0 n1"
1.370 +  shows "degreen (p +\<^sub>p q) m \<le> max (degreen p m) (degreen q m)"
1.371 +  using np nq m
1.372 +proof (induct p q arbitrary: n0 n1 m rule: polyadd.induct)
1.373 +  case (2 c c' n' p' n0 n1) thus ?case  by (cases n', simp_all)
1.374 +next
1.375 +  case (3 c n p c' n0 n1) thus ?case by (cases n, auto)
1.376 +next
1.377 +  case (4 c n p c' n' p' n0 n1 m)
1.378 +  thus ?case
1.379 +    apply (cases "n < n'", simp_all add: Let_def)
1.380 +    apply (cases "n' < n", simp_all)
1.381 +    apply (erule allE[where x="n"],erule allE[where x="Suc n"],clarify)
1.382 +    apply (erule allE[where x="n'"],erule allE[where x="Suc n'"],clarify)
1.383 +    by (erule allE[where x="m"],erule allE[where x="m"], auto)
1.384 +qed auto
1.385 +
1.386 +
1.387 +lemma polyadd_eq_const_degreen: "\<lbrakk> isnpolyh p n0 ; isnpolyh q n1 ; polyadd (p,q) = C c\<rbrakk>
1.388 +  \<Longrightarrow> degreen p m = degreen q m"
1.389 +proof (induct p q arbitrary: m n0 n1 c rule: polyadd.induct)
1.390 +  case (4 c n p c' n' p' m n0 n1 x)
1.391 +  hence z: "CN c n p +\<^sub>p CN c' n' p' = C x" by simp
1.392 +  {assume nn': "n' < n" hence ?case using prems by simp}
1.393 +  moreover
1.394 +  {assume nn':"\<not> n' < n" hence "n < n' \<or> n = n'" by arith
1.395 +    moreover {assume "n < n'" with prems have ?case by simp }
1.396 +    moreover {assume eq: "n = n'" hence ?case using prems
1.397 +	by (cases "p +\<^sub>p p' = 0\<^sub>p", auto simp add: Let_def) }
1.398 +    ultimately have ?case by blast}
1.399 +  ultimately show ?case by blast
1.400 +qed simp_all
1.401 +
1.402 +lemma polymul_properties:
1.403 +  assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
1.404 +  and np: "isnpolyh p n0" and nq: "isnpolyh q n1" and m: "m \<le> min n0 n1"
1.405 +  shows "isnpolyh (p *\<^sub>p q) (min n0 n1)"
1.406 +  and "(p *\<^sub>p q = 0\<^sub>p) = (p = 0\<^sub>p \<or> q = 0\<^sub>p)"
1.407 +  and "degreen (p *\<^sub>p q) m = (if (p = 0\<^sub>p \<or> q = 0\<^sub>p) then 0
1.408 +                             else degreen p m + degreen q m)"
1.409 +  using np nq m
1.410 +proof(induct p q arbitrary: n0 n1 m rule: polymul.induct)
1.411 +  case (2 a b c' n' p')
1.412 +  let ?c = "(a,b)"
1.413 +  { case (1 n0 n1)
1.414 +    hence n: "isnpolyh (C ?c) n'" "isnpolyh c' (Suc n')" "isnpolyh p' n'" "isnormNum ?c"
1.415 +      "isnpolyh (CN c' n' p') n1"
1.416 +      by simp_all
1.417 +    {assume "?c = 0\<^sub>N" hence ?case by auto}
1.418 +      moreover {assume cnz: "?c \<noteq> 0\<^sub>N"
1.419 +	from "2.hyps"(1)[rule_format,where xb="n'",  OF cnz n(1) n(3)]
1.420 +	  "2.hyps"(2)[rule_format, where x="Suc n'"
1.421 +	  and xa="Suc n'" and xb = "n'", OF cnz ] cnz n have ?case
1.422 +	  by (auto simp add: min_def)}
1.423 +      ultimately show ?case by blast
1.424 +  next
1.425 +    case (2 n0 n1) thus ?case by auto
1.426 +  next
1.427 +    case (3 n0 n1) thus ?case  using "2.hyps" by auto }
1.428 +next
1.429 +  case (3 c n p a b){
1.430 +    let ?c' = "(a,b)"
1.431 +    case (1 n0 n1)
1.432 +    hence n: "isnpolyh (C ?c') n" "isnpolyh c (Suc n)" "isnpolyh p n" "isnormNum ?c'"
1.433 +      "isnpolyh (CN c n p) n0"
1.434 +      by simp_all
1.435 +    {assume "?c' = 0\<^sub>N" hence ?case by auto}
1.436 +      moreover {assume cnz: "?c' \<noteq> 0\<^sub>N"
1.437 +	from "3.hyps"(1)[rule_format,where xb="n",  OF cnz n(3) n(1)]
1.438 +	  "3.hyps"(2)[rule_format, where x="Suc n"
1.439 +	  and xa="Suc n" and xb = "n", OF cnz ] cnz n have ?case
1.440 +	  by (auto simp add: min_def)}
1.441 +      ultimately show ?case by blast
1.442 +  next
1.443 +    case (2 n0 n1) thus ?case apply auto done
1.444 +  next
1.445 +    case (3 n0 n1) thus ?case  using "3.hyps" by auto }
1.446 +next
1.447 +  case (4 c n p c' n' p')
1.448 +  let ?cnp = "CN c n p" let ?cnp' = "CN c' n' p'"
1.449 +    {fix n0 n1
1.450 +      assume "isnpolyh ?cnp n0" and "isnpolyh ?cnp' n1"
1.451 +      hence cnp: "isnpolyh ?cnp n" and cnp': "isnpolyh ?cnp' n'"
1.452 +	and np: "isnpolyh p n" and nc: "isnpolyh c (Suc n)"
1.453 +	and np': "isnpolyh p' n'" and nc': "isnpolyh c' (Suc n')"
1.454 +	and nn0: "n \<ge> n0" and nn1:"n' \<ge> n1"
1.455 +	by simp_all
1.456 +      have "n < n' \<or> n' < n \<or> n' = n" by auto
1.457 +      moreover
1.458 +      {assume nn': "n < n'"
1.459 +	with "4.hyps"(5)[rule_format, OF nn' np cnp', where xb ="n"]
1.460 +	  "4.hyps"(6)[rule_format, OF nn' nc cnp', where xb="n"] nn' nn0 nn1 cnp
1.461 +	have "isnpolyh (?cnp *\<^sub>p ?cnp') (min n0 n1)"
1.462 +	  by (simp add: min_def) }
1.463 +      moreover
1.464 +
1.465 +      {assume nn': "n > n'" hence stupid: "n' < n \<and> \<not> n < n'" by arith
1.466 +	with "4.hyps"(3)[rule_format, OF stupid cnp np', where xb="n'"]
1.467 +	  "4.hyps"(4)[rule_format, OF stupid cnp nc', where xb="Suc n'"]
1.468 +	  nn' nn0 nn1 cnp'
1.469 +	have "isnpolyh (?cnp *\<^sub>p ?cnp') (min n0 n1)"
1.470 +	  by (cases "Suc n' = n", simp_all add: min_def)}
1.471 +      moreover
1.472 +      {assume nn': "n' = n" hence stupid: "\<not> n' < n \<and> \<not> n < n'" by arith
1.473 +	from "4.hyps"(1)[rule_format, OF stupid cnp np', where xb="n"]
1.474 +	  "4.hyps"(2)[rule_format, OF stupid cnp nc', where xb="n"] nn' cnp cnp' nn1
1.475 +
1.476 +	have "isnpolyh (?cnp *\<^sub>p ?cnp') (min n0 n1)"
1.477 +	  by simp (rule polyadd_normh,simp_all add: min_def isnpolyh_mono[OF nn0]) }
1.478 +      ultimately show "isnpolyh (?cnp *\<^sub>p ?cnp') (min n0 n1)" by blast }
1.479 +    note th = this
1.480 +    {fix n0 n1 m
1.481 +      assume np: "isnpolyh ?cnp n0" and np':"isnpolyh ?cnp' n1"
1.482 +      and m: "m \<le> min n0 n1"
1.483 +      let ?d = "degreen (?cnp *\<^sub>p ?cnp') m"
1.484 +      let ?d1 = "degreen ?cnp m"
1.485 +      let ?d2 = "degreen ?cnp' m"
1.486 +      let ?eq = "?d = (if ?cnp = 0\<^sub>p \<or> ?cnp' = 0\<^sub>p then 0  else ?d1 + ?d2)"
1.487 +      have "n'<n \<or> n < n' \<or> n' = n" by auto
1.488 +      moreover
1.489 +      {assume "n' < n \<or> n < n'"
1.490 +	with "4.hyps" np np' m
1.491 +	have ?eq apply (cases "n' < n", simp_all)
1.492 +	apply (erule allE[where x="n"],erule allE[where x="n"],auto)
1.493 +	done }
1.494 +      moreover
1.495 +      {assume nn': "n' = n"  hence nn:"\<not> n' < n \<and> \<not> n < n'" by arith
1.496 + 	from "4.hyps"(1)[rule_format, OF nn, where x="n" and xa ="n'" and xb="n"]
1.497 +	  "4.hyps"(2)[rule_format, OF nn, where x="n" and xa ="Suc n'" and xb="n"]
1.498 +	  np np' nn'
1.499 +	have norm: "isnpolyh ?cnp n" "isnpolyh c' (Suc n)" "isnpolyh (?cnp *\<^sub>p c') n"
1.500 +	  "isnpolyh p' n" "isnpolyh (?cnp *\<^sub>p p') n" "isnpolyh (CN 0\<^sub>p n (CN c n p *\<^sub>p p')) n"
1.501 +	  "(?cnp *\<^sub>p c' = 0\<^sub>p) = (c' = 0\<^sub>p)"
1.502 +	  "?cnp *\<^sub>p p' \<noteq> 0\<^sub>p" by (auto simp add: min_def)
1.503 +	{assume mn: "m = n"
1.504 +	  from "4.hyps"(1)[rule_format, OF nn norm(1,4), where xb="n"]
1.505 +	    "4.hyps"(2)[rule_format, OF nn norm(1,2), where xb="n"] norm nn' mn
1.506 +	  have degs:  "degreen (?cnp *\<^sub>p c') n =
1.507 +	    (if c'=0\<^sub>p then 0 else ?d1 + degreen c' n)"
1.508 +	    "degreen (?cnp *\<^sub>p p') n = ?d1  + degreen p' n" by (simp_all add: min_def)
1.509 +	  from degs norm
1.510 +	  have th1: "degreen(?cnp *\<^sub>p c') n < degreen (CN 0\<^sub>p n (?cnp *\<^sub>p p')) n" by simp
1.511 +	  hence neq: "degreen (?cnp *\<^sub>p c') n \<noteq> degreen (CN 0\<^sub>p n (?cnp *\<^sub>p p')) n"
1.512 +	    by simp
1.513 +	  have nmin: "n \<le> min n n" by (simp add: min_def)
1.514 +	  from polyadd_different_degreen[OF norm(3,6) neq nmin] th1
1.515 +	  have deg: "degreen (CN c n p *\<^sub>p c' +\<^sub>p CN 0\<^sub>p n (CN c n p *\<^sub>p p')) n = degreen (CN 0\<^sub>p n (CN c n p *\<^sub>p p')) n" by simp
1.516 +	  from "4.hyps"(1)[rule_format, OF nn norm(1,4), where xb="n"]
1.517 +	    "4.hyps"(2)[rule_format, OF nn norm(1,2), where xb="n"]
1.518 +	    mn norm m nn' deg
1.519 +	  have ?eq by simp}
1.520 +	moreover
1.521 +	{assume mn: "m \<noteq> n" hence mn': "m < n" using m np by auto
1.522 +	  from nn' m np have max1: "m \<le> max n n"  by simp
1.523 +	  hence min1: "m \<le> min n n" by simp
1.524 +	  hence min2: "m \<le> min n (Suc n)" by simp
1.525 +	  {assume "c' = 0\<^sub>p"
1.526 +	    from `c' = 0\<^sub>p` have ?eq
1.527 +	      using "4.hyps"(1)[rule_format, OF nn norm(1,4) min1]
1.528 +	    "4.hyps"(2)[rule_format, OF nn norm(1,2) min2] mn nn'
1.529 +	      apply simp
1.530 +	      done}
1.531 +	  moreover
1.532 +	  {assume cnz: "c' \<noteq> 0\<^sub>p"
1.533 +	    from "4.hyps"(1)[rule_format, OF nn norm(1,4) min1]
1.534 +	      "4.hyps"(2)[rule_format, OF nn norm(1,2) min2]
1.535 +	      degreen_polyadd[OF norm(3,6) max1]
1.536 +
1.537 +	    have "degreen (?cnp *\<^sub>p c' +\<^sub>p CN 0\<^sub>p n (?cnp *\<^sub>p p')) m
1.538 +	      \<le> max (degreen (?cnp *\<^sub>p c') m) (degreen (CN 0\<^sub>p n (?cnp *\<^sub>p p')) m)"
1.539 +	      using mn nn' cnz np np' by simp
1.540 +	    with "4.hyps"(1)[rule_format, OF nn norm(1,4) min1]
1.541 +	      "4.hyps"(2)[rule_format, OF nn norm(1,2) min2]
1.542 +	      degreen_0[OF norm(3) mn'] have ?eq using nn' mn cnz np np' by clarsimp}
1.543 +	  ultimately have ?eq by blast }
1.544 +	ultimately have ?eq by blast}
1.545 +      ultimately show ?eq by blast}
1.546 +    note degth = this
1.547 +    { case (2 n0 n1)
1.548 +      hence np: "isnpolyh ?cnp n0" and np': "isnpolyh ?cnp' n1"
1.549 +	and m: "m \<le> min n0 n1" by simp_all
1.550 +      hence mn: "m \<le> n" by simp
1.551 +      let ?c0p = "CN 0\<^sub>p n (?cnp *\<^sub>p p')"
1.552 +      {assume C: "?cnp *\<^sub>p c' +\<^sub>p ?c0p = 0\<^sub>p" "n' = n"
1.553 +	hence nn: "\<not>n' < n \<and> \<not> n<n'" by simp
1.554 +	from "4.hyps"(1) [rule_format, OF nn, where x="n" and xa = "n" and xb="n"]
1.555 +	  "4.hyps"(2) [rule_format, OF nn, where x="n" and xa = "Suc n" and xb="n"]
1.556 +	  np np' C(2) mn
1.557 +	have norm: "isnpolyh ?cnp n" "isnpolyh c' (Suc n)" "isnpolyh (?cnp *\<^sub>p c') n"
1.558 +	  "isnpolyh p' n" "isnpolyh (?cnp *\<^sub>p p') n" "isnpolyh (CN 0\<^sub>p n (CN c n p *\<^sub>p p')) n"
1.559 +	  "(?cnp *\<^sub>p c' = 0\<^sub>p) = (c' = 0\<^sub>p)"
1.560 +	  "?cnp *\<^sub>p p' \<noteq> 0\<^sub>p"
1.561 +	  "degreen (?cnp *\<^sub>p c') n = (if c'=0\<^sub>p then 0 else degreen ?cnp n + degreen c' n)"
1.562 +	    "degreen (?cnp *\<^sub>p p') n = degreen ?cnp n + degreen p' n"
1.563 +	  by (simp_all add: min_def)
1.564 +
1.565 +	  from norm have cn: "isnpolyh (CN 0\<^sub>p n (CN c n p *\<^sub>p p')) n" by simp
1.566 +	  have degneq: "degreen (?cnp *\<^sub>p c') n < degreen (CN 0\<^sub>p n (?cnp *\<^sub>p p')) n"
1.567 +	    using norm by simp
1.568 +	from polyadd_eq_const_degreen[OF norm(3) cn C(1), where m="n"]  degneq
1.569 +	have "False" by simp }
1.570 +      thus ?case using "4.hyps" by clarsimp}
1.571 +qed auto
1.572 +
1.573 +lemma polymul[simp]: "Ipoly bs (p *\<^sub>p q) = (Ipoly bs p) * (Ipoly bs q)"
1.574 +by(induct p q rule: polymul.induct, auto simp add: ring_simps)
1.575 +
1.576 +lemma polymul_normh:
1.577 +    assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
1.578 +  shows "\<lbrakk>isnpolyh p n0 ; isnpolyh q n1\<rbrakk> \<Longrightarrow> isnpolyh (p *\<^sub>p q) (min n0 n1)"
1.579 +  using polymul_properties(1)  by blast
1.580 +lemma polymul_eq0_iff:
1.581 +  assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
1.582 +  shows "\<lbrakk> isnpolyh p n0 ; isnpolyh q n1\<rbrakk> \<Longrightarrow> (p *\<^sub>p q = 0\<^sub>p) = (p = 0\<^sub>p \<or> q = 0\<^sub>p) "
1.583 +  using polymul_properties(2)  by blast
1.584 +lemma polymul_degreen:
1.585 +  assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
1.586 +  shows "\<lbrakk> isnpolyh p n0 ; isnpolyh q n1 ; m \<le> min n0 n1\<rbrakk> \<Longrightarrow> degreen (p *\<^sub>p q) m = (if (p = 0\<^sub>p \<or> q = 0\<^sub>p) then 0 else degreen p m + degreen q m)"
1.587 +  using polymul_properties(3) by blast
1.588 +lemma polymul_norm:
1.589 +  assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
1.590 +  shows "\<lbrakk> isnpoly p; isnpoly q\<rbrakk> \<Longrightarrow> isnpoly (polymul (p,q))"
1.591 +  using polymul_normh[of "p" "0" "q" "0"] isnpoly_def by simp
1.592 +
1.593 +lemma headconst_zero: "isnpolyh p n0 \<Longrightarrow> headconst p = 0\<^sub>N \<longleftrightarrow> p = 0\<^sub>p"
1.594 +  by (induct p arbitrary: n0 rule: headconst.induct, auto)
1.595 +
1.596 +lemma headconst_isnormNum: "isnpolyh p n0 \<Longrightarrow> isnormNum (headconst p)"
1.597 +  by (induct p arbitrary: n0, auto)
1.598 +
1.599 +lemma monic_eqI: assumes np: "isnpolyh p n0"
1.600 +  shows "INum (headconst p) * Ipoly bs (fst (monic p)) = (Ipoly bs p ::'a::{ring_char_0,power,division_by_zero,field})"
1.601 +  unfolding monic_def Let_def
1.602 +proof(cases "headconst p = 0\<^sub>N", simp_all add: headconst_zero[OF np])
1.603 +  let ?h = "headconst p"
1.604 +  assume pz: "p \<noteq> 0\<^sub>p"
1.605 +  {assume hz: "INum ?h = (0::'a)"
1.606 +    from headconst_isnormNum[OF np] have norm: "isnormNum ?h" "isnormNum 0\<^sub>N" by simp_all
1.607 +    from isnormNum_unique[where ?'a = 'a, OF norm] hz have "?h = 0\<^sub>N" by simp
1.608 +    with headconst_zero[OF np] have "p =0\<^sub>p" by blast with pz have "False" by blast}
1.609 +  thus "INum (headconst p) = (0::'a) \<longrightarrow> \<lparr>p\<rparr>\<^sub>p\<^bsup>bs\<^esup> = 0" by blast
1.610 +qed
1.611 +
1.612 +
1.613 +
1.614 +
1.615 +text{* polyneg is a negation and preserves normal form *}
1.616 +lemma polyneg[simp]: "Ipoly bs (polyneg p) = - Ipoly bs p"
1.617 +by (induct p rule: polyneg.induct, auto)
1.618 +
1.619 +lemma polyneg0: "isnpolyh p n \<Longrightarrow> ((~\<^sub>p p) = 0\<^sub>p) = (p = 0\<^sub>p)"
1.620 +  by (induct p arbitrary: n rule: polyneg.induct, auto simp add: Nneg_def)
1.621 +lemma polyneg_polyneg: "isnpolyh p n0 \<Longrightarrow> ~\<^sub>p (~\<^sub>p p) = p"
1.622 +  by (induct p arbitrary: n0 rule: polyneg.induct, auto)
1.623 +lemma polyneg_normh: "\<And>n. isnpolyh p n \<Longrightarrow> isnpolyh (polyneg p) n "
1.624 +by (induct p rule: polyneg.induct, auto simp add: polyneg0)
1.625 +
1.626 +lemma polyneg_norm: "isnpoly p \<Longrightarrow> isnpoly (polyneg p)"
1.627 +  using isnpoly_def polyneg_normh by simp
1.628 +
1.629 +
1.630 +text{* polysub is a substraction and preserves normalform *}
1.631 +lemma polysub[simp]: "Ipoly bs (polysub (p,q)) = (Ipoly bs p) - (Ipoly bs q)"
1.632 +by (simp add: polysub_def polyneg polyadd)
1.633 +lemma polysub_normh: "\<And> n0 n1. \<lbrakk> isnpolyh p n0 ; isnpolyh q n1\<rbrakk> \<Longrightarrow> isnpolyh (polysub(p,q)) (min n0 n1)"
1.634 +by (simp add: polysub_def polyneg_normh polyadd_normh)
1.635 +
1.636 +lemma polysub_norm: "\<lbrakk> isnpoly p; isnpoly q\<rbrakk> \<Longrightarrow> isnpoly (polysub(p,q))"
1.637 +  using polyadd_norm polyneg_norm by (simp add: polysub_def)
1.638 +lemma polysub_same_0[simp]:   assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
1.639 +  shows "isnpolyh p n0 \<Longrightarrow> polysub (p, p) = 0\<^sub>p"
1.640 +unfolding polysub_def split_def fst_conv snd_conv
1.641 +by (induct p arbitrary: n0,auto simp add: Let_def Nsub0[simplified Nsub_def])
1.642 +
1.643 +lemma polysub_0:
1.644 +  assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
1.645 +  shows "\<lbrakk> isnpolyh p n0 ; isnpolyh q n1\<rbrakk> \<Longrightarrow> (p -\<^sub>p q = 0\<^sub>p) = (p = q)"
1.646 +  unfolding polysub_def split_def fst_conv snd_conv
1.647 +  apply (induct p q arbitrary: n0 n1 rule:polyadd.induct, simp_all add: Nsub0[simplified Nsub_def])
1.648 +  apply (clarsimp simp add: Let_def)
1.649 +  apply (case_tac "n < n'", simp_all)
1.650 +  apply (case_tac "n' < n", simp_all)
1.651 +  apply (erule impE)+
1.652 +  apply (rule_tac x="Suc n" in exI, simp)
1.653 +  apply (rule_tac x="n" in exI, simp)
1.654 +  apply (erule impE)+
1.655 +  apply (rule_tac x="n" in exI, simp)
1.656 +  apply (rule_tac x="Suc n" in exI, simp)
1.657 +  apply (erule impE)+
1.658 +  apply (rule_tac x="Suc n" in exI, simp)
1.659 +  apply (rule_tac x="n" in exI, simp)
1.660 +  apply (erule impE)+
1.661 +  apply (rule_tac x="Suc n" in exI, simp)
1.662 +  apply clarsimp
1.663 +  done
1.664 +
1.665 +text{* polypow is a power function and preserves normal forms *}
1.666 +lemma polypow[simp]: "Ipoly bs (polypow n p) = ((Ipoly bs p :: 'a::{ring_char_0,division_by_zero,field})) ^ n"
1.667 +proof(induct n rule: polypow.induct)
1.668 +  case 1 thus ?case by simp
1.669 +next
1.670 +  case (2 n)
1.671 +  let ?q = "polypow ((Suc n) div 2) p"
1.672 +  let ?d = "polymul(?q,?q)"
1.673 +  have "odd (Suc n) \<or> even (Suc n)" by simp
1.674 +  moreover
1.675 +  {assume odd: "odd (Suc n)"
1.676 +    have th: "(Suc (Suc (Suc (0\<Colon>nat)) * (Suc n div Suc (Suc (0\<Colon>nat))))) = Suc n div 2 + Suc n div 2 + 1" by arith
1.677 +    from odd have "Ipoly bs (p ^\<^sub>p Suc n) = Ipoly bs (polymul(p, ?d))" by (simp add: Let_def)
1.678 +    also have "\<dots> = (Ipoly bs p) * (Ipoly bs p)^(Suc n div 2)*(Ipoly bs p)^(Suc n div 2)"
1.679 +      using "2.hyps" by simp
1.680 +    also have "\<dots> = (Ipoly bs p) ^ (Suc n div 2 + Suc n div 2 + 1)"
1.681 +      apply (simp only: power_add power_one_right) by simp
1.682 +    also have "\<dots> = (Ipoly bs p) ^ (Suc (Suc (Suc (0\<Colon>nat)) * (Suc n div Suc (Suc (0\<Colon>nat)))))"
1.683 +      by (simp only: th)
1.684 +    finally have ?case
1.685 +    using odd_nat_div_two_times_two_plus_one[OF odd, symmetric] by simp  }
1.686 +  moreover
1.687 +  {assume even: "even (Suc n)"
1.688 +    have th: "(Suc (Suc (0\<Colon>nat))) * (Suc n div Suc (Suc (0\<Colon>nat))) = Suc n div 2 + Suc n div 2" by arith
1.689 +    from even have "Ipoly bs (p ^\<^sub>p Suc n) = Ipoly bs ?d" by (simp add: Let_def)
1.690 +    also have "\<dots> = (Ipoly bs p) ^ (Suc n div 2 + Suc n div 2)"
1.691 +      using "2.hyps" apply (simp only: power_add) by simp
1.692 +    finally have ?case using even_nat_div_two_times_two[OF even] by (simp only: th)}
1.693 +  ultimately show ?case by blast
1.694 +qed
1.695 +
1.696 +lemma polypow_normh:
1.697 +    assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
1.698 +  shows "isnpolyh p n \<Longrightarrow> isnpolyh (polypow k p) n"
1.699 +proof (induct k arbitrary: n rule: polypow.induct)
1.700 +  case (2 k n)
1.701 +  let ?q = "polypow (Suc k div 2) p"
1.702 +  let ?d = "polymul (?q,?q)"
1.703 +  from prems have th1:"isnpolyh ?q n" and th2: "isnpolyh p n" by blast+
1.704 +  from polymul_normh[OF th1 th1] have dn: "isnpolyh ?d n" by simp
1.705 +  from polymul_normh[OF th2 dn] have on: "isnpolyh (polymul(p,?d)) n" by simp
1.706 +  from dn on show ?case by (simp add: Let_def)
1.707 +qed auto
1.708 +
1.709 +lemma polypow_norm:
1.710 +  assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
1.711 +  shows "isnpoly p \<Longrightarrow> isnpoly (polypow k p)"
1.712 +  by (simp add: polypow_normh isnpoly_def)
1.713 +
1.714 +text{* Finally the whole normalization*}
1.715 +
1.716 +lemma polynate[simp]: "Ipoly bs (polynate p) = (Ipoly bs p :: 'a ::{ring_char_0,division_by_zero,field})"
1.717 +by (induct p rule:polynate.induct, auto)
1.718 +
1.719 +lemma polynate_norm[simp]:
1.720 +  assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
1.721 +  shows "isnpoly (polynate p)"
1.722 +  by (induct p rule: polynate.induct, simp_all add: polyadd_norm polymul_norm polysub_norm polyneg_norm polypow_norm) (simp_all add: isnpoly_def)
1.723 +
1.724 +text{* shift1 *}
1.725 +
1.726 +
1.727 +lemma shift1: "Ipoly bs (shift1 p) = Ipoly bs (Mul (Bound 0) p)"
1.728 +by (simp add: shift1_def polymul)
1.729 +
1.730 +lemma shift1_isnpoly:
1.731 +  assumes pn: "isnpoly p" and pnz: "p \<noteq> 0\<^sub>p" shows "isnpoly (shift1 p) "
1.732 +  using pn pnz by (simp add: shift1_def isnpoly_def )
1.733 +
1.734 +lemma shift1_nz[simp]:"shift1 p \<noteq> 0\<^sub>p"
1.735 +  by (simp add: shift1_def)
1.736 +lemma funpow_shift1_isnpoly:
1.737 +  "\<lbrakk> isnpoly p ; p \<noteq> 0\<^sub>p\<rbrakk> \<Longrightarrow> isnpoly (funpow n shift1 p)"
1.738 +  by (induct n arbitrary: p, auto simp add: shift1_isnpoly)
1.739 +
1.740 +lemma funpow_isnpolyh:
1.741 +  assumes f: "\<And> p. isnpolyh p n \<Longrightarrow> isnpolyh (f p) n "and np: "isnpolyh p n"
1.742 +  shows "isnpolyh (funpow k f p) n"
1.743 +  using f np by (induct k arbitrary: p, auto)
1.744 +
1.745 +lemma funpow_shift1: "(Ipoly bs (funpow n shift1 p) :: 'a :: {ring_char_0,division_by_zero,field}) = Ipoly bs (Mul (Pw (Bound 0) n) p)"
1.746 +  by (induct n arbitrary: p, simp_all add: shift1_isnpoly shift1 power_Suc )
1.747 +
1.748 +lemma shift1_isnpolyh: "isnpolyh p n0 \<Longrightarrow> p\<noteq> 0\<^sub>p \<Longrightarrow> isnpolyh (shift1 p) 0"
1.749 +  using isnpolyh_mono[where n="n0" and n'="0" and p="p"] by (simp add: shift1_def)
1.750 +
1.751 +lemma funpow_shift1_1:
1.752 +  "(Ipoly bs (funpow n shift1 p) :: 'a :: {ring_char_0,division_by_zero,field}) = Ipoly bs (funpow n shift1 1\<^sub>p *\<^sub>p p)"
1.753 +  by (simp add: funpow_shift1)
1.754 +
1.755 +lemma poly_cmul[simp]: "Ipoly bs (poly_cmul c p) = Ipoly bs (Mul (C c) p)"
1.756 +by (induct p  arbitrary: n0 rule: poly_cmul.induct, auto simp add: ring_simps)
1.757 +
1.758 +lemma behead:
1.759 +  assumes np: "isnpolyh p n"
1.760 +  shows "Ipoly bs (Add (Mul (head p) (Pw (Bound 0) (degree p))) (behead p)) = (Ipoly bs p :: 'a :: {ring_char_0,division_by_zero,field})"
1.761 +  using np
1.762 +proof (induct p arbitrary: n rule: behead.induct)
1.763 +  case (1 c p n) hence pn: "isnpolyh p n" by simp
1.764 +  from prems(2)[OF pn]
1.765 +  have th:"Ipoly bs (Add (Mul (head p) (Pw (Bound 0) (degree p))) (behead p)) = Ipoly bs p" .
1.766 +  then show ?case using "1.hyps" apply (simp add: Let_def,cases "behead p = 0\<^sub>p")
1.767 +    by (simp_all add: th[symmetric] ring_simps power_Suc)
1.768 +qed (auto simp add: Let_def)
1.769 +
1.770 +lemma behead_isnpolyh:
1.771 +  assumes np: "isnpolyh p n" shows "isnpolyh (behead p) n"
1.772 +  using np by (induct p rule: behead.induct, auto simp add: Let_def isnpolyh_mono)
1.773 +
1.774 +subsection{* Miscilanious lemmas about indexes, decrementation, substitution  etc ... *}
1.775 +lemma isnpolyh_polybound0: "isnpolyh p (Suc n) \<Longrightarrow> polybound0 p"
1.776 +proof(induct p arbitrary: n rule: polybound0.induct, auto)
1.777 +  case (goal1 c n p n')
1.778 +  hence "n = Suc (n - 1)" by simp
1.779 +  hence "isnpolyh p (Suc (n - 1))"  using `isnpolyh p n` by simp
1.780 +  with prems(2) show ?case by simp
1.781 +qed
1.782 +
1.783 +lemma isconstant_polybound0: "isnpolyh p n0 \<Longrightarrow> isconstant p \<longleftrightarrow> polybound0 p"
1.784 +by (induct p arbitrary: n0 rule: isconstant.induct, auto simp add: isnpolyh_polybound0)
1.785 +
1.786 +lemma decrpoly_zero[simp]: "decrpoly p = 0\<^sub>p \<longleftrightarrow> p = 0\<^sub>p" by (induct p, auto)
1.787 +
1.788 +lemma decrpoly_normh: "isnpolyh p n0 \<Longrightarrow> polybound0 p \<Longrightarrow> isnpolyh (decrpoly p) (n0 - 1)"
1.789 +  apply (induct p arbitrary: n0, auto)
1.790 +  apply (atomize)
1.791 +  apply (erule_tac x = "Suc nat" in allE)
1.792 +  apply auto
1.793 +  done
1.794 +
1.795 +lemma head_polybound0: "isnpolyh p n0 \<Longrightarrow> polybound0 (head p)"
1.796 + by (induct p  arbitrary: n0 rule: head.induct, auto intro: isnpolyh_polybound0)
1.797 +
1.798 +lemma polybound0_I:
1.799 +  assumes nb: "polybound0 a"
1.800 +  shows "Ipoly (b#bs) a = Ipoly (b'#bs) a"
1.801 +using nb
1.802 +by (induct a rule: polybound0.induct) auto
1.803 +lemma polysubst0_I:
1.804 +  shows "Ipoly (b#bs) (polysubst0 a t) = Ipoly ((Ipoly (b#bs) a)#bs) t"
1.805 +  by (induct t) simp_all
1.806 +
1.807 +lemma polysubst0_I':
1.808 +  assumes nb: "polybound0 a"
1.809 +  shows "Ipoly (b#bs) (polysubst0 a t) = Ipoly ((Ipoly (b'#bs) a)#bs) t"
1.810 +  by (induct t) (simp_all add: polybound0_I[OF nb, where b="b" and b'="b'"])
1.811 +
1.812 +lemma decrpoly: assumes nb: "polybound0 t"
1.813 +  shows "Ipoly (x#bs) t = Ipoly bs (decrpoly t)"
1.814 +  using nb by (induct t rule: decrpoly.induct, simp_all)
1.815 +
1.816 +lemma polysubst0_polybound0: assumes nb: "polybound0 t"
1.817 +  shows "polybound0 (polysubst0 t a)"
1.818 +using nb by (induct a rule: polysubst0.induct, auto)
1.819 +
1.820 +lemma degree0_polybound0: "isnpolyh p n \<Longrightarrow> degree p = 0 \<Longrightarrow> polybound0 p"
1.821 +  by (induct p arbitrary: n rule: degree.induct, auto simp add: isnpolyh_polybound0)
1.822 +
1.823 +fun maxindex :: "poly \<Rightarrow> nat" where
1.824 +  "maxindex (Bound n) = n + 1"
1.825 +| "maxindex (CN c n p) = max  (n + 1) (max (maxindex c) (maxindex p))"
1.826 +| "maxindex (Add p q) = max (maxindex p) (maxindex q)"
1.827 +| "maxindex (Sub p q) = max (maxindex p) (maxindex q)"
1.828 +| "maxindex (Mul p q) = max (maxindex p) (maxindex q)"
1.829 +| "maxindex (Neg p) = maxindex p"
1.830 +| "maxindex (Pw p n) = maxindex p"
1.831 +| "maxindex (C x) = 0"
1.832 +
1.833 +definition wf_bs :: "'a list \<Rightarrow> poly \<Rightarrow> bool" where
1.834 +  "wf_bs bs p = (length bs \<ge> maxindex p)"
1.835 +
1.836 +lemma wf_bs_coefficients: "wf_bs bs p \<Longrightarrow> \<forall> c \<in> set (coefficients p). wf_bs bs c"
1.837 +proof(induct p rule: coefficients.induct)
1.838 +  case (1 c p)
1.839 +  show ?case
1.840 +  proof
1.841 +    fix x assume xc: "x \<in> set (coefficients (CN c 0 p))"
1.842 +    hence "x = c \<or> x \<in> set (coefficients p)" by simp
1.843 +    moreover
1.844 +    {assume "x = c" hence "wf_bs bs x" using "1.prems"  unfolding wf_bs_def by simp}
1.845 +    moreover
1.846 +    {assume H: "x \<in> set (coefficients p)"
1.847 +      from "1.prems" have "wf_bs bs p" unfolding wf_bs_def by simp
1.848 +      with "1.hyps" H have "wf_bs bs x" by blast }
1.849 +    ultimately  show "wf_bs bs x" by blast
1.850 +  qed
1.851 +qed simp_all
1.852 +
1.853 +lemma maxindex_coefficients: " \<forall>c\<in> set (coefficients p). maxindex c \<le> maxindex p"
1.854 +by (induct p rule: coefficients.induct, auto)
1.855 +
1.856 +lemma length_exists: "\<exists>xs. length xs = n" by (rule exI[where x="replicate n x"], simp)
1.857 +
1.858 +lemma wf_bs_I: "wf_bs bs p ==> Ipoly (bs@bs') p = Ipoly bs p"
1.859 +  unfolding wf_bs_def by (induct p, auto simp add: nth_append)
1.860 +
1.861 +lemma take_maxindex_wf: assumes wf: "wf_bs bs p"
1.862 +  shows "Ipoly (take (maxindex p) bs) p = Ipoly bs p"
1.863 +proof-
1.864 +  let ?ip = "maxindex p"
1.865 +  let ?tbs = "take ?ip bs"
1.866 +  from wf have "length ?tbs = ?ip" unfolding wf_bs_def by simp
1.867 +  hence wf': "wf_bs ?tbs p" unfolding wf_bs_def by  simp
1.868 +  have eq: "bs = ?tbs @ (drop ?ip bs)" by simp
1.869 +  from wf_bs_I[OF wf', of "drop ?ip bs"] show ?thesis using eq by simp
1.870 +qed
1.871 +
1.872 +lemma decr_maxindex: "polybound0 p \<Longrightarrow> maxindex (decrpoly p) = maxindex p - 1"
1.873 +  by (induct p, auto)
1.874 +
1.875 +lemma wf_bs_insensitive: "length bs = length bs' \<Longrightarrow> wf_bs bs p = wf_bs bs' p"
1.876 +  unfolding wf_bs_def by simp
1.877 +
1.878 +lemma wf_bs_insensitive': "wf_bs (x#bs) p = wf_bs (y#bs) p"
1.879 +  unfolding wf_bs_def by simp
1.880 +
1.881 +
1.882 +
1.883 +lemma wf_bs_coefficients': "\<forall>c \<in> set (coefficients p). wf_bs bs c \<Longrightarrow> wf_bs (x#bs) p"
1.884 +by(induct p rule: coefficients.induct, auto simp add: wf_bs_def)
1.885 +lemma coefficients_Nil[simp]: "coefficients p \<noteq> []"
1.886 +  by (induct p rule: coefficients.induct, simp_all)
1.887 +
1.888 +
1.889 +lemma coefficients_head: "last (coefficients p) = head p"
1.890 +  by (induct p rule: coefficients.induct, auto)
1.891 +
1.892 +lemma wf_bs_decrpoly: "wf_bs bs (decrpoly p) \<Longrightarrow> wf_bs (x#bs) p"
1.893 +  unfolding wf_bs_def by (induct p rule: decrpoly.induct, auto)
1.894 +
1.895 +lemma length_le_list_ex: "length xs \<le> n \<Longrightarrow> \<exists> ys. length (xs @ ys) = n"
1.896 +  apply (rule exI[where x="replicate (n - length xs) z"])
1.897 +  by simp
1.898 +lemma isnpolyh_Suc_const:"isnpolyh p (Suc n) \<Longrightarrow> isconstant p"
1.899 +by (cases p, auto) (case_tac "nat", simp_all)
1.900 +
1.901 +lemma wf_bs_polyadd: "wf_bs bs p \<and> wf_bs bs q \<longrightarrow> wf_bs bs (p +\<^sub>p q)"
1.902 +  unfolding wf_bs_def
1.903 +  apply (induct p q rule: polyadd.induct)
1.904 +  apply (auto simp add: Let_def)
1.905 +  done
1.906 +
1.907 +lemma wf_bs_polyul: "wf_bs bs p \<Longrightarrow> wf_bs bs q \<Longrightarrow> wf_bs bs (p *\<^sub>p q)"
1.908 +
1.909 + unfolding wf_bs_def
1.910 +  apply (induct p q arbitrary: bs rule: polymul.induct)
1.911 +  apply (simp_all add: wf_bs_polyadd)
1.912 +  apply clarsimp
1.913 +  apply (rule wf_bs_polyadd[unfolded wf_bs_def, rule_format])
1.914 +  apply auto
1.915 +  done
1.916 +
1.917 +lemma wf_bs_polyneg: "wf_bs bs p \<Longrightarrow> wf_bs bs (~\<^sub>p p)"
1.918 +  unfolding wf_bs_def by (induct p rule: polyneg.induct, auto)
1.919 +
1.920 +lemma wf_bs_polysub: "wf_bs bs p \<Longrightarrow> wf_bs bs q \<Longrightarrow> wf_bs bs (p -\<^sub>p q)"
1.921 +  unfolding polysub_def split_def fst_conv snd_conv using wf_bs_polyadd wf_bs_polyneg by blast
1.922 +
1.923 +subsection{* Canonicity of polynomial representation, see lemma isnpolyh_unique*}
1.924 +
1.925 +definition "polypoly bs p = map (Ipoly bs) (coefficients p)"
1.926 +definition "polypoly' bs p = map ((Ipoly bs o decrpoly)) (coefficients p)"
1.927 +definition "poly_nate bs p = map ((Ipoly bs o decrpoly)) (coefficients (polynate p))"
1.928 +
1.929 +lemma coefficients_normh: "isnpolyh p n0 \<Longrightarrow> \<forall> q \<in> set (coefficients p). isnpolyh q n0"
1.930 +proof (induct p arbitrary: n0 rule: coefficients.induct)
1.931 +  case (1 c p n0)
1.932 +  have cp: "isnpolyh (CN c 0 p) n0" by fact
1.933 +  hence norm: "isnpolyh c 0" "isnpolyh p 0" "p \<noteq> 0\<^sub>p" "n0 = 0"
1.934 +    by (auto simp add: isnpolyh_mono[where n'=0])
1.935 +  from "1.hyps"[OF norm(2)] norm(1) norm(4)  show ?case by simp
1.936 +qed auto
1.937 +
1.938 +lemma coefficients_isconst:
1.939 +  "isnpolyh p n \<Longrightarrow> \<forall>q\<in>set (coefficients p). isconstant q"
1.940 +  by (induct p arbitrary: n rule: coefficients.induct,
1.941 +    auto simp add: isnpolyh_Suc_const)
1.942 +
1.943 +lemma polypoly_polypoly':
1.944 +  assumes np: "isnpolyh p n0"
1.945 +  shows "polypoly (x#bs) p = polypoly' bs p"
1.946 +proof-
1.947 +  let ?cf = "set (coefficients p)"
1.948 +  from coefficients_normh[OF np] have cn_norm: "\<forall> q\<in> ?cf. isnpolyh q n0" .
1.949 +  {fix q assume q: "q \<in> ?cf"
1.950 +    from q cn_norm have th: "isnpolyh q n0" by blast
1.951 +    from coefficients_isconst[OF np] q have "isconstant q" by blast
1.952 +    with isconstant_polybound0[OF th] have "polybound0 q" by blast}
1.953 +  hence "\<forall>q \<in> ?cf. polybound0 q" ..
1.954 +  hence "\<forall>q \<in> ?cf. Ipoly (x#bs) q = Ipoly bs (decrpoly q)"
1.955 +    using polybound0_I[where b=x and bs=bs and b'=y] decrpoly[where x=x and bs=bs]
1.956 +    by auto
1.957 +
1.958 +  thus ?thesis unfolding polypoly_def polypoly'_def by simp
1.959 +qed
1.960 +
1.961 +lemma polypoly_poly:
1.962 +  assumes np: "isnpolyh p n0" shows "Ipoly (x#bs) p = poly (polypoly (x#bs) p) x"
1.963 +  using np
1.964 +by (induct p arbitrary: n0 bs rule: coefficients.induct, auto simp add: polypoly_def)
1.965 +
1.966 +lemma polypoly'_poly:
1.967 +  assumes np: "isnpolyh p n0" shows "\<lparr>p\<rparr>\<^sub>p\<^bsup>x # bs\<^esup> = poly (polypoly' bs p) x"
1.968 +  using polypoly_poly[OF np, simplified polypoly_polypoly'[OF np]] .
1.969 +
1.970 +
1.971 +lemma polypoly_poly_polybound0:
1.972 +  assumes np: "isnpolyh p n0" and nb: "polybound0 p"
1.973 +  shows "polypoly bs p = [Ipoly bs p]"
1.974 +  using np nb unfolding polypoly_def
1.975 +  by (cases p, auto, case_tac nat, auto)
1.976 +
1.977 +lemma head_isnpolyh: "isnpolyh p n0 \<Longrightarrow> isnpolyh (head p) n0"
1.978 +  by (induct p rule: head.induct, auto)
1.979 +
1.980 +lemma headn_nz[simp]: "isnpolyh p n0 \<Longrightarrow> (headn p m = 0\<^sub>p) = (p = 0\<^sub>p)"
1.981 +  by (cases p,auto)
1.982 +
1.983 +lemma head_eq_headn0: "head p = headn p 0"
1.984 +  by (induct p rule: head.induct, simp_all)
1.985 +
1.986 +lemma head_nz[simp]: "isnpolyh p n0 \<Longrightarrow> (head p = 0\<^sub>p) = (p = 0\<^sub>p)"
1.987 +  by (simp add: head_eq_headn0)
1.988 +
1.989 +lemma isnpolyh_zero_iff:
1.990 +  assumes nq: "isnpolyh p n0" and eq :"\<forall>bs. wf_bs bs p \<longrightarrow> \<lparr>p\<rparr>\<^sub>p\<^bsup>bs\<^esup> = (0::'a::{ring_char_0,power,division_by_zero,field})"
1.991 +  shows "p = 0\<^sub>p"
1.992 +using nq eq
1.993 +proof (induct n\<equiv>"maxindex p" arbitrary: p n0 rule: nat_less_induct)
1.994 +  fix n p n0
1.995 +  assume H: "\<forall>m<n. \<forall>p n0. isnpolyh p n0 \<longrightarrow>
1.996 +    (\<forall>bs. wf_bs bs p \<longrightarrow> \<lparr>p\<rparr>\<^sub>p\<^bsup>bs\<^esup> = (0::'a)) \<longrightarrow> m = maxindex p \<longrightarrow> p = 0\<^sub>p"
1.997 +    and np: "isnpolyh p n0" and zp: "\<forall>bs. wf_bs bs p \<longrightarrow> \<lparr>p\<rparr>\<^sub>p\<^bsup>bs\<^esup> = (0::'a)" and n: "n = maxindex p"
1.998 +  {assume nz: "n = 0"
1.999 +    then obtain c where "p = C c" using n np by (cases p, auto)
1.1000 +    with zp np have "p = 0\<^sub>p" unfolding wf_bs_def by simp}
1.1001 +  moreover
1.1002 +  {assume nz: "n \<noteq> 0"
1.1003 +    let ?h = "head p"
1.1004 +    let ?hd = "decrpoly ?h"
1.1005 +    let ?ihd = "maxindex ?hd"
1.1006 +    from head_isnpolyh[OF np] head_polybound0[OF np] have h:"isnpolyh ?h n0" "polybound0 ?h"
1.1007 +      by simp_all
1.1008 +    hence nhd: "isnpolyh ?hd (n0 - 1)" using decrpoly_normh by blast
1.1009 +
1.1010 +    from maxindex_coefficients[of p] coefficients_head[of p, symmetric]
1.1011 +    have mihn: "maxindex ?h \<le> n" unfolding n by auto
1.1012 +    with decr_maxindex[OF h(2)] nz  have ihd_lt_n: "?ihd < n" by auto
1.1013 +    {fix bs:: "'a list"  assume bs: "wf_bs bs ?hd"
1.1014 +      let ?ts = "take ?ihd bs"
1.1015 +      let ?rs = "drop ?ihd bs"
1.1016 +      have ts: "wf_bs ?ts ?hd" using bs unfolding wf_bs_def by simp
1.1017 +      have bs_ts_eq: "?ts@ ?rs = bs" by simp
1.1018 +      from wf_bs_decrpoly[OF ts] have tsh: " \<forall>x. wf_bs (x#?ts) ?h" by simp
1.1019 +      from ihd_lt_n have "ALL x. length (x#?ts) \<le> n" by simp
1.1020 +      with length_le_list_ex obtain xs where xs:"length ((x#?ts) @ xs) = n" by blast
1.1021 +      hence "\<forall> x. wf_bs ((x#?ts) @ xs) p" using n unfolding wf_bs_def by simp
1.1022 +      with zp have "\<forall> x. Ipoly ((x#?ts) @ xs) p = 0" by blast
1.1023 +      hence "\<forall> x. Ipoly (x#(?ts @ xs)) p = 0" by simp
1.1024 +      with polypoly_poly[OF np, where ?'a = 'a] polypoly_polypoly'[OF np, where ?'a = 'a]
1.1025 +      have "\<forall>x. poly (polypoly' (?ts @ xs) p) x = poly [] x"  by simp
1.1026 +      hence "poly (polypoly' (?ts @ xs) p) = poly []" by (auto intro: ext)
1.1027 +      hence "\<forall> c \<in> set (coefficients p). Ipoly (?ts @ xs) (decrpoly c) = 0"
1.1028 +	thm poly_zero
1.1029 +	using poly_zero[where ?'a='a] by (simp add: polypoly'_def list_all_iff)
1.1030 +      with coefficients_head[of p, symmetric]
1.1031 +      have th0: "Ipoly (?ts @ xs) ?hd = 0" by simp
1.1032 +      from bs have wf'': "wf_bs ?ts ?hd" unfolding wf_bs_def by simp
1.1033 +      with th0 wf_bs_I[of ?ts ?hd xs] have "Ipoly ?ts ?hd = 0" by simp
1.1034 +      with wf'' wf_bs_I[of ?ts ?hd ?rs] bs_ts_eq have "\<lparr>?hd\<rparr>\<^sub>p\<^bsup>bs\<^esup> = 0" by simp }
1.1035 +    then have hdz: "\<forall>bs. wf_bs bs ?hd \<longrightarrow> \<lparr>?hd\<rparr>\<^sub>p\<^bsup>bs\<^esup> = (0::'a)" by blast
1.1036 +
1.1037 +    from H[rule_format, OF ihd_lt_n nhd] hdz have "?hd = 0\<^sub>p" by blast
1.1038 +    hence "?h = 0\<^sub>p" by simp
1.1039 +    with head_nz[OF np] have "p = 0\<^sub>p" by simp}
1.1040 +  ultimately show "p = 0\<^sub>p" by blast
1.1041 +qed
1.1042 +
1.1043 +lemma isnpolyh_unique:
1.1044 +  assumes np:"isnpolyh p n0" and nq: "isnpolyh q n1"
1.1045 +  shows "(\<forall>bs. \<lparr>p\<rparr>\<^sub>p\<^bsup>bs\<^esup> = (\<lparr>q\<rparr>\<^sub>p\<^bsup>bs\<^esup> :: 'a::{ring_char_0,power,division_by_zero,field})) \<longleftrightarrow>  p = q"
1.1046 +proof(auto)
1.1047 +  assume H: "\<forall>bs. (\<lparr>p\<rparr>\<^sub>p\<^bsup>bs\<^esup> ::'a)= \<lparr>q\<rparr>\<^sub>p\<^bsup>bs\<^esup>"
1.1048 +  hence "\<forall>bs.\<lparr>p -\<^sub>p q\<rparr>\<^sub>p\<^bsup>bs\<^esup>= (0::'a)" by simp
1.1049 +  hence "\<forall>bs. wf_bs bs (p -\<^sub>p q) \<longrightarrow> \<lparr>p -\<^sub>p q\<rparr>\<^sub>p\<^bsup>bs\<^esup> = (0::'a)"
1.1050 +    using wf_bs_polysub[where p=p and q=q] by auto
1.1051 +  with isnpolyh_zero_iff[OF polysub_normh[OF np nq]] polysub_0[OF np nq]
1.1052 +  show "p = q" by blast
1.1053 +qed
1.1054 +
1.1055 +
1.1056 +text{* consequenses of unicity on the algorithms for polynomial normalization *}
1.1057 +
1.1058 +lemma polyadd_commute:   assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
1.1059 +  and np: "isnpolyh p n0" and nq: "isnpolyh q n1" shows "p +\<^sub>p q = q +\<^sub>p p"
1.1060 +  using isnpolyh_unique[OF polyadd_normh[OF np nq] polyadd_normh[OF nq np]] by simp
1.1061 +
1.1062 +lemma zero_normh: "isnpolyh 0\<^sub>p n" by simp
1.1063 +lemma one_normh: "isnpolyh 1\<^sub>p n" by simp
1.1064 +lemma polyadd_0[simp]:
1.1065 +  assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
1.1066 +  and np: "isnpolyh p n0" shows "p +\<^sub>p 0\<^sub>p = p" and "0\<^sub>p +\<^sub>p p = p"
1.1067 +  using isnpolyh_unique[OF polyadd_normh[OF np zero_normh] np]
1.1068 +    isnpolyh_unique[OF polyadd_normh[OF zero_normh np] np] by simp_all
1.1069 +
1.1070 +lemma polymul_1[simp]:
1.1071 +    assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
1.1072 +  and np: "isnpolyh p n0" shows "p *\<^sub>p 1\<^sub>p = p" and "1\<^sub>p *\<^sub>p p = p"
1.1073 +  using isnpolyh_unique[OF polymul_normh[OF np one_normh] np]
1.1074 +    isnpolyh_unique[OF polymul_normh[OF one_normh np] np] by simp_all
1.1075 +lemma polymul_0[simp]:
1.1076 +  assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
1.1077 +  and np: "isnpolyh p n0" shows "p *\<^sub>p 0\<^sub>p = 0\<^sub>p" and "0\<^sub>p *\<^sub>p p = 0\<^sub>p"
1.1078 +  using isnpolyh_unique[OF polymul_normh[OF np zero_normh] zero_normh]
1.1079 +    isnpolyh_unique[OF polymul_normh[OF zero_normh np] zero_normh] by simp_all
1.1080 +
1.1081 +lemma polymul_commute:
1.1082 +    assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
1.1083 +  and np:"isnpolyh p n0" and nq: "isnpolyh q n1"
1.1084 +  shows "p *\<^sub>p q = q *\<^sub>p p"
1.1085 +using isnpolyh_unique[OF polymul_normh[OF np nq] polymul_normh[OF nq np], where ?'a = "'a\<Colon>{ring_char_0,power,division_by_zero,field}"] by simp
1.1086 +
1.1087 +declare polyneg_polyneg[simp]
1.1088 +
1.1089 +lemma isnpolyh_polynate_id[simp]:
1.1090 +  assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
1.1091 +  and np:"isnpolyh p n0" shows "polynate p = p"
1.1092 +  using isnpolyh_unique[where ?'a= "'a::{ring_char_0,division_by_zero,field}", OF polynate_norm[of p, unfolded isnpoly_def] np] polynate[where ?'a = "'a::{ring_char_0,division_by_zero,field}"] by simp
1.1093 +
1.1094 +lemma polynate_idempotent[simp]:
1.1095 +    assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
1.1096 +  shows "polynate (polynate p) = polynate p"
1.1097 +  using isnpolyh_polynate_id[OF polynate_norm[of p, unfolded isnpoly_def]] .
1.1098 +
1.1099 +lemma poly_nate_polypoly': "poly_nate bs p = polypoly' bs (polynate p)"
1.1100 +  unfolding poly_nate_def polypoly'_def ..
1.1101 +lemma poly_nate_poly: shows "poly (poly_nate bs p) = (\<lambda>x:: 'a ::{ring_char_0,division_by_zero,field}. \<lparr>p\<rparr>\<^sub>p\<^bsup>x # bs\<^esup>)"
1.1102 +  using polypoly'_poly[OF polynate_norm[unfolded isnpoly_def], symmetric, of bs p]
1.1103 +  unfolding poly_nate_polypoly' by (auto intro: ext)
1.1104 +
1.1105 +subsection{* heads, degrees and all that *}
1.1106 +lemma degree_eq_degreen0: "degree p = degreen p 0"
1.1107 +  by (induct p rule: degree.induct, simp_all)
1.1108 +
1.1109 +lemma degree_polyneg: assumes n: "isnpolyh p n"
1.1110 +  shows "degree (polyneg p) = degree p"
1.1111 +  using n
1.1112 +  by (induct p arbitrary: n rule: polyneg.induct, simp_all) (case_tac na, auto)
1.1113 +
1.1114 +lemma degree_polyadd:
1.1115 +  assumes np: "isnpolyh p n0" and nq: "isnpolyh q n1"
1.1116 +  shows "degree (p +\<^sub>p q) \<le> max (degree p) (degree q)"
1.1117 +using degreen_polyadd[OF np nq, where m= "0"] degree_eq_degreen0 by simp
1.1118 +
1.1119 +
1.1120 +lemma degree_polysub: assumes np: "isnpolyh p n0" and nq: "isnpolyh q n1"
1.1121 +  shows "degree (p -\<^sub>p q) \<le> max (degree p) (degree q)"
1.1122 +proof-
1.1123 +  from nq have nq': "isnpolyh (~\<^sub>p q) n1" using polyneg_normh by simp
1.1124 +  from degree_polyadd[OF np nq'] show ?thesis by (simp add: polysub_def degree_polyneg[OF nq])
1.1125 +qed
1.1126 +
1.1127 +lemma degree_polysub_samehead:
1.1128 +  assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
1.1129 +  and np: "isnpolyh p n0" and nq: "isnpolyh q n1" and h: "head p = head q"
1.1130 +  and d: "degree p = degree q"
1.1131 +  shows "degree (p -\<^sub>p q) < degree p \<or> (p -\<^sub>p q = 0\<^sub>p)"
1.1132 +unfolding polysub_def split_def fst_conv snd_conv
1.1133 +using np nq h d
1.1134 +proof(induct p q rule:polyadd.induct)
1.1135 +  case (1 a b a' b') thus ?case by (simp add: Nsub_def Nsub0[simplified Nsub_def])
1.1136 +next
1.1137 +  case (2 a b c' n' p')
1.1138 +  let ?c = "(a,b)"
1.1139 +  from prems have "degree (C ?c) = degree (CN c' n' p')" by simp
1.1140 +  hence nz:"n' > 0" by (cases n', auto)
1.1141 +  hence "head (CN c' n' p') = CN c' n' p'" by (cases n', auto)
1.1142 +  with prems show ?case by simp
1.1143 +next
1.1144 +  case (3 c n p a' b')
1.1145 +  let ?c' = "(a',b')"
1.1146 +  from prems have "degree (C ?c') = degree (CN c n p)" by simp
1.1147 +  hence nz:"n > 0" by (cases n, auto)
1.1148 +  hence "head (CN c n p) = CN c n p" by (cases n, auto)
1.1149 +  with prems show ?case by simp
1.1150 +next
1.1151 +  case (4 c n p c' n' p')
1.1152 +  hence H: "isnpolyh (CN c n p) n0" "isnpolyh (CN c' n' p') n1"
1.1153 +    "head (CN c n p) = head (CN c' n' p')" "degree (CN c n p) = degree (CN c' n' p')" by simp+
1.1154 +  hence degc: "degree c = 0" and degc': "degree c' = 0" by simp_all
1.1155 +  hence degnc: "degree (~\<^sub>p c) = 0" and degnc': "degree (~\<^sub>p c') = 0"
1.1156 +    using H(1-2) degree_polyneg by auto
1.1157 +  from H have cnh: "isnpolyh c (Suc n)" and c'nh: "isnpolyh c' (Suc n')"  by simp+
1.1158 +  from degree_polysub[OF cnh c'nh, simplified polysub_def] degc degc' have degcmc': "degree (c +\<^sub>p  ~\<^sub>pc') = 0"  by simp
1.1159 +  from H have pnh: "isnpolyh p n" and p'nh: "isnpolyh p' n'" by auto
1.1160 +  have "n = n' \<or> n < n' \<or> n > n'" by arith
1.1161 +  moreover
1.1162 +  {assume nn': "n = n'"
1.1163 +    have "n = 0 \<or> n >0" by arith
1.1164 +    moreover {assume nz: "n = 0" hence ?case using prems by (auto simp add: Let_def degcmc')}
1.1165 +    moreover {assume nz: "n > 0"
1.1166 +      with nn' H(3) have  cc':"c = c'" and pp': "p = p'" by (cases n, auto)+
1.1167 +      hence ?case using polysub_same_0[OF p'nh, simplified polysub_def split_def fst_conv snd_conv] polysub_same_0[OF c'nh, simplified polysub_def split_def fst_conv snd_conv] using nn' prems by (simp add: Let_def)}
1.1168 +    ultimately have ?case by blast}
1.1169 +  moreover
1.1170 +  {assume nn': "n < n'" hence n'p: "n' > 0" by simp
1.1171 +    hence headcnp':"head (CN c' n' p') = CN c' n' p'"  by (cases n', simp_all)
1.1172 +    have degcnp': "degree (CN c' n' p') = 0" and degcnpeq: "degree (CN c n p) = degree (CN c' n' p')" using prems by (cases n', simp_all)
1.1173 +    hence "n > 0" by (cases n, simp_all)
1.1174 +    hence headcnp: "head (CN c n p) = CN c n p" by (cases n, auto)
1.1175 +    from H(3) headcnp headcnp' nn' have ?case by auto}
1.1176 +  moreover
1.1177 +  {assume nn': "n > n'"  hence np: "n > 0" by simp
1.1178 +    hence headcnp:"head (CN c n p) = CN c n p"  by (cases n, simp_all)
1.1179 +    from prems have degcnpeq: "degree (CN c' n' p') = degree (CN c n p)" by simp
1.1180 +    from np have degcnp: "degree (CN c n p) = 0" by (cases n, simp_all)
1.1181 +    with degcnpeq have "n' > 0" by (cases n', simp_all)
1.1182 +    hence headcnp': "head (CN c' n' p') = CN c' n' p'" by (cases n', auto)
1.1183 +    from H(3) headcnp headcnp' nn' have ?case by auto}
1.1184 +  ultimately show ?case  by blast
1.1185 +qed auto
1.1186 +
1.1187 +lemma shift1_head : "isnpolyh p n0 \<Longrightarrow> head (shift1 p) = head p"
1.1188 +by (induct p arbitrary: n0 rule: head.induct, simp_all add: shift1_def)
1.1189 +
1.1190 +lemma funpow_shift1_head: "isnpolyh p n0 \<Longrightarrow> p\<noteq> 0\<^sub>p \<Longrightarrow> head (funpow k shift1 p) = head p"
1.1191 +proof(induct k arbitrary: n0 p)
1.1192 +  case (Suc k n0 p) hence "isnpolyh (shift1 p) 0" by (simp add: shift1_isnpolyh)
1.1193 +  with prems have "head (funpow k shift1 (shift1 p)) = head (shift1 p)"
1.1194 +    and "head (shift1 p) = head p" by (simp_all add: shift1_head)
1.1195 +  thus ?case by simp
1.1196 +qed auto
1.1197 +
1.1198 +lemma shift1_degree: "degree (shift1 p) = 1 + degree p"
1.1199 +  by (simp add: shift1_def)
1.1200 +lemma funpow_shift1_degree: "degree (funpow k shift1 p) = k + degree p "
1.1201 +  by (induct k arbitrary: p, auto simp add: shift1_degree)
1.1202 +
1.1203 +lemma funpow_shift1_nz: "p \<noteq> 0\<^sub>p \<Longrightarrow> funpow n shift1 p \<noteq> 0\<^sub>p"
1.1204 +  by (induct n arbitrary: p, simp_all add: funpow_def)
1.1205 +
1.1206 +lemma head_isnpolyh_Suc[simp]: "isnpolyh p (Suc n) \<Longrightarrow> head p = p"
1.1207 +  by (induct p arbitrary: n rule: degree.induct, auto)
1.1208 +lemma headn_0[simp]: "isnpolyh p n \<Longrightarrow> m < n \<Longrightarrow> headn p m = p"
1.1209 +  by (induct p arbitrary: n rule: degreen.induct, auto)
1.1210 +lemma head_isnpolyh_Suc': "n > 0 \<Longrightarrow> isnpolyh p n \<Longrightarrow> head p = p"
1.1211 +  by (induct p arbitrary: n rule: degree.induct, auto)
1.1212 +lemma head_head[simp]: "isnpolyh p n0 \<Longrightarrow> head (head p) = head p"
1.1213 +  by (induct p rule: head.induct, auto)
1.1214 +
1.1215 +lemma polyadd_eq_const_degree:
1.1216 +  "\<lbrakk> isnpolyh p n0 ; isnpolyh q n1 ; polyadd (p,q) = C c\<rbrakk> \<Longrightarrow> degree p = degree q"
1.1217 +  using polyadd_eq_const_degreen degree_eq_degreen0 by simp
1.1218 +
1.1219 +lemma polyadd_head: assumes np: "isnpolyh p n0" and nq: "isnpolyh q n1"
1.1220 +  and deg: "degree p \<noteq> degree q"
1.1221 +  shows "head (p +\<^sub>p q) = (if degree p < degree q then head q else head p)"
1.1222 +using np nq deg
1.1223 +apply(induct p q arbitrary: n0 n1 rule: polyadd.induct,simp_all)
1.1224 +apply (case_tac n', simp, simp)
1.1225 +apply (case_tac n, simp, simp)
1.1226 +apply (case_tac n, case_tac n', simp add: Let_def)
1.1227 +apply (case_tac "pa +\<^sub>p p' = 0\<^sub>p")
1.1228 +apply (clarsimp simp add: polyadd_eq_const_degree)
1.1229 +apply clarsimp
1.1230 +apply (erule_tac impE,blast)
1.1231 +apply (erule_tac impE,blast)
1.1232 +apply clarsimp
1.1233 +apply simp
1.1234 +apply (case_tac n', simp_all)
1.1235 +done
1.1236 +
1.1237 +lemma polymul_head_polyeq:
1.1238 +   assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
1.1239 +  shows "\<lbrakk>isnpolyh p n0; isnpolyh q n1 ; p \<noteq> 0\<^sub>p ; q \<noteq> 0\<^sub>p \<rbrakk> \<Longrightarrow> head (p *\<^sub>p q) = head p *\<^sub>p head q"
1.1240 +proof (induct p q arbitrary: n0 n1 rule: polymul.induct)
1.1241 +  case (2 a b c' n' p' n0 n1)
1.1242 +  hence "isnpolyh (head (CN c' n' p')) n1" "isnormNum (a,b)"  by (simp_all add: head_isnpolyh)
1.1243 +  thus ?case using prems by (cases n', auto)
1.1244 +next
1.1245 +  case (3 c n p a' b' n0 n1)
1.1246 +  hence "isnpolyh (head (CN c n p)) n0" "isnormNum (a',b')"  by (simp_all add: head_isnpolyh)
1.1247 +  thus ?case using prems by (cases n, auto)
1.1248 +next
1.1249 +  case (4 c n p c' n' p' n0 n1)
1.1250 +  hence norm: "isnpolyh p n" "isnpolyh c (Suc n)" "isnpolyh p' n'" "isnpolyh c' (Suc n')"
1.1251 +    "isnpolyh (CN c n p) n" "isnpolyh (CN c' n' p') n'"
1.1252 +    by simp_all
1.1253 +  have "n < n' \<or> n' < n \<or> n = n'" by arith
1.1254 +  moreover
1.1255 +  {assume nn': "n < n'" hence ?case
1.1256 +      thm prems
1.1257 +      using norm
1.1258 +    prems(6)[rule_format, OF nn' norm(1,6)]
1.1259 +    prems(7)[rule_format, OF nn' norm(2,6)] by (simp, cases n, simp,cases n', simp_all)}
1.1260 +  moreover {assume nn': "n'< n"
1.1261 +    hence stupid: "n' < n \<and> \<not> n < n'" by simp
1.1262 +    hence ?case using norm prems(4) [rule_format, OF stupid norm(5,3)]
1.1263 +      prems(5)[rule_format, OF stupid norm(5,4)]
1.1264 +      by (simp,cases n',simp,cases n,auto)}
1.1265 +  moreover {assume nn': "n' = n"
1.1266 +    hence stupid: "\<not> n' < n \<and> \<not> n < n'" by simp
1.1267 +    from nn' polymul_normh[OF norm(5,4)]
1.1268 +    have ncnpc':"isnpolyh (CN c n p *\<^sub>p c') n" by (simp add: min_def)
1.1269 +    from nn' polymul_normh[OF norm(5,3)] norm
1.1270 +    have ncnpp':"isnpolyh (CN c n p *\<^sub>p p') n" by simp
1.1271 +    from nn' ncnpp' polymul_eq0_iff[OF norm(5,3)] norm(6)
1.1272 +    have ncnpp0':"isnpolyh (CN 0\<^sub>p n (CN c n p *\<^sub>p p')) n" by simp
1.1273 +    from polyadd_normh[OF ncnpc' ncnpp0']
1.1274 +    have nth: "isnpolyh ((CN c n p *\<^sub>p c') +\<^sub>p (CN 0\<^sub>p n (CN c n p *\<^sub>p p'))) n"
1.1275 +      by (simp add: min_def)
1.1276 +    {assume np: "n > 0"
1.1277 +      with nn' head_isnpolyh_Suc'[OF np nth]
1.1278 +	head_isnpolyh_Suc'[OF np norm(5)] head_isnpolyh_Suc'[OF np norm(6)[simplified nn']]
1.1279 +      have ?case by simp}
1.1280 +    moreover
1.1281 +    {moreover assume nz: "n = 0"
1.1282 +      from polymul_degreen[OF norm(5,4), where m="0"]
1.1283 +	polymul_degreen[OF norm(5,3), where m="0"] nn' nz degree_eq_degreen0
1.1284 +      norm(5,6) degree_npolyhCN[OF norm(6)]
1.1285 +    have dth:"degree (CN c 0 p *\<^sub>p c') < degree (CN 0\<^sub>p 0 (CN c 0 p *\<^sub>p p'))" by simp
1.1286 +    hence dth':"degree (CN c 0 p *\<^sub>p c') \<noteq> degree (CN 0\<^sub>p 0 (CN c 0 p *\<^sub>p p'))" by simp
1.1287 +    from polyadd_head[OF ncnpc'[simplified nz] ncnpp0'[simplified nz] dth'] dth
1.1288 +    have ?case   using norm prems(2)[rule_format, OF stupid norm(5,3)]
1.1289 +	prems(3)[rule_format, OF stupid norm(5,4)] nn' nz by simp }
1.1290 +    ultimately have ?case by (cases n) auto}
1.1291 +  ultimately show ?case by blast
1.1292 +qed simp_all
1.1293 +
1.1294 +lemma degree_coefficients: "degree p = length (coefficients p) - 1"
1.1295 +  by(induct p rule: degree.induct, auto)
1.1296 +
1.1297 +lemma degree_head[simp]: "degree (head p) = 0"
1.1298 +  by (induct p rule: head.induct, auto)
1.1299 +
1.1300 +lemma degree_CN: "isnpolyh p n \<Longrightarrow> degree (CN c n p) \<le> 1+ degree p"
1.1301 +  by (cases n, simp_all)
1.1302 +lemma degree_CN': "isnpolyh p n \<Longrightarrow> degree (CN c n p) \<ge>  degree p"
1.1303 +  by (cases n, simp_all)
1.1304 +
1.1305 +lemma polyadd_different_degree: "\<lbrakk>isnpolyh p n0 ; isnpolyh q n1; degree p \<noteq> degree q\<rbrakk> \<Longrightarrow> degree (polyadd(p,q)) = max (degree p) (degree q)"
1.1306 +  using polyadd_different_degreen degree_eq_degreen0 by simp
1.1307 +
1.1308 +lemma degreen_polyneg: "isnpolyh p n0 \<Longrightarrow> degreen (~\<^sub>p p) m = degreen p m"
1.1309 +  by (induct p arbitrary: n0 rule: polyneg.induct, auto)
1.1310 +
1.1311 +lemma degree_polymul:
1.1312 +  assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
1.1313 +  and np: "isnpolyh p n0" and nq: "isnpolyh q n1"
1.1314 +  shows "degree (p *\<^sub>p q) \<le> degree p + degree q"
1.1315 +  using polymul_degreen[OF np nq, where m="0"]  degree_eq_degreen0 by simp
1.1316 +
1.1317 +lemma polyneg_degree: "isnpolyh p n \<Longrightarrow> degree (polyneg p) = degree p"
1.1318 +  by (induct p arbitrary: n rule: degree.induct, auto)
1.1319 +
1.1320 +lemma polyneg_head: "isnpolyh p n \<Longrightarrow> head(polyneg p) = polyneg (head p)"
1.1321 +  by (induct p arbitrary: n rule: degree.induct, auto)
1.1322 +
1.1323 +subsection {* Correctness of polynomial pseudo division *}
1.1324 +
1.1325 +lemma polydivide_aux_real_domintros:
1.1326 +  assumes call1: "\<lbrakk>s \<noteq> 0\<^sub>p; \<not> degree s < n; a = head s\<rbrakk>
1.1327 +  \<Longrightarrow> polydivide_aux_dom (a, n, p, k, s -\<^sub>p funpow (degree s - n) shift1 p)"
1.1328 +  and call2 : "\<lbrakk>s \<noteq> 0\<^sub>p; \<not> degree s < n; a \<noteq> head s\<rbrakk>
1.1329 +  \<Longrightarrow> polydivide_aux_dom(a, n, p,Suc k, a *\<^sub>p s -\<^sub>p (head s *\<^sub>p funpow (degree s - n) shift1 p))"
1.1330 +  shows "polydivide_aux_dom (a, n, p, k, s)"
1.1331 +proof (rule accpI, erule polydivide_aux_rel.cases)
1.1332 +  fix y aa ka na pa sa x xa xb
1.1333 +  assume eqs: "y = (aa, na, pa, ka, sa -\<^sub>p xb)" "(a, n, p, k, s) = (aa, na, pa, ka, sa)"
1.1334 +     and \<Gamma>1': "sa \<noteq> 0\<^sub>p" "x = head sa" "xa = degree sa" "\<not> xa < na"
1.1335 +    "xb = funpow (xa - na) shift1 pa" "aa = x"
1.1336 +
1.1337 +  hence \<Gamma>1: "s \<noteq> 0\<^sub>p" "a = head s" "xa = degree s" "\<not> degree s < n" "\<not> xa < na"
1.1338 +    "xb = funpow (xa - na) shift1 pa" "aa = x" by auto
1.1339 +
1.1340 +  with call1 have "polydivide_aux_dom (a, n, p, k, s -\<^sub>p funpow (degree s - n) shift1 p)"
1.1341 +    by auto
1.1342 +  with eqs \<Gamma>1 show "polydivide_aux_dom y" by auto
1.1343 +next
1.1344 +  fix y aa ka na pa sa x xa xb
1.1345 +  assume eqs: "y = (aa, na, pa, Suc ka, aa *\<^sub>p sa -\<^sub>p (x *\<^sub>p xb))"
1.1346 +    "(a, n, p, k, s) =(aa, na, pa, ka, sa)"
1.1347 +    and \<Gamma>2': "sa \<noteq> 0\<^sub>p" "x = head sa" "xa = degree sa" "\<not> xa < na"
1.1348 +    "xb = funpow (xa - na) shift1 pa" "aa \<noteq> x"
1.1349 +  hence \<Gamma>2: "s \<noteq> 0\<^sub>p" "a \<noteq> head s" "xa = degree s" "\<not> degree s < n"
1.1350 +    "xb = funpow (xa - na) shift1 pa" "aa \<noteq> x" by auto
1.1351 +  with call2 have "polydivide_aux_dom (a, n, p, Suc k, a *\<^sub>p s -\<^sub>p (head s *\<^sub>p funpow (degree s - n) shift1 p))" by auto
1.1352 +  with eqs \<Gamma>2'  show "polydivide_aux_dom y" by auto
1.1353 +qed
1.1354 +
1.1355 +lemma polydivide_aux_properties:
1.1356 +  assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
1.1357 +  and np: "isnpolyh p n0" and ns: "isnpolyh s n1"
1.1358 +  and ap: "head p = a" and ndp: "degree p = n" and pnz: "p \<noteq> 0\<^sub>p"
1.1359 +  shows "polydivide_aux_dom (a,n,p,k,s) \<and>
1.1360 +  (polydivide_aux (a,n,p,k,s) = (k',r) \<longrightarrow> (k' \<ge> k) \<and> (degree r = 0 \<or> degree r < degree p)
1.1361 +          \<and> (\<exists>nr. isnpolyh r nr) \<and> (\<exists>q n1. isnpolyh q n1 \<and> ((polypow (k' - k) a) *\<^sub>p s = p *\<^sub>p q +\<^sub>p r)))"
1.1362 +  using ns
1.1363 +proof(induct d\<equiv>"degree s" arbitrary: s k k' r n1 rule: nat_less_induct)
1.1364 +  fix d s k k' r n1
1.1365 +  let ?D = "polydivide_aux_dom"
1.1366 +  let ?dths = "?D (a, n, p, k, s)"
1.1367 +  let ?qths = "\<exists>q n1. isnpolyh q n1 \<and> (a ^\<^sub>p (k' - k) *\<^sub>p s = p *\<^sub>p q +\<^sub>p r)"
1.1368 +  let ?qrths = "polydivide_aux (a, n, p, k, s) = (k', r) \<longrightarrow>  k \<le> k' \<and> (degree r = 0 \<or> degree r < degree p)
1.1369 +    \<and> (\<exists>nr. isnpolyh r nr) \<and> ?qths"
1.1370 +  let ?ths = "?dths \<and> ?qrths"
1.1371 +  let ?b = "head s"
1.1372 +  let ?p' = "funpow (d - n) shift1 p"
1.1373 +  let ?xdn = "funpow (d - n) shift1 1\<^sub>p"
1.1374 +  let ?akk' = "a ^\<^sub>p (k' - k)"
1.1375 +  assume H: "\<forall>m<d. \<forall>x xa xb xc xd.
1.1376 +    isnpolyh x xd \<longrightarrow>
1.1377 +    m = degree x \<longrightarrow> ?D (a, n, p, xa, x) \<and>
1.1378 +    (polydivide_aux (a, n, p, xa, x) = (xb, xc) \<longrightarrow>
1.1379 +    xa \<le> xb \<and> (degree xc = 0 \<or> degree xc < degree p) \<and>
1.1380 +    (\<exists> nr. isnpolyh xc nr) \<and>
1.1381 +    (\<exists>q n1. isnpolyh q n1 \<and> a ^\<^sub>p xb - xa *\<^sub>p x = p *\<^sub>p q +\<^sub>p xc))"
1.1382 +    and ns: "isnpolyh s n1" and ds: "d = degree s"
1.1383 +  from np have np0: "isnpolyh p 0"
1.1384 +    using isnpolyh_mono[where n="n0" and n'="0" and p="p"]  by simp
1.1385 +  have np': "isnpolyh ?p' 0" using funpow_shift1_isnpoly[OF np0[simplified isnpoly_def[symmetric]] pnz, where n="d -n"] isnpoly_def by simp
1.1386 +  have headp': "head ?p' = head p" using funpow_shift1_head[OF np pnz] by simp
1.1387 +  from funpow_shift1_isnpoly[where p="1\<^sub>p"] have nxdn: "isnpolyh ?xdn 0" by (simp add: isnpoly_def)
1.1388 +  from polypow_normh [OF head_isnpolyh[OF np0], where k="k' - k"] ap
1.1389 +  have nakk':"isnpolyh ?akk' 0" by blast
1.1390 +  {assume sz: "s = 0\<^sub>p"
1.1391 +    hence dom: ?dths apply - apply (rule polydivide_aux_real_domintros) apply simp_all done
1.1392 +    from polydivide_aux.psimps[OF dom] sz
1.1393 +    have ?qrths using np apply clarsimp by (rule exI[where x="0\<^sub>p"], simp)
1.1394 +    hence ?ths using dom by blast}
1.1395 +  moreover
1.1396 +  {assume sz: "s \<noteq> 0\<^sub>p"
1.1397 +    {assume dn: "d < n"
1.1398 +      with sz ds  have dom:"?dths" by - (rule polydivide_aux_real_domintros,simp_all)
1.1399 +      from polydivide_aux.psimps[OF dom] sz dn ds
1.1400 +      have "?qrths" using ns ndp np by auto (rule exI[where x="0\<^sub>p"],simp)
1.1401 +      with dom have ?ths by blast}
1.1402 +    moreover
1.1403 +    {assume dn': "\<not> d < n" hence dn: "d \<ge> n" by arith
1.1404 +      have degsp': "degree s = degree ?p'"
1.1405 +	using ds dn ndp funpow_shift1_degree[where k = "d - n" and p="p"] by simp
1.1406 +      {assume ba: "?b = a"
1.1407 +	hence headsp': "head s = head ?p'" using ap headp' by simp
1.1408 +	have nr: "isnpolyh (s -\<^sub>p ?p') 0" using polysub_normh[OF ns np'] by simp
1.1409 +	from ds degree_polysub_samehead[OF ns np' headsp' degsp']
1.1410 +	have "degree (s -\<^sub>p ?p') < d \<or> s -\<^sub>p ?p' = 0\<^sub>p" by simp
1.1411 +	moreover
1.1412 +	{assume deglt:"degree (s -\<^sub>p ?p') < d"
1.1413 +	  from  H[rule_format, OF deglt nr,simplified]
1.1414 +	  have domsp: "?D (a, n, p, k, s -\<^sub>p ?p')" by blast
1.1415 +	  have dom: ?dths apply (rule polydivide_aux_real_domintros)
1.1416 +	    using ba ds dn' domsp by simp_all
1.1417 +	  from polydivide_aux.psimps[OF dom] sz dn' ba ds
1.1418 +	  have eq: "polydivide_aux (a,n,p,k,s) = polydivide_aux (a,n,p,k, s -\<^sub>p ?p')"
1.1419 +	    by (simp add: Let_def)
1.1420 +	  {assume h1: "polydivide_aux (a, n, p, k, s) = (k', r)"
1.1421 +	    from H[rule_format, OF deglt nr, where xa = "k" and xb="k'" and xc="r", simplified]
1.1422 +	      trans[OF eq[symmetric] h1]
1.1423 +	    have kk': "k \<le> k'" and nr:"\<exists>nr. isnpolyh r nr" and dr: "degree r = 0 \<or> degree r < degree p"
1.1424 +	      and q1:"\<exists>q nq. isnpolyh q nq \<and> (a ^\<^sub>p k' - k *\<^sub>p (s -\<^sub>p ?p') = p *\<^sub>p q +\<^sub>p r)" by auto
1.1425 +	    from q1 obtain q n1 where nq: "isnpolyh q n1"
1.1426 +	      and asp:"a^\<^sub>p (k' - k) *\<^sub>p (s -\<^sub>p ?p') = p *\<^sub>p q +\<^sub>p r"  by blast
1.1427 +	    from nr obtain nr where nr': "isnpolyh r nr" by blast
1.1428 +	    from polymul_normh[OF nakk' ns] have nakks': "isnpolyh (a ^\<^sub>p (k' - k) *\<^sub>p s) 0" by simp
1.1429 +	    from polyadd_normh[OF polymul_normh[OF nakk' nxdn] nq]
1.1430 +	    have nq': "isnpolyh (?akk' *\<^sub>p ?xdn +\<^sub>p q) 0" by simp
1.1431 +	    from polyadd_normh[OF polymul_normh[OF np
1.1432 +	      polyadd_normh[OF polymul_normh[OF nakk' nxdn] nq]] nr']
1.1433 +	    have nqr': "isnpolyh (p *\<^sub>p (?akk' *\<^sub>p ?xdn +\<^sub>p q) +\<^sub>p r) 0" by simp
1.1434 +	    from asp have "\<forall> (bs:: 'a::{ring_char_0,division_by_zero,field} list). Ipoly bs (a^\<^sub>p (k' - k) *\<^sub>p (s -\<^sub>p ?p')) =
1.1435 +	      Ipoly bs (p *\<^sub>p q +\<^sub>p r)" by simp
1.1436 +	    hence " \<forall>(bs:: 'a::{ring_char_0,division_by_zero,field} list). Ipoly bs (a^\<^sub>p (k' - k)*\<^sub>p s) =
1.1437 +	      Ipoly bs (a^\<^sub>p (k' - k)) * Ipoly bs ?p' + Ipoly bs p * Ipoly bs q + Ipoly bs r"
1.1438 +	      by (simp add: ring_simps)
1.1439 +	    hence " \<forall>(bs:: 'a::{ring_char_0,division_by_zero,field} list). Ipoly bs (a ^\<^sub>p (k' - k) *\<^sub>p s) =
1.1440 +	      Ipoly bs (a^\<^sub>p (k' - k)) * Ipoly bs (funpow (d - n) shift1 1\<^sub>p *\<^sub>p p)
1.1441 +	      + Ipoly bs p * Ipoly bs q + Ipoly bs r"
1.1442 +	      by (auto simp only: funpow_shift1_1)
1.1443 +	    hence "\<forall>(bs:: 'a::{ring_char_0,division_by_zero,field} list). Ipoly bs (a ^\<^sub>p (k' - k) *\<^sub>p s) =
1.1444 +	      Ipoly bs p * (Ipoly bs (a^\<^sub>p (k' - k)) * Ipoly bs (funpow (d - n) shift1 1\<^sub>p)
1.1445 +	      + Ipoly bs q) + Ipoly bs r" by (simp add: ring_simps)
1.1446 +	    hence "\<forall>(bs:: 'a::{ring_char_0,division_by_zero,field} list). Ipoly bs (a ^\<^sub>p (k' - k) *\<^sub>p s) =
1.1447 +	      Ipoly bs (p *\<^sub>p ((a^\<^sub>p (k' - k)) *\<^sub>p (funpow (d - n) shift1 1\<^sub>p) +\<^sub>p q) +\<^sub>p r)" by simp
1.1448 +	    with isnpolyh_unique[OF nakks' nqr']
1.1449 +	    have "a ^\<^sub>p (k' - k) *\<^sub>p s =
1.1450 +	      p *\<^sub>p ((a^\<^sub>p (k' - k)) *\<^sub>p (funpow (d - n) shift1 1\<^sub>p) +\<^sub>p q) +\<^sub>p r" by blast
1.1451 +	    hence ?qths using nq'
1.1452 +	      apply (rule_tac x="(a^\<^sub>p (k' - k)) *\<^sub>p (funpow (d - n) shift1 1\<^sub>p) +\<^sub>p q" in exI)
1.1453 +	      apply (rule_tac x="0" in exI) by simp
1.1454 +	    with kk' nr dr have "k \<le> k' \<and> (degree r = 0 \<or> degree r < degree p) \<and> (\<exists>nr. isnpolyh r nr) \<and> ?qths"
1.1455 +	      by blast } hence ?qrths by blast
1.1456 +	  with dom have ?ths by blast}
1.1457 +	moreover
1.1458 +	{assume spz:"s -\<^sub>p ?p' = 0\<^sub>p"
1.1459 +	  hence domsp: "?D (a, n, p, k, s -\<^sub>p ?p')"
1.1460 +	    apply (simp) by (rule polydivide_aux_real_domintros, simp_all)
1.1461 +	  have dom: ?dths apply (rule polydivide_aux_real_domintros)
1.1462 +	    using ba ds dn' domsp by simp_all
1.1463 +	  from spz isnpolyh_unique[OF polysub_normh[OF ns np'], where q="0\<^sub>p", symmetric, where ?'a = "'a::{ring_char_0,division_by_zero,field}"]
1.1464 +	  have " \<forall>(bs:: 'a::{ring_char_0,division_by_zero,field} list). Ipoly bs s = Ipoly bs ?p'" by simp
1.1465 +	  hence "\<forall>(bs:: 'a::{ring_char_0,division_by_zero,field} list). Ipoly bs s = Ipoly bs (?xdn *\<^sub>p p)" using np nxdn apply simp
1.1466 +	    by (simp only: funpow_shift1_1) simp
1.1467 +	  hence sp': "s = ?xdn *\<^sub>p p" using isnpolyh_unique[OF ns polymul_normh[OF nxdn np]] by blast
1.1468 +	  {assume h1: "polydivide_aux (a,n,p,k,s) = (k',r)"
1.1469 +	    from polydivide_aux.psimps[OF dom] sz dn' ba ds
1.1470 +	    have eq: "polydivide_aux (a,n,p,k,s) = polydivide_aux (a,n,p,k, s -\<^sub>p ?p')"
1.1471 +	      by (simp add: Let_def)
1.1472 +	    also have "\<dots> = (k,0\<^sub>p)" using polydivide_aux.psimps[OF domsp] spz by simp
1.1473 +	    finally have "(k',r) = (k,0\<^sub>p)" using h1 by simp
1.1474 +	    with sp' ns np nxdn polyadd_0(1)[OF polymul_normh[OF np nxdn]]
1.1475 +	      polyadd_0(2)[OF polymul_normh[OF np nxdn]] have ?qrths
1.1476 +	      apply auto
1.1477 +	      apply (rule exI[where x="?xdn"])
1.1478 +	      apply auto
1.1479 +	      apply (rule polymul_commute)
1.1480 +	      apply simp_all
1.1481 +	      done}
1.1482 +	  with dom have ?ths by blast}
1.1483 +	ultimately have ?ths by blast }
1.1484 +      moreover
1.1485 +      {assume ba: "?b \<noteq> a"
1.1486 +	from polysub_normh[OF polymul_normh[OF head_isnpolyh[OF np0, simplified ap] ns]
1.1487 +	  polymul_normh[OF head_isnpolyh[OF ns] np']]
1.1488 +	have nth: "isnpolyh ((a *\<^sub>p s) -\<^sub>p (?b *\<^sub>p ?p')) 0" by(simp add: min_def)
1.1489 +	have nzths: "a *\<^sub>p s \<noteq> 0\<^sub>p" "?b *\<^sub>p ?p' \<noteq> 0\<^sub>p"
1.1490 +	  using polymul_eq0_iff[OF head_isnpolyh[OF np0, simplified ap] ns]
1.1491 +	    polymul_eq0_iff[OF head_isnpolyh[OF ns] np']head_nz[OF np0] ap pnz sz head_nz[OF ns]
1.1492 +	    funpow_shift1_nz[OF pnz] by simp_all
1.1493 +	from polymul_head_polyeq[OF head_isnpolyh[OF np] ns] head_nz[OF np] sz ap head_head[OF np] pnz
1.1494 +	  polymul_head_polyeq[OF head_isnpolyh[OF ns] np'] head_nz [OF ns] sz funpow_shift1_nz[OF pnz, where n="d - n"]
1.1495 +	have hdth: "head (a *\<^sub>p s) = head (?b *\<^sub>p ?p')"
1.1496 +	  using head_head[OF ns] funpow_shift1_head[OF np pnz]
1.1497 +	    polymul_commute[OF head_isnpolyh[OF np] head_isnpolyh[OF ns]]
1.1498 +	  by (simp add: ap)
1.1499 +	from polymul_degreen[OF head_isnpolyh[OF np] ns, where m="0"]
1.1500 +	  head_nz[OF np] pnz sz ap[symmetric]
1.1501 +	  funpow_shift1_nz[OF pnz, where n="d - n"]
1.1502 +	  polymul_degreen[OF head_isnpolyh[OF ns] np', where m="0"]  head_nz[OF ns]
1.1503 +	  ndp ds[symmetric] dn
1.1504 +	have degth: "degree (a *\<^sub>p s) = degree (?b *\<^sub>p ?p') "
1.1505 +	  by (simp add: degree_eq_degreen0[symmetric] funpow_shift1_degree)
1.1506 +	{assume dth: "degree ((a *\<^sub>p s) -\<^sub>p (?b *\<^sub>p ?p')) < d"
1.1507 +	  have th: "?D (a, n, p, Suc k, (a *\<^sub>p s) -\<^sub>p (?b *\<^sub>p ?p'))"
1.1508 +	    using H[rule_format, OF dth nth, simplified] by blast
1.1509 +	  have dom: ?dths
1.1510 +	    using ba ds dn' th apply simp apply (rule polydivide_aux_real_domintros)
1.1511 +	    using ba ds dn'  by simp_all
1.1512 +	  from polysub_normh[OF polymul_normh[OF head_isnpolyh[OF np] ns] polymul_normh[OF head_isnpolyh[OF ns]np']]
1.1513 +	  ap have nasbp': "isnpolyh ((a *\<^sub>p s) -\<^sub>p (?b *\<^sub>p ?p')) 0" by simp
1.1514 +	  {assume h1:"polydivide_aux (a,n,p,k,s) = (k', r)"
1.1515 +	    from h1  polydivide_aux.psimps[OF dom] sz dn' ba ds
1.1516 +	    have eq:"polydivide_aux (a,n,p,Suc k,(a *\<^sub>p s) -\<^sub>p (?b *\<^sub>p ?p')) = (k',r)"
1.1517 +	      by (simp add: Let_def)
1.1518 +	    with H[rule_format, OF dth nasbp', simplified, where xa="Suc k" and xb="k'" and xc="r"]
1.1519 +	    obtain q nq nr where kk': "Suc k \<le> k'" and nr: "isnpolyh r nr" and nq: "isnpolyh q nq"
1.1520 +	      and dr: "degree r = 0 \<or> degree r < degree p"
1.1521 +	      and qr: "a ^\<^sub>p (k' - Suc k) *\<^sub>p ((a *\<^sub>p s) -\<^sub>p (?b *\<^sub>p ?p')) = p *\<^sub>p q +\<^sub>p r" by auto
1.1522 +	    from kk' have kk'':"Suc (k' - Suc k) = k' - k" by arith
1.1523 +	    {fix bs:: "'a::{ring_char_0,division_by_zero,field} list"
1.1524 +
1.1525 +	    from qr isnpolyh_unique[OF polypow_normh[OF head_isnpolyh[OF np], where k="k' - Suc k", simplified ap] nasbp', symmetric]
1.1526 +	    have "Ipoly bs (a ^\<^sub>p (k' - Suc k) *\<^sub>p ((a *\<^sub>p s) -\<^sub>p (?b *\<^sub>p ?p'))) = Ipoly bs (p *\<^sub>p q +\<^sub>p r)" by simp
1.1527 +	    hence "Ipoly bs a ^ (Suc (k' - Suc k)) * Ipoly bs s = Ipoly bs p * Ipoly bs q + Ipoly bs a ^ (k' - Suc k) * Ipoly bs ?b * Ipoly bs ?p' + Ipoly bs r"
1.1528 +	      by (simp add: ring_simps power_Suc)
1.1529 +	    hence "Ipoly bs a ^ (k' - k)  * Ipoly bs s = Ipoly bs p * Ipoly bs q + Ipoly bs a ^ (k' - Suc k) * Ipoly bs ?b * Ipoly bs ?xdn * Ipoly bs p + Ipoly bs r"
1.1530 +	      by (simp add:kk'' funpow_shift1_1[where n="d - n" and p="p"])
1.1531 +	    hence "Ipoly bs (a ^\<^sub>p (k' - k) *\<^sub>p s) = Ipoly bs p * (Ipoly bs q + Ipoly bs a ^ (k' - Suc k) * Ipoly bs ?b * Ipoly bs ?xdn) + Ipoly bs r"
1.1532 +	      by (simp add: ring_simps)}
1.1533 +	    hence ieq:"\<forall>(bs :: 'a::{ring_char_0,division_by_zero,field} list). Ipoly bs (a ^\<^sub>p (k' - k) *\<^sub>p s) =
1.1534 +	      Ipoly bs (p *\<^sub>p (q +\<^sub>p (a ^\<^sub>p (k' - Suc k) *\<^sub>p ?b *\<^sub>p ?xdn)) +\<^sub>p r)" by auto
1.1535 +	    let ?q = "q +\<^sub>p (a ^\<^sub>p (k' - Suc k) *\<^sub>p ?b *\<^sub>p ?xdn)"
1.1536 +	    from polyadd_normh[OF nq polymul_normh[OF polymul_normh[OF polypow_normh[OF head_isnpolyh[OF np], where k="k' - Suc k"] head_isnpolyh[OF ns], simplified ap ] nxdn]]
1.1537 +	    have nqw: "isnpolyh ?q 0" by simp
1.1538 +	    from ieq isnpolyh_unique[OF polymul_normh[OF polypow_normh[OF head_isnpolyh[OF np], where k="k' - k"] ns, simplified ap] polyadd_normh[OF polymul_normh[OF np nqw] nr]]
1.1539 +	    have asth: "(a ^\<^sub>p (k' - k) *\<^sub>p s) = p *\<^sub>p (q +\<^sub>p (a ^\<^sub>p (k' - Suc k) *\<^sub>p ?b *\<^sub>p ?xdn)) +\<^sub>p r" by blast
1.1540 +	    from dr kk' nr h1 asth nqw have ?qrths apply simp
1.1541 +	      apply (rule conjI)
1.1542 +	      apply (rule exI[where x="nr"], simp)
1.1543 +	      apply (rule exI[where x="(q +\<^sub>p (a ^\<^sub>p (k' - Suc k) *\<^sub>p ?b *\<^sub>p ?xdn))"], simp)
1.1544 +	      apply (rule exI[where x="0"], simp)
1.1545 +	      done}
1.1546 +	  hence ?qrths by blast
1.1547 +	  with dom have ?ths by blast}
1.1548 +	moreover
1.1549 +	{assume spz: "a *\<^sub>p s -\<^sub>p (?b *\<^sub>p ?p') = 0\<^sub>p"
1.1550 +	  hence domsp: "?D (a, n, p, Suc k, a *\<^sub>p s -\<^sub>p (?b *\<^sub>p ?p'))"
1.1551 +	    apply (simp) by (rule polydivide_aux_real_domintros, simp_all)
1.1552 +	  have dom: ?dths using sz ba dn' ds domsp
1.1553 +	    by - (rule polydivide_aux_real_domintros, simp_all)
1.1554 +	  {fix bs :: "'a::{ring_char_0,division_by_zero,field} list"
1.1555 +	    from isnpolyh_unique[OF nth, where ?'a="'a" and q="0\<^sub>p",simplified,symmetric] spz
1.1556 +	  have "Ipoly bs (a*\<^sub>p s) = Ipoly bs ?b * Ipoly bs ?p'" by simp
1.1557 +	  hence "Ipoly bs (a*\<^sub>p s) = Ipoly bs (?b *\<^sub>p ?xdn) * Ipoly bs p"
1.1558 +	    by (simp add: funpow_shift1_1[where n="d - n" and p="p"])
1.1559 +	  hence "Ipoly bs (a*\<^sub>p s) = Ipoly bs (p *\<^sub>p (?b *\<^sub>p ?xdn))" by simp
1.1560 +	}
1.1561 +	hence hth: "\<forall> (bs:: 'a::{ring_char_0,division_by_zero,field} list). Ipoly bs (a*\<^sub>p s) = Ipoly bs (p *\<^sub>p (?b *\<^sub>p ?xdn))" ..
1.1562 +	  from hth
1.1563 +	  have asq: "a *\<^sub>p s = p *\<^sub>p (?b *\<^sub>p ?xdn)"
1.1564 +	    using isnpolyh_unique[where ?'a = "'a::{ring_char_0,division_by_zero,field}", OF polymul_normh[OF head_isnpolyh[OF np] ns]
1.1565 +                    polymul_normh[OF np polymul_normh[OF head_isnpolyh[OF ns] nxdn]],
1.1566 +	      simplified ap] by simp
1.1567 +	  {assume h1: "polydivide_aux (a,n,p,k,s) = (k', r)"
1.1568 +	  from h1 sz ds ba dn' spz polydivide_aux.psimps[OF dom] polydivide_aux.psimps[OF domsp]
1.1569 +	  have "(k',r) = (Suc k, 0\<^sub>p)" by (simp add: Let_def)
1.1570 +	  with h1 np head_isnpolyh[OF np, simplified ap] ns polymul_normh[OF head_isnpolyh[OF ns] nxdn]
1.1571 +	    polymul_normh[OF np polymul_normh[OF head_isnpolyh[OF ns] nxdn]] asq
1.1572 +	  have ?qrths apply (clarsimp simp add: Let_def)
1.1573 +	    apply (rule exI[where x="?b *\<^sub>p ?xdn"]) apply simp
1.1574 +	    apply (rule exI[where x="0"], simp)
1.1575 +	    done}
1.1576 +	hence ?qrths by blast
1.1577 +	with dom have ?ths by blast}
1.1578 +	ultimately have ?ths using  degree_polysub_samehead[OF polymul_normh[OF head_isnpolyh[OF np0, simplified ap] ns] polymul_normh[OF head_isnpolyh[OF ns] np'] hdth degth] polymul_degreen[OF head_isnpolyh[OF np] ns, where m="0"]
1.1579 +	  head_nz[OF np] pnz sz ap[symmetric] ds[symmetric]
1.1580 +	  by (simp add: degree_eq_degreen0[symmetric]) blast }
1.1581 +      ultimately have ?ths by blast
1.1582 +    }
1.1583 +    ultimately have ?ths by blast}
1.1584 +  ultimately show ?ths by blast
1.1585 +qed
1.1586 +
1.1587 +lemma polydivide_properties:
1.1588 +  assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
1.1589 +  and np: "isnpolyh p n0" and ns: "isnpolyh s n1" and pnz: "p \<noteq> 0\<^sub>p"
1.1590 +  shows "(\<exists> k r. polydivide s p = (k,r) \<and> (\<exists>nr. isnpolyh r nr) \<and> (degree r = 0 \<or> degree r < degree p)
1.1591 +  \<and> (\<exists>q n1. isnpolyh q n1 \<and> ((polypow k (head p)) *\<^sub>p s = p *\<^sub>p q +\<^sub>p r)))"
1.1592 +proof-
1.1593 +  have trv: "head p = head p" "degree p = degree p" by simp_all
1.1594 +  from polydivide_aux_properties[OF np ns trv pnz, where k="0"]
1.1595 +  have d: "polydivide_aux_dom (head p, degree p, p, 0, s)" by blast
1.1596 +  from polydivide_def[where s="s" and p="p"] polydivide_aux.psimps[OF d]
1.1597 +  have ex: "\<exists> k r. polydivide s p = (k,r)" by auto
1.1598 +  then obtain k r where kr: "polydivide s p = (k,r)" by blast
1.1599 +  from trans[OF meta_eq_to_obj_eq[OF polydivide_def[where s="s" and p="p"], symmetric] kr]
1.1600 +    polydivide_aux_properties[OF np ns trv pnz, where k="0" and k'="k" and r="r"]
1.1601 +  have "(degree r = 0 \<or> degree r < degree p) \<and>
1.1602 +   (\<exists>nr. isnpolyh r nr) \<and> (\<exists>q n1. isnpolyh q n1 \<and> head p ^\<^sub>p k - 0 *\<^sub>p s = p *\<^sub>p q +\<^sub>p r)" by blast
1.1603 +  with kr show ?thesis
1.1604 +    apply -
1.1605 +    apply (rule exI[where x="k"])
1.1606 +    apply (rule exI[where x="r"])
1.1607 +    apply simp
1.1608 +    done
1.1609 +qed
1.1610 +
1.1611 +subsection{* More about polypoly and pnormal etc *}
1.1612 +
1.1613 +definition "isnonconstant p = (\<not> isconstant p)"
1.1614 +
1.1615 +lemma last_map: "xs \<noteq> [] ==> last (map f xs) = f (last xs)" by (induct xs, auto)
1.1616 +
1.1617 +lemma isnonconstant_pnormal_iff: assumes nc: "isnonconstant p"
1.1618 +  shows "pnormal (polypoly bs p) \<longleftrightarrow> Ipoly bs (head p) \<noteq> 0"
1.1619 +proof
1.1620 +  let ?p = "polypoly bs p"
1.1621 +  assume H: "pnormal ?p"
1.1622 +  have csz: "coefficients p \<noteq> []" using nc by (cases p, auto)
1.1623 +
1.1624 +  from coefficients_head[of p] last_map[OF csz, of "Ipoly bs"]
1.1625 +    pnormal_last_nonzero[OF H]
1.1626 +  show "Ipoly bs (head p) \<noteq> 0" by (simp add: polypoly_def)
1.1627 +next
1.1628 +  assume h: "\<lparr>head p\<rparr>\<^sub>p\<^bsup>bs\<^esup> \<noteq> 0"
1.1629 +  let ?p = "polypoly bs p"
1.1630 +  have csz: "coefficients p \<noteq> []" using nc by (cases p, auto)
1.1631 +  hence pz: "?p \<noteq> []" by (simp add: polypoly_def)
1.1632 +  hence lg: "length ?p > 0" by simp
1.1633 +  from h coefficients_head[of p] last_map[OF csz, of "Ipoly bs"]
1.1634 +  have lz: "last ?p \<noteq> 0" by (simp add: polypoly_def)
1.1635 +  from pnormal_last_length[OF lg lz] show "pnormal ?p" .
1.1636 +qed
1.1637 +
1.1638 +lemma isnonconstant_coefficients_length: "isnonconstant p \<Longrightarrow> length (coefficients p) > 1"
1.1639 +  unfolding isnonconstant_def
1.1640 +  apply (cases p, simp_all)
1.1641 +  apply (case_tac nat, auto)
1.1642 +  done
1.1643 +lemma isnonconstant_nonconstant: assumes inc: "isnonconstant p"
1.1644 +  shows "nonconstant (polypoly bs p) \<longleftrightarrow> Ipoly bs (head p) \<noteq> 0"
1.1645 +proof
1.1646 +  let ?p = "polypoly bs p"
1.1647 +  assume nc: "nonconstant ?p"
1.1648 +  from isnonconstant_pnormal_iff[OF inc, of bs] nc
1.1649 +  show "\<lparr>head p\<rparr>\<^sub>p\<^bsup>bs\<^esup> \<noteq> 0" unfolding nonconstant_def by blast
1.1650 +next
1.1651 +  let ?p = "polypoly bs p"
1.1652 +  assume h: "\<lparr>head p\<rparr>\<^sub>p\<^bsup>bs\<^esup> \<noteq> 0"
1.1653 +  from isnonconstant_pnormal_iff[OF inc, of bs] h
1.1654 +  have pn: "pnormal ?p" by blast
1.1655 +  {fix x assume H: "?p = [x]"
1.1656 +    from H have "length (coefficients p) = 1" unfolding polypoly_def by auto
1.1657 +    with isnonconstant_coefficients_length[OF inc] have False by arith}
1.1658 +  thus "nonconstant ?p" using pn unfolding nonconstant_def by blast
1.1659 +qed
1.1660 +
1.1661 +lemma pnormal_length: "p\<noteq>[] \<Longrightarrow> pnormal p \<longleftrightarrow> length (pnormalize p) = length p"
1.1662 +  unfolding pnormal_def
1.1663 + apply (induct p rule: pnormalize.induct, simp_all)
1.1664 + apply (case_tac "p=[]", simp_all)
1.1665 + done
1.1666 +
1.1667 +lemma degree_degree: assumes inc: "isnonconstant p"
1.1668 +  shows "degree p = Polynomial_List.degree (polypoly bs p) \<longleftrightarrow> \<lparr>head p\<rparr>\<^sub>p\<^bsup>bs\<^esup> \<noteq> 0"
1.1669 +proof
1.1670 +  let  ?p = "polypoly bs p"
1.1671 +  assume H: "degree p = Polynomial_List.degree ?p"
1.1672 +  from isnonconstant_coefficients_length[OF inc] have pz: "?p \<noteq> []"
1.1673 +    unfolding polypoly_def by auto
1.1674 +  from H degree_coefficients[of p] isnonconstant_coefficients_length[OF inc]
1.1675 +  have lg:"length (pnormalize ?p) = length ?p"
1.1676 +    unfolding Polynomial_List.degree_def polypoly_def by simp
1.1677 +  hence "pnormal ?p" using pnormal_length[OF pz] by blast
1.1678 +  with isnonconstant_pnormal_iff[OF inc]
1.1679 +  show "\<lparr>head p\<rparr>\<^sub>p\<^bsup>bs\<^esup> \<noteq> 0" by blast
1.1680 +next
1.1681 +  let  ?p = "polypoly bs p"
1.1682 +  assume H: "\<lparr>head p\<rparr>\<^sub>p\<^bsup>bs\<^esup> \<noteq> 0"
1.1683 +  with isnonconstant_pnormal_iff[OF inc] have "pnormal ?p" by blast
1.1684 +  with degree_coefficients[of p] isnonconstant_coefficients_length[OF inc]
1.1685 +  show "degree p = Polynomial_List.degree ?p"
1.1686 +    unfolding polypoly_def pnormal_def Polynomial_List.degree_def by auto
1.1687 +qed
1.1688 +
1.1689 +section{* Swaps ; Division by a certain variable *}
1.1690 +consts swap:: "nat \<Rightarrow> nat \<Rightarrow> poly \<Rightarrow> poly"
1.1691 +primrec
1.1692 +  "swap n m (C x) = C x"
1.1693 +  "swap n m (Bound k) = Bound (if k = n then m else if k=m then n else k)"
1.1694 +  "swap n m (Neg t) = Neg (swap n m t)"
1.1695 +  "swap n m (Add s t) = Add (swap n m s) (swap n m t)"
1.1696 +  "swap n m (Sub s t) = Sub (swap n m s) (swap n m t)"
1.1697 +  "swap n m (Mul s t) = Mul (swap n m s) (swap n m t)"
1.1698 +  "swap n m (Pw t k) = Pw (swap n m t) k"
1.1699 +  "swap n m (CN c k p) = CN (swap n m c) (if k = n then m else if k=m then n else k)
1.1700 +  (swap n m p)"
1.1701 +
1.1702 +lemma swap: assumes nbs: "n < length bs" and mbs: "m < length bs"
1.1703 +  shows "Ipoly bs (swap n m t) = Ipoly ((bs[n:= bs!m])[m:= bs!n]) t"
1.1704 +proof (induct t)
1.1705 +  case (Bound k) thus ?case using nbs mbs by simp
1.1706 +next
1.1707 +  case (CN c k p) thus ?case using nbs mbs by simp
1.1708 +qed simp_all
1.1709 +lemma swap_swap_id[simp]: "swap n m (swap m n t) = t"
1.1710 +  by (induct t,simp_all)
1.1711 +
1.1712 +lemma swap_commute: "swap n m p = swap m n p" by (induct p, simp_all)
1.1713 +
1.1714 +lemma swap_same_id[simp]: "swap n n t = t"
1.1715 +  by (induct t, simp_all)
1.1716 +
1.1717 +definition "swapnorm n m t = polynate (swap n m t)"
1.1718 +
1.1719 +lemma swapnorm: assumes nbs: "n < length bs" and mbs: "m < length bs"
1.1720 +  shows "((Ipoly bs (swapnorm n m t) :: 'a\<Colon>{ring_char_0,division_by_zero,field})) = Ipoly ((bs[n:= bs!m])[m:= bs!n]) t"
1.1721 +  using swap[OF prems] swapnorm_def by simp
1.1722 +
1.1723 +lemma swapnorm_isnpoly[simp]:
1.1724 +    assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
1.1725 +  shows "isnpoly (swapnorm n m p)"
1.1726 +  unfolding swapnorm_def by simp
1.1727 +
1.1728 +definition "polydivideby n s p =
1.1729 +    (let ss = swapnorm 0 n s ; sp = swapnorm 0 n p ; h = head sp; (k,r) = polydivide ss sp
1.1730 +     in (k,swapnorm 0 n h,swapnorm 0 n r))"
1.1731 +
1.1732 +lemma swap_nz [simp]: " (swap n m p = 0\<^sub>p) = (p = 0\<^sub>p)" by (induct p, simp_all)
1.1733 +
1.1734 +consts isweaknpoly :: "poly \<Rightarrow> bool"
1.1735 +recdef isweaknpoly "measure size"
1.1736 +  "isweaknpoly (C c) = True"
1.1737 +  "isweaknpoly (CN c n p) \<longleftrightarrow> isweaknpoly c \<and> isweaknpoly p"
1.1738 +  "isweaknpoly p = False"
1.1739 +
1.1740 +lemma isnpolyh_isweaknpoly: "isnpolyh p n0 \<Longrightarrow> isweaknpoly p"
1.1741 +  by (induct p arbitrary: n0, auto)
1.1742 +
1.1743 +lemma swap_isweanpoly: "isweaknpoly p \<Longrightarrow> isweaknpoly (swap n m p)"
1.1744 +  by (induct p, auto)
1.1745 +
1.1746 +end
1.1747 \ No newline at end of file
```