-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathLessons-Lesson11.html
More file actions
40 lines (40 loc) · 22.1 KB
/
Lessons-Lesson11.html
File metadata and controls
40 lines (40 loc) · 22.1 KB
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1" /><title>Lessons.Lesson11</title><link href="linuwial.css" rel="stylesheet" type="text/css" title="Linuwial" /><link rel="stylesheet" type="text/css" href="quick-jump.css" /><link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=PT+Sans:400,400i,700" /><script src="haddock-bundle.min.js" async="async" type="text/javascript"></script><script type="text/x-mathjax-config">MathJax.Hub.Config({ tex2jax: { processClass: "mathjax", ignoreClass: ".*" } });</script><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML" type="text/javascript"></script></head><body><div id="package-header"><span class="caption empty"> </span><ul class="links" id="page-menu"><li><a href="src/Lessons.Lesson11.html">Source</a></li><li><a href="index.html">Contents</a></li><li><a href="doc-index.html">Index</a></li></ul></div><div id="content"><div id="module-header"><table class="info"><tr><th>Safe Haskell</th><td>Safe-Inferred</td></tr></table><p class="caption">Lessons.Lesson11</p></div><div id="description"><p class="caption">Description</p><div class="doc"><p>Notes taken by Julija Mikeliūnaitė</p></div></div><div id="synopsis"><details id="syn"><summary>Synopsis</summary><ul class="details-toggle" data-details-id="syn"><li class="src short"><a href="#v:foo">foo</a> :: Integer</li><li class="src short"><a href="#v:c">c</a> :: Exception e => IO (Either e Integer)</li><li class="src short"><a href="#v:eager">eager</a> :: [Integer]</li><li class="src short"><a href="#v:lazy">lazy</a> :: [Integer]</li><li class="src short"><a href="#v:lazy-39-">lazy'</a> :: Int</li><li class="src short"><a href="#v:lazy-39--39-">lazy''</a> :: [Integer]</li><li class="src short"><a href="#v:first">first</a> :: Int -> [Integer]</li><li class="src short"><span class="keyword">data</span> <a href="#t:Expr">Expr</a><ul class="subs"><li>= <a href="#v:Lit">Lit</a> Integer</li><li>| <a href="#v:Add">Add</a> <a href="Lessons-Lesson11.html#t:Expr" title="Lessons.Lesson11">Expr</a> <a href="Lessons-Lesson11.html#t:Expr" title="Lessons.Lesson11">Expr</a></li><li>| <a href="#v:Mul">Mul</a> <a href="Lessons-Lesson11.html#t:Expr" title="Lessons.Lesson11">Expr</a> <a href="Lessons-Lesson11.html#t:Expr" title="Lessons.Lesson11">Expr</a></li><li>| <a href="#v:Neg">Neg</a> <a href="Lessons-Lesson11.html#t:Expr" title="Lessons.Lesson11">Expr</a></li></ul></li><li class="src short"><a href="#v:prog1">prog1</a> :: <a href="Lessons-Lesson11.html#t:Expr" title="Lessons.Lesson11">Expr</a></li><li class="src short"><a href="#v:eval">eval</a> :: <a href="Lessons-Lesson11.html#t:Expr" title="Lessons.Lesson11">Expr</a> -> Integer</li><li class="src short"><a href="#v:print-39-">print'</a> :: <a href="Lessons-Lesson11.html#t:Expr" title="Lessons.Lesson11">Expr</a> -> String</li><li class="src short"><span class="keyword">class</span> <a href="#t:Expression">Expression</a> repr <span class="keyword">where</span><ul class="subs"><li><a href="#v:lit">lit</a> :: Integer -> repr</li><li><a href="#v:add">add</a> :: repr -> repr -> repr</li></ul></li><li class="src short"><span class="keyword">class</span> <a href="#t:ExpressionMul">ExpressionMul</a> repr <span class="keyword">where</span><ul class="subs"><li><a href="#v:mul">mul</a> :: repr -> repr -> repr</li></ul></li><li class="src short"><a href="#v:prog2">prog2</a> :: Integer</li><li class="src short"><a href="#v:prog3">prog3</a> :: String</li><li class="src short"><span class="keyword">data</span> <a href="#t:GExpr">GExpr</a> a <span class="keyword">where</span><ul class="subs"><li><a href="#v:GInt">GInt</a> :: Integer -> <a href="Lessons-Lesson11.html#t:GExpr" title="Lessons.Lesson11">GExpr</a> Integer</li><li><a href="#v:GBool">GBool</a> :: Bool -> <a href="Lessons-Lesson11.html#t:GExpr" title="Lessons.Lesson11">GExpr</a> Bool</li><li><a href="#v:GAdd">GAdd</a> :: <a href="Lessons-Lesson11.html#t:GExpr" title="Lessons.Lesson11">GExpr</a> Integer -> <a href="Lessons-Lesson11.html#t:GExpr" title="Lessons.Lesson11">GExpr</a> Integer -> <a href="Lessons-Lesson11.html#t:GExpr" title="Lessons.Lesson11">GExpr</a> Integer</li><li><a href="#v:GEq">GEq</a> :: Eq a => <a href="Lessons-Lesson11.html#t:GExpr" title="Lessons.Lesson11">GExpr</a> a -> <a href="Lessons-Lesson11.html#t:GExpr" title="Lessons.Lesson11">GExpr</a> a -> <a href="Lessons-Lesson11.html#t:GExpr" title="Lessons.Lesson11">GExpr</a> Bool</li></ul></li><li class="src short"><a href="#v:evalG">evalG</a> :: <a href="Lessons-Lesson11.html#t:GExpr" title="Lessons.Lesson11">GExpr</a> a -> a</li></ul></details></div><div id="interface"><h1>Documentation</h1><div class="top"><p class="src"><a id="v:foo" class="def">foo</a> :: Integer <a href="src/Lessons.Lesson11.html#foo" class="link">Source</a> <a href="#v:foo" class="selflink">#</a></p><div class="doc"><p>Haskell has exceptions. The error function is the simplest way to throw exceptions. error is a pure function and can be any type. Here, the error function works as an integer, therefore it can be returned.</p></div></div><div class="top"><p class="src"><a id="v:c" class="def">c</a> :: Exception e => IO (Either e Integer) <a href="src/Lessons.Lesson11.html#c" class="link">Source</a> <a href="#v:c" class="selflink">#</a></p><div class="doc"><p>For exception cathcing you need the Control.Exception package. The try function needs an IO block.</p></div></div><div class="top"><p class="src"><a id="v:eager" class="def">eager</a> :: [Integer] <a href="src/Lessons.Lesson11.html#eager" class="link">Source</a> <a href="#v:eager" class="selflink">#</a></p><div class="doc"><p>If the code is multithreaded, you can send exceptions to any thread.</p><p>Haskell is considered a lazy programming language. In a lazy language computations are only made if absolutely necessary.</p><p>If one of the items in the list is an error, the final result will be the error. This is an example of a strict/eager function.</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>eager
</code></strong>oj
</pre></div></div><div class="top"><p class="src"><a id="v:lazy" class="def">lazy</a> :: [Integer] <a href="src/Lessons.Lesson11.html#lazy" class="link">Source</a> <a href="#v:lazy" class="selflink">#</a></p><div class="doc"><p>The take x function takes the first x elements from the list and returns them. This is an example of a lazy function.</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>lazy
</code></strong>[1,2]
</pre></div></div><div class="top"><p class="src"><a id="v:lazy-39-" class="def">lazy'</a> :: Int <a href="src/Lessons.Lesson11.html#lazy%27" class="link">Source</a> <a href="#v:lazy-39-" class="selflink">#</a></p><div class="doc"><p>length is also a lazy function, because it counts how many items are in a list but does not care about their type (even if some of them may be errors).</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>lazy'
</code></strong>1
</pre></div></div><div class="top"><p class="src"><a id="v:lazy-39--39-" class="def">lazy''</a> :: [Integer] <a href="src/Lessons.Lesson11.html#lazy%27%27" class="link">Source</a> <a href="#v:lazy-39--39-" class="selflink">#</a></p><div class="doc"><p>With take 1, if the first element is an error, then the result is the error.</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>lazy''
</code></strong>ooops
</pre></div></div><div class="top"><p class="src"><a id="v:first" class="def">first</a> :: Int -> [Integer] <a href="src/Lessons.Lesson11.html#first" class="link">Source</a> <a href="#v:first" class="selflink">#</a></p><div class="doc"><p>Even though the integer list is infinite, first 10 works fine, because of the laziness. If you were to print out the whole list [1..], it would just keep going forever to infinity.</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>first 10
</code></strong>[1,2,3,4,5,6,7,8,9,10]
</pre></div></div><div class="top"><p class="src"><span class="keyword">data</span> <a id="t:Expr" class="def">Expr</a> <a href="src/Lessons.Lesson11.html#Expr" class="link">Source</a> <a href="#t:Expr" class="selflink">#</a></p><div class="doc"><p>Lists are constructed using :, therefore the first n elements can be checked one by one.</p><p>Eager calculations - trying to compute all data at once and store everything in memory. It can be achieved using libraries with the keyword <a href="Strict.html">Strict</a>.</p><p>seq function: </p><p>seq :: a -> b -> b
Simplest explanation: the return value is bottom if a is bottom, otherwise it is b.</p><p>Returns 5 and ensures that everything was calculated/checked.</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>seq 4 5
</code></strong>5
</pre><p>Returns 5, because seq removes the top layer of what is uncalculated.</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>seq [error "Aj"] 5
</code></strong>5
</pre><p>Does all of the calculations, no matter the layer.</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>deepseq ([error "Aj"]:: [Int]) 5
</code></strong>Aj
</pre><p>DSL - domain specific language. In our DSL's we had differend commands - a dictionary of tasks which we can use within our language. A classic example - calculator.</p><p>Expr (expression) can be either a literal (number) or an operation for two expressions (add, mul) or an operation for one expression (neg). This is already a DSL and now we can write programs using it.</p></div><div class="subs constructors"><p class="caption">Constructors</p><table><tr><td class="src"><a id="v:Lit" class="def">Lit</a> Integer</td><td class="doc empty"> </td></tr><tr><td class="src"><a id="v:Add" class="def">Add</a> <a href="Lessons-Lesson11.html#t:Expr" title="Lessons.Lesson11">Expr</a> <a href="Lessons-Lesson11.html#t:Expr" title="Lessons.Lesson11">Expr</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a id="v:Mul" class="def">Mul</a> <a href="Lessons-Lesson11.html#t:Expr" title="Lessons.Lesson11">Expr</a> <a href="Lessons-Lesson11.html#t:Expr" title="Lessons.Lesson11">Expr</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a id="v:Neg" class="def">Neg</a> <a href="Lessons-Lesson11.html#t:Expr" title="Lessons.Lesson11">Expr</a></td><td class="doc empty"> </td></tr></table></div><div class="subs instances"><h4 class="instances details-toggle-control details-toggle" data-details-id="i:Expr">Instances</h4><details id="i:Expr" open="open"><summary class="hide-when-js-enabled">Instances details</summary><table><tr><td class="src clearfix"><span class="inst-left"><span class="instance details-toggle-control details-toggle" data-details-id="i:id:Expr:Show:1"></span> Show <a href="Lessons-Lesson11.html#t:Expr" title="Lessons.Lesson11">Expr</a></span> <a href="src/Lessons.Lesson11.html#line-96" class="link">Source</a> <a href="#t:Expr" class="selflink">#</a></td><td class="doc empty"> </td></tr><tr><td colspan="2"><details id="i:id:Expr:Show:1"><summary class="hide-when-js-enabled">Instance details</summary><p>Defined in <a href="Lessons-Lesson11.html">Lessons.Lesson11</a></p> <div class="subs methods"><p class="caption">Methods</p><p class="src"><a href="#v:showsPrec">showsPrec</a> :: Int -> <a href="Lessons-Lesson11.html#t:Expr" title="Lessons.Lesson11">Expr</a> -> ShowS</p><p class="src"><a href="#v:show">show</a> :: <a href="Lessons-Lesson11.html#t:Expr" title="Lessons.Lesson11">Expr</a> -> String</p><p class="src"><a href="#v:showList">showList</a> :: [<a href="Lessons-Lesson11.html#t:Expr" title="Lessons.Lesson11">Expr</a>] -> ShowS</p></div></details></td></tr></table></details></div></div><div class="top"><p class="src"><a id="v:prog1" class="def">prog1</a> :: <a href="Lessons-Lesson11.html#t:Expr" title="Lessons.Lesson11">Expr</a> <a href="src/Lessons.Lesson11.html#prog1" class="link">Source</a> <a href="#v:prog1" class="selflink">#</a></p><div class="doc"><p>Program</p></div></div><div class="top"><p class="src"><a id="v:eval" class="def">eval</a> :: <a href="Lessons-Lesson11.html#t:Expr" title="Lessons.Lesson11">Expr</a> -> Integer <a href="src/Lessons.Lesson11.html#eval" class="link">Source</a> <a href="#v:eval" class="selflink">#</a></p><div class="doc"><p>This is used to define how the program should act with different expressions.</p><p>Evaluate
>>> eval prog1
-15</p></div></div><div class="top"><p class="src"><a id="v:print-39-" class="def">print'</a> :: <a href="Lessons-Lesson11.html#t:Expr" title="Lessons.Lesson11">Expr</a> -> String <a href="src/Lessons.Lesson11.html#print%27" class="link">Source</a> <a href="#v:print-39-" class="selflink">#</a></p><div class="doc"><p>This is used for printing different expressions to the console.</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>print' prog1
</code></strong>"-(5 + 4 + 6)"
</pre></div></div><div class="top"><p class="src"><span class="keyword">class</span> <a id="t:Expression" class="def">Expression</a> repr <span class="keyword">where</span> <a href="src/Lessons.Lesson11.html#Expression" class="link">Source</a> <a href="#t:Expression" class="selflink">#</a></p><div class="doc"><p>If the grammar is not initial, it's either final or tagless final. Tagless - because it doesnt have tags (like lit, add and so on). FInal - because it is the final function. Classes are used to achieve this. Classes define what we can do with something and not how to construct a value. For example, the lit function takes an integer and returns its representation.</p></div><div class="subs methods"><p class="caption">Methods</p><p class="src"><a id="v:lit" class="def">lit</a> :: Integer -> repr <a href="src/Lessons.Lesson11.html#lit" class="link">Source</a> <a href="#v:lit" class="selflink">#</a></p><p class="src"><a id="v:add" class="def">add</a> :: repr -> repr -> repr <a href="src/Lessons.Lesson11.html#add" class="link">Source</a> <a href="#v:add" class="selflink">#</a></p></div><div class="subs instances"><h4 class="instances details-toggle-control details-toggle" data-details-id="i:Expression">Instances</h4><details id="i:Expression" open="open"><summary class="hide-when-js-enabled">Instances details</summary><table><tr><td class="src clearfix"><span class="inst-left"><span class="instance details-toggle-control details-toggle" data-details-id="i:ic:Expression:Expression:1"></span> <a href="Lessons-Lesson11.html#t:Expression" title="Lessons.Lesson11">Expression</a> String</span> <a href="src/Lessons.Lesson11.html#line-144" class="link">Source</a> <a href="#t:Expression" class="selflink">#</a></td><td class="doc"><p>You don't need to cover every instance for printing, it all depends on what you need for your project/business. lit = show, because lit a = show a is redundant since they both have the same signature.</p></td></tr><tr><td colspan="2"><details id="i:ic:Expression:Expression:1"><summary class="hide-when-js-enabled">Instance details</summary><p>Defined in <a href="Lessons-Lesson11.html">Lessons.Lesson11</a></p> <div class="subs methods"><p class="caption">Methods</p><p class="src"><a href="#v:lit">lit</a> :: Integer -> String <a href="src/Lessons.Lesson11.html#lit" class="link">Source</a> <a href="#v:lit" class="selflink">#</a></p><p class="src"><a href="#v:add">add</a> :: String -> String -> String <a href="src/Lessons.Lesson11.html#add" class="link">Source</a> <a href="#v:add" class="selflink">#</a></p></div></details></td></tr><tr><td class="src clearfix"><span class="inst-left"><span class="instance details-toggle-control details-toggle" data-details-id="i:ic:Expression:Expression:2"></span> <a href="Lessons-Lesson11.html#t:Expression" title="Lessons.Lesson11">Expression</a> Integer</span> <a href="src/Lessons.Lesson11.html#line-135" class="link">Source</a> <a href="#t:Expression" class="selflink">#</a></td><td class="doc"><p>The interpretor for the grammar.</p></td></tr><tr><td colspan="2"><details id="i:ic:Expression:Expression:2"><summary class="hide-when-js-enabled">Instance details</summary><p>Defined in <a href="Lessons-Lesson11.html">Lessons.Lesson11</a></p> <div class="subs methods"><p class="caption">Methods</p><p class="src"><a href="#v:lit">lit</a> :: Integer -> Integer <a href="src/Lessons.Lesson11.html#lit" class="link">Source</a> <a href="#v:lit" class="selflink">#</a></p><p class="src"><a href="#v:add">add</a> :: Integer -> Integer -> Integer <a href="src/Lessons.Lesson11.html#add" class="link">Source</a> <a href="#v:add" class="selflink">#</a></p></div></details></td></tr></table></details></div></div><div class="top"><p class="src"><span class="keyword">class</span> <a id="t:ExpressionMul" class="def">ExpressionMul</a> repr <span class="keyword">where</span> <a href="src/Lessons.Lesson11.html#ExpressionMul" class="link">Source</a> <a href="#t:ExpressionMul" class="selflink">#</a></p><div class="doc"><p>A new class is used to add multiplication.</p></div><div class="subs methods"><p class="caption">Methods</p><p class="src"><a id="v:mul" class="def">mul</a> :: repr -> repr -> repr <a href="src/Lessons.Lesson11.html#mul" class="link">Source</a> <a href="#v:mul" class="selflink">#</a></p></div><div class="subs instances"><h4 class="instances details-toggle-control details-toggle" data-details-id="i:ExpressionMul">Instances</h4><details id="i:ExpressionMul" open="open"><summary class="hide-when-js-enabled">Instances details</summary><table><tr><td class="src clearfix"><span class="inst-left"><span class="instance details-toggle-control details-toggle" data-details-id="i:ic:ExpressionMul:ExpressionMul:1"></span> <a href="Lessons-Lesson11.html#t:ExpressionMul" title="Lessons.Lesson11">ExpressionMul</a> Integer</span> <a href="src/Lessons.Lesson11.html#line-155" class="link">Source</a> <a href="#t:ExpressionMul" class="selflink">#</a></td><td class="doc empty"> </td></tr><tr><td colspan="2"><details id="i:ic:ExpressionMul:ExpressionMul:1"><summary class="hide-when-js-enabled">Instance details</summary><p>Defined in <a href="Lessons-Lesson11.html">Lessons.Lesson11</a></p> <div class="subs methods"><p class="caption">Methods</p><p class="src"><a href="#v:mul">mul</a> :: Integer -> Integer -> Integer <a href="src/Lessons.Lesson11.html#mul" class="link">Source</a> <a href="#v:mul" class="selflink">#</a></p></div></details></td></tr></table></details></div></div><div class="top"><p class="src"><a id="v:prog2" class="def">prog2</a> :: Integer <a href="src/Lessons.Lesson11.html#prog2" class="link">Source</a> <a href="#v:prog2" class="selflink">#</a></p><div class="doc"><p>Recreating prog1, just without negation; later we add multiplication.</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>prog2
</code></strong>90
</pre></div></div><div class="top"><p class="src"><a id="v:prog3" class="def">prog3</a> :: String <a href="src/Lessons.Lesson11.html#prog3" class="link">Source</a> <a href="#v:prog3" class="selflink">#</a></p><div class="doc"><p>Returns a string of the whole expression.
>>> prog3
"5 + 4 + 6"</p></div></div><div class="top"><p class="src"><span class="keyword">data</span> <a id="t:GExpr" class="def">GExpr</a> a <span class="keyword">where</span> <a href="src/Lessons.Lesson11.html#GExpr" class="link">Source</a> <a href="#t:GExpr" class="selflink">#</a></p><div class="doc"><p>This solves the issue with types by using multiple constructors.</p></div><div class="subs constructors"><p class="caption">Constructors</p><table><tr><td class="src"><a id="v:GInt" class="def">GInt</a> :: Integer -> <a href="Lessons-Lesson11.html#t:GExpr" title="Lessons.Lesson11">GExpr</a> Integer</td><td class="doc empty"> </td></tr><tr><td class="src"><a id="v:GBool" class="def">GBool</a> :: Bool -> <a href="Lessons-Lesson11.html#t:GExpr" title="Lessons.Lesson11">GExpr</a> Bool</td><td class="doc empty"> </td></tr><tr><td class="src"><a id="v:GAdd" class="def">GAdd</a> :: <a href="Lessons-Lesson11.html#t:GExpr" title="Lessons.Lesson11">GExpr</a> Integer -> <a href="Lessons-Lesson11.html#t:GExpr" title="Lessons.Lesson11">GExpr</a> Integer -> <a href="Lessons-Lesson11.html#t:GExpr" title="Lessons.Lesson11">GExpr</a> Integer</td><td class="doc empty"> </td></tr><tr><td class="src"><a id="v:GEq" class="def">GEq</a> :: Eq a => <a href="Lessons-Lesson11.html#t:GExpr" title="Lessons.Lesson11">GExpr</a> a -> <a href="Lessons-Lesson11.html#t:GExpr" title="Lessons.Lesson11">GExpr</a> a -> <a href="Lessons-Lesson11.html#t:GExpr" title="Lessons.Lesson11">GExpr</a> Bool</td><td class="doc empty"> </td></tr></table></div></div><div class="top"><p class="src"><a id="v:evalG" class="def">evalG</a> :: <a href="Lessons-Lesson11.html#t:GExpr" title="Lessons.Lesson11">GExpr</a> a -> a <a href="src/Lessons.Lesson11.html#evalG" class="link">Source</a> <a href="#v:evalG" class="selflink">#</a></p><div class="doc"><p>You can't add GItn and GBool since they are of different types.</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>evalG (GInt 5)
</code></strong>5
<code class="prompt">>>> </code><strong class="userinput"><code>evalG (GAdd (GInt 5) (GInt 6))
</code></strong>11
<code class="prompt">>>> </code><strong class="userinput"><code>evalG (GEq (GInt 5) (GInt 6))
</code></strong>False
<code class="prompt">>>> </code><strong class="userinput"><code>evalG (GAdd (GInt 5) (GBool False))
</code></strong>Couldn't match type `Bool' with `Integer'
Expected: GExpr Integer
Actual: GExpr Bool
In the second argument of `GAdd', namely `(GBool False)'
In the first argument of `evalG', namely
`(GAdd (GInt 5) (GBool False))'
In the expression: evalG (GAdd (GInt 5) (GBool False))
</pre></div></div></div></div><div id="footer"><p>Produced by <a href="http://www.haskell.org/haddock/">Haddock</a> version 2.29.2</p></div></body></html>