开发者

Error: Ambiguous class occurrence "Ord"

data (Ord a) => Stree a = Null 
| Fork (Stree a) a (Stree a)

mkStree :: (Ord a) => [a] -> Stree a
mkStree [] = Null
mkStree (x:xs) = Fork (mkStree smaller) x (mkStree larger)
               where (smaller,larger) = partition (<= x) xs                    
partition :: (a->Bool) -> [a] -> ([a],[a])
partition p xs = ([ 开发者_JAVA百科x | x <- xs, p x],
                  [ y | y <- xs, (not . p) y])

how can i fix this failure --> Ambiguous class occurrence "Ord" * Could refer to: Hugs.Prelude.Ord


I cannot reproduce this error, but I can take some guesses. The "Ambiguous class occurrence" error means that there is more than one definition of "Ord" in scope, so the real problem is not in the code you've included here. The problem is either that one of the modules you're importing redefines "Ord" for some reason, or you're redefining it in your code. Either way, the only way that can work is if other definitions (such as the default one in the Prelude) are hidden or qualified. If you're trying to use a non-standard Ord implementation, you'll need to import the Prelude hiding Ord:

import Prelude hiding(Ord)

If you're not intentionally using a non-standard Ord, then you'll need to figure out where the second one is coming from and remove or hide it. There's not enough information in your question for me to be able to say how to do so, though. I would expect the error message is also longer than what you've listed here, as it should show the location of both definitions of Ord.


What happens if you remove the Ord context from the data declaration?

data Stree a = Null | Fork (Stree a) a (Stree a)

Class contexts on data declarations are pretty non-intuitive, and I suspect that may be causing Hugs to error.

In any case, you shouldn't put contexts on data or newtype declarations. They're useful with certain GHC extensions, but I don't think Hugs supports any of those cases.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜