Send Beginners mailing list submissions to
        beginners@haskell.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  Putrady 2018 Ch. 3 - STM (Site Administrator)
   2. Re:  Putrady 2018 Ch. 3 - STM (Sylvain Henry)


----------------------------------------------------------------------

Message: 1
Date: Sun, 27 Oct 2019 12:58:44 -0600
From: Site Administrator <ad...@dominionbeef.us>
To: beginners@haskell.org
Subject: [Haskell-beginners] Putrady 2018 Ch. 3 - STM
Message-ID: <bd5bfbce-3e6c-0dc5-0009-06149ef10...@dominionbeef.us>
Content-Type: text/plain; charset="utf-8"; Format="flowed"

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 "e...@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-0001.html>

------------------------------

Message: 2
Date: Mon, 28 Oct 2019 09:46:10 +0100
From: Sylvain Henry <sylv...@haskus.fr>
To: beginners@haskell.org
Subject: Re: [Haskell-beginners] Putrady 2018 Ch. 3 - STM
Message-ID: <f659c3dc-d305-5bc5-c031-9a41a081d...@haskus.fr>
Content-Type: text/plain; charset="utf-8"; Format="flowed"

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 "e...@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@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-0001.html>

------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 136, Issue 4
*****************************************

Reply via email to