Expose free variable information for an AST
This is some mixture of a feature request (for exactprinting renamed ASTs) and also a general inquiry into whether my current approach is reasonable.
I am currently adding extract selection functionality for HLS. This JetBrains wiki page describes the functionality. The idea is that the user selects a range of text, and if that range constitutes a valid expression, then we provide a suggestion that extracts a new definition with this expression as the RHS, with the newly-free variables as arguments to the new function:
import X (x)
foo y = x + y
-- Extract the rhs of foo
import X (x)
newDefinition y = x + y
foo y = newDefinition y
When implementing this feature using GHC's API, we do not currently have access to the free variables of an expression, which is used to decide on which variables need to be captured in the extracted expression.
Note also that we require the variables that are not captured by the enclosing top-level definition, but would want to consider imported definitions as not-free in the new expression.
Currently, I am implementing this feature with a surprisingly-high-fidelity heuristic which you can read about in the HLS MR. However, I think the following 3 step process is what I am considering as an improvement:
- Acquire the AST output of the Renamer phase
- Find those variables in the extracted expression that are not bound in the same expression, using the renamed references that result from the renamer phase. We use the renamer output to ignore imported definitions and to find the location of the binding site for each variable name (RdrName?).
- Extract the expression etc.
- Pretty-print the expression
This would require exactprinting of a renamed AST, which @alanz is already working on I believe? But I'm curious if anyone has any other input besides!