Some time ago I have read A State Monad Tutorial
(http://strabismicgobbledygook.wordpress.com/2010/03/06/a-state-monad-tutorial/).
While reading, I was fixing some minor mistakes (okay, a lot of
mistakes). After all, I had an idea to create PDF with fixed version.
So, here it is:
jean-christophe mincke wrote:
I do not master all the subtilities of lazy evaluation yet and perhaps tail
recursivity does not have the same importance (or does not offer the same
guarantees) in a lazy language as it does in a strict language.
Yep, that's the case. With lazy evaluation, tail
On Mon, Jul 06, 2009 at 12:54:54PM -0400, Thomas Hartman wrote:
Can someone give some simple common scenarios where the state monad is
useful, besides labeling trees?
Implementing the Union-Find data structure[1] for unification based type
inference. As far as I know, no good alternative exists
Can someone give some simple common scenarios where the state monad is
useful, besides labeling trees?
References to puzzles like those in project Euler or similar would be nice.
Thanks!
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
I used the State monad to implement a Brainfuck [1] interpreter a few
months ago. It stored the program counter, pointer and the memory of
the machine.
There might have been a different (better?) way, but as I was trying
to learn more about monads, it was an obvious choice.
Thomas
[1]
Can someone give some simple common scenarios where the state monad is
useful, besides labeling trees?
Emulating the VM given in this years ICFP programming contest was also
a good application of the state monad. Of course you interprate much
simpler language imperative languages, too.
Off the top of my head state is important when getting from A to B
depends on the path you took. As such a common scenario I find
myself in all the time is not having a good CLI craps game. (And
which I resolve by rewriting in every language I learn.) Stake,
current bet, bets outstanding,
It sounds like you've got the monadic solution figured out, so that's
good. Even though I would recommend using State (or StateT if
necessary), I wanted to be sure this question was answered.
ranq1List :: (Word64 - a ) - Word64 - [a]
ranq1List converter state = converter newState : ranq1List
On Sat, May 24, 2008 at 3:39 AM, Bulat Ziganshin [EMAIL PROTECTED]
wrote:
Hello Olivier,
Saturday, May 24, 2008, 5:37:32 AM, you wrote:
(|) = flip (.)
I even started to use it in my code and then stopped. It may be a
stupid concern but as many optimizations performed by GHC are made
On Fri, May 23, 2008 at 2:20 PM, Thomas Hartman [EMAIL PROTECTED] wrote:
The big benefit I got from using the State Monad was that I was able to
reorder the functions by just copy/pasting the function name from one
place to another.
I don't understand... why do you need state to do this?
On Wed, May 21, 2008 at 6:19 PM, Dmitri O.Kondratiev [EMAIL PROTECTED]
wrote:
-- Then we can use this State object (returned by getAny) in a function
generating random values such as:
makeRnd :: StdGen - (Int, StdGen)
makeRnd = runState (do
y - getAny
So, are there any other simple motivating examples that show what
state is really good for?
Here's an example from some code that I'm (trying to) write; I am
writing a DSL for the Povray Scene Description Language. This part of
my program creates a `String' which holds a piece of Povray SDL
2008/5/22 Olivier Boudry [EMAIL PROTECTED]:
On Wed, May 21, 2008 at 6:19 PM, Dmitri O.Kondratiev [EMAIL PROTECTED]
wrote:
-- Then we can use this State object (returned by getAny) in a function
generating random values such as:
makeRnd :: StdGen - (Int, StdGen)
makeRnd = runState (do
I would be interested in seeing good motivating examples for use of
the state monad, other than that example from All About Monads.
Okay, it's good for randomness. What else?
Reading the source code for State, I think I saw an example about
using state to uniquely label elements of a tree with
Thomas Hartman wrote:
I would be interested in seeing good motivating examples for use of
the state monad...
Okay, it's good for randomness. What else?
...I saw an example about
using state to uniquely label elements of a tree
So, are there any other simple motivating examples that show what
Thanks everybody for your help!
Oliver, you provided an excellent write-up on State monad without
going into 'scary' :) details, great work indeed!
Alas, in this case I need the details, and in particular the most scary
ones!
So let's start with fundamental and most intriguing (to me)
On Wed, May 21, 2008 at 8:42 AM, Dmitri O.Kondratiev [EMAIL PROTECTED]
wrote:
So let's start with fundamental and most intriguing (to me) things:
getAny :: (Random a) = State StdGen a
getAny = do g - get -- magically get the current StdGen
First line above declares a data type:
State
Dmitri O.Kondratiev wrote:
Thanks everybody for your help!
Oliver, you provided an excellent write-up on State monad without
going into 'scary' :) details, great work indeed!
Alas, in this case I need the details, and in particular the most scary
ones!
So let's start with fundamental
State is a data type. As any other data type it can be instantiated. State
instance is a structure of one record that contains (\s -(a,s)) lambda
function. This function can be parametrized by types of its arguments 's'
and 'a'. I don't see magic here :)
Ok, then from declaration:
getAny ::
Jules,
Stupid question, please bear with me:
x :: Int -- x declared, but not constructed
x = 1 -- x constructed
s1 :: State StdGen a -- s1 declared, yes, but why s1 is *also already
constructed* ?
On Wed, May 21, 2008 at 6:54 PM, Jules Bean [EMAIL PROTECTED] wrote:
Dmitri O.Kondratiev wrote:
Dmitri O.Kondratiev wrote:
Jules,
Stupid question, please bear with me:
x :: Int -- x declared, but not constructed
x = 1 -- x constructed
s1 :: State StdGen a -- s1 declared, yes, but why s1 is *also already
constructed* ?
it's not.
it's constructed when you do
s1 = return 1
... or ...
-- Jules, Oliver, thanks! Things are getting clarified, I hope.
-- Let me summarize how I now understand getAny operation, please correct me
if I am wrong.
getAny :: (Random a) = State StdGen a
getAny = do g - get
(x,g') - return $ random g
put g'
return x
{--
2008/5/19 Dmitri O.Kondratiev [EMAIL PROTECTED]:
I am trying to understand State monad example15 at:
http://www.haskell.org/all_about_monads/html/statemonad.html
Hi Dmitri,
I'm not sure you need to understand everything about Monad and do-notation
to use the State Monad. So I will try to
Dmitri,
Excellent questions. There's one step you're missing. Most of your
questions revolve around 'foo - bar' constructs within a monad. I
would suggest that you review the de-sugaring rules at
http://en.wikibooks.org/wiki/Haskell/Syntactic_sugar#Do_and_proc_notation
and see if that helps you
Hi Dmitri. I'm just going to ramble on about what I know and how I
think of things, and maybe you'll pick something up :-)
On 5/19/08, Dmitri O.Kondratiev [EMAIL PROTECTED] wrote:
getAny :: (Random a) = State StdGen a
getAny = do g - get
(x,g') - return $ random g
When I load the State module in Hugs, then I can define the function
f below, but I do not immediately see exactly what function return
returns. Explanation welcome.
For example:
f [2..4] [6..9]
[6,7,8,9,6,7,8,9,6,7,8,9]
That is, it just repeats the second argument as many times as the
It has nothing to do with State; it actually works in List monad.
return y is just another way of writing [y].
You don't need to import Control.Monad.State for this to work; you
only need Control.Monad (which is imported by the former).
On 16 Apr 2008, at 16:56, Hans Aberg wrote:
When I
Before somebody noticed: I'm wrong.
It's not List monad, but also a (-) x monad, also defined in
Control.Monad.
Therefore, return y is just const y. Therefore,
x = (return y) = x = (const y) = x y
On 16 Apr 2008, at 17:04, Miguel Mitrofanov wrote:
It has nothing to do with State; it
Miguel Mitrofanov wrote:
It has nothing to do with State; it actually works in List monad.
return y is just another way of writing [y].
Actually, it seems that in this case return is from the ((-) a) monad,
i.e. return=const.
f x y = x = return y
= x = const y
= (concat . map)
Am Mittwoch, 16. April 2008 14:56 schrieb Hans Aberg:
When I load the State module in Hugs, then I can define the function
f below, but I do not immediately see exactly what function return
returns. Explanation welcome.
For example:
f [2..4] [6..9]
[6,7,8,9,6,7,8,9,6,7,8,9]
That
On 16 Apr 2008, at 15:22, Daniel Fischer wrote:
The point is the
instance Monad ((-) a) where
return x = const x
f = g = \x - g (f x) x
which is defined in Control.Monad.Instances...
Thank you. I suspected there was an instance somewhere, and I wanted
to know where it is defined.
On 16 Apr 2008, at 15:14, Miguel Mitrofanov wrote:
Before somebody noticed: I'm wrong.
It's not List monad, but also a (-) x monad, also defined in
Control.Monad.
Therefore, return y is just const y. Therefore,
x = (return y) = x = (const y) = x y
Right. It is an interesting monad, but
Unfortunately, the current situation is that State is only
available as a lazy monad, and StateT is only available
as a strict monad.
I agree with you that both lazy and strict monads are important and
that we should have both options in a monad library.
But the fun doesn't end there.
I don't think they are in the standard libraries but there was some discussion
about them a few months ago but I couldn't find a reference.
Peter, Can you supply one? I think you were a participant in the discussion.
Did you put a library of this sort of thing together?
Here's my tuppenceworth
This thing is giving me fits. I need a global variable of sorts for an
implementation of Syslog. (Yes, I really do.)
It seems the right way to do that is the state monad. But I can't
figure out how to do two simple things:
* Read what is in there without modifying it
* Modify it
--- John Goerzen wrote:
tick :: Int - State Int Int
tick newval = do put newval
return newval
Or this:
tick :: State Int Int
tick = do n - get
return n
That is even more incomprehensible to me -- why would removing a line
before the return cause a type error?
--- end
36 matches
Mail list logo