-
Notifications
You must be signed in to change notification settings - Fork 0
/
nats.tex
1216 lines (1125 loc) · 31.2 KB
/
nats.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\documentclass{beamer}
\usepackage{amssymb}
\usepackage{fancyvrb}
\usepackage{stmaryrd}
\usepackage{graphicx}
\usefonttheme{serif}
\newcommand{\Nat}{\mathbb{N}}
\SaveVerb{zer}=Z=
\SaveVerb{suc}=Succ n=
\SaveVerb{thr}=Succ (Succ (Succ Z))=
\SaveVerb{ele}=Succ (Succ (Succ (Succ (Succ (Succ (Succ (Succ (Succ (Succ (Succ Z))))))))))=
\SaveVerb{enn}=n=
\title{What About the Natural Numbers?}%\texorpdfstring{$\mathbb{N}$}}
\subtitle{JMCT}
\date{}
\usetheme{jmct}
\usepackage{calc}
\newcommand{\textover}[3][l]{%
% #1 is the alignment, default l
% #2 is the text to be printed
% #3 is the text for setting the width
\makebox[\widthof{#3}][#1]{#2}%
}
\newcommand{\blueit}[1]{%
{\color{dark-lucid-blue}#1}%
}
\newcommand{\blueite}[1]{%
\blueit{\emph{#1}}%
}
\newcommand{\myquote}[3]{
``#1''
\vspace{3pt}
\hrule
\begin{flushright}
--- \blueit{\emph{#2}}, \emph{#3}
\end{flushright}
}
\begin{document}
\frame {
\titlepage
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Intro
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\frame {
\centering {\fontsize{88}{68}\selectfont $\mathbb{N}$\phantom{?}}
}
\frame {
\centering {\fontsize{88}{68}\selectfont $\mathbb{N}$?}
}
\frame {
\myquote{Some thirty years into the history of
machine-independent programming language design, the treatment of numbers
is still problematic.}{Colin Runciman}{1989}
}
\frame {
\myquote{Some \textover[c]{\blueit{sixty}}{thirty} years into the history of
machine-independent programming language design, the treatment of numbers
is still problematic.}{Me}{just now}
}
\frame{
\frametitle{Main takeaway}
The number system we use should relate to the structures of the problem
we're solving.
}
\frame{
\frametitle{Main takeaway}
For some domains, the use of Reals\footnote{or their approximation via
\texttt{Float}s} may be appropriate:
\begin{itemize}
\item<2 -> e.g. physics calculations involving volume, speed, or mass
\end{itemize}
}
\frame{
\frametitle{Main takeaway}
For \emph{many} problems \blueit{Integers} are appropriate:
\begin{itemize}
\item<2 -> Fixed-precision DSP
\item<3 -> Bank account balance :'(
\end{itemize}
}
\frame{
\frametitle{Main takeaway}
Runciman's argument:\newline
\hangindent=0.7cm
For many of the discrete structures involved in the
day-to-day practice of programming, the \blueit{natural} numbers are
the most appropriate number system.
}
\frame{
\frametitle{How?}
In the process of exploring the Natural Numbers, we'll be developing an API.
As we progress we'll see how different representations affect our API.
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Outline
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%* Overview of the $\mathbb{N}$s themselves
%* Programming with \texttt{Nat}
%* Arithmetic with \texttt{Nat} and properties we care about
%* How does \texttt{Nat} influence API design?
%* Implementation Concerns
%* Beyond \texttt{Nat}
%* Conclude
%%%%%%%
% Goals
%
\frame{
\frametitle{\#goals}
\begin{enumerate}
\item<2 -> Show you that the [lazy?] $\mathbb{N}$s are \blueite{Good}
and \blueite{Proper}
\item<3 -> Demonstrate that even \emph{simple} choices of types
for an API have deep consequences
\item<4 -> Have you asking ``What about the Natural Numbers?''
next time you create an API.
\end{enumerate}
}
%%%%%%%
% Actual Outline
%
\frame{
\frametitle{Shape of things to come}
\begin{enumerate}
\item<2 -> Overview of the $\mathbb{N}$s themselves
\item<3 -> Programming with \texttt{Nat}
\item<4 -> Arithmetic with \texttt{Nat} and properties we care about
\item<5 -> How does \texttt{Nat} influence API design?
\item<6 -> Implementation concerns
\item<7 -> Beyond \texttt{Nat}
\item<8 -> Conclude
\end{enumerate}
}
%%%%%%%
% What are they?
%
% We actually see them here! We just counted things, _that's_
% the natural number.
\frame{
\frametitle{Let's start}
\begin{enumerate}
\item \blueit{Overview of the $\mathbb{N}$s themselves}
\item Programming with \texttt{Nat}
\item Arithmetic with \texttt{Nat} and properties we care about
\item How does \texttt{Nat} influence API design?
\item Implementation concerns
\item Beyond \texttt{Nat}
\item Conclude
\end{enumerate}
}
\frame{
\frametitle{What \emph{are} they?}
The \emph{Natural} numbers have a few definitions:
\begin{enumerate}
\item<2-> Set Theoretic
\item<3> Peano Axioms
\end{enumerate}
}
%%%%%%%
% Set Theoretic
%
\frame{
\frametitle{\textbf{\underline{Set}}ting Yourself Up For Success}
}
\frame{
\frametitle{\textbf{\underline{Set}}ting Yourself Up For Success}
Several possible Set-theoretic definitions, von Neumann proposed the
following:
\begin{itemize}
\item<2 -> $0 = \{\}$
\item<3 -> $1 = 0 \cup \{0\}$
\item<4 > $2 = 1 \cup \{1\}$
\end{itemize}
}
\frame{
\frametitle{\textbf{\underline{Set}}ting Yourself Up For Success}
Several possible Set-theoretic definitions, von Neumann proposed the
following:
\begin{itemize}
\item<1 -> $0 = \{\}$
\item<1 -> $1 = 0 \cup \{0\} = \{0\} = \{\{\}\}$
\item<1 > $2 = 1 \cup \{1\} = \{0,1\} = \{\{\},\{\{\}\}\}$
\end{itemize}
}
%%%%%%%
% Peano Axioms
%
% That's pretty complicated
%
% Turns our that there is a simpler way
\frame{
\frametitle{\textbf{\underline{Set}}ting Yourself Up For Success}
\centering {\huge oof}
}
\frame{
\frametitle{Setting Yourself Up For \textbf{\underline{Succ}}ess}
In 1889 Giuseppe Peano published\par
\vspace{0.5cm}
``The principles of arithmetic presented by a new method''
}
\frame{
\frametitle{Setting Yourself Up For \textbf{\underline{Succ}}ess}
The two axioms we care about most (right now) are simple enough:
\begin{itemize}
\item <2 -> $0 \in \mathbb{N}$
\item <3 -> $\forall n \in \Nat\ldotp S(n) \in \Nat$
\end{itemize}
}
%%%%%%%
% Data!
%
\frame{
\frametitle{Sign post}
\begin{enumerate}
\item Overview of the $\mathbb{N}$s themselves
\item \blueit{Programming with \texttt{Nat}}
\item Arithmetic with \texttt{Nat} and properties we care about
\item How does \texttt{Nat} influence API design?
\item Implementation concerns
\item Beyond \texttt{Nat}
\item Conclude
\end{enumerate}
}
\frame{
\frametitle{Setting Yourself Up For \textbf{\underline{Succ}}ess}
Okay, but we're concerned with the \emph{practice} of \emph{programming} \dots
}
\begin{frame}[fragile]
\frametitle{Setting Yourself Up For \textbf{\underline{Succ}}ess}
\begin{figure}
{\color{dark-gray}
\centering
\begin{BVerbatim}
type Nat where
Z : Nat
Succ : Nat -> Nat
\end{BVerbatim}
}
\end{figure}
\end{frame}
\begin{frame}[fragile]
\frametitle{Setting Yourself Up For \textbf{\underline{Succ}}ess}
Now we can easily represent any $\Nat$ we want!
{\color{dark-gray}
\begin{align*}
\hfill \\
\hfill
\end{align*}
}
\end{frame}
\begin{frame}[fragile]
\frametitle{Setting Yourself Up For \textbf{\underline{Succ}}ess}
Now we can easily represent any $\Nat$ we want!
{\color{dark-gray}
\begin{align*}
\llbracket \UseVerb{zer} \rrbracket &= \blueit{0}\\
\llbracket \UseVerb{suc} \rrbracket &= \blueit{1~+~} \llbracket \UseVerb{enn} \rrbracket
\end{align*}
}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Construction
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\frame{
\frametitle{Talk over?}
}
\frame{
\frametitle{Talk over?}
This is all very nice and elegant, but the ergonomics \emph{suck}
}
\begin{frame}[fragile]
\frametitle{RSI risk}
Even just typing this slide made my RSI flare up:
\begin{itemize}
\item $\blueit{3} \Rightarrow $ {\color{dark-gray}\UseVerb{thr}}
\item $\blueit{11} \Rightarrow $ {\color{dark-gray}\UseVerb{ele}}
\end{itemize}
\end{frame}
%%%%%%%
% Our friend, the list!
%
\frame{
\frametitle{Spoonful of sugar}
What do we do for other types?
}
\begin{frame}[fragile]
\frametitle{Spoonful of sugar}
\begin{figure}
{\color{dark-gray}
\centering
\begin{BVerbatim}
type List elem where
[] : List elem
(::) : elem -> List elem -> List elem
\end{BVerbatim}
}
\end{figure}
\end{frame}
\begin{frame}[fragile]
\frametitle{Spoonful of sugar}
Lists are flexible and easy to reason about, but they have the same problem!
\begin{itemize}
\item<2 -> {\color{dark-gray} \verb-type String = List Char-}
\item<3 > {\color{dark-gray} \verb-initials = 'P' :: ('W' :: ('L' :: []))-}
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Spoonful of sugar}
Because of this ubiquity of lists, compiler writers quickly came up with syntactic sugar for them:
\begin{itemize}
\item<2 -> {\color{dark-gray} \verb-"PWL"- $\Rightarrow$ \verb-'P' :: ('W' :: ('L' :: []))-}
\item<3 > {\color{dark-gray} \verb-[1..3]- $\Rightarrow$ \verb-1 :: (2 :: (3 :: []))-}
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Spoonful of sugar}
Similarly, we can implement syntactic sugar for the natural numbers:
\begin{itemize}
\item<2 -> {\color{dark-gray} \verb-3- $\Rightarrow$ \UseVerb{thr}}
\end{itemize}
\end{frame}
%%%%%%%
% We still maintain our reasoning power
%
\frame{
\frametitle{Spoonful of sugar}
We lose nothing with the syntactic sugar, we can still pattern match on naturals and
retain all of our inductive reasoning.
}
\begin{frame}[fragile]
\frametitle{Natural usage}
\begin{figure}
{\color{dark-gray}
\centering
\begin{BVerbatim}
... if length xs <= 5
then ...
else ...
\end{BVerbatim}
}
\end{figure}
\end{frame}
\begin{frame}[fragile]
\frametitle{Pattern Matching still available...}
\begin{figure}
{\color{dark-gray}
\centering
\begin{BVerbatim}
(<=) : Nat -> Nat -> Bool
Z _ = True
(Succ _) Z = False
(Succ x) (Succ y) = x <= y
\end{BVerbatim}
}
\end{figure}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Arithmetic
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\frame{
\frametitle{Sign post}
\begin{enumerate}
\item Overview of the $\mathbb{N}$s themselves
\item Programming with \texttt{Nat}
\item \blueit{Arithmetic with \texttt{Nat} and properties we care about}
\item How does \texttt{Nat} influence API design?
\item Implementation concerns
\item Beyond \texttt{Nat}
\item Conclude
\end{enumerate}
}
\frame{
\frametitle{Reading, Writing and ...}
\begin{enumerate}
\item<2 -> Programmers expect some arithmetic `out of the box'
when dealing with numbers.
\item<3 -> At the very least they expect \blueit{$+$}, \blueit{$-$}, \blueit{$\times$}, \blueit{$\div$}
\end{enumerate}
}
\frame{
\frametitle{Real data structures}
When programming with the discrete structures which are common in programming,
there is a correspondence between the operations on numbers and the operations
on the data structures.
\begin{enumerate}
\item<2 -> Think `array indices', or `size'
\item<3 -> What would a \blueite{negative} size mean?
\end{enumerate}
}
\frame{
\frametitle{Who would even do that?}
}
\frame{
\frametitle{Who would even do that?}
\begin{figure}
\centering
\includegraphics[width=0.5\textwidth]{figs/length.png}
\caption{lol}
\end{figure}
}
\frame{
\frametitle{Exceptional negatives}
Think of how many APIs return an ``\blueit{\texttt{Int}}''.
\begin{enumerate}
\item <2> How many of these APIs only use the negative numbers to signal errors?
\end{enumerate}
}
\frame{
\frametitle{What do we want?}
If we think a bit about arithmetic we may conclude the following:
\begin{enumerate}
\item<2 -> Ideally, our operators would be \blueite{total}
\item<3 -> When possible, we want our operators to be \blueite{closed}
\end{enumerate}
}
\begin{frame}[fragile]
\frametitle{Why?}
These properties, when combined, allow us to be confident that when we
operate on two \verb-Nat-s, we get another \verb-Nat-.
\begin{enumerate}
\item<2 -> This isn't true for arithmetic over all number systems (nor should it be!)
\item<3 -> Many languages fail even where it should be!
\end{enumerate}
\end{frame}
\frame{
\frametitle{Totality}
Our functions being \blueite{total} gives us confidence that for any input, we
get a result.
}
\frame{
\frametitle{Closure}
Our functions being \blueite{closed} means that the result values lie within the
same number system as their arguments.
}
\frame{
\frametitle{What do we want? (part 2)}
\myquote{The aim is a total closed system of arithmetic with results that
can be safely interpreted in the context of the discrete
structures in general programming}{Colin Runciman}{1989}
}
\frame{
\frametitle{Back to arithmetic}
Addition and Multiplication present no difficulties.
}
\frame{
\frametitle{Back to arithmetic}
What about Subtraction?
}
\begin{frame}[fragile]
\frametitle{Don't wait, saturate}
\end{frame}
\begin{frame}[fragile]
\frametitle{Don't wait, saturate}
\begin{figure}
{\color{dark-gray}
\centering
\begin{BVerbatim}
(.-.) : Nat -> Nat -> Nat
n .-. Z = n
Z .-. _ = Z
(Succ n) .-. (Succ m) = n .-. m
\end{BVerbatim}
}
\end{figure}
\end{frame}
\frame{
\frametitle{Relate back to data structures}
}
\begin{frame}[fragile]
\frametitle{Relate back to data structures}
\begin{figure}
{\color{dark-gray}
\centering
\begin{BVerbatim}
drop : Nat -> List a -> List a
drop Z xs = xs
drop _ [] = []
drop (Succ n) (x::xs) = drop n xs
\end{BVerbatim}
}
\end{figure}
\end{frame}
\begin{frame}[fragile]
\frametitle{Relate back to data structures}
We \emph{want} a correspondence between operations on data structures and
on numbers:
\begin{figure}
{\color{dark-gray}
\centering
\begin{BVerbatim}
length (drop n xs) === length xs .-. n
\end{BVerbatim}
}
\end{figure}
\end{frame}
\frame{
\frametitle{Relate back to data structures}
These sorts of correspondences are what we use (often in our head) when
programming or refactoring.
}
\frame{
\frametitle{A divisive issue}
Unlike Subtraction, division is \emph{already} closed over Natural Numbers
}
\frame{
\frametitle{A divisive issue}
Unlike Subtraction, division is \emph{already} closed over Natural Numbers
(for the cases for which it is defined!)
}
\frame{
\frametitle{Back to square zero}
Some mathematicians define the Natural Numbers as starting from One! Would
that save us from this issue?
}
\frame{
\frametitle{Back to square zero}
Maybe, but then we'd lose the important correspondence with real data
structures.
}
\frame{
\frametitle{Quick digression}
}
\frame{
\frametitle{Quick digression}
Zero is not nothing!
}
\frame{
\frametitle{Two solutions}
Runciman proposes two solutions to the `division by zero' problem:
\begin{enumerate}
\item<2 -> based on viewing division on $\mathbb{N}$s as `slicing'
\item<3 -> based on using lazy \texttt{Nat}s
\end{enumerate}
}
\frame{
\frametitle{Division as slicing}
Think of dividing \texttt{x} by \texttt{y} as cutting \texttt{x} in
\texttt{y} places.
}
\begin{frame}[fragile]
\frametitle{Division as slicing}
We can write a total division, \texttt{//}, in terms of a partial (fails when dividing by zero)
division, \texttt{/}:
\begin{figure}
{\color{dark-gray}
\centering
\begin{BVerbatim}
\end{BVerbatim}
}
\end{figure}
\end{frame}
\begin{frame}[fragile]
\frametitle{Division as slicing}
We can write a total division, \texttt{//}, in terms of a partial (fails when dividing by zero)
division, \texttt{/}:
\begin{figure}
{\color{dark-gray}
\centering
\begin{BVerbatim}
x // y = x / (Succ y)
\end{BVerbatim}
}
\end{figure}
\end{frame}
\frame{
\frametitle{Umm...}
We get one intuitive property
\begin{itemize}
\item<2 -> Slicing zero times gets you the original thing back
\end{itemize}
}
\frame{
\frametitle{... that's wrong}
At the cost of it being \emph{incorrect} at every other \texttt{Nat}
}
\begin{frame}[fragile]
\frametitle{Let's fix it}
We get back correctness by subtracting $1$ from the divisor before passing it
\texttt{//}
\begin{figure}
{\color{dark-gray}
\centering
\begin{BVerbatim}
x ./. y = x // (y .-. 1)
\end{BVerbatim}
}
\end{figure}
\end{frame}
\frame{
\frametitle{You coward!}
}
\frame{
\frametitle{You coward!}
In a sense we've only side-stepped the problem!
\begin{itemize}
\item<2 -> If you think this is the lazy solution...
\end{itemize}
}
\frame{
\frametitle{Even lazier}
Runciman proposes another solution to this problem:
\begin{itemize}
\item<2 -> Lazy Natural Numbers
\end{itemize}
}
\begin{frame}[fragile]
\frametitle{Lazy \texttt{Nat}s}
If we're in a lazy language we can have infinite structures!
\begin{figure}
{\color{dark-gray}
\centering
\begin{BVerbatim}
\end{BVerbatim}
}
\end{figure}
\end{frame}
\begin{frame}[fragile]
\frametitle{Go infinity...}
If we're in a lazy language we can have infinite structures!
\begin{figure}
{\color{dark-gray}
\centering
\begin{BVerbatim}
infinity = Succ infinity
\end{BVerbatim}
}
\end{figure}
\end{frame}
\begin{frame}[fragile]
\frametitle{Back to division}
\begin{figure}
{\color{dark-gray}
\centering
\begin{BVerbatim}
x ./. 0 = infinity
x ./. y = x / y
\end{BVerbatim}
}
\end{figure}
\end{frame}
\begin{frame}[fragile]
\frametitle{No cheating}
\begin{figure}
{\color{dark-gray}
\centering
\begin{BVerbatim}
\end{BVerbatim}
}
\end{figure}
\end{frame}
\begin{frame}[fragile]
\frametitle{No cheating}
\begin{figure}
{\color{dark-gray}
\centering
\begin{BVerbatim}
x ./. y = if x < y
then 0
else Succ ((x .-. y) ./. y)
\end{BVerbatim}
}
\end{figure}
\end{frame}
\begin{frame}[fragile]
\frametitle{More power to you}
Exponentiation is not closed over the Integers, but over Naturals it is!
\begin{figure}
{\color{dark-gray}
\centering
\begin{BVerbatim}
\end{BVerbatim}
}
\end{figure}
\end{frame}
\begin{frame}[fragile]
\frametitle{More power to you}
Exponentiation is not closed over the Integers, but over Naturals it is!
\begin{figure}
{\color{dark-gray}
\centering
\begin{BVerbatim}
pow n 0 = 1
pow n (Succ p) = n * pow n p
\end{BVerbatim}
}
\end{figure}
\end{frame}
\frame{
\frametitle{Laziness, revisited}
Let's not start a war here
}
\frame{
\frametitle{Laziness, revisited}
Infinite values also allow you to avoid `cheating' in some standard algorithms
}
\frame{
\frametitle{Laziness, revisited}
How many times have you seen \texttt{inf = 999999} in a graph algorithm?
}
\begin{frame}[fragile]
\frametitle{Save yourself some computation}
Are there more than $10$ people in your company?
\begin{figure}
{\color{dark-gray}
\centering
\begin{BVerbatim}
\end{BVerbatim}
}
\end{figure}
\end{frame}
\begin{frame}[fragile]
\frametitle{Save yourself some computation}
Are there more than $10$ people in your company?
\begin{figure}
{\color{dark-gray}
\centering
\begin{BVerbatim}
... expensive > 10 ...
\end{BVerbatim}
}
\end{figure}
\end{frame}
\frame{
\frametitle{Laziness, revisited}
Lazy numbers let us compare the sizes of things without necessarily
fully computing the size!
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% API
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\frame{
\frametitle{Sign post}
\begin{enumerate}
\item Overview of the $\mathbb{N}$s themselves
\item Programming with \texttt{Nat}
\item Arithmetic with \texttt{Nat} and properties we care about
\item \blueit{How does \texttt{Nat} influence API design?}
\item Implementation concerns
\item Beyond \texttt{Nat}
\item Conclude
\end{enumerate}
}
\frame{
\frametitle{APIs}
We've alredy defined an API for arithmetic, with various tradeoffs.
\begin{enumerate}
\item<2 -> Now let's define some non-arithmetic functions and see how
the \texttt{Nat}s guide us
\end{enumerate}
}
\frame{
\frametitle{Size}
Size of structures is very straightforward
}
\begin{frame}[fragile]
\frametitle{Size}
\begin{figure}
{\color{dark-gray}
\centering
\begin{BVerbatim}
size : List elem -> Nat
size [] = Z
size (x::xs) = Succ (size xs)
\end{BVerbatim}
}
\end{figure}
\end{frame}
\frame{
\frametitle{Position/Index}
Finding the index of a thing is a little more interesting
}
\begin{frame}[fragile]
\frametitle{Position/Index: Mark 1}
\begin{figure}
{\color{dark-gray}
\centering
\begin{BVerbatim}
position : elem -> List elem -> ??????????
\end{BVerbatim}
}
\end{figure}
\end{frame}
\begin{frame}[fragile]
\frametitle{Position/Index: Mark 1}
\begin{figure}
{\color{dark-gray}
\centering
\begin{BVerbatim}
position : elem -> List elem -> ??????????
position a xs = pos xs 0
where
pos (x::xs) n =
\end{BVerbatim}
}
\end{figure}
\end{frame}
\begin{frame}[fragile]
\frametitle{Position/Index: Mark 1}
\begin{figure}
{\color{dark-gray}
\centering
\begin{BVerbatim}
position : elem -> List elem -> ??????????
position a xs = pos xs 0
where
pos (x::xs) n =
if a == x
then n
else pos xs (Succ n)
\end{BVerbatim}
}
\end{figure}
\end{frame}
\begin{frame}[fragile]
\frametitle{Position/Index: Mark 1}
\begin{figure}
{\color{dark-gray}
\centering
\begin{BVerbatim}
position : elem -> List elem -> ??????????
position a xs = pos xs 0
where
pos (x::xs) n =
if a == x
then n
else pos xs (Succ n)
pos [] n = ????
\end{BVerbatim}
}
\end{figure}
\end{frame}
\begin{frame}[fragile]
\frametitle{Position/Index: Mark 1}
\begin{figure}
{\color{dark-gray}
\centering
\begin{BVerbatim}
position : elem -> List elem -> Option Nat
position a xs = pos xs 0
where
pos (x::xs) n =
if a == x
then Some n
else pos xs (Succ n)
pos [] n = None
\end{BVerbatim}
}
\end{figure}
\end{frame}
\frame{
\frametitle{Thoughts: Mark 1}
This is satisfying because we're explicit about the possibility of failure
}
\begin{frame}[fragile]
\frametitle{Position/Index: Mark 2}
It should really be \texttt{positions}!
\begin{figure}
{\color{dark-gray}
\centering
\begin{BVerbatim}
\end{BVerbatim}
}
\end{figure}
\end{frame}
\begin{frame}[fragile]
\frametitle{Position/Index: Mark 2}
It should really be \texttt{positions}!
\begin{figure}
{\color{dark-gray}
\centering
\begin{BVerbatim}
positions : elem -> List elem -> List Nat
positions a xs = pos xs 0
where
pos (x::xs) n =
if a == x
then n :: pos xs (Succ n)
else pos xs (Succ n)
pos [] n = []
\end{BVerbatim}
}
\end{figure}
\end{frame}
\frame{
\frametitle{Thoughts: Mark 2}
In a lazy language \texttt{positions} is strictly more flexible
}
\frame{
\frametitle{Thoughts: Mark 1 \& 2}
Mind the gap
\begin{itemize}
\item<2 -> There were none!
\end{itemize}
}
\begin{frame}[fragile]
\frametitle{sublist}
Take the sublist of a list:
\begin{figure}
{\color{dark-gray}
\centering
\begin{BVerbatim}
sublist m n = take (n - m+1) . drop m
\end{BVerbatim}
}
\end{figure}
\end{frame}
\begin{frame}[fragile]
\frametitle{sublist}
The \texttt{sublist} function has invariants that the user has to keep
in mind
\begin{figure}
{\color{dark-gray}
\centering
\begin{BVerbatim}
sublist m n = take (n - m+1) . drop m
\end{BVerbatim}
}
\end{figure}
\begin{itemize}
\item<2 -> What if \texttt{n < (m-1)}?
\item<3 -> \texttt{take} would be passed a negative argument!
\end{itemize}
\end{frame}