-
Notifications
You must be signed in to change notification settings - Fork 442
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
Add a type system to Bend #615
Comments
Kind2 used to have function type declarations within their definitions, so something like this:
Considering gradual typing, I think this also works better than separating it since we could give types only to the variables we want:
Regarding generics, Kind2 does it with the diamond operator following the name of the function being defined. For example:
It's also similar to other existing languages, so I think it's nice. With Hindley-Milner, it would be possible to automatically "float" any undefined names to type quantifiers, but I personally think being explicit is better. I agree with what Kate said on the Discord channel - passing
I'll look into this more later, but if we use a HM style type checker, it could be good to do something similar to the existing literature. |
That doesn't allow for pattern matching. If we want to allow it, then it was
It's a good option |
For native hvm definitions we can give them a type with the imp syntax
|
@edusporto @imaqtkatt @LunaAmora I'm also considering using
This deviates from the python syntax but i think it'll get too hard to read functions that return functions
|
We could also use haskell style for fun syntax, with implicit type variables
|
Ok here's what I settled on. First version has types only on functions. Possibly will need to add annotation expressions later to mix typed and untyped code. For imp syntax
Fun syntax
Hvm definitions are always unchecked, but accept a type
I'm just not sure whether untyped functions should be type checked by default or not.
|
For type definitions, I'll go with
These are always checked, and generate the constructors
|
With implicit type variable in functions (eg. like haskell)
|
The needed builtin types to cover all the operations we have so far are
|
Wouldn't it be more like a Unit type? I'm not too sure about the equivalencies between interaction nets and linear logic, but since erasures can be created, it doesn't seem like their type is empty |
Null types are usually unit types in most languages, inhabited by the null value. I called it We could also substitute it by |
Compiling to kindc didn't work very well, the unification in kind fails for way too many cases that naturally pop up in Bend. I don't think it's reasonable to expect users to understand the limitation of kindc, since it's kind of unrelated to Bend. So instead I'm implementing an actual hindley-milner type system for Bend. My basic implementation (without any bend specifics) is here https://github.com/developedby/algorithm-w-rs. And later i'll port to Bend and integrate with this PR. |
I gave up on the idea of gradual typing and instead went for a very simple 100% static optional typing. Untyped values are not checked at runtime or at compile time, but can still be used together with other types in the expected way. |
Co-authored-by: imaqtkatt <[email protected]>
Is your feature request related to a problem? Please describe.
Bend already assumes by the semantics of ADTs and matches that users are writing programs within the contraints of some kind of polymorphic lambda calculus type system.
This is not enforced anywhere since bend is currently untyped. This allows very easily writing very clever programs that escape a simple type system, but in most cases just leads to writing incorrect programs. It'd be better if there was some way of enforcing it.
Having it be untyped like it currently is makes Bend basically unusable for anything not super tiny.
Describe the solution you'd like
Add optional hindler-milner typing to Bend. Types are created by adt defs.
Needs to have support for axiomatic type statements to allow unscoped variables, superpositions and native hvm definitions to be used with typed code.
Untyped functions have type
Any
, meaning that they are trusted, like in typescript or mypy.For Imp syntax, a syntax like typehints in python could work well
For Fun syntax, we can have separate function type declaration like in haskell for example.
We can compile to Kind to typecheck. That would need some trickery to deal with pairs, sups and unscopeds, but should be possible. Functions that have unscopeds need to be untyped or typed with an axiom.
This leaves a couple things still open:
What syntax to use for type variables. I like these two options
What syntax to use for unchecked type statements. For example we could use
::
instead of->
in Imp syntax for an unchecked type.I can't think of a use for it in expressions (except as from the desugaring of functions) but we could use
::
instead of:
there as well if this is wanted. Initially I think we shouldn't because it incentivizes users destroying types when a type error appears.What syntax to use for foralls. Functions with parametric types compile to lambdas with forall for types
Describe alternatives you've considered
Leaving typing to another language. That would go against our current direction of making Bend the language people use.
Using a dependent type system. While personally I'd love to see that, if values cross into the types then our type checker necessarily needs to know how to evaluate all the hvm quirks. Basically it would restrict the type checker to HVM itself or an implementation of HVM in the type checker. That would be annoying and could potentially be catastrophic if the type checker has the same limitations as HVM.
Using another kind of polymorphic lambda calculus, like system F or system F_omega. I think for users of imperative languages hindley milner is easier to understand and write.
The text was updated successfully, but these errors were encountered: