
Hi everyone, I defined my own list datatype and then tried to declare it as an instance of type class Ord. However when I test it with Nil > Cons 1(Nil) I get an "ERROR - Control stack overflow" I am under the impression that the ord class defines default implementations of (<=), (>),(>=) so that I only have to supply the implementation of (<) shown below. Can some one tell me why this does not work the way I expect it to? My very own list data type
data List a = Nil | Cons a (List a) deriving (Show)
Derive the equality type for list.
instance (Eq a) => Eq (List a) where Nil == Nil = True Nil == Cons y ys = False Cons x xs == Nil = False Cons x xs == Cons y ys = (x == y) && (xs == ys)
Derive the ordered type for list
instance (Ord a) => Ord (List a) where Nil < Nil = False Nil < Cons y ys = True Cons x xs < Nil = False Cons x xs < Cons y ys = (x < y) && (xs < ys)
-- Lloyd G Smith

On Apr 3, 2005 12:41 AM, Lloyd Smith
Hi everyone, I defined my own list datatype and then tried to declare it as an instance of type class Ord. However when I test it with
Nil > Cons 1(Nil) I get an "ERROR - Control stack overflow"
I am under the impression that the ord class defines default implementations of (<=), (>),(>=) so that I only have to supply the implementation of (<) shown below. Can some one tell me why this does not work the way I expect it to?
My very own list data type
data List a = Nil | Cons a (List a) deriving (Show)
Derive the equality type for list.
instance (Eq a) => Eq (List a) where Nil == Nil = True Nil == Cons y ys = False Cons x xs == Nil = False Cons x xs == Cons y ys = (x == y) && (xs == ys)
Derive the ordered type for list
instance (Ord a) => Ord (List a) where Nil < Nil = False Nil < Cons y ys = True Cons x xs < Nil = False Cons x xs < Cons y ys = (x < y) && (xs < ys)
(<=), (>) and (>=) are defined using 'compare', not (<). Write 'compare' yourself and everything will be good. But why don't you just derive Eq and Ord for List? -- Friendly, Lemmih

Hi everyone, I defined my own list datatype and then tried to declare it as an instance of type class Ord. However when I test it with
Nil > Cons 1(Nil) I get an "ERROR - Control stack overflow"
I am under the impression that the ord class defines default implementations of (<=), (>),(>=) so that I only have to supply the implementation of (<) shown below. Can some one tell me why this does not work the way I expect it to?
Now, whereever did that interpretation come from?-) The library docs are pretty clear about what constitutes a minimal definition: http://www.haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t%3A... Have a closer look at Ord and its default definitions to see what happens in your case: http://www.haskell.org/onlinereport/basic.html#sect6.3.2 hth, claus

<=), (>) and (>=) are defined using 'compare', not (<). Write 'compare' yourself and everything will be good. But why don't you just derive Eq and Ord for List? I didn't even think of doing that. So I will say I did this way for
On Apr 2, 2005 4:12 PM, Lemmih
Hi everyone, I defined my own list datatype and then tried to declare it as an instance of type class Ord. However when I test it with
Nil > Cons 1(Nil) I get an "ERROR - Control stack overflow"
I am under the impression that the ord class defines default implementations of (<=), (>),(>=) so that I only have to supply the implementation of (<) shown below. Can some one tell me why this does not work the way I expect it to?
Now, whereever did that interpretation come from?-)
Page 32 of Introduction to Functional Programming using Haskell. I guess thats what I deserve for trusting a dead trees :-)
The library docs are pretty clear about what constitutes a minimal definition:
http://www.haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t%3A...
Have a closer look at Ord and its default definitions to see what happens in your case:
http://www.haskell.org/onlinereport/basic.html#sect6.3.2
hth, claus
Thanks for the pointers to the documentation. -- Lloyd G Smith

Now, whereever did that interpretation come from?-) Page 32 of Introduction to Functional Programming using Haskell. I guess thats what I deserve for trusting a dead trees :-)
I don't have my copy at hand - the online errata http://web.comlab.ox.ac.uk/oucl/publications/books/functional/ mention some confusion regarding Ord on page 33, but perhaps you may want to add to that list of errors? cheers, claus even dead trees come with security patches these days;)
participants (3)
-
Claus Reinke
-
Lemmih
-
Lloyd Smith