【问题标题】:Why does "No instance for (Arbitrary) arising from a use of `quickCheck'" error appears?为什么会出现“No instance for (Arbitrary) from a use of `quickCheck'”错误?
【发布时间】:2018-10-22 15:55:26
【问题描述】:

我是 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 上发生这种情况,因此没有提供解决方案。我也没有在网上找到任何有用的东西。我真的很感激解释。

【问题讨论】:

  • 您需要定义一个instance Arbitrary Direction。请参阅快速检查库的文档

标签: haskell cmd ghci quickcheck


【解决方案1】:

你定义了:

Direction = North | South | East | West
        deriving (Eq, Show)

上面没有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]

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

作为元注释:如果您的讲师没有立即发现此处的问题,则可能是沟通不畅,或者您应该计划补充您的 Haskell 教育,例如使用维基书籍等在线资源、功能工艺等印刷材料编程,或在 freenode 之类的地方进行大量对话。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多