On Mon, 14 Jun 1999, Jose Bernardo Barros wrote:

> According to the definition of the class Bounded, minBound and maxBound
> have types
> 
>   minBound :: Bounded a => a
>   maxBound :: Bounded a => a
>            
> Suppose I define the function 
> 
> f (minBound, maxBound) = (maxBound, minBound)
> 
> shouldn't its type be 
> 
>   f :: (Bounded a, Bounded b, Bounded c, Bounded d) => (a,b) -> (c,d)  ?

If you were to transliterate this example to Mercury, the answer would be yes.
But in Haskell, the answer is no.  The reason is Haskell's scope rules.
In the definition,

        f (minBound, maxBound) = (maxBound, minBound)

the first occurrences of `minBound' and `maxBound' do not refer to
the member functions of the class `Bounded', instead they introduce
new variables.

In Haskell, you can't test for equality using pattern matching.
You need to use explicit calls to `=='.  For example, you can
put calls to `==' in a "|" guard:

        f (x, y) | x == minBound && y == maxBound = (maxBound, minBound)

This will have the type that you expected, with some additional `Eq'
constraints:

        f :: (Eq a, Bounded a, Eq b, Bounded b, Bounded d, Bounded c) =>
                (a,b) -> (c,d)

However, I'm not entirely sure whether it will have the semantics that
you expected, since it wasn't entirely clear from your original post
what semantics you wanted `f' to have.

-- 
Fergus Henderson <[EMAIL PROTECTED]>  |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>  |  of excellence is a lethal habit"
PGP: finger [EMAIL PROTECTED]        |     -- the last words of T. S. Garp.


Reply via email to