[Haskell-cafe] FW: Why does this Ord-class instance crash?

David Menendez dave at zednenem.com
Fri May 21 13:20:32 EDT 2010


2010/5/21 R J <rj248842 at hotmail.com>:
> Why does the following, trivial  code snippet below hang GHCi when I type
> "Scalene > Failure", and what's the fix?

An instance of Ord must declare compare or (<=). You only defined (<),
so (>) is using the default definition. Here are the defaults:

    compare x y = if x == y then EQ
                  else if x <= y then LT
                  else GT

    x <  y = case compare x y of { LT -> True;  _ -> False }
    x <= y = case compare x y of { GT -> False; _ -> True }
    x >  y = case compare x y of { GT -> True;  _ -> False }
    x >= y = case compare x y of { LT -> False; _ -> True }
    max x y = if x <= y then y else x
    min x y = if x <= y then x else y

Note that the default definitions of (<=) and compare call each other,
leading to an infinite loop when both are used.

Simple fix: define (<=) instead of (<).
-- 
Dave Menendez <dave at zednenem.com>
<http://www.eyrie.org/~zednenem/>


More information about the Haskell-Cafe mailing list