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

Specification of div seems to be incorrect #2285

Open
facundominguez opened this issue May 21, 2024 · 3 comments
Open

Specification of div seems to be incorrect #2285

facundominguez opened this issue May 21, 2024 · 3 comments

Comments

@facundominguez
Copy link
Collaborator

The following example doesn't go well

iterateTo0 :: Int -> Int
iterateTo0 0 = 0
iterateTo0 n = iterateTo0 (div n 2)

main :: IO ()
main = print $ iterateTo0 (-1)

It passes verification, but then fails to terminate.

I think the problem might be that the specification of div uses real division where integer division is expected.

@nikivazou
Copy link
Member

This specification was written for div on Reals this is why the filename has Real. If I recall correctly, there was a different one for the integer division.

@facundominguez
Copy link
Collaborator Author

div comes from GHC.Real.

ghci> :i div
type Integral :: * -> Constraint
class (Real a, Enum a) => Integral a where
  ...
  div :: a -> a -> a
  ...
  	-- Defined in ‘GHC.Real’
infixl 7 `div`

but it seems to be instantiated only with types that need integer division.

ghci> :i GHC.Real.Integral
type Integral :: * -> Constraint
class (Real a, Enum a) => Integral a where
  ...
  div :: a -> a -> a
  ...
  {-# MINIMAL quotRem, toInteger #-}
  	-- Defined in ‘GHC.Real’
instance Integral Int -- Defined in ‘GHC.Real’
instance Integral Integer -- Defined in ‘GHC.Real’
instance Integral Word -- Defined in ‘GHC.Real’

@ranjitjhala
Copy link
Member

Good catch @facundominguez !!! Specifically the clause on line 27

((1 < y) => v < x ) &&

Is wrong: the output is only < x if 0 < x

We should have quickchecked these specs!

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

No branches or pull requests

3 participants