Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seems that quickCheck is telling me that False is not equal to False?

Tags:

haskell

Been trying to write my own Prelude, and to write QuickCheck properties along with it. Writing Prelude is going well, but in doing the QuickCheck I have managed to get blocked early. Here are snippets of the 2 files I have:

-- MyLude.hs

module MyLude where

import Prelude (Bool(..))

(&&) :: Bool -> Bool -> Bool
(&&) True a = a
(&&) _ _ = False

-- MyLudeTest.hs

module MyLudeTest where

import qualified MyLude as P

prop_test_and a b = a && b == a P.&& b

Then in ghci I run:

:load MyLudeTest.hs
:m +Test.QuickCheck
quickCheck prop_test_and

and get the following error:

*** Failed! Falsifiable (after 1 test):
False
False

What confuses me is that I have implemented (||) and implemented a quickcheck property for it that does pretty much the same thing the prop_test_and does, and that seems to have no problems. What am I missing?

like image 213
Daniel Avatar asked Nov 18 '25 14:11

Daniel


2 Answers

This is just operator precedence at work. Your property is parsed as:

prop_test_and a b = a && (b == a) P.&& b
like image 128
hammar Avatar answered Nov 20 '25 09:11

hammar


Precedence!

Prelude Test.QuickCheck> (False && False) == myand False False
True
Prelude Test.QuickCheck> False && False == myand False False
False

You're missing parens around your operators. Remember (&&) binds weaker (3) than (==) (4)

like image 31
Don Stewart Avatar answered Nov 20 '25 09:11

Don Stewart



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!