开发者

What does `~` mean in Haskell?

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?


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.


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.


It's equivalent to

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

or

execStateT m s =
  runStateT m s >>= return . snd
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜