-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproof
170 lines (140 loc) · 3.83 KB
/
proof
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
-- Note : if xss is empty, both evaluate to bottom
thm 1.
foldl (zipWith (++)) (head xss) (tail xss)
== {lem 1, associativity of (zipWith (++))}
foldr1 (zipWith (++)) xss
lem 1.
-- given that f is associative, and xs is a finite list, foldl f x xs == foldr1 f (x:xs)
Inductive hypothesis:
foldl f y ys == foldr1 f (y:ys)
case 1.
foldl f x []
== {def foldl}
x
== {def foldr1}
foldr1 f (x:[])
case 2.
foldl f x (y:ys)
== {def foldl}
foldl (f x y) ys
== {lem 1.1, f assoc}
f x (foldl y ys)
== {Ind Hyp}
f x (foldr1 (y:ys))
== {def foldr1}
foldr1 f (x:y:ys)
lem 1.1.
-- given that f is associative, and ys is a finite list foldl f (f x y) ys == f x (foldl y ys)
Inductive hypothesis:
foldl f (f x y) zs == f x (foldl f y zs)
case 1.
foldl f (f x y) (z:zs)
== {def foldl}
foldl f (f (f x y) z) zs
== {associativity of f}
foldl f (f x (f y z)) zs
== {Ind Hyp}
f x (foldl f (f y z) zs)
== {def foldl}
f x (foldl f y (z:zs))
case 2.
foldl f (f x y) []
== {def foldl}
f x y
== {def foldl}
f x (foldl y [])
-- Note - The cases involving bottom rely on my assumptions about how ghc works,
-- of which I am not completely certain. However, they only matter if you care
-- about infinite lists or passing lists which result in an error when evaluating completely
lem 2. (associativity of zipWith (++))
Inductive hypothesis:
zipWith (++) xs (zipWith (++) ys zs)
for xs ys and zs substructures of the arguments in consideration
case 1.
zipWith (++) (x:xs) (zipWith (++) (y:ys) (z:zs))
== {def zipWith}
zipWith (++) (x:xs) ((y ++ z) : zipWith (++) ys zs)
== {def zipWith}
(x ++ (y ++ z)) : zipWith (++) xs (zipWith (++) ys zs)
== {Ind Hyp}
(x ++ (y ++ z)) : zipWith (++) (zipWith (++) xs ys) zs
== {lem 3. associativity of (++)}
((x ++ y) ++ z) : zipWith (++) (zipWith (++) xs ys) zs
== {def zipWith}
zipWith (++) ((x ++ y) : zipWith (++) xs ys) (z:zs)
== {def zipWith}
zipWith (++) (x:xs) (zipWith (++) (y:ys) (z:zs))
case 2.
zipWith (++) [] (zipWith (++) ys zs)
== {def zipWith}
[]
== {def zipWith}
zipWith (++) [] zs
== {def zipWith}
zipWith (++) (zipWith (++) [] ys) zs
case 3.
zipWith (++) (x:xs) (zipWith (++) [] zs)
== {def zipWith}
zipWith (++) (x:xs) []
== {def zipWith}
[]
== {def zipWith}
zipWith (++) [] zs
== {def zipWith}
zipWith (++) (zipWith (++) (x:xs) []) zs
case 4.
zipWith (++) (x:xs) (zipWith (++) (y:ys) [])
== {def zipWith}
zipWith (++) (x:xs) []
== {def zipWith}
[]
== {def zipWith}
zipWith (++) ((x++y) : zipWith (++) xs ys) []
== {def zipWith}
zipWith (++) (zipWith (++) (x:xs) (y:ys)) []
case 5.
zipWith (++) ⊥ (zipWith (++) ys zs)
== {def zipWith}
⊥
== {def zipWith}
zipWith (++) ⊥ zs
== {def zipWith}
zipWith (++) (zipWith (++) ⊥ ys) zs
case 6.
zipWith (++) (x:xs) (zipWith (++) ⊥ zs)
== {def zipWith}
⊥
== {def zipWith}
zipWith (++) ⊥ zs
== {def zipWith}
zipWith (++) (zipWith (++) (x:xs) ⊥) zs
case 7.
zipWith (++) (x:xs) (zipWith (++) (y:ys) ⊥)
== {def zipWith}
zipWith (++) (x:xs) ⊥
== {def zipWith}
⊥
== {def zipWith}
zipWith (++) ((x++y) : zipWith (++) xs ys) ⊥
== {def zipWith}
zipWith (++) (zipWith (++) (x:xs) (y:ys)) ⊥
Definitions:
-- Note that these are not the same as the ones in Data.List/Prelude,
-- Those contain various optimisations that make proving things with them harder.
foldl :: (a -> b -> a) -> [b] -> a
foldl f e [] = e
foldl f e (x:xs) = foldl f (f e x) xs
foldr1 :: (a -> a -> a) -> [a] -> a
foldr1 f [x] = x
foldr1 f (x:xs) = f x (foldr1 f xs)
zipWith f (a:as) (b:bs) = f a b : zipWith as bs
zipWith f _ _ = []
-- I believe the above is equivalent to the below
-- zipWith f (a:as) bs = case bs of
-- (b:bs') -> f a b : zipWith as bs'
-- [] -> []
-- zipWith f [] _ = []
(++) :: [a] -> [a] -> [a]
[] ++ ys = ys
(x:xs) ++ ys = x: (xs ++ ys)
associativity of (++) is left as a exercise to the reader