\documentclass[oribibl]{llncs}
\usepackage{amssymb}
\input{macros}

\begin{document}
\title{Higher-Order Pattern Unification and Proof Irrelevance}
\author{Jason Reed}
\institute{Carnegie Mellon University\\
Pittsburgh, PA 15213\\
\email{ jcreed@andrew.cmu.edu}
}

\maketitle
\begin{abstract}
Extending the type theory of a logical framework
with a proof irrelevance modality 
has several potential advantages, including the ability
to represent subset types and invariants for proof compression
(\cite{proof.irrelevance}).
Although the extended theory is well-behaved, 
it is not yet completely clear how to modify the implementation
of a logical framework to accommodate proof irrelevance.
The unification algorithm in Twelf (\cite{pfenning99cade}), in particular,
works by a process of constraint simplification 
that depends on the notion
of {\em pattern substitution}.
Adapting this algorithm to work with proof irrelevance requires
generalizing the definition of pattern. We propose
such a definition, guided by
work with proof irrelevance and strictness,
and make progress towards proving its correctness.
\end{abstract}
\section{Introduction}
\subsection{Higher-Order Pattern Unification}
Our starting point is an algorithm for higher-order pattern
unification using explicit substitutions due to
Dowek et al.
\cite{dowek.patterns}.
The difficulties of variable capture involved in higher-order
unification 
are avoided by a reduction \cite{dowek.expsubs} to first-order
equational
unification in a language with explicit substitutions \cite{expsubs}.
This first-order algorithm specializes
nicely for a decidable subset of unification problems, the 
{\em pattern fragment}. The strategy of constraint
simplification for solving general unification problems --- trying
to solve equations which are in the pattern fragment and
postponing constraints produced by those that aren't, which may
later be resolved by substitutions arising from other equations
--- though not complete, has been found to work well in practice.

Informally, {\em pattern} is a term where all the variables 
(by which we mean metavariables amenable to substitution) occur
above a sequence of distinct deBruijn indices, i.e. bound variables.
If all variables have atomic type (and we can easily transform problems to
have this property) then this means each variable must occur under
a substitution whose range is a set of distinct deBruijn indices,
a {\em pattern substitution}. Pattern substitutions are desirable
because they have one-sided
inverses, and so are injective. 
Equations of the form
$$X[\zeta] = b$$
therefore have at most one solution,
written $b[\zeta]^{-1}$.
\subsection{Proof Irrelevance}
Constants are naturally injective; if we have a constant $c : \tau_1 \to \cdots \to \tau_n \to \tau$ of $n$
arguments, the terms $c\ M_1\cdots M_n$ and $c\ M'_1\cdots M'_n$ are equal if only if $M_i = M'_i$
for all $1\le i \le n$. 
There are times when this is not the desired behavior. Sometimes we would like to be able make
certain arguments to a function `irrelevant' when it comes to deciding equality.
This can occur when some arguments to a function are meant to
be thought of as witnesses of provability rather than
pieces of data whose structure matters.
This phenomenon is {\em proof irrelevance}.
Generally, suppose we use the following signature in the dependent type system LF
(\cite{harper93jacm}):
\begin{eqnarray*}
&&t, u: \rtype\\
&&p : \Pi x \colon t. \rtype\\
&&c : \Pi x \colon t . \Pi y \colon (p\ x) . u\\
&&a : t\\
&&b, b' : p\ a.
\end{eqnarray*}
Intuitively, we have an object $a$ of type $t$, and some predicate $p$ on objects of type $t$.
The function $c$ takes two arguments: an argument $x$ of type $t$, and an argument $y$ of
type $p\ x$ (which might be thought of as a proof that $p$ holds of $x$) and returns an
object of type $u$. As the preceding discussion describes, $c\ a\ b \ne c\ a\ b'$ because
not all of $c$'s arguments are the same on both sides of the equation --- $b$ and $b'$ differ.
We would like to declare instead a $d : \Pi x \colon t . \Pi y \div (p\ x) . u$, where the $\div$
symbol is supposed to be a promise that the argument $y$ `doesn't matter' in the output of $d$.
We proceed to discuss some applications of proof irrelevance in more detail.

\def\slam{\lambda^{\ge 1}}
\def\rolam{\mathop{\sans olam}}
\def\roccurs{\mathop{\sans occurs}}
\subsection{Encoding Subset Types}
The example above is really a trivial case of the use of proof irrelevance
to obtain adequate encodings
of {\em subset types}, which can be useful when representing
programming languages and logics in a logical framework.
A subset type is a formal version of the common mathematical
set-formation syntax $\{x \in X \st \phi(x)\}$ for some predicate $\phi$ on $X$.
As a typical example of its usefulness, suppose we wish to represent a lambda calculus
which has a `relevant' binder $\slam$ which requires the variable it binds to occur at least
once. We could try doing this by beginning with the usual untyped lambda calculus
\begin{eqnarray*}
&&\rtm : \rtype\\
&&\rapp : \rtm \to (\rtm \to \rtm)\\
&&\rlam : (\rtm \to \rtm) \to \rtm
\end{eqnarray*}
and defining a predicate
$$\roccurs : (\rtm \to \rtm) \to \rtype$$
via the {\em logic program}
\begin{eqnarray*}
&&{\sans occurs\_var} : \roccurs (\lambda x . x)\\
&&{\sans occurs\_appl} : \roccurs (\lambda x.\rapp\ M_1\ M_2) \gets \roccurs (\lambda x.M_1) \\
&&{\sans occurs\_appr} : \roccurs (\lambda x.\rapp\ M_1\ M_2) \gets \roccurs (\lambda x.M_2) \\
&&{\sans occurs\_lam} : \roccurs (\lambda x.\rlam(M\ x)) \gets (\Pi y \cn \rtm . \roccurs (\lambda x . M\ x\ y)) 
\end{eqnarray*}
which captures the proposition that an open term uses its free variable, and
declaring a constant
$$\rolam : \Pi t : (\rtm \to \rtm) . \Pi p :\roccurs t . \rtm$$

This encoding, however, is not {\em adequate} in the sense that there are
generally multiple LF terms which represent a given object-language term.
This is because there are potentially many proofs that a variable occurs
bound --- in fact there will be one proof per occurrence of the variable.
Therefore we want to equate all terms using $\rolam$ that differ only
in which occurrence proof they use. That is, we should declare instead
$$\rolam : \Pi t : (\rtm \to \rtm) . \Pi p \div \roccurs t . \rtm$$
and the new encoding is adequate. Though the revised $\rolam$ still requires 
the existence of a proof
$p$ that $\roccurs t$ holds, it doesn't care {\em which} proof in a certain sense,
which is guaranteed by the type system.

\subsection{Subterm Omission in Proof-Carrying Code}
In a language like Java, some measure of safety of running code is insured by running
the code `in a sandbox,' inside a trusted virtual machine.
Proof-carrying code \cite{necula.pcc} is another technique which aims to
achieve the same (if not greater)
safety properties without sacrificing runtime efficiency to emulating a virtual
machine. The burden of a making a program safe falls instead on the author of
the program, the {\em code producer}. The recipient of a program, the {\em code consumer}
requires a {\em proof} that the program received satisfies some safety policy, and
so the code producer must send with the program a formal certificate of safety which can
be mechanically checked by the code consumer to actually prove that the program won't
violate the policy.

Unfortunately, these certificates can sometimes be large, even on the order of the size
of the size of the program being proved safe. Techniques for reducing proof size
are desirable. Although the problem of finding a proof for a given proposition is
typically undecidable, a proof may have many subproofs which could be easily and
efficiently reconstructed by the code consumer. For instance, as part of a large proof
that shows that a program always computes a certain mathematical function correctly,
it might be necessary to show some trivial fact, say $3 + 4 = 7$. Now the formal correctness
of this program depends on every last detail of the proof being correct, but there
is no need to send a proof of $3 + 4 = 7$ across the network --- there can simply be
a blank spot in the proof with an instruction saying, ``please check that in fact 3+4=7.''
The trade-off here is saving network bandwidth by perhaps spending more time reconstructing
proofs on the code consumer end.

In practice, proofs are frequently represented as terms in a type theory like LF, and checked
with a tool like Twelf. In this case, the idea of omitting subproofs really means omitting
subterms. The question to be addressed is, when can a subterm be safely omitted? Twelf already
has facilities for providing sufficient conditions for {\em termination} of
predicates considered as logic programs. When we can show using these tools that searching for
a proof of $P$ (which is what is meant by ``running the logic program $P$'') always terminates, and
we know a proof of $P$, then we can be sure that the code consumer can also find a proof of $P$.
What we do not know is that the code consumer will find the {\em same} proof. It may seem like
a desirable property of a type system that if we replace a subterm $S$ of a term $M$ with a different
subterm $S'$ of the same type, then $M$ is still well-typed, but dependent types systems do
not necessarily have this property exactly because of the dependence of types on terms.
For example, in the signature
\begin{eqnarray*}
&&a, z : \rtype\\
&&b : a \to \rtype\\
&&c : \Pi x\colon a. (b\ x) \\
&&d : \Pi x\colon a. (b\ x)\to z\\
&&k_1, k_2 : a\\
\end{eqnarray*}
we have the typing
$$\cdot \prov (d\ k_1)(c\ k_1) : z$$
but not
$$\cdot \prov (d\ k_1)(c\ k_2) : z$$
even though we have only changed one subterm of type $a$ to another.

If we introduce proof irrelevance, however, then it can be shown that replacing one
subterm under an irrelevant application with another preserves the whole term being well-typed.
Therefore, it is safe to omit a subproof of a large proof if we can show that
the subproof can be decidably recovered (i.e. if the predicate can be shown
to be terminating) and occurs under an irrelevant application.

\subsection{Extending Unification}
The task before us is to modify the pattern unification algorithm
to work in a language with proof irrelevance, and in particular to
find the right notion of pattern. A similar situation arises
with the notion of strict definitions \cite{pfenning.strictness}, which depends on the
definition of pattern spines. In the absence of irrelevance, 
a pattern spine is again a list of distinct bound variables.
In \cite{jcreed.st02} we found a modification of the definition
of pattern spine which satisfies the same critical lemma
as the original, again an injectivity property.
The changes made were adding an `irrelevant cons' to the
syntax of spines and requiring distinct bound variables at
`relevant' positions and allowing any term at `irrelevant' positions.
We imitate these changes in a definition of pattern substitution,
and observe that one-sided inverses again exist, for an appropriate notion
of equality of substitutions. The theory here is simply-typed throughout,
though we expect an extension to full dependent types to be reasonably
straightforward.

%Unification problems where the terms involved are all patterns
%are decidable.
%The task is to generalize patterns to proof irrelevance.
%We sort of already did this when trying to generalize strictness.
%It looks like the old definition should work still; injectivity
%holds for the right notion of equality.
%Unfortunately, attempting to phrase this notion of equality in terms
%of rewrite rules totally destroys any hope of normal forms.
%Maybe we need a new notion to replace normal forms?

%
\section{Syntax}
We define the $\lsi$-calculus, an
extension of the typed $\lambda\sigma$-calculus 
with term variables \cite{expsubs}
to include proof irrelevance in the sense of \cite{proof.irrelevance}.
For uniformity of syntax, we have a sort of modalities $\mu$
with which we annotate function types, function abstractions,
context and substitution
conses, and applications (written $\app$). The modality $\relsym$
refers to the usual (`relevant') notion from $\lambda$-calculus
and $\irrelsym$ gives the `irrelevant' version.
\begin{center}\begin{tabular}{lll}
Modalities&$\mu$&$::= \relsym \celse \irrelsym$\\
Types&$A,B$&$::=\alpha \celse A \to^\mu B$\\
Contexts&$\Gamma$&$::=\bullet \celse A \cdot^\mu \Gamma$\\
Terms&$a,b$&$::= \deb \celse {\cal X} \celse (a \app^\mu b) \celse \lambda^\mu_{A}a  \celse a[s]$\\
Substitutions&$s,t$&$::= \rid \celse \shifts \celse a \cdot^\mu s \celse s \circ t$\\
%(Spines&$S,T$&$::= \rnil \celse a ;^\mu S$)\\
\end{tabular}\end{center}
%
\section{Typing}
There are two typing judgments,
\begin{center}\begin{tabular}{llp{1.7in}}
Term Typing&$\Gamma \prov a :^\mu A$&
$a$ has type $A$ for modality $\mu$ in the context $\Gamma$.\\
Substitution Typing&$\Gamma \prov s : \Gamma'$&
$s$ is a substitution for the variables of $\Gamma'$ in $\Gamma$.
\end{tabular}\end{center}
Note that the term typing judgment is also annotated with a modality.
We abbreviate $:\rel$ by $:$ and $:\irrel$ by $\div$.

The meaning of the irrelevant typing judgment is given by 
the context operation $\dash^\oplus$, defined recursively by
$$\bullet^\oplus = \bullet$$
$$(A \cdot^\mu \Gamma)^\oplus = A \cdot\rel (\Gamma^\oplus)$$
and the rule
$$\begin{prooftree}
\Gamma^\oplus \prov a : A 
\justifies
\Gamma \prov a \div A
\end{prooftree}$$
$\Gamma^\oplus$ is the context $\Gamma$ with all irrelevant assumptions
promoted to `real' assumptions, so the typing rule allows us to conclude
that $a\div A$ if we can derive $a : A$ when we are allowed to use
even irrelevant assumptions.

The remaining typing rules are
$$\begin{prooftree}
\justifies
A\cdot\rel \Gamma \prov \deb : A
\end{prooftree}
\qquad
\begin{prooftree}
\justifies
A \cdot^\mu \Gamma \prov \shifts : \Gamma
\end{prooftree}$$
$$\begin{prooftree}
A \cdot^\mu \Gamma \prov b : B
\justifies
\Gamma \prov \lambda_A^\mu . b : A \to^\mu B
\end{prooftree}
\qquad
\begin{prooftree}
\Gamma \prov a : A\to^\mu B \qquad \Gamma \prov b :^\mu B
\justifies
\Gamma \prov (a \app^\mu b) : B
\end{prooftree}$$
$$\begin{prooftree}
\justifies
\Gamma \prov \rid : \Gamma
\end{prooftree}$$
$$\begin{prooftree}
\Gamma \prov s : \Gamma' \qquad \Gamma' \prov a :^\mu A
\justifies
\Gamma \prov a[s] :^\mu A
\end{prooftree}$$
$$\begin{prooftree}
\Gamma \prov s : \Gamma' \qquad \Gamma' \prov t : \Gamma''
\justifies
\Gamma \prov t \circ s : \Gamma''
\end{prooftree}$$
$$\begin{prooftree}
\Gamma \prov s : \Gamma' \qquad \Gamma \prov a :^\mu A
\justifies
\Gamma \prov a \cdot^\mu s : A \cdot^\mu\Gamma'
\end{prooftree}$$
$$\begin{prooftree}
\justifies
\Gamma_X \prov X : T_X
\end{prooftree}$$


\section{Reduction}
We list a set of reduction rules which are a straightforward adaptation
of those in $\lambda\sigma$, including those added in \cite{expsubs}
({\bf Id}, {\bf IdR}, {\bf VarShift}, {\bf Scons})
for critical pairs arising from the addition of term variables.

\begin{center}\begin{tabular}{llll}
{\bf Beta}&$(\lambda_A^\mu.a)b$&$\to$&$(a[b\cdot^\mu \rid])$\\
{\bf Eta}&$\lambda_A^\mu.(a \app^\mu \deb)$&$\to$&$b$ if $a =_\sigma b[\shifts]$\\
\\
{\bf App}&$(a\app^\mu b)[s]$&$\to$&$(a[s]\app^\mu b[s])$\\
{\bf VarCons}&$\deb[a \cdot^\mu s]$&$\to$&$a$\\
{\bf Id}&$a[\rid]$&$\to$&$a$\\
{\bf Abs}&$(\lambda^\mu_A.a)[s]$&$\to$&$\lambda^\mu_A.(a[\deb \cdot^\mu (s\circ\shifts)] )$\\
{\bf Clos}&$(a[s])[t]$&$\to$&$a[s\circ t]$\\
\\
{\bf IdL}&$\rid \circ s$&$\to$&$s$\\
{\bf IdR}&$s\circ \rid$&$\to$&$s$\\
{\bf ShiftCons}&$\shifts \circ (a\cdot^\mu s)$&$\to$&$s$\\
{\bf AssEnv}&$(s_1 \circ s_2) \circ s_3$&$\to$&$s_1 \circ (s_2 \circ s_3)$\\
{\bf MapEnv}&$(a \cdot^\mu s) \circ t$&$\to$&$a[t] \cdot^\mu (s \circ t)$\\
{\bf VarShift}&$\deb\cdot^\mu\shifts$&$\to$&$\rid$\\
{\bf Scons}&$\deb[s]\cdot^\mu (\shifts\circ s)$&$\to$&$s$\\
\end{tabular}\end{center}

It remains to work out what normalization and confluence properties
this system enjoys. It seems likely that weak or strong normalization of
any fragment should
follow easily from the same property holding of
the corresponding fragment in the
underlying calculus without irrelevance by a simple erasure
of modality information.
However, to whatever extent the property would depend on the typability
of a term, we might encounter difficulty dealing with full dependent types,
since erasing irrelevance from a dependently typed term may not
result in a well-typed term.

We might also consider adding a reduction rule
$$\hbox{\bf Irrel}\qquad a\cdot\irrel s\to a'\cdot\irrel s$$
to capture the intended meaning of an irrelevant cons, but this
clearly destroys any hope of weak normalization.
We expect to handle the necessary quotienting-out of terms at
irrelevant conses in a different way.

%All of the rules given comprise $\lsi + I$. The system without {\bf Irrel}
%we refer to as $\lsi$, and without {\bf Beta}, {\bf Eta}, and {\bf Irrel}
%we call $\si$.
%By an erasure of all modality annotations the $\lsi$-calculus inherits
%weak normalization from the $\lambda\sigma$-calculus,
%and similarly $\si$ inherits strong normalization from $\sigma$.

\section{Pattern substitutions}
\def\rpat{\mathop{\sans pat}\nolimits}

We define a judgment 
$s \rpat^{n\ge I}$,
where $s$ is a substitution, $n$ a natural number, and $I$ a list
of natural numbers. Its intended meaning is that $s$ is a pattern
substitution using deBruijn indices from $I$ which are no greater than $n$.
$$\begin{prooftree}\justifies \shifts^n \rpat^{n\ge\cdot}\end{prooftree}$$
$$\begin{prooftree}
s \rpat^{n\ge I} \qquad m \le n, m \not\in I
\justifies
{\tt m} \cdot\rel s \rpat^{n\ge I,m}
\end{prooftree}
\qquad
\begin{prooftree}
s \rpat^{n\ge I }
\justifies
a \cdot\irrel s \rpat^{n \ge I}
\end{prooftree}$$
From this we can define {\em pattern terms} via a judgment
$a \rpat^{n\ge I}$,
defined by the rules
$$\begin{prooftree}
s \rpat^{n \ge I}
\justifies
X[s] \rpat^{n \ge I}
\end{prooftree}$$
$$\begin{prooftree}
a \rpat^{n \ge I} \qquad m \le n, m\not \in I
\justifies
a \app\rel {\tt m} \rpat^{n \ge I,m}
\end{prooftree}
\qquad
\begin{prooftree}
a \rpat^{n\ge I}
\justifies
a\app\irrel b \rpat^{n \ge I}
\end{prooftree}$$
\begin{definition}
A $\lsi$-term is a {\em $\lsi$-pattern} if all of its subterms
of the form $a = (X[s]\ b_1 \cdots b_n)$ are such that $a \rpat^{n\ge I}$
for some $n,I$.
A {\em pattern substitution} is a $\lsi$-substitution 
all of whose $\lsi$-normal
forms $s$ are such that $s \rpat^{n\ge I}$ for some $n,I$.
\end{definition}
Note that in the absence of any confluence result we hedge our
definition of of pattern substitutions by referring to all normal
forms rather than {\em the} normal form of the substitution $s$.
Since we do not have substitution variables, we hope that in
fact the normal forms of substitutions are well-behaved, and this
inelegance can be removed.

The proof in \cite{dowek.patterns} that pattern spines have
one-sided inverses is constructive, and so can be described
algorithmically. We first give a direct description, and then sketch how
to modify the algorithm.
By unrolling the induction definition, a pattern substitution $\xi$ is of the form
$$a_1 \cdot^{\mu_1} a_2 \cdot^{\mu_2} \cdots a_n \cdot^{\mu_n} \shifts^m$$
Put 
$$\bar\xi = b_1 \cdot\rel b_2 \cdot\rel \cdots b_m \cdot\rel \shifts^n$$
where
$$b_i = \cases{{\tt j}&If $\exists j . \mu_j=\relsym \land a_j = {\tt i}$;\cr \hbox{a fresh variable } X&otherwise. }$$
Observe that 
$$\xi \circ \bar\xi \to^* a_1[\bar\xi] \cdot^{\mu_1} a_2[\bar\xi] \cdot^{\mu_2} \cdots a_n[\bar\xi] \cdot^{\mu_n} \shifts^n$$
For any $i$, if $\mu_i$ is $\relsym$, then by definition of $\bar\xi$ and pattern substitutions we have $a_i[\bar\xi] \to^* {\tt i}$.
Otherwise, $\mu_i$ is $\irrelsym$, and we have some term $a_i[\bar \xi]$ occurring at an irrelevant position in the substitution
$\xi$. The intended meaning of irrelevance is that this is just as good as any other term at the same
type, in particular the deBruijn index ${\tt i}$. Therefore, in a certain sense (and determining the right way to
formalize this is the subject of our current effort) we have
$$\xi \circ \bar \xi \approx {\tt 1}\cdot^{\mu_1} {\tt 2}\cdot^{\mu_2} \cdots {\tt n}\cdot^{\mu_n} \shifts^n \to^* \rid$$

To modify the algorithm, we add modality annotations to the existing rules in a straightforward way, and 
add two rules,
\def\inv{^{-1}}
\begin{center}\begin{tabular}{rrcl}
{\bf NDI}&$n[a\cdot\irrel\xi]\inv$&$=$&$(n[\xi]\inv)[\shifts]$\\
{\bf UDI}&$\shifts^m \circ (a\cdot\irrel\xi)\inv$&$=$&$(\shifts^m \circ \xi\inv) \circ \shifts$\\
\end{tabular}\end{center}
which cause irrelevant conses to act as if they were deBruijn indices that `never occur,'
in the sense that they never match other indices during application of an inverse substitution
to a term ({\bf NDI} is like {\bf ND}$\ne$ in \cite{dowek.patterns}) and they never occur in the range of $\shifts^m$ during composition
of a substitution with an inverse substitution ({\bf UDI} is like {\bf UD1}).

\section{Future Work}
Computing the inverse is not the only operation on substitutions involved in the unification
algorithm of \cite{dowek.patterns}. We must also extend the definition of intersection $\xi \cap \zeta$
to transform equations of the form $X[\xi] = X[\zeta]$, and the pruning substitution
$\xi | \zeta$ to correctly handle flexible occurrences of metavariables. Moreover it may be
that the notion of flexible occurrence can be extended to include occurrences of metavariables
anywhere under irrelevant application while maintaining the correctness of the overall algorithm.
We intend to work out the appropriate extensions of these concepts towards a complete unification
algorithm for higher-order patterns in the extended theory, and answer the basic questions
of normalization and confluence mentioned above.
%\begin{center}\begin{tabular}{rrcl}
%{\bf A}&$(a_1 \app^\mu a_2)[\xi]\inv$&$=$&$ a_1[\xi]\inv \app^\mu \widehat{a_2}[\xi]\inv$\\
%{\bf L}&$(\lambda_A^\mu a) [\xi]\inv$&$=$&$ \lambda_A^\mu (\widehat a[1 \cdot^\mu (\xi\circ \shifts)]\inv)$\\
%{\bf NK}$>$&$n[\shifts^k]\inv$&$=$&$n - k$ if $n > k$\\
%{\bf NK}$\le$&$n[\shifts^k]\inv$&&fails if $n \le k$\\
%{\bf ND}$=$&$n[n\cdot\rel\xi]\inv$&$=$&1\\
%{\bf ND}$\ne$&$n[m\cdot\rel\xi]\inv$&$=$&$(n[\xi]\inv)[\shifts]$ if $m \ne n$\\
%{\bf NDI}&$n[b\cdot\irrel\xi]\inv$&$=$&$(n[\xi]\inv)[\shifts]$\\
%{\bf D}&$(a \cdot^\mu \zeta) \circ \xi\inv$&$=$&$a[\xi]\inv \cdot^\mu (\zeta \circ \xi\inv)$\\
%\end{tabular}\end{center}

\bibliographystyle{alpha}
\bibliography{/home/godel/public/jcreed0}
\end{document}
