In Lean 4, the `[irreducible]` attribute controls how definitions are treated by the elaborator, tactics (such as `simp`, `dsimp`, `unfold`), and type-class instance synthesis. Lean categorizes definitions into three distinct reducibility levels: 1. `reducible` (or `abbrev`): Unfolded eagerly and everywhere on demand (treated essentially as abbreviations). 2. `semireducible` (the default for `def`): Unfolded by standard tactics and pattern-matching, but skipped by expensive automation like `simp` or type-class resolution unless explicitly requested. 3. `irreducible`: Completely hidden from automatic unfolding. Lean's kernel still recognizes the definition for type-checking soundness, but tactics and type-class synthesis will _never_ expand it automatically. ### Why Use `[irreducible]`? - **Encapsulation and Abstract Data Types (ADTs):** It creates an explicit boundary, forcing proofs and client code to rely strictly on a declared API (theorems and lemmas) rather than the underlying representation. - **Performance Optimization:** It prevents the elaborator and kernel from getting bogged down trying to normalize or unfold massive, deeply nested, or computationally expensive terms, avoiding timeouts and bloated proof states. - **API Stability / Refactoring Resilience:** If you change how a function is implemented internally, downstream proofs won't break—provided the public API lemmas remain valid. ### Three Examples of Usage #### 1. Opaque Type Wrappers (Newtypes / Strong Type Safety) When you want to define a new type that wraps a primitive type (like a `String` or a `Nat`) without allowing clients to exploit its underlying representation, marking it `irreducible` prevents accidental type confusion. ```lean -- A custom version string type that cannot be implicitly treated as a raw String everywhere @[irreducible] def Version : Type := String -- If you try to assign a raw string literal directly where a Version is expected -- without proper constructors/wrappers, the elaborator won't unfold Version -- to see that it is structurally just a String. ``` #### 2. Performance Safeguards for Heavy Computations / Recursive Functions If you define a complex recursive function or a large structural constant, automatic unfolding during routine tactic applications or type-class searches can cripple performance. Marking it `irreducible` requires you to provide explicit rewriting lemmas (often paired with tools like `irreducible_def` from Mathlib). ```lean import Mathlib.Tactic.IrreducibleDef -- Define a heavy or sensitive computation as irreducible irreducible_def heavyComputation (n : Nat) : Nat := match n with | 0 => 1 | m + 1 => heavyComputation m * (m + 2) -- Lean generates `heavyComputation_def` automatically, -- ensuring `simp` doesn't inline the recursive body everywhere unless requested: example (n : Nat) : heavyComputation 0 = 1 := by rw [heavyComputation_def] ``` #### 3. Enforcing Strict API Boundaries in Algebraic Hierarchies In formal mathematics libraries (like Mathlib), complex mathematical objects (such as specialized quotients, completions, or localizations) are often marked `irreducible`. This forces proofs about them to use abstract algebraic properties (e.g., group homomorphisms or universal properties) instead of getting bogged down in low-level set-theoretic or tuple representations. ```lean -- Abstracting an internal representation of a specialized ring completion @[irreducible] def ComplexExtension (R : Type u) [CommRing R] : Type u := -- underlying heavy quotient type implementation hidden from clients PLift R -- Users must interact with ComplexExtension strictly via its designated -- API theorems rather than unfolding it into its PLift R definition. ```