Optimizing Higher-Order Pattern Unification
Total Page:16
File Type:pdf, Size:1020Kb
Optimizing Higher-Order Pattern Unification Brigitte Pientka and Frank Pfenning? Department of Computer Science Carnegie Mellon University [email protected],[email protected] Submitted, January 2003 Abstract. We present an abstract view of existential variables in a de- pendently typed lambda-calculus based on modal type theory. This al- lows us to justify optimizations to pattern unification such as lineariza- tion, which eliminates many unnecessary occurs-checks. The presented modal framework explains a number of features of the current imple- mentation of higher-order unification in Twelf and provides insight into several optimizations. Experimental results demonstrate significant per- formance improvement in many example applications of Twelf, including those in the area of proof-carrying code. 1 Introduction Unification lies at the heart of automated reasoning systems, logic programming and rewrite systems. Thus its performance affects in a crucial way the global efficiency of each of these applications. This need for efficient unification algo- rithms has led to many investigations in the first-order setting. However, the efficient implementation of higher-order unification, especially for dependently typed λ-calculus, is still a central open problem limiting the potential impact of higher-order reasoning systems such as Twelf [15], Isabelle [12], or λProlog [9]. The most comprehensive study on efficient and robust implementation tech- niques for higher-order unification so far has been carried out by Nadathur and colleagues for the simply-typed λ-calculus in the programming language λProlog [7, 8]. The Teyjus compiler [10] embodies many of the insights found, in partic- ular an adequate representation of lambda terms and mechanisms to delay and compose substitutions. Higher-order unification is implemented via Huet’s al- gorithm [5] and special mechanisms are molded into the WAM instruction set to support branching and carrying unification problems. To only perform an occurs-check when necessary, the compiler distinguishes between the first occur- rence and subsequent occurrences of a variable and compiles them into different WAM instructions. While for the first occurrence of a variable the occurs-check may be omitted, full unification is used for all subsequent variables. This ap- proach seems to work well in the simply-typed setting, however it is not clear how to generalize it to dependent types. ? This work has been partially supported by NSF Grant CCR-9988281 “Logical and Meta-Logical Frameworks” and by a Siebel Scholarship awarded to the first author. In this paper, we discuss the efficient implementation of higher-order pattern unification for the dependently typed lambda-calculus. Unlike Huet’s general higher-order unification algorithm which involves branching and backtracking, higher-order pattern unification [6, 13] is deterministic and decidable. An impor- tant step toward the efficient implementation of higher-order pattern unification was the development based on explicit substitutions and de Bruijn indices [3] for the simply-typed lambda-calculus. This allows a clear distinction between bound and existential variables and reduces the problem to essentially first-order uni- fication. Although the use of de Bruijn indices leads to a simple formal system, the readability may be obstructed and critical principles are obfuscated by the technical notation. In addition, some techniques like pre-cooking of terms and optimizations such as lowering and grafting remain ad hoc. This makes it more difficult to transfer these optimizations to other calculi. We present an abstract view of existential variables in the dependently typed lambda-calculus based on modal type theory. Our calculus does not require de Bruijn indices, nor does it require closures M[σ] as first-class terms. This leads to a simple clean framework which allows us to explain a number of features of the current implementation of higher-order unification in Twelf [15] and pro- vides insight into several optimizations. In the paper, we will particularly focus on one optimization called linearization, which eliminates many unnecessary occurs-checks. We have implemented this optimization of higher-order unifica- tion as part of the Twelf system. Experimental results demonstrate significant performance improvement, including those in the area of proof-carrying code. The paper is organized as follows: First we give some background on modal logic and modal type theory, and discuss its relation to the dependently typed lambda calculus (see Section 2). In Section 3 we discuss higher-order pattern uni- fication. In particular, we focus on the optimization, called linearization, which eliminates unnecessary occurs-checks. Finally in Section 4 we discuss experimen- tal results. Related work is discussed in Section 5. 2 A modal foundation for typed existential variables 2.1 Motivation We start by presenting a foundation for dependently typed existential variables based on modal logic. Following the methodology of Pfenning and Davies [14], we can assign constructive explanations to modal operators. A key characteristic of this view, is to distinguish between propositions that are true and propositions that are valid. A proposition is valid if its truth does not depend on the truth of any other propositions. This leads to the basic hypothetical judgment A1 valid,...An valid; B1 true,...,Bm true ` C true. Under the multiple-world interpretation of modal logic, C valid corresponds to C true in all reachable worlds. This means C true without any assumptions, except those that are assumed to be true in all worlds. We can generalize this 2 idea to also capture truth relative to a set of specified assumptions by writing C valid Ψ, where Ψ is the abbreviation for C1 true,...,Cn true. In terms of the multiple world semantics, this means that C is true in any world where C1 through Cn are all true and we say C is valid relative to the assumptions in Ψ. Hypotheses about relative validity are more complex now, so our general judgment form is A1 valid Ψ1,...,An valid Ψn; B1 true,...,Bm true ` C true While it is interesting to investigate this modal logic above in its own right, it does not come alive until we introduce proof terms. In this paper, we investigate the use of a modal proof term calculus as a foundation for existential variables. We will view existential variables u as modal variables of type A in a context Ψ while bound variables are treated as ordinary variables. This allows us to dis- tinguish between existential variables u::(Ψ `A) for relative validity assumptions A valid Ψ declared in a modal context, and x:A for ordinary truth assumptions A true declared in an (ordinary) context. If we have an assumption A valid Ψ we can only conclude A true if we can verify all assumptions in Ψ. ∆, A valid Ψ, ∆0; Γ ` Ψ (∗) ∆, A valid Ψ, ∆0; Γ ` A true In other words, if we know A true in Ψ, and all elements in Ψ can be verified from the assumptions in Γ , then we can conclude A true in Γ . As we will see in the next section, this transition from one context Ψ to another context Γ , can be achieved via a substitutions from Ψ to Γ . 2.2 Dependently typed lambda calculus based on modal logic In this section, we introduce a dependently typed lambda calculus. Existential variables u are treated as modal variables and x denotes ordinary variables. c and a are constants, which are declared in a signature. This is a conservative extension of the LF [4] so we suppress some routine details such as signatures. Kinds K ::= type | Πx:A. K Families A, B, C ::= a | AM | Πx:A1.A2 Objects M, N ::= c | x | u[σ] | λx:A. M | M1 M2 Substitutions σ, τ ::= · | σ, M/x Contexts Γ, Ψ ::= · | Γ, x:A Modal Contexts ∆ ::= · | ∆, u::(Ψ `A) Note that the substitution σ is part of the syntax of existential variables. This eliminates the need of pre-cooking [3] which raises existential variables to the correct context. The principal judgments are listed below. As usual, we omit similar judg- ments on types and kinds and all judgments concerning definitional equality. 3 ∆; Γ ` M : A Object M has type A ∆; Γ ` σ : Ψ Substitution σ matches context Ψ ` ∆ mctx ∆ is a valid modal context ∆ ` Ψ ctx Ψ is a valid context Note substitutions σ are defined only on ordinary variables x and not modal variables u. We write idΓ for the identity substitution (x1/x1, . , xn/xn) for a context Γ = (·, x1:A1, . , xn:An). We will use π for a substitution which may permute the variables, i.e π = (xΦ(1)/x1, . , xΦ(n)/xn) where Φ is a total per- mutation defined on the elements from a context Γ = (·, x1:A1, . , xn:An). We only consider well-typed substitutions, so π must respect possible dependencies in its domain. We also streamline the calculus slightly by always substituting simultaneously for all ordinary variables. This is not essential, but saves some tedium in relating simultaneous and iterated substitution. Moreover, it is also closer to the actual implementation where we use de Bruijn indices and postpone explicit substitutions. The typing rules are given in Figure 1. 0 ∆, u::(Ψ `A), ∆ ; Γ ` σ : Ψ 0 0 (∗) ∆; Γ, x:A, Γ ` x : A ∆, u::(Ψ `A), ∆ ; Γ ` u[σ]:[σ]A ∆; Γ, x:A1 ` M : A2 ∆; Γ ` M1 : Πx:A2.A1 ∆; Γ ` M2 : A2 ∆; Γ ` λx. M : Πx:A1.A2 ∆; Γ ` M1 M2 :[idΓ ,M2/x]A1 ∆; Γ ` σ : Ψ ∆; Γ ` M :[σ]A ∆; Γ ` (·):(·) ∆; Γ ` (σ, M/x):(Ψ, x:A) ` ∆ mctx ∆ ` Ψ ctx ∆; Ψ ` A : type ` (·) mctx ` (∆, u::(Ψ `A)) mctx ∆ ` Ψ ctx ∆; Ψ ` A : type ∆ ` (·) ctx ∆ ` (Ψ, x:A) ctx Fig. 1. Typing rules for objects, substitutions, and context Note that the rule for modal variables is the rule (*) presented in the previ- ous section, annotated with proof terms and slightly generalized, because of the dependent type theory we are working in.