-
-
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
Full type inference, list datatype #443
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This was referenced Jun 4, 2024
Closed
hamirmahal
reviewed
Jun 4, 2024
Comment on lines
+7
to
+8
if a == 0 | ||
then if b == 0 then if c == 0 then "infinitely many solutions" else "no solution" else "x = {-c / b}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
This was referenced Jun 4, 2024
Open
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds proper type inference to Numbat. It also makes the type system more powerful (with respect to generics). A few highlights:
List<T>
for all typesT
(see Add a list datatype #261). Together with proper generics handling (see below), this should allow us to implement Add sums and products #660
,NaN
andinf
(see Zero, NaN, inf should be polymorphic #37)Time -> Length
, then you will get aVelocity
as a result fromdiff
.Other changes:
Never
/!
data type has been removed. Instead, theerror
function now has typeforall A. String -> A
Open challenges:
kg m / s^2 + kg m^2
where we will now get a constraint solver error instead of our nice incompatible-dimensions error.struct
s. Field access is currently broken when accessing structs in functions. This could be fixed by requiring annotations for structs. What type shouldfn f(x) = x.foo
have otherwise? The only reasonable option would be to go back to structurally typed records and do something similar to Haskell (or probably better, similar to PureScript).DateTime
arithmetic is currently broken. The problem is with the overloaded+
/-
operators. If we have something likex - y
with yet-unknown types, we would like to say: either,x
andy
can be of the same dimension type ORx
could be aDateTime
andy
aTime
or aDateTime
. This kind of either-or constraint can not be formulated currently. The way this is solved in other Hindley-Milner languages is through type classes / trait bounds. We would need to add (multi-parameter?) type classes to model things likeforall D: Dim. Sub(D, D)
,Sub(DateTime, DateTime)
andSub(DateTime, Time)
.Dim
constraints and add a new syntax for them, e.g.fn sqrt<D: Dim>(x: D^2) -> D = …
.To do:
error("hi") + true
should be a type check errorfn test(f, xs) = f(head(xs))
forall D. D
orforall D: Dim. D
1 m + 0 * 1 s
work?==
/!=
operator.Dim
constraints (and possibly other operators as well):fn f(x)=x+true
,fn f(x,y)=x+y
fn f(x, y) = x^y
, fixtodo!(…)
fn f(x) = x + true
readable_type
s (see TODOs in bytecode_interpreter and type_scheme.rs)info
deploy.sh
Follow ups:
log
ging dependencies?try_trivial_resolution
can easily analyze the constraint structure further to discard constraints likeList<T0> ~ Bool
, which will yield better error messages in returnEq
type class.true(2)
x + y
, we needtype(x) ~ type(y)
andIsDim(type(x))
, but we could potentially save theIsDim(type(y))
. On the other hand, that would result in worse error messages.mean
,stddev
, etc in Numbatquadratic_equation
aList<B/A>
return type.fn foo<D1, D2>(x: D1*D2) = 1
should yield a "missing Dim bound" constraint, because the type parameters are used type annotation dimension expressionfn f<A: Dim>(x: A) -> A = 2x; f(true)
unit magic = 0; 1 m -> magic
), allow them in variable definitionsfunction_types_in_argument_position
testlet v = 1m/s
would previously printlet v: Velocity = …
. Similarly, it would be cool if we would annotatev
infn f(v)=v+1 knot
with aVelocity
typeCool stuff:
numbat/modules/core/lists.nbt
.map
orfold
. Type something likediff
: Check outexamples/numerical_diff.nbt
on this branchfn f(x) = m² / x + s² * x
orfn f(x, y) = x + y^2
and see the correct type being inferred.closes #29
closes #261
closes #324
closes #37
closes #166