Unit-distance count for a finite planar point set
DefinitionunitDistFor a finite set P in the Euclidean plane, unitDist(P) is the natural-number quotient by 2 of the number of ordered pairs of distinct points of P whose Euclidean distance is exactly 1. By symmetry this is the usual count of unordered unit-distance pairs.
import Mathlib open Classical noncomputable def unitDist (P : Finset (EuclideanSpace ℝ (Fin 2))) : ℕ := (P.offDiag.filter (fun pq => dist pq.1 pq.2 = 1)).card / 2
Read-back
What the Lean code literally says, in plain math · claude-opus-5
Read-back. The declaration introduces a function named unitDist which takes one explicit argument — a finite set (a Finset, i.e. a finite, duplicate-free collection) of points of the space , that is, the set of functions from the two-element index type to equipped with the (Euclidean) norm, so that the distance used is — and returns a natural number; the function is declared noncomputable and the file opens Classical, so the decidability of the filtering predicate below is supplied by classical choice rather than by an algorithm, which affects only computability and not the mathematical value. The returned number is computed as follows: one forms , the finite set of ordered pairs with , and (the product with the diagonal removed; note that both and occur); one keeps exactly those pairs whose first and second components are at Euclidean distance exactly , i.e. as an equality of real numbers (not an approximation and not an inequality); one takes the cardinality of that filtered finite set, a natural number; and finally one divides that cardinality by using natural-number division, which truncates toward zero, so the value is
No hypotheses whatsoever are imposed on : it may be empty or a singleton, in which case the off-diagonal is empty and the value is ; it may contain no pair at distance , again giving ; and because the count ranges over ordered pairs while the divisor is , the definition returns half of the number of ordered unit-distance pairs, with any odd remainder silently discarded by the floor of natural division rather than flagged as an error. Distance is taken between the two components of each pair only, with no further geometric condition (no collinearity, general position, or distinctness beyond ), and the ambient space is fixed to be two-dimensional real Euclidean space; the definition is a plain function with no output specification, no quantifiers other than the single argument , and no typeclass hypotheses beyond those already fixed by the type and by Finset's requirement that be finite.
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.