-
-
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
Struct field access fails if expression type is not yet concrete #459
Comments
Thank you for testing my implementation and reporting this! This is something I completely missed. It might be a bit harder to fix. It's not related to lists. Here's an even simpler example that also fails: fn id(x) = x
struct F { x: Scalar }
id(F { x: 1 }).x The problem is with the polymorphic function call (
Hm. Are there general rules for when this is okay? We are currently solving constraints (and then zonking unification variables) only after we have looked at (and elaborated) the whole statement. We are not doing classical Hindley-Milner Algorithm W, where constraints are solved immediately when they are discovered. Instead, I tried to implement a strategy called "constraint typing", where constraints are first collected and only solved after looking at the full statement. I'm not a type theory expert, so I might have made some mistakes. But it is my understanding that this formulation is more suited (or even needed) for Numbat because (1) we need to support dimension types and (2) we want to support type class constraints. From my understanding of the literature, the constraint typing approach is not strictly needed for (1), but Algorithm W needs some rather complex adaptations to support dimension types directly, while the formulation is more natural using constraint typing. And type class constraints (2), like the Something that doesn't fit well into the current type system are the nominally typed records that you implemented (because I asked you to). Looking back at this, I was recently thinking if structurally typed records would have been the right call after all 🙃. I think they fit better into functional programming languages, and they can fully support type inference (for example: PureScript has a powerful record system using row polymorphism). But nominally type structs still feel a bit more natural to me. And I thought that we could get away with this by simply requiring type annotations for usage of structs. So something like Maybe the solution would be an additional constraint? I quickly thought about this when I encountered a similar thing that you reported here... but then I found another solution for that case (whenever we have closed types, i.e. without any unification variables, we now try to pass them from the inside to the outside as far as possible. This didn't just solve my problem with struct field accesses, but also leads to much better error messages. Otherwise, we would only see constraint solver errors, which are horrible). The additional constraint could be something like When we encounter a field access
and return a type of
Whenever the first argument in the If someone knows more about type systems then I do, I would really appreciate some help 😄. But right now, I'm thinking this might be a way out for this particular problem. |
Oh, haha: https://ghc.gitlab.haskell.org/ghc/doc/users_guide/exts/hasfield.html Need to read it in detail, but sounds very much related to the discussion here |
HasField constraints are a good solution and I've used them often in haskell. But there's some considerations I can think of:
Rust I think is a good example to look into here, there is no exposed HasField constraint, yet a field access always has to resolve a concrete type. |
#461 implements a simple version of HasField constraints.
Right. Since we don't have other types where field access would be useful (except for some of the builtin types like
👍
I think this is what we have now with #461 as well. This discussion here also made me realize that there is a similar problem with one another builtin type ( |
I was playing around with the new type system and came across this issue:
My guess is there just needs to be a zonking step performed before looking up the struct type for field accesses?
The text was updated successfully, but these errors were encountered: