A relation is the extension of a predicate. That is, if you have a statement whose truth value may depend on some variables, then you get a relation that consists of those instantiations of the variables that make the statement true. Equivalently, you can think of a relation as a function whose target is the set of truth values.
Given a family of sets, a relation on that family is a subset of the cartesian product . Equivalently, a relation is a function from to the set of truth values (because is the subobject classifier in Set).
A nullary relation is a relation on the empty family of sets. This is the same as a truth value.
A unary relation on is a relation on the singleton family . This is the same as a subset of .
A binary relation on and is a relation on the family , that is a subset of . This is also called a relation from to , especially in the context of the -category Rel described below.
A binary relation on is a relation on , that is a relation from to itself. This is sometimes called simply a relation on .
An -ary relation on is a relation on a family of copies of , that is a subset of .
For a binary relation, one often uses a symbol such as and writes instead of . Actually, even when a relation is given by a letter such as , one often sees instead of , although now that does not look so good.
If and are each sets equipped with a relation, then what makes a function a morphism of sets so equipped?
There are really two ways to do this, shown below. (We will write these as if each set is equipped with a binary relation , but any fixed arity would work.)
Now, if is a bijection, then it preserves the relation if and only if its inverse reflects it, so clearly an isomorphism of relation-equipped sets should do both. What about a mere morphism?
In general, it's more natural to require only preservation; these are the morphisms you get if you consider a set with a relation as a models of a finite-limit theory or a simply directed graph.
But in some contexts, particularly when dealing only with irreflexive relations, we instead require (only) that a morphism reflect the relation. Sometimes an even stricter condition is imposed, as for well-orders. But even in these cases, the definition of isomorphism comes out the same.
Binary relations are especially widely used.
Special kinds of relations from to include:
A function can be seen as a binary relation that is both functional and entire.
Special kinds of binary relations on additionally include:
Combinations of the above properties of binary relations produce equivalence relations, apartness relations, and the various kinds of orders.
Binary relations form a -category (in fact a -poset) Rel, which is the basic example of an allegory.
The objects are sets, the morphisms from to are the binary relations on and , and there is a -morphism from to (both from to ) if implies (that is, when , then ).
The interesting definition is composition: If is a relation from to and is a relation from to , then the composite (written or ) from to is defined as follows:
The identity morphism is given by equality.
The special properties of the kinds of binary relations listed earlier can all be described in terms internal to ; most of them make sense in any allegory. Irreflexive and asymmetric relations are most useful if the allegory's hom-posets have bottom elements, and linear relations require this. Comparisons require the hom-posets to have finite unions, and well-founded relations require some sort of higher-order structure.
As a function may be seen as a functional, entire relation, so the category Set of sets and functions is a subcategory of Rel (in fact a replete and locally full sub--category).
Most of the preceding makes sense in any category with enough products; giving rise to internal relations. Probably the trickiest bit is the definition of composition of binary relations, so not every category with finite products has an allegory of relations. In fact, in a certain precise sense, a category has an allegory of relations if and only if it is regular. It can then be recovered from this allegory by looking at the functional and entire relations.