[Haskell-beginners] Ord

Brent Yorgey byorgey at seas.upenn.edu
Fri Dec 30 16:48:05 CET 2011


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 <stf-list at eisenbits.com
> > <mailto:stf-list at 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



More information about the Beginners mailing list