Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix weird behaviour when dealing with datatypes following an invariant #2

Open
rudymatela opened this issue Sep 26, 2017 · 1 comment

Comments

@rudymatela
Copy link
Owner

rudymatela commented Sep 26, 2017

{-# LANGUAGE TemplateHaskell #-}
import Test.Extrapolate

newtype Nat = Nat Int deriving (Eq, Show, Ord)

deriveListable ''Nat
deriveGeneralizable ''Nat

-- incorrect property:
prop_lengthTake :: Nat -> [Int] -> Bool
prop_lengthTake (Nat n) xs = length (take n xs) == n

main :: IO ()
main = do
  check prop_lengthTake

The output for the above program is:

*** Failed! Falsifiable (after 3 tests):
(Nat 1) []

Conditional Generalization:
n _  when  n < (Nat 0)

The conditional generalization is correct. However it is only correct for data values that do not follow the data invariant of Nat.

Thanks to @ColinRunciman for discovering this.

@rudymatela
Copy link
Owner Author

Maybe this could be solved by adding yet another function to the Generalizable typeclass?

class Generalizable a where
  ...
  valid :: a -> Bool
  valid = True

instance Generalizable Nat where
  ...
  valid (Nat n) = n >= 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant