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