src/HOL/Multivariate_Analysis/Finite_Cartesian_Product.thy
author huffman
Thu Aug 11 09:11:15 2011 -0700 (2011-08-11)
changeset 44165 d26a45f3c835
parent 44142 8e27e0177518
child 44166 d12d89a66742
permissions -rw-r--r--
remove lemma stupid_ext
     1 (*  Title:      HOL/Multivariate_Analysis/Finite_Cartesian_Product.thy
     2     Author:     Amine Chaieb, University of Cambridge
     3 *)
     4 
     5 header {* Definition of finite Cartesian product types. *}
     6 
     7 theory Finite_Cartesian_Product
     8 imports
     9   Euclidean_Space
    10   L2_Norm
    11   "~~/src/HOL/Library/Numeral_Type"
    12 begin
    13 
    14 subsection {* Finite Cartesian products, with indexing and lambdas. *}
    15 
    16 typedef (open)
    17   ('a, 'b) vec = "UNIV :: (('b::finite) \<Rightarrow> 'a) set"
    18   morphisms vec_nth vec_lambda ..
    19 
    20 notation
    21   vec_nth (infixl "$" 90) and
    22   vec_lambda (binder "\<chi>" 10)
    23 
    24 (*
    25   Translate "'b ^ 'n" into "'b ^ ('n :: finite)". When 'n has already more than
    26   the finite type class write "vec 'b 'n"
    27 *)
    28 
    29 syntax "_finite_vec" :: "type \<Rightarrow> type \<Rightarrow> type" ("(_ ^/ _)" [15, 16] 15)
    30 
    31 parse_translation {*
    32 let
    33   fun vec t u = Syntax.const @{type_syntax vec} $ t $ u;
    34   fun finite_vec_tr [t, u as Free (x, _)] =
    35         if Lexicon.is_tid x then
    36           vec t (Syntax.const @{syntax_const "_ofsort"} $ u $ Syntax.const @{class_syntax finite})
    37         else vec t u
    38     | finite_vec_tr [t, u] = vec t u
    39 in
    40   [(@{syntax_const "_finite_vec"}, finite_vec_tr)]
    41 end
    42 *}
    43 
    44 lemma vec_eq_iff: "(x = y) \<longleftrightarrow> (\<forall>i. x$i = y$i)"
    45   by (simp add: vec_nth_inject [symmetric] fun_eq_iff)
    46 
    47 lemma vec_lambda_beta [simp]: "vec_lambda g $ i = g i"
    48   by (simp add: vec_lambda_inverse)
    49 
    50 lemma vec_lambda_unique: "(\<forall>i. f$i = g i) \<longleftrightarrow> vec_lambda g = f"
    51   by (auto simp add: vec_eq_iff)
    52 
    53 lemma vec_lambda_eta: "(\<chi> i. (g$i)) = g"
    54   by (simp add: vec_eq_iff)
    55 
    56 
    57 subsection {* Group operations and class instances *}
    58 
    59 instantiation vec :: (zero, finite) zero
    60 begin
    61   definition "0 \<equiv> (\<chi> i. 0)"
    62   instance ..
    63 end
    64 
    65 instantiation vec :: (plus, finite) plus
    66 begin
    67   definition "op + \<equiv> (\<lambda> x y. (\<chi> i. x$i + y$i))"
    68   instance ..
    69 end
    70 
    71 instantiation vec :: (minus, finite) minus
    72 begin
    73   definition "op - \<equiv> (\<lambda> x y. (\<chi> i. x$i - y$i))"
    74   instance ..
    75 end
    76 
    77 instantiation vec :: (uminus, finite) uminus
    78 begin
    79   definition "uminus \<equiv> (\<lambda> x. (\<chi> i. - (x$i)))"
    80   instance ..
    81 end
    82 
    83 lemma zero_index [simp]: "0 $ i = 0"
    84   unfolding zero_vec_def by simp
    85 
    86 lemma vector_add_component [simp]: "(x + y)$i = x$i + y$i"
    87   unfolding plus_vec_def by simp
    88 
    89 lemma vector_minus_component [simp]: "(x - y)$i = x$i - y$i"
    90   unfolding minus_vec_def by simp
    91 
    92 lemma vector_uminus_component [simp]: "(- x)$i = - (x$i)"
    93   unfolding uminus_vec_def by simp
    94 
    95 instance vec :: (semigroup_add, finite) semigroup_add
    96   by default (simp add: vec_eq_iff add_assoc)
    97 
    98 instance vec :: (ab_semigroup_add, finite) ab_semigroup_add
    99   by default (simp add: vec_eq_iff add_commute)
   100 
   101 instance vec :: (monoid_add, finite) monoid_add
   102   by default (simp_all add: vec_eq_iff)
   103 
   104 instance vec :: (comm_monoid_add, finite) comm_monoid_add
   105   by default (simp add: vec_eq_iff)
   106 
   107 instance vec :: (cancel_semigroup_add, finite) cancel_semigroup_add
   108   by default (simp_all add: vec_eq_iff)
   109 
   110 instance vec :: (cancel_ab_semigroup_add, finite) cancel_ab_semigroup_add
   111   by default (simp add: vec_eq_iff)
   112 
   113 instance vec :: (cancel_comm_monoid_add, finite) cancel_comm_monoid_add ..
   114 
   115 instance vec :: (group_add, finite) group_add
   116   by default (simp_all add: vec_eq_iff diff_minus)
   117 
   118 instance vec :: (ab_group_add, finite) ab_group_add
   119   by default (simp_all add: vec_eq_iff)
   120 
   121 
   122 subsection {* Real vector space *}
   123 
   124 instantiation vec :: (real_vector, finite) real_vector
   125 begin
   126 
   127 definition "scaleR \<equiv> (\<lambda> r x. (\<chi> i. scaleR r (x$i)))"
   128 
   129 lemma vector_scaleR_component [simp]: "(scaleR r x)$i = scaleR r (x$i)"
   130   unfolding scaleR_vec_def by simp
   131 
   132 instance
   133   by default (simp_all add: vec_eq_iff scaleR_left_distrib scaleR_right_distrib)
   134 
   135 end
   136 
   137 
   138 subsection {* Topological space *}
   139 
   140 instantiation vec :: (topological_space, finite) topological_space
   141 begin
   142 
   143 definition
   144   "open (S :: ('a ^ 'b) set) \<longleftrightarrow>
   145     (\<forall>x\<in>S. \<exists>A. (\<forall>i. open (A i) \<and> x$i \<in> A i) \<and>
   146       (\<forall>y. (\<forall>i. y$i \<in> A i) \<longrightarrow> y \<in> S))"
   147 
   148 instance proof
   149   show "open (UNIV :: ('a ^ 'b) set)"
   150     unfolding open_vec_def by auto
   151 next
   152   fix S T :: "('a ^ 'b) set"
   153   assume "open S" "open T" thus "open (S \<inter> T)"
   154     unfolding open_vec_def
   155     apply clarify
   156     apply (drule (1) bspec)+
   157     apply (clarify, rename_tac Sa Ta)
   158     apply (rule_tac x="\<lambda>i. Sa i \<inter> Ta i" in exI)
   159     apply (simp add: open_Int)
   160     done
   161 next
   162   fix K :: "('a ^ 'b) set set"
   163   assume "\<forall>S\<in>K. open S" thus "open (\<Union>K)"
   164     unfolding open_vec_def
   165     apply clarify
   166     apply (drule (1) bspec)
   167     apply (drule (1) bspec)
   168     apply clarify
   169     apply (rule_tac x=A in exI)
   170     apply fast
   171     done
   172 qed
   173 
   174 end
   175 
   176 lemma open_vector_box: "\<forall>i. open (S i) \<Longrightarrow> open {x. \<forall>i. x $ i \<in> S i}"
   177   unfolding open_vec_def by auto
   178 
   179 lemma open_vimage_vec_nth: "open S \<Longrightarrow> open ((\<lambda>x. x $ i) -` S)"
   180   unfolding open_vec_def
   181   apply clarify
   182   apply (rule_tac x="\<lambda>k. if k = i then S else UNIV" in exI, simp)
   183   done
   184 
   185 lemma closed_vimage_vec_nth: "closed S \<Longrightarrow> closed ((\<lambda>x. x $ i) -` S)"
   186   unfolding closed_open vimage_Compl [symmetric]
   187   by (rule open_vimage_vec_nth)
   188 
   189 lemma closed_vector_box: "\<forall>i. closed (S i) \<Longrightarrow> closed {x. \<forall>i. x $ i \<in> S i}"
   190 proof -
   191   have "{x. \<forall>i. x $ i \<in> S i} = (\<Inter>i. (\<lambda>x. x $ i) -` S i)" by auto
   192   thus "\<forall>i. closed (S i) \<Longrightarrow> closed {x. \<forall>i. x $ i \<in> S i}"
   193     by (simp add: closed_INT closed_vimage_vec_nth)
   194 qed
   195 
   196 lemma tendsto_vec_nth [tendsto_intros]:
   197   assumes "((\<lambda>x. f x) ---> a) net"
   198   shows "((\<lambda>x. f x $ i) ---> a $ i) net"
   199 proof (rule topological_tendstoI)
   200   fix S assume "open S" "a $ i \<in> S"
   201   then have "open ((\<lambda>y. y $ i) -` S)" "a \<in> ((\<lambda>y. y $ i) -` S)"
   202     by (simp_all add: open_vimage_vec_nth)
   203   with assms have "eventually (\<lambda>x. f x \<in> (\<lambda>y. y $ i) -` S) net"
   204     by (rule topological_tendstoD)
   205   then show "eventually (\<lambda>x. f x $ i \<in> S) net"
   206     by simp
   207 qed
   208 
   209 lemma eventually_Ball_finite: (* TODO: move *)
   210   assumes "finite A" and "\<forall>y\<in>A. eventually (\<lambda>x. P x y) net"
   211   shows "eventually (\<lambda>x. \<forall>y\<in>A. P x y) net"
   212 using assms by (induct set: finite, simp, simp add: eventually_conj)
   213 
   214 lemma eventually_all_finite: (* TODO: move *)
   215   fixes P :: "'a \<Rightarrow> 'b::finite \<Rightarrow> bool"
   216   assumes "\<And>y. eventually (\<lambda>x. P x y) net"
   217   shows "eventually (\<lambda>x. \<forall>y. P x y) net"
   218 using eventually_Ball_finite [of UNIV P] assms by simp
   219 
   220 lemma vec_tendstoI:
   221   assumes "\<And>i. ((\<lambda>x. f x $ i) ---> a $ i) net"
   222   shows "((\<lambda>x. f x) ---> a) net"
   223 proof (rule topological_tendstoI)
   224   fix S assume "open S" and "a \<in> S"
   225   then obtain A where A: "\<And>i. open (A i)" "\<And>i. a $ i \<in> A i"
   226     and S: "\<And>y. \<forall>i. y $ i \<in> A i \<Longrightarrow> y \<in> S"
   227     unfolding open_vec_def by metis
   228   have "\<And>i. eventually (\<lambda>x. f x $ i \<in> A i) net"
   229     using assms A by (rule topological_tendstoD)
   230   hence "eventually (\<lambda>x. \<forall>i. f x $ i \<in> A i) net"
   231     by (rule eventually_all_finite)
   232   thus "eventually (\<lambda>x. f x \<in> S) net"
   233     by (rule eventually_elim1, simp add: S)
   234 qed
   235 
   236 lemma tendsto_vec_lambda [tendsto_intros]:
   237   assumes "\<And>i. ((\<lambda>x. f x i) ---> a i) net"
   238   shows "((\<lambda>x. \<chi> i. f x i) ---> (\<chi> i. a i)) net"
   239   using assms by (simp add: vec_tendstoI)
   240 
   241 
   242 subsection {* Metric *}
   243 
   244 (* TODO: move somewhere else *)
   245 lemma finite_choice: "finite A \<Longrightarrow> \<forall>x\<in>A. \<exists>y. P x y \<Longrightarrow> \<exists>f. \<forall>x\<in>A. P x (f x)"
   246 apply (induct set: finite, simp_all)
   247 apply (clarify, rename_tac y)
   248 apply (rule_tac x="f(x:=y)" in exI, simp)
   249 done
   250 
   251 instantiation vec :: (metric_space, finite) metric_space
   252 begin
   253 
   254 definition
   255   "dist x y = setL2 (\<lambda>i. dist (x$i) (y$i)) UNIV"
   256 
   257 lemma dist_vec_nth_le: "dist (x $ i) (y $ i) \<le> dist x y"
   258   unfolding dist_vec_def by (rule member_le_setL2) simp_all
   259 
   260 instance proof
   261   fix x y :: "'a ^ 'b"
   262   show "dist x y = 0 \<longleftrightarrow> x = y"
   263     unfolding dist_vec_def
   264     by (simp add: setL2_eq_0_iff vec_eq_iff)
   265 next
   266   fix x y z :: "'a ^ 'b"
   267   show "dist x y \<le> dist x z + dist y z"
   268     unfolding dist_vec_def
   269     apply (rule order_trans [OF _ setL2_triangle_ineq])
   270     apply (simp add: setL2_mono dist_triangle2)
   271     done
   272 next
   273   (* FIXME: long proof! *)
   274   fix S :: "('a ^ 'b) set"
   275   show "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S)"
   276     unfolding open_vec_def open_dist
   277     apply safe
   278      apply (drule (1) bspec)
   279      apply clarify
   280      apply (subgoal_tac "\<exists>e>0. \<forall>i y. dist y (x$i) < e \<longrightarrow> y \<in> A i")
   281       apply clarify
   282       apply (rule_tac x=e in exI, clarify)
   283       apply (drule spec, erule mp, clarify)
   284       apply (drule spec, drule spec, erule mp)
   285       apply (erule le_less_trans [OF dist_vec_nth_le])
   286      apply (subgoal_tac "\<forall>i\<in>UNIV. \<exists>e>0. \<forall>y. dist y (x$i) < e \<longrightarrow> y \<in> A i")
   287       apply (drule finite_choice [OF finite], clarify)
   288       apply (rule_tac x="Min (range f)" in exI, simp)
   289      apply clarify
   290      apply (drule_tac x=i in spec, clarify)
   291      apply (erule (1) bspec)
   292     apply (drule (1) bspec, clarify)
   293     apply (subgoal_tac "\<exists>r. (\<forall>i::'b. 0 < r i) \<and> e = setL2 r UNIV")
   294      apply clarify
   295      apply (rule_tac x="\<lambda>i. {y. dist y (x$i) < r i}" in exI)
   296      apply (rule conjI)
   297       apply clarify
   298       apply (rule conjI)
   299        apply (clarify, rename_tac y)
   300        apply (rule_tac x="r i - dist y (x$i)" in exI, rule conjI, simp)
   301        apply clarify
   302        apply (simp only: less_diff_eq)
   303        apply (erule le_less_trans [OF dist_triangle])
   304       apply simp
   305      apply clarify
   306      apply (drule spec, erule mp)
   307      apply (simp add: dist_vec_def setL2_strict_mono)
   308     apply (rule_tac x="\<lambda>i. e / sqrt (of_nat CARD('b))" in exI)
   309     apply (simp add: divide_pos_pos setL2_constant)
   310     done
   311 qed
   312 
   313 end
   314 
   315 lemma Cauchy_vec_nth:
   316   "Cauchy (\<lambda>n. X n) \<Longrightarrow> Cauchy (\<lambda>n. X n $ i)"
   317   unfolding Cauchy_def by (fast intro: le_less_trans [OF dist_vec_nth_le])
   318 
   319 lemma vec_CauchyI:
   320   fixes X :: "nat \<Rightarrow> 'a::metric_space ^ 'n"
   321   assumes X: "\<And>i. Cauchy (\<lambda>n. X n $ i)"
   322   shows "Cauchy (\<lambda>n. X n)"
   323 proof (rule metric_CauchyI)
   324   fix r :: real assume "0 < r"
   325   then have "0 < r / of_nat CARD('n)" (is "0 < ?s")
   326     by (simp add: divide_pos_pos)
   327   def N \<equiv> "\<lambda>i. LEAST N. \<forall>m\<ge>N. \<forall>n\<ge>N. dist (X m $ i) (X n $ i) < ?s"
   328   def M \<equiv> "Max (range N)"
   329   have "\<And>i. \<exists>N. \<forall>m\<ge>N. \<forall>n\<ge>N. dist (X m $ i) (X n $ i) < ?s"
   330     using X `0 < ?s` by (rule metric_CauchyD)
   331   hence "\<And>i. \<forall>m\<ge>N i. \<forall>n\<ge>N i. dist (X m $ i) (X n $ i) < ?s"
   332     unfolding N_def by (rule LeastI_ex)
   333   hence M: "\<And>i. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m $ i) (X n $ i) < ?s"
   334     unfolding M_def by simp
   335   {
   336     fix m n :: nat
   337     assume "M \<le> m" "M \<le> n"
   338     have "dist (X m) (X n) = setL2 (\<lambda>i. dist (X m $ i) (X n $ i)) UNIV"
   339       unfolding dist_vec_def ..
   340     also have "\<dots> \<le> setsum (\<lambda>i. dist (X m $ i) (X n $ i)) UNIV"
   341       by (rule setL2_le_setsum [OF zero_le_dist])
   342     also have "\<dots> < setsum (\<lambda>i::'n. ?s) UNIV"
   343       by (rule setsum_strict_mono, simp_all add: M `M \<le> m` `M \<le> n`)
   344     also have "\<dots> = r"
   345       by simp
   346     finally have "dist (X m) (X n) < r" .
   347   }
   348   hence "\<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < r"
   349     by simp
   350   then show "\<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < r" ..
   351 qed
   352 
   353 instance vec :: (complete_space, finite) complete_space
   354 proof
   355   fix X :: "nat \<Rightarrow> 'a ^ 'b" assume "Cauchy X"
   356   have "\<And>i. (\<lambda>n. X n $ i) ----> lim (\<lambda>n. X n $ i)"
   357     using Cauchy_vec_nth [OF `Cauchy X`]
   358     by (simp add: Cauchy_convergent_iff convergent_LIMSEQ_iff)
   359   hence "X ----> vec_lambda (\<lambda>i. lim (\<lambda>n. X n $ i))"
   360     by (simp add: vec_tendstoI)
   361   then show "convergent X"
   362     by (rule convergentI)
   363 qed
   364 
   365 
   366 subsection {* Normed vector space *}
   367 
   368 instantiation vec :: (real_normed_vector, finite) real_normed_vector
   369 begin
   370 
   371 definition "norm x = setL2 (\<lambda>i. norm (x$i)) UNIV"
   372 
   373 definition "sgn (x::'a^'b) = scaleR (inverse (norm x)) x"
   374 
   375 instance proof
   376   fix a :: real and x y :: "'a ^ 'b"
   377   show "0 \<le> norm x"
   378     unfolding norm_vec_def
   379     by (rule setL2_nonneg)
   380   show "norm x = 0 \<longleftrightarrow> x = 0"
   381     unfolding norm_vec_def
   382     by (simp add: setL2_eq_0_iff vec_eq_iff)
   383   show "norm (x + y) \<le> norm x + norm y"
   384     unfolding norm_vec_def
   385     apply (rule order_trans [OF _ setL2_triangle_ineq])
   386     apply (simp add: setL2_mono norm_triangle_ineq)
   387     done
   388   show "norm (scaleR a x) = \<bar>a\<bar> * norm x"
   389     unfolding norm_vec_def
   390     by (simp add: setL2_right_distrib)
   391   show "sgn x = scaleR (inverse (norm x)) x"
   392     by (rule sgn_vec_def)
   393   show "dist x y = norm (x - y)"
   394     unfolding dist_vec_def norm_vec_def
   395     by (simp add: dist_norm)
   396 qed
   397 
   398 end
   399 
   400 lemma norm_nth_le: "norm (x $ i) \<le> norm x"
   401 unfolding norm_vec_def
   402 by (rule member_le_setL2) simp_all
   403 
   404 interpretation vec_nth: bounded_linear "\<lambda>x. x $ i"
   405 apply default
   406 apply (rule vector_add_component)
   407 apply (rule vector_scaleR_component)
   408 apply (rule_tac x="1" in exI, simp add: norm_nth_le)
   409 done
   410 
   411 instance vec :: (banach, finite) banach ..
   412 
   413 
   414 subsection {* Inner product space *}
   415 
   416 instantiation vec :: (real_inner, finite) real_inner
   417 begin
   418 
   419 definition "inner x y = setsum (\<lambda>i. inner (x$i) (y$i)) UNIV"
   420 
   421 instance proof
   422   fix r :: real and x y z :: "'a ^ 'b"
   423   show "inner x y = inner y x"
   424     unfolding inner_vec_def
   425     by (simp add: inner_commute)
   426   show "inner (x + y) z = inner x z + inner y z"
   427     unfolding inner_vec_def
   428     by (simp add: inner_add_left setsum_addf)
   429   show "inner (scaleR r x) y = r * inner x y"
   430     unfolding inner_vec_def
   431     by (simp add: setsum_right_distrib)
   432   show "0 \<le> inner x x"
   433     unfolding inner_vec_def
   434     by (simp add: setsum_nonneg)
   435   show "inner x x = 0 \<longleftrightarrow> x = 0"
   436     unfolding inner_vec_def
   437     by (simp add: vec_eq_iff setsum_nonneg_eq_0_iff)
   438   show "norm x = sqrt (inner x x)"
   439     unfolding inner_vec_def norm_vec_def setL2_def
   440     by (simp add: power2_norm_eq_inner)
   441 qed
   442 
   443 end
   444 
   445 subsection {* Euclidean space *}
   446 
   447 text {* A bijection between @{text "'n::finite"} and @{text "{..<CARD('n)}"} *}
   448 
   449 definition vec_bij_nat :: "nat \<Rightarrow> ('n::finite)" where
   450   "vec_bij_nat = (SOME p. bij_betw p {..<CARD('n)} (UNIV::'n set) )"
   451 
   452 abbreviation "\<pi> \<equiv> vec_bij_nat"
   453 definition "\<pi>' = inv_into {..<CARD('n)} (\<pi>::nat \<Rightarrow> ('n::finite))"
   454 
   455 lemma bij_betw_pi:
   456   "bij_betw \<pi> {..<CARD('n::finite)} (UNIV::('n::finite) set)"
   457   using ex_bij_betw_nat_finite[of "UNIV::'n set"]
   458   by (auto simp: vec_bij_nat_def atLeast0LessThan
   459     intro!: someI_ex[of "\<lambda>x. bij_betw x {..<CARD('n)} (UNIV::'n set)"])
   460 
   461 lemma bij_betw_pi'[intro]: "bij_betw \<pi>' (UNIV::'n set) {..<CARD('n::finite)}"
   462   using bij_betw_inv_into[OF bij_betw_pi] unfolding \<pi>'_def by auto
   463 
   464 lemma pi'_inj[intro]: "inj \<pi>'"
   465   using bij_betw_pi' unfolding bij_betw_def by auto
   466 
   467 lemma pi'_range[intro]: "\<And>i::'n. \<pi>' i < CARD('n::finite)"
   468   using bij_betw_pi' unfolding bij_betw_def by auto
   469 
   470 lemma \<pi>\<pi>'[simp]: "\<And>i::'n::finite. \<pi> (\<pi>' i) = i"
   471   using bij_betw_pi by (auto intro!: f_inv_into_f simp: \<pi>'_def bij_betw_def)
   472 
   473 lemma \<pi>'\<pi>[simp]: "\<And>i. i\<in>{..<CARD('n::finite)} \<Longrightarrow> \<pi>' (\<pi> i::'n) = i"
   474   using bij_betw_pi by (auto intro!: inv_into_f_eq simp: \<pi>'_def bij_betw_def)
   475 
   476 lemma \<pi>\<pi>'_alt[simp]: "\<And>i. i<CARD('n::finite) \<Longrightarrow> \<pi>' (\<pi> i::'n) = i"
   477   by auto
   478 
   479 lemma \<pi>_inj_on: "inj_on (\<pi>::nat\<Rightarrow>'n::finite) {..<CARD('n)}"
   480   using bij_betw_pi[where 'n='n] by (simp add: bij_betw_def)
   481 
   482 instantiation vec :: (euclidean_space, finite) euclidean_space
   483 begin
   484 
   485 definition "dimension (t :: ('a ^ 'b) itself) = CARD('b) * DIM('a)"
   486 
   487 definition "(basis i::'a^'b) =
   488   (if i < (CARD('b) * DIM('a))
   489   then (\<chi> j::'b. if j = \<pi>(i div DIM('a)) then basis (i mod DIM('a)) else 0)
   490   else 0)"
   491 
   492 lemma basis_eq:
   493   assumes "i < CARD('b)" and "j < DIM('a)"
   494   shows "basis (j + i * DIM('a)) = (\<chi> k. if k = \<pi> i then basis j else 0)"
   495 proof -
   496   have "j + i * DIM('a) <  DIM('a) * (i + 1)" using assms by (auto simp: field_simps)
   497   also have "\<dots> \<le> DIM('a) * CARD('b)" using assms unfolding mult_le_cancel1 by auto
   498   finally show ?thesis
   499     unfolding basis_vec_def using assms by (auto simp: vec_eq_iff not_less field_simps)
   500 qed
   501 
   502 lemma basis_eq_pi':
   503   assumes "j < DIM('a)"
   504   shows "basis (j + \<pi>' i * DIM('a)) $ k = (if k = i then basis j else 0)"
   505   apply (subst basis_eq)
   506   using pi'_range assms by simp_all
   507 
   508 lemma split_times_into_modulo[consumes 1]:
   509   fixes k :: nat
   510   assumes "k < A * B"
   511   obtains i j where "i < A" and "j < B" and "k = j + i * B"
   512 proof
   513   have "A * B \<noteq> 0"
   514   proof assume "A * B = 0" with assms show False by simp qed
   515   hence "0 < B" by auto
   516   thus "k mod B < B" using `0 < B` by auto
   517 next
   518   have "k div B * B \<le> k div B * B + k mod B" by (rule le_add1)
   519   also have "... < A * B" using assms by simp
   520   finally show "k div B < A" by auto
   521 qed simp
   522 
   523 lemma split_CARD_DIM[consumes 1]:
   524   fixes k :: nat
   525   assumes k: "k < CARD('b) * DIM('a)"
   526   obtains i and j::'b where "i < DIM('a)" "k = i + \<pi>' j * DIM('a)"
   527 proof -
   528   from split_times_into_modulo[OF k] guess i j . note ij = this
   529   show thesis
   530   proof
   531     show "j < DIM('a)" using ij by simp
   532     show "k = j + \<pi>' (\<pi> i :: 'b) * DIM('a)"
   533       using ij by simp
   534   qed
   535 qed
   536 
   537 lemma linear_less_than_times:
   538   fixes i j A B :: nat assumes "i < B" "j < A"
   539   shows "j + i * A < B * A"
   540 proof -
   541   have "i * A + j < (Suc i)*A" using `j < A` by simp
   542   also have "\<dots> \<le> B * A" using `i < B` unfolding mult_le_cancel2 by simp
   543   finally show ?thesis by simp
   544 qed
   545 
   546 lemma DIM_cart[simp]: "DIM('a^'b) = CARD('b) * DIM('a)"
   547   by (rule dimension_vec_def)
   548 
   549 lemma all_less_DIM_cart:
   550   fixes m n :: nat
   551   shows "(\<forall>i<DIM('a^'b). P i) \<longleftrightarrow> (\<forall>x::'b. \<forall>i<DIM('a). P (i + \<pi>' x * DIM('a)))"
   552 unfolding DIM_cart
   553 apply safe
   554 apply (drule spec, erule mp, erule linear_less_than_times [OF pi'_range])
   555 apply (erule split_CARD_DIM, simp)
   556 done
   557 
   558 lemma eq_pi_iff:
   559   fixes x :: "'c::finite"
   560   shows "i < CARD('c::finite) \<Longrightarrow> x = \<pi> i \<longleftrightarrow> \<pi>' x = i"
   561   by auto
   562 
   563 lemma all_less_mult:
   564   fixes m n :: nat
   565   shows "(\<forall>i<(m * n). P i) \<longleftrightarrow> (\<forall>i<m. \<forall>j<n. P (j + i * n))"
   566 apply safe
   567 apply (drule spec, erule mp, erule (1) linear_less_than_times)
   568 apply (erule split_times_into_modulo, simp)
   569 done
   570 
   571 lemma inner_if:
   572   "inner (if a then x else y) z = (if a then inner x z else inner y z)"
   573   "inner x (if a then y else z) = (if a then inner x y else inner x z)"
   574   by simp_all
   575 
   576 instance proof
   577   show "0 < DIM('a ^ 'b)"
   578     unfolding dimension_vec_def
   579     by (intro mult_pos_pos zero_less_card_finite DIM_positive)
   580 next
   581   fix i :: nat
   582   assume "DIM('a ^ 'b) \<le> i" thus "basis i = (0::'a^'b)"
   583     unfolding dimension_vec_def basis_vec_def
   584     by simp
   585 next
   586   show "\<forall>i<DIM('a ^ 'b). \<forall>j<DIM('a ^ 'b).
   587     inner (basis i :: 'a ^ 'b) (basis j) = (if i = j then 1 else 0)"
   588     apply (simp add: inner_vec_def)
   589     apply safe
   590     apply (erule split_CARD_DIM, simp add: basis_eq_pi')
   591     apply (simp add: inner_if setsum_delta cong: if_cong)
   592     apply (simp add: basis_orthonormal)
   593     apply (elim split_CARD_DIM, simp add: basis_eq_pi')
   594     apply (simp add: inner_if setsum_delta cong: if_cong)
   595     apply (clarsimp simp add: basis_orthonormal)
   596     done
   597 next
   598   fix x :: "'a ^ 'b"
   599   show "(\<forall>i<DIM('a ^ 'b). inner (basis i) x = 0) \<longleftrightarrow> x = 0"
   600     unfolding all_less_DIM_cart
   601     unfolding inner_vec_def
   602     apply (simp add: basis_eq_pi')
   603     apply (simp add: inner_if setsum_delta cong: if_cong)
   604     apply (simp add: euclidean_all_zero)
   605     apply (simp add: vec_eq_iff)
   606     done
   607 qed
   608 
   609 end
   610 
   611 end