1 (* Author: Tobias Nipkow, 2007 *)
3 header {* Lists as vectors *}
10 A vector-space like structure of lists and arithmetic operations on them.
11 Is only a vector space if restricted to lists of the same length. *}
13 text{* Multiplication with a scalar: *}
15 abbreviation scale :: "('a::times) \<Rightarrow> 'a list \<Rightarrow> 'a list" (infix "*\<^sub>s" 70)
16 where "x *\<^sub>s xs \<equiv> map (op * x) xs"
18 lemma scale1[simp]: "(1::'a::monoid_mult) *\<^sub>s xs = xs"
19 by (induct xs) simp_all
21 subsection {* @{text"+"} and @{text"-"} *}
23 fun zipwith0 :: "('a::zero \<Rightarrow> 'b::zero \<Rightarrow> 'c) \<Rightarrow> 'a list \<Rightarrow> 'b list \<Rightarrow> 'c list"
25 "zipwith0 f [] [] = []" |
26 "zipwith0 f (x#xs) (y#ys) = f x y # zipwith0 f xs ys" |
27 "zipwith0 f (x#xs) [] = f x 0 # zipwith0 f xs []" |
28 "zipwith0 f [] (y#ys) = f 0 y # zipwith0 f [] ys"
30 instantiation list :: ("{zero, plus}") plus
34 list_add_def: "op + = zipwith0 (op +)"
40 instantiation list :: ("{zero, uminus}") uminus
44 list_uminus_def: "uminus = map uminus"
50 instantiation list :: ("{zero,minus}") minus
54 list_diff_def: "op - = zipwith0 (op -)"
60 lemma zipwith0_Nil[simp]: "zipwith0 f [] ys = map (f 0) ys"
61 by(induct ys) simp_all
63 lemma list_add_Nil[simp]: "[] + xs = (xs::'a::monoid_add list)"
64 by (induct xs) (auto simp:list_add_def)
66 lemma list_add_Nil2[simp]: "xs + [] = (xs::'a::monoid_add list)"
67 by (induct xs) (auto simp:list_add_def)
69 lemma list_add_Cons[simp]: "(x#xs) + (y#ys) = (x+y)#(xs+ys)"
70 by(auto simp:list_add_def)
72 lemma list_diff_Nil[simp]: "[] - xs = -(xs::'a::group_add list)"
73 by (induct xs) (auto simp:list_diff_def list_uminus_def)
75 lemma list_diff_Nil2[simp]: "xs - [] = (xs::'a::group_add list)"
76 by (induct xs) (auto simp:list_diff_def)
78 lemma list_diff_Cons_Cons[simp]: "(x#xs) - (y#ys) = (x-y)#(xs-ys)"
79 by (induct xs) (auto simp:list_diff_def)
81 lemma list_uminus_Cons[simp]: "-(x#xs) = (-x)#(-xs)"
82 by (induct xs) (auto simp:list_uminus_def)
85 "xs - xs = replicate (length(xs::'a::group_add list)) 0"
86 by(induct xs) simp_all
88 lemma list_add_assoc: fixes xs :: "'a::monoid_add list"
89 shows "(xs+ys)+zs = xs+(ys+zs)"
90 apply(induct xs arbitrary: ys zs)
97 apply(simp add: add_assoc)
100 subsection "Inner product"
102 definition iprod :: "'a::ring list \<Rightarrow> 'a list \<Rightarrow> 'a" ("\<langle>_,_\<rangle>") where
103 "\<langle>xs,ys\<rangle> = (\<Sum>(x,y) \<leftarrow> zip xs ys. x*y)"
105 lemma iprod_Nil[simp]: "\<langle>[],ys\<rangle> = 0"
106 by(simp add: iprod_def)
108 lemma iprod_Nil2[simp]: "\<langle>xs,[]\<rangle> = 0"
109 by(simp add: iprod_def)
111 lemma iprod_Cons[simp]: "\<langle>x#xs,y#ys\<rangle> = x*y + \<langle>xs,ys\<rangle>"
112 by(simp add: iprod_def)
114 lemma iprod0_if_coeffs0: "\<forall>c\<in>set cs. c = 0 \<Longrightarrow> \<langle>cs,xs\<rangle> = 0"
115 apply(induct cs arbitrary:xs)
117 apply(case_tac xs) apply simp
121 lemma iprod_uminus[simp]: "\<langle>-xs,ys\<rangle> = -\<langle>xs,ys\<rangle>"
122 by(simp add: iprod_def uminus_listsum_map o_def split_def map_zip_map list_uminus_def)
124 lemma iprod_left_add_distrib: "\<langle>xs + ys,zs\<rangle> = \<langle>xs,zs\<rangle> + \<langle>ys,zs\<rangle>"
125 apply(induct xs arbitrary: ys zs)
126 apply (simp add: o_def split_def)
131 apply(simp add: distrib_right)
134 lemma iprod_left_diff_distrib: "\<langle>xs - ys, zs\<rangle> = \<langle>xs,zs\<rangle> - \<langle>ys,zs\<rangle>"
135 apply(induct xs arbitrary: ys zs)
136 apply (simp add: o_def split_def)
141 apply(simp add: left_diff_distrib)
144 lemma iprod_assoc: "\<langle>x *\<^sub>s xs, ys\<rangle> = x * \<langle>xs,ys\<rangle>"
145 apply(induct xs arbitrary: ys)
149 apply (simp add: distrib_left mult_assoc)