我是Haskell的新手,但遇到此错误有些麻烦。我在Windows上使用ghci。这是代码:
data Direction = North | South | East | West
deriving (Eq, Show)
type Point = (Int, Int)
origin = (0,0)
type Road = [Direction]
movement :: Point -> Road -> Point
{- ... }
test :: Road -> Road -> Bool
test road1 road2 = movement origin road1 == movement origin road2
-- i check if two roads lead to the same destination starting from
-- the origin point in a grid
这是我尝试运行测试时发生的情况:
*Main> quickCheck test
<interactive>:8:1: error:
* No instance for (Arbitrary Direction)
arising from a use of `quickCheck'
* In the expression: quickCheck test
In an equation for `it': it = quickCheck test
我的老师告诉我我的代码是正确的,但是没有解释为什么在Windows上会发生这种情况,因此没有提供解决方案。我在网络上都找不到任何有用的东西。我非常感谢您的解释。
答案 0 :(得分:7)
您定义:
Direction = North | South | East | West
deriving (Eq, Show)
以上没有instance Arbitrary Direction
。毕竟怎么会有?您刚刚定义了方向,全世界仅有的实例是Eq
和Show
。
尝试:
import Test.QuickCheck
data Direction = North | South | East | West
deriving (Eq,Show)
instance Arbitrary Direction where
arbitrary = elements [North,South,East,West]
elements
函数来自Test.QuickCheck
,就像Arbitrary
一样。
作为元注释:如果您的讲师没有立即在此处看到问题,则可能是沟通不畅,或者您应该计划补充Haskell教育,例如使用诸如Wikibooks的在线资源,诸如The Craft of Functional的印刷资料编程或在freenode之类的地方进行很多对话。