Weeknotes 2026-W06 › Project Pterodactyl: incremental architecture [01K7]
In my spare time I have been continuing to work out the architecture for incremental query-based elaboration on the CAS model. One bit of received wisdom in this area is the “green tree / red tree” distinction, inherited from Roslyn. Green trees are concrete syntax trees that carry no positioning information (except textual length of nodes); red trees are an ephemeral structure that is layered on top of the green tree, and carries absolute positions as well as other useful things. Red trees are easily created and destroyed, and analyses (like type checking, etc.) occur by traversing red trees; localised diagnostics are easily emitted by using the absolute positioning information stored in the red tree.
The design above is, however, a bit of a disaster for incremental analysis and live responsiveness to user actions in IDEs, as Cameron Zwarich pointed out on Mastodon in the Autumn. I didn’t realise how bad an idea red trees are until I got to the point of trying to the design the compiler queries for LSP diagnostics and realised that adding one line of whitespace at the top of the file would result in cache misses for literally every textually-subsequent action. It is a bit odd that anyone ever thought this was a good idea in the context of IDEs…
Anyway, the solution is for the red trees to not carry absolute positioning information, but instead to be positioned relative to some root. One way to express the relative positioning is by means of a relative textual offset; my Part II student Jeremy Chen, who has built an extremely cool incremental elaborator for Martin-Löf type theory in Lean using his own Lean version of Shake, suggested to me that another option would be a path through the tree. I think either of these could be good, though I will probably do the relative textual offsets.
In either case, you get the best of both worlds: expensive elaboration tasks emit relative diagnostics, and then a separate query can request these diagnostics and appropriately localise them. The cache for the outer query is constantly invalidated, but its expensive subquery is stably cached because it is position-independent.