Skip to content
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

Annotate AST nodes with inferred types #990

Closed
byorgey opened this issue Jan 9, 2023 · 0 comments · Fixed by #991
Closed

Annotate AST nodes with inferred types #990

byorgey opened this issue Jan 9, 2023 · 0 comments · Fixed by #991
Assignees
Labels
C-Project A larger project, more suitable for experienced contributors. L-Type inference The process of inferring the type of a Swarm expression. S-Moderate The fix or feature would substantially improve user experience. Z-Feature A new feature to be added to the game. Z-Refactoring This issue is about restructuring the code without changing the behaviour to improve code quality.

Comments

@byorgey
Copy link
Member

byorgey commented Jan 9, 2023

Is your feature request related to a problem? Please describe.
While performing type inference, we recurse through the AST, inferring a type for each node. Currently we throw away all this intermediate information and return only the top-level inferred type for an entire AST. However, there are many situations where having this intermediate type information would be helpful, for example, to help with things like LSP hover hints, or to apply type-specific elaboration.

Describe the solution you'd like
Rather than simply returning a type, the type inference engine should return a new AST annotated with a type at every node. We already have a SrcLoc at every node via the Syntax wrapper type, so this would just require adding a new type field to Syntax.

I have started on implementing this in the https://github.com/swarm-game/swarm/tree/annotate-ast branch. Rewriting the elaboration code is proving tricky but if I bang my head against it a bit more I think I will get it.

@byorgey byorgey added Z-Feature A new feature to be added to the game. Z-Refactoring This issue is about restructuring the code without changing the behaviour to improve code quality. C-Project A larger project, more suitable for experienced contributors. S-Moderate The fix or feature would substantially improve user experience. L-Type inference The process of inferring the type of a Swarm expression. labels Jan 9, 2023
@byorgey byorgey self-assigned this Jan 9, 2023
@mergify mergify bot closed this as completed in #991 Jan 25, 2023
mergify bot pushed a commit that referenced this issue Jan 25, 2023
- Closes #990

Values of type `Syntax` are as before: parsed syntax, with each node annotated with `SrcLoc`.

Values of type `Syntax' Polytype`, however, have each node annotated with *both* a `SrcLoc` *and* a `Polytype`.  (`Syntax` is really just a synonym for `Syntax' ()`.)

Type inference takes a `Syntax` and outputs a `TModule`, which now contains a `Syntax' Polytype`, in other words, a new version of the AST where every node has been annotated with the inferred type of the subterm rooted there.

---

Why is this useful?
1. It will enable us to do type-specific elaboration/rewriting.  For example I think this will allow us to solve #681 , because we only want to apply a rewrite to variables with a command type.
2. It makes type information for any specific subterm easily available.  For example I hope we will be able to use this to enhance the `OnHover` LSP handler, e.g. to show the type of the term under the mouse.

I imagine the code changes might look kind of intimidating but I don't think it's really that bad once you understand what is going on, so I'm happy to answer any questions or explain anything.
mergify bot pushed a commit that referenced this issue Jan 25, 2023
* unfold function application and tuples
* add type signatures to syntax using #990 
* show hovered variable definition name using #993
* fix indenting for multiline text

- closes #985
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-Project A larger project, more suitable for experienced contributors. L-Type inference The process of inferring the type of a Swarm expression. S-Moderate The fix or feature would substantially improve user experience. Z-Feature A new feature to be added to the game. Z-Refactoring This issue is about restructuring the code without changing the behaviour to improve code quality.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant