src/HOL/Library/ListVector.thy
author nipkow
Wed Feb 27 18:01:10 2008 +0100 (2008-02-27)
changeset 26166 dbeab703a28d
child 27109 779e73b02cca
permissions -rw-r--r--
Renamed ListSpace to ListVector
nipkow@26166
     1
(*  ID:         $Id$
nipkow@26166
     2
    Author:     Tobias Nipkow, 2007
nipkow@26166
     3
*)
nipkow@26166
     4
nipkow@26166
     5
header "Lists as vectors"
nipkow@26166
     6
nipkow@26166
     7
theory ListVector
nipkow@26166
     8
imports Main
nipkow@26166
     9
begin
nipkow@26166
    10
nipkow@26166
    11
text{* \noindent
nipkow@26166
    12
A vector-space like structure of lists and arithmetic operations on them.
nipkow@26166
    13
Is only a vector space if restricted to lists of the same length. *}
nipkow@26166
    14
nipkow@26166
    15
text{* Multiplication with a scalar: *}
nipkow@26166
    16
nipkow@26166
    17
abbreviation scale :: "('a::times) \<Rightarrow> 'a list \<Rightarrow> 'a list" (infix "*\<^sub>s" 70)
nipkow@26166
    18
where "x *\<^sub>s xs \<equiv> map (op * x) xs"
nipkow@26166
    19
nipkow@26166
    20
lemma scale1[simp]: "(1::'a::monoid_mult) *\<^sub>s xs = xs"
nipkow@26166
    21
by (induct xs) simp_all
nipkow@26166
    22
nipkow@26166
    23
subsection {* @{text"+"} and @{text"-"} *}
nipkow@26166
    24
nipkow@26166
    25
fun zipwith0 :: "('a::zero \<Rightarrow> 'b::zero \<Rightarrow> 'c) \<Rightarrow> 'a list \<Rightarrow> 'b list \<Rightarrow> 'c list"
nipkow@26166
    26
where
nipkow@26166
    27
"zipwith0 f [] [] = []" |
nipkow@26166
    28
"zipwith0 f (x#xs) (y#ys) = f x y # zipwith0 f xs ys" |
nipkow@26166
    29
"zipwith0 f (x#xs) [] = f x 0 # zipwith0 f xs []" |
nipkow@26166
    30
"zipwith0 f [] (y#ys) = f 0 y # zipwith0 f [] ys"
nipkow@26166
    31
nipkow@26166
    32
instance list :: ("{zero,plus}")plus
nipkow@26166
    33
list_add_def : "op + \<equiv> zipwith0 (op +)" ..
nipkow@26166
    34
nipkow@26166
    35
instance list :: ("{zero,uminus}")uminus
nipkow@26166
    36
list_uminus_def: "uminus \<equiv> map uminus" ..
nipkow@26166
    37
nipkow@26166
    38
instance list :: ("{zero,minus}")minus
nipkow@26166
    39
list_diff_def: "op - \<equiv> zipwith0 (op -)" ..
nipkow@26166
    40
nipkow@26166
    41
lemma zipwith0_Nil[simp]: "zipwith0 f [] ys = map (f 0) ys"
nipkow@26166
    42
by(induct ys) simp_all
nipkow@26166
    43
nipkow@26166
    44
nipkow@26166
    45
lemma list_add_Nil[simp]: "[] + xs = (xs::'a::monoid_add list)"
nipkow@26166
    46
by (induct xs) (auto simp:list_add_def)
nipkow@26166
    47
nipkow@26166
    48
lemma list_add_Nil2[simp]: "xs + [] = (xs::'a::monoid_add list)"
nipkow@26166
    49
by (induct xs) (auto simp:list_add_def)
nipkow@26166
    50
nipkow@26166
    51
lemma list_add_Cons[simp]: "(x#xs) + (y#ys) = (x+y)#(xs+ys)"
nipkow@26166
    52
by(auto simp:list_add_def)
nipkow@26166
    53
nipkow@26166
    54
lemma list_diff_Nil[simp]: "[] - xs = -(xs::'a::group_add list)"
nipkow@26166
    55
by (induct xs) (auto simp:list_diff_def list_uminus_def)
nipkow@26166
    56
nipkow@26166
    57
lemma list_diff_Nil2[simp]: "xs - [] = (xs::'a::group_add list)"
nipkow@26166
    58
by (induct xs) (auto simp:list_diff_def)
nipkow@26166
    59
nipkow@26166
    60
lemma list_diff_Cons_Cons[simp]: "(x#xs) - (y#ys) = (x-y)#(xs-ys)"
nipkow@26166
    61
by (induct xs) (auto simp:list_diff_def)
nipkow@26166
    62
nipkow@26166
    63
lemma list_uminus_Cons[simp]: "-(x#xs) = (-x)#(-xs)"
nipkow@26166
    64
by (induct xs) (auto simp:list_uminus_def)
nipkow@26166
    65
nipkow@26166
    66
lemma self_list_diff:
nipkow@26166
    67
  "xs - xs = replicate (length(xs::'a::group_add list)) 0"
nipkow@26166
    68
by(induct xs) simp_all
nipkow@26166
    69
nipkow@26166
    70
lemma list_add_assoc: fixes xs :: "'a::monoid_add list"
nipkow@26166
    71
shows "(xs+ys)+zs = xs+(ys+zs)"
nipkow@26166
    72
apply(induct xs arbitrary: ys zs)
nipkow@26166
    73
 apply simp
nipkow@26166
    74
apply(case_tac ys)
nipkow@26166
    75
 apply(simp)
nipkow@26166
    76
apply(simp)
nipkow@26166
    77
apply(case_tac zs)
nipkow@26166
    78
 apply(simp)
nipkow@26166
    79
apply(simp add:add_assoc)
nipkow@26166
    80
done
nipkow@26166
    81
nipkow@26166
    82
subsection "Inner product"
nipkow@26166
    83
nipkow@26166
    84
definition iprod :: "'a::ring list \<Rightarrow> 'a list \<Rightarrow> 'a" ("\<langle>_,_\<rangle>") where
nipkow@26166
    85
"\<langle>xs,ys\<rangle> = (\<Sum>(x,y) \<leftarrow> zip xs ys. x*y)"
nipkow@26166
    86
nipkow@26166
    87
lemma iprod_Nil[simp]: "\<langle>[],ys\<rangle> = 0"
nipkow@26166
    88
by(simp add:iprod_def)
nipkow@26166
    89
nipkow@26166
    90
lemma iprod_Nil2[simp]: "\<langle>xs,[]\<rangle> = 0"
nipkow@26166
    91
by(simp add:iprod_def)
nipkow@26166
    92
nipkow@26166
    93
lemma iprod_Cons[simp]: "\<langle>x#xs,y#ys\<rangle> = x*y + \<langle>xs,ys\<rangle>"
nipkow@26166
    94
by(simp add:iprod_def)
nipkow@26166
    95
nipkow@26166
    96
lemma iprod0_if_coeffs0: "\<forall>c\<in>set cs. c = 0 \<Longrightarrow> \<langle>cs,xs\<rangle> = 0"
nipkow@26166
    97
apply(induct cs arbitrary:xs)
nipkow@26166
    98
 apply simp
nipkow@26166
    99
apply(case_tac xs) apply simp
nipkow@26166
   100
apply auto
nipkow@26166
   101
done
nipkow@26166
   102
nipkow@26166
   103
lemma iprod_uminus[simp]: "\<langle>-xs,ys\<rangle> = -\<langle>xs,ys\<rangle>"
nipkow@26166
   104
by(simp add: iprod_def uminus_listsum_map o_def split_def map_zip_map list_uminus_def)
nipkow@26166
   105
nipkow@26166
   106
lemma iprod_left_add_distrib: "\<langle>xs + ys,zs\<rangle> = \<langle>xs,zs\<rangle> + \<langle>ys,zs\<rangle>"
nipkow@26166
   107
apply(induct xs arbitrary: ys zs)
nipkow@26166
   108
apply (simp add: o_def split_def)
nipkow@26166
   109
apply(case_tac ys)
nipkow@26166
   110
apply simp
nipkow@26166
   111
apply(case_tac zs)
nipkow@26166
   112
apply (simp)
nipkow@26166
   113
apply(simp add:left_distrib)
nipkow@26166
   114
done
nipkow@26166
   115
nipkow@26166
   116
lemma iprod_left_diff_distrib: "\<langle>xs - ys, zs\<rangle> = \<langle>xs,zs\<rangle> - \<langle>ys,zs\<rangle>"
nipkow@26166
   117
apply(induct xs arbitrary: ys zs)
nipkow@26166
   118
apply (simp add: o_def split_def)
nipkow@26166
   119
apply(case_tac ys)
nipkow@26166
   120
apply simp
nipkow@26166
   121
apply(case_tac zs)
nipkow@26166
   122
apply (simp)
nipkow@26166
   123
apply(simp add:left_diff_distrib)
nipkow@26166
   124
done
nipkow@26166
   125
nipkow@26166
   126
lemma iprod_assoc: "\<langle>x *\<^sub>s xs, ys\<rangle> = x * \<langle>xs,ys\<rangle>"
nipkow@26166
   127
apply(induct xs arbitrary: ys)
nipkow@26166
   128
apply simp
nipkow@26166
   129
apply(case_tac ys)
nipkow@26166
   130
apply (simp)
nipkow@26166
   131
apply (simp add:right_distrib mult_assoc)
nipkow@26166
   132
done
nipkow@26166
   133
nipkow@26166
   134
end