
From: Control.Monad.Reader type Reader r = ReaderT r IdentityThe parameterizable reader monad. Computations are functions of a shared environment. The return function ignores the environment, while >>= passes the inherited environment to both subcomputations ============ Is there an unparameterizable reader monad? Michael

On Wed, Dec 29, 2010 at 8:06 AM, michael rice
Is there an unparameterizable reader monad?
I'm not sure this is the answer you are looking for, but it seems like the obvious one. Pick an "r", say "String". Now "Reader String" is an unparameterizable reader monad that passes around a String. -- ryan

Hi, Ryan. Since I'm trying to understand Reader, I wanted to be aware of all cases of Reader. ==========
From the docs (and tuts) newtype creates a new type out of an existing type and gives a single constructor for doing so.
From: http://www.haskell.org/tutorial/moretypes.html
newtype Natural = MakeNatural Integer
This creates an entirely new type, Natural, whose only constructor contains
a single Integer. The constructor MakeNatural converts between an Natural and
an Integer:
toNatural :: Integer -> Natural
toNatural x | x < 0 = error "Can't create negative naturals!"
| otherwise = MakeNatural x
fromNatural :: Natural -> Integer
fromNatural (MakeNatural i) = i
In the above case the existing type is Integer. The new type behaves like the existing type, but we can pattern match with the new type.
++++++++++
In the case of ReaderT and StateT
newtype ReaderT r m a = ReaderT {
-- | The underlying computation, as a function of the environment.
runReaderT :: r -> m a
}
newtype StateT s m a = StateT { runStateT :: s -> m (a, s) }
what is the existing type?
Michael
--- On Wed, 12/29/10, Ryan Ingram
Is there an unparameterizable reader monad?
I'm not sure this is the answer you are looking for, but it seems like the obvious one. Pick an "r", say "String". Now "Reader String" is an unparameterizable reader monad that passes around a String. -- ryan

On Wed, 29 Dec 2010, michael rice wrote:
In the case of ReaderT and StateT
newtype ReaderT r m a = ReaderT { -- | The underlying computation, as a function of the environment. runReaderT :: r -> m a }
newtype StateT s m a = StateT { runStateT :: s -> m (a, s) }
what is the existing type?
The existing type is 'r -> m a'. You could also write
newtype ReaderT r m a = ReaderT (r -> m a)
This would be the same type as above, but it would have no accessor function 'runReaderT'.

I think of (r -> m a) as a type signature and Int or Bool by themselves as types. So, all type signatures are themselves types?
Michael
--- On Wed, 12/29/10, Henning Thielemann
In the case of ReaderT and StateT
newtype ReaderT r m a = ReaderT { -- | The underlying computation, as a function of the environment. runReaderT :: r -> m a }
newtype StateT s m a = StateT { runStateT :: s -> m (a, s) }
what is the existing type?
The existing type is 'r -> m a'. You could also write
newtype ReaderT r m a = ReaderT (r -> m a)
This would be the same type as above, but it would have no accessor function 'runReaderT'.

On 10-12-29 12:50 PM, michael rice wrote:
I think of (r -> m a) as a type signature and Int or Bool by themselves as types. So, all type signatures are themselves types?
http://www.haskell.org/onlinereport/haskell2010/haskellch4.html#x10-620004 In particular gendecl → vars :: [context =>] type (type signature) Therefore I think of n :: Int f :: r -> m a as type signatures, and their right-hand sides alone, Int r -> m a as types. I also include the "context =>" part in my types, for example m :: Num a => a I take the type to be Num a => a The grammar splits out the "context =>" part just for the sake of reuse in other places such as topdecl → ... | data [context =>] simpletype [= constrs] [deriving]

2010/12/29 Albert Y. C. Lai
On 10-12-29 12:50 PM, michael rice wrote:
I think of (r -> m a) as a type signature and Int or Bool by themselves as types. So, all type signatures are themselves types?
http://www.haskell.org/onlinereport/haskell2010/haskellch4.html#x10-620004
In particular
gendecl → vars :: [context =>] type (type signature)
Therefore I think of
n :: Int f :: r -> m a
as type signatures, and their right-hand sides alone,
Int r -> m a
as types.
I used to think about types as mathematical sets, and type signatures in this case are just sequences of characters denoting these types. They of course must have some formal grammar as it was quoted above. I'd like to note that one type can be denoted by many type signatures. For example, String and [Char]. And as another case, a -> b and c -> d. One may come up with more advanced examples involving type families.

2010/12/29 michael rice
From the docs (and tuts) newtype creates a new type out of an existing
type
and gives a single constructor for doing so.
what is the existing type?
Michael, you may want to see this section: http://learnyouahaskell.com/functors-applicative-functors-and-monoids#the-ne... and also section titled "type vs. newtype vs. data" below. It has good explanation why there is at least three keywords in Haskell to define types, and which of them do what. And how they do it.
From my experience, this book in a whole could be useful to you. It also has explanation how Reader is constructed step-by-step.
As far as I know, there has been code for Reader monad in Haskell Wiki. Unfortunately, after it has been migrated, "page not found" error has become not very uncommon on it, and either i was not able to found it, or it really is missing. Out of practical considerations, library authors decided to define more complicated ReaderT monad transformer, because it can be made to behave as Reader if the former wraps Identity monad. Hope this helps.

Hi, Michael.
Yes, I'd already noticed that ReaderT preceded Reader. Guess I'll have to check out the Indentity monad too. I hope it's not dependent upon yet another monad. ;-)
Thanks for the link. I go there a lot.
Michael
--- On Wed, 12/29/10, Michael Lazarev
From the docs (and tuts) newtype creates a new type out of an existing type and gives a single constructor for doing so.
what is the existing type?
Michael, you may want to see this section: http://learnyouahaskell.com/functors-applicative-functors-and-monoids#the-ne... and also section titled "type vs. newtype vs. data" below. It has good explanation why there is at least three keywords in Haskell to define types, and which of them do what. And how they do it.
From my experience, this book in a whole could be useful to you. It also has explanation how Reader
is constructed step-by-step. As far as I know, there has been code for Reader monad in Haskell Wiki. Unfortunately, after it has been migrated, "page not found" error has become not very uncommon on it, and either i was not able to found it, or it really is missing. Out of practical considerations, library authors decided to define more complicated ReaderT monad transformer, because it can be made to behave as Reader if the former wraps Identity monad. Hope this helps.

On Wednesday 29 December 2010 19:30:11, michael rice wrote:
Yes, I'd already noticed that ReaderT preceded Reader. Guess I'll have to check out the Indentity monad too. I hope it's not dependent upon yet another monad.
No, the Identity monad stands alone. And as the name suggests, it's pretty simple.

Hi, Daniel.
I had an "Aha!" moment and it all makes sense now. Just as the State monad can hold a generator (which can change) and pass it down a calculation chain, a Reader monad can hold an environment (which doesn't change) and pass it down a calculation chain. I was wondering how I could include a (global) house betting limit in that craps application I've been playing with (without passing it as a parameter) and it sounds like the Reader monad would be an ideal candidate. Correct? It also sounds like a job for monad transforms.
Michael
--- On Wed, 12/29/10, Daniel Fischer
Yes, I'd already noticed that ReaderT preceded Reader. Guess I'll have to check out the Indentity monad too. I hope it's not dependent upon yet another monad.
No, the Identity monad stands alone. And as the name suggests, it's pretty simple.

2010/12/29 michael rice
I had an "Aha!" moment and it all makes sense now. Just as the State monad can hold a generator (which can change) and pass it down a calculation chain, a Reader monad can hold an environment (which doesn't change) and pass it down a calculation chain. I was wondering how I could include a (global) house betting limit in that craps application I've been playing with (without passing it as a parameter) and it sounds like the Reader monad would be an ideal candidate. Correct? It also sounds like a job for monad transforms.
That is right. You need transformers if you want to have one value as settings to read, other value as a state, and so on, and they must be simultaneously accessible in some function. Or, for example, if you want to build a sequence of IO actions by functions that share the same environment. After you said that you had an "Aha!" moment, I remembered how I had something alike not very long ago. That was surprising event when I ended up with some transformer stack written myself although just several minutes ago I would consider this to be some dark wizardry. When I was dealing with monads for the first time, I tried reading source code and explanations. Soon I found that pure unapplied theory was making such a dismal, depressing feeling on me that I was not able to continue. But some time after I was writing an application in Haskell. It was real, and contrary to my previous theoretical studies the process was much fun. I had good FP background at that time, and had no problem writing everything in functional style. Since everything that can be done with monads can also be done without them, I didn't use any monad except IO (in main function :) ). Not that I especially avoided them, I just didn't think about them. And then I noticed that I constantly pass one same parameter to a lot of functions. And then -- since I remembered the boring theory -- bam! -- I introduced Reader into the code. And it worked. Which tempted me to interweave Reader and Writer, and so on, and twenty minutes later I had monstrosity that I only saw before in others' code: ReaderT WriterT State .... and so on :) So, good luck with your application!

On Wed, Dec 29, 2010 at 1:48 PM, Michael Lazarev
I had an "Aha!" moment and it all makes sense now. Just as the State monad can hold a generator (which can change) and pass it down a calculation chain, a Reader monad can hold an environment (which doesn't change) and
2010/12/29 michael rice
pass it down a calculation chain. I was wondering how I could include a (global) house betting limit in that craps application I've been playing with (without passing it as a parameter) and it sounds like the Reader monad would be an ideal candidate. Correct? It also sounds like a job for monad transforms. That is right. You need transformers if you want to have one value as settings to read, other value as a state, and so on, and they must be simultaneously accessible in some function. Or, for example, if you want to build a sequence of IO actions by functions that share the same environment.
After you said that you had an "Aha!" moment, I remembered how I had something alike not very long ago. That was surprising event when I ended up with some transformer stack written myself although just several minutes ago I would consider this to be some dark wizardry.
When I was dealing with monads for the first time, I tried reading source code and explanations. Soon I found that pure unapplied theory was making such a dismal, depressing feeling on me that I was not able to continue.
But some time after I was writing an application in Haskell. It was real, and contrary to my previous theoretical studies the process was much fun. I had good FP background at that time, and had no problem writing everything in functional style. Since everything that can be done with monads can also be done without them, I didn't use any monad except IO (in main function :) ). Not that I especially avoided them, I just didn't think about them.
And then I noticed that I constantly pass one same parameter to a lot of functions. And then -- since I remembered the boring theory -- bam! -- I introduced Reader into the code. And it worked. Which tempted me to interweave Reader and Writer, and so on, and twenty minutes later I had monstrosity that I only saw before in others' code: ReaderT WriterT State .... and so on :)
Reader Writer State is commonly needed in big applications so transformers provides one for us: http://hackage.haskell.org/packages/archive/transformers/0.2.2.0/doc/html/Co... Pretty cool stuff if you ask me. I often wondered about the correct stacking order of Monad transformers, or how often it mattered. Dave
So, good luck with your application!
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

2010/12/30 David Leimbach
Reader Writer State is commonly needed in big applications so transformers provides one for us: http://hackage.haskell.org/packages/archive/transformers/0.2.2.0/doc/html/Co... Pretty cool stuff if you ask me. I often wondered about the correct stacking order of Monad transformers, or how often it mattered.
Thanks, it is very cool indeed! And it turns out that it's not simply kind of the "type RWST r w s m a = ReaderT r WriterT w StateT s m a", it has completely independent implementation. It is done in one layer, so no lifts for tell, get, put, modify and so on.
participants (8)
-
Albert Y. C. Lai
-
Daniel Fischer
-
David Leimbach
-
Henning Thielemann
-
Michael Lazarev
-
michael rice
-
Ryan Ingram
-
Tillmann Rendel