为什么"出现使用'quickCheck'"错误导致的(任意)实例?

mad*_*i98 2 haskell cmd quickcheck ghci

我是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
Run Code Online (Sandbox Code Playgroud)

当我尝试运行测试时会发生这种情况:

*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
Run Code Online (Sandbox Code Playgroud)

我的老师告诉我我的代码是正确的,但没有解释为什么在Windows上发生这种情况,因此没有提供任何解决方案.我在网上找不到任何有用的东西.我真的很感激解释.

Tho*_*son 7

你定义了:

Direction = North | South | East | West
        deriving (Eq, Show)
Run Code Online (Sandbox Code Playgroud)

instance Arbitrary Direction上面没有.毕竟,怎么会有?你刚刚定义了方向,全世界唯一的例子是EqShow.

尝试:

import Test.QuickCheck
data Direction = North | South | East | West
   deriving (Eq,Show)

instance Arbitrary Direction where
    arbitrary = elements [North,South,East,West]
Run Code Online (Sandbox Code Playgroud)

elements函数来自Test.QuickCheck就像Arbitrary.

作为一个元音符:如果你的导师没有立即看到问题,那么就会出现误传,或者你应该计划补充你的Haskell教育,比如使用像wikibooks这样的在线资源,像The Craft of the Functional Programming这样的印刷材料,或者在freenode这样的地方进行了大量的对话.