- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试解决与 this other question 的第 15 章中的 "Haskell Programming from First Principles" 相同的练习。我已经创建了一个 Semigroup 实例,但在编写练习的 QuickCheck 部分时遇到了麻烦。
Semigroup 实例应满足:
a <> (b <> c) == (a <> b) <> c
<>
是 Semigroup mappend。
import Data.Semigroup
import Test.QuickCheck
semigroupAssoc :: (Eq m, Semigroup m) => m -> m -> m -> Bool
semigroupAssoc a b c = (a <> (b <> c)) == ((a <> b) <> c)
newtype Combine a b = Combine { unCombine :: (a -> b) }
instance Semigroup b => Semigroup (Combine a b) where
(Combine f) <> (Combine g) = Combine (\x -> (f x) <> (g x))
instance CoArbitrary (Combine a b) where
coarbitrary (Combine f) = variant 0
instance (CoArbitrary a, Arbitrary b) => Arbitrary (Combine a b) where
arbitrary = do
f <- arbitrary
return $ Combine f
type CombineAssoc a b = Combine a b -> Combine a b -> Combine a b -> Bool
main :: IO ()
main = do
quickCheck (semigroupAssoc :: CombineAssoc Int Bool)
quickCheck
行之外,所有内容都可以编译,它提示存在
No instance for (Eq (Combine Int Bool)) arising from a use of ‘semigroupAssoc’
。
Combine
包装的函数),但练习文本表明这样的事情是可能的。
Hint: This function will eventually be applied to a single value of type a. But you’ll have multiple functions that can produce a value of type b. How do we combine multiple values so we have a single b? This one will probably be tricky! Remember that the type of the value inside of Combine is that of a function. If you can’t figure out CoArbitrary, don’t worry about QuickChecking this one.
最佳答案
你不能决定两个函数是否相等。但是你可以测试它!
两个函数相等当且仅当对于任何输入它们给出相同的输出。这是一个可测试的属性:生成一些输入,比较输出。如果它们不同,那么您就有了一个反例。
-- Test.QuickCheck.(===) requires (Eq b, Show b)
-- but you can use (==) if you prefer.
funEquality :: (Arbitrary a, Show a, Eq b, Show b) => Combine a b -> Combine a b -> Property
funEquality (Combine f) (Combine g) =
property $ \a -> f a === g a
Bool
导致“可判定相等”的类型
(==) :: X -> X -> Bool
替换为
Property
在我们可以称之为“可测试的平等”
funEquality :: X -> X -> Property
.
property
其实没必要用并转换函数
a -> Property
(或
a -> Bool
如果您使用
(==)
)到
Property
,但这样的类型看起来更整洁。
Eq
。 .
type CombineAssoc a b = Combine a b -> Combine a b -> Combine a b -> Property
combineAssoc :: (Arbitrary a, Show a, Eq b, Show b, Semigroup b) => CombineAssoc a b
combineAssoc f g h = ((f <> g) <> h) `funEquality` (f <> (g <> h))
Show
Combine
的实例. QuickCheck 提供了一个包装器
Fun
生成和显示函数作为反例。
main = quickCheck $ \(Fn f) (Fn g) (Fn h) ->
(combineAssoc :: CombineAssoc Int Bool) (Combine f) (Combine g) (Combine h)
关于haskell - 如何测试这种数据类型的半群定律?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41350192/
一年多以来,我一直在阅读C++并用它编写小程序。最近我遇到了三巨头法则。我从来不知道这条法律。 无意中,我在这里找到了它:Rule of Three . 我可以知道 C++ 中的任何其他此类定律吗?
根据 Control.Arrow 文档,对于许多 monads(那些 >>= 操作是严格的)instance MonadFix m => ArrowLoop (Kleisli m)不满足 loop (
通常在 Haskell 中我们定义 Monad s 表示为 return和>>= 。有时分解也方便>>=进入fmap和join 。 Monad一旦您习惯了这两种公式的定律,它们是众所周知的并且相当直观
我正在阅读 James Iry's blog post在 Scala 中的 Monads 上。我在第三部分,我对他关于单元的单子(monad)第二定律的描述感到困惑。特别是这种说法: unit(x)
我见过提到 IO不满足单子(monad)定律,但我没有找到一个简单的例子来说明这一点。有人知道一个例子吗?谢谢。 编辑:如ertes和 n.m.指出,使用 seq有点非法,因为它可以使任何 monad
齐普夫定律是许多现实生活中的一种模式,齐普夫定律最常见的情况是在文本段落中,其中最常用的单词的数量是第二个最常用单词的两倍。 我一直在学习 Python 中的字典,并尝试自己做这件事,但对它们的一些方
这可能是一个幼稚的问题,但是 RSpec 的测试 DSL 是否违反了 Demeter 法则? 这是来自 http://rspec.info 的 RSpec DSL 示例: bowling.score.
Snell's law指出入射角和折射角的正弦比等于给定 Material 折射率比的倒数: 我想实现一个简单的程序来可视化法律。自 , 和 是已知的,这是我计算的方式 : theta2 = asin
嘿,我正在开发一个文本生成器,它应该可以生成数百万种不同的文本。为了使每篇文章的内容更真实,我使用了 Zipf 定律它运行良好,单词分布正确。 但是下面的 next() 函数执行得非常慢,因为我想生成
我正在使用一个工具来自动生成按层次结构组织的 XML 文件的类表示形式。 XML 文件是我的应用程序需要能够访问的设置文件(只读)。 如果我将顶级节点(例如,AppSettings)传递给需要访问一项
不使用形式推导,如何测试自定义的Monad实例是否遵循Monad定律? 最佳答案 FWIW,这是我最近编写的一组 QuickCheck 属性,用于测试从 F 代数派生的 Maybe 实现的 Monad
我是一名优秀的程序员,十分优秀!