At OPLSS this year, I attended an awesome series of lectures
by Ulf Norrell, in which he showed us the basics of
dependently-typed wizardry in Agda. One of the coolest things about
Agda is its Emacs mode, through which you communicate with Agda and do
all kinds of crazy automatic code generation stuff. One of the things
it can do is to automatically create well-typed helper functions while
you’re in the middle of writing a program.
Inspired by this feature (and egged on by James), I wrote a much-hackier version for Coq
proofs: it examines the current proof state and generates a helper
lemma, generalizing over all of the context variables. For example, if
your context looks like
and you hit M-x extract-lemma <RET> in_not_empty <RET>, it’ll add
the following to your kill ring:
It does this in the absolute stupidest possible way: switching to
the goals buffer and doing a bunch of crazy text munging. You’ll will
probably want to clean up the output a bit, since it’ll generalize
over the whole context and you probably don’t actually need/want
that. Still, it’s a nice quick way to get a lemma of the correct
type.
A quick note on why this is useful: separating large Coq proofs into
self-contained, general lemmas is a good idea for the same reason that
separating large programs into small functions is a good idea: it aids
readability, maintainability, and durability. This bit of elisp should
hopefully encourage this style of proof development.