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

Inconsistent generalizations for [Int] and [Bool] #4

Open
rudymatela opened this issue Sep 26, 2017 · 2 comments
Open

Inconsistent generalizations for [Int] and [Bool] #4

rudymatela opened this issue Sep 26, 2017 · 2 comments

Comments

@rudymatela
Copy link
Owner

import Test.Extrapolate

-- incorrect property:
prop_lengthZip :: [a] -> [a] -> Bool
prop_lengthZip xs ys = length (zip xs ys) == max (length xs) (length ys)

main :: IO ()
main = do
  check (prop_lengthZip :: [Int] -> [Int] -> Bool)
  putStrLn "\n"
  check (prop_lengthZip :: [()] -> [()] -> Bool)
  putStrLn "\n"
  check (prop_lengthZip :: [Bool] -> [Bool] -> Bool)

For the above program, the output is the following:

*** Failed! Falsifiable (after 2 tests):
[] [0]

Generalization:
xs (_:xs)

Conditional Generalization:
xs ys  when  length xs /= length ys



*** Failed! Falsifiable (after 2 tests):
[] [()]

Generalization:
us (_:us)



*** Failed! Falsifiable (after 2 tests):
[] [False]

Generalization:
ps (_:ps)

Extrapolate is able to produce a conditional generalization involving length of [Int] but not of [Bool] and [()]. Why?

@rudymatela
Copy link
Owner Author

@ColinRunciman, thanks for discovering this.

@rudymatela
Copy link
Owner Author

rudymatela commented Sep 26, 2017

I know why now. For the [Bool] case, both the arguments to the property are of [Bool] type. Only the background for the [Bool] and its "sub-type" Bool are included.

Since the background of Int insn't there. There is no (/=) :: Int -> Int -> Bool, hence the missing generalization. If we manually add it the problem is solved:

{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE NoMonomorphismRestriction #-} -- AAARRRRGGHHHH!!!!
import Test.Extrapolate

-- incorrect property:
prop_lengthZip :: [a] -> [a] -> Bool
prop_lengthZip xs ys = length (zip xs ys) == max (length xs) (length ys)

main :: IO ()
main = do
  check (prop_lengthZip :: [Int] -> [Int] -> Bool)
  putStrLn "\n"
  chk (prop_lengthZip :: [()] -> [()] -> Bool)
  putStrLn "\n"
  chk (prop_lengthZip :: [Bool] -> [Bool] -> Bool)
  where
  chk = check `withBackground` [constant "/=" ((/=) :: Int -> Int -> Bool)]

output:

*** Failed! Falsifiable (after 2 tests):
[] [0]

Generalization:
xs (_:xs)

Conditional Generalization:
xs ys  when  length xs /= length ys



*** Failed! Falsifiable (after 2 tests):
[] [()]

Generalization:
us (_:us)

Conditional Generalization:
us vs  when  length us /= length vs



*** Failed! Falsifiable (after 2 tests):
[] [False]

Generalization:
ps (_:ps)

Conditional Generalization:
ps qs  when  length ps /= length qs

Hah!

Maybe what I need to do for a permanent fix is to always include the background of Int and some relevant "core types"?

rudymatela added a commit that referenced this issue Oct 1, 2017
Reasons:

* Bool is in every property, all conditions are Bool;
* Int is in a lot of functions from the prelude.

A more definitive solution (for the Int case) would be to check the types of
background and include any types found there.  For example, the list
background has length by default of return type Int.

this partially solves #4
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