<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p>Hi,<br>
</p>
<p>The issue has nothing to do with STM.<br>
</p>
<p>> <font face="Liberation Serif">that would help me understand
why the addAuth function thinks the TVar State we pass to it
should be a D.Auth</font></p>
<p>Let's look at the type of `addAuth`:<font face="Liberation Serif"><font
face="Liberation Serif"><br>
</font></font></p>
<pre><font face="Liberation Serif"><font face="Liberation Serif">addAuth :: InMemory r m => D.Auth -> m (Either D.RegistrationError D.VerificationCode)</font></font></pre>
<p>It takes a single parameter with type `D.Auth`. So you don't have
to pass `s` when you call it.<br>
</p>
<p>The `TVar State` value is passed implicitly thanks to the
constraint `InMemory r m` (i.e. a value with type `r` can be
obtained from `m` Monad thanks to `MonadReader r m` and this value
contains a `TVar State` thanks to the `Has (TVar State) r`
constraint`).</p>
<p>You can't call `addAuth` directly in GHCI (where `m` would be
`IO`), you have to follow the example here:
<a class="moz-txt-link-freetext" href="https://github.com/Apress/practical-webdev-haskell/blob/master/03/src/Lib.hs#L30">https://github.com/Apress/practical-webdev-haskell/blob/master/03/src/Lib.hs#L30</a></p>
<p>Or alternatively you can modify `addAuth` for you GHCI test:<br>
<br>
<font face="Liberation Serif">addAuth :: TVar State -> D.Auth
-> m (Etiher ...)<br>
addAuth tvar auth = do<br>
-- ... rest of function<br>
</font></p>
<p><br>
Hope this helps,<br>
Sylvain<br>
</p>
<p><br>
</p>
<div class="moz-cite-prefix">On 27/10/2019 19:58, Site Administrator
wrote:<br>
</div>
<blockquote type="cite"
cite="mid:bd5bfbce-3e6c-0dc5-0009-06149ef1003e@dominionbeef.us">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<font face="Liberation Serif">Hello,<br>
<br>
I am new not only to Haskell, but to programming in general. I
hope that I am posting in the correct forum.<br>
<br>
I am working through "Practical Web Development With Haskell" by
Ecky Putrady (2018), New York: Apress, and I am having some
difficulty understanding the STM portion of Chapter three. More
specifically, I cannot get the following code snippet to return
the expected result in the REPL:<br>
</font>
<blockquote><font face="Liberation Serif">-- snippet from
Adapter.InMemory.Auth module as composed on pages 53-64 in the
book<br>
import ClassyPrelude<br>
import qualified Domain.Auth as D -- as composed on pages
35-52 in the book<br>
import Conrol.Monad.Except<br>
import Text.StringRandom<br>
import Data.Has<br>
-- ...<br>
data State = State<br>
{ stateAuths :: [(D.UserId, D.Auth)]<br>
-- ...<br>
initialState :: State<br>
initialState = State<br>
{ stateAuths = []<br>
-- ...<br>
type InMemory r m = (Has (TVar State) r, MonadReader r m,
MonadIO m)<br>
<br>
addAuth :: InMemory r m<br>
=> D.Auth -> m (Either
D.RegistrationError D.VerificationCode)<br>
addAuth auth = do<br>
tvar <- asks getter<br>
-- ... rest of function<br>
</font></blockquote>
<font face="Liberation Serif">These are the instructions for
"Verification in REPL" provided on page 64 of the book:<br>
</font>
<blockquote><font face="Liberation Serif">> :l
Adapter.InMemory.Auth<br>
> let email = D.mkEmail <a class="moz-txt-link-rfc2396E"
href="mailto:ecky@test.com" moz-do-not-send="true">"ecky@test.com"</a><br>
> let passw = D.mkPassword "1234ABCDefgh"<br>
> let auth = either undefined id $ D.Auth <$> email
<*> passw<br>
> s <- newTVarIO initialState<br>
> addAuth s auth<br>
</font></blockquote>
<font face="Liberation Serif">Calling the addAuth function is
supposed to print the following:<br>
</font>
<blockquote><font face="Liberation Serif">Right "aBNhtG653Bga9kas"
-- (or whatever random vCode stringRandomIO generates)<br>
</font></blockquote>
<font face="Liberation Serif">However, GHCI instead tells me:<br>
</font>
<blockquote><font face="Liberation Serif"><interactive>:6.9:
error:<br>
* Couldn't match expected type 'D.Auth'<br>
with actual type 'TVar State'<br>
* In the first argument of 'addAuth', namely 's'<br>
In the expression: addAuth s auth<br>
-- ...<br>
</font></blockquote>
<font face="Liberation Serif">The source code for the book at <a
class="moz-txt-link-freetext"
href="https://github.com/Apress/practical-webdev-haskell"
moz-do-not-send="true">https://github.com/Apress/practical-webdev-haskell</a>
was created with stack resolver lts-9.11, while I am using
resolver lts-14.11. I am doing this intentionally, because
debugging the compilation errors helps guide me to become more
familiar with the documentation in the packages being
implemented, as well as helping me to understand general Haskell
concepts little by little. I have tried looking through the
changelogs for the stm and classy-prelude packages on <a
class="moz-txt-link-freetext"
href="https://hackage.haskell.org" moz-do-not-send="true">https://hackage.haskell.org</a>,
for any clues which would be intelligible to me (an extremely
restrictive constraint, admittedly), that would help me
understand why the addAuth function thinks the TVar State we
pass to it should be a D.Auth, and why this presumably would not
have been the case had I built the project with the lts-9.11
resolver, but I have not been able to understand it thus far. <br>
<br>
<br>
</font> <br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<pre class="moz-quote-pre" wrap="">_______________________________________________
Beginners mailing list
<a class="moz-txt-link-abbreviated" href="mailto:Beginners@haskell.org">Beginners@haskell.org</a>
<a class="moz-txt-link-freetext" href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a>
</pre>
</blockquote>
</body>
</html>