How to understand such a newtype ?

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

How to understand such a newtype ?

z_axis
newtype X a = X (ReaderT XConf (StateT XState IO) a)
#ifndef __HADDOCK__
    deriving (Functor, Monad, MonadIO, MonadState XState, MonadReader XConf, Typeable)
#endif

In `X (ReaderT XConf (StateT XState IO) a)`, X is a type constructor, how to understand `(ReaderT XConf (StateT XState IO) a)` ?  And why use `#ifndef __HADDOCK__` ?

Sincerely!
e^(π.i) + 1 = 0
Reply | Threaded
Open this post in threaded view
|

Re: How to understand such a newtype ?

Felipe Lessa
On Sat, Nov 21, 2009 at 06:20:40PM -0800, zaxis wrote:
> newtype X a = X (ReaderT XConf (StateT XState IO) a)
> #ifndef __HADDOCK__
>     deriving (Functor, Monad, MonadIO, MonadState XState, MonadReader XConf,
> Typeable)
> #endif
>
> In `X (ReaderT XConf (StateT XState IO) a)`, X is a type constructor, how to
> understand `(ReaderT XConf (StateT XState IO) a)` ?

Well, “ReaderT XConf (StateT XState IO) a” is *the* type :).
It's a monad that is a Reader of XConf and has a State of XState.
This means you can use, for example,

  ask :: X XConf

and

  get :: X XState

> And why use `#ifndef __HADDOCK__` ?

Because Haddock used to have difficulties in processing some
directives, like that “deriving (..., MonadState XState, ...)”
which is part of the GeneralizedNewtypeDeriving extension.

HTH,

--
Felipe.
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: How to understand such a newtype ?

z_axis
This post was updated on .
thanks your quick answer first !

> :m + Control.Monad.Reader
> :t ReaderT
ReaderT :: (r -> m a) -> ReaderT r m a

> :m + Control.Monad.State
> :t StateT
StateT :: (s -> m (a, s)) -> StateT s m a

data XConf = XConf {...}
data XState = XState {...}

Both ReaderT and StateT needs a function as its 1st parameter, then in `ReaderT XConf (StateT XState IO) a`,  which parts is the function parameter for ReaderT and StateT respectively ?

Felipe Lessa wrote
On Sat, Nov 21, 2009 at 06:20:40PM -0800, zaxis wrote:
> newtype X a = X (ReaderT XConf (StateT XState IO) a)
> #ifndef __HADDOCK__
>     deriving (Functor, Monad, MonadIO, MonadState XState, MonadReader XConf,
> Typeable)
> #endif
>
> In `X (ReaderT XConf (StateT XState IO) a)`, X is a type constructor, how to
> understand `(ReaderT XConf (StateT XState IO) a)` ?

Well, “ReaderT XConf (StateT XState IO) a” is *the* type :).
It's a monad that is a Reader of XConf and has a State of XState.
This means you can use, for example,

  ask :: X XConf

and

  get :: X XState

> And why use `#ifndef __HADDOCK__` ?

Because Haddock used to have difficulties in processing some
directives, like that “deriving (..., MonadState XState, ...)”
which is part of the GeneralizedNewtypeDeriving extension.

HTH,

--
Felipe.
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
e^(π.i) + 1 = 0
Reply | Threaded
Open this post in threaded view
|

Re: How to understand such a newtype ?

Cristiano Paris
In reply to this post by Felipe Lessa
On Sun, Nov 22, 2009 at 3:47 AM, Felipe Lessa <[hidden email]> wrote:
> ...
> Well, “ReaderT XConf (StateT XState IO) a” is *the* type :).
> It's a monad that is a Reader of XConf and has a State of XState.

... and also wraps a monad to allow IO access inside the X monad.

Cristiano
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe