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

'shrink'ing (BitVector 1) gives 'Exception: divide by zero' #153

Closed
samgd opened this issue Jun 30, 2016 · 1 comment
Closed

'shrink'ing (BitVector 1) gives 'Exception: divide by zero' #153

samgd opened this issue Jun 30, 2016 · 1 comment

Comments

@samgd
Copy link

samgd commented Jun 30, 2016

The Arbitrary instance for BitVector is defined using shrinkIntegral, shown below.

-- | Shrink an integral number.
shrinkIntegral :: Integral a => a -> [a]
shrinkIntegral x =
  nub $
  [ -x
  | x < 0, -x > x
  ] ++
  [ x'
  | x' <- takeWhile (<< x) (0:[ x - i | i <- tail (iterate (`quot` 2) x) ])
  ]
 where
   -- a << b is "morally" abs a < abs b, but taking care of overflow.
   a << b = case (a >= 0, b >= 0) of
            (True,  True)  -> a < b
            (False, False) -> a > b
            (True,  False) -> a + b < 0
            (False, True)  -> a + b > 0

This definition causes a *** Exception: divide by zero as x 'quot' 2 == x 'quot' 0 because (2 :: BitVector 1) == 0.

@christiaanb
Copy link
Member

Thanks for the report. I really should have given the arbitrary instances more thought. I'm on holiday right now, but will be back on the 5th of July.

christiaanb pushed a commit that referenced this issue Sep 6, 2018
Some exports of `Clash.Prelude.Safe` were, rather dubiously, not also being exported by `Clash.Prelude`.
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

2 participants