src/HOL/Library/ListVector.thy
author wenzelm
Thu, 03 Apr 2008 16:03:57 +0200
changeset 26527 c392354a1b79
parent 26166 dbeab703a28d
child 27109 779e73b02cca
permissions -rw-r--r--
Symbol.STX, Symbol.DEL;
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
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
     8
imports Main
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
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    32
instance list :: ("{zero,plus}")plus
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    33
list_add_def : "op + \<equiv> zipwith0 (op +)" ..
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    34
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    35
instance list :: ("{zero,uminus}")uminus
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    36
list_uminus_def: "uminus \<equiv> map uminus" ..
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    37
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    38
instance list :: ("{zero,minus}")minus
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    39
list_diff_def: "op - \<equiv> zipwith0 (op -)" ..
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    40
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    41
lemma zipwith0_Nil[simp]: "zipwith0 f [] ys = map (f 0) ys"
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    42
by(induct ys) simp_all
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    43
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    44
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    45
lemma list_add_Nil[simp]: "[] + xs = (xs::'a::monoid_add list)"
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    46
by (induct xs) (auto simp:list_add_def)
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    47
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    48
lemma list_add_Nil2[simp]: "xs + [] = (xs::'a::monoid_add list)"
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    49
by (induct xs) (auto simp:list_add_def)
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    50
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    51
lemma list_add_Cons[simp]: "(x#xs) + (y#ys) = (x+y)#(xs+ys)"
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    52
by(auto simp:list_add_def)
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    53
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    54
lemma list_diff_Nil[simp]: "[] - xs = -(xs::'a::group_add list)"
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    55
by (induct xs) (auto simp:list_diff_def list_uminus_def)
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    56
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    57
lemma list_diff_Nil2[simp]: "xs - [] = (xs::'a::group_add list)"
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    58
by (induct xs) (auto simp:list_diff_def)
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    59
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    60
lemma list_diff_Cons_Cons[simp]: "(x#xs) - (y#ys) = (x-y)#(xs-ys)"
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    61
by (induct xs) (auto simp:list_diff_def)
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    62
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    63
lemma list_uminus_Cons[simp]: "-(x#xs) = (-x)#(-xs)"
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    64
by (induct xs) (auto simp:list_uminus_def)
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    65
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    66
lemma self_list_diff:
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    67
  "xs - xs = replicate (length(xs::'a::group_add list)) 0"
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    68
by(induct xs) simp_all
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    69
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    70
lemma list_add_assoc: fixes xs :: "'a::monoid_add list"
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    71
shows "(xs+ys)+zs = xs+(ys+zs)"
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    72
apply(induct xs arbitrary: ys zs)
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    73
 apply simp
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    74
apply(case_tac ys)
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    75
 apply(simp)
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    76
apply(simp)
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    77
apply(case_tac zs)
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    78
 apply(simp)
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    79
apply(simp add:add_assoc)
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    80
done
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    81
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    82
subsection "Inner product"
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    83
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    84
definition iprod :: "'a::ring list \<Rightarrow> 'a list \<Rightarrow> 'a" ("\<langle>_,_\<rangle>") where
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    85
"\<langle>xs,ys\<rangle> = (\<Sum>(x,y) \<leftarrow> zip xs ys. x*y)"
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    86
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    87
lemma iprod_Nil[simp]: "\<langle>[],ys\<rangle> = 0"
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    88
by(simp add:iprod_def)
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    89
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    90
lemma iprod_Nil2[simp]: "\<langle>xs,[]\<rangle> = 0"
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    91
by(simp add:iprod_def)
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    92
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    93
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
    94
by(simp add:iprod_def)
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    95
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    96
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
    97
apply(induct cs arbitrary:xs)
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    98
 apply simp
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
    99
apply(case_tac xs) apply simp
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
   100
apply auto
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
   101
done
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
   102
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
   103
lemma iprod_uminus[simp]: "\<langle>-xs,ys\<rangle> = -\<langle>xs,ys\<rangle>"
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
   104
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
   105
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
   106
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
   107
apply(induct xs arbitrary: ys zs)
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
   108
apply (simp add: o_def split_def)
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
   109
apply(case_tac ys)
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
   110
apply simp
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
   111
apply(case_tac zs)
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
   112
apply (simp)
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
   113
apply(simp add:left_distrib)
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
   114
done
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
   115
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
   116
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
   117
apply(induct xs arbitrary: ys zs)
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
   118
apply (simp add: o_def split_def)
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
   119
apply(case_tac ys)
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
   120
apply simp
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
   121
apply(case_tac zs)
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
   122
apply (simp)
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
   123
apply(simp add:left_diff_distrib)
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
   124
done
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
   125
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
   126
lemma iprod_assoc: "\<langle>x *\<^sub>s xs, ys\<rangle> = x * \<langle>xs,ys\<rangle>"
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
   127
apply(induct xs arbitrary: ys)
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
   128
apply simp
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 (simp add:right_distrib mult_assoc)
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
   132
done
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
   133
dbeab703a28d Renamed ListSpace to ListVector
nipkow
parents:
diff changeset
   134
end