author | haftmann |
Thu, 19 Jun 2025 17:15:40 +0200 | |
changeset 82734 | 89347c0cc6a3 |
parent 81804 | 5a2e05eb7001 |
permissions | -rw-r--r-- |
30663
0b6aff7451b2
Main is (Complex_Main) base entry point in library theories
haftmann
parents:
27487
diff
changeset
|
1 |
(* Author: Tobias Nipkow, 2007 *) |
26166 | 2 |
|
60500 | 3 |
section \<open>Lists as vectors\<close> |
26166 | 4 |
|
5 |
theory ListVector |
|
81804 | 6 |
imports Main |
26166 | 7 |
begin |
8 |
||
60500 | 9 |
text\<open>\noindent |
26166 | 10 |
A vector-space like structure of lists and arithmetic operations on them. |
60500 | 11 |
Is only a vector space if restricted to lists of the same length.\<close> |
26166 | 12 |
|
60500 | 13 |
text\<open>Multiplication with a scalar:\<close> |
26166 | 14 |
|
80914
d97fdabd9e2b
standardize mixfix annotations via "isabelle update -a -u mixfix_cartouches" --- to simplify systematic editing;
wenzelm
parents:
69064
diff
changeset
|
15 |
abbreviation scale :: "('a::times) \<Rightarrow> 'a list \<Rightarrow> 'a list" (infix \<open>*\<^sub>s\<close> 70) |
81804 | 16 |
where "x *\<^sub>s xs \<equiv> map ((*) x) xs" |
26166 | 17 |
|
18 |
lemma scale1[simp]: "(1::'a::monoid_mult) *\<^sub>s xs = xs" |
|
81804 | 19 |
by (induct xs) simp_all |
26166 | 20 |
|
61585 | 21 |
subsection \<open>\<open>+\<close> and \<open>-\<close>\<close> |
26166 | 22 |
|
23 |
fun zipwith0 :: "('a::zero \<Rightarrow> 'b::zero \<Rightarrow> 'c) \<Rightarrow> 'a list \<Rightarrow> 'b list \<Rightarrow> 'c list" |
|
81804 | 24 |
where |
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" |
|
26166 | 29 |
|
27109 | 30 |
instantiation list :: ("{zero, plus}") plus |
31 |
begin |
|
32 |
||
33 |
definition |
|
67399 | 34 |
list_add_def: "(+) = zipwith0 (+)" |
27109 | 35 |
|
36 |
instance .. |
|
37 |
||
38 |
end |
|
39 |
||
40 |
instantiation list :: ("{zero, uminus}") uminus |
|
41 |
begin |
|
26166 | 42 |
|
27109 | 43 |
definition |
44 |
list_uminus_def: "uminus = map uminus" |
|
45 |
||
46 |
instance .. |
|
47 |
||
48 |
end |
|
26166 | 49 |
|
27109 | 50 |
instantiation list :: ("{zero,minus}") minus |
51 |
begin |
|
52 |
||
53 |
definition |
|
67399 | 54 |
list_diff_def: "(-) = zipwith0 (-)" |
27109 | 55 |
|
56 |
instance .. |
|
57 |
||
58 |
end |
|
26166 | 59 |
|
60 |
lemma zipwith0_Nil[simp]: "zipwith0 f [] ys = map (f 0) ys" |
|
81804 | 61 |
by(induct ys) simp_all |
26166 | 62 |
|
63 |
lemma list_add_Nil[simp]: "[] + xs = (xs::'a::monoid_add list)" |
|
81804 | 64 |
by (induct xs) (auto simp:list_add_def) |
26166 | 65 |
|
66 |
lemma list_add_Nil2[simp]: "xs + [] = (xs::'a::monoid_add list)" |
|
81804 | 67 |
by (induct xs) (auto simp:list_add_def) |
26166 | 68 |
|
69 |
lemma list_add_Cons[simp]: "(x#xs) + (y#ys) = (x+y)#(xs+ys)" |
|
81804 | 70 |
by(auto simp:list_add_def) |
26166 | 71 |
|
72 |
lemma list_diff_Nil[simp]: "[] - xs = -(xs::'a::group_add list)" |
|
81804 | 73 |
by (induct xs) (auto simp:list_diff_def list_uminus_def) |
26166 | 74 |
|
75 |
lemma list_diff_Nil2[simp]: "xs - [] = (xs::'a::group_add list)" |
|
81804 | 76 |
by (induct xs) (auto simp:list_diff_def) |
26166 | 77 |
|
78 |
lemma list_diff_Cons_Cons[simp]: "(x#xs) - (y#ys) = (x-y)#(xs-ys)" |
|
81804 | 79 |
by (induct xs) (auto simp:list_diff_def) |
26166 | 80 |
|
81 |
lemma list_uminus_Cons[simp]: "-(x#xs) = (-x)#(-xs)" |
|
81804 | 82 |
by (induct xs) (auto simp:list_uminus_def) |
26166 | 83 |
|
84 |
lemma self_list_diff: |
|
85 |
"xs - xs = replicate (length(xs::'a::group_add list)) 0" |
|
81804 | 86 |
by(induct xs) simp_all |
26166 | 87 |
|
81804 | 88 |
lemma list_add_assoc: |
89 |
fixes xs :: "'a::monoid_add list" |
|
90 |
shows "(xs+ys)+zs = xs+(ys+zs)" |
|
91 |
proof (induct xs arbitrary: ys zs) |
|
92 |
case Nil |
|
93 |
then show ?case by simp |
|
94 |
next |
|
95 |
case (Cons a xs ys zs) |
|
96 |
show ?case |
|
97 |
by (cases ys; cases zs; simp add: add.assoc Cons) |
|
98 |
qed |
|
26166 | 99 |
|
100 |
subsection "Inner product" |
|
101 |
||
81142 | 102 |
definition iprod :: "'a::ring list \<Rightarrow> 'a list \<Rightarrow> 'a" (\<open>(\<open>open_block notation=\<open>mixfix iprod\<close>\<close>\<langle>_,_\<rangle>)\<close>) |
103 |
where "\<langle>xs,ys\<rangle> = (\<Sum>(x,y) \<leftarrow> zip xs ys. x*y)" |
|
26166 | 104 |
|
105 |
lemma iprod_Nil[simp]: "\<langle>[],ys\<rangle> = 0" |
|
81804 | 106 |
by(simp add: iprod_def) |
26166 | 107 |
|
108 |
lemma iprod_Nil2[simp]: "\<langle>xs,[]\<rangle> = 0" |
|
81804 | 109 |
by(simp add: iprod_def) |
26166 | 110 |
|
111 |
lemma iprod_Cons[simp]: "\<langle>x#xs,y#ys\<rangle> = x*y + \<langle>xs,ys\<rangle>" |
|
81804 | 112 |
by(simp add: iprod_def) |
26166 | 113 |
|
114 |
lemma iprod0_if_coeffs0: "\<forall>c\<in>set cs. c = 0 \<Longrightarrow> \<langle>cs,xs\<rangle> = 0" |
|
81804 | 115 |
proof (induct cs arbitrary: xs) |
116 |
case Nil |
|
117 |
then show ?case by simp |
|
118 |
next |
|
119 |
case (Cons a cs xs) |
|
120 |
then show ?case |
|
121 |
by (cases xs; fastforce) |
|
122 |
qed |
|
26166 | 123 |
|
124 |
lemma iprod_uminus[simp]: "\<langle>-xs,ys\<rangle> = -\<langle>xs,ys\<rangle>" |
|
81804 | 125 |
by(simp add: iprod_def uminus_sum_list_map o_def split_def map_zip_map list_uminus_def) |
26166 | 126 |
|
127 |
lemma iprod_left_add_distrib: "\<langle>xs + ys,zs\<rangle> = \<langle>xs,zs\<rangle> + \<langle>ys,zs\<rangle>" |
|
81804 | 128 |
proof (induct xs arbitrary: ys zs) |
129 |
case Nil |
|
130 |
then show ?case by simp |
|
131 |
next |
|
132 |
case (Cons a xs ys zs) |
|
133 |
show ?case |
|
134 |
by (cases ys; cases zs; simp add: distrib_right Cons) |
|
135 |
qed |
|
26166 | 136 |
|
137 |
lemma iprod_left_diff_distrib: "\<langle>xs - ys, zs\<rangle> = \<langle>xs,zs\<rangle> - \<langle>ys,zs\<rangle>" |
|
81804 | 138 |
proof (induct xs arbitrary: ys zs) |
139 |
case Nil |
|
140 |
then show ?case by simp |
|
141 |
next |
|
142 |
case (Cons a xs ys zs) |
|
143 |
show ?case |
|
144 |
by (cases ys; cases zs; simp add: left_diff_distrib Cons) |
|
145 |
qed |
|
26166 | 146 |
|
147 |
lemma iprod_assoc: "\<langle>x *\<^sub>s xs, ys\<rangle> = x * \<langle>xs,ys\<rangle>" |
|
81804 | 148 |
proof (induct xs arbitrary: ys) |
149 |
case Nil |
|
150 |
then show ?case by simp |
|
151 |
next |
|
152 |
case (Cons a xs ys) |
|
153 |
show ?case |
|
154 |
by (cases ys; simp add: distrib_left mult.assoc Cons) |
|
155 |
qed |
|
26166 | 156 |
|
49961 | 157 |
end |