src/HOL/Library/Euclidean_Space.thy
changeset 30582 638b088bb840
parent 30549 d2d7874648bd
child 30655 88131f2807b6
child 30661 54858c8ad226
     1.1 --- a/src/HOL/Library/Euclidean_Space.thy	Wed Mar 18 22:17:23 2009 +0100
     1.2 +++ b/src/HOL/Library/Euclidean_Space.thy	Thu Mar 19 01:29:19 2009 -0700
     1.3 @@ -13,32 +13,50 @@
     1.4  
     1.5  text{* Some common special cases.*}
     1.6  
     1.7 -lemma forall_1: "(\<forall>(i::'a::{order,one}). 1 <= i \<and> i <= 1 --> P i) \<longleftrightarrow> P 1"
     1.8 -  by (metis order_eq_iff)
     1.9 -lemma forall_dimindex_1: "(\<forall>i \<in> {1..dimindex(UNIV:: 1 set)}. P i) \<longleftrightarrow> P 1"
    1.10 -  by (simp add: dimindex_def)
    1.11 -
    1.12 -lemma forall_2: "(\<forall>(i::nat). 1 <= i \<and> i <= 2 --> P i) \<longleftrightarrow> P 1 \<and> P 2"
    1.13 -proof-
    1.14 -  have "\<And>i::nat. 1 <= i \<and> i <= 2 \<longleftrightarrow> i = 1 \<or> i = 2" by arith
    1.15 -  thus ?thesis by metis
    1.16 +lemma forall_1: "(\<forall>i::1. P i) \<longleftrightarrow> P 1"
    1.17 +  by (metis num1_eq_iff)
    1.18 +
    1.19 +lemma exhaust_2:
    1.20 +  fixes x :: 2 shows "x = 1 \<or> x = 2"
    1.21 +proof (induct x)
    1.22 +  case (of_int z)
    1.23 +  then have "0 <= z" and "z < 2" by simp_all
    1.24 +  then have "z = 0 | z = 1" by arith
    1.25 +  then show ?case by auto
    1.26  qed
    1.27  
    1.28 -lemma forall_3: "(\<forall>(i::nat). 1 <= i \<and> i <= 3 --> P i) \<longleftrightarrow> P 1 \<and> P 2 \<and> P 3"
    1.29 -proof-
    1.30 -  have "\<And>i::nat. 1 <= i \<and> i <= 3 \<longleftrightarrow> i = 1 \<or> i = 2 \<or> i = 3" by arith
    1.31 -  thus ?thesis by metis
    1.32 +lemma forall_2: "(\<forall>i::2. P i) \<longleftrightarrow> P 1 \<and> P 2"
    1.33 +  by (metis exhaust_2)
    1.34 +
    1.35 +lemma exhaust_3:
    1.36 +  fixes x :: 3 shows "x = 1 \<or> x = 2 \<or> x = 3"
    1.37 +proof (induct x)
    1.38 +  case (of_int z)
    1.39 +  then have "0 <= z" and "z < 3" by simp_all
    1.40 +  then have "z = 0 \<or> z = 1 \<or> z = 2" by arith
    1.41 +  then show ?case by auto
    1.42  qed
    1.43  
    1.44 -lemma setsum_singleton[simp]: "setsum f {x} = f x" by simp
    1.45 -lemma setsum_1: "setsum f {(1::'a::{order,one})..1} = f 1"
    1.46 -  by (simp add: atLeastAtMost_singleton)
    1.47 -
    1.48 -lemma setsum_2: "setsum f {1::nat..2} = f 1 + f 2"
    1.49 -  by (simp add: nat_number  atLeastAtMostSuc_conv add_commute)
    1.50 -
    1.51 -lemma setsum_3: "setsum f {1::nat..3} = f 1 + f 2 + f 3"
    1.52 -  by (simp add: nat_number  atLeastAtMostSuc_conv add_commute)
    1.53 +lemma forall_3: "(\<forall>i::3. P i) \<longleftrightarrow> P 1 \<and> P 2 \<and> P 3"
    1.54 +  by (metis exhaust_3)
    1.55 +
    1.56 +lemma UNIV_1: "UNIV = {1::1}"
    1.57 +  by (auto simp add: num1_eq_iff)
    1.58 +
    1.59 +lemma UNIV_2: "UNIV = {1::2, 2::2}"
    1.60 +  using exhaust_2 by auto
    1.61 +
    1.62 +lemma UNIV_3: "UNIV = {1::3, 2::3, 3::3}"
    1.63 +  using exhaust_3 by auto
    1.64 +
    1.65 +lemma setsum_1: "setsum f (UNIV::1 set) = f 1"
    1.66 +  unfolding UNIV_1 by simp
    1.67 +
    1.68 +lemma setsum_2: "setsum f (UNIV::2 set) = f 1 + f 2"
    1.69 +  unfolding UNIV_2 by simp
    1.70 +
    1.71 +lemma setsum_3: "setsum f (UNIV::3 set) = f 1 + f 2 + f 3"
    1.72 +  unfolding UNIV_3 by (simp add: add_ac)
    1.73  
    1.74  subsection{* Basic componentwise operations on vectors. *}
    1.75  
    1.76 @@ -76,10 +94,8 @@
    1.77  instantiation "^" :: (ord,type) ord
    1.78   begin
    1.79  definition vector_less_eq_def:
    1.80 -  "less_eq (x :: 'a ^'b) y = (ALL i : {1 .. dimindex (UNIV :: 'b set)}.
    1.81 -  x$i <= y$i)"
    1.82 -definition vector_less_def: "less (x :: 'a ^'b) y = (ALL i : {1 ..
    1.83 -  dimindex (UNIV :: 'b set)}. x$i < y$i)"
    1.84 +  "less_eq (x :: 'a ^'b) y = (ALL i. x$i <= y$i)"
    1.85 +definition vector_less_def: "less (x :: 'a ^'b) y = (ALL i. x$i < y$i)"
    1.86  
    1.87  instance by (intro_classes)
    1.88  end
    1.89 @@ -102,19 +118,19 @@
    1.90  text{* Dot products. *}
    1.91  
    1.92  definition dot :: "'a::{comm_monoid_add, times} ^ 'n \<Rightarrow> 'a ^ 'n \<Rightarrow> 'a" (infix "\<bullet>" 70) where
    1.93 -  "x \<bullet> y = setsum (\<lambda>i. x$i * y$i) {1 .. dimindex (UNIV:: 'n set)}"
    1.94 +  "x \<bullet> y = setsum (\<lambda>i. x$i * y$i) UNIV"
    1.95 +
    1.96  lemma dot_1[simp]: "(x::'a::{comm_monoid_add, times}^1) \<bullet> y = (x$1) * (y$1)"
    1.97 -  by (simp add: dot_def dimindex_def)
    1.98 +  by (simp add: dot_def setsum_1)
    1.99  
   1.100  lemma dot_2[simp]: "(x::'a::{comm_monoid_add, times}^2) \<bullet> y = (x$1) * (y$1) + (x$2) * (y$2)"
   1.101 -  by (simp add: dot_def dimindex_def nat_number)
   1.102 +  by (simp add: dot_def setsum_2)
   1.103  
   1.104  lemma dot_3[simp]: "(x::'a::{comm_monoid_add, times}^3) \<bullet> y = (x$1) * (y$1) + (x$2) * (y$2) + (x$3) * (y$3)"
   1.105 -  by (simp add: dot_def dimindex_def nat_number)
   1.106 +  by (simp add: dot_def setsum_3)
   1.107  
   1.108  subsection {* A naive proof procedure to lift really trivial arithmetic stuff from the basis of the vector space. *}
   1.109  
   1.110 -lemmas Cart_lambda_beta' = Cart_lambda_beta[rule_format]
   1.111  method_setup vector = {*
   1.112  let
   1.113    val ss1 = HOL_basic_ss addsimps [@{thm dot_def}, @{thm setsum_addf} RS sym,
   1.114 @@ -125,7 +141,7 @@
   1.115                @{thm vector_minus_def}, @{thm vector_uminus_def},
   1.116                @{thm vector_one_def}, @{thm vector_zero_def}, @{thm vec_def},
   1.117                @{thm vector_scaleR_def},
   1.118 -              @{thm Cart_lambda_beta'}, @{thm vector_scalar_mult_def}]
   1.119 +              @{thm Cart_lambda_beta}, @{thm vector_scalar_mult_def}]
   1.120   fun vector_arith_tac ths =
   1.121     simp_tac ss1
   1.122     THEN' (fn i => rtac @{thm setsum_cong2} i
   1.123 @@ -145,39 +161,38 @@
   1.124  
   1.125  text{* Obvious "component-pushing". *}
   1.126  
   1.127 -lemma vec_component: " i \<in> {1 .. dimindex (UNIV :: 'n set)} \<Longrightarrow> (vec x :: 'a ^ 'n)$i = x"
   1.128 +lemma vec_component [simp]: "(vec x :: 'a ^ 'n)$i = x"
   1.129    by (vector vec_def)
   1.130  
   1.131 -lemma vector_add_component:
   1.132 -  fixes x y :: "'a::{plus} ^ 'n"  assumes i: "i \<in> {1 .. dimindex(UNIV:: 'n set)}"
   1.133 +lemma vector_add_component [simp]:
   1.134 +  fixes x y :: "'a::{plus} ^ 'n"
   1.135    shows "(x + y)$i = x$i + y$i"
   1.136 -  using i by vector
   1.137 -
   1.138 -lemma vector_minus_component:
   1.139 -  fixes x y :: "'a::{minus} ^ 'n"  assumes i: "i \<in> {1 .. dimindex(UNIV:: 'n set)}"
   1.140 +  by vector
   1.141 +
   1.142 +lemma vector_minus_component [simp]:
   1.143 +  fixes x y :: "'a::{minus} ^ 'n"
   1.144    shows "(x - y)$i = x$i - y$i"
   1.145 -  using i  by vector
   1.146 -
   1.147 -lemma vector_mult_component:
   1.148 -  fixes x y :: "'a::{times} ^ 'n"  assumes i: "i \<in> {1 .. dimindex(UNIV:: 'n set)}"
   1.149 +  by vector
   1.150 +
   1.151 +lemma vector_mult_component [simp]:
   1.152 +  fixes x y :: "'a::{times} ^ 'n"
   1.153    shows "(x * y)$i = x$i * y$i"
   1.154 -  using i by vector
   1.155 -
   1.156 -lemma vector_smult_component:
   1.157 -  fixes y :: "'a::{times} ^ 'n"  assumes i: "i \<in> {1 .. dimindex(UNIV:: 'n set)}"
   1.158 +  by vector
   1.159 +
   1.160 +lemma vector_smult_component [simp]:
   1.161 +  fixes y :: "'a::{times} ^ 'n"
   1.162    shows "(c *s y)$i = c * (y$i)"
   1.163 -  using i by vector
   1.164 -
   1.165 -lemma vector_uminus_component:
   1.166 -  fixes x :: "'a::{uminus} ^ 'n"  assumes i: "i \<in> {1 .. dimindex(UNIV:: 'n set)}"
   1.167 +  by vector
   1.168 +
   1.169 +lemma vector_uminus_component [simp]:
   1.170 +  fixes x :: "'a::{uminus} ^ 'n"
   1.171    shows "(- x)$i = - (x$i)"
   1.172 -  using i by vector
   1.173 -
   1.174 -lemma vector_scaleR_component:
   1.175 +  by vector
   1.176 +
   1.177 +lemma vector_scaleR_component [simp]:
   1.178    fixes x :: "'a::scaleR ^ 'n"
   1.179 -  assumes i: "i \<in> {1 .. dimindex(UNIV :: 'n set)}"
   1.180    shows "(scaleR r x)$i = scaleR r (x$i)"
   1.181 -  using i by vector
   1.182 +  by vector
   1.183  
   1.184  lemma cond_component: "(if b then x else y)$i = (if b then x$i else y$i)" by vector
   1.185  
   1.186 @@ -250,7 +265,7 @@
   1.187  instance "^" :: (semiring_0,type) semiring_0
   1.188    apply (intro_classes) by (vector ring_simps)+
   1.189  instance "^" :: (semiring_1,type) semiring_1
   1.190 -  apply (intro_classes) apply vector using dimindex_ge_1 by auto
   1.191 +  apply (intro_classes) by vector
   1.192  instance "^" :: (comm_semiring,type) comm_semiring
   1.193    apply (intro_classes) by (vector ring_simps)+
   1.194  
   1.195 @@ -274,16 +289,16 @@
   1.196  instance "^" :: (real_algebra_1,type) real_algebra_1 ..
   1.197  
   1.198  lemma of_nat_index:
   1.199 -  "i\<in>{1 .. dimindex (UNIV :: 'n set)} \<Longrightarrow> (of_nat n :: 'a::semiring_1 ^'n)$i = of_nat n"
   1.200 +  "(of_nat n :: 'a::semiring_1 ^'n)$i = of_nat n"
   1.201    apply (induct n)
   1.202    apply vector
   1.203    apply vector
   1.204    done
   1.205  lemma zero_index[simp]:
   1.206 -  "i\<in>{1 .. dimindex (UNIV :: 'n set)} \<Longrightarrow> (0 :: 'a::zero ^'n)$i = 0" by vector
   1.207 +  "(0 :: 'a::zero ^'n)$i = 0" by vector
   1.208  
   1.209  lemma one_index[simp]:
   1.210 -  "i\<in>{1 .. dimindex (UNIV :: 'n set)} \<Longrightarrow> (1 :: 'a::one ^'n)$i = 1" by vector
   1.211 +  "(1 :: 'a::one ^'n)$i = 1" by vector
   1.212  
   1.213  lemma one_plus_of_nat_neq_0: "(1::'a::semiring_char_0) + of_nat n \<noteq> 0"
   1.214  proof-
   1.215 @@ -296,28 +311,7 @@
   1.216  proof (intro_classes)
   1.217    fix m n ::nat
   1.218    show "(of_nat m :: 'a^'b) = of_nat n \<longleftrightarrow> m = n"
   1.219 -  proof(induct m arbitrary: n)
   1.220 -    case 0 thus ?case apply vector
   1.221 -      apply (induct n,auto simp add: ring_simps)
   1.222 -      using dimindex_ge_1 apply auto
   1.223 -      apply vector
   1.224 -      by (auto simp add: of_nat_index one_plus_of_nat_neq_0)
   1.225 -  next
   1.226 -    case (Suc n m)
   1.227 -    thus ?case  apply vector
   1.228 -      apply (induct m, auto simp add: ring_simps of_nat_index zero_index)
   1.229 -      using dimindex_ge_1 apply simp apply blast
   1.230 -      apply (simp add: one_plus_of_nat_neq_0)
   1.231 -      using dimindex_ge_1 apply simp apply blast
   1.232 -      apply (simp add: vector_component one_index of_nat_index)
   1.233 -      apply (simp only: of_nat.simps(2)[where ?'a = 'a, symmetric] of_nat_eq_iff)
   1.234 -      using  dimindex_ge_1 apply simp apply blast
   1.235 -      apply (simp add: vector_component one_index of_nat_index)
   1.236 -      apply (simp only: of_nat.simps(2)[where ?'a = 'a, symmetric] of_nat_eq_iff)
   1.237 -      using dimindex_ge_1 apply simp apply blast
   1.238 -      apply (simp add: vector_component one_index of_nat_index)
   1.239 -      done
   1.240 -  qed
   1.241 +    by (simp add: Cart_eq of_nat_index)
   1.242  qed
   1.243  
   1.244  instance "^" :: (comm_ring_1,type) comm_ring_1 by intro_classes
   1.245 @@ -341,8 +335,7 @@
   1.246    by (vector ring_simps)
   1.247  
   1.248  lemma vec_eq[simp]: "(vec m = vec n) \<longleftrightarrow> (m = n)"
   1.249 -  apply (auto simp add: vec_def Cart_eq vec_component Cart_lambda_beta )
   1.250 -  using dimindex_ge_1 apply auto done
   1.251 +  by (simp add: Cart_eq)
   1.252  
   1.253  subsection {* Square root of sum of squares *}
   1.254  
   1.255 @@ -513,11 +506,11 @@
   1.256  
   1.257  subsection {* Norms *}
   1.258  
   1.259 -instantiation "^" :: (real_normed_vector, type) real_normed_vector
   1.260 +instantiation "^" :: (real_normed_vector, finite) real_normed_vector
   1.261  begin
   1.262  
   1.263  definition vector_norm_def:
   1.264 -  "norm (x::'a^'b) = setL2 (\<lambda>i. norm (x$i)) {1 .. dimindex (UNIV:: 'b set)}"
   1.265 +  "norm (x::'a^'b) = setL2 (\<lambda>i. norm (x$i)) UNIV"
   1.266  
   1.267  definition vector_sgn_def:
   1.268    "sgn (x::'a^'b) = scaleR (inverse (norm x)) x"
   1.269 @@ -533,14 +526,11 @@
   1.270    show "norm (x + y) \<le> norm x + norm y"
   1.271      unfolding vector_norm_def
   1.272      apply (rule order_trans [OF _ setL2_triangle_ineq])
   1.273 -    apply (rule setL2_mono)
   1.274 -    apply (simp add: vector_component norm_triangle_ineq)
   1.275 -    apply simp
   1.276 +    apply (simp add: setL2_mono norm_triangle_ineq)
   1.277      done
   1.278    show "norm (scaleR a x) = \<bar>a\<bar> * norm x"
   1.279      unfolding vector_norm_def
   1.280 -    by (simp add: vector_component norm_scaleR setL2_right_distrib
   1.281 -             cong: strong_setL2_cong)
   1.282 +    by (simp add: norm_scaleR setL2_right_distrib)
   1.283    show "sgn x = scaleR (inverse (norm x)) x"
   1.284      by (rule vector_sgn_def)
   1.285  qed
   1.286 @@ -549,11 +539,11 @@
   1.287  
   1.288  subsection {* Inner products *}
   1.289  
   1.290 -instantiation "^" :: (real_inner, type) real_inner
   1.291 +instantiation "^" :: (real_inner, finite) real_inner
   1.292  begin
   1.293  
   1.294  definition vector_inner_def:
   1.295 -  "inner x y = setsum (\<lambda>i. inner (x$i) (y$i)) {1 .. dimindex(UNIV::'b set)}"
   1.296 +  "inner x y = setsum (\<lambda>i. inner (x$i) (y$i)) UNIV"
   1.297  
   1.298  instance proof
   1.299    fix r :: real and x y z :: "'a ^ 'b"
   1.300 @@ -562,10 +552,10 @@
   1.301      by (simp add: inner_commute)
   1.302    show "inner (x + y) z = inner x z + inner y z"
   1.303      unfolding vector_inner_def
   1.304 -    by (vector inner_left_distrib)
   1.305 +    by (simp add: inner_left_distrib setsum_addf)
   1.306    show "inner (scaleR r x) y = r * inner x y"
   1.307      unfolding vector_inner_def
   1.308 -    by (vector inner_scaleR_left)
   1.309 +    by (simp add: inner_scaleR_left setsum_right_distrib)
   1.310    show "0 \<le> inner x x"
   1.311      unfolding vector_inner_def
   1.312      by (simp add: setsum_nonneg)
   1.313 @@ -613,25 +603,16 @@
   1.314    show ?case by (simp add: h)
   1.315  qed
   1.316  
   1.317 -lemma dot_eq_0: "x \<bullet> x = 0 \<longleftrightarrow> (x::'a::{ordered_ring_strict,ring_no_zero_divisors} ^ 'n) = 0"
   1.318 -proof-
   1.319 -  {assume f: "finite (UNIV :: 'n set)"
   1.320 -    let ?S = "{Suc 0 .. card (UNIV :: 'n set)}"
   1.321 -    have fS: "finite ?S" using f by simp
   1.322 -    have fp: "\<forall> i\<in> ?S. x$i * x$i>= 0" by simp
   1.323 -    have ?thesis by (vector dimindex_def f setsum_squares_eq_0_iff[OF fS fp])}
   1.324 -  moreover
   1.325 -  {assume "\<not> finite (UNIV :: 'n set)" then have ?thesis by (vector dimindex_def)}
   1.326 -  ultimately show ?thesis by metis
   1.327 -qed
   1.328 -
   1.329 -lemma dot_pos_lt[simp]: "(0 < x \<bullet> x) \<longleftrightarrow> (x::'a::{ordered_ring_strict,ring_no_zero_divisors} ^ 'n) \<noteq> 0" using dot_eq_0[of x] dot_pos_le[of x]
   1.330 +lemma dot_eq_0: "x \<bullet> x = 0 \<longleftrightarrow> (x::'a::{ordered_ring_strict,ring_no_zero_divisors} ^ 'n::finite) = 0"
   1.331 +  by (simp add: dot_def setsum_squares_eq_0_iff Cart_eq)
   1.332 +
   1.333 +lemma dot_pos_lt[simp]: "(0 < x \<bullet> x) \<longleftrightarrow> (x::'a::{ordered_ring_strict,ring_no_zero_divisors} ^ 'n::finite) \<noteq> 0" using dot_eq_0[of x] dot_pos_le[of x]
   1.334    by (auto simp add: le_less)
   1.335  
   1.336  subsection{* The collapse of the general concepts to dimension one. *}
   1.337  
   1.338  lemma vector_one: "(x::'a ^1) = (\<chi> i. (x$1))"
   1.339 -  by (vector dimindex_def)
   1.340 +  by (simp add: Cart_eq forall_1)
   1.341  
   1.342  lemma forall_one: "(\<forall>(x::'a ^1). P x) \<longleftrightarrow> (\<forall>x. P(\<chi> i. x))"
   1.343    apply auto
   1.344 @@ -640,7 +621,7 @@
   1.345    done
   1.346  
   1.347  lemma norm_vector_1: "norm (x :: _^1) = norm (x$1)"
   1.348 -  by (simp add: vector_norm_def dimindex_def)
   1.349 +  by (simp add: vector_norm_def UNIV_1)
   1.350  
   1.351  lemma norm_real: "norm(x::real ^ 1) = abs(x$1)"
   1.352    by (simp add: norm_vector_1)
   1.353 @@ -648,17 +629,16 @@
   1.354  text{* Metric *}
   1.355  
   1.356  text {* FIXME: generalize to arbitrary @{text real_normed_vector} types *}
   1.357 -definition dist:: "real ^ 'n \<Rightarrow> real ^ 'n \<Rightarrow> real" where
   1.358 +definition dist:: "real ^ 'n::finite \<Rightarrow> real ^ 'n \<Rightarrow> real" where
   1.359    "dist x y = norm (x - y)"
   1.360  
   1.361  lemma dist_real: "dist(x::real ^ 1) y = abs((x$1) - (y$1))"
   1.362 -  using dimindex_ge_1[of "UNIV :: 1 set"]
   1.363 -  by (auto simp add: norm_real dist_def vector_component Cart_lambda_beta[where ?'a = "1"] )
   1.364 +  by (auto simp add: norm_real dist_def)
   1.365  
   1.366  subsection {* A connectedness or intermediate value lemma with several applications. *}
   1.367  
   1.368  lemma connected_real_lemma:
   1.369 -  fixes f :: "real \<Rightarrow> real ^ 'n"
   1.370 +  fixes f :: "real \<Rightarrow> real ^ 'n::finite"
   1.371    assumes ab: "a \<le> b" and fa: "f a \<in> e1" and fb: "f b \<in> e2"
   1.372    and dst: "\<And>e x. a <= x \<Longrightarrow> x <= b \<Longrightarrow> 0 < e ==> \<exists>d > 0. \<forall>y. abs(y - x) < d \<longrightarrow> dist(f y) (f x) < e"
   1.373    and e1: "\<forall>y \<in> e1. \<exists>e > 0. \<forall>y'. dist y' y < e \<longrightarrow> y' \<in> e1"
   1.374 @@ -758,7 +738,11 @@
   1.375  
   1.376  text{* Hence derive more interesting properties of the norm. *}
   1.377  
   1.378 -lemma norm_0[simp]: "norm (0::real ^ 'n) = 0"
   1.379 +text {*
   1.380 +  This type-specific version is only here
   1.381 +  to make @{text normarith.ML} happy.
   1.382 +*}
   1.383 +lemma norm_0: "norm (0::real ^ _) = 0"
   1.384    by (rule norm_zero)
   1.385  
   1.386  lemma norm_mul[simp]: "norm(a *s x) = abs(a) * norm x"
   1.387 @@ -770,7 +754,7 @@
   1.388    by (simp add: vector_norm_def setL2_def dot_def power2_eq_square)
   1.389  lemma norm_pow_2: "norm x ^ 2 = x \<bullet> x"
   1.390    by (simp add: real_vector_norm_def)
   1.391 -lemma norm_eq_0_imp: "norm x = 0 ==> x = (0::real ^'n)" by (metis norm_eq_zero)
   1.392 +lemma norm_eq_0_imp: "norm x = 0 ==> x = (0::real ^'n::finite)" by (metis norm_eq_zero)
   1.393  lemma vector_mul_eq_0[simp]: "(a *s x = 0) \<longleftrightarrow> a = (0::'a::idom) \<or> x = 0"
   1.394    by vector
   1.395  lemma vector_mul_lcancel[simp]: "a *s x = a *s y \<longleftrightarrow> a = (0::real) \<or> x = y"
   1.396 @@ -781,7 +765,9 @@
   1.397    by (metis vector_mul_lcancel)
   1.398  lemma vector_mul_rcancel_imp: "x \<noteq> 0 \<Longrightarrow> (a::real) *s x = b *s x ==> a = b"
   1.399    by (metis vector_mul_rcancel)
   1.400 -lemma norm_cauchy_schwarz: "x \<bullet> y <= norm x * norm y"
   1.401 +lemma norm_cauchy_schwarz:
   1.402 +  fixes x y :: "real ^ 'n::finite"
   1.403 +  shows "x \<bullet> y <= norm x * norm y"
   1.404  proof-
   1.405    {assume "norm x = 0"
   1.406      hence ?thesis by (simp add: dot_lzero dot_rzero)}
   1.407 @@ -802,50 +788,74 @@
   1.408    ultimately show ?thesis by metis
   1.409  qed
   1.410  
   1.411 -lemma norm_cauchy_schwarz_abs: "\<bar>x \<bullet> y\<bar> \<le> norm x * norm y"
   1.412 +lemma norm_cauchy_schwarz_abs:
   1.413 +  fixes x y :: "real ^ 'n::finite"
   1.414 +  shows "\<bar>x \<bullet> y\<bar> \<le> norm x * norm y"
   1.415    using norm_cauchy_schwarz[of x y] norm_cauchy_schwarz[of x "-y"]
   1.416    by (simp add: real_abs_def dot_rneg)
   1.417  
   1.418 -lemma norm_triangle_sub: "norm (x::real ^'n) <= norm(y) + norm(x - y)"
   1.419 +lemma norm_triangle_sub: "norm (x::real ^'n::finite) <= norm(y) + norm(x - y)"
   1.420    using norm_triangle_ineq[of "y" "x - y"] by (simp add: ring_simps)
   1.421 -lemma norm_triangle_le: "norm(x::real ^'n) + norm y <= e ==> norm(x + y) <= e"
   1.422 +lemma norm_triangle_le: "norm(x::real ^'n::finite) + norm y <= e ==> norm(x + y) <= e"
   1.423    by (metis order_trans norm_triangle_ineq)
   1.424 -lemma norm_triangle_lt: "norm(x::real ^'n) + norm(y) < e ==> norm(x + y) < e"
   1.425 +lemma norm_triangle_lt: "norm(x::real ^'n::finite) + norm(y) < e ==> norm(x + y) < e"
   1.426    by (metis basic_trans_rules(21) norm_triangle_ineq)
   1.427  
   1.428 -lemma component_le_norm: "i \<in> {1 .. dimindex(UNIV :: 'n set)} ==> \<bar>x$i\<bar> <= norm (x::real ^ 'n)"
   1.429 +lemma setsum_delta:
   1.430 +  assumes fS: "finite S"
   1.431 +  shows "setsum (\<lambda>k. if k=a then b k else 0) S = (if a \<in> S then b a else 0)"
   1.432 +proof-
   1.433 +  let ?f = "(\<lambda>k. if k=a then b k else 0)"
   1.434 +  {assume a: "a \<notin> S"
   1.435 +    hence "\<forall> k\<in> S. ?f k = 0" by simp
   1.436 +    hence ?thesis  using a by simp}
   1.437 +  moreover
   1.438 +  {assume a: "a \<in> S"
   1.439 +    let ?A = "S - {a}"
   1.440 +    let ?B = "{a}"
   1.441 +    have eq: "S = ?A \<union> ?B" using a by blast
   1.442 +    have dj: "?A \<inter> ?B = {}" by simp
   1.443 +    from fS have fAB: "finite ?A" "finite ?B" by auto
   1.444 +    have "setsum ?f S = setsum ?f ?A + setsum ?f ?B"
   1.445 +      using setsum_Un_disjoint[OF fAB dj, of ?f, unfolded eq[symmetric]]
   1.446 +      by simp
   1.447 +    then have ?thesis  using a by simp}
   1.448 +  ultimately show ?thesis by blast
   1.449 +qed
   1.450 +
   1.451 +lemma component_le_norm: "\<bar>x$i\<bar> <= norm (x::real ^ 'n::finite)"
   1.452    apply (simp add: vector_norm_def)
   1.453    apply (rule member_le_setL2, simp_all)
   1.454    done
   1.455  
   1.456 -lemma norm_bound_component_le: "norm(x::real ^ 'n) <= e
   1.457 -                ==> \<forall>i \<in> {1 .. dimindex(UNIV:: 'n set)}. \<bar>x$i\<bar> <= e"
   1.458 +lemma norm_bound_component_le: "norm(x::real ^ 'n::finite) <= e
   1.459 +                ==> \<bar>x$i\<bar> <= e"
   1.460    by (metis component_le_norm order_trans)
   1.461  
   1.462 -lemma norm_bound_component_lt: "norm(x::real ^ 'n) < e
   1.463 -                ==> \<forall>i \<in> {1 .. dimindex(UNIV:: 'n set)}. \<bar>x$i\<bar> < e"
   1.464 +lemma norm_bound_component_lt: "norm(x::real ^ 'n::finite) < e
   1.465 +                ==> \<bar>x$i\<bar> < e"
   1.466    by (metis component_le_norm basic_trans_rules(21))
   1.467  
   1.468 -lemma norm_le_l1: "norm (x:: real ^'n) <= setsum(\<lambda>i. \<bar>x$i\<bar>) {1..dimindex(UNIV::'n set)}"
   1.469 +lemma norm_le_l1: "norm (x:: real ^'n::finite) <= setsum(\<lambda>i. \<bar>x$i\<bar>) UNIV"
   1.470    by (simp add: vector_norm_def setL2_le_setsum)
   1.471  
   1.472 -lemma real_abs_norm[simp]: "\<bar> norm x\<bar> = norm (x :: real ^'n)"
   1.473 +lemma real_abs_norm: "\<bar>norm x\<bar> = norm (x :: real ^ _)"
   1.474    by (rule abs_norm_cancel)
   1.475 -lemma real_abs_sub_norm: "\<bar>norm(x::real ^'n) - norm y\<bar> <= norm(x - y)"
   1.476 +lemma real_abs_sub_norm: "\<bar>norm(x::real ^'n::finite) - norm y\<bar> <= norm(x - y)"
   1.477    by (rule norm_triangle_ineq3)
   1.478 -lemma norm_le: "norm(x::real ^ 'n) <= norm(y) \<longleftrightarrow> x \<bullet> x <= y \<bullet> y"
   1.479 +lemma norm_le: "norm(x::real ^ _) <= norm(y) \<longleftrightarrow> x \<bullet> x <= y \<bullet> y"
   1.480    by (simp add: real_vector_norm_def)
   1.481 -lemma norm_lt: "norm(x::real ^'n) < norm(y) \<longleftrightarrow> x \<bullet> x < y \<bullet> y"
   1.482 +lemma norm_lt: "norm(x::real ^ _) < norm(y) \<longleftrightarrow> x \<bullet> x < y \<bullet> y"
   1.483    by (simp add: real_vector_norm_def)
   1.484 -lemma norm_eq: "norm (x::real ^'n) = norm y \<longleftrightarrow> x \<bullet> x = y \<bullet> y"
   1.485 +lemma norm_eq: "norm (x::real ^ _) = norm y \<longleftrightarrow> x \<bullet> x = y \<bullet> y"
   1.486    by (simp add: order_eq_iff norm_le)
   1.487 -lemma norm_eq_1: "norm(x::real ^ 'n) = 1 \<longleftrightarrow> x \<bullet> x = 1"
   1.488 +lemma norm_eq_1: "norm(x::real ^ _) = 1 \<longleftrightarrow> x \<bullet> x = 1"
   1.489    by (simp add: real_vector_norm_def)
   1.490  
   1.491  text{* Squaring equations and inequalities involving norms.  *}
   1.492  
   1.493  lemma dot_square_norm: "x \<bullet> x = norm(x)^2"
   1.494 -  by (simp add: real_vector_norm_def  dot_pos_le )
   1.495 +  by (simp add: real_vector_norm_def)
   1.496  
   1.497  lemma norm_eq_square: "norm(x) = a \<longleftrightarrow> 0 <= a \<and> x \<bullet> x = a^2"
   1.498    by (auto simp add: real_vector_norm_def)
   1.499 @@ -885,7 +895,7 @@
   1.500  
   1.501  text{* Equality of vectors in terms of @{term "op \<bullet>"} products.    *}
   1.502  
   1.503 -lemma vector_eq: "(x:: real ^ 'n) = y \<longleftrightarrow> x \<bullet> x = x \<bullet> y\<and> y \<bullet> y = x \<bullet> x" (is "?lhs \<longleftrightarrow> ?rhs")
   1.504 +lemma vector_eq: "(x:: real ^ 'n::finite) = y \<longleftrightarrow> x \<bullet> x = x \<bullet> y\<and> y \<bullet> y = x \<bullet> x" (is "?lhs \<longleftrightarrow> ?rhs")
   1.505  proof
   1.506    assume "?lhs" then show ?rhs by simp
   1.507  next
   1.508 @@ -907,7 +917,7 @@
   1.509    done
   1.510  
   1.511    (* FIXME: Move all these theorems into the ML code using lemma antiquotation *)
   1.512 -lemma norm_add_rule_thm: "b1 >= norm(x1 :: real ^'n) \<Longrightarrow> b2 >= norm(x2) ==> b1 + b2 >= norm(x1 + x2)"
   1.513 +lemma norm_add_rule_thm: "b1 >= norm(x1 :: real ^'n::finite) \<Longrightarrow> b2 >= norm(x2) ==> b1 + b2 >= norm(x1 + x2)"
   1.514    apply (rule norm_triangle_le) by simp
   1.515  
   1.516  lemma ge_iff_diff_ge_0: "(a::'a::ordered_ring) \<ge> b == a - b \<ge> 0"
   1.517 @@ -936,13 +946,13 @@
   1.518    "(c *s x + w) + (d *s y + z) == d *s y + ((c *s x + w) + z)" by ((atomize (full)), vector)+
   1.519  lemma pth_d: "x + (0::real ^'n) == x" by (atomize (full)) vector
   1.520  
   1.521 -lemma norm_imp_pos_and_ge: "norm (x::real ^ 'n) == n \<Longrightarrow> norm x \<ge> 0 \<and> n \<ge> norm x"
   1.522 +lemma norm_imp_pos_and_ge: "norm (x::real ^ _) == n \<Longrightarrow> norm x \<ge> 0 \<and> n \<ge> norm x"
   1.523    by (atomize) (auto simp add: norm_ge_zero)
   1.524  
   1.525  lemma real_eq_0_iff_le_ge_0: "(x::real) = 0 == x \<ge> 0 \<and> -x \<ge> 0" by arith
   1.526  
   1.527  lemma norm_pths:
   1.528 -  "(x::real ^'n) = y \<longleftrightarrow> norm (x - y) \<le> 0"
   1.529 +  "(x::real ^'n::finite) = y \<longleftrightarrow> norm (x - y) \<le> 0"
   1.530    "x \<noteq> y \<longleftrightarrow> \<not> (norm (x - y) \<le> 0)"
   1.531    using norm_ge_zero[of "x - y"] by auto
   1.532  
   1.533 @@ -988,13 +998,13 @@
   1.534  
   1.535  lemma dist_le_0[simp]: "dist x y <= 0 \<longleftrightarrow> x = y" by norm
   1.536  
   1.537 +lemma setsum_component [simp]:
   1.538 +  fixes f:: " 'a \<Rightarrow> ('b::comm_monoid_add) ^'n"
   1.539 +  shows "(setsum f S)$i = setsum (\<lambda>x. (f x)$i) S"
   1.540 +  by (cases "finite S", induct S set: finite, simp_all)
   1.541 +
   1.542  lemma setsum_eq: "setsum f S = (\<chi> i. setsum (\<lambda>x. (f x)$i ) S)"
   1.543 -  apply vector
   1.544 -  apply auto
   1.545 -  apply (cases "finite S")
   1.546 -  apply (rule finite_induct[of S])
   1.547 -  apply (auto simp add: vector_component zero_index)
   1.548 -  done
   1.549 +  by (simp add: Cart_eq)
   1.550  
   1.551  lemma setsum_clauses:
   1.552    shows "setsum f {} = 0"
   1.553 @@ -1005,13 +1015,7 @@
   1.554  lemma setsum_cmul:
   1.555    fixes f:: "'c \<Rightarrow> ('a::semiring_1)^'n"
   1.556    shows "setsum (\<lambda>x. c *s f x) S = c *s setsum f S"
   1.557 -  by (simp add: setsum_eq Cart_eq Cart_lambda_beta vector_component setsum_right_distrib)
   1.558 -
   1.559 -lemma setsum_component:
   1.560 -  fixes f:: " 'a \<Rightarrow> ('b::semiring_1) ^'n"
   1.561 -  assumes i: "i \<in> {1 .. dimindex(UNIV:: 'n set)}"
   1.562 -  shows "(setsum f S)$i = setsum (\<lambda>x. (f x)$i) S"
   1.563 -  using i by (simp add: setsum_eq Cart_lambda_beta)
   1.564 +  by (simp add: Cart_eq setsum_right_distrib)
   1.565  
   1.566  lemma setsum_norm:
   1.567    fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
   1.568 @@ -1028,7 +1032,7 @@
   1.569  qed
   1.570  
   1.571  lemma real_setsum_norm:
   1.572 -  fixes f :: "'a \<Rightarrow> real ^'n"
   1.573 +  fixes f :: "'a \<Rightarrow> real ^'n::finite"
   1.574    assumes fS: "finite S"
   1.575    shows "norm (setsum f S) <= setsum (\<lambda>x. norm(f x)) S"
   1.576  proof(induct rule: finite_induct[OF fS])
   1.577 @@ -1054,7 +1058,7 @@
   1.578  qed
   1.579  
   1.580  lemma real_setsum_norm_le:
   1.581 -  fixes f :: "'a \<Rightarrow> real ^ 'n"
   1.582 +  fixes f :: "'a \<Rightarrow> real ^ 'n::finite"
   1.583    assumes fS: "finite S"
   1.584    and fg: "\<forall>x \<in> S. norm (f x) \<le> g x"
   1.585    shows "norm (setsum f S) \<le> setsum g S"
   1.586 @@ -1074,7 +1078,7 @@
   1.587    by simp
   1.588  
   1.589  lemma real_setsum_norm_bound:
   1.590 -  fixes f :: "'a \<Rightarrow> real ^ 'n"
   1.591 +  fixes f :: "'a \<Rightarrow> real ^ 'n::finite"
   1.592    assumes fS: "finite S"
   1.593    and K: "\<forall>x \<in> S. norm (f x) \<le> K"
   1.594    shows "norm (setsum f S) \<le> of_nat (card S) * K"
   1.595 @@ -1155,13 +1159,13 @@
   1.596  by (auto intro: setsum_0')
   1.597  
   1.598  lemma vsum_norm_allsubsets_bound:
   1.599 -  fixes f:: "'a \<Rightarrow> real ^'n"
   1.600 +  fixes f:: "'a \<Rightarrow> real ^'n::finite"
   1.601    assumes fP: "finite P" and fPs: "\<And>Q. Q \<subseteq> P \<Longrightarrow> norm (setsum f Q) \<le> e"
   1.602 -  shows "setsum (\<lambda>x. norm (f x)) P \<le> 2 * real (dimindex(UNIV :: 'n set)) *  e"
   1.603 +  shows "setsum (\<lambda>x. norm (f x)) P \<le> 2 * real CARD('n) *  e"
   1.604  proof-
   1.605 -  let ?d = "real (dimindex (UNIV ::'n set))"
   1.606 +  let ?d = "real CARD('n)"
   1.607    let ?nf = "\<lambda>x. norm (f x)"
   1.608 -  let ?U = "{1 .. dimindex (UNIV :: 'n set)}"
   1.609 +  let ?U = "UNIV :: 'n set"
   1.610    have th0: "setsum (\<lambda>x. setsum (\<lambda>i. \<bar>f x $ i\<bar>) ?U) P = setsum (\<lambda>i. setsum (\<lambda>x. \<bar>f x $ i\<bar>) P) ?U"
   1.611      by (rule setsum_commute)
   1.612    have th1: "2 * ?d * e = of_nat (card ?U) * (2 * e)" by (simp add: real_of_nat_def)
   1.613 @@ -1178,11 +1182,11 @@
   1.614      have thp0: "?Pp \<inter> ?Pn ={}" by auto
   1.615      have PpP: "?Pp \<subseteq> P" and PnP: "?Pn \<subseteq> P" by blast+
   1.616      have Ppe:"setsum (\<lambda>x. \<bar>f x $ i\<bar>) ?Pp \<le> e"
   1.617 -      using i component_le_norm[OF i, of "setsum (\<lambda>x. f x) ?Pp"]  fPs[OF PpP]
   1.618 -      by (auto simp add: setsum_component intro: abs_le_D1)
   1.619 +      using component_le_norm[of "setsum (\<lambda>x. f x) ?Pp" i]  fPs[OF PpP]
   1.620 +      by (auto intro: abs_le_D1)
   1.621      have Pne: "setsum (\<lambda>x. \<bar>f x $ i\<bar>) ?Pn \<le> e"
   1.622 -      using i component_le_norm[OF i, of "setsum (\<lambda>x. - f x) ?Pn"]  fPs[OF PnP]
   1.623 -      by (auto simp add: setsum_negf setsum_component vector_component intro: abs_le_D1)
   1.624 +      using component_le_norm[of "setsum (\<lambda>x. - f x) ?Pn" i]  fPs[OF PnP]
   1.625 +      by (auto simp add: setsum_negf intro: abs_le_D1)
   1.626      have "setsum (\<lambda>x. \<bar>f x $ i\<bar>) P = setsum (\<lambda>x. \<bar>f x $ i\<bar>) ?Pp + setsum (\<lambda>x. \<bar>f x $ i\<bar>) ?Pn"
   1.627        apply (subst thp)
   1.628        apply (rule setsum_Un_zero)
   1.629 @@ -1204,32 +1208,29 @@
   1.630  
   1.631  definition "basis k = (\<chi> i. if i = k then 1 else 0)"
   1.632  
   1.633 +lemma basis_component [simp]: "basis k $ i = (if k=i then 1 else 0)"
   1.634 +  unfolding basis_def by simp
   1.635 +
   1.636  lemma delta_mult_idempotent:
   1.637    "(if k=a then 1 else (0::'a::semiring_1)) * (if k=a then 1 else 0) = (if k=a then 1 else 0)" by (cases "k=a", auto)
   1.638  
   1.639  lemma norm_basis:
   1.640 -  assumes k: "k \<in> {1 .. dimindex (UNIV :: 'n set)}"
   1.641 -  shows "norm (basis k :: real ^'n) = 1"
   1.642 -  using k
   1.643 +  shows "norm (basis k :: real ^'n::finite) = 1"
   1.644    apply (simp add: basis_def real_vector_norm_def dot_def)
   1.645    apply (vector delta_mult_idempotent)
   1.646 -  using setsum_delta[of "{1 .. dimindex (UNIV :: 'n set)}" "k" "\<lambda>k. 1::real"]
   1.647 +  using setsum_delta[of "UNIV :: 'n set" "k" "\<lambda>k. 1::real"]
   1.648    apply auto
   1.649    done
   1.650  
   1.651 -lemma norm_basis_1: "norm(basis 1 :: real ^'n) = 1"
   1.652 -  apply (simp add: basis_def real_vector_norm_def dot_def)
   1.653 -  apply (vector delta_mult_idempotent)
   1.654 -  using setsum_delta[of "{1 .. dimindex (UNIV :: 'n set)}" "1" "\<lambda>k. 1::real"] dimindex_nonzero[of "UNIV :: 'n set"]
   1.655 -  apply auto
   1.656 -  done
   1.657 -
   1.658 -lemma vector_choose_size: "0 <= c ==> \<exists>(x::real^'n). norm x = c"
   1.659 -  apply (rule exI[where x="c *s basis 1"])
   1.660 -  by (simp only: norm_mul norm_basis_1)
   1.661 +lemma norm_basis_1: "norm(basis 1 :: real ^'n::{finite,one}) = 1"
   1.662 +  by (rule norm_basis)
   1.663 +
   1.664 +lemma vector_choose_size: "0 <= c ==> \<exists>(x::real^'n::finite). norm x = c"
   1.665 +  apply (rule exI[where x="c *s basis arbitrary"])
   1.666 +  by (simp only: norm_mul norm_basis)
   1.667  
   1.668  lemma vector_choose_dist: assumes e: "0 <= e"
   1.669 -  shows "\<exists>(y::real^'n). dist x y = e"
   1.670 +  shows "\<exists>(y::real^'n::finite). dist x y = e"
   1.671  proof-
   1.672    from vector_choose_size[OF e] obtain c:: "real ^'n"  where "norm c = e"
   1.673      by blast
   1.674 @@ -1237,56 +1238,50 @@
   1.675    then show ?thesis by blast
   1.676  qed
   1.677  
   1.678 -lemma basis_inj: "inj_on (basis :: nat \<Rightarrow> real ^'n) {1 .. dimindex (UNIV :: 'n set)}"
   1.679 -  by (auto simp add: inj_on_def basis_def Cart_eq Cart_lambda_beta)
   1.680 -
   1.681 -lemma basis_component: "i \<in> {1 .. dimindex(UNIV:: 'n set)} ==> (basis k ::('a::semiring_1)^'n)$i = (if k=i then 1 else 0)"
   1.682 -  by (simp add: basis_def Cart_lambda_beta)
   1.683 +lemma basis_inj: "inj (basis :: 'n \<Rightarrow> real ^'n::finite)"
   1.684 +  by (simp add: inj_on_def Cart_eq)
   1.685  
   1.686  lemma cond_value_iff: "f (if b then x else y) = (if b then f x else f y)"
   1.687    by auto
   1.688  
   1.689  lemma basis_expansion:
   1.690 -  "setsum (\<lambda>i. (x$i) *s basis i) {1 .. dimindex (UNIV :: 'n set)} = (x::('a::ring_1) ^'n)" (is "?lhs = ?rhs" is "setsum ?f ?S = _")
   1.691 -  by (auto simp add: Cart_eq basis_component[where ?'n = "'n"] setsum_component vector_component cond_value_iff setsum_delta[of "?S", where ?'b = "'a", simplified] cong del: if_weak_cong)
   1.692 +  "setsum (\<lambda>i. (x$i) *s basis i) UNIV = (x::('a::ring_1) ^'n::finite)" (is "?lhs = ?rhs" is "setsum ?f ?S = _")
   1.693 +  by (auto simp add: Cart_eq cond_value_iff setsum_delta[of "?S", where ?'b = "'a", simplified] cong del: if_weak_cong)
   1.694  
   1.695  lemma basis_expansion_unique:
   1.696 -  "setsum (\<lambda>i. f i *s basis i) {1 .. dimindex (UNIV :: 'n set)} = (x::('a::comm_ring_1) ^'n) \<longleftrightarrow> (\<forall>i\<in>{1 .. dimindex(UNIV:: 'n set)}. f i = x$i)"
   1.697 -  by (simp add: Cart_eq setsum_component vector_component basis_component setsum_delta cond_value_iff cong del: if_weak_cong)
   1.698 +  "setsum (\<lambda>i. f i *s basis i) UNIV = (x::('a::comm_ring_1) ^'n::finite) \<longleftrightarrow> (\<forall>i. f i = x$i)"
   1.699 +  by (simp add: Cart_eq setsum_delta cond_value_iff cong del: if_weak_cong)
   1.700  
   1.701  lemma cond_application_beta: "(if b then f else g) x = (if b then f x else g x)"
   1.702    by auto
   1.703  
   1.704  lemma dot_basis:
   1.705 -  assumes i: "i \<in> {1 .. dimindex (UNIV :: 'n set)}"
   1.706 -  shows "basis i \<bullet> x = x$i" "x \<bullet> (basis i :: 'a^'n) = (x$i :: 'a::semiring_1)"
   1.707 -  using i
   1.708 -  by (auto simp add: dot_def basis_def Cart_lambda_beta cond_application_beta  cond_value_iff setsum_delta cong del: if_weak_cong)
   1.709 -
   1.710 -lemma basis_eq_0: "basis i = (0::'a::semiring_1^'n) \<longleftrightarrow> i \<notin> {1..dimindex(UNIV ::'n set)}"
   1.711 -  by (auto simp add: Cart_eq basis_component zero_index)
   1.712 +  shows "basis i \<bullet> x = x$i" "x \<bullet> (basis i :: 'a^'n::finite) = (x$i :: 'a::semiring_1)"
   1.713 +  by (auto simp add: dot_def basis_def cond_application_beta  cond_value_iff setsum_delta cong del: if_weak_cong)
   1.714 +
   1.715 +lemma basis_eq_0: "basis i = (0::'a::semiring_1^'n) \<longleftrightarrow> False"
   1.716 +  by (auto simp add: Cart_eq)
   1.717  
   1.718  lemma basis_nonzero:
   1.719 -  assumes k: "k \<in> {1 .. dimindex(UNIV ::'n set)}"
   1.720    shows "basis k \<noteq> (0:: 'a::semiring_1 ^'n)"
   1.721 -  using k by (simp add: basis_eq_0)
   1.722 -
   1.723 -lemma vector_eq_ldot: "(\<forall>x. x \<bullet> y = x \<bullet> z) \<longleftrightarrow> y = (z::'a::semiring_1^'n)"
   1.724 +  by (simp add: basis_eq_0)
   1.725 +
   1.726 +lemma vector_eq_ldot: "(\<forall>x. x \<bullet> y = x \<bullet> z) \<longleftrightarrow> y = (z::'a::semiring_1^'n::finite)"
   1.727    apply (auto simp add: Cart_eq dot_basis)
   1.728    apply (erule_tac x="basis i" in allE)
   1.729    apply (simp add: dot_basis)
   1.730    apply (subgoal_tac "y = z")
   1.731    apply simp
   1.732 -  apply vector
   1.733 +  apply (simp add: Cart_eq)
   1.734    done
   1.735  
   1.736 -lemma vector_eq_rdot: "(\<forall>z. x \<bullet> z = y \<bullet> z) \<longleftrightarrow> x = (y::'a::semiring_1^'n)"
   1.737 +lemma vector_eq_rdot: "(\<forall>z. x \<bullet> z = y \<bullet> z) \<longleftrightarrow> x = (y::'a::semiring_1^'n::finite)"
   1.738    apply (auto simp add: Cart_eq dot_basis)
   1.739    apply (erule_tac x="basis i" in allE)
   1.740    apply (simp add: dot_basis)
   1.741    apply (subgoal_tac "x = y")
   1.742    apply simp
   1.743 -  apply vector
   1.744 +  apply (simp add: Cart_eq)
   1.745    done
   1.746  
   1.747  subsection{* Orthogonality. *}
   1.748 @@ -1294,16 +1289,12 @@
   1.749  definition "orthogonal x y \<longleftrightarrow> (x \<bullet> y = 0)"
   1.750  
   1.751  lemma orthogonal_basis:
   1.752 -  assumes i:"i \<in> {1 .. dimindex(UNIV ::'n set)}"
   1.753 -  shows "orthogonal (basis i :: 'a^'n) x \<longleftrightarrow> x$i = (0::'a::ring_1)"
   1.754 -  using i
   1.755 -  by (auto simp add: orthogonal_def dot_def basis_def Cart_lambda_beta cond_value_iff cond_application_beta setsum_delta cong del: if_weak_cong)
   1.756 +  shows "orthogonal (basis i :: 'a^'n::finite) x \<longleftrightarrow> x$i = (0::'a::ring_1)"
   1.757 +  by (auto simp add: orthogonal_def dot_def basis_def cond_value_iff cond_application_beta setsum_delta cong del: if_weak_cong)
   1.758  
   1.759  lemma orthogonal_basis_basis:
   1.760 -  assumes i:"i \<in> {1 .. dimindex(UNIV ::'n set)}"
   1.761 -  and j: "j \<in> {1 .. dimindex(UNIV ::'n set)}"
   1.762 -  shows "orthogonal (basis i :: 'a::ring_1^'n) (basis j) \<longleftrightarrow> i \<noteq> j"
   1.763 -  unfolding orthogonal_basis[OF i] basis_component[OF i] by simp
   1.764 +  shows "orthogonal (basis i :: 'a::ring_1^'n::finite) (basis j) \<longleftrightarrow> i \<noteq> j"
   1.765 +  unfolding orthogonal_basis[of i] basis_component[of j] by simp
   1.766  
   1.767    (* FIXME : Maybe some of these require less than comm_ring, but not all*)
   1.768  lemma orthogonal_clauses:
   1.769 @@ -1326,51 +1317,43 @@
   1.770  
   1.771  subsection{* Explicit vector construction from lists. *}
   1.772  
   1.773 -lemma Cart_lambda_beta_1[simp]: "(Cart_lambda g)$1 = g 1"
   1.774 -  apply (rule Cart_lambda_beta[rule_format])
   1.775 -  using dimindex_ge_1 apply auto done
   1.776 -
   1.777 -lemma Cart_lambda_beta_1'[simp]: "(Cart_lambda g)$(Suc 0) = g 1"
   1.778 -  by (simp only: One_nat_def[symmetric] Cart_lambda_beta_1)
   1.779 -
   1.780 -definition "vector l = (\<chi> i. if i <= length l then l ! (i - 1) else 0)"
   1.781 +primrec from_nat :: "nat \<Rightarrow> 'a::{monoid_add,one}"
   1.782 +where "from_nat 0 = 0" | "from_nat (Suc n) = 1 + from_nat n"
   1.783 +
   1.784 +lemma from_nat [simp]: "from_nat = of_nat"
   1.785 +by (rule ext, induct_tac x, simp_all)
   1.786 +
   1.787 +primrec
   1.788 +  list_fun :: "nat \<Rightarrow> _ list \<Rightarrow> _ \<Rightarrow> _"
   1.789 +where
   1.790 +  "list_fun n [] = (\<lambda>x. 0)"
   1.791 +| "list_fun n (x # xs) = fun_upd (list_fun (Suc n) xs) (from_nat n) x"
   1.792 +
   1.793 +definition "vector l = (\<chi> i. list_fun 1 l i)"
   1.794 +(*definition "vector l = (\<chi> i. if i <= length l then l ! (i - 1) else 0)"*)
   1.795  
   1.796  lemma vector_1: "(vector[x]) $1 = x"
   1.797 -  using dimindex_ge_1
   1.798 -  by (auto simp add: vector_def Cart_lambda_beta[rule_format])
   1.799 -lemma dimindex_2[simp]: "2 \<in> {1 .. dimindex (UNIV :: 2 set)}"
   1.800 -  by (auto simp add: dimindex_def)
   1.801 -lemma dimindex_2'[simp]: "2 \<in> {Suc 0 .. dimindex (UNIV :: 2 set)}"
   1.802 -  by (auto simp add: dimindex_def)
   1.803 -lemma dimindex_3[simp]: "2 \<in> {1 .. dimindex (UNIV :: 3 set)}" "3 \<in> {1 .. dimindex (UNIV :: 3 set)}"
   1.804 -  by (auto simp add: dimindex_def)
   1.805 -
   1.806 -lemma dimindex_3'[simp]: "2 \<in> {Suc 0 .. dimindex (UNIV :: 3 set)}" "3 \<in> {Suc 0 .. dimindex (UNIV :: 3 set)}"
   1.807 -  by (auto simp add: dimindex_def)
   1.808 +  unfolding vector_def by simp
   1.809  
   1.810  lemma vector_2:
   1.811   "(vector[x,y]) $1 = x"
   1.812   "(vector[x,y] :: 'a^2)$2 = (y::'a::zero)"
   1.813 -  apply (simp add: vector_def)
   1.814 -  using Cart_lambda_beta[rule_format, OF dimindex_2, of "\<lambda>i. if i \<le> length [x,y] then [x,y] ! (i - 1) else (0::'a)"]
   1.815 -  apply (simp only: vector_def )
   1.816 -  apply auto
   1.817 -  done
   1.818 +  unfolding vector_def by simp_all
   1.819  
   1.820  lemma vector_3:
   1.821   "(vector [x,y,z] ::('a::zero)^3)$1 = x"
   1.822   "(vector [x,y,z] ::('a::zero)^3)$2 = y"
   1.823   "(vector [x,y,z] ::('a::zero)^3)$3 = z"
   1.824 -apply (simp_all add: vector_def Cart_lambda_beta dimindex_3)
   1.825 -  using Cart_lambda_beta[rule_format, OF dimindex_3(1), of "\<lambda>i. if i \<le> length [x,y,z] then [x,y,z] ! (i - 1) else (0::'a)"]   using Cart_lambda_beta[rule_format, OF dimindex_3(2), of "\<lambda>i. if i \<le> length [x,y,z] then [x,y,z] ! (i - 1) else (0::'a)"]
   1.826 -  by simp_all
   1.827 +  unfolding vector_def by simp_all
   1.828  
   1.829  lemma forall_vector_1: "(\<forall>v::'a::zero^1. P v) \<longleftrightarrow> (\<forall>x. P(vector[x]))"
   1.830    apply auto
   1.831    apply (erule_tac x="v$1" in allE)
   1.832    apply (subgoal_tac "vector [v$1] = v")
   1.833    apply simp
   1.834 -  by (vector vector_def dimindex_def)
   1.835 +  apply (vector vector_def)
   1.836 +  apply (simp add: forall_1)
   1.837 +  done
   1.838  
   1.839  lemma forall_vector_2: "(\<forall>v::'a::zero^2. P v) \<longleftrightarrow> (\<forall>x y. P(vector[x, y]))"
   1.840    apply auto
   1.841 @@ -1378,9 +1361,8 @@
   1.842    apply (erule_tac x="v$2" in allE)
   1.843    apply (subgoal_tac "vector [v$1, v$2] = v")
   1.844    apply simp
   1.845 -  apply (vector vector_def dimindex_def)
   1.846 -  apply auto
   1.847 -  apply (subgoal_tac "i = 1 \<or> i =2", auto)
   1.848 +  apply (vector vector_def)
   1.849 +  apply (simp add: forall_2)
   1.850    done
   1.851  
   1.852  lemma forall_vector_3: "(\<forall>v::'a::zero^3. P v) \<longleftrightarrow> (\<forall>x y z. P(vector[x, y, z]))"
   1.853 @@ -1390,9 +1372,8 @@
   1.854    apply (erule_tac x="v$3" in allE)
   1.855    apply (subgoal_tac "vector [v$1, v$2, v$3] = v")
   1.856    apply simp
   1.857 -  apply (vector vector_def dimindex_def)
   1.858 -  apply auto
   1.859 -  apply (subgoal_tac "i = 1 \<or> i =2 \<or> i = 3", auto)
   1.860 +  apply (vector vector_def)
   1.861 +  apply (simp add: forall_3)
   1.862    done
   1.863  
   1.864  subsection{* Linear functions. *}
   1.865 @@ -1400,7 +1381,7 @@
   1.866  definition "linear f \<longleftrightarrow> (\<forall>x y. f(x + y) = f x + f y) \<and> (\<forall>c x. f(c *s x) = c *s f x)"
   1.867  
   1.868  lemma linear_compose_cmul: "linear f ==> linear (\<lambda>x. (c::'a::comm_semiring) *s f x)"
   1.869 -  by (vector linear_def Cart_eq Cart_lambda_beta[rule_format] ring_simps)
   1.870 +  by (vector linear_def Cart_eq ring_simps)
   1.871  
   1.872  lemma linear_compose_neg: "linear (f :: 'a ^'n \<Rightarrow> 'a::comm_ring ^'m) ==> linear (\<lambda>x. -(f(x)))" by (vector linear_def Cart_eq)
   1.873  
   1.874 @@ -1426,9 +1407,9 @@
   1.875  
   1.876  lemma linear_vmul_component:
   1.877    fixes f:: "'a::semiring_1^'m \<Rightarrow> 'a^'n"
   1.878 -  assumes lf: "linear f" and k: "k \<in> {1 .. dimindex (UNIV :: 'n set)}"
   1.879 +  assumes lf: "linear f"
   1.880    shows "linear (\<lambda>x. f x $ k *s v)"
   1.881 -  using lf k
   1.882 +  using lf
   1.883    apply (auto simp add: linear_def )
   1.884    by (vector ring_simps)+
   1.885  
   1.886 @@ -1485,15 +1466,15 @@
   1.887  qed
   1.888  
   1.889  lemma linear_bounded:
   1.890 -  fixes f:: "real ^'m \<Rightarrow> real ^'n"
   1.891 +  fixes f:: "real ^'m::finite \<Rightarrow> real ^'n::finite"
   1.892    assumes lf: "linear f"
   1.893    shows "\<exists>B. \<forall>x. norm (f x) \<le> B * norm x"
   1.894  proof-
   1.895 -  let ?S = "{1..dimindex(UNIV:: 'm set)}"
   1.896 +  let ?S = "UNIV:: 'm set"
   1.897    let ?B = "setsum (\<lambda>i. norm(f(basis i))) ?S"
   1.898    have fS: "finite ?S" by simp
   1.899    {fix x:: "real ^ 'm"
   1.900 -    let ?g = "(\<lambda>i::nat. (x$i) *s (basis i) :: real ^ 'm)"
   1.901 +    let ?g = "(\<lambda>i. (x$i) *s (basis i) :: real ^ 'm)"
   1.902      have "norm (f x) = norm (f (setsum (\<lambda>i. (x$i) *s (basis i)) ?S))"
   1.903        by (simp only:  basis_expansion)
   1.904      also have "\<dots> = norm (setsum (\<lambda>i. (x$i) *s f (basis i))?S)"
   1.905 @@ -1501,7 +1482,7 @@
   1.906        by auto
   1.907      finally have th0: "norm (f x) = norm (setsum (\<lambda>i. (x$i) *s f (basis i))?S)" .
   1.908      {fix i assume i: "i \<in> ?S"
   1.909 -      from component_le_norm[OF i, of x]
   1.910 +      from component_le_norm[of x i]
   1.911        have "norm ((x$i) *s f (basis i :: real ^'m)) \<le> norm (f (basis i)) * norm x"
   1.912        unfolding norm_mul
   1.913        apply (simp only: mult_commute)
   1.914 @@ -1514,7 +1495,7 @@
   1.915  qed
   1.916  
   1.917  lemma linear_bounded_pos:
   1.918 -  fixes f:: "real ^'n \<Rightarrow> real ^ 'm"
   1.919 +  fixes f:: "real ^'n::finite \<Rightarrow> real ^ 'm::finite"
   1.920    assumes lf: "linear f"
   1.921    shows "\<exists>B > 0. \<forall>x. norm (f x) \<le> B * norm x"
   1.922  proof-
   1.923 @@ -1595,12 +1576,12 @@
   1.924  qed
   1.925  
   1.926  lemma bilinear_bounded:
   1.927 -  fixes h:: "real ^'m \<Rightarrow> real^'n \<Rightarrow> real ^ 'k"
   1.928 +  fixes h:: "real ^'m::finite \<Rightarrow> real^'n::finite \<Rightarrow> real ^ 'k::finite"
   1.929    assumes bh: "bilinear h"
   1.930    shows "\<exists>B. \<forall>x y. norm (h x y) \<le> B * norm x * norm y"
   1.931  proof-
   1.932 -  let ?M = "{1 .. dimindex (UNIV :: 'm set)}"
   1.933 -  let ?N = "{1 .. dimindex (UNIV :: 'n set)}"
   1.934 +  let ?M = "UNIV :: 'm set"
   1.935 +  let ?N = "UNIV :: 'n set"
   1.936    let ?B = "setsum (\<lambda>(i,j). norm (h (basis i) (basis j))) (?M \<times> ?N)"
   1.937    have fM: "finite ?M" and fN: "finite ?N" by simp_all
   1.938    {fix x:: "real ^ 'm" and  y :: "real^'n"
   1.939 @@ -1622,7 +1603,7 @@
   1.940  qed
   1.941  
   1.942  lemma bilinear_bounded_pos:
   1.943 -  fixes h:: "real ^'m \<Rightarrow> real^'n \<Rightarrow> real ^ 'k"
   1.944 +  fixes h:: "real ^'m::finite \<Rightarrow> real^'n::finite \<Rightarrow> real ^ 'k::finite"
   1.945    assumes bh: "bilinear h"
   1.946    shows "\<exists>B > 0. \<forall>x y. norm (h x y) \<le> B * norm x * norm y"
   1.947  proof-
   1.948 @@ -1649,12 +1630,12 @@
   1.949  lemma choice_iff: "(\<forall>x. \<exists>y. P x y) \<longleftrightarrow> (\<exists>f. \<forall>x. P x (f x))" by metis
   1.950  
   1.951  lemma adjoint_works_lemma:
   1.952 -  fixes f:: "'a::ring_1 ^'n \<Rightarrow> 'a ^ 'm"
   1.953 +  fixes f:: "'a::ring_1 ^'n::finite \<Rightarrow> 'a ^ 'm::finite"
   1.954    assumes lf: "linear f"
   1.955    shows "\<forall>x y. f x \<bullet> y = x \<bullet> adjoint f y"
   1.956  proof-
   1.957 -  let ?N = "{1 .. dimindex (UNIV :: 'n set)}"
   1.958 -  let ?M = "{1 .. dimindex (UNIV :: 'm set)}"
   1.959 +  let ?N = "UNIV :: 'n set"
   1.960 +  let ?M = "UNIV :: 'm set"
   1.961    have fN: "finite ?N" by simp
   1.962    have fM: "finite ?M" by simp
   1.963    {fix y:: "'a ^ 'm"
   1.964 @@ -1667,7 +1648,7 @@
   1.965  	by (simp add: linear_cmul[OF lf])
   1.966        finally have "f x \<bullet> y = x \<bullet> ?w"
   1.967  	apply (simp only: )
   1.968 -	apply (simp add: dot_def setsum_component Cart_lambda_beta setsum_left_distrib setsum_right_distrib vector_component setsum_commute[of _ ?M ?N] ring_simps del: One_nat_def)
   1.969 +	apply (simp add: dot_def setsum_left_distrib setsum_right_distrib setsum_commute[of _ ?M ?N] ring_simps)
   1.970  	done}
   1.971    }
   1.972    then show ?thesis unfolding adjoint_def
   1.973 @@ -1677,34 +1658,34 @@
   1.974  qed
   1.975  
   1.976  lemma adjoint_works:
   1.977 -  fixes f:: "'a::ring_1 ^'n \<Rightarrow> 'a ^ 'm"
   1.978 +  fixes f:: "'a::ring_1 ^'n::finite \<Rightarrow> 'a ^ 'm::finite"
   1.979    assumes lf: "linear f"
   1.980    shows "x \<bullet> adjoint f y = f x \<bullet> y"
   1.981    using adjoint_works_lemma[OF lf] by metis
   1.982  
   1.983  
   1.984  lemma adjoint_linear:
   1.985 -  fixes f :: "'a::comm_ring_1 ^'n \<Rightarrow> 'a ^ 'm"
   1.986 +  fixes f :: "'a::comm_ring_1 ^'n::finite \<Rightarrow> 'a ^ 'm::finite"
   1.987    assumes lf: "linear f"
   1.988    shows "linear (adjoint f)"
   1.989    by (simp add: linear_def vector_eq_ldot[symmetric] dot_radd dot_rmult adjoint_works[OF lf])
   1.990  
   1.991  lemma adjoint_clauses:
   1.992 -  fixes f:: "'a::comm_ring_1 ^'n \<Rightarrow> 'a ^ 'm"
   1.993 +  fixes f:: "'a::comm_ring_1 ^'n::finite \<Rightarrow> 'a ^ 'm::finite"
   1.994    assumes lf: "linear f"
   1.995    shows "x \<bullet> adjoint f y = f x \<bullet> y"
   1.996    and "adjoint f y \<bullet> x = y \<bullet> f x"
   1.997    by (simp_all add: adjoint_works[OF lf] dot_sym )
   1.998  
   1.999  lemma adjoint_adjoint:
  1.1000 -  fixes f:: "'a::comm_ring_1 ^ 'n \<Rightarrow> _"
  1.1001 +  fixes f:: "'a::comm_ring_1 ^ 'n::finite \<Rightarrow> 'a ^ 'm::finite"
  1.1002    assumes lf: "linear f"
  1.1003    shows "adjoint (adjoint f) = f"
  1.1004    apply (rule ext)
  1.1005    by (simp add: vector_eq_ldot[symmetric] adjoint_clauses[OF adjoint_linear[OF lf]] adjoint_clauses[OF lf])
  1.1006  
  1.1007  lemma adjoint_unique:
  1.1008 -  fixes f:: "'a::comm_ring_1 ^ 'n \<Rightarrow> 'a ^ 'm"
  1.1009 +  fixes f:: "'a::comm_ring_1 ^ 'n::finite \<Rightarrow> 'a ^ 'm::finite"
  1.1010    assumes lf: "linear f" and u: "\<forall>x y. f' x \<bullet> y = x \<bullet> f y"
  1.1011    shows "f' = adjoint f"
  1.1012    apply (rule ext)
  1.1013 @@ -1716,14 +1697,14 @@
  1.1014  consts generic_mult :: "'a \<Rightarrow> 'b \<Rightarrow> 'c" (infixr "\<star>" 75)
  1.1015  
  1.1016  defs (overloaded)
  1.1017 -matrix_matrix_mult_def: "(m:: ('a::semiring_1) ^'n^'m) \<star> (m' :: 'a ^'p^'n) \<equiv> (\<chi> i j. setsum (\<lambda>k. ((m$i)$k) * ((m'$k)$j)) {1 .. dimindex (UNIV :: 'n set)}) ::'a ^ 'p ^'m"
  1.1018 +matrix_matrix_mult_def: "(m:: ('a::semiring_1) ^'n^'m) \<star> (m' :: 'a ^'p^'n) \<equiv> (\<chi> i j. setsum (\<lambda>k. ((m$i)$k) * ((m'$k)$j)) (UNIV :: 'n set)) ::'a ^ 'p ^'m"
  1.1019  
  1.1020  abbreviation
  1.1021    matrix_matrix_mult' :: "('a::semiring_1) ^'n^'m \<Rightarrow> 'a ^'p^'n \<Rightarrow> 'a ^ 'p ^'m"  (infixl "**" 70)
  1.1022    where "m ** m' == m\<star> m'"
  1.1023  
  1.1024  defs (overloaded)
  1.1025 -  matrix_vector_mult_def: "(m::('a::semiring_1) ^'n^'m) \<star> (x::'a ^'n) \<equiv> (\<chi> i. setsum (\<lambda>j. ((m$i)$j) * (x$j)) {1..dimindex(UNIV ::'n set)}) :: 'a^'m"
  1.1026 +  matrix_vector_mult_def: "(m::('a::semiring_1) ^'n^'m) \<star> (x::'a ^'n) \<equiv> (\<chi> i. setsum (\<lambda>j. ((m$i)$j) * (x$j)) (UNIV ::'n set)) :: 'a^'m"
  1.1027  
  1.1028  abbreviation
  1.1029    matrix_vector_mult' :: "('a::semiring_1) ^'n^'m \<Rightarrow> 'a ^'n \<Rightarrow> 'a ^ 'm"  (infixl "*v" 70)
  1.1030 @@ -1731,19 +1712,19 @@
  1.1031    "m *v v == m \<star> v"
  1.1032  
  1.1033  defs (overloaded)
  1.1034 -  vector_matrix_mult_def: "(x::'a^'m) \<star> (m::('a::semiring_1) ^'n^'m) \<equiv> (\<chi> j. setsum (\<lambda>i. ((m$i)$j) * (x$i)) {1..dimindex(UNIV :: 'm set)}) :: 'a^'n"
  1.1035 +  vector_matrix_mult_def: "(x::'a^'m) \<star> (m::('a::semiring_1) ^'n^'m) \<equiv> (\<chi> j. setsum (\<lambda>i. ((m$i)$j) * (x$i)) (UNIV :: 'm set)) :: 'a^'n"
  1.1036  
  1.1037  abbreviation
  1.1038    vactor_matrix_mult' :: "'a ^ 'm \<Rightarrow> ('a::semiring_1) ^'n^'m \<Rightarrow> 'a ^'n "  (infixl "v*" 70)
  1.1039    where
  1.1040    "v v* m == v \<star> m"
  1.1041  
  1.1042 -definition "(mat::'a::zero => 'a ^'n^'m) k = (\<chi> i j. if i = j then k else 0)"
  1.1043 +definition "(mat::'a::zero => 'a ^'n^'n) k = (\<chi> i j. if i = j then k else 0)"
  1.1044  definition "(transp::'a^'n^'m \<Rightarrow> 'a^'m^'n) A = (\<chi> i j. ((A$j)$i))"
  1.1045 -definition "(row::nat => 'a ^'n^'m \<Rightarrow> 'a ^'n) i A = (\<chi> j. ((A$i)$j))"
  1.1046 -definition "(column::nat =>'a^'n^'m =>'a^'m) j A = (\<chi> i. ((A$i)$j))"
  1.1047 -definition "rows(A::'a^'n^'m) = { row i A | i. i \<in> {1 .. dimindex(UNIV :: 'm set)}}"
  1.1048 -definition "columns(A::'a^'n^'m) = { column i A | i. i \<in> {1 .. dimindex(UNIV :: 'n set)}}"
  1.1049 +definition "(row::'m => 'a ^'n^'m \<Rightarrow> 'a ^'n) i A = (\<chi> j. ((A$i)$j))"
  1.1050 +definition "(column::'n =>'a^'n^'m =>'a^'m) j A = (\<chi> i. ((A$i)$j))"
  1.1051 +definition "rows(A::'a^'n^'m) = { row i A | i. i \<in> (UNIV :: 'm set)}"
  1.1052 +definition "columns(A::'a^'n^'m) = { column i A | i. i \<in> (UNIV :: 'n set)}"
  1.1053  
  1.1054  lemma mat_0[simp]: "mat 0 = 0" by (vector mat_def)
  1.1055  lemma matrix_add_ldistrib: "(A ** (B + C)) = (A \<star> B) + (A \<star> C)"
  1.1056 @@ -1756,16 +1737,20 @@
  1.1057    using setsum_delta[OF fS, of a b, symmetric]
  1.1058    by (auto intro: setsum_cong)
  1.1059  
  1.1060 -lemma matrix_mul_lid: "mat 1 ** A = A"
  1.1061 +lemma matrix_mul_lid:
  1.1062 +  fixes A :: "'a::semiring_1 ^ 'm ^ 'n::finite"
  1.1063 +  shows "mat 1 ** A = A"
  1.1064    apply (simp add: matrix_matrix_mult_def mat_def)
  1.1065    apply vector
  1.1066 -  by (auto simp only: cond_value_iff cond_application_beta setsum_delta'[OF finite_atLeastAtMost]  mult_1_left mult_zero_left if_True)
  1.1067 -
  1.1068 -
  1.1069 -lemma matrix_mul_rid: "A ** mat 1 = A"
  1.1070 +  by (auto simp only: cond_value_iff cond_application_beta setsum_delta'[OF finite]  mult_1_left mult_zero_left if_True UNIV_I)
  1.1071 +
  1.1072 +
  1.1073 +lemma matrix_mul_rid:
  1.1074 +  fixes A :: "'a::semiring_1 ^ 'm::finite ^ 'n"
  1.1075 +  shows "A ** mat 1 = A"
  1.1076    apply (simp add: matrix_matrix_mult_def mat_def)
  1.1077    apply vector
  1.1078 -  by (auto simp only: cond_value_iff cond_application_beta setsum_delta[OF finite_atLeastAtMost]  mult_1_right mult_zero_right if_True cong: if_cong)
  1.1079 +  by (auto simp only: cond_value_iff cond_application_beta setsum_delta[OF finite]  mult_1_right mult_zero_right if_True UNIV_I cong: if_cong)
  1.1080  
  1.1081  lemma matrix_mul_assoc: "A ** (B ** C) = (A ** B) ** C"
  1.1082    apply (vector matrix_matrix_mult_def setsum_right_distrib setsum_left_distrib mult_assoc)
  1.1083 @@ -1779,31 +1764,31 @@
  1.1084    apply simp
  1.1085    done
  1.1086  
  1.1087 -lemma matrix_vector_mul_lid: "mat 1 *v x = x"
  1.1088 +lemma matrix_vector_mul_lid: "mat 1 *v x = (x::'a::semiring_1 ^ 'n::finite)"
  1.1089    apply (vector matrix_vector_mult_def mat_def)
  1.1090    by (simp add: cond_value_iff cond_application_beta
  1.1091      setsum_delta' cong del: if_weak_cong)
  1.1092  
  1.1093  lemma matrix_transp_mul: "transp(A ** B) = transp B ** transp (A::'a::comm_semiring_1^'m^'n)"
  1.1094 -  by (simp add: matrix_matrix_mult_def transp_def Cart_eq Cart_lambda_beta mult_commute)
  1.1095 -
  1.1096 -lemma matrix_eq: "A = B \<longleftrightarrow>  (\<forall>x. A *v x = B *v x)" (is "?lhs \<longleftrightarrow> ?rhs")
  1.1097 +  by (simp add: matrix_matrix_mult_def transp_def Cart_eq mult_commute)
  1.1098 +
  1.1099 +lemma matrix_eq:
  1.1100 +  fixes A B :: "'a::semiring_1 ^ 'n::finite ^ 'm"
  1.1101 +  shows "A = B \<longleftrightarrow>  (\<forall>x. A *v x = B *v x)" (is "?lhs \<longleftrightarrow> ?rhs")
  1.1102    apply auto
  1.1103    apply (subst Cart_eq)
  1.1104    apply clarify
  1.1105 -  apply (clarsimp simp add: matrix_vector_mult_def basis_def cond_value_iff cond_application_beta Cart_eq Cart_lambda_beta cong del: if_weak_cong)
  1.1106 +  apply (clarsimp simp add: matrix_vector_mult_def basis_def cond_value_iff cond_application_beta Cart_eq cong del: if_weak_cong)
  1.1107    apply (erule_tac x="basis ia" in allE)
  1.1108 -  apply (erule_tac x="i" in ballE)
  1.1109 -  by (auto simp add: basis_def cond_value_iff cond_application_beta Cart_lambda_beta setsum_delta[OF finite_atLeastAtMost] cong del: if_weak_cong)
  1.1110 +  apply (erule_tac x="i" in allE)
  1.1111 +  by (auto simp add: basis_def cond_value_iff cond_application_beta setsum_delta[OF finite] cong del: if_weak_cong)
  1.1112  
  1.1113  lemma matrix_vector_mul_component:
  1.1114 -  assumes k: "k \<in> {1.. dimindex (UNIV :: 'm set)}"
  1.1115    shows "((A::'a::semiring_1^'n'^'m) *v x)$k = (A$k) \<bullet> x"
  1.1116 -  using k
  1.1117 -  by (simp add: matrix_vector_mult_def Cart_lambda_beta dot_def)
  1.1118 +  by (simp add: matrix_vector_mult_def dot_def)
  1.1119  
  1.1120  lemma dot_lmul_matrix: "((x::'a::comm_semiring_1 ^'n) v* A) \<bullet> y = x \<bullet> (A *v y)"
  1.1121 -  apply (simp add: dot_def matrix_vector_mult_def vector_matrix_mult_def setsum_left_distrib setsum_right_distrib Cart_lambda_beta mult_ac)
  1.1122 +  apply (simp add: dot_def matrix_vector_mult_def vector_matrix_mult_def setsum_left_distrib setsum_right_distrib mult_ac)
  1.1123    apply (subst setsum_commute)
  1.1124    by simp
  1.1125  
  1.1126 @@ -1815,23 +1800,16 @@
  1.1127  
  1.1128  lemma row_transp:
  1.1129    fixes A:: "'a::semiring_1^'n^'m"
  1.1130 -  assumes i: "i \<in> {1.. dimindex (UNIV :: 'n set)}"
  1.1131    shows "row i (transp A) = column i A"
  1.1132 -  using i
  1.1133 -  by (simp add: row_def column_def transp_def Cart_eq Cart_lambda_beta)
  1.1134 +  by (simp add: row_def column_def transp_def Cart_eq)
  1.1135  
  1.1136  lemma column_transp:
  1.1137    fixes A:: "'a::semiring_1^'n^'m"
  1.1138 -  assumes i: "i \<in> {1.. dimindex (UNIV :: 'm set)}"
  1.1139    shows "column i (transp A) = row i A"
  1.1140 -  using i
  1.1141 -  by (simp add: row_def column_def transp_def Cart_eq Cart_lambda_beta)
  1.1142 +  by (simp add: row_def column_def transp_def Cart_eq)
  1.1143  
  1.1144  lemma rows_transp: "rows(transp (A::'a::semiring_1^'n^'m)) = columns A"
  1.1145 -apply (auto simp add: rows_def columns_def row_transp intro: set_ext)
  1.1146 -apply (rule_tac x=i in exI)
  1.1147 -apply (auto simp add: row_transp)
  1.1148 -done
  1.1149 +by (auto simp add: rows_def columns_def row_transp intro: set_ext)
  1.1150  
  1.1151  lemma columns_transp: "columns(transp (A::'a::semiring_1^'n^'m)) = rows A" by (metis transp_transp rows_transp)
  1.1152  
  1.1153 @@ -1840,25 +1818,25 @@
  1.1154  lemma matrix_mult_dot: "A *v x = (\<chi> i. A$i \<bullet> x)"
  1.1155    by (simp add: matrix_vector_mult_def dot_def)
  1.1156  
  1.1157 -lemma matrix_mult_vsum: "(A::'a::comm_semiring_1^'n^'m) *v x = setsum (\<lambda>i. (x$i) *s column i A) {1 .. dimindex(UNIV:: 'n set)}"
  1.1158 -  by (simp add: matrix_vector_mult_def Cart_eq setsum_component Cart_lambda_beta vector_component column_def mult_commute)
  1.1159 +lemma matrix_mult_vsum: "(A::'a::comm_semiring_1^'n^'m) *v x = setsum (\<lambda>i. (x$i) *s column i A) (UNIV:: 'n set)"
  1.1160 +  by (simp add: matrix_vector_mult_def Cart_eq column_def mult_commute)
  1.1161  
  1.1162  lemma vector_componentwise:
  1.1163 -  "(x::'a::ring_1^'n) = (\<chi> j. setsum (\<lambda>i. (x$i) * (basis i :: 'a^'n)$j) {1..dimindex(UNIV :: 'n set)})"
  1.1164 +  "(x::'a::ring_1^'n::finite) = (\<chi> j. setsum (\<lambda>i. (x$i) * (basis i :: 'a^'n)$j) (UNIV :: 'n set))"
  1.1165    apply (subst basis_expansion[symmetric])
  1.1166 -  by (vector Cart_eq Cart_lambda_beta setsum_component)
  1.1167 +  by (vector Cart_eq setsum_component)
  1.1168  
  1.1169  lemma linear_componentwise:
  1.1170 -  fixes f:: "'a::ring_1 ^ 'm \<Rightarrow> 'a ^ 'n"
  1.1171 -  assumes lf: "linear f" and j: "j \<in> {1 .. dimindex (UNIV :: 'n set)}"
  1.1172 -  shows "(f x)$j = setsum (\<lambda>i. (x$i) * (f (basis i)$j)) {1 .. dimindex (UNIV :: 'm set)}" (is "?lhs = ?rhs")
  1.1173 +  fixes f:: "'a::ring_1 ^ 'm::finite \<Rightarrow> 'a ^ 'n"
  1.1174 +  assumes lf: "linear f"
  1.1175 +  shows "(f x)$j = setsum (\<lambda>i. (x$i) * (f (basis i)$j)) (UNIV :: 'm set)" (is "?lhs = ?rhs")
  1.1176  proof-
  1.1177 -  let ?M = "{1 .. dimindex (UNIV :: 'm set)}"
  1.1178 -  let ?N = "{1 .. dimindex (UNIV :: 'n set)}"
  1.1179 +  let ?M = "(UNIV :: 'm set)"
  1.1180 +  let ?N = "(UNIV :: 'n set)"
  1.1181    have fM: "finite ?M" by simp
  1.1182    have "?rhs = (setsum (\<lambda>i.(x$i) *s f (basis i) ) ?M)$j"
  1.1183 -    unfolding vector_smult_component[OF j, symmetric]
  1.1184 -    unfolding setsum_component[OF j, of "(\<lambda>i.(x$i) *s f (basis i :: 'a^'m))" ?M]
  1.1185 +    unfolding vector_smult_component[symmetric]
  1.1186 +    unfolding setsum_component[of "(\<lambda>i.(x$i) *s f (basis i :: 'a^'m))" ?M]
  1.1187      ..
  1.1188    then show ?thesis unfolding linear_setsum_mul[OF lf fM, symmetric] basis_expansion ..
  1.1189  qed
  1.1190 @@ -1876,38 +1854,38 @@
  1.1191  where "matrix f = (\<chi> i j. (f(basis j))$i)"
  1.1192  
  1.1193  lemma matrix_vector_mul_linear: "linear(\<lambda>x. A *v (x::'a::comm_semiring_1 ^ 'n))"
  1.1194 -  by (simp add: linear_def matrix_vector_mult_def Cart_eq Cart_lambda_beta vector_component ring_simps setsum_right_distrib setsum_addf)
  1.1195 -
  1.1196 -lemma matrix_works: assumes lf: "linear f" shows "matrix f *v x = f (x::'a::comm_ring_1 ^ 'n)"
  1.1197 -apply (simp add: matrix_def matrix_vector_mult_def Cart_eq Cart_lambda_beta mult_commute del: One_nat_def)
  1.1198 +  by (simp add: linear_def matrix_vector_mult_def Cart_eq ring_simps setsum_right_distrib setsum_addf)
  1.1199 +
  1.1200 +lemma matrix_works: assumes lf: "linear f" shows "matrix f *v x = f (x::'a::comm_ring_1 ^ 'n::finite)"
  1.1201 +apply (simp add: matrix_def matrix_vector_mult_def Cart_eq mult_commute)
  1.1202  apply clarify
  1.1203  apply (rule linear_componentwise[OF lf, symmetric])
  1.1204 -apply simp
  1.1205  done
  1.1206  
  1.1207 -lemma matrix_vector_mul: "linear f ==> f = (\<lambda>x. matrix f *v (x::'a::comm_ring_1 ^ 'n))" by (simp add: ext matrix_works)
  1.1208 -
  1.1209 -lemma matrix_of_matrix_vector_mul: "matrix(\<lambda>x. A *v (x :: 'a:: comm_ring_1 ^ 'n)) = A"
  1.1210 +lemma matrix_vector_mul: "linear f ==> f = (\<lambda>x. matrix f *v (x::'a::comm_ring_1 ^ 'n::finite))" by (simp add: ext matrix_works)
  1.1211 +
  1.1212 +lemma matrix_of_matrix_vector_mul: "matrix(\<lambda>x. A *v (x :: 'a:: comm_ring_1 ^ 'n::finite)) = A"
  1.1213    by (simp add: matrix_eq matrix_vector_mul_linear matrix_works)
  1.1214  
  1.1215  lemma matrix_compose:
  1.1216 -  assumes lf: "linear (f::'a::comm_ring_1^'n \<Rightarrow> _)" and lg: "linear g"
  1.1217 +  assumes lf: "linear (f::'a::comm_ring_1^'n::finite \<Rightarrow> 'a^'m::finite)"
  1.1218 +  and lg: "linear (g::'a::comm_ring_1^'m::finite \<Rightarrow> 'a^'k)"
  1.1219    shows "matrix (g o f) = matrix g ** matrix f"
  1.1220    using lf lg linear_compose[OF lf lg] matrix_works[OF linear_compose[OF lf lg]]
  1.1221    by (simp  add: matrix_eq matrix_works matrix_vector_mul_assoc[symmetric] o_def)
  1.1222  
  1.1223 -lemma matrix_vector_column:"(A::'a::comm_semiring_1^'n^'m) *v x = setsum (\<lambda>i. (x$i) *s ((transp A)$i)) {1..dimindex(UNIV:: 'n set)}"
  1.1224 -  by (simp add: matrix_vector_mult_def transp_def Cart_eq Cart_lambda_beta setsum_component vector_component mult_commute)
  1.1225 -
  1.1226 -lemma adjoint_matrix: "adjoint(\<lambda>x. (A::'a::comm_ring_1^'n^'m) *v x) = (\<lambda>x. transp A *v x)"
  1.1227 +lemma matrix_vector_column:"(A::'a::comm_semiring_1^'n^'m) *v x = setsum (\<lambda>i. (x$i) *s ((transp A)$i)) (UNIV:: 'n set)"
  1.1228 +  by (simp add: matrix_vector_mult_def transp_def Cart_eq mult_commute)
  1.1229 +
  1.1230 +lemma adjoint_matrix: "adjoint(\<lambda>x. (A::'a::comm_ring_1^'n::finite^'m::finite) *v x) = (\<lambda>x. transp A *v x)"
  1.1231    apply (rule adjoint_unique[symmetric])
  1.1232    apply (rule matrix_vector_mul_linear)
  1.1233 -  apply (simp add: transp_def dot_def Cart_lambda_beta matrix_vector_mult_def setsum_left_distrib setsum_right_distrib)
  1.1234 +  apply (simp add: transp_def dot_def matrix_vector_mult_def setsum_left_distrib setsum_right_distrib)
  1.1235    apply (subst setsum_commute)
  1.1236    apply (auto simp add: mult_ac)
  1.1237    done
  1.1238  
  1.1239 -lemma matrix_adjoint: assumes lf: "linear (f :: 'a::comm_ring_1^'n \<Rightarrow> 'a ^ 'm)"
  1.1240 +lemma matrix_adjoint: assumes lf: "linear (f :: 'a::comm_ring_1^'n::finite \<Rightarrow> 'a ^ 'm::finite)"
  1.1241    shows "matrix(adjoint f) = transp(matrix f)"
  1.1242    apply (subst matrix_vector_mul[OF lf])
  1.1243    unfolding adjoint_matrix matrix_of_matrix_vector_mul ..
  1.1244 @@ -1980,21 +1958,21 @@
  1.1245  qed
  1.1246  
  1.1247  
  1.1248 -lemma lambda_skolem: "(\<forall>i \<in> {1 .. dimindex(UNIV :: 'n set)}. \<exists>x. P i x) \<longleftrightarrow>
  1.1249 -   (\<exists>x::'a ^ 'n. \<forall>i \<in> {1 .. dimindex(UNIV:: 'n set)}. P i (x$i))" (is "?lhs \<longleftrightarrow> ?rhs")
  1.1250 +lemma lambda_skolem: "(\<forall>i. \<exists>x. P i x) \<longleftrightarrow>
  1.1251 +   (\<exists>x::'a ^ 'n. \<forall>i. P i (x$i))" (is "?lhs \<longleftrightarrow> ?rhs")
  1.1252  proof-
  1.1253 -  let ?S = "{1 .. dimindex(UNIV :: 'n set)}"
  1.1254 +  let ?S = "(UNIV :: 'n set)"
  1.1255    {assume H: "?rhs"
  1.1256      then have ?lhs by auto}
  1.1257    moreover
  1.1258    {assume H: "?lhs"
  1.1259 -    then obtain f where f:"\<forall>i\<in> ?S. P i (f i)" unfolding Ball_def choice_iff by metis
  1.1260 +    then obtain f where f:"\<forall>i. P i (f i)" unfolding choice_iff by metis
  1.1261      let ?x = "(\<chi> i. (f i)) :: 'a ^ 'n"
  1.1262 -    {fix i assume i: "i \<in> ?S"
  1.1263 -      with f i have "P i (f i)" by metis
  1.1264 -      then have "P i (?x$i)" using Cart_lambda_beta[of f, rule_format, OF i] by auto
  1.1265 +    {fix i
  1.1266 +      from f have "P i (f i)" by metis
  1.1267 +      then have "P i (?x$i)" by auto
  1.1268      }
  1.1269 -    hence "\<forall>i \<in> ?S. P i (?x$i)" by metis
  1.1270 +    hence "\<forall>i. P i (?x$i)" by metis
  1.1271      hence ?rhs by metis }
  1.1272    ultimately show ?thesis by metis
  1.1273  qed
  1.1274 @@ -2237,7 +2215,7 @@
  1.1275  definition "onorm f = rsup {norm (f x)| x. norm x = 1}"
  1.1276  
  1.1277  lemma norm_bound_generalize:
  1.1278 -  fixes f:: "real ^'n \<Rightarrow> real^'m"
  1.1279 +  fixes f:: "real ^'n::finite \<Rightarrow> real^'m::finite"
  1.1280    assumes lf: "linear f"
  1.1281    shows "(\<forall>x. norm x = 1 \<longrightarrow> norm (f x) \<le> b) \<longleftrightarrow> (\<forall>x. norm (f x) \<le> b * norm x)" (is "?lhs \<longleftrightarrow> ?rhs")
  1.1282  proof-
  1.1283 @@ -2248,8 +2226,8 @@
  1.1284  
  1.1285    moreover
  1.1286    {assume H: ?lhs
  1.1287 -    from H[rule_format, of "basis 1"]
  1.1288 -    have bp: "b \<ge> 0" using norm_ge_zero[of "f (basis 1)"] dimindex_ge_1[of "UNIV:: 'n set"]
  1.1289 +    from H[rule_format, of "basis arbitrary"]
  1.1290 +    have bp: "b \<ge> 0" using norm_ge_zero[of "f (basis arbitrary)"]
  1.1291        by (auto simp add: norm_basis elim: order_trans [OF norm_ge_zero])
  1.1292      {fix x :: "real ^'n"
  1.1293        {assume "x = 0"
  1.1294 @@ -2270,14 +2248,14 @@
  1.1295  qed
  1.1296  
  1.1297  lemma onorm:
  1.1298 -  fixes f:: "real ^'n \<Rightarrow> real ^'m"
  1.1299 +  fixes f:: "real ^'n::finite \<Rightarrow> real ^'m::finite"
  1.1300    assumes lf: "linear f"
  1.1301    shows "norm (f x) <= onorm f * norm x"
  1.1302    and "\<forall>x. norm (f x) <= b * norm x \<Longrightarrow> onorm f <= b"
  1.1303  proof-
  1.1304    {
  1.1305      let ?S = "{norm (f x) |x. norm x = 1}"
  1.1306 -    have Se: "?S \<noteq> {}" using  norm_basis_1 by auto
  1.1307 +    have Se: "?S \<noteq> {}" using  norm_basis by auto
  1.1308      from linear_bounded[OF lf] have b: "\<exists> b. ?S *<= b"
  1.1309        unfolding norm_bound_generalize[OF lf, symmetric] by (auto simp add: setle_def)
  1.1310      {from rsup[OF Se b, unfolded onorm_def[symmetric]]
  1.1311 @@ -2294,10 +2272,10 @@
  1.1312    }
  1.1313  qed
  1.1314  
  1.1315 -lemma onorm_pos_le: assumes lf: "linear (f::real ^'n \<Rightarrow> real ^'m)" shows "0 <= onorm f"
  1.1316 -  using order_trans[OF norm_ge_zero onorm(1)[OF lf, of "basis 1"], unfolded norm_basis_1] by simp
  1.1317 -
  1.1318 -lemma onorm_eq_0: assumes lf: "linear (f::real ^'n \<Rightarrow> real ^'m)"
  1.1319 +lemma onorm_pos_le: assumes lf: "linear (f::real ^'n::finite \<Rightarrow> real ^'m::finite)" shows "0 <= onorm f"
  1.1320 +  using order_trans[OF norm_ge_zero onorm(1)[OF lf, of "basis arbitrary"], unfolded norm_basis] by simp
  1.1321 +
  1.1322 +lemma onorm_eq_0: assumes lf: "linear (f::real ^'n::finite \<Rightarrow> real ^'m::finite)"
  1.1323    shows "onorm f = 0 \<longleftrightarrow> (\<forall>x. f x = 0)"
  1.1324    using onorm[OF lf]
  1.1325    apply (auto simp add: onorm_pos_le)
  1.1326 @@ -2307,7 +2285,7 @@
  1.1327    apply arith
  1.1328    done
  1.1329  
  1.1330 -lemma onorm_const: "onorm(\<lambda>x::real^'n. (y::real ^ 'm)) = norm y"
  1.1331 +lemma onorm_const: "onorm(\<lambda>x::real^'n::finite. (y::real ^ 'm::finite)) = norm y"
  1.1332  proof-
  1.1333    let ?f = "\<lambda>x::real^'n. (y::real ^ 'm)"
  1.1334    have th: "{norm (?f x)| x. norm x = 1} = {norm y}"
  1.1335 @@ -2317,13 +2295,14 @@
  1.1336      apply (rule rsup_unique) by (simp_all  add: setle_def)
  1.1337  qed
  1.1338  
  1.1339 -lemma onorm_pos_lt: assumes lf: "linear (f::real ^ 'n \<Rightarrow> real ^'m)"
  1.1340 +lemma onorm_pos_lt: assumes lf: "linear (f::real ^ 'n::finite \<Rightarrow> real ^'m::finite)"
  1.1341    shows "0 < onorm f \<longleftrightarrow> ~(\<forall>x. f x = 0)"
  1.1342    unfolding onorm_eq_0[OF lf, symmetric]
  1.1343    using onorm_pos_le[OF lf] by arith
  1.1344  
  1.1345  lemma onorm_compose:
  1.1346 -  assumes lf: "linear (f::real ^'n \<Rightarrow> real ^'m)" and lg: "linear g"
  1.1347 +  assumes lf: "linear (f::real ^'n::finite \<Rightarrow> real ^'m::finite)"
  1.1348 +  and lg: "linear (g::real^'k::finite \<Rightarrow> real^'n::finite)"
  1.1349    shows "onorm (f o g) <= onorm f * onorm g"
  1.1350    apply (rule onorm(2)[OF linear_compose[OF lg lf], rule_format])
  1.1351    unfolding o_def
  1.1352 @@ -2335,18 +2314,18 @@
  1.1353    apply (rule onorm_pos_le[OF lf])
  1.1354    done
  1.1355  
  1.1356 -lemma onorm_neg_lemma: assumes lf: "linear (f::real ^'n \<Rightarrow> real^'m)"
  1.1357 +lemma onorm_neg_lemma: assumes lf: "linear (f::real ^'n::finite \<Rightarrow> real^'m::finite)"
  1.1358    shows "onorm (\<lambda>x. - f x) \<le> onorm f"
  1.1359    using onorm[OF linear_compose_neg[OF lf]] onorm[OF lf]
  1.1360    unfolding norm_minus_cancel by metis
  1.1361  
  1.1362 -lemma onorm_neg: assumes lf: "linear (f::real ^'n \<Rightarrow> real^'m)"
  1.1363 +lemma onorm_neg: assumes lf: "linear (f::real ^'n::finite \<Rightarrow> real^'m::finite)"
  1.1364    shows "onorm (\<lambda>x. - f x) = onorm f"
  1.1365    using onorm_neg_lemma[OF lf] onorm_neg_lemma[OF linear_compose_neg[OF lf]]
  1.1366    by simp
  1.1367  
  1.1368  lemma onorm_triangle:
  1.1369 -  assumes lf: "linear (f::real ^'n \<Rightarrow> real ^'m)" and lg: "linear g"
  1.1370 +  assumes lf: "linear (f::real ^'n::finite \<Rightarrow> real ^'m::finite)" and lg: "linear g"
  1.1371    shows "onorm (\<lambda>x. f x + g x) <= onorm f + onorm g"
  1.1372    apply(rule onorm(2)[OF linear_compose_add[OF lf lg], rule_format])
  1.1373    apply (rule order_trans)
  1.1374 @@ -2357,14 +2336,14 @@
  1.1375    apply (rule onorm(1)[OF lg])
  1.1376    done
  1.1377  
  1.1378 -lemma onorm_triangle_le: "linear (f::real ^'n \<Rightarrow> real ^'m) \<Longrightarrow> linear g \<Longrightarrow> onorm(f) + onorm(g) <= e
  1.1379 +lemma onorm_triangle_le: "linear (f::real ^'n::finite \<Rightarrow> real ^'m::finite) \<Longrightarrow> linear g \<Longrightarrow> onorm(f) + onorm(g) <= e
  1.1380    \<Longrightarrow> onorm(\<lambda>x. f x + g x) <= e"
  1.1381    apply (rule order_trans)
  1.1382    apply (rule onorm_triangle)
  1.1383    apply assumption+
  1.1384    done
  1.1385  
  1.1386 -lemma onorm_triangle_lt: "linear (f::real ^'n \<Rightarrow> real ^'m) \<Longrightarrow> linear g \<Longrightarrow> onorm(f) + onorm(g) < e
  1.1387 +lemma onorm_triangle_lt: "linear (f::real ^'n::finite \<Rightarrow> real ^'m::finite) \<Longrightarrow> linear g \<Longrightarrow> onorm(f) + onorm(g) < e
  1.1388    ==> onorm(\<lambda>x. f x + g x) < e"
  1.1389    apply (rule order_le_less_trans)
  1.1390    apply (rule onorm_triangle)
  1.1391 @@ -2381,7 +2360,7 @@
  1.1392    by (simp add: vec1_def)
  1.1393  
  1.1394  lemma vec1_dest_vec1[simp]: "vec1(dest_vec1 x) = x" "dest_vec1(vec1 y) = y"
  1.1395 -  by (simp_all add: vec1_def dest_vec1_def Cart_eq Cart_lambda_beta dimindex_def del: One_nat_def)
  1.1396 +  by (simp_all add: vec1_def dest_vec1_def Cart_eq forall_1)
  1.1397  
  1.1398  lemma forall_vec1: "(\<forall>x. P x) \<longleftrightarrow> (\<forall>x. P (vec1 x))" by (metis vec1_dest_vec1)
  1.1399  
  1.1400 @@ -2451,21 +2430,21 @@
  1.1401    shows "linear f \<Longrightarrow> linear (\<lambda>x. dest_vec1(f x) *s v)"
  1.1402    unfolding dest_vec1_def
  1.1403    apply (rule linear_vmul_component)
  1.1404 -  by (auto simp add: dimindex_def)
  1.1405 +  by auto
  1.1406  
  1.1407  lemma linear_from_scalars:
  1.1408    assumes lf: "linear (f::'a::comm_ring_1 ^1 \<Rightarrow> 'a^'n)"
  1.1409    shows "f = (\<lambda>x. dest_vec1 x *s column 1 (matrix f))"
  1.1410    apply (rule ext)
  1.1411    apply (subst matrix_works[OF lf, symmetric])
  1.1412 -  apply (auto simp add: Cart_eq matrix_vector_mult_def dest_vec1_def column_def Cart_lambda_beta vector_component dimindex_def mult_commute del: One_nat_def )
  1.1413 +  apply (auto simp add: Cart_eq matrix_vector_mult_def dest_vec1_def column_def  mult_commute UNIV_1)
  1.1414    done
  1.1415  
  1.1416 -lemma linear_to_scalars: assumes lf: "linear (f::'a::comm_ring_1 ^'n \<Rightarrow> 'a^1)"
  1.1417 +lemma linear_to_scalars: assumes lf: "linear (f::'a::comm_ring_1 ^'n::finite \<Rightarrow> 'a^1)"
  1.1418    shows "f = (\<lambda>x. vec1(row 1 (matrix f) \<bullet> x))"
  1.1419    apply (rule ext)
  1.1420    apply (subst matrix_works[OF lf, symmetric])
  1.1421 -  apply (auto simp add: Cart_eq matrix_vector_mult_def vec1_def row_def Cart_lambda_beta vector_component dimindex_def dot_def mult_commute)
  1.1422 +  apply (simp add: Cart_eq matrix_vector_mult_def vec1_def row_def dot_def mult_commute forall_1)
  1.1423    done
  1.1424  
  1.1425  lemma dest_vec1_eq_0: "dest_vec1 x = 0 \<longleftrightarrow> x = 0"
  1.1426 @@ -2485,25 +2464,25 @@
  1.1427  text{* Pasting vectors. *}
  1.1428  
  1.1429  lemma linear_fstcart: "linear fstcart"
  1.1430 -  by (auto simp add: linear_def fstcart_def Cart_eq Cart_lambda_beta vector_component dimindex_finite_sum)
  1.1431 +  by (auto simp add: linear_def Cart_eq)
  1.1432  
  1.1433  lemma linear_sndcart: "linear sndcart"
  1.1434 -  by (auto simp add: linear_def sndcart_def Cart_eq Cart_lambda_beta vector_component dimindex_finite_sum)
  1.1435 +  by (auto simp add: linear_def Cart_eq)
  1.1436  
  1.1437  lemma fstcart_vec[simp]: "fstcart(vec x) = vec x"
  1.1438 -  by (vector fstcart_def vec_def dimindex_finite_sum)
  1.1439 -
  1.1440 -lemma fstcart_add[simp]:"fstcart(x + y) = fstcart (x::'a::{plus,times}^('b,'c) finite_sum) + fstcart y"
  1.1441 -  using linear_fstcart[unfolded linear_def] by blast
  1.1442 -
  1.1443 -lemma fstcart_cmul[simp]:"fstcart(c*s x) = c*s fstcart (x::'a::{plus,times}^('b,'c) finite_sum)"
  1.1444 -  using linear_fstcart[unfolded linear_def] by blast
  1.1445 -
  1.1446 -lemma fstcart_neg[simp]:"fstcart(- x) = - fstcart (x::'a::ring_1^('b,'c) finite_sum)"
  1.1447 -unfolding vector_sneg_minus1 fstcart_cmul ..
  1.1448 -
  1.1449 -lemma fstcart_sub[simp]:"fstcart(x - y) = fstcart (x::'a::ring_1^('b,'c) finite_sum) - fstcart y"
  1.1450 -  unfolding diff_def fstcart_add fstcart_neg  ..
  1.1451 +  by (simp add: Cart_eq)
  1.1452 +
  1.1453 +lemma fstcart_add[simp]:"fstcart(x + y) = fstcart (x::'a::{plus,times}^('b + 'c)) + fstcart y"
  1.1454 +  by (simp add: Cart_eq)
  1.1455 +
  1.1456 +lemma fstcart_cmul[simp]:"fstcart(c*s x) = c*s fstcart (x::'a::{plus,times}^('b + 'c))"
  1.1457 +  by (simp add: Cart_eq)
  1.1458 +
  1.1459 +lemma fstcart_neg[simp]:"fstcart(- x) = - fstcart (x::'a::ring_1^('b + 'c))"
  1.1460 +  by (simp add: Cart_eq)
  1.1461 +
  1.1462 +lemma fstcart_sub[simp]:"fstcart(x - y) = fstcart (x::'a::ring_1^('b + 'c)) - fstcart y"
  1.1463 +  by (simp add: Cart_eq)
  1.1464  
  1.1465  lemma fstcart_setsum:
  1.1466    fixes f:: "'d \<Rightarrow> 'a::semiring_1^_"
  1.1467 @@ -2512,19 +2491,19 @@
  1.1468    by (induct rule: finite_induct[OF fS], simp_all add: vec_0[symmetric] del: vec_0)
  1.1469  
  1.1470  lemma sndcart_vec[simp]: "sndcart(vec x) = vec x"
  1.1471 -  by (vector sndcart_def vec_def dimindex_finite_sum)
  1.1472 -
  1.1473 -lemma sndcart_add[simp]:"sndcart(x + y) = sndcart (x::'a::{plus,times}^('b,'c) finite_sum) + sndcart y"
  1.1474 -  using linear_sndcart[unfolded linear_def] by blast
  1.1475 -
  1.1476 -lemma sndcart_cmul[simp]:"sndcart(c*s x) = c*s sndcart (x::'a::{plus,times}^('b,'c) finite_sum)"
  1.1477 -  using linear_sndcart[unfolded linear_def] by blast
  1.1478 -
  1.1479 -lemma sndcart_neg[simp]:"sndcart(- x) = - sndcart (x::'a::ring_1^('b,'c) finite_sum)"
  1.1480 -unfolding vector_sneg_minus1 sndcart_cmul ..
  1.1481 -
  1.1482 -lemma sndcart_sub[simp]:"sndcart(x - y) = sndcart (x::'a::ring_1^('b,'c) finite_sum) - sndcart y"
  1.1483 -  unfolding diff_def sndcart_add sndcart_neg  ..
  1.1484 +  by (simp add: Cart_eq)
  1.1485 +
  1.1486 +lemma sndcart_add[simp]:"sndcart(x + y) = sndcart (x::'a::{plus,times}^('b + 'c)) + sndcart y"
  1.1487 +  by (simp add: Cart_eq)
  1.1488 +
  1.1489 +lemma sndcart_cmul[simp]:"sndcart(c*s x) = c*s sndcart (x::'a::{plus,times}^('b + 'c))"
  1.1490 +  by (simp add: Cart_eq)
  1.1491 +
  1.1492 +lemma sndcart_neg[simp]:"sndcart(- x) = - sndcart (x::'a::ring_1^('b + 'c))"
  1.1493 +  by (simp add: Cart_eq)
  1.1494 +
  1.1495 +lemma sndcart_sub[simp]:"sndcart(x - y) = sndcart (x::'a::ring_1^('b + 'c)) - sndcart y"
  1.1496 +  by (simp add: Cart_eq)
  1.1497  
  1.1498  lemma sndcart_setsum:
  1.1499    fixes f:: "'d \<Rightarrow> 'a::semiring_1^_"
  1.1500 @@ -2533,10 +2512,10 @@
  1.1501    by (induct rule: finite_induct[OF fS], simp_all add: vec_0[symmetric] del: vec_0)
  1.1502  
  1.1503  lemma pastecart_vec[simp]: "pastecart (vec x) (vec x) = vec x"
  1.1504 -  by (simp add: pastecart_eq fstcart_vec sndcart_vec fstcart_pastecart sndcart_pastecart)
  1.1505 +  by (simp add: pastecart_eq fstcart_pastecart sndcart_pastecart)
  1.1506  
  1.1507  lemma pastecart_add[simp]:"pastecart (x1::'a::{plus,times}^_) y1 + pastecart x2 y2 = pastecart (x1 + x2) (y1 + y2)"
  1.1508 -  by (simp add: pastecart_eq fstcart_add sndcart_add fstcart_pastecart sndcart_pastecart)
  1.1509 +  by (simp add: pastecart_eq fstcart_pastecart sndcart_pastecart)
  1.1510  
  1.1511  lemma pastecart_cmul[simp]: "pastecart (c *s (x1::'a::{plus,times}^_)) (c *s y1) = c *s pastecart x1 y1"
  1.1512    by (simp add: pastecart_eq fstcart_pastecart sndcart_pastecart)
  1.1513 @@ -2553,109 +2532,53 @@
  1.1514    shows "pastecart (setsum f S) (setsum g S) = setsum (\<lambda>i. pastecart (f i) (g i)) S"
  1.1515    by (simp  add: pastecart_eq fstcart_setsum[OF fS] sndcart_setsum[OF fS] fstcart_pastecart sndcart_pastecart)
  1.1516  
  1.1517 -lemma norm_fstcart: "norm(fstcart x) <= norm (x::real ^('n,'m) finite_sum)"
  1.1518 +lemma setsum_Plus:
  1.1519 +  "\<lbrakk>finite A; finite B\<rbrakk> \<Longrightarrow>
  1.1520 +    (\<Sum>x\<in>A <+> B. g x) = (\<Sum>x\<in>A. g (Inl x)) + (\<Sum>x\<in>B. g (Inr x))"
  1.1521 +  unfolding Plus_def
  1.1522 +  by (subst setsum_Un_disjoint, auto simp add: setsum_reindex)
  1.1523 +
  1.1524 +lemma setsum_UNIV_sum:
  1.1525 +  fixes g :: "'a::finite + 'b::finite \<Rightarrow> _"
  1.1526 +  shows "(\<Sum>x\<in>UNIV. g x) = (\<Sum>x\<in>UNIV. g (Inl x)) + (\<Sum>x\<in>UNIV. g (Inr x))"
  1.1527 +  apply (subst UNIV_Plus_UNIV [symmetric])
  1.1528 +  apply (rule setsum_Plus [OF finite finite])
  1.1529 +  done
  1.1530 +
  1.1531 +lemma norm_fstcart: "norm(fstcart x) <= norm (x::real ^('n::finite + 'm::finite))"
  1.1532  proof-
  1.1533 -  let ?n = "dimindex (UNIV :: 'n set)"
  1.1534 -  let ?m = "dimindex (UNIV :: 'm set)"
  1.1535 -  let ?N = "{1 .. ?n}"
  1.1536 -  let ?M = "{1 .. ?m}"
  1.1537 -  let ?NM = "{1 .. dimindex (UNIV :: ('n,'m) finite_sum set)}"
  1.1538 -  have th_0: "1 \<le> ?n +1" by simp
  1.1539    have th0: "norm x = norm (pastecart (fstcart x) (sndcart x))"
  1.1540      by (simp add: pastecart_fst_snd)
  1.1541    have th1: "fstcart x \<bullet> fstcart x \<le> pastecart (fstcart x) (sndcart x) \<bullet> pastecart (fstcart x) (sndcart x)"
  1.1542 -    by (simp add: dot_def setsum_add_split[OF th_0, of _ ?m] pastecart_def dimindex_finite_sum Cart_lambda_beta setsum_nonneg zero_le_square del: One_nat_def)
  1.1543 +    by (simp add: dot_def setsum_UNIV_sum pastecart_def setsum_nonneg)
  1.1544    then show ?thesis
  1.1545      unfolding th0
  1.1546      unfolding real_vector_norm_def real_sqrt_le_iff id_def
  1.1547 -    by (simp add: dot_def dimindex_finite_sum Cart_lambda_beta)
  1.1548 +    by (simp add: dot_def)
  1.1549  qed
  1.1550  
  1.1551  lemma dist_fstcart: "dist(fstcart (x::real^_)) (fstcart y) <= dist x y"
  1.1552    by (metis dist_def fstcart_sub[symmetric] norm_fstcart)
  1.1553  
  1.1554 -lemma norm_sndcart: "norm(sndcart x) <= norm (x::real ^('n,'m) finite_sum)"
  1.1555 +lemma norm_sndcart: "norm(sndcart x) <= norm (x::real ^('n::finite + 'm::finite))"
  1.1556  proof-
  1.1557 -  let ?n = "dimindex (UNIV :: 'n set)"
  1.1558 -  let ?m = "dimindex (UNIV :: 'm set)"
  1.1559 -  let ?N = "{1 .. ?n}"
  1.1560 -  let ?M = "{1 .. ?m}"
  1.1561 -  let ?nm = "dimindex (UNIV :: ('n,'m) finite_sum set)"
  1.1562 -  let ?NM = "{1 .. ?nm}"
  1.1563 -  have thnm[simp]: "?nm = ?n + ?m" by (simp add: dimindex_finite_sum)
  1.1564 -  have th_0: "1 \<le> ?n +1" by simp
  1.1565    have th0: "norm x = norm (pastecart (fstcart x) (sndcart x))"
  1.1566      by (simp add: pastecart_fst_snd)
  1.1567 -  let ?f = "\<lambda>n. n - ?n"
  1.1568 -  let ?S = "{?n+1 .. ?nm}"
  1.1569 -  have finj:"inj_on ?f ?S"
  1.1570 -    using dimindex_nonzero[of "UNIV :: 'n set"] dimindex_nonzero[of "UNIV :: 'm set"]
  1.1571 -    apply (simp add: Ball_def atLeastAtMost_iff inj_on_def dimindex_finite_sum del: One_nat_def)
  1.1572 -    by arith
  1.1573 -  have fS: "?f ` ?S = ?M"
  1.1574 -    apply (rule set_ext)
  1.1575 -    apply (simp add: image_iff Bex_def) using dimindex_nonzero[of "UNIV :: 'n set"] dimindex_nonzero[of "UNIV :: 'm set"] by arith
  1.1576    have th1: "sndcart x \<bullet> sndcart x \<le> pastecart (fstcart x) (sndcart x) \<bullet> pastecart (fstcart x) (sndcart x)"
  1.1577 -    by (simp add: dot_def setsum_add_split[OF th_0, of _ ?m] pastecart_def dimindex_finite_sum Cart_lambda_beta setsum_nonneg zero_le_square setsum_reindex[OF finj, unfolded fS] del: One_nat_def)
  1.1578 +    by (simp add: dot_def setsum_UNIV_sum pastecart_def setsum_nonneg)
  1.1579    then show ?thesis
  1.1580      unfolding th0
  1.1581      unfolding real_vector_norm_def real_sqrt_le_iff id_def
  1.1582 -    by (simp add: dot_def dimindex_finite_sum Cart_lambda_beta)
  1.1583 +    by (simp add: dot_def)
  1.1584  qed
  1.1585  
  1.1586  lemma dist_sndcart: "dist(sndcart (x::real^_)) (sndcart y) <= dist x y"
  1.1587    by (metis dist_def sndcart_sub[symmetric] norm_sndcart)
  1.1588  
  1.1589 -lemma dot_pastecart: "(pastecart (x1::'a::{times,comm_monoid_add}^'n) (x2::'a::{times,comm_monoid_add}^'m)) \<bullet> (pastecart y1 y2) =  x1 \<bullet> y1 + x2 \<bullet> y2"
  1.1590 -proof-
  1.1591 -  let ?n = "dimindex (UNIV :: 'n set)"
  1.1592 -  let ?m = "dimindex (UNIV :: 'm set)"
  1.1593 -  let ?N = "{1 .. ?n}"
  1.1594 -  let ?M = "{1 .. ?m}"
  1.1595 -  let ?nm = "dimindex (UNIV :: ('n,'m) finite_sum set)"
  1.1596 -  let ?NM = "{1 .. ?nm}"
  1.1597 -  have thnm: "?nm = ?n + ?m" by (simp add: dimindex_finite_sum)
  1.1598 -  have th_0: "1 \<le> ?n +1" by simp
  1.1599 -  have th_1: "\<And>i. i \<in> {?m + 1 .. ?nm} \<Longrightarrow> i - ?m \<in> ?N" apply (simp add: thnm) by arith
  1.1600 -  let ?f = "\<lambda>a b i. (a$i) * (b$i)"
  1.1601 -  let ?g = "?f (pastecart x1 x2) (pastecart y1 y2)"
  1.1602 -  let ?S = "{?n +1 .. ?nm}"
  1.1603 -  {fix i
  1.1604 -    assume i: "i \<in> ?N"
  1.1605 -    have "?g i = ?f x1 y1 i"
  1.1606 -      using i
  1.1607 -      apply (simp add: pastecart_def Cart_lambda_beta thnm) done
  1.1608 -  }
  1.1609 -  hence th2: "setsum ?g ?N = setsum (?f x1 y1) ?N"
  1.1610 -    apply -
  1.1611 -    apply (rule setsum_cong)
  1.1612 -    apply auto
  1.1613 -    done
  1.1614 -  {fix i
  1.1615 -    assume i: "i \<in> ?S"
  1.1616 -    have "?g i = ?f x2 y2 (i - ?n)"
  1.1617 -      using i
  1.1618 -      apply (simp add: pastecart_def Cart_lambda_beta thnm) done
  1.1619 -  }
  1.1620 -  hence th3: "setsum ?g ?S = setsum (\<lambda>i. ?f x2 y2 (i -?n)) ?S"
  1.1621 -    apply -
  1.1622 -    apply (rule setsum_cong)
  1.1623 -    apply auto
  1.1624 -    done
  1.1625 -  let ?r = "\<lambda>n. n - ?n"
  1.1626 -  have rinj: "inj_on ?r ?S" apply (simp add: inj_on_def Ball_def thnm) by arith
  1.1627 -  have rS: "?r ` ?S = ?M" apply (rule set_ext)
  1.1628 -    apply (simp add: thnm image_iff Bex_def) by arith
  1.1629 -  have "pastecart x1 x2 \<bullet> (pastecart y1 y2) = setsum ?g ?NM" by (simp add: dot_def)
  1.1630 -  also have "\<dots> = setsum ?g ?N + setsum ?g ?S"
  1.1631 -    by (simp add: dot_def thnm setsum_add_split[OF th_0, of _ ?m] del: One_nat_def)
  1.1632 -  also have "\<dots> = setsum (?f x1 y1) ?N + setsum (?f x2 y2) ?M"
  1.1633 -    unfolding setsum_reindex[OF rinj, unfolded rS o_def] th2 th3 ..
  1.1634 -  finally
  1.1635 -  show ?thesis by (simp add: dot_def)
  1.1636 -qed
  1.1637 -
  1.1638 -lemma norm_pastecart: "norm(pastecart x y) <= norm(x :: real ^ _) + norm(y)"
  1.1639 +lemma dot_pastecart: "(pastecart (x1::'a::{times,comm_monoid_add}^'n::finite) (x2::'a::{times,comm_monoid_add}^'m::finite)) \<bullet> (pastecart y1 y2) =  x1 \<bullet> y1 + x2 \<bullet> y2"
  1.1640 +  by (simp add: dot_def setsum_UNIV_sum pastecart_def)
  1.1641 +
  1.1642 +lemma norm_pastecart: "norm(pastecart x y) <= norm(x :: real ^ 'm::finite) + norm(y::real^'n::finite)"
  1.1643    unfolding real_vector_norm_def dot_pastecart real_sqrt_le_iff id_def
  1.1644    apply (rule power2_le_imp_le)
  1.1645    apply (simp add: real_sqrt_pow2[OF add_nonneg_nonneg[OF dot_pos_le[of x] dot_pos_le[of y]]])
  1.1646 @@ -3419,7 +3342,7 @@
  1.1647  
  1.1648  (* Standard bases are a spanning set, and obviously finite.                  *)
  1.1649  
  1.1650 -lemma span_stdbasis:"span {basis i :: 'a::ring_1^'n | i. i \<in> {1 .. dimindex(UNIV :: 'n set)}} = UNIV"
  1.1651 +lemma span_stdbasis:"span {basis i :: 'a::ring_1^'n::finite | i. i \<in> (UNIV :: 'n set)} = UNIV"
  1.1652  apply (rule set_ext)
  1.1653  apply auto
  1.1654  apply (subst basis_expansion[symmetric])
  1.1655 @@ -3431,47 +3354,43 @@
  1.1656  apply (auto simp add: Collect_def mem_def)
  1.1657  done
  1.1658  
  1.1659 -
  1.1660 -lemma has_size_stdbasis: "{basis i ::real ^'n | i. i \<in> {1 .. dimindex (UNIV :: 'n set)}} hassize (dimindex(UNIV :: 'n set))" (is "?S hassize ?n")
  1.1661 +lemma has_size_stdbasis: "{basis i ::real ^'n::finite | i. i \<in> (UNIV :: 'n set)} hassize CARD('n)" (is "?S hassize ?n")
  1.1662  proof-
  1.1663 -  have eq: "?S = basis ` {1 .. ?n}" by blast
  1.1664 +  have eq: "?S = basis ` UNIV" by blast
  1.1665    show ?thesis unfolding eq
  1.1666      apply (rule hassize_image_inj[OF basis_inj])
  1.1667      by (simp add: hassize_def)
  1.1668  qed
  1.1669  
  1.1670 -lemma finite_stdbasis: "finite {basis i ::real^'n |i. i\<in> {1 .. dimindex(UNIV:: 'n set)}}"
  1.1671 +lemma finite_stdbasis: "finite {basis i ::real^'n::finite |i. i\<in> (UNIV:: 'n set)}"
  1.1672    using has_size_stdbasis[unfolded hassize_def]
  1.1673    ..
  1.1674  
  1.1675 -lemma card_stdbasis: "card {basis i ::real^'n |i. i\<in> {1 .. dimindex(UNIV :: 'n set)}} = dimindex(UNIV :: 'n set)"
  1.1676 +lemma card_stdbasis: "card {basis i ::real^'n::finite |i. i\<in> (UNIV :: 'n set)} = CARD('n)"
  1.1677    using has_size_stdbasis[unfolded hassize_def]
  1.1678    ..
  1.1679  
  1.1680  lemma independent_stdbasis_lemma:
  1.1681    assumes x: "(x::'a::semiring_1 ^ 'n) \<in> span (basis ` S)"
  1.1682 -  and i: "i \<in> {1 .. dimindex (UNIV :: 'n set)}"
  1.1683    and iS: "i \<notin> S"
  1.1684    shows "(x$i) = 0"
  1.1685  proof-
  1.1686 -  let ?n = "dimindex (UNIV :: 'n set)"
  1.1687 -  let ?U = "{1 .. ?n}"
  1.1688 +  let ?U = "UNIV :: 'n set"
  1.1689    let ?B = "basis ` S"
  1.1690    let ?P = "\<lambda>(x::'a^'n). \<forall>i\<in> ?U. i \<notin> S \<longrightarrow> x$i =0"
  1.1691   {fix x::"'a^'n" assume xS: "x\<in> ?B"
  1.1692 -   from xS have "?P x" by (auto simp add: basis_component)}
  1.1693 +   from xS have "?P x" by auto}
  1.1694   moreover
  1.1695   have "subspace ?P"
  1.1696 -   by (auto simp add: subspace_def Collect_def mem_def zero_index vector_component)
  1.1697 +   by (auto simp add: subspace_def Collect_def mem_def)
  1.1698   ultimately show ?thesis
  1.1699 -   using x span_induct[of ?B ?P x] i iS by blast
  1.1700 +   using x span_induct[of ?B ?P x] iS by blast
  1.1701  qed
  1.1702  
  1.1703 -lemma independent_stdbasis: "independent {basis i ::real^'n |i. i\<in> {1 .. dimindex(UNIV :: 'n set)}}"
  1.1704 +lemma independent_stdbasis: "independent {basis i ::real^'n::finite |i. i\<in> (UNIV :: 'n set)}"
  1.1705  proof-
  1.1706 -  let ?n = "dimindex (UNIV :: 'n set)"
  1.1707 -  let ?I = "{1 .. ?n}"
  1.1708 -  let ?b = "basis :: nat \<Rightarrow> real ^'n"
  1.1709 +  let ?I = "UNIV :: 'n set"
  1.1710 +  let ?b = "basis :: _ \<Rightarrow> real ^'n"
  1.1711    let ?B = "?b ` ?I"
  1.1712    have eq: "{?b i|i. i \<in> ?I} = ?B"
  1.1713      by auto
  1.1714 @@ -3484,8 +3403,8 @@
  1.1715        apply (rule inj_on_image_set_diff[symmetric])
  1.1716        apply (rule basis_inj) using k(1) by auto
  1.1717      from k(2) have th0: "?b k \<in> span (?b ` (?I - {k}))" unfolding eq2 .
  1.1718 -    from independent_stdbasis_lemma[OF th0 k(1), simplified]
  1.1719 -    have False by (simp add: basis_component[OF k(1), of k])}
  1.1720 +    from independent_stdbasis_lemma[OF th0, of k, simplified]
  1.1721 +    have False by simp}
  1.1722    then show ?thesis unfolding eq dependent_def ..
  1.1723  qed
  1.1724  
  1.1725 @@ -3665,19 +3584,19 @@
  1.1726      done
  1.1727  qed
  1.1728  
  1.1729 -lemma finite_Atleast_Atmost_nat[simp]: "finite {f x |x. x\<in> {(i::nat) .. j}}"
  1.1730 +lemma finite_Atleast_Atmost_nat[simp]: "finite {f x |x. x\<in> (UNIV::'a::finite set)}"
  1.1731  proof-
  1.1732 -  have eq: "{f x |x. x\<in> {i .. j}} = f ` {i .. j}" by auto
  1.1733 +  have eq: "{f x |x. x\<in> UNIV} = f ` UNIV" by auto
  1.1734    show ?thesis unfolding eq
  1.1735      apply (rule finite_imageI)
  1.1736 -    apply (rule finite_atLeastAtMost)
  1.1737 +    apply (rule finite)
  1.1738      done
  1.1739  qed
  1.1740  
  1.1741  
  1.1742  lemma independent_bound:
  1.1743 -  fixes S:: "(real^'n) set"
  1.1744 -  shows "independent S \<Longrightarrow> finite S \<and> card S <= dimindex(UNIV :: 'n set)"
  1.1745 +  fixes S:: "(real^'n::finite) set"
  1.1746 +  shows "independent S \<Longrightarrow> finite S \<and> card S <= CARD('n)"
  1.1747    apply (subst card_stdbasis[symmetric])
  1.1748    apply (rule independent_span_bound)
  1.1749    apply (rule finite_Atleast_Atmost_nat)
  1.1750 @@ -3686,23 +3605,23 @@
  1.1751    apply (rule subset_UNIV)
  1.1752    done
  1.1753  
  1.1754 -lemma dependent_biggerset: "(finite (S::(real ^'n) set) ==> card S > dimindex(UNIV:: 'n set)) ==> dependent S"
  1.1755 +lemma dependent_biggerset: "(finite (S::(real ^'n::finite) set) ==> card S > CARD('n)) ==> dependent S"
  1.1756    by (metis independent_bound not_less)
  1.1757  
  1.1758  (* Hence we can create a maximal independent subset.                         *)
  1.1759  
  1.1760  lemma maximal_independent_subset_extend:
  1.1761 -  assumes sv: "(S::(real^'n) set) \<subseteq> V" and iS: "independent S"
  1.1762 +  assumes sv: "(S::(real^'n::finite) set) \<subseteq> V" and iS: "independent S"
  1.1763    shows "\<exists>B. S \<subseteq> B \<and> B \<subseteq> V \<and> independent B \<and> V \<subseteq> span B"
  1.1764    using sv iS
  1.1765 -proof(induct d\<equiv> "dimindex (UNIV :: 'n set) - card S" arbitrary: S rule: nat_less_induct)
  1.1766 +proof(induct d\<equiv> "CARD('n) - card S" arbitrary: S rule: nat_less_induct)
  1.1767    fix n and S:: "(real^'n) set"
  1.1768 -  assume H: "\<forall>m<n. \<forall>S \<subseteq> V. independent S \<longrightarrow> m = dimindex (UNIV::'n set) - card S \<longrightarrow>
  1.1769 +  assume H: "\<forall>m<n. \<forall>S \<subseteq> V. independent S \<longrightarrow> m = CARD('n) - card S \<longrightarrow>
  1.1770                (\<exists>B. S \<subseteq> B \<and> B \<subseteq> V \<and> independent B \<and> V \<subseteq> span B)"
  1.1771 -    and sv: "S \<subseteq> V" and i: "independent S" and n: "n = dimindex (UNIV :: 'n set) - card S"
  1.1772 +    and sv: "S \<subseteq> V" and i: "independent S" and n: "n = CARD('n) - card S"
  1.1773    let ?P = "\<lambda>B. S \<subseteq> B \<and> B \<subseteq> V \<and> independent B \<and> V \<subseteq> span B"
  1.1774    let ?ths = "\<exists>x. ?P x"
  1.1775 -  let ?d = "dimindex (UNIV :: 'n set)"
  1.1776 +  let ?d = "CARD('n)"
  1.1777    {assume "V \<subseteq> span S"
  1.1778      then have ?ths  using sv i by blast }
  1.1779    moreover
  1.1780 @@ -3713,7 +3632,7 @@
  1.1781      from independent_insert[of a S]  i a
  1.1782      have th1: "independent (insert a S)" by auto
  1.1783      have mlt: "?d - card (insert a S) < n"
  1.1784 -      using aS a n independent_bound[OF th1] dimindex_ge_1[of "UNIV :: 'n set"]
  1.1785 +      using aS a n independent_bound[OF th1]
  1.1786        by auto
  1.1787  
  1.1788      from H[rule_format, OF mlt th0 th1 refl]
  1.1789 @@ -3725,14 +3644,14 @@
  1.1790  qed
  1.1791  
  1.1792  lemma maximal_independent_subset:
  1.1793 -  "\<exists>(B:: (real ^'n) set). B\<subseteq> V \<and> independent B \<and> V \<subseteq> span B"
  1.1794 +  "\<exists>(B:: (real ^'n::finite) set). B\<subseteq> V \<and> independent B \<and> V \<subseteq> span B"
  1.1795    by (metis maximal_independent_subset_extend[of "{}:: (real ^'n) set"] empty_subsetI independent_empty)
  1.1796  
  1.1797  (* Notion of dimension.                                                      *)
  1.1798  
  1.1799  definition "dim V = (SOME n. \<exists>B. B \<subseteq> V \<and> independent B \<and> V \<subseteq> span B \<and> (B hassize n))"
  1.1800  
  1.1801 -lemma basis_exists:  "\<exists>B. (B :: (real ^'n) set) \<subseteq> V \<and> independent B \<and> V \<subseteq> span B \<and> (B hassize dim V)"
  1.1802 +lemma basis_exists:  "\<exists>B. (B :: (real ^'n::finite) set) \<subseteq> V \<and> independent B \<and> V \<subseteq> span B \<and> (B hassize dim V)"
  1.1803  unfolding dim_def some_eq_ex[of "\<lambda>n. \<exists>B. B \<subseteq> V \<and> independent B \<and> V \<subseteq> span B \<and> (B hassize n)"]
  1.1804  unfolding hassize_def
  1.1805  using maximal_independent_subset[of V] independent_bound
  1.1806 @@ -3740,37 +3659,37 @@
  1.1807  
  1.1808  (* Consequences of independence or spanning for cardinality.                 *)
  1.1809  
  1.1810 -lemma independent_card_le_dim: "(B::(real ^'n) set) \<subseteq> V \<Longrightarrow> independent B \<Longrightarrow> finite B \<and> card B \<le> dim V"
  1.1811 +lemma independent_card_le_dim: "(B::(real ^'n::finite) set) \<subseteq> V \<Longrightarrow> independent B \<Longrightarrow> finite B \<and> card B \<le> dim V"
  1.1812  by (metis basis_exists[of V] independent_span_bound[where ?'a=real] hassize_def subset_trans)
  1.1813  
  1.1814 -lemma span_card_ge_dim:  "(B::(real ^'n) set) \<subseteq> V \<Longrightarrow> V \<subseteq> span B \<Longrightarrow> finite B \<Longrightarrow> dim V \<le> card B"
  1.1815 +lemma span_card_ge_dim:  "(B::(real ^'n::finite) set) \<subseteq> V \<Longrightarrow> V \<subseteq> span B \<Longrightarrow> finite B \<Longrightarrow> dim V \<le> card B"
  1.1816    by (metis basis_exists[of V] independent_span_bound hassize_def subset_trans)
  1.1817  
  1.1818  lemma basis_card_eq_dim:
  1.1819 -  "B \<subseteq> (V:: (real ^'n) set) \<Longrightarrow> V \<subseteq> span B \<Longrightarrow> independent B \<Longrightarrow> finite B \<and> card B = dim V"
  1.1820 +  "B \<subseteq> (V:: (real ^'n::finite) set) \<Longrightarrow> V \<subseteq> span B \<Longrightarrow> independent B \<Longrightarrow> finite B \<and> card B = dim V"
  1.1821    by (metis order_eq_iff independent_card_le_dim span_card_ge_dim independent_mono)
  1.1822  
  1.1823 -lemma dim_unique: "(B::(real ^'n) set) \<subseteq> V \<Longrightarrow> V \<subseteq> span B \<Longrightarrow> independent B \<Longrightarrow> B hassize n \<Longrightarrow> dim V = n"
  1.1824 +lemma dim_unique: "(B::(real ^'n::finite) set) \<subseteq> V \<Longrightarrow> V \<subseteq> span B \<Longrightarrow> independent B \<Longrightarrow> B hassize n \<Longrightarrow> dim V = n"
  1.1825    by (metis basis_card_eq_dim hassize_def)
  1.1826  
  1.1827  (* More lemmas about dimension.                                              *)
  1.1828  
  1.1829 -lemma dim_univ: "dim (UNIV :: (real^'n) set) = dimindex (UNIV :: 'n set)"
  1.1830 -  apply (rule dim_unique[of "{basis i |i. i\<in> {1 .. dimindex (UNIV :: 'n set)}}"])
  1.1831 +lemma dim_univ: "dim (UNIV :: (real^'n::finite) set) = CARD('n)"
  1.1832 +  apply (rule dim_unique[of "{basis i |i. i\<in> (UNIV :: 'n set)}"])
  1.1833    by (auto simp only: span_stdbasis has_size_stdbasis independent_stdbasis)
  1.1834  
  1.1835  lemma dim_subset:
  1.1836 -  "(S:: (real ^'n) set) \<subseteq> T \<Longrightarrow> dim S \<le> dim T"
  1.1837 +  "(S:: (real ^'n::finite) set) \<subseteq> T \<Longrightarrow> dim S \<le> dim T"
  1.1838    using basis_exists[of T] basis_exists[of S]
  1.1839    by (metis independent_span_bound[where ?'a = real and ?'n = 'n] subset_eq hassize_def)
  1.1840  
  1.1841 -lemma dim_subset_univ: "dim (S:: (real^'n) set) \<le> dimindex (UNIV :: 'n set)"
  1.1842 +lemma dim_subset_univ: "dim (S:: (real^'n::finite) set) \<le> CARD('n)"
  1.1843    by (metis dim_subset subset_UNIV dim_univ)
  1.1844  
  1.1845  (* Converses to those.                                                       *)
  1.1846  
  1.1847  lemma card_ge_dim_independent:
  1.1848 -  assumes BV:"(B::(real ^'n) set) \<subseteq> V" and iB:"independent B" and dVB:"dim V \<le> card B"
  1.1849 +  assumes BV:"(B::(real ^'n::finite) set) \<subseteq> V" and iB:"independent B" and dVB:"dim V \<le> card B"
  1.1850    shows "V \<subseteq> span B"
  1.1851  proof-
  1.1852    {fix a assume aV: "a \<in> V"
  1.1853 @@ -3784,7 +3703,7 @@
  1.1854  qed
  1.1855  
  1.1856  lemma card_le_dim_spanning:
  1.1857 -  assumes BV: "(B:: (real ^'n) set) \<subseteq> V" and VB: "V \<subseteq> span B"
  1.1858 +  assumes BV: "(B:: (real ^'n::finite) set) \<subseteq> V" and VB: "V \<subseteq> span B"
  1.1859    and fB: "finite B" and dVB: "dim V \<ge> card B"
  1.1860    shows "independent B"
  1.1861  proof-
  1.1862 @@ -3805,7 +3724,7 @@
  1.1863    then show ?thesis unfolding dependent_def by blast
  1.1864  qed
  1.1865  
  1.1866 -lemma card_eq_dim: "(B:: (real ^'n) set) \<subseteq> V \<Longrightarrow> B hassize dim V \<Longrightarrow> independent B \<longleftrightarrow> V \<subseteq> span B"
  1.1867 +lemma card_eq_dim: "(B:: (real ^'n::finite) set) \<subseteq> V \<Longrightarrow> B hassize dim V \<Longrightarrow> independent B \<longleftrightarrow> V \<subseteq> span B"
  1.1868    by (metis hassize_def order_eq_iff card_le_dim_spanning
  1.1869      card_ge_dim_independent)
  1.1870  
  1.1871 @@ -3814,13 +3733,13 @@
  1.1872  (* ------------------------------------------------------------------------- *)
  1.1873  
  1.1874  lemma independent_bound_general:
  1.1875 -  "independent (S:: (real^'n) set) \<Longrightarrow> finite S \<and> card S \<le> dim S"
  1.1876 +  "independent (S:: (real^'n::finite) set) \<Longrightarrow> finite S \<and> card S \<le> dim S"
  1.1877    by (metis independent_card_le_dim independent_bound subset_refl)
  1.1878  
  1.1879 -lemma dependent_biggerset_general: "(finite (S:: (real^'n) set) \<Longrightarrow> card S > dim S) \<Longrightarrow> dependent S"
  1.1880 +lemma dependent_biggerset_general: "(finite (S:: (real^'n::finite) set) \<Longrightarrow> card S > dim S) \<Longrightarrow> dependent S"
  1.1881    using independent_bound_general[of S] by (metis linorder_not_le)
  1.1882  
  1.1883 -lemma dim_span: "dim (span (S:: (real ^'n) set)) = dim S"
  1.1884 +lemma dim_span: "dim (span (S:: (real ^'n::finite) set)) = dim S"
  1.1885  proof-
  1.1886    have th0: "dim S \<le> dim (span S)"
  1.1887      by (auto simp add: subset_eq intro: dim_subset span_superset)
  1.1888 @@ -3833,10 +3752,10 @@
  1.1889      using fB(2)  by arith
  1.1890  qed
  1.1891  
  1.1892 -lemma subset_le_dim: "(S:: (real ^'n) set) \<subseteq> span T \<Longrightarrow> dim S \<le> dim T"
  1.1893 +lemma subset_le_dim: "(S:: (real ^'n::finite) set) \<subseteq> span T \<Longrightarrow> dim S \<le> dim T"
  1.1894    by (metis dim_span dim_subset)
  1.1895  
  1.1896 -lemma span_eq_dim: "span (S:: (real ^'n) set) = span T ==> dim S = dim T"
  1.1897 +lemma span_eq_dim: "span (S:: (real ^'n::finite) set) = span T ==> dim S = dim T"
  1.1898    by (metis dim_span)
  1.1899  
  1.1900  lemma spans_image:
  1.1901 @@ -3845,7 +3764,9 @@
  1.1902    unfolding span_linear_image[OF lf]
  1.1903    by (metis VB image_mono)
  1.1904  
  1.1905 -lemma dim_image_le: assumes lf: "linear f" shows "dim (f ` S) \<le> dim (S:: (real ^'n) set)"
  1.1906 +lemma dim_image_le:
  1.1907 +  fixes f :: "real^'n::finite \<Rightarrow> real^'m::finite"
  1.1908 +  assumes lf: "linear f" shows "dim (f ` S) \<le> dim (S:: (real ^'n::finite) set)"
  1.1909  proof-
  1.1910    from basis_exists[of S] obtain B where
  1.1911      B: "B \<subseteq> S" "independent B" "S \<subseteq> span B" "B hassize dim S" by blast
  1.1912 @@ -3889,14 +3810,14 @@
  1.1913      (* FIXME : Move to some general theory ?*)
  1.1914  definition "pairwise R S \<longleftrightarrow> (\<forall>x \<in> S. \<forall>y\<in> S. x\<noteq>y \<longrightarrow> R x y)"
  1.1915  
  1.1916 -lemma vector_sub_project_orthogonal: "(b::'a::ordered_field^'n) \<bullet> (x - ((b \<bullet> x) / (b\<bullet>b)) *s b) = 0"
  1.1917 +lemma vector_sub_project_orthogonal: "(b::'a::ordered_field^'n::finite) \<bullet> (x - ((b \<bullet> x) / (b\<bullet>b)) *s b) = 0"
  1.1918    apply (cases "b = 0", simp)
  1.1919    apply (simp add: dot_rsub dot_rmult)
  1.1920    unfolding times_divide_eq_right[symmetric]
  1.1921    by (simp add: field_simps dot_eq_0)
  1.1922  
  1.1923  lemma basis_orthogonal:
  1.1924 -  fixes B :: "(real ^'n) set"
  1.1925 +  fixes B :: "(real ^'n::finite) set"
  1.1926    assumes fB: "finite B"
  1.1927    shows "\<exists>C. finite C \<and> card C \<le> card B \<and> span C = span B \<and> pairwise orthogonal C"
  1.1928    (is " \<exists>C. ?P B C")
  1.1929 @@ -3972,7 +3893,7 @@
  1.1930  qed
  1.1931  
  1.1932  lemma orthogonal_basis_exists:
  1.1933 -  fixes V :: "(real ^'n) set"
  1.1934 +  fixes V :: "(real ^'n::finite) set"
  1.1935    shows "\<exists>B. independent B \<and> B \<subseteq> span V \<and> V \<subseteq> span B \<and> (B hassize dim V) \<and> pairwise orthogonal B"
  1.1936  proof-
  1.1937    from basis_exists[of V] obtain B where B: "B \<subseteq> V" "independent B" "V \<subseteq> span B" "B hassize dim V" by blast
  1.1938 @@ -4000,7 +3921,7 @@
  1.1939  
  1.1940  lemma span_not_univ_orthogonal:
  1.1941    assumes sU: "span S \<noteq> UNIV"
  1.1942 -  shows "\<exists>(a:: real ^'n). a \<noteq>0 \<and> (\<forall>x \<in> span S. a \<bullet> x = 0)"
  1.1943 +  shows "\<exists>(a:: real ^'n::finite). a \<noteq>0 \<and> (\<forall>x \<in> span S. a \<bullet> x = 0)"
  1.1944  proof-
  1.1945    from sU obtain a where a: "a \<notin> span S" by blast
  1.1946    from orthogonal_basis_exists obtain B where
  1.1947 @@ -4039,17 +3960,17 @@
  1.1948  qed
  1.1949  
  1.1950  lemma span_not_univ_subset_hyperplane:
  1.1951 -  assumes SU: "span S \<noteq> (UNIV ::(real^'n) set)"
  1.1952 +  assumes SU: "span S \<noteq> (UNIV ::(real^'n::finite) set)"
  1.1953    shows "\<exists> a. a \<noteq>0 \<and> span S \<subseteq> {x. a \<bullet> x = 0}"
  1.1954    using span_not_univ_orthogonal[OF SU] by auto
  1.1955  
  1.1956  lemma lowdim_subset_hyperplane:
  1.1957 -  assumes d: "dim S < dimindex (UNIV :: 'n set)"
  1.1958 -  shows "\<exists>(a::real ^'n). a  \<noteq> 0 \<and> span S \<subseteq> {x. a \<bullet> x = 0}"
  1.1959 +  assumes d: "dim S < CARD('n::finite)"
  1.1960 +  shows "\<exists>(a::real ^'n::finite). a  \<noteq> 0 \<and> span S \<subseteq> {x. a \<bullet> x = 0}"
  1.1961  proof-
  1.1962    {assume "span S = UNIV"
  1.1963      hence "dim (span S) = dim (UNIV :: (real ^'n) set)" by simp
  1.1964 -    hence "dim S = dimindex (UNIV :: 'n set)" by (simp add: dim_span dim_univ)
  1.1965 +    hence "dim S = CARD('n)" by (simp add: dim_span dim_univ)
  1.1966      with d have False by arith}
  1.1967    hence th: "span S \<noteq> UNIV" by blast
  1.1968    from span_not_univ_subset_hyperplane[OF th] show ?thesis .
  1.1969 @@ -4196,7 +4117,7 @@
  1.1970  qed
  1.1971  
  1.1972  lemma linear_independent_extend:
  1.1973 -  assumes iB: "independent (B:: (real ^'n) set)"
  1.1974 +  assumes iB: "independent (B:: (real ^'n::finite) set)"
  1.1975    shows "\<exists>g. linear g \<and> (\<forall>x\<in>B. g x = f x)"
  1.1976  proof-
  1.1977    from maximal_independent_subset_extend[of B UNIV] iB
  1.1978 @@ -4249,7 +4170,8 @@
  1.1979  qed
  1.1980  
  1.1981  lemma subspace_isomorphism:
  1.1982 -  assumes s: "subspace (S:: (real ^'n) set)" and t: "subspace T"
  1.1983 +  assumes s: "subspace (S:: (real ^'n::finite) set)"
  1.1984 +  and t: "subspace (T :: (real ^ 'm::finite) set)"
  1.1985    and d: "dim S = dim T"
  1.1986    shows "\<exists>f. linear f \<and> f ` S = T \<and> inj_on f S"
  1.1987  proof-
  1.1988 @@ -4324,12 +4246,12 @@
  1.1989  qed
  1.1990  
  1.1991  lemma linear_eq_stdbasis:
  1.1992 -  assumes lf: "linear (f::'a::ring_1^'m \<Rightarrow> 'a^'n)" and lg: "linear g"
  1.1993 -  and fg: "\<forall>i \<in> {1 .. dimindex(UNIV :: 'm set)}. f (basis i) = g(basis i)"
  1.1994 +  assumes lf: "linear (f::'a::ring_1^'m::finite \<Rightarrow> 'a^'n::finite)" and lg: "linear g"
  1.1995 +  and fg: "\<forall>i. f (basis i) = g(basis i)"
  1.1996    shows "f = g"
  1.1997  proof-
  1.1998    let ?U = "UNIV :: 'm set"
  1.1999 -  let ?I = "{basis i:: 'a^'m|i. i \<in> {1 .. dimindex ?U}}"
  1.2000 +  let ?I = "{basis i:: 'a^'m|i. i \<in> ?U}"
  1.2001    {fix x assume x: "x \<in> (UNIV :: ('a^'m) set)"
  1.2002      from equalityD2[OF span_stdbasis]
  1.2003      have IU: " (UNIV :: ('a^'m) set) \<subseteq> span ?I" by blast
  1.2004 @@ -4369,12 +4291,12 @@
  1.2005  qed
  1.2006  
  1.2007  lemma bilinear_eq_stdbasis:
  1.2008 -  assumes bf: "bilinear (f:: 'a::ring_1^'m \<Rightarrow> 'a^'n \<Rightarrow> 'a^'p)"
  1.2009 +  assumes bf: "bilinear (f:: 'a::ring_1^'m::finite \<Rightarrow> 'a^'n::finite \<Rightarrow> 'a^'p)"
  1.2010    and bg: "bilinear g"
  1.2011 -  and fg: "\<forall>i\<in> {1 .. dimindex (UNIV :: 'm set)}. \<forall>j\<in>  {1 .. dimindex (UNIV :: 'n set)}. f (basis i) (basis j) = g (basis i) (basis j)"
  1.2012 +  and fg: "\<forall>i j. f (basis i) (basis j) = g (basis i) (basis j)"
  1.2013    shows "f = g"
  1.2014  proof-
  1.2015 -  from fg have th: "\<forall>x \<in> {basis i| i. i\<in> {1 .. dimindex (UNIV :: 'm set)}}. \<forall>y\<in>  {basis j |j. j \<in> {1 .. dimindex (UNIV :: 'n set)}}. f x y = g x y" by blast
  1.2016 +  from fg have th: "\<forall>x \<in> {basis i| i. i\<in> (UNIV :: 'm set)}. \<forall>y\<in>  {basis j |j. j \<in> (UNIV :: 'n set)}. f x y = g x y" by blast
  1.2017    from bilinear_eq[OF bf bg equalityD2[OF span_stdbasis] equalityD2[OF span_stdbasis] th] show ?thesis by (blast intro: ext)
  1.2018  qed
  1.2019  
  1.2020 @@ -4389,16 +4311,14 @@
  1.2021    by (metis matrix_transp_mul transp_mat transp_transp)
  1.2022  
  1.2023  lemma linear_injective_left_inverse:
  1.2024 -  assumes lf: "linear (f::real ^'n \<Rightarrow> real ^'m)" and fi: "inj f"
  1.2025 +  assumes lf: "linear (f::real ^'n::finite \<Rightarrow> real ^'m::finite)" and fi: "inj f"
  1.2026    shows "\<exists>g. linear g \<and> g o f = id"
  1.2027  proof-
  1.2028    from linear_independent_extend[OF independent_injective_image, OF independent_stdbasis, OF lf fi]
  1.2029 -  obtain h:: "real ^'m \<Rightarrow> real ^'n" where h: "linear h" " \<forall>x \<in> f ` {basis i|i. i \<in> {1 .. dimindex (UNIV::'n set)}}. h x = inv f x" by blast
  1.2030 +  obtain h:: "real ^'m \<Rightarrow> real ^'n" where h: "linear h" " \<forall>x \<in> f ` {basis i|i. i \<in> (UNIV::'n set)}. h x = inv f x" by blast
  1.2031    from h(2)
  1.2032 -  have th: "\<forall>i\<in>{1..dimindex (UNIV::'n set)}. (h \<circ> f) (basis i) = id (basis i)"
  1.2033 +  have th: "\<forall>i. (h \<circ> f) (basis i) = id (basis i)"
  1.2034      using inv_o_cancel[OF fi, unfolded stupid_ext[symmetric] id_def o_def]
  1.2035 -    apply auto
  1.2036 -    apply (erule_tac x="basis i" in allE)
  1.2037      by auto
  1.2038  
  1.2039    from linear_eq_stdbasis[OF linear_compose[OF lf h(1)] linear_id th]
  1.2040 @@ -4407,14 +4327,14 @@
  1.2041  qed
  1.2042  
  1.2043  lemma linear_surjective_right_inverse:
  1.2044 -  assumes lf: "linear (f:: real ^'m \<Rightarrow> real ^'n)" and sf: "surj f"
  1.2045 +  assumes lf: "linear (f:: real ^'m::finite \<Rightarrow> real ^'n::finite)" and sf: "surj f"
  1.2046    shows "\<exists>g. linear g \<and> f o g = id"
  1.2047  proof-
  1.2048    from linear_independent_extend[OF independent_stdbasis]
  1.2049    obtain h:: "real ^'n \<Rightarrow> real ^'m" where
  1.2050 -    h: "linear h" "\<forall> x\<in> {basis i| i. i\<in> {1 .. dimindex (UNIV :: 'n set)}}. h x = inv f x" by blast
  1.2051 +    h: "linear h" "\<forall> x\<in> {basis i| i. i\<in> (UNIV :: 'n set)}. h x = inv f x" by blast
  1.2052    from h(2)
  1.2053 -  have th: "\<forall>i\<in>{1..dimindex (UNIV::'n set)}. (f o h) (basis i) = id (basis i)"
  1.2054 +  have th: "\<forall>i. (f o h) (basis i) = id (basis i)"
  1.2055      using sf
  1.2056      apply (auto simp add: surj_iff o_def stupid_ext[symmetric])
  1.2057      apply (erule_tac x="basis i" in allE)
  1.2058 @@ -4426,7 +4346,7 @@
  1.2059  qed
  1.2060  
  1.2061  lemma matrix_left_invertible_injective:
  1.2062 -"(\<exists>B. (B::real^'m^'n) ** (A::real^'n^'m) = mat 1) \<longleftrightarrow> (\<forall>x y. A *v x = A *v y \<longrightarrow> x = y)"
  1.2063 +"(\<exists>B. (B::real^'m^'n) ** (A::real^'n::finite^'m::finite) = mat 1) \<longleftrightarrow> (\<forall>x y. A *v x = A *v y \<longrightarrow> x = y)"
  1.2064  proof-
  1.2065    {fix B:: "real^'m^'n" and x y assume B: "B ** A = mat 1" and xy: "A *v x = A*v y"
  1.2066      from xy have "B*v (A *v x) = B *v (A*v y)" by simp
  1.2067 @@ -4445,13 +4365,13 @@
  1.2068  qed
  1.2069  
  1.2070  lemma matrix_left_invertible_ker:
  1.2071 -  "(\<exists>B. (B::real ^'m^'n) ** (A::real^'n^'m) = mat 1) \<longleftrightarrow> (\<forall>x. A *v x = 0 \<longrightarrow> x = 0)"
  1.2072 +  "(\<exists>B. (B::real ^'m::finite^'n::finite) ** (A::real^'n^'m) = mat 1) \<longleftrightarrow> (\<forall>x. A *v x = 0 \<longrightarrow> x = 0)"
  1.2073    unfolding matrix_left_invertible_injective
  1.2074    using linear_injective_0[OF matrix_vector_mul_linear, of A]
  1.2075    by (simp add: inj_on_def)
  1.2076  
  1.2077  lemma matrix_right_invertible_surjective:
  1.2078 -"(\<exists>B. (A::real^'n^'m) ** (B::real^'m^'n) = mat 1) \<longleftrightarrow> surj (\<lambda>x. A *v x)"
  1.2079 +"(\<exists>B. (A::real^'n::finite^'m::finite) ** (B::real^'m^'n) = mat 1) \<longleftrightarrow> surj (\<lambda>x. A *v x)"
  1.2080  proof-
  1.2081    {fix B :: "real ^'m^'n"  assume AB: "A ** B = mat 1"
  1.2082      {fix x :: "real ^ 'm"
  1.2083 @@ -4475,11 +4395,11 @@
  1.2084  qed
  1.2085  
  1.2086  lemma matrix_left_invertible_independent_columns:
  1.2087 -  fixes A :: "real^'n^'m"
  1.2088 -  shows "(\<exists>(B::real ^'m^'n). B ** A = mat 1) \<longleftrightarrow> (\<forall>c. setsum (\<lambda>i. c i *s column i A) {1 .. dimindex(UNIV :: 'n set)} = 0 \<longrightarrow> (\<forall>i\<in> {1 .. dimindex (UNIV :: 'n set)}. c i = 0))"
  1.2089 +  fixes A :: "real^'n::finite^'m::finite"
  1.2090 +  shows "(\<exists>(B::real ^'m^'n). B ** A = mat 1) \<longleftrightarrow> (\<forall>c. setsum (\<lambda>i. c i *s column i A) (UNIV :: 'n set) = 0 \<longrightarrow> (\<forall>i. c i = 0))"
  1.2091     (is "?lhs \<longleftrightarrow> ?rhs")
  1.2092  proof-
  1.2093 -  let ?U = "{1 .. dimindex(UNIV :: 'n set)}"
  1.2094 +  let ?U = "UNIV :: 'n set"
  1.2095    {assume k: "\<forall>x. A *v x = 0 \<longrightarrow> x = 0"
  1.2096      {fix c i assume c: "setsum (\<lambda>i. c i *s column i A) ?U = 0"
  1.2097        and i: "i \<in> ?U"
  1.2098 @@ -4487,7 +4407,7 @@
  1.2099        have th0:"A *v ?x = 0"
  1.2100  	using c
  1.2101  	unfolding matrix_mult_vsum Cart_eq
  1.2102 -	by (auto simp add: vector_component zero_index setsum_component Cart_lambda_beta)
  1.2103 +	by auto
  1.2104        from k[rule_format, OF th0] i
  1.2105        have "c i = 0" by (vector Cart_eq)}
  1.2106      hence ?rhs by blast}
  1.2107 @@ -4501,16 +4421,16 @@
  1.2108  qed
  1.2109  
  1.2110  lemma matrix_right_invertible_independent_rows:
  1.2111 -  fixes A :: "real^'n^'m"
  1.2112 -  shows "(\<exists>(B::real^'m^'n). A ** B = mat 1) \<longleftrightarrow> (\<forall>c. setsum (\<lambda>i. c i *s row i A) {1 .. dimindex(UNIV :: 'm set)} = 0 \<longrightarrow> (\<forall>i\<in> {1 .. dimindex (UNIV :: 'm set)}. c i = 0))"
  1.2113 +  fixes A :: "real^'n::finite^'m::finite"
  1.2114 +  shows "(\<exists>(B::real^'m^'n). A ** B = mat 1) \<longleftrightarrow> (\<forall>c. setsum (\<lambda>i. c i *s row i A) (UNIV :: 'm set) = 0 \<longrightarrow> (\<forall>i. c i = 0))"
  1.2115    unfolding left_invertible_transp[symmetric]
  1.2116      matrix_left_invertible_independent_columns
  1.2117    by (simp add: column_transp)
  1.2118  
  1.2119  lemma matrix_right_invertible_span_columns:
  1.2120 -  "(\<exists>(B::real ^'n^'m). (A::real ^'m^'n) ** B = mat 1) \<longleftrightarrow> span (columns A) = UNIV" (is "?lhs = ?rhs")
  1.2121 +  "(\<exists>(B::real ^'n::finite^'m::finite). (A::real ^'m^'n) ** B = mat 1) \<longleftrightarrow> span (columns A) = UNIV" (is "?lhs = ?rhs")
  1.2122  proof-
  1.2123 -  let ?U = "{1 .. dimindex (UNIV :: 'm set)}"
  1.2124 +  let ?U = "UNIV :: 'm set"
  1.2125    have fU: "finite ?U" by simp
  1.2126    have lhseq: "?lhs \<longleftrightarrow> (\<forall>y. \<exists>(x::real^'m). setsum (\<lambda>i. (x$i) *s column i A) ?U = y)"
  1.2127      unfolding matrix_right_invertible_surjective matrix_mult_vsum surj_def
  1.2128 @@ -4545,7 +4465,7 @@
  1.2129  	  x: "setsum (\<lambda>i. (x$i) *s column i A) ?U = y2" by blast
  1.2130  	let ?x = "(\<chi> j. if j = i then c + (x$i) else (x$j))::real^'m"
  1.2131  	show "?P (c*s y1 + y2)"
  1.2132 -	  proof(rule exI[where x= "?x"], vector, auto simp add: i x[symmetric]Cart_lambda_beta setsum_component cond_value_iff right_distrib cond_application_beta vector_component cong del: if_weak_cong, simp only: One_nat_def[symmetric])
  1.2133 +	  proof(rule exI[where x= "?x"], vector, auto simp add: i x[symmetric] cond_value_iff right_distrib cond_application_beta cong del: if_weak_cong)
  1.2134  	    fix j
  1.2135  	    have th: "\<forall>xa \<in> ?U. (if xa = i then (c + (x$i)) * ((column xa A)$j)
  1.2136             else (x$xa) * ((column xa A$j))) = (if xa = i then c * ((column i A)$j) else 0) + ((x$xa) * ((column xa A)$j))" using i(1)
  1.2137 @@ -4570,7 +4490,7 @@
  1.2138  qed
  1.2139  
  1.2140  lemma matrix_left_invertible_span_rows:
  1.2141 -  "(\<exists>(B::real^'m^'n). B ** (A::real^'n^'m) = mat 1) \<longleftrightarrow> span (rows A) = UNIV"
  1.2142 +  "(\<exists>(B::real^'m::finite^'n::finite). B ** (A::real^'n^'m) = mat 1) \<longleftrightarrow> span (rows A) = UNIV"
  1.2143    unfolding right_invertible_transp[symmetric]
  1.2144    unfolding columns_transp[symmetric]
  1.2145    unfolding matrix_right_invertible_span_columns
  1.2146 @@ -4579,7 +4499,7 @@
  1.2147  (* An injective map real^'n->real^'n is also surjective.                       *)
  1.2148  
  1.2149  lemma linear_injective_imp_surjective:
  1.2150 -  assumes lf: "linear (f:: real ^'n \<Rightarrow> real ^'n)" and fi: "inj f"
  1.2151 +  assumes lf: "linear (f:: real ^'n::finite \<Rightarrow> real ^'n)" and fi: "inj f"
  1.2152    shows "surj f"
  1.2153  proof-
  1.2154    let ?U = "UNIV :: (real ^'n) set"
  1.2155 @@ -4641,7 +4561,7 @@
  1.2156  qed
  1.2157  
  1.2158  lemma linear_surjective_imp_injective:
  1.2159 -  assumes lf: "linear (f::real ^'n => real ^'n)" and sf: "surj f"
  1.2160 +  assumes lf: "linear (f::real ^'n::finite => real ^'n)" and sf: "surj f"
  1.2161    shows "inj f"
  1.2162  proof-
  1.2163    let ?U = "UNIV :: (real ^'n) set"
  1.2164 @@ -4701,14 +4621,14 @@
  1.2165    by (simp add: expand_fun_eq o_def id_def)
  1.2166  
  1.2167  lemma linear_injective_isomorphism:
  1.2168 -  assumes lf: "linear (f :: real^'n \<Rightarrow> real ^'n)" and fi: "inj f"
  1.2169 +  assumes lf: "linear (f :: real^'n::finite \<Rightarrow> real ^'n)" and fi: "inj f"
  1.2170    shows "\<exists>f'. linear f' \<and> (\<forall>x. f' (f x) = x) \<and> (\<forall>x. f (f' x) = x)"
  1.2171  unfolding isomorphism_expand[symmetric]
  1.2172  using linear_surjective_right_inverse[OF lf linear_injective_imp_surjective[OF lf fi]] linear_injective_left_inverse[OF lf fi]
  1.2173  by (metis left_right_inverse_eq)
  1.2174  
  1.2175  lemma linear_surjective_isomorphism:
  1.2176 -  assumes lf: "linear (f::real ^'n \<Rightarrow> real ^'n)" and sf: "surj f"
  1.2177 +  assumes lf: "linear (f::real ^'n::finite \<Rightarrow> real ^'n)" and sf: "surj f"
  1.2178    shows "\<exists>f'. linear f' \<and> (\<forall>x. f' (f x) = x) \<and> (\<forall>x. f (f' x) = x)"
  1.2179  unfolding isomorphism_expand[symmetric]
  1.2180  using linear_surjective_right_inverse[OF lf sf] linear_injective_left_inverse[OF lf linear_surjective_imp_injective[OF lf sf]]
  1.2181 @@ -4717,7 +4637,7 @@
  1.2182  (* Left and right inverses are the same for R^N->R^N.                        *)
  1.2183  
  1.2184  lemma linear_inverse_left:
  1.2185 -  assumes lf: "linear (f::real ^'n \<Rightarrow> real ^'n)" and lf': "linear f'"
  1.2186 +  assumes lf: "linear (f::real ^'n::finite \<Rightarrow> real ^'n)" and lf': "linear f'"
  1.2187    shows "f o f' = id \<longleftrightarrow> f' o f = id"
  1.2188  proof-
  1.2189    {fix f f':: "real ^'n \<Rightarrow> real ^'n"
  1.2190 @@ -4735,7 +4655,7 @@
  1.2191  (* Moreover, a one-sided inverse is automatically linear.                    *)
  1.2192  
  1.2193  lemma left_inverse_linear:
  1.2194 -  assumes lf: "linear (f::real ^'n \<Rightarrow> real ^'n)" and gf: "g o f = id"
  1.2195 +  assumes lf: "linear (f::real ^'n::finite \<Rightarrow> real ^'n)" and gf: "g o f = id"
  1.2196    shows "linear g"
  1.2197  proof-
  1.2198    from gf have fi: "inj f" apply (auto simp add: inj_on_def o_def id_def stupid_ext[symmetric])
  1.2199 @@ -4750,7 +4670,7 @@
  1.2200  qed
  1.2201  
  1.2202  lemma right_inverse_linear:
  1.2203 -  assumes lf: "linear (f:: real ^'n \<Rightarrow> real ^'n)" and gf: "f o g = id"
  1.2204 +  assumes lf: "linear (f:: real ^'n::finite \<Rightarrow> real ^'n)" and gf: "f o g = id"
  1.2205    shows "linear g"
  1.2206  proof-
  1.2207    from gf have fi: "surj f" apply (auto simp add: surj_def o_def id_def stupid_ext[symmetric])
  1.2208 @@ -4767,7 +4687,7 @@
  1.2209  (* The same result in terms of square matrices.                              *)
  1.2210  
  1.2211  lemma matrix_left_right_inverse:
  1.2212 -  fixes A A' :: "real ^'n^'n"
  1.2213 +  fixes A A' :: "real ^'n::finite^'n"
  1.2214    shows "A ** A' = mat 1 \<longleftrightarrow> A' ** A = mat 1"
  1.2215  proof-
  1.2216    {fix A A' :: "real ^'n^'n" assume AA': "A ** A' = mat 1"
  1.2217 @@ -4796,21 +4716,20 @@
  1.2218  
  1.2219  lemma transp_columnvector:
  1.2220   "transp(columnvector v) = rowvector v"
  1.2221 -  by (simp add: transp_def rowvector_def columnvector_def Cart_eq Cart_lambda_beta)
  1.2222 +  by (simp add: transp_def rowvector_def columnvector_def Cart_eq)
  1.2223  
  1.2224  lemma transp_rowvector: "transp(rowvector v) = columnvector v"
  1.2225 -  by (simp add: transp_def columnvector_def rowvector_def Cart_eq Cart_lambda_beta)
  1.2226 +  by (simp add: transp_def columnvector_def rowvector_def Cart_eq)
  1.2227  
  1.2228  lemma dot_rowvector_columnvector:
  1.2229    "columnvector (A *v v) = A ** columnvector v"
  1.2230    by (vector columnvector_def matrix_matrix_mult_def matrix_vector_mult_def)
  1.2231  
  1.2232 -lemma dot_matrix_product: "(x::'a::semiring_1^'n) \<bullet> y = (((rowvector x ::'a^'n^1) ** (columnvector y :: 'a^1^'n))$1)$1"
  1.2233 -  apply (vector matrix_matrix_mult_def rowvector_def columnvector_def dot_def)
  1.2234 -  by (simp add: Cart_lambda_beta)
  1.2235 +lemma dot_matrix_product: "(x::'a::semiring_1^'n::finite) \<bullet> y = (((rowvector x ::'a^'n^1) ** (columnvector y :: 'a^1^'n))$1)$1"
  1.2236 +  by (vector matrix_matrix_mult_def rowvector_def columnvector_def dot_def)
  1.2237  
  1.2238  lemma dot_matrix_vector_mul:
  1.2239 -  fixes A B :: "real ^'n ^'n" and x y :: "real ^'n"
  1.2240 +  fixes A B :: "real ^'n::finite ^'n" and x y :: "real ^'n"
  1.2241    shows "(A *v x) \<bullet> (B *v y) =
  1.2242        (((rowvector x :: real^'n^1) ** ((transp A ** B) ** (columnvector y :: real ^1^'n)))$1)$1"
  1.2243  unfolding dot_matrix_product transp_columnvector[symmetric]
  1.2244 @@ -4818,30 +4737,28 @@
  1.2245  
  1.2246  (* Infinity norm.                                                            *)
  1.2247  
  1.2248 -definition "infnorm (x::real^'n) = rsup {abs(x$i) |i. i\<in> {1 .. dimindex(UNIV :: 'n set)}}"
  1.2249 -
  1.2250 -lemma numseg_dimindex_nonempty: "\<exists>i. i \<in> {1 .. dimindex (UNIV :: 'n set)}"
  1.2251 -  using dimindex_ge_1 by auto
  1.2252 +definition "infnorm (x::real^'n::finite) = rsup {abs(x$i) |i. i\<in> (UNIV :: 'n set)}"
  1.2253 +
  1.2254 +lemma numseg_dimindex_nonempty: "\<exists>i. i \<in> (UNIV :: 'n set)"
  1.2255 +  by auto
  1.2256  
  1.2257  lemma infnorm_set_image:
  1.2258 -  "{abs(x$i) |i. i\<in> {1 .. dimindex(UNIV :: 'n set)}} =
  1.2259 -  (\<lambda>i. abs(x$i)) ` {1 .. dimindex(UNIV :: 'n set)}" by blast
  1.2260 +  "{abs(x$i) |i. i\<in> (UNIV :: 'n set)} =
  1.2261 +  (\<lambda>i. abs(x$i)) ` (UNIV :: 'n set)" by blast
  1.2262  
  1.2263  lemma infnorm_set_lemma:
  1.2264 -  shows "finite {abs((x::'a::abs ^'n)$i) |i. i\<in> {1 .. dimindex(UNIV :: 'n set)}}"
  1.2265 -  and "{abs(x$i) |i. i\<in> {1 .. dimindex(UNIV :: 'n set)}} \<noteq> {}"
  1.2266 +  shows "finite {abs((x::'a::abs ^'n::finite)$i) |i. i\<in> (UNIV :: 'n set)}"
  1.2267 +  and "{abs(x$i) |i. i\<in> (UNIV :: 'n::finite set)} \<noteq> {}"
  1.2268    unfolding infnorm_set_image
  1.2269 -  using dimindex_ge_1[of "UNIV :: 'n set"]
  1.2270    by (auto intro: finite_imageI)
  1.2271  
  1.2272 -lemma infnorm_pos_le: "0 \<le> infnorm x"
  1.2273 +lemma infnorm_pos_le: "0 \<le> infnorm (x::real^'n::finite)"
  1.2274    unfolding infnorm_def
  1.2275    unfolding rsup_finite_ge_iff[ OF infnorm_set_lemma]
  1.2276    unfolding infnorm_set_image
  1.2277 -  using dimindex_ge_1
  1.2278    by auto
  1.2279  
  1.2280 -lemma infnorm_triangle: "infnorm ((x::real^'n) + y) \<le> infnorm x + infnorm y"
  1.2281 +lemma infnorm_triangle: "infnorm ((x::real^'n::finite) + y) \<le> infnorm x + infnorm y"
  1.2282  proof-
  1.2283    have th: "\<And>x y (z::real). x - y <= z \<longleftrightarrow> x - z <= y" by arith
  1.2284    have th1: "\<And>S f. f ` S = { f i| i. i \<in> S}" by blast
  1.2285 @@ -4857,12 +4774,12 @@
  1.2286    unfolding rsup_finite_ge_iff[ OF infnorm_set_lemma]
  1.2287  
  1.2288    unfolding infnorm_set_image ball_simps bex_simps
  1.2289 -  apply (simp add: vector_add_component)
  1.2290 -  apply (metis numseg_dimindex_nonempty th2)
  1.2291 +  apply simp
  1.2292 +  apply (metis th2)
  1.2293    done
  1.2294  qed
  1.2295  
  1.2296 -lemma infnorm_eq_0: "infnorm x = 0 \<longleftrightarrow> (x::real ^'n) = 0"
  1.2297 +lemma infnorm_eq_0: "infnorm x = 0 \<longleftrightarrow> (x::real ^'n::finite) = 0"
  1.2298  proof-
  1.2299    have "infnorm x <= 0 \<longleftrightarrow> x = 0"
  1.2300      unfolding infnorm_def
  1.2301 @@ -4880,9 +4797,7 @@
  1.2302    apply (rule cong[of "rsup" "rsup"])
  1.2303    apply blast
  1.2304    apply (rule set_ext)
  1.2305 -  apply (auto simp add: vector_component abs_minus_cancel)
  1.2306 -  apply (rule_tac x="i" in exI)
  1.2307 -  apply (simp add: vector_component)
  1.2308 +  apply auto
  1.2309    done
  1.2310  
  1.2311  lemma infnorm_sub: "infnorm (x - y) = infnorm (y - x)"
  1.2312 @@ -4905,16 +4820,16 @@
  1.2313  lemma real_abs_infnorm: " \<bar>infnorm x\<bar> = infnorm x"
  1.2314    using infnorm_pos_le[of x] by arith
  1.2315  
  1.2316 -lemma component_le_infnorm: assumes i: "i \<in> {1 .. dimindex (UNIV :: 'n set)}"
  1.2317 -  shows "\<bar>x$i\<bar> \<le> infnorm (x::real^'n)"
  1.2318 +lemma component_le_infnorm:
  1.2319 +  shows "\<bar>x$i\<bar> \<le> infnorm (x::real^'n::finite)"
  1.2320  proof-
  1.2321 -  let ?U = "{1 .. dimindex (UNIV :: 'n set)}"
  1.2322 +  let ?U = "UNIV :: 'n set"
  1.2323    let ?S = "{\<bar>x$i\<bar> |i. i\<in> ?U}"
  1.2324    have fS: "finite ?S" unfolding image_Collect[symmetric]
  1.2325      apply (rule finite_imageI) unfolding Collect_def mem_def by simp
  1.2326 -  have S0: "?S \<noteq> {}" using numseg_dimindex_nonempty by blast
  1.2327 +  have S0: "?S \<noteq> {}" by blast
  1.2328    have th1: "\<And>S f. f ` S = { f i| i. i \<in> S}" by blast
  1.2329 -  from rsup_finite_in[OF fS S0] rsup_finite_Ub[OF fS S0] i
  1.2330 +  from rsup_finite_in[OF fS S0] rsup_finite_Ub[OF fS S0]
  1.2331    show ?thesis unfolding infnorm_def isUb_def setle_def
  1.2332      unfolding infnorm_set_image ball_simps by auto
  1.2333  qed
  1.2334 @@ -4923,9 +4838,9 @@
  1.2335    apply (subst infnorm_def)
  1.2336    unfolding rsup_finite_le_iff[OF infnorm_set_lemma]
  1.2337    unfolding infnorm_set_image ball_simps
  1.2338 -  apply (simp add: abs_mult vector_component del: One_nat_def)
  1.2339 -  apply (rule ballI)
  1.2340 -  apply (drule component_le_infnorm[of _ x])
  1.2341 +  apply (simp add: abs_mult)
  1.2342 +  apply (rule allI)
  1.2343 +  apply (cut_tac component_le_infnorm[of x])
  1.2344    apply (rule mult_mono)
  1.2345    apply auto
  1.2346    done
  1.2347 @@ -4958,18 +4873,16 @@
  1.2348    unfolding infnorm_set_image  ball_simps
  1.2349    by (metis component_le_norm)
  1.2350  lemma card_enum: "card {1 .. n} = n" by auto
  1.2351 -lemma norm_le_infnorm: "norm(x) <= sqrt(real (dimindex(UNIV ::'n set))) * infnorm(x::real ^'n)"
  1.2352 +lemma norm_le_infnorm: "norm(x) <= sqrt(real CARD('n)) * infnorm(x::real ^'n::finite)"
  1.2353  proof-
  1.2354 -  let ?d = "dimindex(UNIV ::'n set)"
  1.2355 -  have d: "?d = card {1 .. ?d}" by auto
  1.2356 +  let ?d = "CARD('n)"
  1.2357    have "real ?d \<ge> 0" by simp
  1.2358    hence d2: "(sqrt (real ?d))^2 = real ?d"
  1.2359      by (auto intro: real_sqrt_pow2)
  1.2360    have th: "sqrt (real ?d) * infnorm x \<ge> 0"
  1.2361 -    by (simp add: dimindex_ge_1 zero_le_mult_iff real_sqrt_ge_0_iff infnorm_pos_le)
  1.2362 +    by (simp add: zero_le_mult_iff real_sqrt_ge_0_iff infnorm_pos_le)
  1.2363    have th1: "x\<bullet>x \<le> (sqrt (real ?d) * infnorm x)^2"
  1.2364      unfolding power_mult_distrib d2
  1.2365 -    apply (subst d)
  1.2366      apply (subst power2_abs[symmetric])
  1.2367      unfolding real_of_nat_def dot_def power2_eq_square[symmetric]
  1.2368      apply (subst power2_abs[symmetric])
  1.2369 @@ -4986,7 +4899,7 @@
  1.2370  
  1.2371  (* Equality in Cauchy-Schwarz and triangle inequalities.                     *)
  1.2372  
  1.2373 -lemma norm_cauchy_schwarz_eq: "(x::real ^'n) \<bullet> y = norm x * norm y \<longleftrightarrow> norm x *s y = norm y *s x" (is "?lhs \<longleftrightarrow> ?rhs")
  1.2374 +lemma norm_cauchy_schwarz_eq: "(x::real ^'n::finite) \<bullet> y = norm x * norm y \<longleftrightarrow> norm x *s y = norm y *s x" (is "?lhs \<longleftrightarrow> ?rhs")
  1.2375  proof-
  1.2376    {assume h: "x = 0"
  1.2377      hence ?thesis by simp}
  1.2378 @@ -5012,7 +4925,9 @@
  1.2379    ultimately show ?thesis by blast
  1.2380  qed
  1.2381  
  1.2382 -lemma norm_cauchy_schwarz_abs_eq: "abs(x \<bullet> y) = norm x * norm y \<longleftrightarrow>
  1.2383 +lemma norm_cauchy_schwarz_abs_eq:
  1.2384 +  fixes x y :: "real ^ 'n::finite"
  1.2385 +  shows "abs(x \<bullet> y) = norm x * norm y \<longleftrightarrow>
  1.2386                  norm x *s y = norm y *s x \<or> norm(x) *s y = - norm y *s x" (is "?lhs \<longleftrightarrow> ?rhs")
  1.2387  proof-
  1.2388    have th: "\<And>(x::real) a. a \<ge> 0 \<Longrightarrow> abs x = a \<longleftrightarrow> x = a \<or> x = - a" by arith
  1.2389 @@ -5029,7 +4944,9 @@
  1.2390    finally show ?thesis ..
  1.2391  qed
  1.2392  
  1.2393 -lemma norm_triangle_eq: "norm(x + y) = norm x + norm y \<longleftrightarrow> norm x *s y = norm y *s x"
  1.2394 +lemma norm_triangle_eq:
  1.2395 +  fixes x y :: "real ^ 'n::finite"
  1.2396 +  shows "norm(x + y) = norm x + norm y \<longleftrightarrow> norm x *s y = norm y *s x"
  1.2397  proof-
  1.2398    {assume x: "x =0 \<or> y =0"
  1.2399      hence ?thesis by (cases "x=0", simp_all)}
  1.2400 @@ -5107,7 +5024,9 @@
  1.2401    ultimately show ?thesis by blast
  1.2402  qed
  1.2403  
  1.2404 -lemma norm_cauchy_schwarz_equal: "abs(x \<bullet> y) = norm x * norm y \<longleftrightarrow> collinear {(0::real^'n),x,y}"
  1.2405 +lemma norm_cauchy_schwarz_equal:
  1.2406 +  fixes x y :: "real ^ 'n::finite"
  1.2407 +  shows "abs(x \<bullet> y) = norm x * norm y \<longleftrightarrow> collinear {(0::real^'n),x,y}"
  1.2408  unfolding norm_cauchy_schwarz_abs_eq
  1.2409  apply (cases "x=0", simp_all add: collinear_2)
  1.2410  apply (cases "y=0", simp_all add: collinear_2 insert_commute)