-
-
Notifications
You must be signed in to change notification settings - Fork 52
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
unit_of is unsound #521
Comments
Just to have an MRE,
|
Also it appears only to affect the literal 0, not something like 1-1. Neither If you could reach into a |
On second thought is the underlying issue that The solution probably has to be applied at a lower level than elaboration — something at runtime that makes a unitful zero coercible to any other unit. Then at this stage |
This is the intended behavior. There is no way we could infer a polymorphic type for arbitrary expressions that evaluate to zero. Just think of something like Why do we want a polymorphic type for fn failing_sqrt<D: Dim>(x: D^2) -> D =
if x >= 0
then sqrt(x)
else error("Argument to failing_sqrt is negative") This generic function can be used with dimensionful arguments like On the other hand, even if I understand that this is a strange concept, but I am quite confident that this is the right behavior. It's very similar to how the empty list
Yes. I think the "unit attached to zero" thing might work. I need to think about this. The ideal solution for me would be to get rid of |
After tinkering around a bit, I agree with you that polymorphic (literal) zero is 100% the correct decision. (I tried removing it and was met with immediate pain.) I guess my question is where it should be applied: permanently during elaboration, or temporarily everywhere it's needed to make the types check? With the second option, 0 would be polymorphic during type checking and evaluation, but would live in the VM itself as a concrete unitful value. This seems like it may be overkill though if all it does is enable a sound |
This is a hotfix for #521. It doesn't solve the underlying issue, but it prevents Numbat from crashing, throwing a runtime error instead.
This is a hotfix for #521. It doesn't solve the underlying issue, but it prevents Numbat from crashing, throwing a runtime error instead.
I think the issue goes deeper than this. The mathematical equivalence Even if we change the runtime representation of I quickly thought about run-time type information or something similar. If we have a "problematic" function like
in the program, we do infer a type of I think it's useful to think about alternatives to
But the fully polymorphic version somehow needs to create a
One simple (and not totally unreasonable) way to fix this would be to ask the user to provide
This would also get rid of the "magic" 1e-10 value that can never be correct for all applications. But we need to look at other instances of For now, I'll merge a hotfix that patches the crash: #615 |
The
unit_of
functionwhich returns e.g.
1 km/h
for an input of30 km/h
can lead to soundness bugs when the argument is zero.Consider this case, for example:
The type checker doesn't complain since the signature of
unit_of
promises to return aLength
for this case. Butunit_of(0) == 1
, which leads to the subsequent conversion error.It can even lead to crashes, like in this example:
The text was updated successfully, but these errors were encountered: