[Haskell-cafe] Are there any reasons against using ~ when matching one-constructor data types?

Roman Cheplyaka roma at ro-che.info
Wed Mar 13 20:18:15 CET 2013


* Petr Pudlák <petr.mvd at gmail.com> [2013-03-13 16:32:45+0100]
> If I define a function that matches on a single-constructor data type, such
> as (,), is there any reason against using ~? Like
> 
>   f (a,b) = ...
> 
> instead of
> 
>   f ~(a,b) = ...
> 
> ? I've seen that not using ~ can lead to problems sometimes, but not the
> other way around.
> 
> (Of course changing the semantics of existing functions such as `swap` is
> problematic, my question targets the problem in general.)

The usual dangers of laziness apply.

Consider, for example, the State monad. The difference between lazy and
strict State is exactly the way of pattern matching on the tuple.

Strict state monad allows you to keep your state evaluated. Lazy
doesn't.

  Prelude Control.Monad.State.Strict> flip evalState () $ (put $! undefined) >> return ()
  *** Exception: Prelude.undefined

  Prelude Control.Monad.State> flip evalState () $ (put $! undefined) >> return ()
  ()

Roman



More information about the Haskell-Cafe mailing list