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

Better incompatible-dimension / conversion errors #104

Closed
5 of 7 tasks
sharkdp opened this issue Jul 11, 2023 · 0 comments · Fixed by #136
Closed
5 of 7 tasks

Better incompatible-dimension / conversion errors #104

sharkdp opened this issue Jul 11, 2023 · 0 comments · Fixed by #136
Assignees

Comments

@sharkdp
Copy link
Owner

sharkdp commented Jul 11, 2023

The idea of this ticket is to make "incompatible dimensions" errors better. Take this example:

>>> kg m / s^2 + kg m^2
error: while type checking
  ┌─ <input>:1:1
  │
1 │ kg m / s^2 + kg m^2
  │ ^^^^^^^^^^ - ^^^^^^ Length² × Mass
  │ │          │  
  │ │          incompatible dimensions in addition
  │ Length × Mass × Time⁻²
  │
  =  left hand side: Length × Mass × Time⁻²
    right hand side: Length² × Mass

There are several things that could be improved here.

Definitely included in this ticket:

  • In the upper part of the error message (and probably everywhere else where we print out types), format types like Length × Time⁻¹ as Length / Time, which is much easier to read. This can be done in analogy to what we do for pretty-printing units already.
  • In the lower part of the error message, line up the dimensions for easy comparison, i.e. sort them, add "¹" in places with no exponent, and add ⁰ for missing dimensions on either side:
      =  left hand side: Length¹ × Mass × Time⁻²
        right hand side: Length² × Mass × Time⁰
    
    (debatable whether or not we want a ¹ exponent for the Mass part)
  • In addition, show known dimension names, i.e. Speed for Length / Time. This can probably be done with a dictionary lookup over existing dimensions:
      =  left hand side: Length¹ × Mass × Time⁻²    (Force)
        right hand side: Length² × Mass × Time⁰     (MomentOfInertia)
    
    (MomentOfInertia is currently not defined, but we should add a few more dimensions in dimensions.nbt anyway). We would not do that for base dimensions like Length (i.e. whenever we would not add additional information). But we would show Area for Length².
  • Show suggestions on what to do. We can probably go crazy here but let's start with something simple: we could compute missing_type = type(lhs)/type(rhs) and add a suggestion to multiply the right-hand-side by missing_type. We would probably only do this, if missing_type is "not too complicated". And if missing_type contains "a lot of negative exponents", we could compute the inverse instead (type(rhs)/type(lhs)), and suggest to multiply the left-hand-side by the result. Or maybe it's better to always show both alternatives. For the example above, we could show something like: "Suggestion: multiply the left-hand-side by Time² × Length".

Stretch goals:

  • See if it's possible/sensible to avoid the code duplication between pretty-printing dimension expressions and "unit expressions". For example: the logic for formatting the unit meter × second⁻¹ as m/s and the logic for formatting the type Length × Time⁻¹ as Length / Time is exactly the same.
  • Colorize the "diff" in the types, e.g. highlight the Length and Time factors in the example above in red, and the Mass factor in green.
  • Think of more sophisticated heuristics for providing even better suggestions.
@sharkdp sharkdp changed the title Better conversion errors Better incompatible-dimension / conversion errors Jul 24, 2023
sharkdp pushed a commit that referenced this issue Aug 29, 2023
This changeset enhances "incompatible dimensions" errors by aligning
them for better readability, adding additional information and providing
suggestions on how to fix them.

closes #104
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

Successfully merging a pull request may close this issue.

2 participants