arXiv:2609.02005cs.PLSep 2026

Manuscript Offprint

arXiv:2609.02005

Unifying Function- and Argument-First Bidirectional Type Systems

Takuma Yoshioka, Taro Sekiyama, Atsushi Igarashi

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Abstract

Bidirectional typing mixes type synthesis and type checking into a single process. Existing bidirectional type systems can be classified into two styles based on whether, given a function application, a bidirectional typing algorithm synthesizes the function's type first and typechecks the argument against the synthesized argument type, or it synthesizes the arguments' types first and typechecks the function against the synthesized arguments' types. We call the former function-first and the latter argument-first. Not only do the two styles significantly differ in how the type systems and typing algorithms are formalized, but also they lead to incompatible typeabilities, forcing a language designer to select one style and to give up the other's typeabilities.

In this paper, we unify the two styles and develop λBH\lambda^{BH} with a new bidirectional type system for higher-rank polymorphism. Key ideas of the unification are twofold. Each function application is annotated with a bit of information to represent whether function- or argument-first typing is used, to allow a language designer (or even a programmer) to switch between the two styles at their discretion. We reformulate the function- and argument-first type systems by using ideas from colored types (Odersky et al., 2001) and boxy types (Vytiniotis et al., 2006), which can specify which part of a type should be synthesized or used for checking in a flexible manner. We also develop a typing algorithm based on the worklist approach by Zhao et al.

*This is a full version of the paper that has been accepted for publication in the Proceedings of Asian Symposium on Programming Languages and Systems (APLAS 2026), Lecture Notes in Computer Science (LNCS), Springer. This version discusses the algorithmic typing in detail with its full definition, a heuristics of guide insertion, and possible extensions of λBH\lambda^{BH}. The definitions omitted from the short version can also be found.

The (declarative) type system of λBH\lambda^{BH} is shown to be sound and to subsume two representative function- and argument-first bidirectional type systems. Our typing algorithm is shown to be sound with respect to the type system of λBH\lambda^{BH} and complete with respect to representative function- and argument-first systems. We mechanically prove the metatheorems using the Abella theorem prover.

1 Introduction

Bidirectional typing [12] mixes type checking, which takes a type as input and determines whether a given program has that type, and type synthesis, which returns the type of a given program, into a single process. For example, given a function application f(λx.x)f(\lambda x.x), a bidirectional type system may synthesize the type, say, (int→int)→unit(\text{int} \to \text{int}) \to \text{unit} for the function ff, then check that the argument λx.x\lambda x.x has the type int→int\text{int} \to \text{int}. Bidirectional typing has been used for implementing complex typing features, such as dependent types [15, 1], higher-rank polymorphism [11, 5], and total type error localization and recovery [21].

However, each existing bidirectional type system fixes the built-in typing order, making their typeabilities incompatible. Typing function applications is one of the main problems. Many bidirectional type systems [12, 11, 5] prefer to synthesize the type of the function first, then extract the argument type from the synthesized type, and check that the argument has the extracted type. We call this typing style function-first. By contrast, another system [18] prefers to synthesize the type of the argument first, which we call argument-first. The argument-first style checks that the given function has the synthesized type as its argument type. The gap between the function- and argument-first styles makes them incompatible, thereby forcing the language designer to select one and give up the other's typeability. Furthermore, supporting (higher-rank) polymorphism also distinguishes between the two formalizations, in terms of when polymorphic types are instantiated.

We propose a language λBH\lambda^{BH} that subsumes function- and argument-first bidirectional typing, and a typing algorithm for that system.¹ The type system of λBH\lambda^{BH} chooses either of the two styles based on “guides”, statically given at each function application². The language designer can choose the function-first (resp. argument-first) bidirectional typing at their discretion by inserting the guide for the function-first (resp. argument-first) style. For example, our framework allows the argument-first style only if a given argument is a variable and the function-first style otherwise. The existing two styles can be obtained as instances of the type system, by fixing all the guides in a program to the function-first or argument-first. Thanks to the guides, we can separate the problem of determining the typing order from a bidirectional type system and

¹BH stands for “both-handed” because our language flexibly switches between the function- and argument-first styles. ²Contextual typing [19] can also choose between the two styles, but it cannot switch between them for every function application. See Section 5.3 for more details.

leave that decision to the language designer. To show that this separation works well not only in a simple setting, λBH\lambda^{BH} supports higher-rank polymorphism.

A challenge with incorporating function- and argument-first bidirectional typing is to address partially known type information. For example, suppose that, given the expression fxyf x y, a typing process first synthesizes the type of yy, as in the argument-first style, secondly synthesizes the type of ff using yy's type, and then checks that xx has the first argument type of ff. At the point of synthesizing ff's type, the known information on ff is that ff takes two arguments, and the second argument type is the same as the type of yy. Therefore, we need a mechanism to use partially known type information, but neither the function-nor argument-first style represents such type information.

Actually, the idea of such partially known type information during type synthesis/checking has been studied in the literature [10, 16]. In Odersky et al. [10], each type constructor in a type expression is assigned one of the two colors, which stand for which part should be synthesized, while, in Vytiniotis et al. [16], boxes in a type expression specify which part of a type should be synthesized. For instance, a colored type unit→unit\text{unit} \to \text{unit} and a boxy type unit→int\text{unit} \to \text{int} mean that the argument type should be synthesized, resulting in unit, and the other parts are given for checking. The notation used in this paper is based on boxy types, because we adopt the restriction on boxy types that no synthesized part of a type occurs within a checking part.³ The distinction between synthesized and checked parts in a type expression is helpful to unify function- and argument-first bidirectional typing. For example, recall the above typing process for the expression fxyf x y. The boxy type A1→A2→B\boxed{A_1} \to A_2 \to \boxed{B} indicates that the part outside the boxes consists of the known type information for synthesizing ff's type, that is, the second argument yy's type is A2A_2.

The type system of λBH\lambda^{BH} is declarative in that it guesses types for universal quantifiers, and predicative in that the guessed types are only monotypes. Although the bidirectional type system in the simply typed setting [4] immediately leads to its typing algorithm, bidirectional type systems [11, 5, 18] for higher-rank polymorphism usually rely on guessing oracles. Furthermore, those systems restrict the guessed types to monomorphic types, making their typing algorithms simple and decidable. The type system of λBH\lambda^{BH} follows these design decisions⁴.

We provide a typing algorithm for the declarative type system. Our typing algorithm is based on the worklist approach introduced by Zhao et al. [24]. Their typing algorithm is for the function-first style bidirectional type system proposed by Dunfield and Krishnaswami [5]. Still, we show that its core idea is also applicable to the argument-first style, and even to the unified style.

The metatheory of λBH\lambda^{BH} is classified into declarative and algorithmic parts. From the declarative point of view, the type system of λBH\lambda^{BH} is sound with respect to System F extended with type coercions [3], and complete with respect to the

³In colored types, synthesized and checked parts can be arbitrarily nested.

⁴Section 5.2 discusses the choice to follow ML-style type systems supporting first-class polymorphism.

function-first bidirectional type system by Dunfield and Krishnaswami [5] and the argument-first one by Xie and Oliveira [18]. From the algorithmic point of view, our typing algorithm is sound with respect to the type system of λBH\lambda^{BH}. Although it is not quite complete with respect to the type system of λBH\lambda^{BH}, it is complete with respect to weaker systems—the two bidirectional type systems of Dunfield and Krishnaswami [5] and Xie and Oliveira [18].

The contributions of this work are summarized as follows.

  • We provide a unified type system that subsumes both the function- and argument-first bidirectional type systems with higher-rank polymorphism using boxy types. Our language λBH\lambda^{BH} serves as an intermediate language in that a language designer can use their own approach to determine whether to choose function- or argument-first bidirectional typing and reflect the choice in compiling their language to λBH\lambda^{BH}.

  • We provide a sound typing algorithm with respect to the type system of λBH\lambda^{BH}. It is also complete with respect to existing function- and argument-first bidirectional type systems. This algorithm is independent of which of the two styles a language designer chooses.

  • We mechanically formalize both declarative and algorithmic typing and prove the aforementioned soundness and completeness theorems, as well as termination of the algorithm, using the Abella theorem prover [7].

In addition, we have a prototype implementation of our typing algorithm with a heuristic elaboration of an ML-like language to λBH\lambda^{BH}. It is written in OCaml and available on Zenodo at https://doi.org/10.5281/zenodo.22218838, together with the Abella proofs.

The rest of the paper is organized as follows. Section 2 reviews the technical background of our work and overviews our approach. Section 3 presents λBH\lambda^{BH}, its declarative type system, and its metatheory. Section 4 summarizes our typing algorithm for λBH\lambda^{BH} and its metatheory. Section 5 discusses related work and Section 6 concludes.