src/HOL/Multivariate_Analysis/Finite_Cartesian_Product.thy
author wenzelm
Wed Dec 29 17:34:41 2010 +0100 (2010-12-29)
changeset 41413 64cd30d6b0b8
parent 39302 d7728f65b353
child 42290 b1f544c84040
permissions -rw-r--r--
explicit file specifications -- avoid secondary load path;
     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   "~~/src/HOL/Library/Inner_Product"
    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 Cart)
    17   ('a, 'b) cart = "UNIV :: (('b::finite) \<Rightarrow> 'a) set"
    18   morphisms Cart_nth Cart_lambda ..
    19 
    20 notation
    21   Cart_nth (infixl "$" 90) and
    22   Cart_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 "cart 'b 'n"
    27 *)
    28 
    29 syntax "_finite_cart" :: "type \<Rightarrow> type \<Rightarrow> type" ("(_ ^/ _)" [15, 16] 15)
    30 
    31 parse_translation {*
    32 let
    33   fun cart t u = Syntax.const @{type_syntax cart} $ t $ u;
    34   fun finite_cart_tr [t, u as Free (x, _)] =
    35         if Syntax.is_tid x then
    36           cart t (Syntax.const @{syntax_const "_ofsort"} $ u $ Syntax.const @{class_syntax finite})
    37         else cart t u
    38     | finite_cart_tr [t, u] = cart t u
    39 in
    40   [(@{syntax_const "_finite_cart"}, finite_cart_tr)]
    41 end
    42 *}
    43 
    44 lemma stupid_ext: "(\<forall>x. f x = g x) \<longleftrightarrow> (f = g)"
    45   by (auto intro: ext)
    46 
    47 lemma Cart_eq: "(x = y) \<longleftrightarrow> (\<forall>i. x$i = y$i)"
    48   by (simp add: Cart_nth_inject [symmetric] fun_eq_iff)
    49 
    50 lemma Cart_lambda_beta [simp]: "Cart_lambda g $ i = g i"
    51   by (simp add: Cart_lambda_inverse)
    52 
    53 lemma Cart_lambda_unique: "(\<forall>i. f$i = g i) \<longleftrightarrow> Cart_lambda g = f"
    54   by (auto simp add: Cart_eq)
    55 
    56 lemma Cart_lambda_eta: "(\<chi> i. (g$i)) = g"
    57   by (simp add: Cart_eq)
    58 
    59 
    60 subsection {* Group operations and class instances *}
    61 
    62 instantiation cart :: (zero,finite) zero
    63 begin
    64   definition vector_zero_def : "0 \<equiv> (\<chi> i. 0)"
    65   instance ..
    66 end
    67 
    68 instantiation cart :: (plus,finite) plus
    69 begin
    70   definition  vector_add_def : "op + \<equiv> (\<lambda> x y.  (\<chi> i. (x$i) + (y$i)))"
    71   instance ..
    72 end
    73 
    74 instantiation cart :: (minus,finite) minus
    75 begin
    76   definition vector_minus_def : "op - \<equiv> (\<lambda> x y.  (\<chi> i. (x$i) - (y$i)))"
    77   instance ..
    78 end
    79 
    80 instantiation cart :: (uminus,finite) uminus
    81 begin
    82   definition vector_uminus_def : "uminus \<equiv> (\<lambda> x.  (\<chi> i. - (x$i)))"
    83   instance ..
    84 end
    85 
    86 lemma zero_index [simp]: "0 $ i = 0"
    87   unfolding vector_zero_def by simp
    88 
    89 lemma vector_add_component [simp]: "(x + y)$i = x$i + y$i"
    90   unfolding vector_add_def by simp
    91 
    92 lemma vector_minus_component [simp]: "(x - y)$i = x$i - y$i"
    93   unfolding vector_minus_def by simp
    94 
    95 lemma vector_uminus_component [simp]: "(- x)$i = - (x$i)"
    96   unfolding vector_uminus_def by simp
    97 
    98 instance cart :: (semigroup_add, finite) semigroup_add
    99   by default (simp add: Cart_eq add_assoc)
   100 
   101 instance cart :: (ab_semigroup_add, finite) ab_semigroup_add
   102   by default (simp add: Cart_eq add_commute)
   103 
   104 instance cart :: (monoid_add, finite) monoid_add
   105   by default (simp_all add: Cart_eq)
   106 
   107 instance cart :: (comm_monoid_add, finite) comm_monoid_add
   108   by default (simp add: Cart_eq)
   109 
   110 instance cart :: (cancel_semigroup_add, finite) cancel_semigroup_add
   111   by default (simp_all add: Cart_eq)
   112 
   113 instance cart :: (cancel_ab_semigroup_add, finite) cancel_ab_semigroup_add
   114   by default (simp add: Cart_eq)
   115 
   116 instance cart :: (cancel_comm_monoid_add, finite) cancel_comm_monoid_add ..
   117 
   118 instance cart :: (group_add, finite) group_add
   119   by default (simp_all add: Cart_eq diff_minus)
   120 
   121 instance cart :: (ab_group_add, finite) ab_group_add
   122   by default (simp_all add: Cart_eq)
   123 
   124 
   125 subsection {* Real vector space *}
   126 
   127 instantiation cart :: (real_vector, finite) real_vector
   128 begin
   129 
   130 definition vector_scaleR_def: "scaleR = (\<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 vector_scaleR_def by simp
   134 
   135 instance
   136   by default (simp_all add: Cart_eq scaleR_left_distrib scaleR_right_distrib)
   137 
   138 end
   139 
   140 
   141 subsection {* Topological space *}
   142 
   143 instantiation cart :: (topological_space, finite) topological_space
   144 begin
   145 
   146 definition open_vector_def:
   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_vector_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_vector_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_vector_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_vector_def by auto
   181 
   182 lemma open_vimage_Cart_nth: "open S \<Longrightarrow> open ((\<lambda>x. x $ i) -` S)"
   183 unfolding open_vector_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_Cart_nth: "closed S \<Longrightarrow> closed ((\<lambda>x. x $ i) -` S)"
   189 unfolding closed_open vimage_Compl [symmetric]
   190 by (rule open_vimage_Cart_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_Cart_nth)
   197 qed
   198 
   199 lemma tendsto_Cart_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_Cart_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 tendsto_vector:
   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_vector_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_Cart_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: tendsto_vector)
   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 cart :: (metric_space, finite) metric_space
   255 begin
   256 
   257 definition dist_vector_def:
   258   "dist x y = setL2 (\<lambda>i. dist (x$i) (y$i)) UNIV"
   259 
   260 lemma dist_nth_le_cart: "dist (x $ i) (y $ i) \<le> dist x y"
   261 unfolding dist_vector_def
   262 by (rule member_le_setL2) simp_all
   263 
   264 instance proof
   265   fix x y :: "'a ^ 'b"
   266   show "dist x y = 0 \<longleftrightarrow> x = y"
   267     unfolding dist_vector_def
   268     by (simp add: setL2_eq_0_iff Cart_eq)
   269 next
   270   fix x y z :: "'a ^ 'b"
   271   show "dist x y \<le> dist x z + dist y z"
   272     unfolding dist_vector_def
   273     apply (rule order_trans [OF _ setL2_triangle_ineq])
   274     apply (simp add: setL2_mono dist_triangle2)
   275     done
   276 next
   277   (* FIXME: long proof! *)
   278   fix S :: "('a ^ 'b) set"
   279   show "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S)"
   280     unfolding open_vector_def open_dist
   281     apply safe
   282      apply (drule (1) bspec)
   283      apply clarify
   284      apply (subgoal_tac "\<exists>e>0. \<forall>i y. dist y (x$i) < e \<longrightarrow> y \<in> A i")
   285       apply clarify
   286       apply (rule_tac x=e in exI, clarify)
   287       apply (drule spec, erule mp, clarify)
   288       apply (drule spec, drule spec, erule mp)
   289       apply (erule le_less_trans [OF dist_nth_le_cart])
   290      apply (subgoal_tac "\<forall>i\<in>UNIV. \<exists>e>0. \<forall>y. dist y (x$i) < e \<longrightarrow> y \<in> A i")
   291       apply (drule finite_choice [OF finite], clarify)
   292       apply (rule_tac x="Min (range f)" in exI, simp)
   293      apply clarify
   294      apply (drule_tac x=i in spec, clarify)
   295      apply (erule (1) bspec)
   296     apply (drule (1) bspec, clarify)
   297     apply (subgoal_tac "\<exists>r. (\<forall>i::'b. 0 < r i) \<and> e = setL2 r UNIV")
   298      apply clarify
   299      apply (rule_tac x="\<lambda>i. {y. dist y (x$i) < r i}" in exI)
   300      apply (rule conjI)
   301       apply clarify
   302       apply (rule conjI)
   303        apply (clarify, rename_tac y)
   304        apply (rule_tac x="r i - dist y (x$i)" in exI, rule conjI, simp)
   305        apply clarify
   306        apply (simp only: less_diff_eq)
   307        apply (erule le_less_trans [OF dist_triangle])
   308       apply simp
   309      apply clarify
   310      apply (drule spec, erule mp)
   311      apply (simp add: dist_vector_def setL2_strict_mono)
   312     apply (rule_tac x="\<lambda>i. e / sqrt (of_nat CARD('b))" in exI)
   313     apply (simp add: divide_pos_pos setL2_constant)
   314     done
   315 qed
   316 
   317 end
   318 
   319 lemma Cauchy_Cart_nth:
   320   "Cauchy (\<lambda>n. X n) \<Longrightarrow> Cauchy (\<lambda>n. X n $ i)"
   321 unfolding Cauchy_def by (fast intro: le_less_trans [OF dist_nth_le_cart])
   322 
   323 lemma Cauchy_vector:
   324   fixes X :: "nat \<Rightarrow> 'a::metric_space ^ 'n"
   325   assumes X: "\<And>i. Cauchy (\<lambda>n. X n $ i)"
   326   shows "Cauchy (\<lambda>n. X n)"
   327 proof (rule metric_CauchyI)
   328   fix r :: real assume "0 < r"
   329   then have "0 < r / of_nat CARD('n)" (is "0 < ?s")
   330     by (simp add: divide_pos_pos)
   331   def N \<equiv> "\<lambda>i. LEAST N. \<forall>m\<ge>N. \<forall>n\<ge>N. dist (X m $ i) (X n $ i) < ?s"
   332   def M \<equiv> "Max (range N)"
   333   have "\<And>i. \<exists>N. \<forall>m\<ge>N. \<forall>n\<ge>N. dist (X m $ i) (X n $ i) < ?s"
   334     using X `0 < ?s` by (rule metric_CauchyD)
   335   hence "\<And>i. \<forall>m\<ge>N i. \<forall>n\<ge>N i. dist (X m $ i) (X n $ i) < ?s"
   336     unfolding N_def by (rule LeastI_ex)
   337   hence M: "\<And>i. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m $ i) (X n $ i) < ?s"
   338     unfolding M_def by simp
   339   {
   340     fix m n :: nat
   341     assume "M \<le> m" "M \<le> n"
   342     have "dist (X m) (X n) = setL2 (\<lambda>i. dist (X m $ i) (X n $ i)) UNIV"
   343       unfolding dist_vector_def ..
   344     also have "\<dots> \<le> setsum (\<lambda>i. dist (X m $ i) (X n $ i)) UNIV"
   345       by (rule setL2_le_setsum [OF zero_le_dist])
   346     also have "\<dots> < setsum (\<lambda>i::'n. ?s) UNIV"
   347       by (rule setsum_strict_mono, simp_all add: M `M \<le> m` `M \<le> n`)
   348     also have "\<dots> = r"
   349       by simp
   350     finally have "dist (X m) (X n) < r" .
   351   }
   352   hence "\<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < r"
   353     by simp
   354   then show "\<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < r" ..
   355 qed
   356 
   357 instance cart :: (complete_space, finite) complete_space
   358 proof
   359   fix X :: "nat \<Rightarrow> 'a ^ 'b" assume "Cauchy X"
   360   have "\<And>i. (\<lambda>n. X n $ i) ----> lim (\<lambda>n. X n $ i)"
   361     using Cauchy_Cart_nth [OF `Cauchy X`]
   362     by (simp add: Cauchy_convergent_iff convergent_LIMSEQ_iff)
   363   hence "X ----> Cart_lambda (\<lambda>i. lim (\<lambda>n. X n $ i))"
   364     by (simp add: tendsto_vector)
   365   then show "convergent X"
   366     by (rule convergentI)
   367 qed
   368 
   369 
   370 subsection {* Normed vector space *}
   371 
   372 instantiation cart :: (real_normed_vector, finite) real_normed_vector
   373 begin
   374 
   375 definition norm_vector_def:
   376   "norm x = setL2 (\<lambda>i. norm (x$i)) UNIV"
   377 
   378 definition vector_sgn_def:
   379   "sgn (x::'a^'b) = scaleR (inverse (norm x)) x"
   380 
   381 instance proof
   382   fix a :: real and x y :: "'a ^ 'b"
   383   show "0 \<le> norm x"
   384     unfolding norm_vector_def
   385     by (rule setL2_nonneg)
   386   show "norm x = 0 \<longleftrightarrow> x = 0"
   387     unfolding norm_vector_def
   388     by (simp add: setL2_eq_0_iff Cart_eq)
   389   show "norm (x + y) \<le> norm x + norm y"
   390     unfolding norm_vector_def
   391     apply (rule order_trans [OF _ setL2_triangle_ineq])
   392     apply (simp add: setL2_mono norm_triangle_ineq)
   393     done
   394   show "norm (scaleR a x) = \<bar>a\<bar> * norm x"
   395     unfolding norm_vector_def
   396     by (simp add: setL2_right_distrib)
   397   show "sgn x = scaleR (inverse (norm x)) x"
   398     by (rule vector_sgn_def)
   399   show "dist x y = norm (x - y)"
   400     unfolding dist_vector_def norm_vector_def
   401     by (simp add: dist_norm)
   402 qed
   403 
   404 end
   405 
   406 lemma norm_nth_le: "norm (x $ i) \<le> norm x"
   407 unfolding norm_vector_def
   408 by (rule member_le_setL2) simp_all
   409 
   410 interpretation Cart_nth: bounded_linear "\<lambda>x. x $ i"
   411 apply default
   412 apply (rule vector_add_component)
   413 apply (rule vector_scaleR_component)
   414 apply (rule_tac x="1" in exI, simp add: norm_nth_le)
   415 done
   416 
   417 instance cart :: (banach, finite) banach ..
   418 
   419 
   420 subsection {* Inner product space *}
   421 
   422 instantiation cart :: (real_inner, finite) real_inner
   423 begin
   424 
   425 definition inner_vector_def:
   426   "inner x y = setsum (\<lambda>i. inner (x$i) (y$i)) UNIV"
   427 
   428 instance proof
   429   fix r :: real and x y z :: "'a ^ 'b"
   430   show "inner x y = inner y x"
   431     unfolding inner_vector_def
   432     by (simp add: inner_commute)
   433   show "inner (x + y) z = inner x z + inner y z"
   434     unfolding inner_vector_def
   435     by (simp add: inner_add_left setsum_addf)
   436   show "inner (scaleR r x) y = r * inner x y"
   437     unfolding inner_vector_def
   438     by (simp add: setsum_right_distrib)
   439   show "0 \<le> inner x x"
   440     unfolding inner_vector_def
   441     by (simp add: setsum_nonneg)
   442   show "inner x x = 0 \<longleftrightarrow> x = 0"
   443     unfolding inner_vector_def
   444     by (simp add: Cart_eq setsum_nonneg_eq_0_iff)
   445   show "norm x = sqrt (inner x x)"
   446     unfolding inner_vector_def norm_vector_def setL2_def
   447     by (simp add: power2_norm_eq_inner)
   448 qed
   449 
   450 end
   451 
   452 end