-
Notifications
You must be signed in to change notification settings - Fork 0
/
coursework2.tex
397 lines (287 loc) · 14.9 KB
/
coursework2.tex
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
\documentclass{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage[margin=1in]{geometry}
\title{CM20256 - Coursework 2}
\date{02/05/2017}
\author{Adam Jaamour (aj645)}
% ____________________________________ DOCUMENT _____________________________________
\begin{document}
% ________________________________ COMMANDS _________________________________
\newcommand{\lamb}{$\lambda$}
\newcommand{\la}{\lambda}
\newcommand{\be}{$\beta$}
\newcommand{\sa}{\quad}
\newcommand{\saq}{\, \, \, \,}
\newcommand{\equals}{\rightarrow_\beta}
\newcommand{\equalsT}{\sa \rightarrow_\beta^*\sa}
\newcommand{\tim}{\text{times}}
% _______________________________ PRESENTATION ________________________________
\pagenumbering{gobble}
\maketitle
\newpage
\pagenumbering{gobble}
\begin{Large}
\tableofcontents
\end{Large}
\newpage
\pagenumbering{arabic}
% _________________________________ PART1 __________________________________
\section{Part 1 (10\%)}
\begin{Large}
\textbf{Show that the term \textit{``[3,2,1] times 1''} $\beta$-reduces to 6:}
\begin{align*}
\saq [3,2,1] \ \tim \ 1 \ &\triangleq \saq (\underline{\la c. \la n . \ c \ 3 \ (c \ 2 \ (c \ 1 \ n)) \ \tim }) \ 1 \\
&\equals (\underline{ \la n . \ \tim \ 3 \ (\tim \ 2 \ (\tim \ 1 \ n))) \ 1} \\
&\equals \tim \ 3 \ (\tim \ 2 \ (\underline{\tim \ 1 \ 1})) \\
&\equals \tim \ 3 \ (\tim \ 2 \ (1 \times 1)) \\
&= \saq \tim \ 3 \ (\underline{\tim \ 2 \ (1)}) \\
&\equals \tim \ 3 \ (2 \times 1) \\
&= \saq \underline{\tim \ 3 \ (2)} \\
&\equals 3 \times 2 \\
&= \saq 6
\end{align*}
``$\tim \ m \ n \ \equals n \times m$'' was used for this part.\\
\newline
\end{Large}
% _________________________________ PART 2 _________________________________
\section{Part 2 (15\%)}
\begin{Large}
\textbf{Reduce \textit{``cons 3 [2,1]''} to \textit{``[3,2,1]''}:}
\begin{align*}
cons \ 3 \ [2,1] \saq &= \saq (((\underline{\la x.\la l.\la c.\la n \ c \ x \ (l \ c \ n))\ 3}) \ [2,1] \\
&\equals (\underline{\la l.\la c.\la n \ c \ 3 \ (l \ c \ n)) \ [2,1]} \\
&\equals \la c.\la n \ c \ 3 \ (\underline{[2,1]} \ c \ n) \\
&\equiv_\alpha \, \, \la c.\la n \ c \ 3 \ (\underline{\la d.\la m. \ d \ 2 \ (d \ 1 \ m) \ c} \ n) \\
&\equals \la c.\la n \ c \ 3 \ (\underline{\la m. \ c \ 2 \ (c \ 1 \ m) \ n}) \\
&\equals \la c.\la n \ c \ 3 \ ( c \ 2 \ (c \ 1 \ n)) \\
&= \saq [3,2,1]
\end{align*}
``$cons \triangleq \la x.\la .l.\la x.\la n. \ c \ x \ (l \ c \ n)$'' was used for this part.
\end{Large}
\newpage
% _________________________________ PART 3 _________________________________
\section{Part 3 (15\%)}
\begin{Large}
\textbf{Define terms \textit{``head''} and \textit{``empty''} such that:}
\begin{align*}
head \sa [ N, ...] &\equalsT N\\
empty \sa [\sa] &\equalsT true\\
empty \sa [ N, ...] &\equalsT false
\end{align*}
\newline
The \lamb -term found for \textit{``head''} is:
\begin{equation*}
head \triangleq \la l.l \ (\la .x.\la y.x) \ n
\end{equation*}
\newline
The \lamb -term found for \textit{``empty''} is:
\begin{equation*}
empty \triangleq \la l.l \ (\la .a.\la b.false) \ true
\end{equation*}
\newline
Proof by example using the \lamb -terms found for \textit{``head''} and \textit{``empty''}:
\begin{align*}
head \ [2,1] \ &\triangleq \saq \underline{(\la l.l \ (\la .x.\la y.x) \ n) \ \la c.\la n. \ c \ 2 \ (c \ 1 \ n)} \\
&\equals (\underline{\la c.\la n. \ c \ 2 \ (c \ 1 \ n)) \ (\la .x.\la y.x}) \ n \\
&\equals \underline{\la n.\ ((\la .x.\la y.x) \ 2) \ (((\la .x.\la y.x) \ 1) \ n) \ n} \\
&\equals ((\la .x.\la y.x) \ 2) \ ((\underline{(\la .x.\la y.x) \ 1}) \ n) \\
&\equals ((\la .x.\la y.x) \ 2) \ ((\underline{\la y.1 ) \ n}) \\
&\equals ((\underline{\la .x.\la y.x) \ 2}) \ 1 \\
&\equals (\underline{\la y.2) \ 1} \\
&\equals 2
\end{align*}
\begin{align*}
empty \ [ \ ] \ &\triangleq \saq \underline{(\la l.l \ (\la .a.\la b.false) \ true) \ \la c.\la n.n} \\
&\equals \underline{\la c.\la n.n \ (\la .a.\la b.false)} \ true \\
&\equals (\underline{\la n.n) \ true} \\
&\equals true
\end{align*}
\begin{align*}
empty \ [2,1] \ &\triangleq \saq \underline{(\la l.l \ (\la .a.\la b.false) \ true) \ \la c.\la n. \ c \ 2 \ (c \ 1 \ n)} \\
&\equals \underline{(\la c.\la n. \ c \ 2 \ (c \ 1 \ n)) \ (\la .a.\la b.false)} \ true \\
&\equals \underline{(\la n. \ ((\la .a.\la b.false) \ 2) \ (((\la .a.\la b.false) \ 1) \ n)) \ true} \\
&\equals ((\la .a.\la b.false) \ 2) \ ((\underline{(\la .a.\la b.false) \ 1}) \ true) \\
&\equals ((\la .a.\la b.false) \ 2) \ ((\underline{\la b.false) \ true}) \\
&\equals (\underline{(\la .a.\la b.false) \ 2}) \ false \\
&\equals (\underline{\la b.false) \ false} \\
&\equals false
\end{align*}
\newline
``$ \ [ \ ] \ = \ \la c.\la n.n$'' , where $[ \ ]$ is the empty list, was used for this part.
\end{Large}
\newpage
% _________________________________ PART 4 _________________________________
\section{Part 4 (35\%)}
\begin{Large}
\subsection{List represented by $L_m$}
\textbf{What does $L_m$ = \lamb c.\lamb n.$L'_m$ (for any m) represent?}\\
The list represented by the term $L_m$ corresponds to the list of descending natural numbers from $m$ to $1$, excluding 0, such as m $\in \ \mathbb{Z}^*$.
\newline
Proof by example, using $m \ = \ 4$:
\begin{align*}
L_4 &= \la c.\la n. \ L_4^\prime \\
&= \la c.\la n. \ c \ 4 \ ( L_3^\prime ) \\
&= \la c.\la n. \ c \ 4 \ ( c \ 3 \ (L_2^\prime )) \\
&= \la c.\la n. \ c \ 4 \ ( c \ 3 \ (c \ 2 \ (L_1^\prime ))) \\
&= \la c.\la n. \ c \ 4 \ ( c \ 3 \ (c \ 2 \ (c \ 1 \ (L_0^\prime )))) \\
&= \la c.\la n. \ c \ 4 \ ( c \ 3 \ (c \ 2 \ (c \ 1 \ n))) \\
&= [5,4,3,2,1]
\end{align*}
\newline
\subsection{Inductive proof}
\textbf{Prove by induction on $m$ that $L'_m$ [ times/c , 1/n ] $\equals^*$ m!}\\
\underline{Base case}: $m=0$
\begin{align*}
L_0^\prime \ [\ \tim / c \ , \ 1/n \ ] &= \saq n \ [\ \tim / c \ , \ 1/n \ ] \\
&\equals 1
\end{align*}
Since $0! = 1$, the base case is therefore true.
\newline
\underline{Inductive case}: \\
The inductive hypothesis is $L'_m$ [ times/c , 1/n ] $\equals^*$ m!, and it is assumed to be true.
If it can be proved with $m+1$, then $L'_m$ [ times/c , 1/n ] $\equals^*$ m! will be true for all $m$.
For $m+1$:
\begin{align*}
&L_{m+1}^\prime \ [\ \tim / c \ , \ 1/n \ ] \\
&= \saq (c \ (m+1) \ L_{m-1+1}^\prime ) \ [\ \tim / c \ , \ 1/n \ ] \\
&\equals \tim \ (m+1) \ ( L_m^\prime \ [\ \tim / c \ , \ 1/n \ ]) \\
&= \saq \tim \ (m+1) \ m! \\
&\equals (m+1) \times m! \\
&= \saq (m+1)!
\end{align*}
On the second line of solution, $L_m^\prime \ [\ \tim / c \ , \ 1/n \ ]$ is substitued with $m!$ throughout the inductive hypothesis. \\
``$\tim \ m \ n \ \equals n \times m$'' was also used for this part. \\
True for $m+1$, therefore $L'_m$ [ times/c , 1/n ] $\equals^*$ is true for all $m$. \\
\newline
\textbf{Based on previous answer, prove that $L_m$ times 1 $\equals^*$ m!}
\begin{align*}
L_m^\prime \ \tim \ 1 &= (\la c.\la n. \ L_m^\prime \ \tim) \ 1 \\
&= \la n. \ L_m^\prime \ [\tim \ / \ c] \ 1 \\
&= L_m^\prime \ [\tim /c \ , \ 1/n] \\
&= m!
\end{align*}
Previous proof that $L'_m$ [ times/c , 1/n ] $\equals^*$ m! used to find $m!$
\end{Large}
\newpage
% _________________________________ PART 5 _________________________________
\section{Part 5 (25\%)}
\begin{Large}
\subsection{foldr}
\textbf{Give a \lamb -term corresponding to Haskell function \textit{foldr} such as:}
\begin{equation*}
foldr \ f \ u \ [N_1 \ ,..., \ N_k] \equalsT f \ N_1\ (f \ N_2 \ ( \ ... \ (f \ N_k \ u)))
\end{equation*}
\newline
The \lamb -term found for \textit{``foldr''} is:
\begin{equation*}
foldr \triangleq \la a.\la b.\la l.(l \ a \ b)
\end{equation*}
\newline
The function was created by analyzing the property it should have, which was provided by the coursework specification. Looking at the term,
it can be seen that $foldr$ takes three inputs: the function $f$, the accumulator $u$ and the list $[N_1,...,N_k]$.
In Haskell, $foldr$ is defined as the function that combines the accumulator $u$ to each list element using the function $f$, starting from the right and moving to the left.\\
Looking more closely at the resulting term $f \ N_1\ (f \ N_2 \ ( \ ... \ (f \ N_k \ u)))$, which is obtained after performing the beta reductions,
it is obvious that the term follows the same pattern as the one of a list: $c \ N_1 (c \ N_2 ( ... ( c \ N_k \ n)...))$,
with $c$ being replaced by $f$ and $n$ being replaced by $u$. \\
This is achieved by substituting the inputs $a$, $b$, and $l$ from the term found for $foldr$ with their corresponding inputs
to get a term with the list first, followed by $f$ and $u$, of the form $ ([N_1,...,N_k] \ f) \ u $.
After extending the list, beta-reductions can be perfomed on the $cons$ and the $nil$ of the extended list,
thus replacing each $cons$ by $f$ and each $nil$ by $u$, which corresponds to the term $foldr$ should beta-reduce to.\\
\newpage
\underline{Proof by example using the \lamb -terms found for \textit{``foldr''}:}\\
\underline{non-empty list:}
\newline
$foldr \ f \ u \ [3,2,1]$ should return $f \ 3 \ (f \ 2 \ (f \ 1 \ u))$
\begin{align*}
foldr \ f \ u \ [3,2,1] &\triangleq \saq ((\underline{\la a.\la b.\la l.(l \ a \ b) \ f}) \ u) \ [3,2,1] \\
&\equals (\underline{\la b.\la l.(l \ f \ b) \ u}) \ [3,2,1] \\
&\equals \underline{\la l.(l \ f \ u) \ [3,2,1]} \\
&\equals ([3,2,1] \ f) \ u \\
&= \saq (\underline{\la c.\la n. \ c \ 3 \ (c \ 2 \ (c \ 1 \ n)) \ f}) \ u \\
&\equals \underline{\la n. \ f \ 3 \ (f \ 2 \ (f \ 1 \ n)) \ u} \\
&\equals f \ 3 \ (f \ 2 \ (f \ 1 \ u))
\end{align*}
\newline
\underline{empty list:}
\newline
$foldr \ f \ u \ [ \ ]$ should return $u$, since $nil$ is replaced by the accumulator $u$:
\begin{align*}
foldr \ f \ u \ [ \ ] &\triangleq \saq ((\underline{\la a.\la b.\la l.(l \ a \ b) \ f}) \ u) \ [ \ ] \\
&\equals (\underline{\la b.\la l.(l \ f \ b) \ u}) \ [ \ ] \\
&\equals \underline{\la l.(l \ f \ u) \ [ \ ]} \\
&\equals ([ \ ] \ f) \ u \\
&= \saq (\underline{(\la c.\la n. \ n) \ f} )\ u \\
&\equals (\underline{\la n.n) \ u} \\
&\equals u
\end{align*}
\newpage
% ________
\subsection{map}
\textbf{Give a \lamb -term corresponding to Haskell function \textit{map} such as:}
\begin{equation*}
map \ f \ [N_1\ ,..., \ N_k] \equalsT [f \ N_1 \ , f \ N_2 \ ,...,f \ N_k]
\end{equation*}
\newline
The \lamb -term found for \textit{``map''} is:
\begin{equation*}
map \triangleq \la a.\la l.\la c. \ l \ (\la x.\ c \ a \ x)
\end{equation*}
\newline
Looking at the property $map$ should have (given by the coursework specification), the objective is to build a function which
applies a function $f$ to each element of a list. This also corresponds to the definition of the $map$ function in Haskell.
Looking at the term, $map$ takes two inputs: the function $f$ which needs to be applied to each element of the list $[N_1,...,N_k]$, which is the second input.\\
The resulting term corresponds to a list where $f$ is applied to each of its elements. Consequently, the function $map$ should build
a list of the type $\la c.\la n.c \ N_1 (c \ N_2 ( ... ( c \ N_k \ n)...))$, inserting $f$ between each $c$ and $N$ for each element of
the list to get a term of the form $\la c.\la n.c \ N_1 (c \ N_2 ( ... ( c \ N_k \ n)...))$.\\
This can be done by substituting inputs $a$ and $l$ from the term found for $map$ with their corresponding inputs to get a term starting with $\la c$,
followed by the list and ending with a term $(\la x.c \ f \ x)$ which keeps the form $(c f x)$ when beta-reduced, with $x$ corresponding to the rest of the list.
Performing beta-reductions from left to right builds a list with $f$ between $c$ and $N$, until the end of the list is reached.
\newpage
\underline{Proof by example using the \lamb -terms found for \textit{``map''}:}\\
\underline{non-empty list:}
\newline
$map \ f \ [3,2,1]$ should return $ [f \ 3 \ , \ f \ 2 \ , \ f \ 1]$
\begin{align*}
map \ f \ [3,2,1] &\triangleq \saq (\underline{\la a.\la l.\la c. \ l \ (\la x.\ c \ a \ x) \ f}) \ [3,2,1] \\
&\equals \underline{\la l.\la c. \ l \ (\la x.\ c \ f \ x) \ [3,2,1]} \\
&\equals \la c. \ \underline{[3,2,1]} \ (\la x.\ c \ f \ x) \\
&\equiv_\alpha \, \, \la c. \ (\underline{\la d.\la n. \ d \ 3 \ (d \ 2 \ (d \ 1 \ n)) \ (\la x.\ c \ f \ x)}) \\
&\equals \la c. \ (\la n. \ (\underline{\la x.\ c \ f \ x) \ 3} \ ((\la x.\ c \ f \ x) \ 2 \ ((\la x.\ c \ f \ x) \ 1 \ n))) \\
&\equals \la c. \ (\la n. \ c\ f \ 3 \ ((\underline{\la x.\ c \ f \ x) \ 2} \ ((\la x.\ c \ f \ x) \ 1 \ n))) \\
&\equals \la c. \ (\la n. \ c\ f \ 3 \ (c \ f \ 2 \ ((\underline{\la x.\ c \ f \ x) \ 1 \ n}))) \\
&\equals \la c. \ (\la n. \ c\ f \ 3 \ (c \ f \ 2 \ (c \ f \ 1 \ n))) \\
&= \saq \la c.\la n. \ c\ f \ 3 \ (c \ f \ 2 \ (c \ f \ 1 \ n))) \\
&= [\ f \ 3 \ , \ f \ 2 \ , \ f \ 1\ ]
\end{align*}
\underline{empty list:}
\newline
$map \ f \ [ \ ]$ should return the empty-list $[ \ ]$
\begin{align*}
map \ f \ [ \ ] &\triangleq \saq (\underline{\la a.\la l.\la c. \ l \ (\la x.\ c \ a \ x) \ f}) \ [ \ ] \\
&\equals \underline{\la l.\la c. \ l \ (\la x.\ c \ f \ x) \ [ \ ]} \\
&\equals \la c. \ \underline{[ \ ]} \ (\la x.\ c \ f \ x) \\
&\equiv_\alpha \, \, \la c. \ (\underline{\la d.\la n.n) \ (\la x.\ c \ f \ x}) \\
&\equals \la c.\la n.n \\
&= \saq [ \ ]
\end{align*}
\newpage
% ________
\subsection{infinite list}
\textbf{Give a \lamb -term corresponding to infinite list $[0,1,2,...]$:}\\
The infinite list can be written as $[0,1,2,3,...]$.\\
It is considered to start from the point 0 onwards.\\
To begin with, the infinite list can be written as $infinite \triangleq infinite \ m$.
There are two cases to take into account: when $m=0$, and $m \neq 0$.\\
Combined with the term for a finite list $\la c.\la n.c \ N_1 (c \ N_2 ( ... ( c \ N_k \ n)...))$, a first idea of the logic of the infinite list can be written:\\
$if \ m=0 \ then \ (\la c.\ c \ m \ infinite (succ \ m))$\\
$else \ if \ m \neq 0 \ (c \ m ( infinite (succ \ m))) $\\
Note that the n is not included since the empty list is impossible in an infinite list.\\
Using the logic stated previously, the term for $infinite$ can be written:\\
$infinite \ = \ \la m. \ ifthen(iszero \ m)(\la c.\ c \ m \ infinite (succ \ m)) \ (c \ m ( infinite (succ \ m)))$\\
where: $ifthen \triangleq \la a.\la x.\la y. \ (a \ x \ y)$\\
where: $iszero \triangleq \la n.n\ (\la w. false \ true)$\\
where $succ \triangleq \la n.\la f . \la x. \ f(n\ f \ x)$\\
To end, the Y-combinator is applied to this term to get an infinitely recursive list.
\end{Large}
\end{document}