Proposed "Restructuring" of State Monad page in Haskell Wikibooks

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

Proposed "Restructuring" of State Monad page in Haskell Wikibooks

David James

Hello – I’m proposing to restructure this, and I have a draft here. I’ve given my main reasons for the restructuring at the top of the draft page.

 

I’d very much like feedback before updating the real page, especially if people don’t like the new one much.

 

I’m new here, so should probably say a bit about myself. I’ve been learning-by-doing Haskell for about two years. Sometimes I feel I’m starting to get it, but these feelings don’t usually last long. I’m certainly not an expert, and don’t have a PhD in Very Clever Things. I do have quite an extensive IT background, originally programming in Lisp and Prolog, then (sadly) C++, VB and SQL but have had quite a long break from programming (doing dumb things like architecting systems and project managing) before looking at Haskell. I built this website and this library, just for my own amusement.

 

Apologies if this isn’t the right place to send this. (But then where is?) I’ve already put a note on the page itself about the new draft, but I’ve no idea whether anyone will notice it.

 

Thanks very much,

David.

 

 

 

 

 

 

 


_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Proposed "Restructuring" of State Monad page in Haskell Wikibooks

Bryan Richter-2
This looks great! Although I admit I didn't read the version you want to replace. :)

I'm always in favor of motivated people making good faith changes to (any) wiki.

On Monday, September 28, 2020, David James <[hidden email]> wrote:

> Hello – I’m proposing to restructure this, and I have a draft here. I’ve given my main reasons for the restructuring at the top of the draft page.
>
>  
>
> I’d very much like feedback before updating the real page, especially if people don’t like the new one much.
>
>  
>
> I’m new here, so should probably say a bit about myself. I’ve been learning-by-doing Haskell for about two years. Sometimes I feel I’m starting to get it, but these feelings don’t usually last long. I’m certainly not an expert, and don’t have a PhD in Very Clever Things. I do have quite an extensive IT background, originally programming in Lisp and Prolog, then (sadly) C++, VB and SQL but have had quite a long break from programming (doing dumb things like architecting systems and project managing) before looking at Haskell. I built this website and this library, just for my own amusement.
>
>  
>
> Apologies if this isn’t the right place to send this. (But then where is?) I’ve already put a note on the page itself about the new draft, but I’ve no idea whether anyone will notice it.
>
>  
>
> Thanks very much,
>
> David.
>
>  
>
>  
>
>  
>
>  
>
>  
>
>  
>
>  
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Proposed "Restructuring" of State Monad page in Haskell Wikibooks

Hilco Wijbenga
In reply to this post by David James
I like it!

P.S. I did notice a small typo: mondayS :: State TurnstileState
[TurstileOutput] (note the missing 'n').

On Mon, Sep 28, 2020 at 9:38 AM David James <[hidden email]> wrote:

>
> Hello – I’m proposing to restructure this, and I have a draft here. I’ve given my main reasons for the restructuring at the top of the draft page.
>
>
>
> I’d very much like feedback before updating the real page, especially if people don’t like the new one much.
>
>
>
> I’m new here, so should probably say a bit about myself. I’ve been learning-by-doing Haskell for about two years. Sometimes I feel I’m starting to get it, but these feelings don’t usually last long. I’m certainly not an expert, and don’t have a PhD in Very Clever Things. I do have quite an extensive IT background, originally programming in Lisp and Prolog, then (sadly) C++, VB and SQL but have had quite a long break from programming (doing dumb things like architecting systems and project managing) before looking at Haskell. I built this website and this library, just for my own amusement.
>
>
>
> Apologies if this isn’t the right place to send this. (But then where is?) I’ve already put a note on the page itself about the new draft, but I’ve no idea whether anyone will notice it.
>
>
>
> Thanks very much,
>
> David.
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Proposed "Restructuring" of State Monad page in Haskell Wikibooks

Branimir Maksimovic
In reply to this post by David James

Two years is not enough to do anything seroius in Haskell. That is beyond demo programs.


Greets, Branimir.


On 9/28/20 6:37 PM, David James wrote:

Hello – I’m proposing to restructure this, and I have a draft here. I’ve given my main reasons for the restructuring at the top of the draft page.

 

I’d very much like feedback before updating the real page, especially if people don’t like the new one much.

 

I’m new here, so should probably say a bit about myself. I’ve been learning-by-doing Haskell for about two years. Sometimes I feel I’m starting to get it, but these feelings don’t usually last long. I’m certainly not an expert, and don’t have a PhD in Very Clever Things. I do have quite an extensive IT background, originally programming in Lisp and Prolog, then (sadly) C++, VB and SQL but have had quite a long break from programming (doing dumb things like architecting systems and project managing) before looking at Haskell. I built this website and this library, just for my own amusement.

 

Apologies if this isn’t the right place to send this. (But then where is?) I’ve already put a note on the page itself about the new draft, but I’ve no idea whether anyone will notice it.

 

Thanks very much,

David.

 

 

 

 

 

 

 


_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Proposed "Restructuring" of State Monad page in Haskell Wikibooks

Gregory Guthrie-2

If this is true, Haskell will probably never move up into even the top 40 of used languages, which would be too bad! And it would be interesting to see why – certainly people find good productivity in the mainstream IP languages in even 6-9 months.

  (Look at salaries from bootcamp placements.)

 

Currently Haskell is significantly below Cobol, Fortran, Lisp, and Pascal in the language usage/ratings.   :-(

 

 

Dr. Gregory Guthrie

Maharishi International University

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

 

From: Haskell-Cafe <[hidden email]> On Behalf Of Branimir Maksimovic
Sent: Tuesday, September 29, 2020 7:17 AM
To: [hidden email]
Subject: Re: [Haskell-cafe] Proposed "Restructuring" of State Monad page in Haskell Wikibooks

 

Two years is not enough to do anything seroius in Haskell. That is beyond demo programs.

 

Greets, Branimir.

 

On 9/28/20 6:37 PM, David James wrote:

Hello – I’m proposing to restructure this, and I have a draft here. I’ve given my main reasons for the restructuring at the top of the draft page.

 

I’d very much like feedback before updating the real page, especially if people don’t like the new one much.

 

I’m new here, so should probably say a bit about myself. I’ve been learning-by-doing Haskell for about two years. Sometimes I feel I’m starting to get it, but these feelings don’t usually last long. I’m certainly not an expert, and don’t have a PhD in Very Clever Things. I do have quite an extensive IT background, originally programming in Lisp and Prolog, then (sadly) C++, VB and SQL but have had quite a long break from programming (doing dumb things like architecting systems and project managing) before looking at Haskell. I built this website and this library, just for my own amusement.

 

Apologies if this isn’t the right place to send this. (But then where is?) I’ve already put a note on the page itself about the new draft, but I’ve no idea whether anyone will notice it.

 

Thanks very much,

David.

 

 

 

 

 

 

 



_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Proposed "Restructuring" of State Monad page in Haskell Wikibooks

Jaro Reinders
I think part of the reason is that most mainstream languages are very similar
(imperative, strict and impure) and Haskell does many things radically
different. I also think that focused study can bring down the time to become
productive significantly. I have personally started learning Haskell by doing
hobby projects when I had the time. Studying and using Haskell full-time would
certainly have sped up my learning progress.

I would also like to point out that language usage statistics are not reliable.
The TIOBE index, based on number of search results by popular search engines,
indeed puts Haskell below the languages that you mention, but for example the
RedMonk rankings put Haskell above those languages, both by the number of Stack
Overflow questions and by the number of GitHub projects. The PYPL PopularitY
ranking, based on the number of Google searches for language tutorials, puts
only Cobol above Haskell (the other languages are not listed).

On 9/29/20 3:10 PM, Gregory Guthrie wrote:

> If this is true, Haskell will probably never move up into even the top 40 of
> used languages, which would be too bad! And it would be interesting to see why
> – certainly people find good productivity in the mainstream IP languages in
> even 6-9 months.
>
>   (Look at salaries from bootcamp placements.)
>
>  
>
> Currently Haskell is significantly below Cobol, Fortran, Lisp, and Pascal in
> the language usage/ratings.   :-(
>
>  
>
>  
>
> Dr. Gregory Guthrie
>
> Maharishi International University
>
> ----------------------------------------------------------------
>
>  
>
> *From:* Haskell-Cafe <[hidden email]> *On Behalf Of *Branimir
> Maksimovic
> *Sent:* Tuesday, September 29, 2020 7:17 AM
> *To:* [hidden email]
> *Subject:* Re: [Haskell-cafe] Proposed "Restructuring" of State Monad page in
> Haskell Wikibooks
>
>  
>
> Two years is not enough to do anything seroius in Haskell. That is beyond demo
> programs.
>
>  
>
> Greets, Branimir.
>
>  
>
> On 9/28/20 6:37 PM, David James wrote:
>
>     Hello – I’m proposing to restructure this
>     <https://en.wikibooks.org/wiki/Haskell/Understanding_monads/State>, and I
>     have a draft here
>     <https://en.wikibooks.org/wiki/Davjam2:Example/StateMonad>. I’ve given my
>     main reasons for the restructuring at the top of the draft page.
>
>      
>
>     I’d very much like feedback before updating the real page, especially if
>     people don’t like the new one much.
>
>      
>
>     I’m new here, so should probably say a bit about myself. I’ve been
>     learning-by-doing Haskell for about two years. Sometimes I feel I’m
>     starting to get it, but these feelings don’t usually last long. I’m
>     certainly not an expert, and don’t have a PhD in Very Clever Things. I do
>     have quite an extensive IT background, originally programming in Lisp and
>     Prolog, then (sadly) C++, VB and SQL but have had quite a long break from
>     programming (doing dumb things like architecting systems and project
>     managing) before looking at Haskell. I built this website
>     <https://decimos.net/> and this library
>     <https://hackage.haskell.org/package/MapWith>, just for my own amusement.
>
>      
>
>     Apologies if this isn’t the right place to send this. (But then where is?)
>     I’ve already put a note on the page itself about the new draft, but I’ve no
>     idea whether anyone will notice it.
>
>      
>
>     Thanks very much,
>
>     David.
>
>      
>
>      
>
>      
>
>      
>
>      
>
>      
>
>      
>
>
>
>     _______________________________________________
>
>     Haskell-Cafe mailing list
>
>     To (un)subscribe, modify options or view archives go to:
>
>     http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
>
>     Only members subscribed via the mailman list are allowed to post.
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.
>
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Proposed "Restructuring" of State Monad page in Haskell Wikibooks

Tom Smeding
In reply to this post by Gregory Guthrie-2
Hi all,

Branimir wrote:
> Two years is not enough to do anything serious in Haskell. That is beyond demo programs.

This statement carries a lot of assumptions, some of which may be true in this case, some of which may not -- I don't know David James -- but I think it isn't good to let this statement stand on its own like that.
If a student who has never programmed before starts learning Haskell, they will not be able to succeed professionally after just two years unless they have other useful background knowledge and/or talent (let's not discuss whether talent is a thing or not). However, the email by David James did not sound like he is a beginner in the act of programming; indeed, with experience in imperative (C++, VB), declarative (SQL), functional (Lisp) and logic (Prolog) programming languages, that certainly does not count as "has never programmed before". And I think we can agree that someone with sufficient experience in programming at large can learn to use Haskell effectively in two years (which is quite a long time, even if it's irregular practice). At least, I strongly believe this until proven otherwise. If that is indeed not the case, then something is seriously wrong on Haskell's side, as Gregory has noted below.

David: I haven't read your rewritten State monad tutorial, for which my apologies; also apologies for kind of derailing this thread.

Cheers, and have a great day,
Tom Smeding

P.S. This message wasn't accepted by the mailing list earlier, so was just sent to some people personally. Sorry for duplicate emails.

On Tue, 29 Sep 2020 at 15:14, Gregory Guthrie <[hidden email]> wrote:

If this is true, Haskell will probably never move up into even the top 40 of used languages, which would be too bad! And it would be interesting to see why – certainly people find good productivity in the mainstream IP languages in even 6-9 months.

  (Look at salaries from bootcamp placements.)

 

Currently Haskell is significantly below Cobol, Fortran, Lisp, and Pascal in the language usage/ratings.   :-(

 

 

Dr. Gregory Guthrie

Maharishi International University

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

 

From: Haskell-Cafe <[hidden email]> On Behalf Of Branimir Maksimovic
Sent: Tuesday, September 29, 2020 7:17 AM
To: [hidden email]
Subject: Re: [Haskell-cafe] Proposed "Restructuring" of State Monad page in Haskell Wikibooks

 

Two years is not enough to do anything seroius in Haskell. That is beyond demo programs.

 

Greets, Branimir.

 

On 9/28/20 6:37 PM, David James wrote:

Hello – I’m proposing to restructure this, and I have a draft here. I’ve given my main reasons for the restructuring at the top of the draft page.

 

I’d very much like feedback before updating the real page, especially if people don’t like the new one much.

 

I’m new here, so should probably say a bit about myself. I’ve been learning-by-doing Haskell for about two years. Sometimes I feel I’m starting to get it, but these feelings don’t usually last long. I’m certainly not an expert, and don’t have a PhD in Very Clever Things. I do have quite an extensive IT background, originally programming in Lisp and Prolog, then (sadly) C++, VB and SQL but have had quite a long break from programming (doing dumb things like architecting systems and project managing) before looking at Haskell. I built this website and this library, just for my own amusement.

 

Apologies if this isn’t the right place to send this. (But then where is?) I’ve already put a note on the page itself about the new draft, but I’ve no idea whether anyone will notice it.

 

Thanks very much,

David.

 

 

 

 

 

 

 



_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Proposed "Restructuring" of State Monad page in Haskell Wikibooks

David James

No problem! My original comment was meant to be somewhat tongue in cheek (as maybe was Branimir’s response?). My website (IMHO) is way beyond trivial, and was probably up and running after a mere 6 months! (I’d never built a website before, or used e.g. GitHub, Docker, Ubuntu, AWS, etc, so they were all a bit of a challenge too).

 

Anyway, enough of this. If anyone does have comments on the page, it would probably be best to add them on the discussion page alongside the article (now here).

 

Unless there are any strong objections, I’ll overwrite the main page with the reworked one in a week or so, and I’ll be doing some final reviewing and tweaking of the page in the meantime. (And thanks to Bryan and Hilco for their comments).

 

Thanks!

David.

 

From: [hidden email]
Sent: 29 September 2020 14:46
To: [hidden email]
Subject: Re: [Haskell-cafe] Proposed "Restructuring" of State Monad page in Haskell Wikibooks

 

Hi all,

 

Branimir wrote:

> Two years is not enough to do anything serious in Haskell. That is beyond demo programs.

 

This statement carries a lot of assumptions, some of which may be true in this case, some of which may not -- I don't know David James -- but I think it isn't good to let this statement stand on its own like that.

If a student who has never programmed before starts learning Haskell, they will not be able to succeed professionally after just two years unless they have other useful background knowledge and/or talent (let's not discuss whether talent is a thing or not). However, the email by David James did not sound like he is a beginner in the act of programming; indeed, with experience in imperative (C++, VB), declarative (SQL), functional (Lisp) and logic (Prolog) programming languages, that certainly does not count as "has never programmed before". And I think we can agree that someone with sufficient experience in programming at large can learn to use Haskell effectively in two years (which is quite a long time, even if it's irregular practice). At least, I strongly believe this until proven otherwise. If that is indeed not the case, then something is seriously wrong on Haskell's side, as Gregory has noted below.

 

David: I haven't read your rewritten State monad tutorial, for which my apologies; also apologies for kind of derailing this thread.

 

Cheers, and have a great day,

Tom Smeding

 

P.S. This message wasn't accepted by the mailing list earlier, so was just sent to some people personally. Sorry for duplicate emails.

 

On Tue, 29 Sep 2020 at 15:14, Gregory Guthrie <[hidden email]> wrote:

If this is true, Haskell will probably never move up into even the top 40 of used languages, which would be too bad! And it would be interesting to see why – certainly people find good productivity in the mainstream IP languages in even 6-9 months.

  (Look at salaries from bootcamp placements.)

 

Currently Haskell is significantly below Cobol, Fortran, Lisp, and Pascal in the language usage/ratings.   :-(

 

 

Dr. Gregory Guthrie

Maharishi International University

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

 

From: Haskell-Cafe <[hidden email]> On Behalf Of Branimir Maksimovic
Sent: Tuesday, September 29, 2020 7:17 AM
To: [hidden email]
Subject: Re: [Haskell-cafe] Proposed "Restructuring" of State Monad page in Haskell Wikibooks

 

Two years is not enough to do anything seroius in Haskell. That is beyond demo programs.

 

Greets, Branimir.

 

On 9/28/20 6:37 PM, David James wrote:

Hello – I’m proposing to restructure this, and I have a draft here. I’ve given my main reasons for the restructuring at the top of the draft page.

 

I’d very much like feedback before updating the real page, especially if people don’t like the new one much.

 

I’m new here, so should probably say a bit about myself. I’ve been learning-by-doing Haskell for about two years. Sometimes I feel I’m starting to get it, but these feelings don’t usually last long. I’m certainly not an expert, and don’t have a PhD in Very Clever Things. I do have quite an extensive IT background, originally programming in Lisp and Prolog, then (sadly) C++, VB and SQL but have had quite a long break from programming (doing dumb things like architecting systems and project managing) before looking at Haskell. I built this website and this library, just for my own amusement.

 

Apologies if this isn’t the right place to send this. (But then where is?) I’ve already put a note on the page itself about the new draft, but I’ve no idea whether anyone will notice it.

 

Thanks very much,

David.

 

 

 

 

 

 

 

 

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.

 


_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Proposed "Restructuring" of State Monad page in Haskell Wikibooks

Olaf Klinke
In reply to this post by David James
> Hello – I’m proposing to restructure this<
> https://en.wikibooks.org/wiki/Haskell/Understanding_monads/State>;,
> and I have a draft here<
> https://en.wikibooks.org/wiki/Davjam2:Example/StateMonad>;. I’ve
> given my main reasons for the restructuring at the top of the draft
> page.
>
> I’d very much like feedback before updating the real page, especially
> if people don’t like the new one much.

+1 for the new version by David.

In addition to the content already present, I'd like to see examples
(and exercises! these are great!) of State used with other common
monadic combinators. You covered sequence, but what about mapM a.k.a.
traverse, what about foldM? Can filterM be put to good use with state
monads?
You could introduce another type
TurnstileInput = Push | Coin
and combine pushS, coinS into a single function of type
TurnstileInput -> State TurnstileState TurnstileOutput
which can be used in mapM. Your exercises of different people can then
be implemented as mapM of a single function (namely the FSM) over
finite sequences of TurnstileInputs.

As a side-note, Data.Traversable.mapAccumR is implemented using a
StateR that passes the state backwards. Only an Applicative instance is
given in Data.Functor.Utils. I wonder whether this is a lawful monad or
not. The comment says so, but I don't see how.

Here is a departure from the otherwise very concrete and down-to-earth
explanation of State, but a fun exercise nevertheless: Implement return
and (>>=) for
type State s a = s -> (s,a)
using the following two functions and the Functor instance of ((->) s).

iso :: ((s,a) -> b) -> a -> s -> b
iso uncurried = curry (uncurried . Data.Tuple.swap)
iso' :: (a -> s -> b) -> (s,a) -> b -- inverse of iso
iso' curried = uncurry (flip curried)

Olaf

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Proposed "Restructuring" of State Monad page in Haskell Wikibooks

David James

Thanks for your comments and suggestions.

 

Re mapM, etc: I’ve added a new section.

 

Re Data.Traversable.mapAccumR: I think you’re referring to the comment “right-to-left state-transforming monad”, despite it not being an instance of Monad? It was originally commented as “right-to-left state transformer”, but changed (per issue 15189), so as not to cause confusion with StateT the (State monad transformer), and I think the word monad snuck in somewhat accidentally. Maybe it should be “right-to-left state processor”?

 

I’m afraid I got stuck on your fun exercise    This:

 

{-# LANGUAGE FlexibleInstances #-}

type State s a = s -> (s, a)

instance Monad (State s) where

 

gives “The type synonym ‘State’ should have 2 arguments, but has been given 1”. Maybe I misunderstood something?

 

Thanks, David.

 

From: [hidden email]
Sent: 29 September 2020 22:17
To: [hidden email]
Cc: [hidden email]
Subject: Re: Proposed "Restructuring" of State Monad page in Haskell Wikibooks

 

> Hello – I’m proposing to restructure this<
> https://en.wikibooks.org/wiki/Haskell/Understanding_monads/State>;,
> and I have a draft here<
> https://en.wikibooks.org/wiki/Davjam2:Example/StateMonad>;. I’ve
> given my main reasons for the restructuring at the top of the draft
> page.
>
> I’d very much like feedback before updating the real page, especially
> if people don’t like the new one much.

+1 for the new version by David.

In addition to the content already present, I'd like to see examples
(and exercises! these are great!) of State used with other common
monadic combinators. You covered sequence, but what about mapM a.k.a.
traverse, what about foldM? Can filterM be put to good use with state
monads?
You could introduce another type
TurnstileInput = Push | Coin
and combine pushS, coinS into a single function of type
TurnstileInput -> State TurnstileState TurnstileOutput
which can be used in mapM. Your exercises of different people can then
be implemented as mapM of a single function (namely the FSM) over
finite sequences of TurnstileInputs.

As a side-note, Data.Traversable.mapAccumR is implemented using a
StateR that passes the state backwards. Only an Applicative instance is
given in Data.Functor.Utils. I wonder whether this is a lawful monad or
not. The comment says so, but I don't see how.

Here is a departure from the otherwise very concrete and down-to-earth
explanation of State, but a fun exercise nevertheless: Implement return
and (>>=) for
type State s a = s -> (s,a)
using the following two functions and the Functor instance of ((->) s).

iso :: ((s,a) -> b) -> a -> s -> b
iso uncurried = curry (uncurried . Data.Tuple.swap)
iso' :: (a -> s -> b) -> (s,a) -> b -- inverse of iso
iso' curried = uncurry (flip curried)

Olaf

 


_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Proposed "Restructuring" of State Monad page in Haskell Wikibooks

Olaf Klinke
On Mon, 2020-10-05 at 10:21 +0000, David James wrote:

> Thanks for your comments and suggestions.
>
> I’m afraid I got stuck on your fun exercise  ☹  This:
>
> {-# LANGUAGE FlexibleInstances #-}
> type State s a = s -> (s, a)
> instance Monad (State s) where
>
> gives “The type synonym ‘State’ should have 2 arguments, but has been
> given 1”. Maybe I misunderstood something?
>
> Thanks, David.
>
You need the TypeSynonymInstances extension for that. My wording of the
excercise was incomplete. It should read:
For type State s a = s -> (s, a),
implement functions
returnState :: a -> State s a
bindState   :: State s a -> (a -> State s b) -> State s b
using only iso, iso' below and the Functor instance of ((->) s).
(This way, you don't need TypeSynonymInstances.)

Olaf

> Here is a departure from the otherwise very concrete and down-to-
> earth
> explanation of State, but a fun exercise nevertheless: Implement
> return
> and (>>=) for
> type State s a = s -> (s,a)
> using the following two functions and the Functor instance of ((->)
> s).
>
> iso :: ((s,a) -> b) -> a -> s -> b
> iso uncurried = curry (uncurried . Data.Tuple.swap)
> iso' :: (a -> s -> b) -> (s,a) -> b -- inverse of iso
> iso' curried = uncurry (flip curried)
>
> Olaf
>

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.