FW: Why does this Ord-class instance crash?

Why does the following, trivial code snippet below hang GHCi when I type"Scalene > Failure", and what's the fix? data Triangle = Failure | Equilateral | Isosceles | Scalene deriving (Eq, Show) instance Ord Triangle where Failure < Failure = False Failure < _ = True Equilateral < Failure = False Equilateral < Equilateral = False Equilateral < _ = True Isosceles < Scalene = True Isosceles < _ = False Scalene < _ = False (I tried submitting this to beginners@haskell.org, but even though I've signed up for that mailing list, I got a bounce-back saying that I needed admin approval to submit anything to that list, and I haven't heard from an admin, so I'm posting it here.) _________________________________________________________________ The New Busy is not the too busy. Combine all your e-mail accounts with Hotmail. http://www.windowslive.com/campaign/thenewbusy?tile=multiaccount&ocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_4

2010/5/21 R J
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

On Friday 21 May 2010 19:06:51, R J wrote:
Why does the following, trivial code snippet below hang GHCi when I type"Scalene > Failure", and what's the fix?
For an Ord instance, you need to define at least one of compare and (<=) or the other functions from the class won't work. All methods have default implementations in terms of compare, compare has a default implementation in terms of (<=). If you only implement (<) and then query (>), it's going in circles. Fixes: a) implement compare or (<=) b) choose the correct order for the constructors and add Ord to the deriving clause.
data Triangle = Failure | Equilateral | Isosceles | Scalene deriving (Eq, Show) instance Ord Triangle where Failure < Failure = False Failure < _ = True Equilateral < Failure = False Equilateral < Equilateral = False Equilateral < _ = True Isosceles < Scalene = True Isosceles < _ = False Scalene < _ = False
Your newlines never make it to my mail programme, is your mail programme configured to send only '\r' and not '\n' ?
(I tried submitting this to beginners@haskell.org, but even though I've signed up for that mailing list, I got a bounce-back saying that I needed admin approval to submit anything to that list, and I haven't heard from an admin, so I'm posting it here.)
If you sent very shortly after subscribing, your subscription may not have been registered by the watchdog. If you continue to have problems you should contact the list manager.

From Prelude.hs: class (Eq a) => Ord a where compare :: a -> a -> Ordering (<), (<=), (>), (>=) :: a -> a -> Bool max, min :: a -> a -> a compare x y = if x == y then EQ -- NB: must be '<=' not '<' to validate the -- above claim about the minimal things that -- can be defined for an instance of Ord: 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 } -- These two default methods use '<=' rather than 'compare' -- because the latter is often more expensive max x y = if x <= y then y else x min x y = if x <= y then x else y So, in your case: Scalene > Failure -> {Gee, there's no definition of ">" in this "Ord" instance; I'll have to take the default one} compare Scalene Failure -> {There's no definition of "compare" either; again, the default one} Scalene <= Failure -> {Again, there is no definition of "<="} compare Scalene Failure -> loop On 21 May 2010, at 21:06, R J wrote:
Why does the following, trivial code snippet below hang GHCi when I type "Scalene > Failure", and what's the fix?
data Triangle = Failure | Equilateral | Isosceles | Scalene deriving (Eq, Show)
instance Ord Triangle where Failure < Failure = False Failure < _ = True
Equilateral < Failure = False Equilateral < Equilateral = False Equilateral < _ = True
Isosceles < Scalene = True Isosceles < _ = False
Scalene < _ = False
(I tried submitting this to beginners@haskell.org, but even though I've signed up for that mailing list, I got a bounce-back saying that I needed admin approval to submit anything to that list, and I haven't heard from an admin, so I'm posting it here.)
The New Busy is not the too busy. Combine all your e-mail accounts with Hotmail. Get busy._______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (4)
-
Daniel Fischer
-
David Menendez
-
Miguel Mitrofanov
-
R J