Higher-order pattern unification and…patterns [01KL]

In the implementation of dependently typed proof assistants and programming languages, “pattern unification” can mean two different things.

  1. Higher-order pattern unification is used to resolve implicit parameters, etc. This kind of pattern unification is always up to judgemental equality.
  2. A version of first-order unification is used to refine the motives of elimination problems. This kind of unification is (at times) involving constraints up to propositional equality.

Although the two notions of unification are almost entirely different, in an Epigram-like system they may be related to each other. In Epigram, the left-hand side of a definition is actually a core language term that serves as the index to a phantom type. Definitions actually proceed by matching a user-written left-hand-side against the actual core-language left-hand side, whose variables are (naturally) already bound. It seems particularly a nice idea to do this matching using full higher-order pattern unification, because we would be able to write programs like the following, as suggested by Conor Titania Mc Bride:

def uncurry (f : A -> B -> C) : (A*B -> C) where
  uncurry (\x y => h (x, y)) => h

The thing about this that is actually surprisingly difficult is not really specifically the pattern unification itself, but the interpretation and presentation of code with user-chosen names. In the above, it is easy enough for the elaborator to know that h should be read as \u => f u.1 u.2 where f is the internal name for the argument to uncurry, but it is much more difficult for the elaborator to know that it should print f in any diagnostics as \u => h (u.1, u.2). Prior to going off on my pattern unification tangent this week, I had written about some preliminary ideas for dealing with this last week.

I now want to think about how we might combine the two ideas to make this work properly. Maybe the user-written left-hand-side should be elaborated so that every unbound variable it mentions is emitted as a metavariable which is then solved by unification. When the metavariable ?0 is emitted corresponding to the user-pattern-variable h, we should record in the elaboration environment’s “dub table” that the term resulting from applying this meta to all local variables should be dubbed h.

Now, there is a real subtlety here. Consider the following example:

def swap (u : A*B) : B*A where
  swap (x,y) => (y,x)

In this case, we would have dubbed the core term u.1 as x and u.2 as y. Therefore, if the distiller/printer encounters u.1 it will print x, but unfortunately if it encounters just u it will not know what to print!

I am not sure how to deal with this problem. Some ideas involving \eta -laws present themselves, but I don’t see how to do this efficiently. I increasingly feel that I have taken a wrong turn… But it does occur to me that similar ideas to those of Nested Pattern Unification might also be used to boil down the dubbings to something useful.