-
Notifications
You must be signed in to change notification settings - Fork 6
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
Unlawful Num instance #6
Comments
Hi, If this doesn't work as expected let me know... The docs say that bit-vectors are in most cases interpreted as unsigned integers, and that's why I simply zero-extended. If you want to perform signed arithmetic, then you need to make sure that you operate bit-vectors of the same size. Size "polymorphism" assumes unsigned arithmetic. At the time I implemented I want to revisit this decision but it will be a "major" change in the library and it will break backwards compatibility, so I want to think how much this would affect existing users. I'm not even sure whether that would be enough or whether there will still be some unsatisfied laws. Ultimately one could use sized-types... but then I guess that part of the design space is already covered by bv-sized. If this is a big issue for you and you are willing to help that would be very appreciated. I have little spare time nowadays. |
Hi again, Sorry I didn't reply to the associativity of multiplication example, although the justification is kind of the same. I assume you know that your examples behave like that because So, more generally, if you want arithmetic laws to apply then the library requires you to be explicit about the sizes of bit-vectors in most cases. The automatic zero-extension feaure is there for convenience (at least I thought it was convenient like for bit-masks...). At the time I thought it was a better idea than making virtually all the functions partial and raise run-time errors. Unfortunately I cannot fix associativity of multiplication. Perhaps I should consider deprecating the Let me know if you have suggestions to improve the library (that can be implemented without sized-types). |
I am not an active user of IMHO the best solution is to check that both arguments of I can possibly prepare a PR, if you'd have time to review and merge it. |
OK, I will just think on this. Allowing operations between bit-vectors of different sizes is convenient. I am aware that this breaks arithmetic laws, although I'm not sure whether I should sacrifice that convenience for the sake of theoretical correctness. I would even argue that in Sign-extending would fix some of these cases (but not all). Perhaps I could just offer a flag to turn this off the same way that you can turn off bounds checks. In any case I should probably clarify the docs. Thanks for the feedback! |
I cannot really agree that non-associativity is just a question of theoretical correctness. It means that users have to keep it in mind all the time, when using Back to my original question, it would be still nice to clarify in |
If you look at the QuickCheck properties it should be clear that the intended arithmetic is that of bit-vectors of size "Automagic" zero-extension to resolve size mismatching was supposed to be a mere convenience that, back in 2012-ish, I judged better than just making everything a partial function. If I remember correctly, at the time there were no sized types in Haskell. Not sure whether to change the code or how exactly but, sure, I should definitely clarify it in the |
README.md
says thatbut I struggle to grasp what kind of arithmetic it is. It seems that neither
(+)
nor(*)
are associative:The text was updated successfully, but these errors were encountered: