
Hi Could anyone please explain to me what is going on here? ------------------------------------------------------------------------ -- | The 'Ord' class is used for totally ordered datatypes. -- -- Instances of 'Ord' can be derived for any user-defined -- datatype whose constituent types are in 'Ord'. The declared order -- of the constructors in the data declaration determines the ordering -- in derived 'Ord' instances. The 'Ordering' datatype allows a single -- comparison to determine the precise ordering of two objects. -- -- Minimal complete definition: either 'compare' or '<='. -- Using 'compare' can be more efficient for complex types. -- 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 http://www.haskell.org/ghc/docs/latest/html/libraries/base/src/GHC-Classes.h... ------------------------------------------------------------------------ AFAIU this is the definition of the Ord type class in ghc. But what is this <= function that is used in the definition of compare? Here: else if x <= y then LT -- This e-mail address is invalid, see: http://people.eisenbits.com/~stf/public-email-note.html . OpenPGP: E3D9 C030 88F5 D254 434C 6683 17DD 22A0 8A3B 5CC0

2011/12/30 Stanisław Findeisen
Could anyone please explain to me what is going on here?
You must, when declaring an instance of Ord, provide *either* a definition of (compare) or a definition of (<=). Both have default implementations defined, but in terms of each other; so if you don't provide either one in your instance declaration, you get an infinite loop when you try to use Ord methods (because (compare) invokes (<=), which invokes (compare) ...). -- brandon s allbery allbery.b@gmail.com wandering unix systems administrator (available) (412) 475-9364 vm/sms

On 2011-12-30 12:58, Brandon Allbery wrote:
2011/12/30 Stanisław Findeisen
mailto:stf-list@eisenbits.com> Could anyone please explain to me what is going on here?
You must, when declaring an instance of Ord, provide *either* a definition of (compare) or a definition of (<=). Both have default implementations defined, but in terms of each other; so if you don't provide either one in your instance declaration, you get an infinite loop when you try to use Ord methods (because (compare) invokes (<=), which invokes (compare) ...).
If you don't provide either one, then you get an infinite loop, but this is a runtime problem, i.e. the code will compile. Right? How to declare an instance of Ord? -- This e-mail address is invalid, see: http://people.eisenbits.com/~stf/public-email-note.html . OpenPGP: E3D9 C030 88F5 D254 434C 6683 17DD 22A0 8A3B 5CC0

On Fri, Dec 30, 2011 at 01:28:42PM +0100, Stanisław Findeisen wrote:
On 2011-12-30 12:58, Brandon Allbery wrote:
2011/12/30 Stanisław Findeisen
mailto:stf-list@eisenbits.com> Could anyone please explain to me what is going on here?
You must, when declaring an instance of Ord, provide *either* a definition of (compare) or a definition of (<=). Both have default implementations defined, but in terms of each other; so if you don't provide either one in your instance declaration, you get an infinite loop when you try to use Ord methods (because (compare) invokes (<=), which invokes (compare) ...).
If you don't provide either one, then you get an infinite loop, but this is a runtime problem, i.e. the code will compile. Right?
Correct.
How to declare an instance of Ord?
instance Ord SomeType where compare x y = ... Declaring instances of type classes should be covered in any Haskell tutorial. -Brent

2011/12/30 Stanisław Findeisen
How to declare an instance of Ord?
You very rarely need to do so... In fact even in the report a lot of the basic instances are already derived automatically (which you can do by adding "deriving (Ord)" at the end of your data type definition, or "deriving instance Ord YourType" on a line by itself). If you do need to, it could look like that :
instance Ord Bool where compare True True = EQ compare True False = GT compare False True = LT compare False False = EQ
defining compare is enough, or you could define just (<=) :
instance Ord Bool where False <= _ = True True <= True = True True <= False = False
Note that nothing prevents you from defining both, or even the other comparison functions if there are performance considerations. -- Jedaï

As you can probably see, there are circular definitions with compare and <= To make 'yourtype' an an instance of ord you must supply
-- Minimal complete definition: either 'compare' or '<='.
and then the circular definitions will be broken and your definition will provide the key missing part. br On 30 Dec 2011, at 11:51, Stanisław Findeisen wrote:
Hi
Could anyone please explain to me what is going on here?
------------------------------------------------------------------------ -- | The 'Ord' class is used for totally ordered datatypes. -- -- Instances of 'Ord' can be derived for any user-defined -- datatype whose constituent types are in 'Ord'. The declared order -- of the constructors in the data declaration determines the ordering -- in derived 'Ord' instances. The 'Ordering' datatype allows a single -- comparison to determine the precise ordering of two objects. -- -- Minimal complete definition: either 'compare' or '<='. -- Using 'compare' can be more efficient for complex types. -- 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
http://www.haskell.org/ghc/docs/latest/html/libraries/base/src/GHC-Classes.h... ------------------------------------------------------------------------
AFAIU this is the definition of the Ord type class in ghc. But what is this <= function that is used in the definition of compare? Here:
else if x <= y then LT
-- This e-mail address is invalid, see: http://people.eisenbits.com/~stf/public-email-note.html .
OpenPGP: E3D9 C030 88F5 D254 434C 6683 17DD 22A0 8A3B 5CC0
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
participants (5)
-
Brandon Allbery
-
Brent Yorgey
-
Chaddaï Fouché
-
Henry Lockyer
-
Stanisław Findeisen