You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

600 lines
19 KiB

  1. \documentclass[10pt,a5paper,mathserif,serif,usenames,dvipsnames]{beamer}
  2. % packages
  3. \usepackage{xcolor}
  4. \usepackage[utf8]{inputenc}
  5. \usepackage{amsmath}
  6. \usepackage{amsfonts}
  7. \usepackage{amssymb}
  8. \usepackage{graphicx}
  9. \usepackage{listings}
  10. % package configuration
  11. \DeclareGraphicsExtensions{.pdf,.png,.jpg}
  12. \beamertemplatenavigationsymbolsempty
  13. % title page information
  14. \author{Julian Ospald}
  15. \institute{FH Bielefeld}
  16. \title{Haskell: introduction}
  17. % color definition
  18. \definecolor{solarized}{HTML}{002B36}
  19. \definecolor{mygreen}{rgb}{0,0.6,0}
  20. % macros and environments
  21. \newcommand{\code}[1]{\texttt{#1}}
  22. \newcommand{\setHaskellCodeStyle}{
  23. \lstset{
  24. language=Haskell,
  25. backgroundcolor=\color{lightgray},
  26. commentstyle=\color{mygreen},
  27. keywordstyle=\color{blue},
  28. frame=single,
  29. keepspaces=true
  30. }
  31. }
  32. \newcommand{\setCCodeStyle}{
  33. \lstset{
  34. language=C,
  35. backgroundcolor=\color{lightgray},
  36. commentstyle=\color{mygreen},
  37. keywordstyle=\color{blue},
  38. frame=single,
  39. keepspaces=true
  40. }
  41. }
  42. \newcommand{\setCppCodeStyle}{
  43. \lstset{
  44. language=C++,
  45. backgroundcolor=\color{lightgray},
  46. commentstyle=\color{mygreen},
  47. keywordstyle=\color{blue},
  48. frame=single,
  49. keepspaces=true
  50. }
  51. }
  52. \begin{document}
  53. \frame{\titlepage}
  54. \begin{frame}
  55. \frametitle{Why haskell?}
  56. A Haskeller might claim: haskell...
  57. \begin{itemize}[<+->]
  58. \item eliminates certain classes of bugs
  59. \item makes it easy to reason about code
  60. \item decreases the bus-factor
  61. \item makes it possible to apply huge changes to large programs without worrying about the implicit state machine
  62. \item makes it easier to program complex problems
  63. \item allows for clean APIs, even without any OOP
  64. \item a haskell program of 10K LOC isn't that much different to maintain as a 100K LOC
  65. \end{itemize}
  66. \vspace{\baselineskip}
  67. \onslide<+->
  68. We'll have to see if this holds true.
  69. \end{frame}
  70. \begin{frame}[fragile]
  71. \frametitle{Why haskell? (ctn.)}
  72. From C++ std:
  73. \setCppCodeStyle
  74. \begin{lstlisting}
  75. void pop();
  76. \end{lstlisting}
  77. \setCCodeStyle
  78. \onslide<+->
  79. From the C FLINT library:
  80. \begin{lstlisting}
  81. void fmpz_mod_poly_add(
  82. fmpz_mod_poly_t res,
  83. const fmpz_mod_poly_t poly1,
  84. const fmpz_mod_poly_t poly2);
  85. \end{lstlisting}
  86. \vspace{\baselineskip}
  87. \onslide<+->
  88. Regular C functions in real-world (omitting examples on purpose):
  89. \begin{itemize}[<+->]
  90. \item 100+ LOC
  91. \item at least 7 ifs, 4 whiles, 12 variables, 1 goto
  92. \item accesses both static and global variables
  93. \item indenting level of 5 or more
  94. \item a lot of memory management and custom-made error handling
  95. \item references everywhere!
  96. \end{itemize}
  97. \end{frame}
  98. \begin{frame}
  99. \frametitle{Why haskell? (ctn.)}
  100. You need to change only one single line in such a C function. You have to know:
  101. \begin{itemize}[<+->]
  102. \item does the order of function calls matter?
  103. \item how does the change effect the memory management? Do we have memory leaks? Do we access invalid memory?
  104. \item does it change the state of static or global variables?
  105. \item does it implicitly change the state of out-parameters?
  106. \item if it changes any of those states, is the function still correct?
  107. \item what happens if the program flow reaches this codepath with variable X in that particular state, while variable Z is NULL, and...
  108. \item did you just nuke a small former Soviet state?
  109. \end{itemize}
  110. \vspace{\baselineskip}
  111. \onslide<+->
  112. Conclusion: you really need to understand the complete environment of that line/function.
  113. \end{frame}
  114. \begin{frame}
  115. \frametitle{Why haskell? (ctn.)}
  116. But java helps! Does it?
  117. Sort of, because:
  118. \begin{itemize}[<+->]
  119. \item it improves APIs compared to C, since you can hide or encapsulate information in the state of an object
  120. \item it has a garbage collector, so you don't need to worry too much about memory
  121. \end{itemize}
  122. \onslide<+->
  123. Unfortunately, we:
  124. \begin{itemize}[<+->]
  125. \item now got even more states to keep track of (intellectual complexity?)
  126. \item have clouded the program flow... it's now about object-interaction with their explicit and implicit states
  127. \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
  128. \end{itemize}
  129. \onslide<+->
  130. 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"?
  131. \onslide<+->
  132. We are lucky. There is. It's called \textbf{Haskell}.
  133. \end{frame}
  134. \begin{frame}
  135. \frametitle{What is haskell?}
  136. Haskell is a \textbf{functional}, \textbf{pure}, \textbf{lazy} and \textbf{statically typed} high-level programming language.\\
  137. \onslide<+->
  138. \vspace{\baselineskip}
  139. 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?".
  140. \end{frame}
  141. \begin{frame}
  142. \frametitle{What does \textbf{functional} mean?}
  143. Think of haskell functions as regular \emph{mathematical} functions.
  144. \onslide<+->
  145. \vspace{\baselineskip}
  146. \includegraphics*[scale=0.4]{function-machine.png}
  147. \begin{itemize}[<+->]
  148. \item does this function write to the hard drive?
  149. \item does the output depend on anything else except the input (e.g. time, environment, ...)?
  150. \end{itemize}
  151. \onslide<+->
  152. \vspace{\baselineskip}
  153. It's all about \emph{input} and \emph{ouput} of functions! And that's it. Nothing else to worry about.
  154. \end{frame}
  155. \begin{frame}
  156. \frametitle{What does \textbf{functional} mean? (ctn.)}
  157. \begin{itemize}[<+->]
  158. \item \emph{first-class} citizens: functions are values and can be used as such
  159. \item a haskell program is what happens when \emph{expressions are evaluated}, it's not about executing instructions
  160. \end{itemize}
  161. \end{frame}
  162. \begin{frame}
  163. \frametitle{What does \textbf{pure} mean?}
  164. \emph{Referential transparency}, as in:
  165. \onslide<+->
  166. \begin{itemize}[<+->]
  167. \item everything (variables, data structures...) is \emph{immutable}
  168. \item expressions never have side-effects (remember: mathematical functions)
  169. \item same input $\mapsto$ same output... \emph{always}!
  170. \item replace a function with it's (return) value? Yes. (what happens in C or java if you do that?)
  171. \end{itemize}
  172. \onslide<+->
  173. \vspace{\baselineskip}
  174. possible benefits?
  175. \begin{itemize}[<+->]
  176. \item parallelism
  177. \item equational reasoning and refactoring
  178. \item less bugs!
  179. \end{itemize}
  180. \onslide<+->
  181. \vspace{\baselineskip}
  182. Question: call-by-value? call-by-reference? call-by-need?
  183. \end{frame}
  184. \begin{frame}
  185. \frametitle{What does \textbf{lazy} mean?}
  186. In haskell expressions are not evaluated until their results are actually needed. That has a lot of consequences, a few of them being:
  187. \onslide<+->
  188. \begin{itemize}[<+->]
  189. \item infinite data structures are now possible (recursive and non-recursive)
  190. \item defining new control structures by just defining a function (since not everything is evaluated... who needs if-then-else anyway?)
  191. \item important for compositional programming and efficiency
  192. \item laziness causes (memory) overhead, but most of the time the benefits outweigh the costs
  193. \end{itemize}
  194. \end{frame}
  195. \begin{frame}
  196. \frametitle{What does \textbf{statically typed} mean?}
  197. 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.\\
  198. \vspace{\baselineskip}
  199. 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).
  200. \end{frame}
  201. \begin{frame}
  202. \frametitle{What is haskell again?}
  203. Let's reiterate. Haskell is:
  204. \begin{itemize}
  205. \item functional
  206. \item pure
  207. \item lazy
  208. \item statically typed (and truly typesafe)
  209. \item even garbage collected
  210. \item the world's finest imperative language (what??)
  211. \end{itemize}
  212. \end{frame}
  213. \begin{frame}
  214. \frametitle{Types, types, types}
  215. \begin{itemize}
  216. \item think in types!
  217. \item don't be afraid of type errors
  218. \item let the type-checker do the work for you (does this function do what I think it does?)
  219. \item understand functions just by looking at their type signature?
  220. \end{itemize}
  221. \end{frame}
  222. \begin{frame}[fragile]
  223. \frametitle{Declarations}
  224. Let's go!
  225. \setHaskellCodeStyle
  226. \begin{lstlisting}
  227. x :: Int
  228. x = 3
  229. -- how about this?
  230. x = 5
  231. -- Int vs Integer
  232. n :: Integer
  233. n = 12345678909876543219873409823349873498723498
  234. d :: Double
  235. d = 5.0
  236. c :: Char
  237. c = 'k'
  238. s :: String
  239. s = "Hello, world?"
  240. \end{lstlisting}
  241. \end{frame}
  242. \begin{frame}[fragile]
  243. \frametitle{Arithmetic and co.}
  244. \setHaskellCodeStyle
  245. GHCi:
  246. \begin{lstlisting}
  247. > 3 + 5
  248. > (3 :: Integer) + (5 :: Int)
  249. > 6 * 5.0
  250. > "Hello" ++ " world"
  251. > "Haskell" > "C++"
  252. > True && False
  253. \end{lstlisting}
  254. \end{frame}
  255. \begin{frame}[fragile]
  256. \frametitle{Functions and control structures}
  257. \setHaskellCodeStyle
  258. Let's make our first function. We want something like the following mathematical function\\
  259. $f(x) = x * x$\\
  260. \vspace{\baselineskip}
  261. How could the haskell code look like?
  262. \pause
  263. Almost the same:
  264. \begin{lstlisting}
  265. f x = x * x
  266. \end{lstlisting}
  267. \vspace{\baselineskip}
  268. GHCi...\\
  269. \pause
  270. \vspace{\baselineskip}
  271. What is a possible type signature for this function?
  272. \begin{lstlisting}
  273. f :: Int -> Int
  274. f x = x * x
  275. \end{lstlisting}
  276. 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.
  277. \end{frame}
  278. \begin{frame}[fragile]
  279. \frametitle{Functions and control structures (ctn.)}
  280. 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 arguments. Let's see:
  281. \pause
  282. \setHaskellCodeStyle
  283. \vspace{\baselineskip}
  284. \begin{lstlisting}
  285. isZero :: Int -> Bool
  286. isZero 0 = True
  287. isZero x = False
  288. \end{lstlisting}
  289. \vspace{\baselineskip}
  290. 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.\\
  291. \vspace{\baselineskip}
  292. What might happen if we remove the second or the third line? What is a \textbf{partial function} and a \textbf{total function}?
  293. \end{frame}
  294. \begin{frame}[fragile]
  295. \frametitle{Functions and control structures (ctn.)}
  296. How about \emph{recursion}? Let's define the modulo of any \emph{Int} to \emph{2}.
  297. \pause
  298. \setHaskellCodeStyle
  299. \vspace{\baselineskip}
  300. \begin{lstlisting}
  301. mod2 :: Int -> Int
  302. mod2 x
  303. | x - 2 == 0 = 0
  304. | x - 2 < 0 = x
  305. | otherwise = mod2 (x - 2)
  306. \end{lstlisting}
  307. 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.\\ \code{otherwise} on the last line is just defined as \code{True}, to make these constructs reasier to read and catch all other cases of input.
  308. \end{frame}
  309. \begin{frame}[fragile]
  310. \frametitle{Pairs/Tuples}
  311. Defining a pair is easy.
  312. \setHaskellCodeStyle
  313. \begin{lstlisting}
  314. p :: (Int, Char) -- this is the type
  315. p = (2, 'y') -- this is how we construct the pair
  316. -- pattern matching against pairs
  317. sumPair :: (Int, Int) -> Int
  318. sumPair (x, y) = x + y
  319. \end{lstlisting}
  320. \pause
  321. 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.
  322. \end{frame}
  323. \begin{frame}[fragile]
  324. \frametitle{Lists}
  325. \setHaskellCodeStyle
  326. 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.
  327. \pause
  328. We build lists by using either the \code{[]} notation:
  329. \begin{lstlisting}
  330. list1 :: [Integer]
  331. list1 = [1, 2]
  332. \end{lstlisting}
  333. \pause
  334. 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.
  335. \begin{lstlisting}
  336. emptyList = []
  337. list2 = 1 : []
  338. -- is this really a list?
  339. list3 = [1, 2] == 1 : 2 : []
  340. \end{lstlisting}
  341. \pause
  342. How about something more interesting:
  343. \begin{lstlisting}
  344. infiniteList = [1..]
  345. \end{lstlisting}
  346. \end{frame}
  347. \begin{frame}[fragile]
  348. \frametitle{Lists (ctn.)}
  349. A String in haskell is just a list of Chars!
  350. \setHaskellCodeStyle
  351. \begin{lstlisting}
  352. > ['a', 'b', 'c']
  353. > 'a' : []
  354. > head "abc"
  355. > 'a' ++ 'c'
  356. \end{lstlisting}
  357. \end{frame}
  358. \begin{frame}[fragile]
  359. \frametitle{Lists (ctn.)}
  360. Again, we can do pattern matching on lists.
  361. \setHaskellCodeStyle
  362. \begin{lstlisting}
  363. listLen :: [Integer] -> Integer
  364. listLen [] = 0
  365. listLen (x:xs) = 1 + listLen xs
  366. \end{lstlisting}
  367. \pause
  368. We can also nest pattern matching:
  369. \begin{lstlisting}
  370. sumEveryTwo :: [Integer] -> [Integer]
  371. sumEveryTwo [] = 0
  372. sumEveryTwo (x:[]) = [x]
  373. sumEveryTwo (x:(y:zs)) = (x + y) : sumEveryTwo zs
  374. \end{lstlisting}
  375. Note that \code{(x:(y:zs))} may also be written as \code{(x:y:zs)}.
  376. \end{frame}
  377. \begin{frame}[fragile]
  378. \frametitle{Lists (ctn.)}
  379. Haskell also supports \textbf{list comprehension} which is basically syntactic sugar for what we already know from maths.\\
  380. Let's define a set that contains the first ten even natural numbers:\\
  381. \pause
  382. $S = \{2 \times x\ |\ x \in \mathbb{N},\ x \leq 10\}$\\
  383. \vspace{\baselineskip}
  384. \pause
  385. How does this look in haskell?
  386. \pause
  387. \setHaskellCodeStyle
  388. \begin{lstlisting}
  389. > [x*2 | x <- [1..10]]
  390. \end{lstlisting}
  391. \pause
  392. Now let's say we want all numbers between 50 and 100 that have the remainder 0 when divided by 12:
  393. \pause
  394. \setHaskellCodeStyle
  395. \begin{lstlisting}
  396. > [x | x <- [50..100], mod x 12 == 0]
  397. \end{lstlisting}
  398. \code{x <- [50..100]} is the binding, while \code{mod x 12 == 0} is the predicate, separated by a comma. We can have multiple predicates.
  399. \end{frame}
  400. \begin{frame}[fragile]
  401. \frametitle{Algebraic Data Types}
  402. Of course we can also define our own data types in haskell. One very common type is the \emph{enumeration}. For example, we could define a data type for the Week:
  403. \setHaskellCodeStyle
  404. \begin{lstlisting}
  405. data WeekDay = Monday
  406. | Tuesday
  407. | Thursday
  408. | Wednsday
  409. | Friday
  410. | Saturday
  411. | Sunday
  412. \end{lstlisting}
  413. This declares the new data type \code{WeekDay} with 7 \emph{constructors}. That means \code{Monday}, \code{Tuesday} etc. are all values of the type \code{WeekDay}.
  414. \pause
  415. \\
  416. We could now define a whole week, by creating a list:
  417. \pause
  418. \setHaskellCodeStyle
  419. \begin{lstlisting}
  420. week :: [WeekDay]
  421. week = [Monday, Tuesday, Thursday, Wednsday
  422. , Friday, Saturday, Sunday]
  423. \end{lstlisting}
  424. \end{frame}
  425. \begin{frame}[fragile]
  426. \frametitle{Algebraic Data Types (ctn.)}
  427. And we can again \emph{pattern match} on our \code{WeekDay} type. Let's find out if a given day is a monday:
  428. \pause
  429. \setHaskellCodeStyle
  430. \begin{lstlisting}
  431. isMonday :: WeekDay -> Bool
  432. isMonday Monday = True
  433. isMonday x = False
  434. \end{lstlisting}
  435. \end{frame}
  436. \begin{frame}[fragile]
  437. \frametitle{Algebraic Data Types (ctn.)}
  438. But we can do more than enumerations. How about we do some error handling? Let's say we want a function to return an \code{Int}, but in case something went horribly wrong, we don't just want to return a 0 or some magic number, but a proper error message. Here we go:
  439. \pause
  440. \setHaskellCodeStyle
  441. \begin{lstlisting}
  442. data MaybeInt = NoError Int
  443. | Error String
  444. \end{lstlisting}
  445. \pause
  446. And now we can do sanity checks:
  447. \pause
  448. \setHaskellCodeStyle
  449. \begin{lstlisting}
  450. calcSomething :: Int -> MaybeInt
  451. calcSomething x
  452. | x < 100 = NoError (x * 5)
  453. | otherwise = Error "Int out of range!"
  454. \end{lstlisting}
  455. \pause
  456. So constructors are just \emph{functions}! And they can have arguments, just like functions. Let's check their types:
  457. \setHaskellCodeStyle
  458. \begin{lstlisting}
  459. > :t NoError
  460. NoError :: Int -> MaybeInt
  461. > :t Error
  462. Error :: String -> MaybeInt
  463. \end{lstlisting}
  464. \end{frame}
  465. \begin{frame}[fragile]
  466. \frametitle{Algebraic Data Types (ctn.)}
  467. Let's define something more complex. How about a tree?
  468. \pause
  469. \setHaskellCodeStyle
  470. \begin{lstlisting}
  471. data Tree = Leaf Char
  472. | Node Tree Int Tree
  473. \end{lstlisting}
  474. Uh... that looks mean. Let's examine this.\\
  475. \pause
  476. We have:
  477. \begin{itemize}[<+->]
  478. \item defined a data type \code{Tree}
  479. \item a constructor \code{Leaf} of type \code{Tree} with one arguments of type \code{Char}
  480. \item a constructor \code{Node} of type \code{Tree} with 3 arguments
  481. \begin{itemize}[<+->]
  482. \item \code{Tree}
  483. \item \code{Int}
  484. \item \code{Tree}
  485. \end{itemize}
  486. \end{itemize}
  487. \onslide<+->
  488. That means: a \code{Tree} can either be a \code{Leaf} or an internal \code{Node} with two sub-trees. If we want to create a \code{Leaf}, we have to pass the constructor a \code{Char}. If we want to create a \code{Node}, we have to pass 3 arguments, in order: another \code{Tree}, an \code{Int} and yet another \code{Tree}.\\
  489. So we can save information in the leafs (\code{Char}) and in the internal nodes (\code{Int}).\\
  490. This is just an example. There are endless more ways of trees.
  491. \end{frame}
  492. \begin{frame}[fragile]
  493. \frametitle{Algebraic Data Types (ctn.)}
  494. Let's build our tree:
  495. \setHaskellCodeStyle
  496. \begin{lstlisting}
  497. tree :: Tree
  498. tree = Node
  499. (Leaf 'x')
  500. 1
  501. (Node
  502. (Leaf 'y')
  503. 2
  504. (Leaf 'z')
  505. )
  506. \end{lstlisting}
  507. See board...
  508. \end{frame}
  509. \begin{frame}[fragile]
  510. \frametitle{Algebraic Data Types (ctn.)}
  511. So if we want to generalize it, an algebraic data type has one or more \textbf{constructors}, and each of them can have zero or more arguments. E.g.:
  512. \setHaskellCodeStyle
  513. \begin{lstlisting}
  514. data AlgDataType = Constr1 Type11 Type12
  515. | Constr2 Type21
  516. | Constr3 Type31 Type32 Type33
  517. | Constr4
  518. \end{lstlisting}
  519. \end{frame}
  520. \begin{frame}
  521. \frametitle{Toolchain}
  522. You need:
  523. \begin{itemize}
  524. \item \textbf{GHC}: this is the Haskell compiler
  525. \item \textbf{GHCi}: this an interactive environment of GHC, similar to the interactive ruby shell \emph{irb}
  526. \item \textbf{The Haskell Platform}: a collection including GHC, GHCi and basic libraries
  527. \end{itemize}
  528. Go to \url{https://www.haskell.org/platform}\\
  529. For haskell IDEs, see \url{https://wiki.haskell.org/IDEs}
  530. \end{frame}
  531. \begin{frame}
  532. \frametitle{Further reading and useful links}
  533. \begin{itemize}
  534. \item the most popular haskell course from Brent Yorgey:\\ \url{https://www.seas.upenn.edu/~cis194/fall14/spring13/lectures.html}
  535. \item very verbose and enthusiastic haskell book, good for reading once:\\ \url{http://learnyouahaskell.com}
  536. \item collection of websites teaching haskell:\\ \url{https://github.com/bitemyapp/learnhaskell}
  537. \item haskell programming tips (and wiki):\\ \url{https://wiki.haskell.org/Haskell_programming_tips}
  538. \item the standard module (similar to libc in C):\\ \url{https://hackage.haskell.org/package/base-4.7.0.0/docs/Prelude.html}
  539. \item debugging in haskell:\\ \url{https://wiki.haskell.org/Debugging}
  540. \end{itemize}
  541. \end{frame}
  542. \begin{frame}
  543. \frametitle{Sources}
  544. \begin{itemize}
  545. \item much content was borrowed or is based on the haskell course from Brent Yorgey:\\ \url{https://www.seas.upenn.edu/~cis194/fall14/spring13/lectures.html}
  546. \item a few small pieces from the LYAH book \url{http://learnyouahaskell.com}
  547. \item general information from wikipedia: \\ \url{https://en.wikipedia.org}
  548. \item general information from haskell wiki: \\ \url{https://wiki.haskell.org}
  549. \end{itemize}
  550. \end{frame}
  551. \end{document}