Skip to the content.
← Index Next: Structures with type parameters →

Recall

Formal definitions cited in this section, gathered here for quick reference (full citations in the Bibliography):


A structure groups several pieces of data under one name. We will use this Lean feature constantly once we define groups and rings.

structure Point where
  x : Nat
  y : Nat

def origin : Point := { x := 0, y := 0 }

#eval origin.x        -- 0

Key points:

def shift (p : Point) (dx dy : Nat) : Point :=
  { x := p.x + dx, y := p.y + dy }

#eval (shift origin 3 4).y   -- 4

shift shows a structure used on both sides of a function: it takes a Point in (reading its fields back out with the same p.x/p.y projection notation) and builds a new one via { x := ..., y := ... }, the same field-naming syntax origin used above.

Note also that structures can bundle proofs alongside data, not just data. This is exactly how a group will be defined later — a carrier type, an operation, and proofs that the operation satisfies the group axioms, all in one structure.

Mathematical reading. structure Point where x : Nat; y : Nat is the Cartesian product $\mathrm{Point} = \mathbb{N} \times \mathbb{N}$, with x and y playing the role of the two projections $\pi_1, \pi_2 : \mathrm{Point} \to \mathbb{N}$. Here p.x computes $\pi_1(p)$, p.y computes $\pi_2(p)$, and { x := ..., y := ... } builds an element via the universal property of the product (a pair of maps into the factors determines a unique map into the product). More generally, a structure with fields of types $A_1, \ldots, A_n$ is the $n$-fold product $A_1 \times \cdots \times A_n$. Once fields are allowed to be proofs (propositions), the same construction becomes a subset cut out by conditions: a structure { data : D, proof : P data } is the dependent pair (subset) ${\, d \in D \mid P(d) \,}$, categorically a subobject of $D$.

Here is the universal property itself, in this box’s own notation: the unique $h$ making both triangles commute is exactly what { x := ..., y := ... } constructs when $f$ and $g$ are $p$’s two argument expressions.

graph TD
    C -->|f| A
    C -->|g| B
    C -.->|"exists! h"| AB["A x B"]
    AB -->|p_A| A
    AB -->|p_B| B

References

Full citations in the Bibliography. Formal definitions and verbatim quotes are gathered in Recall, above.


← Index Next: Structures with type parameters →
Try Lean
Lean playground · v1.4.18