-
Notifications
You must be signed in to change notification settings - Fork 52
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
Make type variables scoped #2168
Comments
This seems like a reasonable idea. Off the top of my head, to implement this I guess we should do something like:
In Haskell, of course (1) See also #2102 which wouldn't solve the issue but is relevant here. |
The problem I run into is the following limitation of records:
So I have to provide a type annotation, but I also can't write it: tydef RBTree k v = rec b. Unit + [c: Bool, k: k, v: v, l: b, r: b] end
tydef RBNode k v = rec n. [c: Bool, k: k, v: v, l: Unit + n, r: Unit + n] end
def balance : RBNode k v -> RBTree k v = \t.
let baseCase /* : RBTree k v */ = inr t in
let balanceRR /* : RBNode k v -> RBTree k v */ = \rn.
if true {baseCase} {undefined rn.x} // can't infer r.x
in undefined
end @byorgey the only workaround I found was passing the |
@byorgey btw. I think it is a very beginner-friendly extension that "does what I meant". 😄 |
That is a very good motivating use case!
I didn't know that! That makes me feel better, that there aren't lurking downsides we're not thinking of. |
So here's what I'm thinking in terms of a detailed design. Comments welcome!
So, for example, this will work:
The type signature on
then the Finally, if one wrote
then only A plan for actually implementing this:
|
@byorgey Thanks for the detailed breakdown. 👍 Adding type annotations for local functions is so rare that I would not even mind having to always write explicit forall. 🙂 |
Fair enough. Requiring explicit forall wouldn't really make the implementation any easier, though (actually it might be the opposite), so unless you know of good reasons that we should require explicit I have an implementation which is almost working, just a bug or two to track down yet... |
Is your feature request related to a problem? Please describe.
When writing a complex function, it is helpful to use local type annotations. But this breaks when I use local variables:
This is the same thing as Haskell without
ScopedTypeVariables
:Describe the solution you'd like
I would like to have scoped type variables by default.
Describe alternatives you've considered
It's possible that this would cause problems for later definitions if they were technically inside the scope. An alternative would be using a type annotation inside the body, like the Haskell example:
Additional context
I discovered this issue while writing #2161. The only workaround is to not specify the local definition signature or pass parameters with the local types.
The text was updated successfully, but these errors were encountered: