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

Type inference for math functions in ANSI standard #21

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

digikar99
Copy link
Contributor

@digikar99 digikar99 commented Nov 23, 2023

Adding type inference for math functions is going to be a long project. Here are the initial steps in case anyone wants to build upon them. I will probably push to this as and when need them in other projects.

I don't think this should be merged without adding a good number of tests because this can break other code relying on correct behavior of cl-form-types.

Reference:

  1. http://clhs.lisp.se/Body/c_number.htm
  2. http://clhs.lisp.se/Body/12_a.htm

@digikar99 digikar99 force-pushed the cl-numerics-type-inference branch from aab3325 to c62bcbe Compare November 23, 2023 16:18
@alex-gutev
Copy link
Owner

Thanks for the contribution. Writing comprehensive unit tests will be quite some work. If you can summarize the type deduction logic that you've implemented it will make it easier. I can sort of grasp it from the code. You've defined a set of rules for the math operators and whether they produce a value of the same type depending on whether the arguments are fixnums, integers, rationals, or floats, and then deducing the type using these rules.

@digikar99
Copy link
Contributor Author

Broadly, I'm trying to form a table out of certain common set of properties applicable to all (or most) numeric operations. I'm unsure if the properties I have currently considered are the good ones - so if someone comes up with a better set of properties, they can try implementing with those properties.

The main type deducer is the numeric-op-form-type function which considers two cases: either all the form types are (eql ...) or they are not. In the former case, the result type can be obtained by applying the operation to the arguments obtained from the eql type-specifiers. In the latter case, I hand over the work to numeric-result-type.

numeric-result-type attempts to use the properties of the numeric operation defined in the table to arrive at a reasonably precise type. It does so by considering progressively larger sets of operation-result-properties and the argument-types. This progression appears in the form of the several (when <prop> ...) clauses in numeric-result-type.

One of the central issue concerning how the argument types interact with the operation properties is the several numeric contagions: http://clhs.lisp.se/Body/12_aab.htm. It is probably this that can be the source of bugs.

@alex-gutev
Copy link
Owner

alex-gutev commented Dec 27, 2023

I added a few tests for +, -, *, /, 1+, and 1-. The tests cover the basic rules for fixnum, integer, rational and float arguments. I also added tests for when all the arguments are float but not single-float or double-float. In this case I believe the type float should be returned, provided closed-under-float-p is true for the operation, however the current implementation returns number and hence these tests fail. I haven't yet added tests for complex number arguments and the remaining rules.

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 this pull request may close these issues.

2 participants