[Haskell-beginners] Putrady 2018 Ch. 3 - STM

Sylvain Henry sylvain at haskus.fr
Mon Oct 28 08:46:10 UTC 2019


Hi,

The issue has nothing to do with STM.

 > that would help me understand why the addAuth function thinks the 
TVar State we pass to it should be a D.Auth

Let's look at the type of `addAuth`:

addAuth :: InMemory r m => D.Auth -> m (Either D.RegistrationError 
D.VerificationCode)

It takes a single parameter with type `D.Auth`. So you don't have to 
pass `s` when you call it.

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`).

You can't call `addAuth` directly in GHCI (where `m` would be `IO`), you 
have to follow the example here: 
https://github.com/Apress/practical-webdev-haskell/blob/master/03/src/Lib.hs#L30

Or alternatively you can modify `addAuth` for you GHCI test:

addAuth :: TVar State -> D.Auth -> m (Etiher ...)
addAuth tvar auth = do
   -- ... rest of function


Hope this helps,
Sylvain


On 27/10/2019 19:58, Site Administrator wrote:
> Hello,
>
> I am new not only to Haskell, but to programming in general. I hope 
> that I am posting in the correct forum.
>
> 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:
>
>     -- snippet from Adapter.InMemory.Auth module as composed on pages
>     53-64 in the book
>     import ClassyPrelude
>     import qualified Domain.Auth as D -- as composed on pages 35-52 in
>     the book
>     import Conrol.Monad.Except
>     import Text.StringRandom
>     import Data.Has
>     -- ...
>     data State = State
>       { stateAuths :: [(D.UserId, D.Auth)]
>     -- ...
>     initialState :: State
>     initialState = State
>       { stateAuths = []
>     -- ...
>     type InMemory r m = (Has (TVar State) r, MonadReader r m, MonadIO m)
>
>     addAuth :: InMemory r m
>                    => D.Auth -> m (Either D.RegistrationError
>     D.VerificationCode)
>     addAuth auth = do
>       tvar <- asks getter
>     -- ... rest of function
>
> These are the instructions for "Verification in REPL" provided on page 
> 64 of the book:
>
>     > :l Adapter.InMemory.Auth
>     > let email = D.mkEmail "ecky at test.com"
>     > let passw = D.mkPassword "1234ABCDefgh"
>     > let auth = either undefined id $ D.Auth <$> email <*> passw
>     > s <- newTVarIO initialState
>     > addAuth s auth
>
> Calling the addAuth function is supposed to print the following:
>
>     Right "aBNhtG653Bga9kas" -- (or whatever random vCode
>     stringRandomIO generates)
>
> However, GHCI instead tells me:
>
>     <interactive>:6.9: error:
>            * Couldn't match expected type 'D.Auth'
>                                    with actual type 'TVar State'
>            * In the first argument of 'addAuth', namely 's'
>               In the expression: addAuth s auth
>     -- ...
>
> The source code for the book at 
> https://github.com/Apress/practical-webdev-haskell 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 
> https://hackage.haskell.org, 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.
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20191028/5eeb228d/attachment.html>


More information about the Beginners mailing list