Reputation: 1931
I'm studying the mtl
library and trying to do some MonadTransformers of my own. I was checking the Control.Monad.State.StateT
declaration, and across all the code, I see this syntax:
execStateT :: (Monad m) => StateT s m a -> s -> m s
execStateT m s = do
~(_, s') <- runStateT m s
return s'
What does this ~
operand mean?
Upvotes: 21
Views: 2174
Reputation: 229613
For a normal pattern match, the value that should be matched needs to be evaluated, so that it can be compared against the pattern.
~
denotes a lazy pattern match: It is just assumed that the value will match the pattern. The match is then only done later, if the value of a matched variable is actually used.
Upvotes: 9
Reputation: 24468
This is the notation for a lazy pattern in Haskell. I can't say that I'm familiar with it but from here:
It is called a lazy pattern, and has the form ~pat. Lazy patterns are irrefutable: matching a value v against ~pat always succeeds, regardless of pat. Operationally speaking, if an identifier in pat is later "used" on the right-hand-side, it will be bound to that portion of the value that would result if v were to successfully match pat, and ⊥ otherwise.
Also, this section may be useful.
Upvotes: 16
Reputation: 48629
It's equivalent to
execStateT m s = do
r <- runStateT m s
return (snd r)
or
execStateT m s =
runStateT m s >>= return . snd
Upvotes: 4