gpt4 book ai didi

haskell - HSpec 无预期编译失败

转载 作者:行者123 更新时间:2023-12-02 14:35:23 24 4
gpt4 key购买 nike

我正在学习 Haskell,我已经编写了这个函数:

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

我现在正在尝试使用 HSpec 对其进行测试:

import Test.Hspec

main :: IO ()
main = hspec spec

spec :: Spec
spec =

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

但是编译失败:

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

我也尝试过这个:

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

但是仍然失败:

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

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

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

那么,这是关于Nothing本身的问题,它不能通过相等进行比较?那么你如何断言某物返回Nothing呢?或者我的功能太通用了?

旁注:我在这个函数中看到过类似的错误:

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

尝试测试空列表时:

palindrome [] `shouldBe` []

失败的原因是:

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?

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

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

这不是强制,您只是明确要使用所有可能类型中的哪一种。其他示例:

  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)

关于haskell - HSpec 无预期编译失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30612654/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com