Petr Pudlák | 13 Mar 16:32 2013
Picon

Are there any reasons against using ~ when matching one-constructor data types?

Dear Haskellers,

   b) A related suggestion would be the addition of an
      irrefutable swap, (swap'?), defined as
       "swap ~(a,b) = (b,a)", and its addition to Prelude for
      the same reasons.

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.)

Best regards,
Petr Pudlak
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Roman Cheplyaka | 13 Mar 20:18 2013

Re: Are there any reasons against using ~ when matching one-constructor data types?

* 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

Gmane