首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HSpec无期望未编译

HSpec无期望未编译
EN

Stack Overflow用户
提问于 2015-06-03 06:41:51
回答 1查看 558关注 0票数 3

我正在学习Haskell和我写了这个函数:

代码语言:javascript
复制
safeHead :: [a] -> Maybe a
safeHead [] = Nothing
safeHead (x:xs) = Just x

我现在正在尝试用HSpec测试它:

代码语言:javascript
复制
import Test.Hspec

main :: IO ()
main = hspec spec

spec :: Spec
spec =

  describe "safeHead" $
    it "should return Nothing for empty list" $
      safeHead [] `shouldBe` Nothing

但这不能编译:

代码语言:javascript
复制
Error:(14, 19) ghc: 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 (Maybe a) -- Defined in ‘GHC.Base’
      instance Eq a => Eq (GHC.Real.Ratio a) -- Defined in ‘GHC.Real’
      instance Eq Ordering -- Defined in ‘ghc-prim-0.4.0.0:GHC.Classes’
      ...plus 31 others
    In the second argument of ‘($)’, namely
      ‘safeHead [] `shouldBe` Nothing’
    In the second argument of ‘($)’, namely
      ‘it "should return Nothing for empty list"
       $ safeHead [] `shouldBe` Nothing’
    In the expression:
      describe "safeHead"
      $ it "should return Nothing for empty list"
        $ safeHead [] `shouldBe` Nothing

我也尝试过这样做:

代码语言:javascript
复制
safeHead :: (Eq a) => [a] -> Maybe a
safeHead [] = Nothing
safeHead (x:xs) = Just x

但这仍然不能满足以下情况:

代码语言:javascript
复制
Error:(14, 19) ghc: 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 Eq Data.Monoid.All -- Defined in ‘Data.Monoid’
      instance forall (k :: BOX) (f :: k -> *) (a :: k).
               Eq (f a) =>
               Eq (Data.Monoid.Alt f a)
        -- Defined in ‘Data.Monoid’
      ...plus 43 others
    In the second argument of ‘($)’, namely
      ‘safeHead [] `shouldBe` Nothing’
    In the second argument of ‘($)’, namely
      ‘it "should return Nothing for empty list"
       $ safeHead [] `shouldBe` Nothing’
    In the expression:
      describe "safeHead"
      $ it "should return Nothing for empty list"
        $ safeHead [] `shouldBe` Nothing

我不知道这有什么问题。如果我尝试像这样的其他测试,它会编译得很好:

代码语言:javascript
复制
    it "should return the head" $ do
      safeHead [1] `shouldBe` Just 1
      safeHead [2,3,4,5,6,1] `shouldBe` Just 2

所以,它是关于Nothing本身的,它不能被等价物比较?那么,如何断言某物返回Nothing呢?还是我的功能太普通了?

附带注意:我看到了与此函数类似的错误:

代码语言:javascript
复制
palindrome :: (Eq a) => [a] -> [a]
palindrome xs = xs ++ reverse xs

当尝试测试空列表时:

代码语言:javascript
复制
palindrome [] `shouldBe` []

在以下几个方面都失败了:

代码语言:javascript
复制
Error:(26, 21) ghc: 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 (Maybe a) -- Defined in ‘GHC.Base’
      instance Eq a => Eq (GHC.Real.Ratio a) -- Defined in ‘GHC.Real’
      instance Eq Ordering -- Defined in ‘ghc-prim-0.4.0.0:GHC.Classes’
      ...plus 32 others
    In a stmt of a 'do' block: palindrome [] `shouldBe` []
    In the second argument of ‘($)’, namely
      ‘do { palindrome [] `shouldBe` [] }’
    In the second argument of ‘($)’, namely
      ‘it
         "should turn a list into a palindrome, so it reads same both forwards and backwards"
       $ do { palindrome [] `shouldBe` [] }’
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-03 10:12:11

所以,它本身并不是什么,它不能被等价物比较吗?

Nothing的类型是什么?它是Nothing :: Maybe a。在这种情况下,GHC不喜欢a:“类型变量‘a0’是模棱两可的”。毕竟,shouldBe接受任何可以与(==)进行比较并显示的内容。如果aEq的一个实例,Maybe aEq的一个实例。GHC不可能知道要使用哪个a,所以需要手动指定它:

代码语言:javascript
复制
  describe "safeHead" $
    it "should return Nothing for empty list" $
      safeHead [] `shouldBe` (Nothing :: Maybe Int)

这不是胁迫,你只是明确了你想要使用的所有可能类型中的哪种。其他例子:

代码语言:javascript
复制
  describe "safeHead" $
    it "should return Nothing for empty list" $ do
      safeHead [] `shouldBe` (Nothing :: Maybe Int)
      safeHead [] `shouldBe` (Nothing :: Maybe ())
      safeHead [] `shouldBe` (Nothing :: Maybe Integer)
      safeHead [] `shouldBe` (Nothing :: Maybe Char)
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30612654

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档