Skip to content
Part IA Michaelmas Term

Function and Relation Composition

Function composition

Given functions ff and gg, function composition is written (fg)(x)=f(g(x))(f \circ g)(x) = f(g(x)). If g(x)=yg(x) = y and f(y)=zf(y) = z, then (fg)(x)=z(f \circ g)(x) = z.

The domain of fgf \circ g is those xx in the domain of gg for which g(x)g(x) lies in the domain of ff.

Relation composition

Given relations RS×TR \subseteq S \times T and QT×UQ \subseteq T \times U, their composition is:

QRS×UQ \circ R \subseteq S \times U

QR{(s,u)tT.  (s,t)R(t,u)Q}Q \circ R \equiv \{(s, u) \mid \exists t \in T.\; (s, t) \in R \land (t, u) \in Q\}

We read QRQ \circ R as ”QQ after RR”: first follow RR from ss to some tt, then follow QQ from tt to uu. The intermediate element tt is existentially quantified and does not appear in the result.

Partial functions as relations

A relation RS×TR \subseteq S \times T defines a (partial) function when:

(s,t1)R(s,t2)R    t1=t2(s, t_1) \in R \land (s, t_2) \in R \implies t_1 = t_2

That is, each ss is related to at most one tt. This is the univalence (or functional) property.

A relation uniquely defines a total function if additionally every sSs \in S has some tt with (s,t)R(s, t) \in R (the relation is left-total).

When mathematicians say “function” they typically mean a total function. A relation may give multiple answers for the same input; a partial function may give at most one; a total function gives exactly one.

PropertyRelationPartial functionTotal function
Univalued (at most one output)Not requiredRequiredRequired
Left-total (at least one output)Not requiredNot requiredRequired

Composition of functions is a special case

If RR and QQ are functions (univalued and left-total), then QRQ \circ R as defined by relation composition coincides with ordinary function composition. The existential quantification t\exists t is equivalent to function application because tt is uniquely determined.

Joins as generalised composition

If we write QRQ \circ R in join notation, identifying the TT-component as the join column:

R2=1QR \bowtie_{2=1} Q

We see that joins are a generalisation of function composition. The generalisation works in two directions:

  1. Functions to relations: joins cope with relations that are not univalued (multiple outputs per input) and not left-total (some inputs have no output).
  2. Equi-join to natural join: generalises from joining on a positional column to joining on named attributes.

Example

Let R(student,course)R(\text{student}, \text{course}) represent enrolment, and Q(course,lecturer)Q(\text{course}, \text{lecturer}) represent who teaches each course. Then QRQ \circ R gives (student,lecturer)(\text{student}, \text{lecturer}) pairs: students and the lecturers of courses they take.

In SQL:

SELECT R.student, Q.lecturer
FROM R
JOIN Q ON R.course = Q.course;

If a student takes multiple courses, they appear with multiple lecturers. If a course has no lecturer (not left-total), the student for that course contributes no row to the result under an inner join.

Summary

  • Function composition: (fg)(x)=f(g(x))(f \circ g)(x) = f(g(x)).
  • Relation composition: QR={(s,u)t.  (s,t)R(t,u)Q}Q \circ R = \{(s, u) \mid \exists t.\; (s, t) \in R \land (t, u) \in Q\}.
  • Functions are univalued relations; total functions are also left-total.
  • Function composition is a special case of relation composition.
  • Joins (RQR \bowtie Q) are a generalisation of relation composition, further generalised by named attributes and outer join variants.