@@ -1,8 +1,6 @@
1−Haskell 2024 - Intro
2−====================
1+* Haskell 2024 - Intro
32
4−What is Haskell?
5−----------------
3+** What is Haskell?
64
75As written in Wikipedia:
86
@@ -14,96 +12,95 @@ operator overloading, and monadic input/output (IO). It is named after logician
1412Haskell Curry. Haskell's main implementation is the Glasgow Haskell Compiler
1513(GHC).
1614
17−Where is Haskell used?
18−----------------------
15+** Where is Haskell used?
1916
2017Haskell is used in academia and in industry:
2118
22−* The proof assistant Agda is written in Haskell.
23−* Darcs is a revision control system written in Haskell, with several innovative
24− features, such as more precise control of patches to apply.
25−* GHC is also often a testbed for advanced functional programming features and
26− optimizations in other programming languages.
27−* Git-annex is a tool to manage (big) data files under Git version control. It
28− also provides a distributed file synchronization system (git-annex assistant).
29−* Linspire Linux chose Haskell for system tools development.
30−* Pandoc is a tool to convert one markup format into another.
31−* Pugs is a compiler and interpreter for the Raku programming language (formerly
32− Perl 6).
33−* TidalCycles is a domain special language for live coding musical patterns,
34− embedded in Haskell.
35−* Xmonad is a window manager for the X Window System, written fully in Haskell.
36−* GarganText is a collaborative tool to map through semantic analysis texts on
37− any web browser, written fully in Haskell and PureScript, which is used for
38− instance in the research community to draw up state-of-the-art reports and
39− roadmaps.
40−
41−Industry
42−~~~~~~~~
43−
44−* Bluespec SystemVerilog (BSV) is a language for semiconductor design that is an
45− extension of Haskell. Also, Bluespec, Inc.'s tools are implemented in Haskell.
46− Note this is an example of a domain specific language embedded into Haskell.
47−* Cryptol, a language and toolchain for developing and verifying cryptography
48− algorithms, is implemented in Haskell.
49−* Facebook implements its anti-spam programs in Haskell, maintaining the
50− underlying data access library as open-source software.
51−* The Cardano blockchain platform is implemented in Haskell.
52−* GitHub implemented Semantic, an open-source library for analysis, diffing, and
53− interpretation of untrusted source code, in Haskell.
54−* Standard Chartered's financial modelling language Mu is syntactic Haskell
55− running on a strict runtime.
56−* seL4, the first formally verified microkernel, used Haskell as a prototyping
57− language for the OS developer. At the same time, the Haskell code defined an
58− executable specification with which to reason, for automatic translation by
59− the theorem-proving tool. The Haskell code thus served as an intermediate
60− prototype before final C refinement.
61−* Target stores' supply chain optimization software is written in Haskell.
62−* Co–Star
63−
64−Some more examples from Serokell
65−~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
66−
67−* Hasura is an open-source GraphQL engine that gives you instant access to a
68− GraphQL API for your data. In the GitHub repository of Hasura, Haskell is the
69− most used language, followed by languages like TypeScript, JavaScript, Python,
70− and Go. At the start of 2022, Hasura raised \$100 million.
71−* Microsoft uses Haskell in Bond, a cross-platform framework for working with
72− schematized data. The Haskell part of Bond is mainly gbc, a command-line code
73− generation tool that can generate C++ and C# code using Bond. For a long time,
74− Microsoft Research also significantly supported the Glasgow Haskell Compiler
75− (GHC) by employing Simon Peyton Jones to work on it.
76−* Tesla has been regularly hiring Haskell engineers and interns for some years
77− already. There isn’t a lot of information out there about the exact specifics
78− of their work. According to a comment in a job post made on Reddit, they use
79− Haskell to generate C code that is then compiled into vehicle firmware.
80−* Freckle is an online learning platform that adjusts to the skills of students.
81− They use Haskell for all of their backend services and most of their tooling.
82−* Galois is a company that specializes in the research and development of
83− trustworthy software systems for fields where failure is unacceptable. One of
84− their most interesting projects is Copilot, a stream-based DSL for writing and
85− monitoring embedded C programs.
86−* Serokell uses Haskell to great success for most of their day-to-day software
87− development projects. They have used it to build the settlement layer of the
88− Cardano blockchain, programming languages for the Tezos blockchain,
89− cryptocurrency exchanges, a multi-currency wallet with its own DSL, and more.
90−
91−What are the benefits of using Haskell?
92−---------------------------------------
19+ * The proof assistant Agda is written in Haskell.
20+ * Darcs is a revision control system written in Haskell, with several
21+ innovative features, such as more precise control of patches to apply.
22+ * GHC is also often a testbed for advanced functional programming features and
23+ optimizations in other programming languages.
24+ * Git-annex is a tool to manage (big) data files under Git version control. It
25+ also provides a distributed file synchronization system
26+ (git-annex assistant).
27+ * Linspire Linux chose Haskell for system tools development.
28+ * Pandoc is a tool to convert one markup format into another.
29+ * Pugs is a compiler and interpreter for the Raku programming language
30+ (formerly Perl 6).
31+ * TidalCycles is a domain special language for live coding musical patterns,
32+ embedded in Haskell.
33+ * Xmonad is a window manager for the X Window System written fully in Haskell.
34+ * GarganText is a collaborative tool to map through semantic analysis texts on
35+ any web browser, written fully in Haskell and PureScript, which is used for
36+ instance in the research community to draw up state-of-the-art reports and
37+ roadmaps.
38+
39+*** Industry
40+
41+ * Bluespec SystemVerilog (BSV) is a language for semiconductor design that is
42+ an extension of Haskell. Also, Bluespec, Inc.'s tools are implemented in
43+ Haskell. Note this is an example of a DSL embedded into Haskell.
44+ * Cryptol, a language and toolchain for developing and verifying cryptography
45+ algorithms, is implemented in Haskell.
46+ * Facebook implements its anti-spam programs in Haskell, maintaining the
47+ underlying data access library as open-source software.
48+ * The Cardano blockchain platform is implemented in Haskell.
49+ * GitHub implemented Semantic, an open-source library for analysis, diffing,
50+ and interpretation of untrusted source code, in Haskell.
51+ * Standard Chartered's financial modelling language Mu is syntactic Haskell
52+ running on a strict runtime.
53+ * seL4, the first formally verified microkernel, used Haskell as a prototyping
54+ language for the OS developer. At the same time, the Haskell code defined an
55+ executable specification with which to reason, for automatic translation by
56+ the theorem-proving tool. The Haskell code thus served as an intermediate
57+ prototype before final C refinement.
58+ * Target stores' supply chain optimization software is written in Haskell.
59+ * Co–Star
60+
61+*** Some more examples from Serokell
62+
63+ * Hasura is an open-source GraphQL engine that gives you instant access to a
64+ GraphQL API for your data. In the GitHub repository of Hasura, Haskell is
65+ the most used language, followed by languages like TypeScript, JavaScript,
66+ Python, and Go. At the start of 2022, Hasura raised \$100 million.
67+ * Microsoft uses Haskell in Bond, a cross-platform framework for working with
68+ schematized data. The Haskell part of Bond is mainly gbc, a command-line
69+ code generation tool that can generate C++ and C# code using Bond. For a
70+ long time, Microsoft Research also significantly supported the Glasgow
71+ Haskell Compiler (GHC) by employing Simon Peyton Jones to work on it.
72+ * Tesla has been regularly hiring Haskell engineers and interns for some years
73+ already. There isn’t a lot of information out there about the exact
74+ specifics of their work. According to a comment in a job post made on
75+ Reddit, they use Haskell to generate C code that is then compiled into
76+ vehicle firmware.
77+ * Freckle is an online learning platform that adjusts to the skills of
78+ students. They use Haskell for all of their backend services and most of
79+ their tooling.
80+ * Galois is a company that specializes in the research and development of
81+ trustworthy software systems for fields where failure is unacceptable.
82+ One of their most interesting projects is Copilot, a stream-based DSL for
83+ writing and monitoring embedded C programs.
84+ * Serokell uses Haskell to great success for most of their day-to-day software
85+ development projects. They have used it to build the settlement layer of the
86+ Cardano blockchain, programming languages for the Tezos blockchain,
87+ cryptocurrency exchanges, a multi-currency wallet with its own DSL and more.
88+
89+** What are the benefits of using Haskell?
9390
9491Companies above list three things as the most significant benefits of Haskell:
9592
96−1. Expressive type system.
97−2. Strong correctness guarantees.
98−3. Easy refactoring.
93+ 1. Expressive type system.
94+ 2. Strong correctness guarantees.
95+ 3. Easy refactoring.
9996
10097In addition, while Haskell is a great programming language for everyday
10198purposes, we can separate two popular use cases:
10299
103−1. Complex systems where strong guarantees are required (such as blockchains
104− like Cardano and fintech projects like Klarna).
105−2. The second is compilers and DSLs. On our list, the examples for this use case
106− are projects like Bond, Semantic, Copilot.
100+ 1. Complex systems where strong guarantees are required (such as blockchains
101+ like Cardano and fintech projects like Klarna).
102+ 2. The second is compilers and DSLs. On our list, the examples for this use
103+ case are projects like Bond, Semantic, Copilot.
107104
108105Both of these use cases heavily benefit from the strengths of Haskell and manage
109106to dodge most of its cons. They can rightfully be called the “killer apps” of
@@ -112,23 +109,21 @@ Haskell.
112109If you are considering a project in these two areas, it’s hard to go wrong with
113110picking Haskell.
114111
115−Basic language constructs
116−=========================
112+* Basic language constructs
117113
118−This file is actually a _literate Haskell_ file with extension `.lhs` where
119−every piece of code is indented with `>` like this:
114+This file is actually a /literate Haskell/ file with extension @.lhs@ where
115+every piece of code is indented with @>@ like this:
120116
121117> a = 5
122118
123−Normal code is written without indent and is split into _modules_ each in their
124−own file with extension `.hs`. To write a comment, a double dash is used:
119+Normal code is written without indent and is split into /modules/ each in their
120+own file with extension @.hs@. To write a comment, a double dash is used:
125121
126122> -- This is a comment inside code block in a literate Haskell file.
127123> {- And this is a multiline comment.
128124> A little useless, isn't it? -}
129125
130−Primitive Datatypes and Operators
131−---------------------------------
126+** Primitive Datatypes and Operators
132127
133128Of course, Haskell has numbers, booleans, characters, strings and whatnot:
134129
@@ -137,17 +132,17 @@ Of course, Haskell has numbers, booleans, characters, strings and whatnot:
137132> d = 10 * 2 -- 20
138133> e = 35 / 5
139134
140−However, if you load this file into a Haskell interpreter called `ghci`, you'll
135+However, if you load this file into a Haskell interpreter called @ghci@, you'll
141136see the following:
142137
143−```
144−$ ghci 1.Intro.lhs
138+@
139+~/haskell-lectures-2024 $ ghci 1.Intro.lhs
145140GHCi, version 9.4.8: https://www.haskell.org/ghc/ :? for help
146141[1 of 2] Compiling Main ( 1.Intro.lhs, interpreted )
147142Ok, one module loaded.
148143ghci> e
1491447.0
150−```
145+@
151146
152147So division is not integer division by default. Actual integer division:
153148
@@ -163,9 +158,9 @@ Boolean operations:
163158> k = 1 /= 1
164159> l = 1 < 10
165160
166−In the above examples, `not` is a function that takes one value. Haskell doesn't
161+In the above examples, @not@ is a function that takes one value. Haskell doesn't
167162need parentheses for function calls --- all the arguments are just listed after
168−the function. So the general pattern is: `func arg1 arg2 arg3...` See the
163+the function. So the general pattern is: @func arg1 arg2 arg3...@ See the
169164section on functions for information on how to write your own.
170165
171166Strings and characters:
@@ -182,12 +177,11 @@ A string is a list of characters:
182177
183178> q = ['H', 'e', 'l', 'l', 'o']
184179
185−Lists can be indexed with the `!!` operator followed by an index
180+Lists can be indexed with the @!!@ operator followed by an index
186181
187182> s = "This is a string" !! 0
188183
189−Lists
190−-----
184+** Lists
191185
192186Every element in a list must have the same type. These two lists are equal:
193187
@@ -219,7 +213,7 @@ element of your list and Haskell will give it to you:
219213
220214> someNat = [1..] !! 999 -- 1000
221215
222−And now Haskell has evaluated elements 1 - 1000 of this list...but the rest of
216+And now Haskell has evaluated elements 1 - 1000 of this list... but the rest of
223217the elements of this "infinite" list don't exist yet! Haskell won't actually
224218evaluate them until it needs to.
225219
@@ -253,8 +247,7 @@ With several iteratees
253247
254248> twoD = take 10 [x ^ 2 * y | x <- [1..], even x, x `mod` 3 == 0, y <- [1, -1]]
255249
256−Tuples
257−------
250+** Tuples
258251
259252Every element in a tuple can be a different type, but a tuple has a fixed
260253length. A tuple:
@@ -270,8 +263,7 @@ Pair element accessing does not work on n-tuples (i.e. triple, quadruple, etc)
270263
271264> -- snd ("snd", "can't touch this", "da na na na") -- error! see function below
272265
273−Functions
274−---------
266+** Functions
275267
276268A simple function that takes two variables
277269
@@ -291,8 +283,7 @@ own operators! Here's an operator that does integer division
291283> (//) a b = a `div` b
292284> eight = 35 // 4 -- 8
293285
294−Guards
295−~~~~~~
286+*** Guards
296287
297288An easy way to do branching in functions
298289
@@ -300,26 +291,23 @@ An easy way to do branching in functions
300291> | x < 2 = 1
301292> | otherwise = fib (x - 1) + fib (x - 2)
302293
303−Pattern matching
304−~~~~~~~~~~~~~~~~
294+*** Pattern matching
305295
306296Pattern matching is similar. Here we have given three different equations that
307−define `fib'`. Haskell will automatically use the first equation whose left hand
297+define @fib'@. Haskell will automatically use the first equation whose left hand
308298side pattern matches the value.
309299
310300> fib' 1 = 1
311301> fib' 2 = 2
312302> fib' x = fib' (x - 1) + fib' (x - 2)
313303
314−Pattern matching on tuples
315−~~~~~~~~~~~~~~~~~~~~~~~~~~
304+*** Pattern matching on tuples
316305
317306> sndOfTriple (_, y, _) = y -- use a wild card (_) to bypass naming unused value
318307
319−Pattern matching on lists
320−~~~~~~~~~~~~~~~~~~~~~~~~~
308+*** Pattern matching on lists
321309
322−Here `x` is the first element in the list, and `xs` is the rest of the list. We
310+Here @x@ is the first element in the list, and @xs@ is the rest of the list. We
323311can write our own map function:
324312
325313> myMap func [] = []
@@ -329,16 +317,14 @@ Anonymous functions are created with a backslash followed by all the arguments.
329317
330318> mapped = myMap (\x -> x + 2) [1..5] -- [3, 4, 5, 6, 7]
331319
332−Using fold (called `inject` in some languages) with an anonymous function
333−~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
320+*** Using fold (called @inject@ in some languages) with an anonymous function
334321
335322foldl1 means fold left, and use the first value in the list as the initial value
336323for the accumulator.
337324
338325> usingFold = foldl1 (\acc x -> acc + x) [1..5] -- 15
339326
340−Partial application
341−~~~~~~~~~~~~~~~~~~~
327+*** Partial application
342328
343329If you don't pass in all the arguments to a function, it gets "partially
344330applied". That means it returns a function that takes the rest of the arguments.
@@ -351,22 +337,20 @@ Another way to write the same thing. It is called a "section":
351337> foo' = (10 +)
352338> alsoFifteen = foo' 5 -- 15
353339
354−Function composition
355−~~~~~~~~~~~~~~~~~~~~
340+*** Function composition
356341
357−The operator `.` chains functions together. For example, here foo is a function
342+The operator @.@ chains functions together. For example, here foo is a function
358343that takes a value. It adds 10 to it, multiplies the result of that by 4, and
359344then returns the final value.
360345
361346> foo'' = (4 *) . (10 +)
362347> sixty = foo'' 5 -- 4*(10+5) = 60
363348
364−Fixing precedence
365−~~~~~~~~~~~~~~~~~
349+*** Fixing precedence
366350
367−Haskell has an operator called `$`. This operator applies a function to a given
351+Haskell has an operator called @$@. This operator applies a function to a given
368352parameter. In contrast to standard function application, which has highest
369−possible priority of 10 and is left-associative, the `$` operator has priority
353+possible priority of 10 and is left-associative, the @$@ operator has priority
370354of 0 and is right-associative. Such a low priority means that the expression on
371355its right is applied as a parameter to the function on its left.
372356
@@ -382,8 +366,7 @@ Composing functions:
382366
383367> evenSeven'' = even . fib $ 7 -- false
384368
385−Type signatures
386−---------------
369+** Type signatures
387370
388371Haskell has a very strong type system, and every valid expression has a type.
389372Some basic types:
@@ -392,7 +375,7 @@ Some basic types:
392375> hello = "hello" :: String
393376> truth = True :: Bool
394377
395−Functions have types too. `not` takes a boolean and returns a boolean:
378+Functions have types too. @not@ takes a boolean and returns a boolean:
396379
397380> not' = not :: Bool -> Bool
398381
@@ -405,8 +388,7 @@ When you define a value, it's good practice to write its type above it:
405388> double :: Integer -> Integer
406389> double x = x * 2
407390
408−Typed holes
409−~~~~~~~~~~~
391+*** Typed holes
410392
411393Haskell's type system allows one to use "typed holes" -- you can replace an
412394expression with an underscore and a compiler would tell you what is the expected
@@ -414,8 +396,7 @@ type of a missing expression:
414396
415397> -- double' x = _ * 2 :: Integer
416398
417−Control Flow and If Expressions
418−-------------------------------
399+** Control Flow and If Expressions
419400
420401if-expressions
421402
@@ -455,7 +436,7 @@ We could've written that like this too:
455436
456437> strs = for [0..5] show
457438
458−You can use foldl or foldr to reduce a list: foldl <fn> <initial value> <list>
439+You can use foldl or foldr to reduce a list: @foldl <fn> <initial value> <list>@
459440
460441> fortyThree = foldl (\x y -> 2*x + y) 4 [1,2,3] -- 43
461442
@@ -467,8 +448,7 @@ While foldl is left-handed, foldr is right-handed
467448
468449This is now the same as (2 * 1 + (2 * 2 + (2 * 3 + 4)))
469450
470−Data Types
471−----------
451+** Data Types
472452
473453A data type is declared with a 'type constructor' on the left and one or more
474454'data constructors' on the right, separated by the pipe | symbol. This is a
@@ -539,21 +519,21 @@ It's also useful to pattern match data constructors in `case` expressions
539519
540520Your data types can have type parameters too:
541521
542−```haskell
522+@
543523data Maybe a = Nothing | Just a
544−```
524+@
545525
546−These are all Maybes, but with different `a`s:
526+These are all Maybes, but with different @a@s:
547527
548528> justHello = Just "hello" -- of type `Maybe String`
549529> justOne = Just 1 -- of type `Maybe Int`
550530> nothing = Nothing -- of type `Maybe a` for any `a`
551531
552−For convenience we can also create type synonyms with the 'type' keyword
532+For convenience we can also create type synonyms with the @type@ keyword
553533
554534> type String' = [Char]
555535
556−Unlike `data` types, type synonyms need no constructor, and can be used anywhere
536+Unlike @data@ types, type synonyms need no constructor, and can be used anywhere
557537a synonymous data type could be used. Say we have the following type synonyms
558538and items with the following type signatures
559539
@@ -586,8 +566,7 @@ sense semantically, because the type synonyms reduce to the same base types:
586566
587567> nonsense = distance2D (getMyHeightAndWeight somePerson) (findCenter someCircle)
588568
589−Typeclasses
590−-----------
569+** Typeclasses
591570
592571Typeclasses are one way Haskell does polymorphism. They are similar to
593572interfaces in other languages: a typeclass defines a set of functions that must
@@ -598,20 +577,20 @@ The
598577typeclass is for types whose instances can be tested for equality with one
599578another.
600579
601−```Haskell
580+@
602581class Eq a where
603582 (==) :: a -> a -> Bool
604583 (/=) :: a -> a -> Bool
605584 x == y = not (x /= y)
606585 x /= y = not (x == y)
607−```
586+@
608587
609−This defines a typeclass that requires two functions, (==) and (/=). It also
588+This defines a typeclass that requires two functions, @(==)@ and @(/=)@. It also
610589declares that one function can be declared in terms of another, so it is enough
611−that *either* the (==) function or the (/=) is defined and the other will be
612−'filled in' based on the typeclass definition.
590+that __either__ the @(==)@ function or the @(/=)@ is defined and the other will
591+be "filled in" based on the typeclass definition.
613592
614−To make a type a member of a type class, the `instance` keyword is used
593+To make a type a member of a type class, the @instance@ keyword is used
615594
616595> data TrafficLight = TLRed | TLYellow | TLGreen
617596>
@@ -621,13 +600,13 @@ To make a type a member of a type class, the `instance` keyword is used
621600> TLYellow == TLYellow = True
622601> _ == _ = False
623602
624−(Note that, while declaring `TrafficLight` constructors, we had to name
625−constructors differently in order for them to not clash with `Color`
603+(Note that, while declaring @TrafficLight@ constructors, we had to name
604+constructors differently in order for them to not clash with @Color@
626605constructors we defined earlier. It is common to either prefix constructor
627606names with type name abbreviation or define types in different modules
628607altogether)
629608
630−Now we can use `(==)` and `(/=)` with TrafficLight objects:
609+Now we can use @(==)@ and @(/=)@ with TrafficLight objects:
631610
632611> canProceedThrough :: TrafficLight -> Bool
633612> canProceedThrough t = t /= TLRed
@@ -641,31 +620,30 @@ types, assuming that the function only relies on features of the typeclass:
641620> isEqual x y = x == y
642621
643622Note that x and y MUST be the same type, as they are both defined as being of
644−type parameter 'a'. A typeclass does not state that different types in the
645−typeclass can be mixed together. So `isEqual Red 2` is invalid even though 2 is
623+type parameter @a@. A typeclass does not state that different types in the
624+typeclass can be mixed together. So @isEqual Red 2@ is invalid even though 2 is
646625an Int which is an instance of Eq, and Red is a TrafficLight which is also an
647626instance of Eq.
648627
649628Other common typeclasses are:
650629
651−* `Ord` for types that can be ordered, allowing you to use >, <=, etc.
652−* `Read` for types that can be created from a string representation
653−* `Show` for types that can be converted to a string for display
654−* `Num`, `Real`, `Integral`, `Fractional` for types that can do math
655−* `Enum` for types that can be stepped through
656−* `Bounded` for types with a maximum and minimum
630+ * @Ord@ for types that can be ordered, allowing you to use >, <=, etc.
631+ * @Read@ for types that can be created from a string representation
632+ * @Show@ for types that can be converted to a string for display
633+ * @Num@, @Real@, @Integral@, @Fractional@ for types that can do math
634+ * @Enum@ for types that can be stepped through
635+ * @Bounded@ for types with a maximum and minimum
657636
658−Literals
659−~~~~~~~~
637+*** Literals
660638
661639We used character, string and list literals back from the very start. It is
662640simple to understand what they mean and how to use them. However, numeric
663641literals are not so simple. If we ask GHCi: "what is the type of 2?", we get:
664642
665−```
643+@
666644ghci> :t 2
6676452 :: Num a => a
668−```
646+@
669647
670648So numeric literals are actually polymorphic and do not have a concrete type.
671649Not that this is not the same as casts because the following is not allowed:
@@ -673,29 +651,27 @@ Not that this is not the same as casts because the following is not allowed:
673651> two = 2 :: Integer
674652> -- float = two * 0.5 -- would not pass the type check
675653
676−Deriving
677−~~~~~~~~
654+*** Deriving
678655
679656Haskell can automatically make types part of Eq, Ord, Read, Show, Enum and
680−Bounded with the `deriving` keyword at the end of the type declaration
657+Bounded with the @deriving@ keyword at the end of the type declaration
681658
682659> data PointDerives = Point Float Float deriving (Eq, Read, Show)
683660
684−In this case it is NOT necessary to create an 'instance' definition.
661+In this case it is NOT necessary to create an @instance@ definition.
685662
686−Haskell IO
687−----------
663+** Haskell IO
688664
689665While IO can't be explained fully without explaining monads, it is not hard to
690666explain enough to get going.
691667
692−When a Haskell program is executed, `main` is called. It must return a value of
693−type `IO a` for some type `a`. For example:
668+When a Haskell program is executed, @main@ is called. It must return a value of
669+type @IO a@ for some type @a@. For example:
694670
695671> main :: IO ()
696672> main = putStrLn $ "Hello, sky! " ++ (say Blue)
697673
698−(`putStrLn` has type `String -> IO ()`)
674+(@putStrLn@ has type @String -> IO ()@)
699675
700676It is easiest to do IO if you can implement your program as a function from
701677String to String. The function
@@ -709,9 +685,9 @@ Inputs some text, runs a function on it, and prints out the output.
709685
710686> main' = interact countLines
711687
712−You can think of a value of type `IO ()` as representing a sequence of actions
688+You can think of a value of type @IO ()@ as representing a sequence of actions
713689for the computer to do, much like a computer program written in an imperative
714−language. We can use the `do` notation to chain actions together. For example:
690+language. We can use the @do@ notation to chain actions together. For example:
715691
716692> sayHello :: IO ()
717693> sayHello = do
@@ -719,23 +695,32 @@ language. We can use the `do` notation to chain actions together. For example:
719695> name <- getLine -- this gets a line and gives it the name "name"
720696> putStrLn $ "Hello, " ++ name
721697
722−Exercise: write your own version of `interact` that only reads one line of
698+Exercise: write your own version of @interact@ that only reads one line of
723699input.
724700
725−The code in `sayHello` will never be executed, however. The only action that
726−ever gets executed in the source file is the value of `main`. To run `sayHello`,
727−comment out the above definition of `main`, replace it with `main = sayHello`
728−and run `runhaskell <file name>`.
701+The code in @sayHello@ will never be executed, however. The only action that
702+ever gets executed in the source file is the value of @main@. To run @sayHello@,
703+comment out the above definition of @main@, replace it with @main = sayHello@
704+and run @runhaskell <file name>@.
729705
730−Let's understand better how the function `getLine` we just used works. Its type
706+Or, if you are running this module in GHCi, it can run any IO action on demand:
707+
708+@
709+ghci> sayHello
710+What is your name?
711+Pavel
712+Hello, Pavel
713+@
714+
715+Let's understand better how the function @getLine@ we just used works. Its type
731716is:
732717
733718> getLine' = getLine :: IO String
734719
735−You can think of a value of type `IO a` as representing a computer program that
736−will generate a value of type `a` when executed (in addition to anything else it
737−does). We can name and reuse this value using `<-`. We can also make our own
738−action of type `IO String`:
720+You can think of a value of type @IO a@ as representing a computer program that
721+will generate a value of type @a@ when executed (in addition to anything else it
722+does). We can name and reuse this value using @<-@. We can also make our own
723+action of type @IO String@:
739724
740725> action :: IO String
741726> action = do
@@ -746,10 +731,14 @@ action of type `IO String`:
746731> -- `return` is not a keyword, but merely a function
747732> return (input1 ++ "\n" ++ input2) -- return :: String -> IO String
748733
734+However, do-notation is not some compiler magic, this is just a syntactic sugar
735+over two operators called @>>@ and @>>=@. The action below is the same as the
736+action above:
737+
749738> action' = putStrLn "This is a line" >> (getLine >>= (\input1 ->
750739> getLine >>= (\input2 -> return (input1 ++ "\n" ++ input2))))
751740
752−We can use this just like we used `getLine`:
741+We can use this just like we used @getLine@:
753742
754743> main'' = do
755744> putStrLn "I will echo two lines!"
@@ -757,39 +746,39 @@ We can use this just like we used `getLine`:
757746> putStrLn result
758747> putStrLn "This was all, folks!"
759748
760−The type `IO` is an example of a "monad". The way Haskell uses a monad to do IO
749+The type @IO@ is an example of a "monad". The way Haskell uses a monad to do IO
761750allows it to be a purely functional language. Any function that interacts with
762−the outside world (i.e. does IO) gets marked as `IO` in its type signature. This
751+the outside world (i.e. does IO) gets marked as @IO@ in its type signature. This
763752lets us reason about which functions are "pure" (don't interact with the outside
764753world or modify state) and which functions aren't.
765754
766755This is a powerful feature, because it's easy to run pure functions
767756concurrently; so, concurrency in Haskell is very easy.
768757
769−Monads
770−------
758+** Monads
771759
772−Actually, `do`-notation has wider application than only constructing IO values.
773−Is is applicable for every _monad_:
760+Actually, @do@-notation has wider application than only constructing IO values.
761+Is is applicable for every /monad/:
774762
775−```
763+@
776764ghci> :i Monad
777765type Monad :: (* -> *) -> Constraint
778766class Applicative m => Monad m where
779767 (>>=) :: m a -> (a -> m b) -> m b
780768 (>>) :: m a -> m b -> m b
781769 return :: a -> m a
782−```
770+@
771+
772+In other words, @m@ is a monad if it allows to construct "pure" values of type
773+@m a@ from any value of type @a@ and if it allows to sequence actions of type
774+@m a@ and @m b@ and yield a result of type @m b@, where the second action either
775+can (@(>>=)@) or cannot (@(>>)@) depend on the result of the first action.
783776
784−In other words, `m` is a monad if it allows to construct "pure" values of type
785−`m a` from any value of type `a` and if it allows to sequence actions of type
786−`m a` and `m b` and yield a result of type `m b`, where the second action either
787−can (`(>>=)`) or cannot (`(>>)`) depend on the result of the first action.
777+This is discussed more in detail in the next lecture.
788778
789−Sources & References
790−====================
779+* Sources & References
791780
792−* [Wikipedia page on Haskell](https://en.wikipedia.org/wiki/Haskell)
793−* [Serokell blog post on Haskell in industry](https://serokell.io/blog/top-software-written-in-haskell)
794−* [Memo for basic Haskell constructions](https://learnxinyminutes.com/docs/haskell/)
795−* [Hoogle, Haskell code search](https://hoogle.haskell.org/)
781+ * [Wikipedia page on Haskell](https://en.wikipedia.org/wiki/Haskell)
782+ * [Serokell blog post on Haskell in industry](https://serokell.io/blog/top-software-written-in-haskell)
783+ * [Memo for basic Haskell constructions](https://learnxinyminutes.com/docs/haskell/)
784+ * [Hoogle, Haskell code search](https://hoogle.haskell.org/)