21

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?

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
Roman Gonzalez
  • 1,901
  • 13
  • 18

3 Answers3

16

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.

jrockway
  • 42,082
  • 9
  • 61
  • 86
Michael Easter
  • 23,733
  • 7
  • 76
  • 107
9

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
  • 222,467
  • 53
  • 283
  • 367
4

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
  • 47,861
  • 24
  • 143
  • 221