3

说我有一个功能:

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 }’

为什么?我该如何解决?

4

3 回答 3

4

正如 user2407038 评论的那样,编译器不知道如何实例化a. 他提出的修复可能是最好的——你应该明确指定类型a

但为了完整起见,我想指出,还有其他解决方案,扩展默认规则

{-# LANGUAGE ExtendedDefaultRules #-}

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

该扩展修改了标准默认规则以包括更多情况,例如Eq类型类。

补充:经过一番思考,我重新考虑了我的答案。多态函数的单元测试结果不应依赖于实例化类型变量的特定方式。那么扩展的默认规则可能是测试中的正确方法吗?我从来没有将它们用作真正的代码,所以我不能肯定地说,但它绝对值得思考。

于 2016-01-16T13:00:37.743 回答
2
shouldBe                :: (Eq a, Show a) => a -> a -> Expectation
safeHead                ::         [a] ->    Maybe a
[]                      ::         [a]
safeHead []             ::                   Maybe a
Nothing                 ::                   Maybe a
shouldBe (safeHead [])  :: (Eq a, Show a) => Maybe a -> Expectation

shouldBe (safeHead []) Nothing ::                       Expectation -- but what's `a`?

如您所见,a完全模棱两可。它可以是具有实例Show的任何类型。Eq这也是您的错误消息的一部分:

没有因使用“shouldBe”而产生 (Eq a0) 的实例
类型变量“a0”不明确

所以选择一个:

it "returns Nothing for an empty list" $
    safeHead [] `shouldBe` (Nothing :: Maybe ())

当您使用它时,请使用 QuickCheck 来验证它的safeHead工作方式如下head

it "returns Just (head xs) for a non-empty list" $ property $ \(NonEmpty xs) ->
    safeHead xs `shouldBe` (Just (head xs) :: Maybe Integer)
于 2016-01-16T12:58:20.323 回答
0

此代码测试返回“Just”值的 Maybe Monad:

     import Test.Tasty
     import Test.Tasty.HUnit
     import Data.List         

     main = defaultMain tests

     tests = testGroup "Tests uncons form Data.List"

     [testCase "Show uncons [1,2,3,4] = (1, [2,3,4])" $
     Just (1,[2,3,4]) @=? (uncons [1,2,3,4])]
于 2018-12-05T20:45:24.853 回答