-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathLessons-Lesson10.html
More file actions
26 lines (26 loc) · 13.4 KB
/
Lessons-Lesson10.html
File metadata and controls
26 lines (26 loc) · 13.4 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
<!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.Lesson10</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.Lesson10.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.Lesson10</p></div><div id="description"><p class="caption">Description</p><div class="doc"><p>Notes taken by Deimantė Davidavičiūtė</p></div></div><div id="synopsis"><details id="syn"><summary>Synopsis</summary><ul class="details-toggle" data-details-id="syn"><li class="src short"><span class="keyword">type</span> <a href="#t:ErrorMsg">ErrorMsg</a> = String</li><li class="src short"><span class="keyword">type</span> <a href="#t:Input">Input</a> = String</li><li class="src short"><span class="keyword">type</span> <a href="#t:Parser">Parser</a> a = ExceptT <a href="Lessons-Lesson10.html#t:ErrorMsg" title="Lessons.Lesson10">ErrorMsg</a> (State <a href="Lessons-Lesson10.html#t:Input" title="Lessons.Lesson10">Input</a>) a</li><li class="src short"><a href="#v:parseLetter">parseLetter</a> :: <a href="Lessons-Lesson10.html#t:Parser" title="Lessons.Lesson10">Parser</a> Char</li><li class="src short"><a href="#v:parseTwoLetters">parseTwoLetters</a> :: <a href="Lessons-Lesson10.html#t:Parser" title="Lessons.Lesson10">Parser</a> String</li><li class="src short"><a href="#v:parseTwoLetters-39-">parseTwoLetters'</a> :: <a href="Lessons-Lesson10.html#t:Parser" title="Lessons.Lesson10">Parser</a> String</li><li class="src short"><a href="#v:parse">parse</a> :: <a href="Lessons-Lesson10.html#t:Parser" title="Lessons.Lesson10">Parser</a> a -> <a href="Lessons-Lesson10.html#t:Input" title="Lessons.Lesson10">Input</a> -> (Either <a href="Lessons-Lesson10.html#t:ErrorMsg" title="Lessons.Lesson10">ErrorMsg</a> a, <a href="Lessons-Lesson10.html#t:Input" title="Lessons.Lesson10">Input</a>)</li><li class="src short"><span class="keyword">type</span> <a href="#t:Weird">Weird</a> a = ExceptT Int (StateT String IO) a</li><li class="src short"><a href="#v:weird">weird</a> :: <a href="Lessons-Lesson10.html#t:Weird" title="Lessons.Lesson10">Weird</a> Double</li><li class="src short"><a href="#v:weird-39-">weird'</a> :: <a href="Lessons-Lesson10.html#t:Weird" title="Lessons.Lesson10">Weird</a> Double</li><li class="src short"><span class="keyword">data</span> <a href="#t:SomeData">SomeData</a><ul class="subs"><li>= <a href="#v:Foo">Foo</a> String</li><li>| <a href="#v:Bar">Bar</a> Integer</li></ul></li></ul></details></div><div id="interface"><h1>Documentation</h1><div class="top"><p class="src"><span class="keyword">type</span> <a id="t:ErrorMsg" class="def">ErrorMsg</a> = String <a href="src/Lessons.Lesson10.html#ErrorMsg" class="link">Source</a> <a href="#t:ErrorMsg" class="selflink">#</a></p><div class="doc"><p>Basic types for the parser.</p><p><code>Parser a</code> is an <code>ExceptT</code> over <code>State</code>.</p><p><code>State Input</code> carries the remaining input string.</p><p><code>ExceptT ErrorMsg</code> allows failure with an error message.</p></div></div><div class="top"><p class="src"><span class="keyword">type</span> <a id="t:Input" class="def">Input</a> = String <a href="src/Lessons.Lesson10.html#Input" class="link">Source</a> <a href="#t:Input" class="selflink">#</a></p></div><div class="top"><p class="src"><span class="keyword">type</span> <a id="t:Parser" class="def">Parser</a> a = ExceptT <a href="Lessons-Lesson10.html#t:ErrorMsg" title="Lessons.Lesson10">ErrorMsg</a> (State <a href="Lessons-Lesson10.html#t:Input" title="Lessons.Lesson10">Input</a>) a <a href="src/Lessons.Lesson10.html#Parser" class="link">Source</a> <a href="#t:Parser" class="selflink">#</a></p></div><div class="top"><p class="src"><a id="v:parseLetter" class="def">parseLetter</a> :: <a href="Lessons-Lesson10.html#t:Parser" title="Lessons.Lesson10">Parser</a> Char <a href="src/Lessons.Lesson10.html#parseLetter" class="link">Source</a> <a href="#v:parseLetter" class="selflink">#</a></p><div class="doc"><p>Parse a single alphabetic character.</p><p>Consumes one character from the input state or fails with an error message.</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>parse parseLetter "a1"
</code></strong>(Right 'a',"1")
<code class="prompt">>>> </code><strong class="userinput"><code>parse parseLetter "1a"
</code></strong>(Left "A letter is expected, but got 1","1a")
<code class="prompt">>>> </code><strong class="userinput"><code>parse parseLetter ""
</code></strong>(Left "A letter is expected but got empty input","")
</pre></div></div><div class="top"><p class="src"><a id="v:parseTwoLetters" class="def">parseTwoLetters</a> :: <a href="Lessons-Lesson10.html#t:Parser" title="Lessons.Lesson10">Parser</a> String <a href="src/Lessons.Lesson10.html#parseTwoLetters" class="link">Source</a> <a href="#v:parseTwoLetters" class="selflink">#</a></p><div class="doc"><p>Parse two letters using 'do' notation.</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>parse parseTwoLetters "ds"
</code></strong>(Right "ds","")
<code class="prompt">>>> </code><strong class="userinput"><code>parse parseTwoLetters "d5"
</code></strong>(Left "A letter is expected, but got 5","5")
</pre></div></div><div class="top"><p class="src"><a id="v:parseTwoLetters-39-" class="def">parseTwoLetters'</a> :: <a href="Lessons-Lesson10.html#t:Parser" title="Lessons.Lesson10">Parser</a> String <a href="src/Lessons.Lesson10.html#parseTwoLetters%27" class="link">Source</a> <a href="#v:parseTwoLetters-39-" class="selflink">#</a></p><div class="doc"><p>Same as <code><a href="Lessons-Lesson10.html#v:parseTwoLetters" title="Lessons.Lesson10">parseTwoLetters</a></code>, but using applicative style.</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>parse parseTwoLetters' "ab"
</code></strong>(Right "ab","")
</pre></div></div><div class="top"><p class="src"><a id="v:parse" class="def">parse</a> :: <a href="Lessons-Lesson10.html#t:Parser" title="Lessons.Lesson10">Parser</a> a -> <a href="Lessons-Lesson10.html#t:Input" title="Lessons.Lesson10">Input</a> -> (Either <a href="Lessons-Lesson10.html#t:ErrorMsg" title="Lessons.Lesson10">ErrorMsg</a> a, <a href="Lessons-Lesson10.html#t:Input" title="Lessons.Lesson10">Input</a>) <a href="src/Lessons.Lesson10.html#parse" class="link">Source</a> <a href="#v:parse" class="selflink">#</a></p><div class="doc"><p>Running the parser:</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>:t runExceptT parseTwoLetters
</code></strong>runExceptT parseTwoLetters :: State Input (Either ErrorMsg String)
</pre><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>:t runState (runExceptT parseTwoLetters)
</code></strong>runState (runExceptT parseTwoLetters) :: Input -> (Either ErrorMsg String, Input)
</pre><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>parse parseTwoLetters ""
</code></strong>(Left "A letter is expected but got empty input","")
<code class="prompt">>>> </code><strong class="userinput"><code>parse parseTwoLetters "ds"
</code></strong>(Right "ds","")
<code class="prompt">>>> </code><strong class="userinput"><code>parse parseTwoLetters "545435"
</code></strong>(Left "A letter is expected, but got 5","545435")
</pre><p>Helper to run a <code><a href="Lessons-Lesson10.html#t:Parser" title="Lessons.Lesson10">Parser</a></code> on an input string.</p></div></div><div class="top"><p class="src"><span class="keyword">type</span> <a id="t:Weird" class="def">Weird</a> a = ExceptT Int (StateT String IO) a <a href="src/Lessons.Lesson10.html#Weird" class="link">Source</a> <a href="#t:Weird" class="selflink">#</a></p><div class="doc"><p>A more complex monad stack: IO + State + Except.</p><p><code>Weird a</code> fails with an Int, keeps a String state, and can do IO.</p></div></div><div class="top"><p class="src"><a id="v:weird" class="def">weird</a> :: <a href="Lessons-Lesson10.html#t:Weird" title="Lessons.Lesson10">Weird</a> Double <a href="src/Lessons.Lesson10.html#weird" class="link">Source</a> <a href="#v:weird" class="selflink">#</a></p><div class="doc"><p>Version using nested <code>lift</code> calls.
Interacts with the console and stores the last answer in state.</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>:t runStateT (runExceptT weird) "init"
</code></strong>runStateT (runExceptT weird) "init" :: IO (Either Int Double, String)
</pre></div></div><div class="top"><p class="src"><a id="v:weird-39-" class="def">weird'</a> :: <a href="Lessons-Lesson10.html#t:Weird" title="Lessons.Lesson10">Weird</a> Double <a href="src/Lessons.Lesson10.html#weird%27" class="link">Source</a> <a href="#v:weird-39-" class="selflink">#</a></p><div class="doc"><p>Same behavior, but with <code>liftIO</code> for cleaner IO lifting.</p></div></div><div class="top"><p class="src"><span class="keyword">data</span> <a id="t:SomeData" class="def">SomeData</a> <a href="src/Lessons.Lesson10.html#SomeData" class="link">Source</a> <a href="#t:SomeData" class="selflink">#</a></p><div class="doc"><p>A simple sum type used with QuickCheck.</p></div><div class="subs constructors"><p class="caption">Constructors</p><table><tr><td class="src"><a id="v:Foo" class="def">Foo</a> String</td><td class="doc empty"> </td></tr><tr><td class="src"><a id="v:Bar" class="def">Bar</a> Integer</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:SomeData">Instances</h4><details id="i:SomeData" 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:SomeData:Arbitrary:1"></span> Arbitrary <a href="Lessons-Lesson10.html#t:SomeData" title="Lessons.Lesson10">SomeData</a></span> <a href="src/Lessons.Lesson10.html#line-132" class="link">Source</a> <a href="#t:SomeData" class="selflink">#</a></td><td class="doc"><p>Arbitrary instance for SomeData.</p><p>Generates Foo with a random string or Bar with a random integer.</p></td></tr><tr><td colspan="2"><details id="i:id:SomeData:Arbitrary:1"><summary class="hide-when-js-enabled">Instance details</summary><p>Defined in <a href="Lessons-Lesson10.html">Lessons.Lesson10</a></p> <div class="subs methods"><p class="caption">Methods</p><p class="src"><a href="#v:arbitrary">arbitrary</a> :: Gen <a href="Lessons-Lesson10.html#t:SomeData" title="Lessons.Lesson10">SomeData</a></p><p class="src"><a href="#v:shrink">shrink</a> :: <a href="Lessons-Lesson10.html#t:SomeData" title="Lessons.Lesson10">SomeData</a> -> [<a href="Lessons-Lesson10.html#t:SomeData" title="Lessons.Lesson10">SomeData</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:id:SomeData:Show:2"></span> Show <a href="Lessons-Lesson10.html#t:SomeData" title="Lessons.Lesson10">SomeData</a></span> <a href="src/Lessons.Lesson10.html#line-127" class="link">Source</a> <a href="#t:SomeData" class="selflink">#</a></td><td class="doc empty"> </td></tr><tr><td colspan="2"><details id="i:id:SomeData:Show:2"><summary class="hide-when-js-enabled">Instance details</summary><p>Defined in <a href="Lessons-Lesson10.html">Lessons.Lesson10</a></p> <div class="subs methods"><p class="caption">Methods</p><p class="src"><a href="#v:showsPrec">showsPrec</a> :: Int -> <a href="Lessons-Lesson10.html#t:SomeData" title="Lessons.Lesson10">SomeData</a> -> ShowS</p><p class="src"><a href="#v:show">show</a> :: <a href="Lessons-Lesson10.html#t:SomeData" title="Lessons.Lesson10">SomeData</a> -> String</p><p class="src"><a href="#v:showList">showList</a> :: [<a href="Lessons-Lesson10.html#t:SomeData" title="Lessons.Lesson10">SomeData</a>] -> ShowS</p></div></details></td></tr></table></details></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>