As I mentioned in my previous post (Reworking Pterodactyl’s program elaboration), I wish to handle the elaboration of program trees in Project Pterodactyl very similarly to how it is done in Figure 6 of the paper Elaborating Inductive Definitions. In this paper, the elaboration is described in a precise but informal fashion that is abstracted away from the difference between “internal” names and user-provided names. A lower-level specification of the elaboration must carefully distinguish these things.
This is actually non-trivial in the case of elaborating the left-hand sides to Epigram-style programs. In the versions of Epigram that I actually have the source code to, this was handled by first abstracting all the variables of the expected lhs (using internal names) and then walking through the user’s provided pattern to accumulate a relabelling, and then unbinding all local variables and rebinding them with the new names. This is very complicated and error-prone, and probably did not respect \eta -equality of core terms…
One suggestion of Conor Titania Mc Bride (which appears not to have been implemented in the version of Epigram I have) is to keep track of user-chosen names and their mapping to core terms. These associations are referred to dubbings. This is more ambitious than simply keeping track of the names assigned to each variable in the context, because in principle you can map a name to a complex term. This is actually important because the core programming label may include an argument in \eta -expanded form, which we should not be able to syntactically distinguish from a variable!
This is enough for mapping user-written syntax into the core language, but it poses some difficulties for going the other direction—which is needed for the display of goals, error messages, etc. In the vast majority of cases, the mapping will indeed relate names to actual variables, and here it is easy; but when you end up mapping a name to a complex term like \texttt {a} \mapsto \lambda x.\alpha (x), it is not easy to determine how to print the core name \alpha for the user, even though we know it ought to be printed as \texttt {a}. My quick-and-dirty solution for these edge cases is actually to walk through the naming context and check definitional equality until we find the appropriate name. This seems crazy, but it might not be too bad if we memoise it (keeping in mind that it is rare for a user to have more than 10-15 names in scope anyway).
This solves one part of the problem, but I’m still hoping we can find a better solution that feels less like “putting toothpaste back into the tube”. The other part of the problem is the actual elaboration of patterns, and what this means judgementally. I believe that the following scheme will work: pattern-forms correspond to either canonical, definitionally injective term constructors, or they are dot-patterns (literal terms that bind no pattern variables). The judgemental structure of constructing pattern-forms is kind of interesting: I think that it is a form of judgement like
\Gamma \vdash M : A \lhd \mathtt {p} \leadsto \mathcal {D}
in which M:A are core-language inputs, \mathtt {p} is a surface-language pattern subject, and \mathcal {D} is a sequence of induced dubbings. The rules for elaborating patterns then amount to deconstructing M in lockstep with \mathtt {p} whilst respecting the judgemental equality of M, until you reach one of the following base cases:
- In the case of a dot-pattern .(\mathtt {e}), we elaborate \mathtt {e} with type A and check that it is judgementally equal to M.
- In the case of an identifier \mathtt {a} that is not yet the subject of a dubbing, we emit a new dubbing \mathtt {a} \mapsto M.
- In the case of a wildcard pattern \mathtt {\_}, we simply stop.
It is a little bit annoying to have to implement this structure for all injective constructors in the language, when the structure is going to really be mirroring the existing rules for forming terms. There may be a way to consolidate this, but I am currently thinking that it may not be a good idea to do so. This is worth revisiting in the future.
By the way, this machinery will make it really easy to implement a match goal with ... tactic.