HSpec没有期望无法编译 [英] HSpec Nothing expectation failing to compile

查看:125
本文介绍了HSpec没有期望无法编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  safeHead :: [a]  - >我学习Haskell并编写了这个函数:可能是
safeHead [] = Nothing
safeHead(x:xs)=只是x



  import Test.Hspec 

main :: IO()
main = hspec spec

spec :: Spec
spec =

描述safeHead$
it 应该返回没有空列表$
safeHead []`shouldBe`没有

但是(b)没有编译的实例:

 错误:(14,19)ghc:没有实例(等式a0) 'shouldBe'
类型变量'a0'不明确
注意:有几个潜在的实例:
instance Eq a =>等式(也许a) - 定义于'GHC.Base'
实例等式a =>公式(GHC.Real.Ratio a) - 在'GHC.Real'中定义
实例公式排序 - 定义在'ghc-prim-0.4.0.0:GHC.Classes'
... plus其他
在'($)'的第二个参数中,即
'safeHead []`shouldBe` Nothing'
在'($)'的第二个参数中,即
'it应该返回Nothing for empty list
$ safeHead []`shouldBe` Nothing'
在表达式中:
描述safeHead
$ it应该返回Nothing for empty list
$ safeHead []`shouldBe` Nothing

我也试过:

  safeHead ::(等式a)=> [a]  - >可能是
safeHead [] = Nothing
safeHead(x:xs)=只是x

但是仍然失败:

 错误:(14,19)ghc:(Eq a0)没有实例出现从'shouldBe'的使用
类型变量'a0'不明确
注意:有几种潜在的情况:
instance(Eq a,Eq b)=>等式(或ab)
- 定义于'Data.Either'
实例Eq Data.Monoid.All - 定义于'Data.Monoid'
实例forall(k :: BOX) (f :: k - > *)(a :: k)。
Eq(f a)=>
Eq(Data.Monoid.Alt fa)
- 在'Data.Monoid'中定义
...加43其他
在'($)'的第二个参数中,即
'safeHead []`shouldBe`没有'
在'($)'的第二个参数中,即
'它应该返回空列表的任何值
$ safeHead []`shouldBe` Nothing'
在表达式中:
描述safeHead
$它应该为空列表返回Nothing
$ safeHead []`shouldBe` Nothing

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

 它应该返回头部$ do 
safeHead [1]`shouldBe`只需1
safeHead [2,3,4,5,6,1]`shouldBe`只需2

所以,这是关于 Nothing 本身的东西,它不能通过等于比较吗?你如何断言返回 Nothing 然后呢?或者是我的函数过于泛型?



边注:
我看过类似的错误:

  palindrome ::(等式a)=> [a]  - > [a] 
palindrome xs = xs ++反向xs

当试图测试空列表:

  palindrome []`shouldBe` [] 

错误:(26,21)ghc:

没有使用'shouldBe'引发(Eq a0)的实例
类型变量'a0'不明确
注意:有几个潜在实例:
instance Eq a =>等式(也许a) - 定义于'GHC.Base'
实例等式a =>公式(GHC.Real.Ratio a) - 在'GHC.Real'中定义
实例公式排序 - 定义在'ghc-prim-0.4.0.0:GHC.Classes'
... plus 32其他
在'do'区块中:palindrome []`shouldBe` []
在'($)'的第二个参数中,即
'do {palindrome [] `shouldBe` []}'
在'($)'的第二个参数中,即
'它
应该将一个列表变成一个回文,所以它前后读取相同
$ do {palindrome []`shouldBe` []}'


解决方案



这是关于Nothing本身的事情,它不能用equals来比较吗?

什么是没有的类型?它是 Nothing ::也许是一个。在这种情况下,GHC不喜欢 a :类型变量'a0'不明确。毕竟, shouldBe 会将任何可与(==)进行比较并显示的内容。并且如果 a 是,也许是 Eq 的一个实例一个 Eq 的实例。 GHC不可能知道您要使用哪一种 a ,因此您需要手动指定它:

 描述safeHead$ 
它应该为空列表返回Nothing$
safeHead []`shouldBe`(Nothing :: Maybe诠释)

这不是强制,你只是清楚哪个的所有可能的类型,你想使用。其他示例:

$ pre $ 描述safeHead$
它应该为空列表返回Nothing$ do
safeHead []`shouldBe`(Nothing :: Maybe Int)
safeHead []`shouldBe`(Nothing :: Maybe())
safeHead []`shouldBe`(Nothing :: Maybe Integer)
safeHead []`shouldBe`(Nothing :: Maybe Char)


I'm learning Haskell and I've written this function:

safeHead :: [a] -> Maybe a
safeHead [] = Nothing
safeHead (x:xs) = Just x

I'm now trying to test it with HSpec:

import Test.Hspec

main :: IO ()
main = hspec spec

spec :: Spec
spec =

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

But that fails to compile:

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

I have also tried this:

safeHead :: (Eq a) => [a] -> Maybe a
safeHead [] = Nothing
safeHead (x:xs) = Just x

But that still fails with:

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

I don't know what's the problem here. If I try other tests like these, it compiles fine:

    it "should return the head" $ do
      safeHead [1] `shouldBe` Just 1
      safeHead [2,3,4,5,6,1] `shouldBe` Just 2

So, it's something about Nothing itself, that it cannot be compared by equals? How do you assert that something returns Nothing then? Or is my function too generic?

SIDE NOTE: I've seen a similar error with this function:

palindrome :: (Eq a) => [a] -> [a]
palindrome xs = xs ++ reverse xs

When trying to test for empty lists:

palindrome [] `shouldBe` []

Which fails with:

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` [] }’

解决方案

So, it's something about Nothing itself, that it cannot be compared by equals?

What's Nothing's type? It's Nothing :: Maybe a. And GHC doesn't like a in this context: "The type variable ‘a0’ is ambiguous". After all, shouldBe takes anything that can be compared with (==) and shown. And Maybe a is an instance of Eq if a is an instance of Eq. GHC can't possibly know which a you want to use, so you need to specify it by hand:

  describe "safeHead" $
    it "should return Nothing for empty list" $
      safeHead [] `shouldBe` (Nothing :: Maybe Int)

That's not coercion, you're just making clear which of all possible types you want to use. Other examples:

  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)

这篇关于HSpec没有期望无法编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆