-
Notifications
You must be signed in to change notification settings - Fork 15
/
ex10.txt
173 lines (157 loc) · 4.51 KB
/
ex10.txt
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
171
172
173
1. return x >>= f = f x
2. mx >>= return = mx
3. (mx >>= f) >>= g = mx >>= (\x -> (f x >>= g))
Definition of list monad functions:
return x = [x]
xs >>= f = [y | x <- xs, y <- f x]
List comprehension properties
Distribution law: [z | z <- (xs ++ ys)] = [z | z <- xs] ++ [z | z <- ys]
Proof
Base case:
[z | z <- ([] ++ ys)]
= {identity of ++}
[z | z <- ys]
= {identity of ++}
[] ++ [z | z <- ys]
= {property of empty list comprehension}
[z | z <- []] ++ [z | z <- ys]
Inductive case:
[z | z <- ((x:xs) ++ ys)]
= {unapplying ++}
[z | z <- ([x] ++ xs ++ ys)]
= {induction hypothesis}
[z | z <- [x]] ++ [z | z <- xs ++ ys)]
= {induction hypothesis}
[z | z <- [x]] ++ [z | z <- xs] ++ [z | z <- ys]
= {induction hypothesis}
[z | z <- [x] ++ xs] ++ [z | z <- ys]
= {applying ++}
[z | z <- (x:xs)] ++ [z | z <- ys]
List comprehension empty property: [x | x <- []] = []
List comprehension one element property: [x | x <- [y]] = [y]
List comprehension application property: [y | y <- xs] = xs
Base case:
[y | y <- []]
= {list comprehension empty property}
[]
Inductive case):
[y | y <- (x:xs)]
= {list notation}
[y | y <- ([x] ++ xs)]
= {distributivity}
[y | y <- [x]] ++ [y | y <- xs]
= {property of list comprehension}
[x] ++ [y | y <- xs]
= {induction hypothesis}
[x] ++ xs
= {applying ++}
x:xs
List comprehension apply generator: [y | x <- xs, y <- [x]] = x
Base case:
[y | x <- [], y <- [x]]
= {list comprehension empty property}
[]
Inductive case:
[y | z <- (x:xs), y <- [z]]
= {unapplying ++}
[y | z <- [x] ++ [xs], y <- [z]]
= {distributivity of ++}
[y | z <- [x], y <- [z]] ++ [y | z <- xs, y <- [z]]
= {induction hypothesis}
[y | z <- [x], y <- [z]] ++ xs
= {property of list comprehension}
[x] ++ xs
= {applying ++}
(x:xs)
Proof of Monad 1. return x >>= f = f x
return x >>= f
= {applying return}
[x] >>= f
= {applying >>=}
[y | z <- [x], y <- f z]
= {applying generator z <- [x]}
[y | y <- f x]
= {property of list comprehension}
f x
Proof of Monad 2. mx >>= return = mx
Base case:
[] >>= return
= {applying >>=}
[y | x <- [], y <- return x]
= {property of empty list comprehension}
[]
Inductive case:
(x:xs) >>= return
= {applying >>=}
[y | z <- (x:xs), y <- return z]
= {applying return}
[y | z <- (x:xs), y <- [z]]
= {unapplying ++}
[y | z <- [x] ++ xs, y <- [z]]
= {distributivity}
[y | z <- [x], y <- [z]] ++ [y | z <- xs, y <- [z]]
= {property of list comprehension}
[x] ++ [y | z <- xs, y <- [z]]
= {unapplying return}
[x] ++ [y | z <- xs, y <- return z]
= {unapplying >>=}
[x] ++ (xs >>= return)
= {induction hypothesis}
[x] ++ xs
= {applying ++}
x:xs
Proof of Monad 3. (mx >>= f) >>= g = mx >>= (\x -> (f x >>= g))
Base case:
([] >>= f) >>= g
= {applying >>=}
[y | x <- [], y <- f x] >>= g
= {property of empty list comprehension}
[] >>= g
= {applying >>=}
[y | x <- [], y <- g x]
= {property of empty list comprehension}
[]
= {identity of empty list}
[y | z <- [], y <- (\x -> (f x >>= g)) z]
= {unapplying >>=}
[] >>= (\x -> (f x >>= g))
Inductive case:
((x:xs) >>= f) >>= g
= {applying >>=}
[y | z <- (x:xs), y <- f z] >>= g
= {applying >>=}
[b | a <- [y | z <- (x:xs), y <- f z], b <- g a]
= {distributivity of ++}
[b | a <- [y | z <- [x], y <- f z], b <- g a] ++ [b | a <- [y | z <- xs, y <- f z], b <- g a]
= {property of list comprehension}
[b | a <- [y | y <- f x], b <- g a] ++ [b | a <- [y | z <- xs, y <- f z], b <- g a]
= {property of list comprehension}
[b | a <- f x, b <- g a] ++ [b | a <- [y | z <- xs, y <- f z], b <- g a]
= {unapplying >>=}
[b | a <- f x, b <- g a] ++ ([y | z <- xs, y <- f z] >>= g)
= {unapplying >>=}
[b | a <- f x, b <- g a] ++ (xs >>= f >>= g)
= {induction hypothesis}
[b | a <- f x, b <- g a] ++ (xs >>= (\w -> (f w >>= g))
= {applying >>=}
[b | a <- f x, b <- g a] ++ [y | z <- xs, y <- (\w -> (f w >>= g)) z]
= {applying \w}
[b | a <- f x, b <- g a] ++ [y | z <- xs, y <- (f z >>= g)]
= {unapplying >>=}
[b | a <- f x, b <- g a] ++ (xs >>= (f z >>= g))
= {applying >>=}
[b | a <- f x, b <- g a] ++ (xs >>= [b | a <- f z, b <- g a])
= {applying >>=}
[b | a <- f x, b <- g a] ++ [y | z <- xs, y <- [b | a <- f z, b <- g a]]
= {property of list comprehension}
[y | y <- [b | a <- f x, b <- g a]] ++ [y | z <- xs, y <- [b | a <- f z, b <- g a]]
= {unapplying generator a <- [x]}
[y | z <- [x], y <- [b | a <- f z, b <- g a])] ++ [y | z <- xs, y <- [b | a <- f z, b <- g a]]
= {distributivity of ++}
[y | z <- (x:xs), y <- [b | a <- f z, b <- g a]]
= {unapplying \w}
[y | z <- (x:xs), y <- (\w -> [b | a <- f w, b <- g a]) z]
= {unapplying >>=}
(x:xs) >>= (\w -> [b | a <- f w, b <- g a])
= {unapplying >>=}
(x:xs) >>= (\w -> (f w >>= g))