I get Ambiguous occurrence
Really, let's try the code you posted..
cat <<EOF >umi.hs
import Data.Functor.Identity
import Control.Monad.Trans.State
type State s = StateT s Identity
EOF
ghci umi.hs
We get
Ok, one module loaded.
So that's actually fine. I suspect you have more code that tries to use State
but it is ambiguous because you have imported a State
and do not need to define your own State
type.
When I tried :t State, I find nothing. This is self-contradictory
When you try :t State
you get more than nothing, you get:
:t State
<interactive>:1:1: error:
• Data constructor not in scope: State
Read that carefully, it says "Data constructor" not "type" or "type constructor". You can't check the type of a type (:t
is short of :type
). You can, however, use info:
> :i State
type Control.Monad.Trans.State.State s =
StateT s Identity :: * -> *
-- Defined in ‘Control.Monad.Trans.State.Lazy’
type Main.State s = StateT s Identity :: * -> *
-- Defined at umi.hs:4:5
So you see, your (Main
s) type alias is redundant of the type alias you imported from Control.Monad.Trans.State
. Solution? Just don't define your type alias.
EDIT: The above was intended to clear up some misunderstandings. Answering your actual questions here.
Where is state monad
There are many State monads defined in Haskell libraries. The most popular one is in Control.Monad.Trans.State
from the transformers
package which you have already found. There's also a the monadlib
version of State, and the entirely different MonadState
class (in the mtl
package) which is a pair of get and set operations for any monad that has some notion of state.
and how can I use it?
Well you import the module and you can runState stateMonadicOperation initialState
:
> runState (mapM (\x -> state $ \s -> (x,s+x)) [1..4]) 9
([1,2,3,4],19)
There are a lot of state monad questions you can find here on stackoverflow if you have specific questions.
I wonder whether it exists or not?
This depends on the it
in your question. There is a State
type constructor but no State
data constructor.
using :i get, I found MonadState, is this the new implement of State?
No. State
the monad is defined in the tranformers as :
type State s = StateT s Data.Functor.Identity.Identity :: * -> *
newtype StateT s (m :: * -> *) a
= StateT {runStateT :: s -> m (a, s)}
So there is a transformer with a data constructor and a type alias of the transformer and identity monad.
In contrast, the MonadState is just an abstraction of the ability to get and set values:
class Monad m => MonadState s (m :: * -> *) | m -> s where
get :: m s
put :: s -> m ()
State
type constructor doesn't exist anymore. Instead you use thestate
function which resides in theMonadState
type class. For more check Control.Monad.State.Lazy – ReduState
as a standalone monad doesn’t exist any more, but aState
monad still exists; however, it’s defined in terms of anotherStateT
monad instead of being implemented as a separate monad of its own. This new state monad lives in theControl.Monad.Trans.State
module, in thetransformers
library. EDIT: I’ve just noticed you are already aware ofControl.Monad.Trans.State
— sorry for the redundant comment! – bradrn