[Haskell-cafe] Re: [Haskell] specified or not

Ross Paterson ross at soi.city.ac.uk
Thu Mar 3 09:56:45 EST 2005


[moving to the cafe]

On Thu, Mar 03, 2005 at 09:39:17AM -0500, Scott Turner wrote:
> Is the behavior of evaluating z unspecified?
>   z = f (0, z)
>   f x = case x of
>  (1,1) -> z
>  _ -> 0
> Hugs and GHC agree that z evaluates to 0. However, if the first line is 
> changed to
>   z = f (z,0)
> then both implementations loop. In other words, the behavior depends on order 
> of evaluation, which AFAIK is not specified.

The order of pattern matching is specified: pre-order, left to right.
In detail: according to the rules of Section 3.17.3 of the Report,
the definition of f is equivalent to

f x = case x of
        (x1, x2) -> case x1 of
                1 -> case x2 of
                        1 -> z
                        _ -> 0
                _ -> 0
        _ -> 0

and hence

f (0, anything) = 0
f (undefined, 0) = undefined


More information about the Haskell-Cafe mailing list