Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ambiguous Occurrence

I am currently learning how to write type classes. I can't seem to write the Ord type class with compile errors of ambiguous occurrence.

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 x y
         | x == y    =  EQ
         | x <= y    =  LT
         | otherwise =  GT

    x <= y           =  compare x y /= GT
    x <  y           =  compare x y == LT
    x >= y           =  compare x y /= LT
    x >  y           =  compare x y == GT

    -- note that (min x y, max x y) = (x,y) or (y,x)
    max x y 
         | x <= y    =  y
         | otherwise =  x
    min x y
         | x <= y    =  x
         | otherwise =  y

Errors are

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
...

and so on. I think it is clashing with the Prelude defined version.

like image 368
user1850254 Avatar asked May 07 '13 23:05

user1850254


1 Answers

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

To solve this, you can add an explicit import declaration which hides the conflicting names:

module Practice where

import Prelude hiding (Ord, compare, (<), (<=), (>=), (>), max, min)

...
like image 146
hammar Avatar answered Oct 31 '22 01:10

hammar