Browse Source

Update and complete missing slides

Julian Ospald 4 years ago
parent
commit
d7832bb7ba
No account linked to committer's email address
1 changed files with 309 additions and 25 deletions
  1. 309
    25
      VL1.tex

+ 309
- 25
VL1.tex View File

@@ -11,6 +11,7 @@
11 11
 
12 12
 % package configuration
13 13
 \DeclareGraphicsExtensions{.pdf,.png,.jpg}
14
+\beamertemplatenavigationsymbolsempty
14 15
 
15 16
 % title page information
16 17
 \author{Julian Ospald}
@@ -19,17 +20,49 @@
19 20
 
20 21
 % color definition
21 22
 \definecolor{solarized}{HTML}{002B36}
23
+\definecolor{mygreen}{rgb}{0,0.6,0}
24
+
25
+% macros and environments
26
+\newcommand{\code}[1]{\texttt{#1}}
27
+\newcommand{\setHaskellCodeStyle}{
28
+	\lstset{
29
+  		language=Haskell,
30
+		backgroundcolor=\color{lightgray},
31
+		commentstyle=\color{mygreen},
32
+		keywordstyle=\color{blue},
33
+		frame=single,
34
+		keepspaces=true
35
+	}
36
+}
37
+\newcommand{\setCCodeStyle}{
38
+	\lstset{
39
+  		language=C,
40
+		backgroundcolor=\color{lightgray},
41
+		commentstyle=\color{mygreen},
42
+		keywordstyle=\color{blue},
43
+		frame=single,
44
+		keepspaces=true
45
+	}
46
+}
47
+\newcommand{\setCppCodeStyle}{
48
+	\lstset{
49
+  		language=C++,
50
+		backgroundcolor=\color{lightgray},
51
+		commentstyle=\color{mygreen},
52
+		keywordstyle=\color{blue},
53
+		frame=single,
54
+		keepspaces=true
55
+	}
56
+}
22 57
 
23 58
 \begin{document}
24 59
 
25
-% page 0
26 60
 \frame{\titlepage}
27 61
 
28
-% page 1
29 62
 \begin{frame}
30 63
 \frametitle{Why haskell?}
31 64
 A Haskeller might claim: haskell...
32
-\begin{itemize}
65
+\begin{itemize}[<+->]
33 66
 \item eliminates certain classes of bugs
34 67
 \item makes it easy to reason about code
35 68
 \item decreases the bus-factor
@@ -39,25 +72,19 @@ A Haskeller might claim: haskell...
39 72
 \item a haskell program of 10K LOC isn't that much different to maintain as a 100K LOC
40 73
 \end{itemize}
41 74
 \vspace{\baselineskip}
75
+\onslide<+->
42 76
 We'll have to see if this holds true.
43 77
 \end{frame}
44 78
 
45
-% page 1
46 79
 \begin{frame}[fragile]
47 80
 \frametitle{Why haskell? (ctn.)}
48 81
 From C++ std:
49
-\lstset{
50
-	language=C++,
51
-	backgroundcolor=\color{White},
52
-	frame=single,
53
-	keepspaces=true
54
-}
82
+\setCppCodeStyle
55 83
 \begin{lstlisting}
56 84
 void pop();
57 85
 \end{lstlisting}
58
-\lstset{
59
-	language=C,
60
-}
86
+\setCCodeStyle
87
+\onslide<+->
61 88
 From the C FLINT library:
62 89
 \begin{lstlisting}
63 90
 void fmpz_mod_poly_add(
@@ -66,20 +93,22 @@ void fmpz_mod_poly_add(
66 93
     const fmpz_mod_poly_t poly2);
67 94
 \end{lstlisting}
68 95
 \vspace{\baselineskip}
96
+\onslide<+->
69 97
 Regular C functions in real-world (omitting examples on purpose):
70
-\begin{itemize}
98
+\begin{itemize}[<+->]
71 99
 \item 100+ LOC
72 100
 \item at least 7 ifs, 4 whiles, 12 variables, 1 goto
73 101
 \item accesses both static and global variables
74 102
 \item indenting level of 5 or more
75 103
 \item a lot of memory management and custom-made error handling
104
+\item references everywhere!
76 105
 \end{itemize}
77 106
 \end{frame}
78 107
 
79 108
 \begin{frame}
80 109
 \frametitle{Why haskell? (ctn.)}
81 110
 You need to change only one single line in such a C function. You have to know:
82
-\begin{itemize}
111
+\begin{itemize}[<+->]
83 112
 \item does the order of function calls matter?
84 113
 \item how does the change effect the memory management? Do we have memory leaks? Do we access invalid memory?
85 114
 \item does it change the state of static or global variables?
@@ -89,6 +118,7 @@ You need to change only one single line in such a C function. You have to know:
89 118
 \item did you just nuke a small former Soviet state?
90 119
 \end{itemize}
91 120
 \vspace{\baselineskip}
121
+\onslide<+->
92 122
 Conclusion: you really need to understand the complete environment of that line/function.
93 123
 \end{frame}
94 124
 
@@ -96,45 +126,299 @@ Conclusion: you really need to understand the complete environment of that line/
96 126
 \frametitle{Why haskell? (ctn.)}
97 127
 But java helps! Does it?
98 128
 Sort of, because:
99
-\begin{itemize}
129
+\begin{itemize}[<+->]
100 130
 \item it improves APIs compared to C, since you can hide or encapsulate information in the state of an object
101 131
 \item it has a garbage collector, so you don't need to worry too much about memory
102 132
 \end{itemize}
133
+\onslide<+->
103 134
 Unfortunately, we:
104
-\begin{itemize}
135
+\begin{itemize}[<+->]
105 136
 \item now got even more states to keep track of (intellectual complexity?)
106 137
 \item have clouded the program flow... it's now about object-interaction with their explicit and implicit states
107 138
 \item still have \textbf{side effects} everywhere: one object changes the state of another and vice versa, may arbitrarily write to the hard drive, do kernel calls or launch a missle
108 139
 \end{itemize}
109
-Some parts of the implicit state machine have been made explicit by modelling classes, but it's still there and we have to deal with it, because we are modelling everything around states. Wouldn't it be nice if we could just forget about the global state machine? Maybe there is even a way to remove side effects and have more "predictability"? We are lucky. There is. It's called \textbf{Haskell}.
140
+\onslide<+->
141
+Some parts of the implicit state machine have been made explicit by modelling classes, but it's still there and we have to deal with it, because we are modelling everything around states. Wouldn't it be nice if we could just forget about the global state machine? Maybe there is even a way to remove side effects and have more "predictability"?
142
+\onslide<+->
143
+We are lucky. There is. It's called \textbf{Haskell}.
110 144
 \end{frame}
111 145
 
112
-% page 2
113 146
 \begin{frame}
114 147
 \frametitle{What is haskell?}
115
-Haskell is a \textbf{functional}, \textbf{pure}, \textbf{lazy} and \textbf{statically typed} high-level programming language.
148
+Haskell is a \textbf{functional}, \textbf{pure}, \textbf{lazy} and \textbf{statically typed} high-level programming language.\\
149
+\onslide<+->
150
+\vspace{\baselineskip}
151
+A few of the following facts will be white lies. But let's not worry about that. It's maybe more about "how to think in terms of haskell?" than "what is haskell?".
116 152
 \end{frame}
117 153
 
118
-% page3
119 154
 \begin{frame}
120 155
 \frametitle{What does \textbf{functional} mean?}
121 156
 Think of haskell functions as regular \emph{mathematical} functions.
157
+\onslide<+->
122 158
 \vspace{\baselineskip}
123 159
 \includegraphics*[scale=0.4]{function-machine.png}
124
-\begin{itemize}
160
+\begin{itemize}[<+->]
125 161
 \item does this function write to the hard drive?
126 162
 \item does the output depend on anything else except the input (e.g. time, environment, ...)?
127 163
 \end{itemize}
164
+\onslide<+->
165
+\vspace{\baselineskip}
166
+It's all about \emph{input} and \emph{ouput} of functions! And that's it. Nothing else to worry about.
128 167
 \end{frame}
129 168
 
130
-% page4
131 169
 \begin{frame}
132 170
 \frametitle{What does \textbf{functional} mean? (ctn.)}
171
+\begin{itemize}[<+->]
172
+\item \emph{first-class} citizens: functions are values and can be used as such
173
+\item a haskell program is what happens when \emph{expressions are evaluated}, it's not about executing instructions
174
+\end{itemize}
175
+\end{frame}
176
+
177
+\begin{frame}
178
+\frametitle{What does \textbf{pure} mean?}
179
+\emph{Referential transparency}, as in:
180
+\onslide<+->
181
+\begin{itemize}[<+->]
182
+\item everything (variables, data structures...) is \emph{immutable}
183
+\item expressions never have side-effects (remember: mathematical functions)
184
+\item same input $\mapsto$ same output... \emph{always}!
185
+\item replace a function with it's (return) value? Yes. (what happens in C or java if you do that?)
186
+\end{itemize}
187
+\onslide<+->
188
+\vspace{\baselineskip}
189
+possible benefits?
190
+\begin{itemize}[<+->]
191
+\item parallelism
192
+\item equational reasoning and refactoring
193
+\item less bugs!
194
+\end{itemize}
195
+\onslide<+->
196
+\vspace{\baselineskip}
197
+Question: call-by-value? call-by-reference? call-by-need?
198
+\end{frame}
199
+
200
+\begin{frame}
201
+\frametitle{What does \textbf{lazy} mean?}
202
+In haskell expressions are not evaluated until their results are actually needed. That has a lot of consequences, a few of them being:
203
+\onslide<+->
204
+\begin{itemize}[<+->]
205
+\item infinite data structures are now possible (recursive and non-recursive)
206
+\item defining new control structures by just defining a function (since not everything is evaluated... who needs if-then-else anyway?)
207
+\item important for compositional programming and efficiency
208
+\item laziness causes (memory) overhead, but most of the time the benefits outweigh the costs
209
+\end{itemize}
210
+\end{frame}
211
+
212
+\begin{frame}
213
+\frametitle{What does \textbf{statically typed} mean?}
214
+Every haskell expression has a type and types are always checked at \emph{compile-time}. Programs with errors will not compile and definitely not run.\\
215
+\vspace{\baselineskip}
216
+It is possible to simulate dynamic types however. In the end, they are still statically type-checked (as in: the compiler always knows what's going on).
217
+\end{frame}
218
+
219
+\begin{frame}
220
+\frametitle{What is haskell again?}
221
+Let's reiterate. Haskell is:
222
+\begin{itemize}
223
+\item functional
224
+\item pure
225
+\item lazy
226
+\item statically typed (and truly typesafe)
227
+\end{itemize}
228
+\end{frame}
229
+
230
+\begin{frame}
231
+\frametitle{Types, types, types}
133 232
 \begin{itemize}
134
-\item \emph{first-class} citizens: functions are values
135
-\item a haskell program is what happens when \emph{expressions are evaluated}
233
+\item think in types!
234
+\item don't be afraid of type errors
235
+\item let the type-checker do the work for you (does this function do what I think it does?)
236
+\item understand functions just by looking at their type signature?
136 237
 \end{itemize}
137 238
 \end{frame}
138 239
 
240
+\begin{frame}[fragile]
241
+\frametitle{Declarations}
242
+Let's go!
243
+\setHaskellCodeStyle
244
+\begin{lstlisting}
245
+x :: Int
246
+x = 3
247
+
248
+-- how about this?
249
+x = 5
250
+
251
+-- Int vs Integer
252
+n :: Integer
253
+n = 12345678909876543219873409823349873498723498
254
+
255
+d :: Double
256
+d = 5.0
257
+
258
+c :: Char
259
+c = 'k'
260
+
261
+s :: String
262
+s = "Hello, world?"
263
+\end{lstlisting}
264
+\end{frame}
265
+
266
+\begin{frame}[fragile]
267
+\frametitle{Arithmetic and co.}
268
+\setHaskellCodeStyle
269
+GHCi:
270
+\begin{lstlisting}
271
+> 3 + 5
272
+> (3 :: Integer) + (5 :: Int)
273
+> 6 * 5.0
274
+> "Hello" ++ " world"
275
+> "Haskell" > "C++"
276
+> True && False
277
+\end{lstlisting}
278
+\end{frame}
279
+
280
+\begin{frame}[fragile]
281
+\frametitle{Functions and control structures}
282
+\setHaskellCodeStyle
283
+Let's make our first function. We want something like the following mathematical function\\
284
+$f(x) = x * x$\\
285
+\vspace{\baselineskip}
286
+How could the haskell code look like?
287
+\pause
288
+Almost the same:
289
+\begin{lstlisting}
290
+f x = x * x
291
+\end{lstlisting}
292
+\vspace{\baselineskip}
293
+GHCi...\\
294
+\pause
295
+\vspace{\baselineskip}
296
+What is a possible type signature for this function?
297
+\begin{lstlisting}
298
+f :: Int -> Int
299
+f x = x * x
300
+\end{lstlisting}
301
+So the function gets an Int and returns an Int. Don't get confused by "\verb|->|". Just think of it as a symbol for separating input and output.
302
+\end{frame}
303
+
304
+\begin{frame}[fragile]
305
+\frametitle{Functions and control structures (ctn.)}
306
+In haskell we often use \textbf{pattern matching}. That means we define a function multiple times, but e.g. for different values of its input parameters. Let's see:
307
+\pause
308
+\setHaskellCodeStyle
309
+\vspace{\baselineskip}
310
+\begin{lstlisting}
311
+isZero :: Int -> Bool
312
+isZero 0 = True
313
+isZero x = False
314
+\end{lstlisting}
315
+\vspace{\baselineskip}
316
+So if we pass it 0, we get True. If we do not pass 0, we get False and the value we passed gets basically ignored.\\
317
+\vspace{\baselineskip}
318
+What might happen if we remove the second or the third line? What is a \textbf{partial function} and a \textbf{total function}?
319
+\end{frame}
320
+
321
+\begin{frame}[fragile]
322
+\frametitle{Functions and control structures (ctn.)}
323
+How about \emph{recursion}? Let's define the modulo of any \emph{Int} to \emph{2}.
324
+\pause
325
+\setHaskellCodeStyle
326
+\vspace{\baselineskip}
327
+\begin{lstlisting}
328
+mod2 :: Int -> Int
329
+mod2 x
330
+  | x - 2 == 0 = 0
331
+  | x - 2 < 0  = x
332
+  | otherwise  = mod2 (x - 2)
333
+\end{lstlisting}
334
+These \verb#|# things above are called \textbf{guards} and are similar to \emph{pattern matching}. They are processed in order. If the condition on the left side of the equation is true, then it returns what stands on the right side of the equation. If it's false, then it processes the next line.\\ \emph{"otherwise"} on the last line is just defined as \emph{True}, to make these constructs reasier to read and catch all other cases of input.
335
+\end{frame}
336
+
337
+\begin{frame}[fragile]
338
+\frametitle{Pairs/Tuples}
339
+Defining a pair is easy.
340
+\setHaskellCodeStyle
341
+\begin{lstlisting}
342
+p :: (Int, Char) -- this is the type
343
+p = (2, 'y') -- this is how we construct the pair
344
+
345
+-- pattern matching against pairs
346
+sumPair :: (Int, Int) -> Int
347
+sumPair (x, y) = x + y
348
+\end{lstlisting}
349
+\pause
350
+Note: we use \code{(x, y)} notation for both the type and the definition! Those are still two different things. We can also have triples, quadruples etc.
351
+\end{frame}
352
+
353
+\begin{frame}[fragile]
354
+\frametitle{Lists}
355
+\setHaskellCodeStyle
356
+The list is probably the most basic data structure in Haskell. Like the array in C. It is a singly-linked list and is very lazy. The compiler has numerous ways to optimize lists, so don't be afraid to use them, even for huge things.
357
+\pause
358
+We build lists by using either the \code{[]} notation:
359
+\begin{lstlisting}
360
+list1 :: [Integer]
361
+list1 = [1, 2]
362
+\end{lstlisting}
363
+\pause
364
+or by using the \emph{cons} operator \code{(:)} which takes an element and a list and produces a new list with the element prepended to the front.
365
+\begin{lstlisting}
366
+list2 = 1 : []
367
+
368
+-- is this really a list?
369
+list3 = [1, 2] == 1 : 2 : []
370
+
371
+emptyList = []
372
+\end{lstlisting}
373
+\pause
374
+How about something more interesting:
375
+\begin{lstlisting}
376
+infiniteList = [1..]
377
+\end{lstlisting}
378
+\end{frame}
379
+
380
+\begin{frame}[fragile]
381
+\frametitle{Lists (ctn.)}
382
+Again, we can do pattern matching on lists.
383
+\setHaskellCodeStyle
384
+\begin{lstlisting}
385
+listLen :: [Integer] -> Integer
386
+listLen [] = 0
387
+listLen (x:xs) = 1 + listLen xs
388
+\end{lstlisting}
389
+We can also nest pattern matching:
390
+\begin{lstlisting}
391
+sumEveryTwo :: [Integer] -> [Integer]
392
+sumEveryTwo []         = 0
393
+sumEveryTwo (x:[])     = [x]
394
+sumEveryTwo (x:(y:zs)) = (x + y) : sumEveryTwo zs
395
+\end{lstlisting}
396
+Note that \code{(x:(y:zs))} may also be written as \code{(x:y:zs)}.
397
+\end{frame}
398
+
399
+\begin{frame}
400
+\frametitle{Toolchain}
401
+You need:
402
+\begin{itemize}
403
+\item \textbf{GHC}: this is the Haskell compiler
404
+\item \textbf{GHCi}: this an interactive environment of GHC, similar to the interactive ruby shell \emph{irb}
405
+\item \textbf{The Haskell Platform}: a collection including GHC, GHCi and basic libraries
406
+\end{itemize}
407
+Go to \url{https://www.haskell.org/platform}\\
408
+For haskell IDEs, see \url{https://wiki.haskell.org/IDEs}
409
+\end{frame}
410
+
411
+\begin{frame}
412
+\frametitle{Further reading and useful links}
413
+\begin{itemize}
414
+\item the most popular haskell course:\\ \url{https://www.seas.upenn.edu/~cis194/fall14/spring13/lectures.html}
415
+\item very verbose and enthusiastic haskell book, good for reading once:\\ \url{http://learnyouahaskell.com}
416
+\item collection of websites teaching haskell:\\ \url{https://github.com/bitemyapp/learnhaskell}
417
+\item haskell programming tips (and wiki):\\ \url{https://wiki.haskell.org/Haskell_programming_tips}
418
+\item the standard module (similar to libc in C):\\ \url{https://hackage.haskell.org/package/base-4.7.0.0/docs/Prelude.html}
419
+\item debugging in haskell:\\ \url{https://wiki.haskell.org/Debugging}
420
+\end{itemize}
421
+
422
+\end{frame}
139 423
 
140 424
 \end{document}