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

Site Administrator admin at dominionbeef.us
Sun Oct 27 18:58:44 UTC 2019


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.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20191027/317ccec5/attachment.html>


More information about the Beginners mailing list