views:

223

answers:

3

Hey Haskellers,

Right now I'm studying the mtl library, 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'

I'm wondering, what this "~" operand means?

Thanks in advance.

+8  A: 

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.

Michael Easter
For anybody confused, that "|" in the quoted text above is supposed to be a "⊥". Looks like SO's text formatting got in the way.
ephemient
Oops. thanks for the note / edit!
Michael Easter
+1  A: 

It's equivalent to

execStateT m s = do
  r <- runStateT m s
  return (snd r)

or

execStateT m s =
  runStateT m s >>= return . snd
finnw
+1  A: 

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.

sth