Controversial incident

I am currently learning how to write type classes. I can’t write a class like Ord with ambiguous compilation errors.

module Practice where class (Eq a) => Ord a where compare :: a -> a -> Ordering (<), (<=), (>=), (>) :: a -> a -> Bool max, min :: a -> a -> a -- Minimal complete definition: -- (<=) or compare -- Using compare can be more efficient for complex types. compare xy | x == y = EQ | x <= y = LT | otherwise = GT x <= y = compare xy /= GT x < y = compare xy == LT x >= y = compare xy /= LT x > y = compare xy == GT -- note that (min xy, max xy) = (x,y) or (y,x) max xy | x <= y = y | otherwise = x min xy | x <= y = x | otherwise = y 

Mistakes

 Practice.hs:26:14: Ambiguous occurrence `<=' It could refer to either `Practice.<=', defined at Practice.hs:5:10 or `Prelude.<=', imported from `Prelude' at Practice.hs:1:8-15 ... 

etc. I think it is facing a specific version of Prelude.

+7
source share
2 answers

The problem is that the names of your functions collide with the standard ones from Prelude.

To solve this problem, you can add an explicit import declaration that hides conflicting names:

 module Practice where import Prelude hiding (Ord, compare, (<), (<=), (>=), (>), max, min) ... 
+19
source

hammar is right, this is due to a clash with the standard Prelude names. But there is one more solution besides the hiding names from Prelude.

You can import the foreplay:

 module Practice where import qualified Prelude as P ... 

Then you can access both the standard and standard versions: max will execute your version, and P.max will execute standard prelude.

There is also a way to completely hide all standard Prelude functions: the GHC extension NoImplicitPrelude ( http://www.haskell.org/haskellwiki/No_import_of_Prelude ). It can be activated by recording

 {-# LANGUAGE NoImplicitPrelude #-} 

at the very beginning of your file

+10
source

All Articles