Formalizing relations in type theory

Type theory plays an important role in foundations of mathematics as a framework for formalizing mathematics and a base for proof assistants providing semi-automatic proof checking and construction. Derivation of each theorem in type theory results in a formal term encapsulating the whole proof process. In this paper we use a variant of type theory, namely the Calculus of Constructions with Definitions, to formalize the standard theory of binary relations. This includes basic operations on relations, criteria for special properties of relations, invariance of these properties under the basic operations, equivalence relation, well-ordering, and transfinite induction. Definitions and proofs are presented as flag-style derivations.


Introduction
Type theories were developed as alternatives to set theory for the foundation of mathematics. Important type theories were introduced by A. Church and P. Martin-Lof; they are typed λ-calculus (see, for example, Barendregt (2012)) and intuitionistic type theory (see, for example, Granstrom (2011)). There are several higher-order variants of typed λ-calculus, such as Calculus of Constructions (CoC) and Calculus of Inductive Constructions (CIC) (Bertot and Casteran, 2013). These variants make formal bases of proof assistants, which are computer tools for formalizing and developing mathematics. In particular, the well-known proof assistant Coq (Coq Development Team, 2021) is based on the CIC.
This study uses the variant λD of CoC developed by Nederpelt and Geuvers (2014); λD is called the Calculus of Constructions with Definitions. λD is chosen because of its following useful properties described in their book.
 In λD, as in other variants of CoC, proofs are expressed as formal terms and thus are incorporated into the system.  In λD type checking is decidable and therefore proof checking is decidable. So the correctness of a proof can be checked by an algorithm.  λD is strongly normalizing, which implies the logical consistency of this theory, even with classical logic (when no extra axioms are added).
The theory λD is weaker than CIC because λD does not have inductive types. This does not limit its capability for formalizing mathematics because in λD one can use an axiomatic approach and higher-order logic to express the objects that CIC defines with inductive types.
In these formalizations, the author aims to keep the language and theorems as close as possible to the ones of standard mathematics. Definitions and proofs in this study use the flag-style derivation described in Nederpelt and Geuvers (2014). Long formal derivations are moved from the main text to Appendices for better readability. Nederpelt and Geuvers (2014) developed a formal theory λD and formalized some parts of logic and mathematics in it. The main features of λD are briefly described below.

Type Theory λD
The language of λD described in Nederpelt and Geuvers (2014) has an infinite set of variables, V, and an infinite set of constants, C; these two sets are disjoint. There are also special symbols □ and * .

Definition 2.1
Expressions of the language are defined recursively as follows.
(1) Each variable is an expression.
(2) Each constant is an expression.
(3) The constant * is an expression. (4) The constant □ is an expression.  (4) A primitive definition is of the form: where, x ̅ , A̅ , and c are described the same way as in (3), and N is an expression. The symbol ╨ denotes the nonexisting definiens. Primitive definitions are used for introducing axioms where no proof terms are needed. where, M and N are expressions of the language, ∆ is an environment (a properly constructed sequence of definitions) and Γ is a context (a properly constructed sequence of declarations).
For brevity, most definitions use implicit variables by omitting the previously declared variables x̅ in c(x ̅ ) in (3) and (4).
The following informally explains the meaning of expressions.
(1) If an expression M appears in a derived statement of the form M: * , then M is interpreted as a type, which represents a set or a proposition.
Note: There is only one type * in λD. But informally * p is often used for propositions and * s for sets to make proofs more readable.
(2) If an expression M appears in a derived statement of the form M: N, where N is a type, then M is interpreted as an object at the lowest level. When N is interpreted as a set, then M is regarded as an element of this set. When N is interpreted as a proposition, then M is regarded as a proof (or a proof term) of this proposition.
(3) The symbol □ represents the highest level.
(4) Sort is * or. Letters s, s1, s2,... are used as variables for sorts. (5) If an expression M appears in a statement of the form M: □, then M is called a kind.
λD contains the derivation rule: It is an axiom (the only axiom) of λD, because it has an empty environment and an empty context.
Further details of the language and derivation rules of the theory λD can be found in Nederpelt and Geuvers (2014). Judgments are formally derived in λD using the derivation rules.

Flag Format of Derivations
The flag-style deduction was introduced by Jaskowski and Fitch; it is described in detail by Nederpelt and Kamareddine (2004), and Nederpelt and Geuvers (2014). In short, a derivation in the flag format is a linear deduction. Each "flag" (a rectangular box) contains a declaration that introduces a variable or an assumption; a collection of already introduced variables and assumptions makes the current context. The scope of the variable or assumption is established by the "flag pole". In the scope, one constructs definitions and proof terms for proving statements/theorems in λD. Each new flag extends the context and at the end of each flag pole, the context is reduced by the corresponding declaration. For brevity, several declarations can be combined in one flag.

Logic in λD
The rules of intuitionistic logic are derived from the theory λD as shown in Nederpelt and Geuvers (2014). These are briefly described below by showing the introduction and elimination rules for logical connectives and quantifiers.

Implication
The logical implication A ⇒ B is identified with the arrow type A → B. The rules for implication follow the following general rules for the arrow type (here they are written in the flag format):

Falsity and Negation
Falsity ⊥ is introduced in λD by: ⊥ := ΠA : * p.A : * p. From this definition one gets a rule for falsity: The rule states that falsity implies any proposition. As usual, negation is defined by: ¬A: = A → ⊥. Other logical connectives and quantifiers are also defined using second-order encoding. Here only a list of their derived rules and names of the corresponding terms are provided, without details of their construction. The exact values of the terms can be found in Nederpelt and Geuvers (2014).
Some of our flag derivations contain the proof terms that will be re-used in other proofs; such proof terms are written in bold font, e.g., ∧-in in the first derived rule for conjunction as follows.

Conjunction
These are derived rules for conjunction ∧:

Disjunction
These are derived rules for disjunction ∨:

Bi-Implication
Bi-implication ⇔ has the standard definition:

Lemma 2.3.
This lemma will be often used to prove bi-implication A ⇔ B.

Universal Quantifier
The universal quantifier ∀ is defined through the dependent product:

Existential Quantifier
These are derived rules for the existential quantifier ∃: Here x is not a free variable in C.

Classical Logic
This study uses mostly intuitionistic logic. But sometimes classical logic is needed; in these cases, the following Axiom of Excluded Third is added:

Intensional Equality in λD
This section introduces intensional equality for elements of any type; it is called just equality. The next section will introduce extensional equality and the axiom of extensionality relating to the two types of equality.

Properties of Equality Reflexivity
The following diagram proves the reflexivity property of equality in λD: Proof terms are constructed similarly for the following properties of Substitutivity, Congruence, Symmetry, and Transitivity (see Nederpelt and Geuvers (2014)).

Substitutivity
Substitutivity means that equality is consistent with predicates of corresponding types.

Congruence
Congruence means that equality is consistent with functions of corresponding types.

Symmetry
The following diagram expresses the symmetry property of equality in λD.

Transitivity
The following diagram expresses the transitivity property of equality in λD.

Sets in λD
Below are some definitions from Nederpelt and Geuvers (2014) relating to sets, in particular, subsets of type S: Thus, a subset V of S is regarded as a predicate on S and xεV means x satisfies the predicate V.

Defining Binary Relations in λD
Binary relations are introduced in Nederpelt and Geuvers (2014), together with the properties of reflexivity, symmetry, antisymmetry, and transitivity, and definitions of equivalence relation and partial order. These are used here as a starting point for formalizing the theory of binary relations in λD.
A relation on S is a binary predicate on S, which is regarded in λD as a composition of unary predicates. The type br(S) of all binary relations on S is introduced below, for brevity: In the rest of the article, binary relations are called just relations. The equality of relations and operations on relations are defined similarly to the set equality and set operations.
Next, the extensional equality of relations is defined vs the intentional equality introduced in the previous section.
The following axiom of extensionality for relations is added to the theory λD.
The axiom is introduced in the last line by a primitive definition with the symbol ╨ replacing a non-existing proof term. The Extensionality Axiom states that the two types of equality are the same for binary relations. So the symbol = will be used for both without elaborating on details of applying the axiom of extensionality, when converting one type of equality to the other.

Operations on Binary Relations
The flag format is used to introduce the identity relation idS on type S and converse R −1 of a relation R: Next, the operations of union ∪, intersection ∩, and composition • of relations are introduced:

Properties of Operations
The following two technical lemmas will be used in some future proofs.

Lemma 4.1.
This lemma gives a shortcut for constructing an element of a composite relation.

Lemma 4.2.
This lemma gives a shortcut for proving the equality of two relations:

Theorem 4.3.
For relations R, P, and Q on type S the following hold: The formal proof is in Appendix A. The proof of part 2) has the form: Its proof term conv-prod (S, R, Q) will be re-used later in the paper.

Properties of Binary Relations
The properties of reflexivity, symmetry, antisymmetry, transitivity and the relations of equivalence and partial order are defined in Nederpelt and Geuvers (2014) as follows.
Suppose R is a relation on type S. Then the following hold. 1) Criterion of reflexivity. R is reflexive ⇔ idS ⊆ R.

2) First criterion of symmetry. R is symmetric
The formal proof is in Appendix B. The proof of part 3) has the form: Its proof term sym-criterion (S, R) will be re-used later in the paper.

Theorem 5.2.
Relation R on S is reflexive, symmetric, and antisymmetric ⇒ R = idS.
Proof. The formal proof is in the following flag diagram.

Theorem 5.3. Invariance under converse operation.
Suppose R is a relation on type S. Then the following hold: 3)

□ Theorem 5.4. Invariance under intersection.
Suppose R and Q are relations on type S. Then the following hold. 3)

□ Theorem 5.5. Invariance under union.
Suppose R and Q are relations on type S. Then the following hold.
Suppose R and Q are relations on type S. Then the following hold.
3) The derivation below uses the proof term symcriterion (S, R) from Theorem 5.1.3) for the second criterion of symmetry and the proof term conv-prod from Theorem 4.3.2).

Equivalence Relation and Partition
Theorem 6.1. Invariance of equivalence relation under converse operation and intersection.
Suppose R and Q are equivalence relations on type S. Then the following hold. 1) R −1 is an equivalence relation on S.
2) R ∩ Q is an equivalence relation on S.
The formal proofs are skipped. □ Next is a formalization of the fact that there is a correspondence between equivalence relations on S and partitions of S. Equivalence classes are introduced in Nederpelt and Geuvers (2014) as follows.
Next, a partition of type S is defined: As usual, one can regard a partition R as a collection Rx (x ∈ S) of subsets of S. From this point of view, the above diagram expresses the standard two facts for a partition: (1) any element of S belongs to one of the subsets from the collection (namely Rx); (2) if intersection of two subsets Rx and Ry is non-empty, then they coincide.
(1) implies that each subset from the collection is nonempty and that the union of all subsets from the collection is S. Theorem 6.2.
Any equivalence relation R on type S is a partition of S and vice versa.
Proof. The type of partitions of S is S → ps(S), which is S → S → * p, and it is the same as the type br(S) of relations on S. The proof consists of two steps.
Step 1. Any equivalence relation is a partition.
This proves the first part of the definition of partition (S, R), and the second part was proven in Nederpelt and Geuvers (2014), pg. 291.
Step 2. Any partition is an equivalence relation. □ 6.2. Partial Order Theorem 6.3. Invariance of partial order under converse operation and intersection.
Suppose R and Q are partial orders on type S. Then the following hold. 1) R −1 is a partial order on S. 2) R ∩ Q is a partial order on S.

Example 6.4
⊆ is a partial order on the power set ps(S) of type S.

Proof.
This is the formal proof. □

Well-Ordering and Transfinite Induction
Notation ≤ will be used for a partial order. The following diagram defines the strict order <, the least element of a partially ordered set, and the well-ordering of type S. Theorem 6.5. Transfinite Induction.
Suppose ≤ is a well-ordering of type S. Then for any predicate P on S: ∀x: S.[(∀y : S.(y < x ⇒ Py) ⇒ Px] ⇒ ∀x : S.Px.

Proof
Here is the formal proof.
Here the Double Negation theorem is used (twice) with the proof term doub-neg. This is the only place in this study where classical (not intuitionistic) logic is used.

Conclusion
Starting with the definitions from Nederpelt and Geuvers (2014) of binary relations and properties of reflexivity, symmetry, antisymmetry, and transitivity, this study formalizes in the theory λD (the Calculus of Constructions with Definitions) criteria for these properties and proves their invariance under operations of union, intersection, composition, and taking converse. The author provides a formal definition of partition and formally proves correspondence between equivalence relations and partitions. The author derives formal proof that ⊆ is a partial order on the power set. Finally, the author formally proves the principle of transfinite induction for a type with well-ordering.
The results can be transferred to the proof assistants that are based on the Calculus of Constructions. Since binary relation is an abstract concept used in many areas of mathematics, the results can be useful for further formalizations of mathematics in λD. Next direction of research is formalization of parts of probability theory in λD that was outlined in Kachapova (2018).