"Delegating" class instance definition

data Cycle = Cycle { name :: String, size :: Double } deriving (Eq, Show) instance Ord Cycle where (<) a b = (<) (size a) (size b) -- problem statement! … How can I specify that, on the right-hand side of the problem statement, I refer to (<) :: Double -> Double -> Bool instead of (<) :: Cycle -> Cycle -> Bool? Is there another way to state that "Cycle is an instance of Ord based on size"?

On Sat, Oct 11, 2014 at 9:02 AM, Nicolaas du Preez
data Cycle = Cycle { name :: String, size :: Double } deriving (Eq, Show)
instance Ord Cycle where (<) a b = (<) (size a) (size b) -- problem statement! …
How can I specify that, on the right-hand side of the problem statement, I refer to (<) :: Double -> Double -> Bool instead of (<) :: Cycle -> Cycle -> Bool?
That should be automatic, since it knows that size :: Cycle -> Double and therefore should use the Double instance. Can you provide a minimal full example and full error message? -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

Thanks for the reply - since you mentioned that it should work I figured there must’ve been a mistake somewhere else.
Looking closer I realised that when I copied & pasted the definition for (<) to the rest of the functions and the problem actually occurred at
...
max a b = max (size a) (size b)
min a b = min (size a) (size b)
…
which has return type of Double where it should be Cycle.
Regards,
Nicolaas
On 11 Oct 2014, at 15:39, Brandon Allbery
On Sat, Oct 11, 2014 at 9:02 AM, Nicolaas du Preez
wrote: data Cycle = Cycle { name :: String, size :: Double } deriving (Eq, Show) instance Ord Cycle where (<) a b = (<) (size a) (size b) -- problem statement! …
How can I specify that, on the right-hand side of the problem statement, I refer to (<) :: Double -> Double -> Bool instead of (<) :: Cycle -> Cycle -> Bool?
That should be automatic, since it knows that size :: Cycle -> Double and therefore should use the Double instance. Can you provide a minimal full example and full error message?
-- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Looking closer I realised that when I copied & pasted the definition for (<) to the rest of the functions and the problem actually occurred at ... max a b = max (size a) (size b) min a b = min (size a) (size b) … which has return type of Double where it should be Cycle.
To define an Ord instance you only need to implement compare because the other functions are derivable in terms of it. If you wany to explicitly implement max, a correct implementation could be: max a b = if size a < size b then b else a

Thanks for mentioning it; It saves a lot of copy & paste!
On 12 Oct 2014, at 01:03, Nadir Sampaoli
To define an Ord instance you only need to implement compare because the other functions are derivable in terms of it. If you wany to explicitly implement max, a correct implementation could be:

"Nicolaas du Preez":
data Cycle = Cycle { name :: String, size :: Double } deriving (Eq, Show)
instance Ord Cycle where (<) a b = (<) (size a) (size b) -- problem statement! …
How can I specify that, on the right-hand side of the problem statement,
I refer to (<) :: Double -> Double -> Bool instead of (<) :: Cycle -> Cycle -> Bool?
Is there another way to state that "Cycle is an instance of Ord based on
size"?
There is the `on` function in Data.Function which allows you to express such correlation. On a side note, you probably just want to implement `compare` so all the other functions of the Ord type class can be derived from it. -- Nadir
participants (3)
-
Brandon Allbery
-
Nadir Sampaoli
-
Nicolaas du Preez