[Haskell-cafe] Why isn't pattern matching lazy by default?

Isaac Dupree isaacdupree at charter.net
Wed Sep 19 15:50:37 EDT 2007

Chaddaï Fouché wrote:
> 2007/9/19, Miguel Mitrofanov <miguelimo38 at yandex.ru>:
>>>> Now why isn't pattern matching lazy by default?  This seems odd
>>>> for a newbie since everything else is lazy by default.
>>> It's even more confusing that pattern matching in 'let' _is_ lazy.
>> No, it's not.
>> See, in let or where constructs you don't have a choice; you can't do
>> different things depending on whether some value is Just x or
>> Nothing. Therefore, there is no need to perform pattern matching
>> strictly.
> In other words, the pattern matching in case can't be lazy by default
> since the primary purpose of case is to choose an alternative and for
> that you NEED to evaluate the matched value. In let or where, the
> pattern matching is only there to allow convenient deconstruction of
> the value, but you don't make any choice based on the form of the
> value, so you can afford to be lazy (which is the default in the
> language everywhere it makes sense, it doesn't in a normal "case"
> construct).
> If you want to use your pattern matching in a case only to deconstruct
> the value but not to choose an alternative, you need to specify it
> using ~ .
> So, IMHO, the rules on strictness of the pattern matching are
> perfectly consistent and clear. You just need to reflect on the
> purpose of the constructs where you use it..

Except that newtype deconstruction doesn't introduce strictness - and 
you can't necessarily see from the syntax of a pattern-match whether 
it's newtype or data.


More information about the Haskell-Cafe mailing list