Abstract 1 Introduction 2 Machine-Based Pure Subtype Systems 3 Transitivity elimination 4 Towards a Type-Safe System 5 Related work 6 Future work 7 Conclusion References

Towards the Type Safety of Pure Subtype Systems

Valentin Pasquale ORCID CEA List, Université Paris-Saclay, France Álvaro García-Pérez ORCID CEA List, Université Paris-Saclay, France
Abstract

Hutchins’ Pure Subtype Systems (PSS) offer a unified framework for types and terms, promising significant advancements in language design for features like dependent types and higher-order subtyping. However, the theory has been hampered by a critical gap: a proof of type safety has remained an open problem for over a decade. The original attempt to prove this property relied on the conjectured commutativity of two fundamental reduction relations, equivalence and subtyping. Proving transitivity elimination, however, requires this commutativity, a property that is notoriously difficult to establish for higher-order subtyping systems.

In this paper, we address this issue by introducing Machine-Based PSS (MPSS), a novel reformulation of the original system. MPSS integrates a continuation stack mechanism, reminiscent of the Krivine Abstract Machine, to keep track of arguments that are passed during function application, enabling more fine-grained reductions. This architectural change exposes crucial intermediate reduction steps that were absent in the original PSS. The primary contribution of our work is a direct proof that the equivalence and subtyping reductions in MPSS commute. This result formally establishes transitivity elimination, which is the cornerstone of the inversion lemma required for type safety. We conclude by outlining a pathway from our foundational result to a complete, type-safe system, thereby paving the way for the practical realization of PSS-based languages.

Keywords and phrases:
Lambda calculus, Pure subtype systems, Dependent types, Higher-order subtyping, Type safety
Copyright and License:
[Uncaptioned image] © Valentin Pasquale and Álvaro García-Pérez; licensed under Creative Commons License CC-BY 4.0
2012 ACM Subject Classification:
Theory of computation Type theory
Related Version:
Full Version: https://arxiv.org/abs/2407.13882
Editors:
Stefano Guerrini and Barbara König

1 Introduction

Hutchins’ Pure Subtype Systems (PSS for short) [19, 18] have been proposed as a novel approach to type theory that enables a number of advanced language features for extensibility and genericity. Among other benefits, PSS (hereafter in the singular) harmoniously solves both the expression problem [30, 32] and the tag-elimination problem [18]. PSS’s expressivity allows one to implement extensible modules based on deep mixin composition, which is a key result for a type system [18]. By blurring the distinction between types and terms, PSS naturally combines dependent types and higher-order subtyping, which makes the approach very promising as a basis for generic, modular, and extensible programming. However, a proof of type safety for PSS is conspicuously lacking. This crucial property has only been shown to hold under the conjecture that two key reduction relations in the system, equivalence and subtyping, commute [18]. The commutativity of these two reductions is an instance of a recurrent problem in higher-order subtyping known as transitivity elimination. Consequently, the type safety of PSS has remained an open problem for more than a decade.

In this paper, we resolve this long-standing open problem by introducing a novel reformulation of Hutchins’ PSS. Our system is based on a mechanism reminiscent of the Krivine Abstract Machine, it allows more fine-grained reductions and exposes crucial intermediate reduction steps that were absent in the original PSS. This architectural change allows for a direct and elegant proof of transitivity elimination.

Contrary to traditional type systems, PSS harmoniously mixes terms and types. Consider the natural number 3 and the type 𝑁𝑎𝑡 of natural numbers. In PSS one can have a term that encodes 𝑁𝑎𝑡+3 with the naive Church encoding of naturals, which type-checks to 𝑁𝑎𝑡. Terms can also be used instead of types: for instance the function λx 3.x, whose subtype annotation is 3 (we write for the subtyping relation, that is, the substitute of typing in our calculus), is a valid expression. The latter example is an instance of bounded quantification [26] where the term 3 is the singleton type {3} and the subtyping relation is akin to the subset relation.

By replacing the typing relation with a more general subtyping relation, PSS greatly increases expressivity. Since terms and types belong to the same syntactic kind, PSS naturally subsumes dependent types and higher-order subtyping. This unification enables a host of advanced language features for extensibility, genericity, and efficiency, including virtual types, recursive types, deep mixin composition, feature-oriented programming, bounded quantification, and partial evaluation [19, 32, 18].

In the original work on PSS, Hutchins introduces a declarative system with two relations: one for equivalence (akin to β-equivalence) and another for subtyping, both of which are defined with transitive closure. The proof of type safety requires a type preservation property, which states that the type of a program is preserved under evaluation. This property, in turn, hinges on a critical inversion lemma: any two functions in the subtyping relation must have equivalent subtype annotations. This lemma cannot be proven directly in the declarative system due to the presence of transitivity, which introduces intermediate terms that may not be structurally related to the functions being compared. Proving type preservation thus amounts to showing that transitivity is an admissible rule in the system.

In order to prove transitivity elimination, Hutchins proposes an equivalent syntax-directed, algorithmic system with two different notions of reduction: an equivalence reduction that models β-reduction, and a subtyping reduction that models small-step subtyping, where subtyping reduction subsumes equivalence reduction. For short, we may write “reduction” for the β-reduction, and “promotion” for the subtyping reduction. The declarative subtyping relation is shown to be equivalent to the combination of subtyping and equivalence reduction sequences as depicted below.

utiffexistsvsuch that

In the algorithmic setting, transitivity can be shown to be admissible if these two notions of reduction commute. Diagrammatically, commutativity ensures that a transitive derivation which forms a “stair” of reduction and promotion steps can be flattened into a single angle.

Despite his efforts, Hutchins failed to prove that commutativity holds in his algorithmic system, which prevented him from proving transitivity elimination and type safety. The culprit of this failure is exemplified by a very elementary case (depicted below), which involves the reduction of a redex and the promotion of the formal parameter in the redex’s abstraction to its annotation.

The redex (λxt.x)v on the bottom-left corner of the diagram is reduced (in the horizontal) to v, and promoted (in the vertical) to (λxt.t)v. It is then unclear how to complete the right edge of the diagram in a single step.

To tackle this problem, Hutchins resorted to simultaneous reduction and to the decreasing diagrams technique of [29], but he failed to assign depths to each reduction step so as to show that reduction decreases in the way prescribed by the decreasing diagrams technique, because β-reduction increases this depth. Hutchins himself explains this failure in detail in his PhD thesis (Section 2.7: Confluence and commutativity)[18].

We reformulate the PSS theory by providing an alternative version of the algorithmic system with a more fine-grained notion of subtyping, in which we can now prove the commutativity result. Our version, which we dub Machine-Based PSS (MPSS for short), uses a continuation stack mechanism, reminiscent of the Krivine Abstract Machine (KAM) [20, 1] to track operands passed to abstractions. This mechanism enables a direct proof of commutativity. Indeed, by explicitly managing operands on a stack, the subtyping relation in MPSS exposes all the intermediate terms that are needed to complete the commutativity diagram directly, without resorting to complex techniques like decreasing diagrams. For instance, in our system, the subtyping relation (λxt.x)v(λxt.t)v doesn’t exist, instead the variable x must first be promoted to the operand v, which later may be promoted to the annotation t. Exposing this intermediate step, essentially disallowing premature promotion that exists in Hutchins’ system, allows us to complete the above commutativity diagram:

Our detailed contributions are the following:

  • We introduce MPSS, a reformulation of Hutchins’ PSS that, among other changes, keeps track of the operands that have been passed at a particular scope by using a mechanism reminiscent of the continuation stack of the KAM [20, 1]. MPSS is a more fine-grained system in the sense that the stack mechanism exposes intermediate terms that were absent in Hutchins’ system.

  • We prove the commutativity of the subtyping and equivalence reductions of MPSS, which enables transitivity elimination and the inversion lemma. Our proof of commutativity is direct and proceeds by simple structural induction on terms.

  • We sketch how a type system can be derived from our alternative system, thanks to transitivity being now admissible.

The remainder of this paper is organised as follows. Section 2 introduces MPSS. Our system is a reformulation of Hutchins’ algorithmic framework equipped with a stack mechanism inspired by the Krivine Abstract Machine (KAM) to track operands, alongside two reductions for subtyping and equivalence. Section 3 proves the commutativity of the two reduction relations in MPSS. Section 4 sketches the design of a type system built upon MPSS and discusses the path to proving its type safety. Section 5 and 6 discuss related and future work, and Section 7 concludes.

All the formal proofs of the lemmas and theorems stated in the paper are collected in the appendix of the full version of this paper accessible online at https://arxiv.org/abs/2407.13882.

2 Machine-Based Pure Subtype Systems

Machine-Based PSS (MPSS for short) is a reformulation of PSS where the algorithmic reductions are instrumented with a stack mechanism reminiscent of the Krivine Abstract Machine (KAM) [20, 1].

MPSS is an extension of the pure lambda calculus with a constant symbol 𝖳𝗈𝗉 for the most general supertype and with subtype annotations in the formal parameters of abstractions. Assuming a countably infinite set of variables V ranged over by x,y,, the syntax of terms, contexts, and stacks is defined as follows:

t,u,v,α,Λ::=x𝖳𝗈𝗉(λxt.u)(uv)Γ::=εΓ,xtΓ,xαs::=nilα::s

A variable xV is a term. The term 𝖳𝗈𝗉 is the most general supertype, such that any other term is its subtype. An abstraction (λxt.u) has a parameter x, a subtype annotation t, and a body u. An application (uv) has an operator u and an operand v. The Greek letter α is a metavariable for terms that originate as operands from the stack.

We assume the usual notions of free and bound variables, and the usual capture-avoiding substitution function, denoted by u[x\v], that replaces the free occurrences of variable x in u by v, while avoiding the capture of any bound variable in u. We consider terms that result after the renaming of bound variables to be identical. When needed, we assume that α-equivalence is applied at will to avoid clashing of free variables.

An application of the form (λxt.u)v is a redex. Contraction of a redex is modelled by β-reduction, which rewrites (λxt.u)v into u[x\v]. Consider the following example of a redex involving the term λx𝖳𝗈𝗉.x for universal identity. Since universal identity is a subtype of 𝖳𝗈𝗉, the self-application of universal identity, i.e. the redex (λx𝖳𝗈𝗉.x)(λx𝖳𝗈𝗉.x), reduces to x[x\λx𝖳𝗈𝗉.x], which is equal to universal identity by the definition of the substitution function. We assume conventional parenthesis-dropping conventions where abstractions associate to the right and applications to the left, and applications bind stronger than abstractions. We define normal forms as terms that are either Top, functions that have a subtype annotation and a body in normal form, or a variable applied to any number of normal forms.

Prevalidity of logical contexts Γprevalid

Prevalidity of extended contexts Γ;sprevalid

Figure 1: Prevalidity in MPSS.

Our system uses logical contexts (contexts for short) Γ to store annotations from abstractions encountered within a given scope. A context Γ is a sequence of subtype annotations xt and equivalence annotations xα. We write xt to denote either kind of annotation, and throughout this paper the metavariable designates either or . We use ε for the empty context and we write Γ,xt for the operation that appends annotation xt to the subtype annotations of Γ. By abuse of notation, we write Γ1,Γ2 for the concatenation of contexts Γ1 and Γ2. We say that x has subtype t in Γ (written xtΓ) if xt is the rightmost annotation for x in Γ. Respectively, we say that x is equivalent to α in Γ (written xαΓ) under similar conditions.

Equivalence reduction Γ;suv

Subtyping reduction Γ;suv

Figure 2: Equivalence and subtyping reduction in MPSS.

In order to accommodate the stack mechanism, we introduce extended contexts Γ;s, which are logical contexts Γ coupled with a continuation stack s. Extended contexts adapt the algorithmic reduction relations to the stack mechanism of MPSS.

Figure 1 introduces the prevalidity condition on contexts that ensures each annotation in a context mentions a different variable, to avoid clashing of variable names. We say that x is in the domain of Γ (written xdom(Γ)) if and only if an annotation for variable x occurs in Γ, i.e., there exists a term t (or a term α) such that xtΓ (or xαΓ). The empty context ε is prevalid (Rule Pv-Emp in Figure 1). Adding a subtype annotation xt, or an equivalence annotation xα to a prevalid context Γ yields a prevalid context if the free variables of t (respectively, α) are in the domain of Γ, and if x is not already in the domain of Γ (Rules Pv-Ctx and Pv-EqA). Prevalidity for extended contexts (also defined in Figure 1) stipulates that a prevalid context coupled with an empty stack is prevalid (Rule Pv-Nil), and that an extended context Γ;α::s is prevalid if the free variables of α are in the domain of Γ (Rule Pv-Sta). We now present our reduction relations.

Figure 2 presents our equivalence () and subtyping () reductions. We do away with a declarative system like Hutchins’ system, and our MPSS resembles his algorithmic system (p. 58 of [18]) where we adopt simultaneous reduction upfront in order to simplify the commutativity proofs (resembling p. 74 of [18]), and where contexts are coupled with a stack in order to accommodate the stack mechanism. The equivalence reduction models a reflexive, small-step, simultaneous β-reduction. Rule Me-Bet performs β-reduction, while Rule Me-Pro replaces a variable by its equivalent operand from the context. Rules Me-Var and Me-Top allow the reduction relation to be reflexive. Rule Me-TAp is a technical device from Hutchins’ formulation in [18], whose objective is to accommodate terms with shape 𝖳𝗈𝗉u, which may pop up in reductions, and which cannot be β-reduced by Rule Me-Bet (see p. 59 of [18] for a further discussion on this issue). Me-App propagates promotion through unapplied applications by pushing the operand onto the stack and proceeding with the operator, mimicking KAM’s stack management. Rules Me-Fun and Me-FOp distinguish between unapplied and applied abstractions: Me-Fun propagates promotion by enlarging the context with the abstraction’s annotation and proceeding with the body, and Me-FOp promotes the body of an applied abstraction by taking the operand from the stack and enlarging the context with an equivalence annotation.

The subtyping reduction defines promotion in a stack-aware manner. Rule Ms-Pro promotes a variable to its subtype annotation, and Rule Ms-Top promotes any term to 𝖳𝗈𝗉. Rule Ms-Equ subsumes equivalence reduction. Rules Ms-App, Ms-Fun and Ms-FOp are the counterparts of the equivalence reduction rules Me-App, Me-Fun and Me-FOp respectively. Because Hutchins’ declarative system is not contravariant, its algorithmic system, which we took inspiration from, does not change the type annotation of abstractions: in his system, there is no rule λxt.uλxt.u with assumption tt. The main motivation for this design choice is to avoid the undecidability of the related typechecking, as described in [25], and to avoid complicating the meta-theory even more. Our system follows the same principle, both Rules Ms-Fun and Ms-FOp do not change the type annotation, and in general there is no reduction rule to change the type annotations, except from equivalence reductions that are allowed. Handling the interaction between the stack and the context is the cornerstone to prove the commutativity result, and to avoid the premature promotion issues that plagued Hutchins’ approach.

To illustrate our stack mechanism, consider the following subtyping reduction derivation

Rule Ms-App pushes the operand v onto the stack. Inside the abstraction, the variable x is not promoted to its type t but is instead reduced to the operand v retrieved from the context, which was placed there by Ms-FOp after popping from the stack. Keeping track of operands prevents the premature promotion that caused Hutchins’ commutativity proof to fail.

As in Hutchins’ PSS, we define a subtyping relation , on which we wish to prove that transitivity is admissible in the next section, as follows, with being the transitive closure of :

utiffexistsvsuch that

3 Transitivity elimination

As explained in the introduction, proving transitivity elimination requires us to show that the equivalence () and subtyping () reductions commute, which we do below in this section. In order to state the commutativity result, we first define a reduction relation on extended contexts, which captures the evolution of annotations during reduction.

Reduction of extended context Γ;sΓ;s

With this definition, we can now state our main commutativity result.

Lemma 1 ( and strongly commute).

Let Γ;s be an extended context. Let t0, t1, and t2 be terms. If Γ;st0t1 and Γ;st0t2, then for any extended context Γ;s such that Γ;sΓ;s, there exists a term t3, such that Γ;st2t3 and Γ;st1t3.

Diagrammatically, the existence of the solid arrows implies the existence of the dashed arrows:

The generalisation over all possible resulting contexts Γ;s is crucial for the inductive proof. Indeed, consider the following commutation diagram, with context Γ;nil:

The only possible arrow that could exist, apart from a promotion of the whole term to 𝖳𝗈𝗉, is a promotion of the variable x to its subtype annotation t. Therefore, the top right term is λxt.t, and the diagram is completed as follows:

However, in the proof of this theorem, everything works by induction on the terms. Therefore, starting from the original diagram, we apply the induction hypothesis on the following diagram, with context Γ,xt;nil:

And as we have seen above, we must complete this diagram with t being the top right term, as the only possible completion of the original diagram is the term λxt.t. Therefore, the completed diagram by the induction hypothesis must be:

Hence the requirement that the context of the right arrow must be Γ,xt;nil, and therefore the formulation of the theorem with a generalisation over contexts.

Similarly, our equivalence reduction relation satisfies the diamond property, on which the result above rests. A similar generalisation over contexts is required, with two different reduced extended contexts however, as both the top and the right edge can now do variable promotions thanks to Rule Me-Pro. The pathological case is the following, with context Γ;nil:

To solve this pathological case in the induction, one of the sub-induction calls is the following diagram:

And similarly as above, the top edge is completed with extended context Γ,xα2;nil, whereas the right edge is completed with extended context Γ,xα1;nil. Both these contexts satisfy the requirement of the theorem (that is, we have Γ,xα0;nilΓ,xα2;nil and Γ,xα0;nilΓ,xα1;nil).

Lemma 2 ( has the diamond property).

Let Γ0;s0 be an extended context. Let t0, t1, and t2 be terms. If Γ0;s0t0t1 and Γ0;s0t0t2, then for any extended contexts Γ1;s1 and Γ2;s2 such that Γ0;s0Γ1;s1 and Γ0;s0Γ2;s2, there exists a term t3 such that Γ1;s1t1t3 and Γ2;s2t2t3.

Moreover, for any variable x, if in the derivation of Γ0;s0t0t1 (respectively Γ0;s0t0t2) there isn’t an application of the Rule Me-Pro that makes a promotion of variable x, then in the derivation Γ2;s2t2t3 (respectively Γ1;s1t1t3) there won’t be an application of the Rule Me-Pro that makes a promotion of variable x.

Diagrammatically:

With commutativity and the diamond property established, we can prove that our system enjoys the transitivity elimination property.

Theorem 3 (Transitivity is admissible).

Let Γ;s be an extended context. Let u and v be terms. If Γ;suv then Γ;suv.

In order to illustrate our result, and highlight the differences between PSS and MPSS, we detail below an example of an application of Theorem 3:

Example of transitivity elimination

Consider the Church encodings for type “Unit” (the type with only one inhabitant, term 𝑈𝑛𝑖𝑡 below), the encoding of the constructor “single” of type Unit (term 𝑠𝑖𝑛𝑔𝑙𝑒 below), and an arbitrary well-formed subtype A that encodes some interesting type. Let the identity over the elements of subtype A be the term 𝑖𝑑A below, and consider an arbitrary well-formed term a of subtype A such that some derivation ε;nilaA exists which may possibly be as lengthy or complicated as it may be.

𝑈𝑛𝑖𝑡=λy𝖳𝗈𝗉.λxy.y𝑠𝑖𝑛𝑔𝑙𝑒=λy𝖳𝗈𝗉.λxy.xA=arbitrary subtype𝑖𝑑A=λxA.xa=arbitrary subtype of A

Consider the following derivation:

Γ;nil𝑖𝑑Aa𝑠𝑖𝑛𝑔𝑙𝑒Aa𝑈𝑛𝑖𝑡Aa

Figure 3 details all the elementary steps (diagrammatically) of the above algorithmic derivation. We illustrate the proof of Theorem 3 over this diagram.

Figure 3: Derivation of Γ;nil(λxA.x)a𝑈𝑛𝑖𝑡Aa.

Theorem 3 ensures that the transitivity step collecting the two derivations Γ;nil𝑖𝑑Aa𝑠𝑖𝑛𝑔𝑙𝑒Aa and Γ;nil𝑠𝑖𝑛𝑔𝑙𝑒Aa𝑈𝑛𝑖𝑡Aa can be removed, obtaining a transitivity-free derivation

Γ;nil𝑖𝑑Aa𝑈𝑛𝑖𝑡Aa

Our constructive proof produces a transitivity-free derivation of Γ;nil𝑠𝑖𝑛𝑔𝑙𝑒Aa𝑈𝑛𝑖𝑡Aa (i.e., flattens it into Γ;nil𝑠𝑖𝑛𝑔𝑙𝑒Aa𝑈𝑛𝑖𝑡Aa witnessed by the straight vertical and horizontal paths in the middle of the diagram), and then applies the commutativity theorem to complete the upper-right corner of the diagram, obtaining the transitivity-free derivation Γ;nil(λxA.x)a𝑈𝑛𝑖𝑡Aa. This proof is possible in our algorithmic system thanks to the operand stack and Rule Ms-FOp which exposes the term (λxA.a)a, and to the fact that the derivation Γ;nil(λxA.a)a(λxA.A)a in the middle of the diagram preserves the possibly convoluted derivation Γ,xa;nilaA as a sub-derivation (marked with ). Although preserving this convoluted derivation seems at first glance counter-intuitive, it gives the opportunity to apply the induction hypothesis of the Theorem 3 to the term (λxA.a)a, which will flatten the convoluted derivation. Contrary to ours, Hutchins’ attempts at proving that transitivity is admissible reduce the complexity of the intermediate step by avoiding the convoluted derivation upfront (by directly taking the reduction (λxA.x)a(λxA.A)a which promotes the formal parameter a of the redex to its annotation A).

But this premature promotion of a formal parameter to its type annotation is in fact counter-productive, which manifests in the failure of Hutchins’ proof of the commutativity theorem, where the premature promotion prevents one from completing the diagram for redexes with a transitivity-free right edge.

This result was the missing cornerstone in Hutchins’ original theory, and its proof here is necessary for establishing type safety. In the next section, we present what can be a type-safe theory based on our MPSS.

4 Towards a Type-Safe System

Thanks to the above commutative system, we can now sketch the design of a type-safe system. The static semantics for this system is defined by a well-formedness judgment, presented in Figure 4, which is the static condition for type safety. Rules Wf-PrS and Wf-PrE ensure that a variable is well-formed if and only if it occurs in a prevalid context (respectively, in a subtype or an equivalence annotation). Rule Wf-Top ensures that 𝖳𝗈𝗉 is universally well-formed. Rule Wf-Fun ensures that an abstraction is well-formed if and only if it has a well-formed annotation, and its body is well-formed in a context enlarged with the abstraction annotation. Finally, Rule Wf-App ensures that an application is well-formed if and only if it has an operator that is a well-subtype of a function, and an operand that is a well-subtype of the operator’s subtype annotation.

Well-formedness depends on the well-subtyping relation wf, which in turn subsumes well-equivalence wf. The rules for well-subtyping connect the static system to our dynamic reduction semantics. For example, Rule Ws-Lf2 states that v is a well-subtype of t if v promotes to some v that is itself a well-subtype of t.

Term well-formedness Γtwf

Transitive well-subtyping Γvwft

Well-subtyping Γuwft

Figure 4: Well-subtyping in MPSS.

We define type safety as progress (well-formed terms either are in normal form or can be reduced), and preservation (subtyping relations between terms are preserved under reduction by the operational semantics below). We define the operational semantics as a context-based reduction semantics [13]: the evaluation is defined as the congruence closure (Rule Os-Con and evaluation contexts 𝖢) of β-reduction (Rule Os-Bet).

Operational Semantics tt

Well-formedness depends on the well-subtyping relation defined in Figure 4. Well-subtyping (wf) subsumes well-equivalence (wf). We use the meta-variable and define (wf), which captures both relations, and similarly for their transitive closure (wf). By a slight abuse of language, and to simplify the text, we write “well-subtyping” to refer to the meta-notation (wf) in the paragraph that follows.

Figure 4 introduces transitive well-subtyping, the straightforward transitive closure of the well-subtyping relation (Rules Ws-Sub and Ws-Trs). Next in the figure is well-subtyping, which is defined similarly to the diagrammatic subtyping relation defined in Section 2, but with extra requirements on the well-formedness of the terms involved. Rule Ws-Rfl states that well-subtyping is reflexive. Rule Ws-Lf1 ensures that v is a well-subtype of t if and only if v is a well-subtype of t and v reduces to v. Rule Ws-Lf2 ensures that v is a well-subtype of t if and only if v is a well-subtype of t and v promotes to v, but with the extra requirements that both v and v are well-formed, whose role is explained in a minute. Rule Ws-Rgh ensures that v is a well-subtype of t if and only if v is a well-subtype of t and t reduces to t.

Thanks to these definitions, we may define type safety in our calculus as the following progress and preservation theorems.

Theorem 4 (Progress).

Let t be a term. For every logical context Γ, if Γtwf then either t is in normal form, or there exists a term t such that tt.

Theorem 5 (Preservation).

Let Γ be a logical context. Let t, t, and u be terms. If Γtwfu and tt, then Γtwfu.

The proof of type preservation (Theorem 5) rests on showing that reduction preserves term well-formedness. Lemma 6 establishes this fact.

Lemma 6 (Evaluation preserves well-formedness).

Let Γ be a logical context. Let t and t be terms such that tt and such that Γtwf. We have Γtwf.

A crucial case in the proof of Lemma 6 is β-reduction, where a well-formed application (λxt.u)α reduces to u[x\α]. To handle this case, we need to show that well-formedness is preserved by substitution. The following lemma provides this guarantee, where the premise Γαwft comes from the well-formedness of the original term.

Lemma 7 (Substitution preserves well-formedness).

Let Γ and Γ be logical contexts, and let t, u and α be terms. Let x be a variable. If Γ,xt,Γuwf, and Γαwft, then Γ,Γ[x\α]u[x\α]wf.

Our subtyping relation in MPSS is context-dependent, unlike in PSS where it isn’t, which implies that Lemma 7 relies on the conjecture collected below, which states that the well-subtyping relation is independent of the surrounding program structure, on certain contexts named covariant contexts defined as follows:

𝖢𝗈::=(λxt.𝖢𝗈)(𝖢𝗈t)
Conjecture 8 (Well-subtyping is context independent).

Let Γ be a logical context and u and t be terms such that Γuwft. Let 𝖢𝗈 be a covariant context such that both 𝖢𝗈[u] and 𝖢𝗈[t] are well-formed in Γ. We conjecture that Γ𝖢𝗈[u]wf𝖢𝗈[t].

We believe that the conjecture holds with the additional well-formedness requirements that both v and v are well-formed in Rule Ws-Lf2 above.

All in all, type safety, which is the combination of progress (Theorem 4) and preservation (Theorem 5), holds under the assumption that Conjecture 8 holds. The proof that both these theorems hold under the assumption that Conjecture 8 holds can be found in the appendix of the full version online at https://arxiv.org/abs/2407.13882.

Establishing this conjecture is the final step towards a complete proof of type safety. The sketch of a complete type system provided in this section demonstrates a path forward, built upon the solid foundation of our commutativity result. Future work could either adopt this sketch and complete the proof of type safety, or develop a different type system that leverages the commutativity result established in Section 3 to establish type safety in a different way.

5 Related work

PSS naturally subsumes dependent types and higher-order subtyping, so we comment on both ideas.

There are many works related to dependent types. For instance in Cayenne [4], Idris [7] or Haskell [21], dependent types are an extension of the language, in contrast to our case where dependent types are built-in as there is no distinction between terms and types.

Other languages mainly use dependent types for theorem proving, for instance Coq [12] or Agda [6]. Our language is not strongly normalising, and our main goal is not theorem proving as we cannot use the language as a proof system, since applying the Curry-Howard correspondence to PSS in a consistent way is still an open question.

To accommodate the Curry-Howard correspondence, one possible approach is to distinguish between potentially non-terminating terms and terminating terms, either with monads or via other means [23, 24]. λθ is a calculus that explores this idea [9, 10, 28]. It combines proofs and programs into a single language that distinguishes two fragments, a logical fragment where every term is strongly normalising that is suited to write proofs, and a programmatic fragment that is Turing complete and lacks termination. Such systems allow so-called freedom of speech, which is the ability to write (terminating) proofs about non-terminating terms.

Subtyping extensions of type systems have been considered. In 1997, Chen proposed λC [11], an extension of the calculus of constructions λC with subtyping. However, Chen’s λC supports neither top types nor bounded quantification as we do in PSS.

Various extensions of System F, in order to equip it with subtyping, have also been considered in the literature. Within these we find System F [8] and System Fω [26], which respectively add subtyping and higher-order subtyping capabilities. PSS subsumes the version of System F without contravariant arrow-types [18].

The origin of mixing terms and types goes back to the introduction of pure type systems [22], which generalises the seminal works on lambda calculi with types by [5]. These systems have the ability to unify types and terms under the same kind, but there is a typing relation instead of a subtyping one. Moreover, in some of these pure type systems, the type is inhabited. This gives rise to the well-known Girard’s paradox (see [17]). In our system, there is not a primitive subtype, but we could encode such a subtype to be inhabited by itself, and that would be unproblematic since our aim is not to use PSS as a theorem prover.

Systems where terms can be used in certain ways as types have been considered. Aspinall studied the combination of subtyping with singleton types [3]. Singleton types are close to what we study, as in our calculus, terms can be seen as singleton types of this unique value. We have for instance, for every term t, εtt, which would be translated to t:{t} in a system with singleton types.

Other attempts to unify typing and subtyping exist. A more conservative attempt that tries to unify typing and subtyping is [31]. This work has two major differences with our approach. First, in [31] the subtyping relation still tracks types (their unification of subtyping and typing is more conservative than ours). Second, in PSS functions and function (sub)types are defined with the same syntax, and therefore they are both instances of the same construct, which is not the case in [31].

Because many interesting features described in [18] are related to modules, other type systems especially related to objects have been studied. Our work shares foundational goals with the Dependent Object Types (DOT) calculus, the theoretical basis for Scala 3 [2, 27]. Both PSS and DOT aim to create powerful, unified type systems that support advanced modularity, and provide a formal basis for solving challenges akin to the expression problem, the tag-elimination problem, and others. DOT achieves this, among other features, through path-dependent types, where the type of a member, p.T, depends on the term p. Because DOT allows objects to contain types as members, it provides a form of dependent typing. PSS takes a more radical approach by collapsing types and terms into a single syntactic category, where the traditional typing relation (:) is completely replaced by a universal subtyping relation (). This design is what enables PSS’s key features described in Sections 3 and 4 of [18].

6 Future work

As described in Section 4, it is left as future work to define a type system that is built upon MPSS and to prove its type safety. Another interesting area to explore is the link between our system and Hutchins’ system, to determine if both well-formed notions are equivalent.

Once this system is defined, it is also left as future work to obtain an algorithm that typechecks terms in MPSS. Hutchins describes an algorithm for his system in [18], but one should adapt his algorithm to this new system, and study upon which conditions it terminates. One possible approach is to use the operational relevance of terms, for which there exist some arguments that lead them to terminate. We leave as future work to explore other practical algorithms which terminate on operationally relevant terms.

Another venue of future work is to study whether PSS and MPSS are Turing-complete. To the best of our knowledge, whether PSS is Turing-complete has not been formally proven yet. The argument in favor of Turing-completeness rests on the existence of a (well-formed) looping combinator in PSS, which would consist of a family of terms Ln such that Lnf β-reduces to f(Ln+1f) for any f. Such a looping combinator exists for System λ [16], which can be encoded into Hutchins’ PSS thanks to the encoding of System λ in Hutchins’ PSS, provided on page 47 of [18]. In order to establish Turing-completeness of PSS, one should now adapt the argument made by [14] in Section 2 or the argument made in [15]. There is, however, no simple known expression of a looping combinator in System λ. The looping combinator from [16] can only be derived mechanically, and working with it directly is proven to be hard as its length is more than 40 pages (see the discussion on the logical consistency and impredicativity of System λ on pages 48–51 of [18]). It may therefore be difficult to conclude about Turing-completeness of either PSS or MPSS, which is left as future work.

7 Conclusion

This paper presents a variant of the PSS originally introduced by Hutchins in [18], which we name MPSS, and proves that our reformulation enjoys a key commutation property which was missing in Hutchins’ system. We also sketch how a type system can be derived from MPSS, which could lead to a type-safe system. To the best of our knowledge PSS and MPSS are the first theories that unify harmoniously terms and types by using a single kind where everything is a subtype.

Meta-theories of languages with advanced features on types are notoriously difficult to work with. We believe our work could help in laying the foundations for a new approach that mixes terms and types in innovative ways.

References

  • [1] Mads S. Ager, Dariusz Biernacki, Olivier Danvy, and Jan Midtgaard. A functional correspondence between evaluators and abstract machines. In Proceedings of International Conference on Principles and Practice of Declarative Programming, pages 8–19, 2003. doi:10.1145/888251.888254.
  • [2] Nada Amin, Samuel Grütter, Martin Odersky, Tiark Rompf, and Sandro Stucki. The essence of dependent object types. A List of Successes That Can Change the World: Essays Dedicated to Philip Wadler on the Occasion of His 60th Birthday, pages 249–272, 2016. doi:10.1007/978-3-319-30936-1_14.
  • [3] David Aspinall. Subtyping with singleton types. In International Workshop on Computer Science Logic, pages 1–15. Springer, 1994. doi:10.1007/BFB0022243.
  • [4] Lennart Augustsson. Cayenne—a language with dependent types. ACM SIGPLAN Notices, 34(1):239–250, 1998.
  • [5] Henk Barendregt. Lambda Calculi with Types, volume 2. Oxford University Press, 1993.
  • [6] Ana Bove, Peter Dybjer, and Ulf Norell. A brief overview of agda–a functional language with dependent types. In Theorem Proving in Higher Order Logics, pages 73–78. Springer, 2009. doi:10.1007/978-3-642-03359-9_6.
  • [7] Edwin Brady. Idris, a general-purpose dependently typed programming language: Design and implementation. Journal of functional programming, 23(5):552–593, 2013. doi:10.1017/S095679681300018X.
  • [8] Luca Cardelli, Simone Martini, John C. Mitchell, and Andre Scedrov. An extension of System F with subtyping. Information and computation, 109(1-2):4–56, 1994. doi:10.1006/INCO.1994.1013.
  • [9] Chris Casinghino. Combining proofs and programs. University of Pennsylvania, 2014.
  • [10] Chris Casinghino, Vilhelm Sjöberg, and Stephanie Weirich. Combining proofs and programs in a dependently typed language. ACM SIGPLAN Notices, 49(1):33–45, 2014. doi:10.1145/2535838.2535883.
  • [11] Gang Chen. Subtyping calculus of construction. In International Symposium on Mathematical Foundations of Computer Science, pages 189–198. Springer, 1997.
  • [12] Thierry Coquand and Gérard Huet. The calculus of constructions. PhD thesis, INRIA, 1986.
  • [13] Matthias Felleisen. The Calculi of Lambda-v-CS Conversion: A Syntactic Theory of Control and State in Imperative Higher-Order Programming Languages. PhD thesis, Department of Computer Science, Indiana University, 1987.
  • [14] Herman Geuvers and Joep Verkoelen. On fixed point and looping combinators in type theory, 2009.
  • [15] J. Roger Hindley and Jonathan P. Seldin. Lambda-Calculus and Combinators, An Introduction, 2nd Edition. Cambridge University Press, 2008.
  • [16] Douglas J. Howe. The computational behaviour of girard’s paradox. In Proceedings of the Symposium on Logic in Computer Science, pages 205–214. IEEE Computer Society, 1987.
  • [17] Antonius J. C. Hurkens. A simplification of girard’s paradox. In Typed Lambda Calculi and Applications: Second International Conference on Typed Lambda Calculi and Applications, TLCA’95 Edinburgh, United Kingdom, April 10–12, 1995 Proceedings 2, pages 266–278. Springer, 1995. doi:10.1007/BFB0014058.
  • [18] DeLesley S. Hutchins. Pure subtype systems: A type theory for extensible software. The University of Edinburgh, 2009.
  • [19] DeLesley S. Hutchins. Pure subtype systems. In Proceedings of the 37th Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, POPL ’10, pages 287–298. Association for Computing Machinery, 2010. doi:10.1145/1706299.1706334.
  • [20] Jean-Louis Krivine. A call-by-name lambda-calculus machine. Higher-Order and Symbolic Computation, 20(3):199–207, 2007. doi:10.1007/S10990-007-9018-9.
  • [21] Simon Marlow et al. Haskell 2010 language report, 2010. URL: http://www.haskell.org/.
  • [22] James McKinna and Robert Pollack. Pure type systems formalized. In International Conference on Typed Lambda Calculi and Applications, pages 289–305. Springer, 1993. doi:10.1007/BFB0037113.
  • [23] Eugenio Moggi. Notions of computation and monads. Information and computation, 93(1):55–92, 1991. doi:10.1016/0890-5401(91)90052-4.
  • [24] Frank Pfenning and Rowan Davies. A judgmental reconstruction of modal logic. Mathematical structures in computer science, 11(4):511–540, 2001. doi:10.1017/S0960129501003322.
  • [25] Benjamin C. Pierce. Bounded quantification is undecidable. Information and Computation, 112(1):131–165, 1994. doi:10.1006/INCO.1994.1055.
  • [26] Benjamin C. Pierce and Martin Steffen. Higher-order subtyping. Theoretical computer science, 176(1-2):235–282, 1997. doi:10.1016/S0304-3975(96)00096-5.
  • [27] Tiark Rompf and Nada Amin. Type soundness for dependent object types (dot). In Proceedings of the 2016 ACM SIGPLAN International Conference on Object-Oriented Programming, Systems, Languages, and Applications, pages 624–641, 2016. doi:10.1145/2983990.2984008.
  • [28] Vilhelm Sjoberg. A dependently typed language with nontermination. University of Pennsylvania, 2015.
  • [29] Vincent van Oostrom. Confluence by decreasing diagrams. Theoretical computer science, 126(2):259–280, 1994. doi:10.1016/0304-3975(92)00023-K.
  • [30] Philip Wadler. The expression problem, 1998. Posted to Java Genericity internet mailing list. URL: https://homepages.inf.ed.ac.uk/wadler/papers/expression/expression.txt.
  • [31] Yanpeng Yang and Bruno C. d. S. Oliveira. Unifying typing and subtyping. Proceedings of the ACM on Programming Languages, 1(OOPSLA):47:1–47:26, 2017. doi:10.1145/3133871.
  • [32] Matthias Zenger and Martin Odersky. Independently extensible solutions to the expression problem. In Workshop on Foundations of Object-Oriented Languages, 2005. URL: http://zenger.org/papers/fool05.pdf.