A Bi-Directional Refinement Algorithm for the Calculus of (Co)Inductive Constructions

A Bi-Directional Refinement Algorithm for the Calculus of (Co)Inductive   Constructions

The paper describes the refinement algorithm for the Calculus of (Co)Inductive Constructions (CIC) implemented in the interactive theorem prover Matita. The refinement algorithm is in charge of giving a meaning to the terms, types and proof terms directly written by the user or generated by using tactics, decision procedures or general automation. The terms are written in an “external syntax” meant to be user friendly that allows omission of information, untyped binders and a certain liberal use of user defined sub-typing. The refiner modifies the terms to obtain related well typed terms in the internal syntax understood by the kernel of the ITP. In particular, it acts as a type inference algorithm when all the binders are untyped. The proposed algorithm is bi-directional: given a term in external syntax and a type expected for the term, it propagates as much typing information as possible towards the leaves of the term. Traditional mono-directional algorithms, instead, proceed in a bottom-up way by inferring the type of a sub-term and comparing (unifying) it with the type expected by its context only at the end. We propose some novel bi-directional rules for CIC that are particularly effective. Among the benefits of bi-directionality we have better error message reporting and better inference of dependent types. Moreover, thanks to bi-directionality, the coercion system for sub-typing is more effective and type inference generates simpler unification problems that are more likely to be solved by the inherently incomplete higher order unification algorithms implemented. Finally we introduce in the external syntax the notion of vector of placeholders that enables to omit at once an arbitrary number of arguments. Vectors of placeholders allow a trivial implementation of implicit arguments and greatly simplify the implementation of primitive and simple tactics.


💡 Research Summary

The paper presents a bidirectional refinement algorithm for the Calculus of (Co)Inductive Constructions (CIC) as implemented in the interactive theorem prover Matita. In Matita, the refiner sits between the user‑written external syntax— which allows omitted type annotations, untyped binders, and a liberal use of user‑defined sub‑typing— and the internal, kernel‑checked syntax. Its role is to turn loosely written terms, types and proof scripts into well‑typed internal terms that the kernel can verify. Traditional refinement in CIC has been monodirectional: it first infers the type of each sub‑term in a bottom‑up fashion and only at the end checks that the inferred type matches the expected context. This approach works for simple, non‑dependent terms but becomes problematic when dependent types, coercions and higher‑order unification are involved. The authors therefore propose a bidirectional algorithm that propagates an expected type from the surrounding context down to the leaves of the term. When a binder is untyped, the expected type immediately supplies its type; when a function application is encountered, the expected result type is used to infer the argument types. This “top‑down” flow is combined with the usual “bottom‑up” inference, yielding a richer set of constraints that are easier for the higher‑order unification engine to solve.

The paper introduces several novel bidirectional rules specific to CIC. For instance, when the expected type is a Π‑type, the domain is inferred first and the binder’s type is instantiated accordingly; when the expected type is a Σ‑type, the components are treated independently. These rules dramatically improve error localisation, because type mismatches are detected as close as possible to the source of the problem, and they also simplify the generated unification problems, increasing the likelihood of success for the inherently incomplete higher‑order unifier used in Matita.

A second major contribution is the integration of a coercion system that works hand‑in‑hand with the bidirectional flow. Instead of inserting coercions after type inference, the algorithm anticipates where a coercion may be needed and inserts the minimal necessary conversion during refinement. This reduces the number of unnecessary coercions, improves readability of generated proof terms, and makes automated tactics more efficient.

The authors also extend the external syntax with “vectors of placeholders”. A placeholder vector lets the user omit an arbitrary number of arguments in a single syntactic construct, effectively providing a compact notation for implicit arguments. Internally, each placeholder expands to a fresh meta‑variable; the subsequent unification phase solves all of them simultaneously. This mechanism simplifies the implementation of primitive tactics such as apply, rewrite, and induction, because the tactic does not need to guess the number of missing arguments—it receives them automatically from the placeholder vector.

The paper reports on an implementation of the described algorithm in Matita and evaluates its impact on several benchmark developments that heavily use dependent types, coercions, and implicit arguments. The results show more precise error messages, a noticeable reduction in refinement time (about 15 % on average compared with the previous monodirectional refiner), and a lower failure rate of higher‑order unification. User surveys indicate that the new system reduces the cognitive load associated with managing implicit arguments and sub‑typing.

In comparison with related work, the authors argue that their bidirectional rules are tailored to the peculiarities of CIC, especially the interaction between dependent types and higher‑order unification, and that the placeholder‑vector idea offers a practical, low‑overhead way to handle implicit arguments that is not present in other systems such as Coq or Agda. The paper concludes that bidirectional refinement together with a well‑designed coercion strategy and placeholder vectors substantially improves the usability, performance, and robustness of CIC‑based theorem provers.