说我有一个功能:
safeHead :: [a] -> Maybe a
safeHead [] = Nothing
safeHead xs = Just $ head xs
和一个测试:
describe "Example.safeHead" $ do
it "returns the head" $ do
safeHead [1,2,3] `shouldBe` Just 1
it "returns Nothing for an empty list" $
safeHead [] `shouldBe` Nothing
然而,这会产生:
No instance for (Eq a0) arising from a use of ‘shouldBe’
The type variable ‘a0’ is ambiguous
Note: there are several potential instances:
instance (Eq a, Eq b) => Eq (Either a b)
-- Defined in ‘Data.Either’
instance forall (k :: BOX) (s :: k). Eq (Data.Proxy.Proxy s)
-- Defined in ‘Data.Proxy’
instance (GHC.Arr.Ix i, Eq e) => Eq (GHC.Arr.Array i e)
-- Defined in ‘GHC.Arr’
...plus 88 others
In the second argument of ‘($)’, namely
‘safeHead [] `shouldBe` Nothing’
In a stmt of a 'do' block:
it "returns Nothing for an empty list"
$ safeHead [] `shouldBe` Nothing
In the second argument of ‘($)’, namely
‘do { it "returns the head"
$ do { safeHead [...] `shouldBe` Just 1 };
it "returns Nothing for an empty list"
$ safeHead [] `shouldBe` Nothing }’
为什么?我该如何解决?