
Hello all, with this code import Data.List import Data.Function data Tree a = B a (Tree a) (Tree a) | L a Char deriving Show get (B a _ _) = a get (L a _) = a tcmp = compare `on` get build :: [Tree Int] -> [Tree Int] build (t:[]) = [t] -- build t = let xs = sortBy (compare `on` get) t -- < -- build t = let xs = sortBy tcmp t in build (merge (xs!!0) (xs!!1) : drop 2 xs) The commented line -- < -- does not work, though I am just replacing equals with equals. I get No instance for (Ord b0) arising from a use of ‘compare’ The type variable ‘b0’ is ambiguous Relevant bindings include tcmp :: Tree b0 -> Tree b0 -> Ordering (bound at /home/martin/projects/haskell/exercises/99_questions/xxx.hs:10:1) Note: there are several potential instances: instance Integral a => Ord (GHC.Real.Ratio a) -- Defined in ‘GHC.Real’ instance Ord a => Ord (Control.Applicative.ZipList a) -- Defined in ‘Control.Applicative’ instance Ord Integer -- Defined in ‘integer-gmp:GHC.Integer.Type’ ...plus 24 others In the first argument of ‘on’, namely ‘compare’ In the expression: compare `on` get In an equation for ‘tcmp’: tcmp = compare `on` get Why is that so?

On Tue, Dec 08, 2015 at 11:29:22PM +0100, martin wrote:
Hello all,
with this code
[...]
The commented line -- < -- does not work, though I am just replacing equals with equals. I get
No instance for (Ord b0) arising from a use of ‘compare’
Try to erase the `build` function and see what happens; You will get the same error. GHC complains that there is no good `Ord` instance and this can be solved by adding an appropriate type signature: tcmp :: (Ord a) => Tree a -> Tree a -> Ordering Now, with the `build` function present (the one with `let xs = sortBy tcmp t`), GHC will infer `tcmp` signature, as t is an Int λ> :t tcmp tcmp :: Tree Int -> Tree Int -> Ordering as since Int is an instance of Ord everything is fine. Does that help?

Am 12/09/2015 um 01:03 AM schrieb Francesco Ariis:
On Tue, Dec 08, 2015 at 11:29:22PM +0100, martin wrote:
Hello all,
with this code
[...]
The commented line -- < -- does not work, though I am just replacing equals with equals. I get
No instance for (Ord b0) arising from a use of ‘compare’
Try to erase the `build` function and see what happens; You will get the same error. GHC complains that there is no good `Ord` instance and this can be solved by adding an appropriate type signature:
tcmp :: (Ord a) => Tree a -> Tree a -> Ordering
Now, with the `build` function present (the one with `let xs = sortBy tcmp t`), GHC will infer `tcmp` signature, as t is an Int
λ> :t tcmp tcmp :: Tree Int -> Tree Int -> Ordering
as since Int is an instance of Ord everything is fine. Does that help?
Oh, I see. I was assuming the message referred to the build function (because I made chages there) and not to tcmp. Didn't check the line number. But really tcmp has lost its roots by not being used in build anymore.

On Tue, 08 Dec 2015 23:29:22 +0100, martin
data Tree a = B a (Tree a) (Tree a) | L a Char deriving Show
get (B a _ _) = a get (L a _) = a
tcmp = compare `on` get
build :: [Tree Int] -> [Tree Int] build (t:[]) = [t] -- build t = let xs = sortBy (compare `on` get) t -- < -- build t = let xs = sortBy tcmp t in build (merge (xs!!0) (xs!!1) : drop 2 xs)
The commented line -- < -- does not work, though I am just replacing equals with equals. I get
No instance for (Ord b0) arising from a use of ‘compare’ The type variable ‘b0’ is ambiguous Relevant bindings include tcmp :: Tree b0 -> Tree b0 -> Ordering : Why is that so?
If you do not use the function tcmp, the compiler can not deduce the type of it. When you add a type like tcmp :: Ord a => Tree a -> Tree a -> Ordering , the compiler has enough information to compile the program. Regards, Henk-Jan van Tuyl -- Folding@home What if you could share your unused computer power to help find a cure? In just 5 minutes you can join the world's biggest networked computer and get us closer sooner. Watch the video. http://folding.stanford.edu/ http://Van.Tuyl.eu/ http://members.chello.nl/hjgtuyl/tourdemonad.html Haskell programming --
participants (3)
-
Francesco Ariis
-
Henk-Jan van Tuyl
-
martin