Mappings That Preserve Structure

A category as objects, arrows and a composition satisfying two laws; a functor as a mapping that preserves those laws rather than merely matching up objects; the composite that detects a mapping which looks structural and is not; and naturality as a square that has to commute at every arrow.

Definition

A category consists of objects, arrows each with a source and target object, a composition assigning to arrows f : A → B and g : B → C an arrow g ∘ f : A → C , and an identity arrow id A for each object, subject to two laws:

h ∘ ( g ∘ f ) = ( h ∘ g ) ∘ f , f ∘ id A = f = id B ∘ f .

A functor F from category C to D maps objects to objects and arrows to arrows, preserving sources and targets, and satisfies

F ( id A ) = id F ( A ) , F ( g ∘ f ) = F ( g ) ∘ F ( f ) .

The second law is what makes the mapping structural. A map can send every object somewhere sensible and give every arrow the right endpoints while failing it.

A natural transformation α between functors F , G : C → D assigns to each object A an arrow α A : F ( A ) → G ( A ) such that for every arrow f : A → B ,

α B ∘ F ( f ) = G ( f ) ∘ α A .

This is the naturality square, and it is a condition at every arrow, not a property of the diagram being drawable.

In programming. Types and functions form a category. A type constructor with a map operation is a functor when map id = id and map (g . f) = map g . map f; a polymorphic function whose behaviour does not inspect the contained type is a natural transformation, and its naturality square is the statement that mapping before or after the function gives the same result.

Assumptions and scope

  • Composition must be defined exactly when the target of one arrow is the source of the next. A 'category' whose composition table pairs arrows that do not meet is not a category, and the error is in the data rather than in the laws.

  • Associativity and the identity laws are checked over every composable triple and every arrow. Verifying a sample establishes nothing, since a single failing triple refutes the structure.

  • A functor preserves sources and targets by definition, so a mapping violating that is not a candidate functor at all. The interesting failures are mappings that satisfy the endpoint conditions and break the composition law.

  • Naturality is a family of equations indexed by arrows, not by objects. A transformation can satisfy the square at several arrows and fail at another, so the check is not complete until every arrow has been examined.

  • A degenerate example can satisfy the laws for uninteresting reasons. When both functors are constant, every naturality square reduces to the same equation, which verifies the mechanics of the check without testing whether the two paths could differ.

  • In a programming setting the laws are claims about a concrete implementation, and a map that reorders or duplicates elements can break them while type-checking. The compiler enforces the types and not the laws.

Worked material

Example

Five structures and whether the laws hold

A partial order as a category. Objects are the elements; there is one arrow a → b exactly when a ≤ b . Composition is transitivity, identities are reflexivity, and associativity is automatic because any two arrows with the same source and target are equal. The concept hierarchy of the knowledge-representation unit is a category in this sense, and a monotone map between two such orders is precisely a functor.

Lists with map. Objects are types, arrows are functions, and the list constructor with map is a functor when map id = id and map (g . f) = map g . map f. Both hold for the standard implementation. A variant that reverses the list while mapping type-checks identically and breaks the first law, since mapping the identity would reverse.

A monoid as a one-object category. One object, one arrow per element, composition is the monoid operation. Associativity of the category is associativity of the monoid; the identity arrow is the unit. A functor between two such categories is exactly a monoid homomorphism. The categorical laws reduce to the algebraic ones with nothing left over.

A mapping that collapses everything. Send every object of C to a single object X and every arrow to id X . The identity law holds because every identity is sent to id X , which is the identity at the only object in the image. The composition law holds because both sides reduce to the same arrow whatever was composed: F ( g ∘ f ) = id X , and F ( g ) ∘ F ( f ) = id X ∘ id X = id X . So this is a functor. It is uninformative and entirely legitimate, which shows that satisfying the laws is a minimum rather than a merit.

reverse on lists as a natural transformation. For each type T , reverse maps a list of T to a list of T . Naturality says map f . reverse = reverse . map f, which holds because reversing rearranges positions without examining elements. Contrast sort, which inspects elements: map f . sort and sort . map f differ as soon as f does not preserve the ordering, so sort is not natural.

---

The first three are cases where familiar structures turn out to be categories and familiar maps turn out to be functors, so the laws recover what was already known. The fourth shows the laws are a floor, not a distinction. The fifth is the one that bites: two functions of the same type, one natural and one not, distinguished by an equation rather than by their signatures.

Contrast

Pairs that differ in one respect

F against G , on the same category.

F G
object map sensibleyesyes
endpoints correctyesyes
F ( id ) = id yesyes
image of g ∘ f h id Y
composite of images h h
a functoryesno

The first four rows are identical. Only the fifth comparison separates them, and it is the only row that could not be settled by inspection.

The typing condition against the composition law.

A mapping sending f : A → B to an arrow X → Z when F ( B ) = Y fails the typing condition, and the composite F ( g ) ∘ F ( f ) is then not even defined. There is nothing to check. That is a malformed candidate. A mapping passing the typing condition and failing composition is a well-formed candidate that is false, which is the more instructive failure and the harder one to spot.

Collapsing against contradicting.

F sends two objects to one and a non-identity arrow to an identity, losing information, and remains a functor. G assigns to g f something incompatible with its assignments to f and g . Losing information is permitted; being inconsistent about composites is not.

reverse against sort.

Both have the same shape: for each type, a function from lists of that type to lists of that type. reverse satisfies map f . reverse = reverse . map f because it moves elements without looking at them. sort does not, because it compares them, so mapping first can change the order it produces. Identical signatures, different answers to an equation.

A drawn square against a commuting square.

Every naturality square can be drawn: the four arrows exist and their endpoints match by construction. Whether the two routes are the same arrow is a separate question, settled by evaluating both. Drawing establishes that the question is well posed, not that the answer is yes.

A check that could fail against one that could not.

Verifying naturality between two constant functors gives agreement at every arrow, and both routes reduce to the same arrow whatever the transformation, so no assignment could have failed. Verifying between functors that act differently on arrows is a check with a possible negative outcome. Only the second is evidence.

Common errors

Common misconception

That a mapping is a functor once it sends objects to objects and arrows to arrows with matching sources and targets. Those conditions make it a candidate; the composition law decides. In the worked example a mapping G sends every object somewhere sensible and gives every arrow correct endpoints, yet G ( g ∘ f ) = id Y while G ( g ) ∘ G ( f ) = id Y ∘ h = h . The two differ, so G is not a functor, and nothing about the object map or the endpoints revealed it. The same holds in programming: a type constructor with a map that type-checks may still break map (g . f) = map g . map f, because the compiler enforces the types and not the laws.

Common misconception

That drawing the naturality square establishes naturality. Every such square can be drawn: the arrows α A , α B , F ( f ) and G ( f ) exist by definition and their endpoints always line up. What naturality asserts is that the two routes around the square are the same arrow, α B ∘ F ( f ) = G ( f ) ∘ α A , and that is an equation to be evaluated at every arrow of the source category rather than a property of the picture. A transformation can satisfy the equation at several arrows and fail at another, so a check is incomplete until each arrow has been examined. Verifying it on a case where both functors are constant confirms the mechanics of the check and tests nothing, since every square then reduces to the same equation.

Related units

Requires

Connected

Learn this topic

Used in

Sources

Results update as you type. Use the up and down arrow keys to move between results, Enter to open one, and Escape to close.

Type to search.

Settings

Appearance

Interface density

Your record

Your progress is stored in this browser and nowhere else: an identifier, the answers you have given, the mastery states and review schedule derived from them, and the lesson you last opened. Clearing it makes you a new learner on this device. It cannot be undone, and it will not affect your appearance or density settings.

Focus timer

Focus--minutes remaining

Phase

Kept in this browser only, and used to label the session in your own history.

Today

Nothing recorded yet. Finish a focus session and it will appear here.

Settings

Focus sessions between long breaks.

Sessions you are aiming for in a day.

Notifications

Your history

Sessions are stored in this browser and nowhere else. They are not evidence and never reach your mastery record.