-
-
Notifications
You must be signed in to change notification settings - Fork 40
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
Functions: full type inference #29
Comments
As so often, after understanding and thinking about the problem, I'm finally able to properly search for existing work. I found this: Wand, Mitchell, and Patrick O'Keefe. "Automatic Dimensional Inference." Computational Logic-Essays in Honor of Alan Robinson. 1991 (https://www.ccis.northeastern.edu/home/wand/papers/dimensions.ps) and this: Programming Languages and Dimensions, Andrew John Kennedy, University of Cambridge, Computer Laboratory, 1996 (https://www.cl.cam.ac.uk/techreports/UCAM-CL-TR-391.pdf) |
Recursion can lead to more interesting constraints for the return type, e.g.
|
If we don't fully implement this at the beginning, we could think about doing the following — just to make it possibly for users to write simple function definitions without having to add type annotations:
This would allow a user to define e.g. Edit: This is implemented now |
This is now fully supported. |
It would be very cool if we could infer function parameter types and return types. For example, given hypothetical code like
we could easily infer from
d + 1 meter
thatd: Length
, and fromg(t)
thatt: Time
. Furthermore, we could compute the return type to beLength / Speed =Time
, leading to an inferred type ofThis also relates to generics (#25), as some (most?) functions would probably have inferred generic types. For example, in the absence of any annotations,
would have an inferred type of
Possible constraints
[expr1] == [expr2]
constraint. Similarly,expr1 -> expr2
also forcesexpr1
to have the same dimension asexpr2
.[exprK] == [parameter k of f]
. A simple example would be something liked
andalpha
.[expr2] == 1
. In general, we can not assume thatexpr1
is also a scalar (meter^2
is perfectly fine, for example). So there is also no constraint forexpr1
.Formalization
Given a (partially annotated) function definition like one of these (
Cx
is a concrete type):We start by filling in blanks with free type variables
T0, T1, …
andT_ret
:Next, we walk the AST of the right hand side expression and record all constraints that we discover (see above). For example, given a declaration like
f(x0, x1, x2) = sqrt((x0 · x2² + second) · second / x1²) -> meter
, we would identify:T0 · T2^2 == Time
(fromx0 · x2² + second
)((T0 · T2^2) · Time / T1²)^(1/2) == T0^(1/2) · T1 · T2 · Time^(1/2) == Length
(from the->
conversion operator)These constraints would then have to be converted to a system of linear equations on the exponents.
where
exponents_i
and the right hand sides would be vectors with one rational number entry for each of the D physical dimensions. In total, this is a set of N_constraints · D equations, where the left-hand side will be the same regardless of the physical dimension.Examples
Unique solution
Example 1
2^z
, we infer[z]: 1
.y + 2 * second
, we infer[y]: Time
g(sqrt(x)/y)
, we infersqrt(x)/y: Length
, i.e.sqrt(x): Length · Time
orx: Length² · Time²
.Length · Time
Example 2
should be inferred as
Example 3
(from https://www.cl.cam.ac.uk/techreports/UCAM-CL-TR-391.pdf)
should ideally be inferred as
Underdetermined
For a function definition like
we could infer one of those (equivalent) types:
Overdetermined
A function like
should lead to a type check error.
The text was updated successfully, but these errors were encountered: