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