Focusing on Binding and Computation

Variable binding is a prevalent feature of the syntax and proof theory of many logical systems. In this paper, we define a programming language that provides intrinsic support for both representing and computing with binding. This language is extracted as the Curry-Howard interpretation of a focused sequent calculus with two kinds of implication, of opposite polarity. The representational arrow extends systems of definitional reflection with a notion of scoped inference rules, which are used to represent binding. On the other hand, the usual computational arrow classifies recursive functions defined by pattern-matching. Unlike many previous approaches, both kinds of implication are connectives in a single logic, which serves as a rich logical framework capable of representing inference rules that mix binding and computation.


Introduction
A logical framework provides a set of reusable abstractions that simplify the task of representing the syntax and semantics of logical systems, such as programming languages and proof theories.For example, the LF logical framework [17] permits facile representations of binding and scope (α-equivalence, capture-avoiding substitution) using the LF function space, a type which corresponds to logical implication.In this broad sense of the phrase, programming languages such as ML and Haskell are even more basic examples of logical frameworks, in that algebraic datatypes permit first-order representations of syntax and proofs.These languages' function spaces, which also correspond to logical implication, provide support not for representing binding, but for computing with syntax and proofs by pattern-matching.In contrast, LF requires a separate layer such as Twelf [29] for computation, which means that it is impossible to embed computations On the other hand, one way of distinguishing different aspects of the "same" connective is through the logical notion of polarity [15].For example, linear logic exposes two conjunctions of opposite polarity (positive ⊗ and negative ), and likewise two disjunctions (positive ⊕ and negative ).Operationally, polarity can be given an intuitive explanation in terms of pattern-matching [41]: values of positive polarity can be eliminated by pattern-matching against their constructors, whereas values of negative polarity can be introduced by pattern-matching against their destructors.This is why, for example, in ordinary functional programming, functions can be defined by pattern-matching, but it is impossible to pattern-match against a function (except with a variable pattern)-implication is a negative connective.
Our work began with the observation that, although variable binding behaves in some ways like ordinary implication, it also seems to have positive polarity.For example, in Twelf, LF functions are analyzed by matching against higher-order patterns.Following this intuition, we define a logic that includes both a positive form of implication-used to represent binding-and ordinary negative implication-used to compute with binding.This logic builds on definitional reflection [16,35], which supposes a database of rules used for both building proofs of propositional atoms and for deriving consequences of atoms by "reflection" (i.e., by inverting the rules).Through the Curry-Howard interpretation, the rule database corresponds to a database of datatype constructors, which can be used both to build datatype values and to define functions by pattern-matching.The key novelty of our positive implication is that it permits this database of rules to vary.Positive implication, written R ⇒ A, internalizes the act of hypothesizing a new rule: a proof of R ⇒ A is a proof of A under assumption of the inference rule R. A value of type R ⇒ A has the form λ u.V , where u is a scoped datatype constructor.Such a value is deconstructed by pattern-matching with a higher-order pattern.We call this positive connective ⇒ the representational arrow, opposed to the negative connective →, the ordinary computational arrow.
This approach to representing variable binding, which we call definitional variation, provides a more general theory of inference rules than LF, because rules can mix representational and computational functions.All rule systems representable in LF satisfy the structural properties of a hypothetical judgement (weakening, exchange, contraction, and substitution) because all LF rules are pure [4]-they place no constraints on the context in which they can be applied.In contrast, computational functions can be used to define impure rules: for example, if a rule system includes a rule with premise P → ⊥ asserting the refutability of P, it will not be possible to weaken a derivation using this rule to a context including a proof of P.However, this failure of the structural properties is not problematic in our framework: First, the representational arrow is eliminated by pattern matching, not by application (modus ponens), so the framework itself requires no commitment to the structural properties.Second, using a notion of subordination [39], we can give general conditions under which the structural properties hold , and provide operations such as weakening and substitution "for free" when these conditions are satisfied.In this sense our calculus maintains the practical benefits of the LF approach, where the structural properties are provided by the framework, while providing a more general theory of inference rules.
Following Zeilberger [42], our computational arrow admits a form of open-endedness [20]: computational functions in our type theory are represented abstractly by metalevel functions from patterns to expressions.This openendedness has several practical benefits: (1) We can implement structural properties such as weakening and substitution once as a datatype-generic program at the metalevel, reusing one implementation for a large class of rule systems.(2) We can realize meta-functions as programs in existing proof assistants, which permits us to reuse their pattern coverage checkers.(3) We can use our type theory as an interface for combining functions written in different proof assistants, using different implementations of binding, in a single program.
The technical contributions of this paper are as follows: In Section 2, we present a focused sequent calculus with both implications → and ⇒, as well as a suite of other connectives.We define the identity and cut-elimination procedures, and prove they are total under assumptions about the form of the rule database.We discuss some counterintuitive logical properties of ⇒, as well as a dual representational conjunction.In Section 3, we give a proof term assignment to the sequent calculus, yielding a functional programming language with an operational semantics given by cut elimination.In Section 4, we show that our framework extends simply-typed LF and discuss datatype-generic implementations of the structural properties.In Section 5, we illustrate programming in our type theory with an example that mixes binding and computation; more examples are available in our companion technical report [22].

Sequent Calculus
When describing the sequent calculus in this section, we foreshadow the proof-term assignment given in Section 3, freely interchanging logical and type-theoretic terminology ("proposition" and "type", "implication" and "function space", "logic" and "type theory", etc.).The logic we construct is polarized, meaning that we maintain a syntactic separation between positive and negative propositions, and its proofs are focalized in the sense of Andreoli [3].Following Zeilberger [41], the focused sequent calculus is defined in two stages.First, the polarized connectives are defined by axiomatizing the structure of patterns.Positive connectives are defined by constructor patterns, and negative connectives by destructor patterns.Second, there is a general focusing framework that is independent of the particular connectives of the logic.
For the sake of presentation, we begin by defining a focused sequent calculus for polarized intuitionistic logic, including the simple structure of patterns and the general focusing rules-this sequent calculus is a variation of the one given for polarized classical logic in [41].We then extend the structure of patterns to describe the more expressive logic of definitional variation.Next, we prove the identity and cut theorems for this logic, and consider some interesting properties of the representational connectives.

Simple contexts and patterns
We write X + ,Y + , Z + and X -,Y -, Z -to stand for positive and negative propositional variables (atomic propositions), and A + , B + ,C + and A -, B -,C -to stand for arbitrary positive and negative formulas.We use α to range over assumptions X + or C -, and dually γ to range over conclusions X -or C + .A linear context ∆ is a list of assumptions.
The positive connectives are defined through the judgement ∆ C + , which corresponds to applying only linear right-rules to show C + from ∆.For example, the rules for atoms, conjunction, and disjunction are as follows: Foreshadowing the Curry-Howard interpretation, we refer to derivations of this judgement as constructor patterns; linearity captures the restriction familiar from functional programming that a pattern binds a variable just once.
Negative connectives are defined by ∆ C -> γ, which corresponds to using linear left-rules to decompose C -into the conclusion γ.A proof term for this judgement is a destructor pattern, which gives the shape of an elimination context (continuation) for negative types: Observe that a destructor pattern for A + → B -includes a constructor pattern for A + , as well as a destructor pattern for B -, matching the possible observations on a function type.We have adopted linear logic notation by writing ⊗ for positive and for negative conjunction.In the present setting, both of these connectives encode ordinary intuitionistic conjunction with respect to provability, but they have different proof terms: positive conjunction is introduced by an eager pair whose components are values, and eliminated by pattern-matching against both components; negative conjunction is eliminated by projecting one of the components, and introduced by pattern-matching against either possible observation, i.e. by a lazy pair.

Focusing Judgements
In Figure 1, we present the focusing rules.In these rules, Γ stands for a sequence of linear contexts ∆, but Γ itself is treated in an unrestricted manner (i.e., variables are bound once in a pattern, but may be used any number of times within the pattern's scope).
The first two judgements concern the positive connectives.The judgement Γ [C + ] defines right-focus on a positive formula, or positive values: a positive value is a constructor pattern under a substitution for its free variables.Focus judgements make choices: to prove C + in focus, it is necessary to choose a particular shape of value by giving a constructor pattern, and then satisfy the pattern's free variables.Values are eliminated with the left-inversion judgement Γ γ 0 > γ, which defines a positive continuation by case-analysis.Inversion steps respond to all possible choices that the corresponding focus step could make: the rule for C + quantifies over all constructor patterns for that formula, producing a result in each case.By convention, we tacitly universally quantify over metavariables such as ∆ that appear first in a judgement that is universally quantified, so in full the premise reads "for all ∆, if ∆ C + then Γ, ∆ γ."The positive connectives are thus introduced by choosing a value (focus) and eliminated by continuations that are prepared to handle any such value (inversion).For atoms, the only case-analysis is the identity.

Figure 1. Focusing rules
The next two judgements concern the negative connectives, where the relationship between introduction/elimination and focus/inversion is reversed.
A negative formula is eliminated by the left-focus judgement Γ [C -] > γ, which chooses how to observe C -by giving a negative continuation.A negative continuation consists of a destructor pattern, a substitution, and a case-analysis.The destructor pattern and substitution decompose a negative type C -to some conclusion γ 0 , for instance a positive type C + .However, it may take further case-analysis of this positive type to reach the desired conclusion γ.Dually, negative types are introduced by inversion, which responds to left-focus by giving sufficient evidence to support all possible observations.The right-inversion judgement Γ α, where assumptions α are negative formula or positive atoms, specifies the structure of a negative value.A negative value for C -must show that for all destructors of C -, the conclusion is justified by the variables bound by the patterns in it.
The judgement Γ γ, defines a neutral sequent, or an expression: from a neutral sequent, one can either right-focus and return a value, or left-focus on an assumption in Γ and apply a negative continuation to it.Finally, a substitution Γ ∆ provides a negative value for each hypothesis.
At this point, the reader may wish to work through some instances of these rules (using the above pattern rules) to see that they give the expected derived rules for the connectives: (no rule for 0)

Patterns for Definitional Variation
In Section 2.1, we gave a fixed set of rules for constructing simple patterns.We now describe patterns for definitional variation by including an open-ended database of rules.A rule R takes the form , where A + 1 , . . ., A + n are positive formulas and P is a defined atom.Rules are collected in a rule context Ψ, which is now carried through the pattern-typing judgments (∆ ; Ψ A + and ∆; Ψ A -> γ).
A rule Ψ can be applied to produce a constructor pattern for P: Note that rules can be applied an arbitrary number of times while constructing a pattern.Now, consider the patterntyping rules for the new connectives of definitional variation, representational implication and representational conjunction: Both connectives expand the rule context, introducing a scoped constructor of type R. The rule for R ⇒ B + builds a constructor pattern for B + under assumption of R and essentially (if we ignore structural punctuation) looks like an implication right-rule, while the rule for R B -builds a destructor pattern for B -and looks like a conjunction left-rule.However, as we will see in Section 2.5, these connectives behave quite differently from ordinary implication and conjunction, in part due to their non-standard polarity.
Most of the remaining rules (see Figure 2) for the connectives of polarized logic are unremarkable, since they simply carry the rule context through unchanged.The "shift" connectives ↑ and ↓ deserve explanation, though.Following Girard [14], these mark the boundary between positive and negative polarity, and correspondingly they mark the point where pattern-matching must end [41].Because the rule context can change during the course of pattern-matching, it is necessary to associate assumptions and conclusions with a specific rule context.We indicate this with contextual formulas Ψ A + and Ψ A -, so that the rules for the shift connectives are: In spite of this richer notion of patterns, the generic focusing rules of Figure 1 remain unchanged if we adopt some notational sleight-of-hand: we now take C + and C -to range over contextual formulas, and write ∆ Ψ A + as notation for ∆ ; Ψ A + , and Example Consider the syntax of the untyped λ -calculus: e ::= x | λ x.e | e 1 e 2 This syntax is represented in our type theory by the following definition signature Ψ λ : For clarity, we name the rules in the rule context here, foreshadowing the presentation with proof terms in Section 3. The λ -calculus terms with free variables x 1 , . . ., x n are isomorphic to derivations of the constructor pattern judgement • ; Ψ λ , x 1 : exp, . . ., x n : exp exp.The fact that the rules defining exp may vary during a derivation is essential to this representation of the new variables bound in a term.The computational arrow then provides the means to induct over λ -terms: A negative value • Ψ λ exp → ↑exp represents a function from λ -terms in the empty context to λ -terms in the empty context.Such a term is defined by an ω-rule which gives one case for each λ -term: whereas the traditional definitional reflection rule [16,35] unrolls a definition only a single step, our inversion rules unroll a definition until they reach a polarity shift.

Identity and Cut
In addition to inductive types like exp, the context Ψ can be used to define arbitrary recursive types.For example, consider an atom D defined by one constant D is essentially the recursive type µX.X → X, which can be used to write non-terminating programs.
Because the rule context permits the definition of general recursive types, it should not be surprising that the identity and cut principles are not admissible in general.Through the Curry-Howard interpretation, however, we can still make sense of the identity and cut principles as corresponding, respectively, to the possibly infinite processes of η-expansion and β -reduction.We now state these principles, "prove" admissibility of cut with an operationally sound but possibly non-terminating procedure (see our technical report [22] for the analogous identity procedure), and then discuss criteria under which this proof is well-founded.
Procedure.Consider the first cut principle.The two derivations must take the following form: By plugging ∆ C -> γ 0 from the right derivation into the higher-order premise of the left derivation, we obtain Γ, ∆ γ 0 .Then Γ γ 0 by substitution with Γ ∆, whence Γ γ by composition with Γ γ 0 > γ.The case of positive reduction is analogous (but appeals only to substitution).In all cases of composition, if γ 0 = X -then the statement is trivial.Otherwise, we examine the last rule of the left derivation.For the first composition principle, there are two cases: either the sequent was derived by right-focusing on the conclusion γ 0 = C + , or else by left-focusing on some hypothesis C -∈ Γ.In the former case, we immediately appeal to positive reduction.In the latter case, we apply the second composition principle, which in turn reduces to the third, which then reduces back to the first.
Likewise, to show substitution we examine the rule concluding Γ, ∆ J. Dually to the composition principle, the only interesting case is when the sequent was derived by left-focusing on C -∈ ∆, wherein we immediately apply a negative reduction.
Observe that we have made no mention of particular connectives or rule contexts, instead reasoning uniformly about focusing derivations.As we alluded to above, however, in general this procedure is not terminating.Here we state sufficient conditions for termination.They are stated in terms of a strict subformula ordering, a more abstract version of the usual structural subformula ordering.
Definition 1 (Strict subformula ordering).We define an ordering C ± 1 C ± 2 between contextual formulas as the least transitive relation closed under the following properties: For any contextual formula C ± , we define C to be the restriction of to formulas below C ± .
The strict subformula ordering does not mention atoms X + or X -, since they only play a trivial role in identity and cut.
Definition 2 (Well-founded formulas).We say that a contextual formula C ± is well-founded if C is well-founded.
Proposition 1. Positive and negative identity are admissible on well-founded formulas.
Proposition 2. Positive and negative reduction are admissible on well-founded formulas.
Proof.By inspection of the above procedure.Positive and negative reduction are proved by mutual induction using the order C , with a side induction on the left derivation to show composition, and a side induction on the right derivation to show substitution.
Definition 3 (Pure rules).A rule R is called pure if it contains no shifted negative formulas ↓A -as premises (or structural subformulas of premises).For example, exp ⇐ (exp ⇒ exp) is pure, but D ⇐ ↓(D → ↑D) is not.
Proof.By induction on the structure of A ± .Every pattern typing rule (recall Figure 2) examines only structural subformulas of A ± , except when A + = P.But any P defined by pure rules n in fact has no strict subformulas, since the ∆ i such that ∆ i ; Ψ A + i can contain only atomic formulas X + .
The restriction to pure rules precludes premises involving the computational arrow.However, as we show below, it includes all inference rules definable in the LF logical framework, generalizing Schroeder-Heister's [35] proof of cut-elimination for the fragment of definitional reflection with →-free rules (since pure rules do not exclude ⇒'s).Moreover, as we explained, the identity and cut principles are always operationally meaningful, even in the presence of arbitrary recursive types.Technically, we could adopt a coinductive reading of the focusing rules (cf.[14]), in which case identity is always productive, and cut-elimination is a partial operation that attempts to build a cut-free proof bottom-up.We conjecture that cut-elimination is total given a positivity restriction for rules.

Shock therapy
In §6.2 of "Locus Solum", Girard [14] considers several "shocking equalities"-counterintuitive properties of the universal and existential quantifiers that emerge when they are given non-standard polarities.For example, positive ∀ commutes under ⊕, while negative ∃ commutes over .In our setting, ⇒ behaves almost like a positive universal quantifier, and almost like a negative existential. 1 And indeed, we can reproduce analogues of these commutations.
These relations are extended to (non-contextual) polarized formulas if they hold under all rule contexts.Proposition 3 ("Shocking" equalities).

R ⇒ (A
1 These would become real quantifiers in an extension to dependent types.k

Figure 3. Proof Terms
Proof.Immediate-indeed, in each case, both sides have an isomorphic set of patterns.
Why are these equalities shocking?Well, if we ignore polarity and treat all the connectives as ordinary implication, disjunction, and conjunction, then (2) is reasonable but (1) is only valid in classical logic.And if we interpret ⇒ and as ∀ and ∃, then both equations are shockingly anticlassical:

(∃x.A) (∃x.B) ≈ ∃x.(A B)
On the other hand, from a computational perspective, these equalities are quite familiar.For example, (1) says that a value of type A ⊕ B with a free variable is either the left injection of an A with a free variable or the right injection of a B with a free variable.
We can state another pair of surprising equivalences between the connectives ⇒ and under polarity shifts: Proposition 4 (Some/any).
Again, this coincidence under shifts is not too surprising, since it recalls the some/any quantifier Nx.A of nominal logic [31], as well as the self-dual ∇ connective of Miller and Tiu [25].Nx.A can be interpreted as asserting either that A holds for some fresh name, or for all fresh nameswith both interpretations being equivalent.

Proof Terms
In Figure 3, we present a proof term assignment to the focused sequent calculus described above, with one proof

Figure 4. Operational Semantics
term for each rule in the calculus.Additionally, we internalize the cut and identity principles: v -• k -and v + • k + witness reduction; e ; k + and k -; k + and k + 1 ; k + 2 witness composition; and x and ε and id witness identity.For programming convenience (see Section 5), we also internalize an admissible substitution concatenation principle (σ 1 , σ 2 ), and a general recursion operator fix(x.v-).To make the examples below more concise, we tacitly parametrize all judgements by a fixed initial definition context Σ, which acts as a prefix on each contextual formula in the judgement forms (i.e., Ψ A acts as Σ, Ψ A did without the signature).The full typing rules are presented in Figures 6 and 7 at the end of this article.

α-equivalence
The pattern for a contextual type Ψ A + is a contextual pattern Ψ.p, where Ψ notates the bare variables (no rule annotations) from Ψ. Contextual patterns for Ψ A + are modal [27]-all of the rule variables free in the pattern must be explicitly bound by Ψ-and are typed by the judgement ∆ Ψ.p :: Ψ A + .This judgement is defined by passing to the judgement ∆ ; Ψ p :: A + , in which the variables in Ψ are free in p. Negative patterns Ψ.n are typed similarly: In the sequent calculus above, we treated the judgements ∆ Ψ A + and ∆ ; Ψ A + synonymously, but here this distinction clarifies the binding structure of our language.The proof terms Ψ.p and Ψ.n, as well as the proof terms λ u. p and unpack; u.n for ⇒ and , are binding forms, and the standard notion of α-equivalence applies to them and to the context Ψ in the typing judgements for p and n.Other contexts Ψ appear in contextual types in ∆ and γ, but these are separate binding occurrences and can be renamed independently. 2 Similarly, in ∆ Ψ.p :: Ψ A + , the variables in Ψ and in Ψ are independent binding occurrences.Because patterns are modal, no rule variables are free in values, continuations, expressions, or substitutions.We tacitly quotient patterns by α-equivalence at the meta-level, so that meta-functions are defined on α-equivalence classes of patterns.This ensures that computational functions respect α-equivalence of represented languages.
Meta-functions.Our type theory is, by design, openended with respect to the meta-functions φ , mapping patterns to expressions, which are used to represent caseanalysis and induction.We have exploited this freedom by implementing simple embeddings of our language in Agda and Coq, 3 where meta-functions are realized as functions in Agda/Coq, and totality of meta-functions is established using the pattern coverage checkers of these existing tools.Both of these embeddings use de Bruijn indices to represent rule variables, but other implementations of our type theory are free to use different representations of variables, and program fragments written using different representations of binding can be combined.

Operational Semantics
In Figure 4, we adapt the above cut-elimination procedure into a small-step operational semantics on closed expressions.We use an auxiliary metaoperation e [σ ] implementing capture-avoiding substitution, which is defined using the induction principle for the iterated inductive definition of our proof term syntax.Consequently, the operational semantics require that the the class of meta-functions φ is closed under definitions using this induction principle.The canonical forms of simply-typed LF (STLF) [40] are summarized in Figure 5.We show that the STLF terms exist as closed patterns, and therefore as values, in our type  theory.This theorem permits us to inherit en masse the adequacy of all systems that have been represented in STLFe.g., the above signature Ψ λ , which is the embedding of the usual LF encoding of this syntax.
Every STLF type τ can be parsed both as an inference rule r(τ) and as a positive formula p(τ) (for convenience, we identify STLF base types with our defined atoms P): The function r(τ) can then be used to map STLF signatures Σ and contexts Φ to inference rule contexts Ψ.
To check that LF substitution is faithfully modelled in our calculus, we can recast the usual hereditary substitution algorithm for LF [40] as a meta-operation on closed patterns.However, it is also possible to prove a much more general substitution principle, which covers uses of iterated inductive definitions.

Structural Properties
As discussed above, the rule context Ψ does not in general satisfy the structural properties of a hypothetical judgement, because computational functions can be used to define impure rules.However, we can establish the structural properties generically under sufficient conditions that computational functions cannot interfere.To state these conditions, we use a notion of subordination [39], which tracks when values of one type are relevant to values of another.First, we define a judgement P A ± ∈ Ψ ("P is insubordinate to A ± "), which means that no rule concluding P can be used by a value of type A ± .Next, we define a judgement P A ± ∈ Ψ ("P is uncircumscribed by A ± "), which means that P is insubordinate to the domain of any computational arrow in A ± .We say that a rule n is insubordinate to/uncircumscribed by A ± iff P is.Finally, we define a judgement bindsof A ± B ± ∈ Ψ, which means that all rules that may be bound by a value of type A ± are uncircumscribed by B ± .We refer the interested reader to our companion Agda code for the formal definitions of these judgements. Let Using our Agda implementation, we have given negative values of the following types: • apply : Ψ (((P ⇒ A ± ) ⊗ P) → A ± ) if P A ± ∈ Ψ, u : P and bindsof A ± P ∈ (Ψ, u : P).
The function strengthen removes an insubordinate rule from the context.The function weaken adds an uncircumscribed rule to the context.The value apply substitutes a value for a base type; the subordination conditions are necessary for strengthening the arguments to computational functions in A ± (P A ± ∈ Ψ, u : P) and weakening the proof of P as the substitution operation passes under binders in A ± (bindsof A ± P ∈ (Ψ, u : P)).These functions are defined by the same recursion on types as the proof of the identity theorem in Section 2, and they are total/productive in the same circumstances as identity.At present, we have only implemented substitution for base types generically, though we conjecture a generalization to all higher-order rules in the embedding of LF.
To illustrate these structural properties, consider a signature Ψ with constants lam : exp ⇐ (exp ⇒ exp) and omega : exp ⇐ ↓(nat → ↑exp), which might arise in representing a proof theory for natural numbers with an ω-rule.In this signature, exp is insubordinate to nat (expressions cannot be used to build natural numbers), but exp is not insubordinate to exp (expressions can be used to build expressions).Thus, strengthen permits strengthening away expvariables, but not nat-variables, from a nat.However, exp is uncircumscribed by exp, whereas nat is not uncircumscribed by exp (because of the computational premise of omega).Thus, weaken allows for weakening an exp with an exp, but not with a nat (which would add a new case to the computational argument to omega).Moreover, the only rule bound by exp, namely exp, is uncircumscribed by exp, so apply allows for substituting an exp into an exp.

Programming Example
We present one simple example of mixing binding and computation; our companion technical report [22] contains several additional examples, including ones illustrating our approach to computing with open terms.We write the example using a named syntax for rule variables, which could either be implemented directly in a proof assistant, or elaborated into de Bruijn form.
Consider the syntax of a simple language of arithmetic expressions, where numeric primitives are represented by computational functions.In LF, each primitive operation would require its own constructor; here, we represent binary primops (binops) uniformly as computational functions of type nat ⊗ nat → ↑nat.The language includes numeric constants, binops, and let-binding: For example, if plus is a function (negative value) of type • (nat ⊗ nat → ↑nat) implementing addition on nats, then the value (binop (num 4) f (num 5)) [plus/f ] is the abstract syntax for the arithmetic expression "4 + 5".We implement an evaluator for closed programs using a fixed point: The body of the negative value is defined by a meta-function (an Agda function in our Agda implementation), where the variable ev is the recursive reference: In this code, we employ a bit of syntactic sugar, suppressing the ε terminating a destructor pattern, the identity substitution id, and the identity case-analysis ε .The variables p are meta-variables ranging over patterns (Agda variables in our Agda implementation), which allow us to specify the behavior of ev * on all arithmetic expressions using only finitely many cases.In the binop case, we recursively evaluate the first argument p 1 , and match the result as p 1 , then we evaluate the second argument p 2 , and finally we apply the embedded computational function f to the values of the arguments.
In the let case, we apply the body of the let to the let-bound term and evaluate the result.The function apply, which was discussed in Section 4, applies a representational function to an argument by performing substitution.The subordination conditions necessary for calling apply are satisfied in this case: while the rules for ari use a computational function that circumscribes nat, they do not circumscribe ari.

Related Work
Variable binding can be implemented concretely in a variety of ways (see Aydemir et al. [5] for a survey).Among the concrete representation techniques, definitional variation is most similar to representations where the context of a term is marked in its type, such as de Bruijn representations using nested types or dependency [1,6,7].In these representations, binders introduce a new constructor for variables, which are explicitly injected into terms.Our framework builds this use of dependency into the language: all types are contextual and all datatypes may be extended by rule variables introduced by ⇒ and .This creates an opportunity to implement the structural properties once (modulo subordination conditions) for all types, including negative types such as computational functions, and to abstract away from the concrete implementation of variables themselves-as in LF, we can provide a named notation without requiring the programmer to manage names.
In systems based on the LF logical framework [17], LF is taken as a pure representation language, and a separate layer is provided for computation.In Twelf [29], Delphin [33], and Beluga [30], the computational layer is an entirely separate language.Schürmann et al. [36] describe an approach in which the same arrow is used for both computation and representation, with primitive recursion isolated by a modality, but computation is nonetheless segregated because the computational modality cannot appear in rules.These stratified approaches have the advantage that all representations automatically obey the structural properties of a hypothetical judgement, with the disadvantage that certain encoding techniques, which rely on embedding computation in data, are not possible.Our framework removes this stratification, allowing rules that embed computation, with the consequence that not all representable rule systems satisfy the structural properties.However, as discussed in Section 4, we have implemented strengthening, weakening, and substitution generically under certain subordination conditions.Consequently, our framework provides meta-operations implementing the structural properties "for free" for all rule systems definable in simply-typed LF, as well as for many more rule systems that use iterated inductive definitions.
Our current framework lacks dependent types, a limitation we plan to address in future work.In a dependentlytyped setting, equality of terms influences equality of types, and equality of types influences type checking.In our setting, type checking will thus depend on the equational behavior of the meta-functions implementing the structural properties such as substitution.We are optimistic that the equational theory of the LF fragment of our framework will agree with LF definitional equality, even in an intensional setting, because we have implemented substitution by ex-tending the hereditary substitution algorithm used in canonical LF [40].However, we leave a detailed investigation of this issue to future work.
It is tempting to try to reuse the computational function space of existing proof assistants such as Coq and Isabelle/HOL to represent binding, but the naive approach admits too many functions.One solution to this problem is to use a predicate to identify those computational functions that are in fact substitution functions [2,9,11,18,26].Another solution is to bind meta-language variables of an abstract type defined only by an axiomatic characterization of the properties of variables [8].In contrast, our representational functions provide a direct means of adequately encoding binding, without requiring side conditions or axioms.Moreover, as we hope to have demonstrated, encoding ⇒ in terms of → ignores some of its essential properties, such as the distributivity principles in Section 2.5, and the ability to decompose a representational function by pattern-matching.
Nominal logic [13] is a theory of names and binding that has been implemented in several programming languages (e.g., FreshML [32,37] and the Isabelle nominal datatype package [38]).The differences between the nominal approach and ours stem from the fact that FreshML separates fresh name generation from the binding of a name in a scope, whereas in our type theory rule variables do not exist outside of the scope in which they are bound.Nominal logic facilitates the direct representation of informal algorithms that use names without being explicit about their scope, whereas our approach follows the LF methodology of recasting these algorithms in terms of a more disciplined binding structure.Separating name generation from scoping makes it more difficult to determine what names are free in a computation, requiring freshness analyses [32], specification logics [34], or stateful operational semantics [37] in order to ensure that functions respect α-equivalence of representations.In contrast, the free rule variables of all computations are tracked by our type system, and respect for α-equivalence is achieved simply, by quotienting patterns by α-equivalence.
In light of the present analysis, it is interesting to reexamine an old proposal by Miller [24] for an extension to ML with primitives for binding, including a new function type 'a => 'b and a restricted form of higher-order pattern-matching.For example, the fact that the codomain 'b must be an equality type in Miller's proposal is related to the present restriction that the codomain A + be positivealthough it is less general, since A + can contain embedded negative formulas, which are not equality types.Technically, we are able to go beyond Miller's proposal because we associate negative hypotheses with a context of parameters.This idea appears in Miller and Tiu's more recent work [25], as well as in contextual modal type theory [27].Indeed, Miller and Tiu's self-dual ∇ connective is closely related to ⇒ and , also capturing the notion of a scoped constant.An essential difference, however, is that because the ∇ proof theory adopts a logic programming-based distinction between propositions and types (∇ quantifies over a type and forms a proposition), it is significantly less subtle than definitional variation.For example, ∇ cannot appear in the domain of a ∇ (in contrast to ⇒).
Fiore et al. [12] and Hofmann [19] give semantic accounts of variable binding.It would be interesting to see whether these semantic accounts can be extended to rule systems such as ours which permit computational functions in premises.

Conclusion
We have presented a language that enables the free interaction of binding with computation, extracted as the Curry-Howard interpretation of a focused sequent calculus with two forms of implication.We believe this provides an appropriate logical foundation, but much work remains to be done.We plan to pursue an independent implementation of our language by giving a first-order language for metafunctions, rather than relying on existing tools.Additionally, a generalization to dependent types (which we have already begun to explore [21]) would realize the goal of giving intrinsic support for variable binding in a constructive type theory, combining the best of frameworks such as Twelf and Coq.

Theorem 1 (
Type safety).Progress: If • e : γ then e = v + or e → e .Preservation: If • e : γ and e → e then • e : γ 4 Adequacy and Structural Properties 4.1 Embedding of Simply-Typed LF