diff --git a/src/v4/std/integer.dag b/src/v4/std/integer.dag index cba1bf07ad5..1fe242ac3b8 100644 --- a/src/v4/std/integer.dag +++ b/src/v4/std/integer.dag @@ -17,10 +17,14 @@ // (Result-return / refinement-precondition — STRUCTURE.md commitment #2) // // Consumes: -// - std/node.dag: Instantiation +// - std/node.dag: Instantiation, Symbol // - std/nat.dag: Nat -// - std/machine.dag: MachineWidth -// - std/algebra.dag: OrderedRing, AbelianGroup +// - std/machine.dag: MachineWidth, PointerWidth, Word8, Word16, +// Word32, Word64, Word128 +// - std/algebra.dag: Magma, Semigroup, Monoid, Group, AbelianGroup, +// Ring, OrderedRing, Ordering +// - std/diagnostic.dag: Diagnostic, Locus, Correction, +// NoCorrectionReason, Outcome for Tier-2 divide/modulo // // D2 (operator-ratified 2026-05-16; see DECISIONS.md D2) — this file is // the SINGLE authority for integer algebra inhabitance. `Int` inhabits @@ -44,12 +48,257 @@ // selects one via its alias target plus a sibling `OverflowDisposition` // carrier declared in its own `extdeps` file. // -// Status: scaffold — fill per TASKS.md T-3 (scalar/numeric stack). Replaces -// part of the deleted std/primitive.dag; see src/v4/STRUCTURE.md -// §"scalar/numeric concept decomposition". +// Status: T-3 modeled 2026-05-16 (jolly-gull-889). Replaces part of the +// deleted std/primitive.dag; see src/v4/STRUCTURE.md §"scalar/numeric +// concept decomposition". +// HEADER RECONCILE (2026-05-16, #3190, smart-fox-602 + operator reason +// ruling): +// the frozen scaffold header understated delivered imports and remained +// marked scaffold after the model landed. Consumes now names the actual +// machine-width, algebra-witness, diagnostic, and Symbol dependencies. +// The operator reason ruling also fixes integer-owned diagnostic reasons +// as Symbol name-references declared in this file, not caller-supplied +// values. // Brief: see BRIEF_TEMPLATE.md; this file's I/O contract is the immutable // contract for the worker. Splitting this file requires explicit operator // ratification (substrate extension = stop signal). module v4.std.integer + + +import v4.std.algebra { + AbelianGroup, + Group, + Magma, + Monoid, + OrderedRing, + Ordering, + Ring, + Semigroup +} +import v4.std.diagnostic { + Correction, + Diagnostic, + Locus, + NoCorrectionReason, + Outcome +} +import v4.std.node { Symbol } +import v4.std.machine { + MachineWidth, + PointerWidth, + Word8, + Word16, + Word32, + Word64, + Word128 +} +import v4.std.nat { Nat } + + +// ───────────────────────────────────────────────────────────────────── +// Modeling notes (read before validating against the anchor) +// +// V4 SPLITS CARRIER FROM WITNESS. The v3 spelling +// `Int = AbelianGroup>` named the construction chain +// compactly, but v4's scalar files use instance-values for inhabitance +// (see nat.dag / logic.dag): the concrete carrier is named first, then +// the algebra value over that carrier is declared here. Consequently: +// - `GroupCompletion` is the derived carrier construction. +// - `Int = GroupCompletion` is the abstract signed-integer carrier. +// - `data int_abelian_group` and `data int_ordered_ring` are the +// machine-readable inhabitance edges. +// +// GROUP-COMPLETION IS LOCAL TO THIS CONCEPT FILE. std/algebra.dag's v4 +// scope is "structures only" and deliberately omits construction +// carriers except FreeMonoid, whose carrier shape is itself the root +// free construction. `GroupCompletion` exists here because this file +// is the sole v4 consumer in the closed tree: the construction is needed +// to model integers from Nat, and exporting it from algebra.dag would +// widen the T-2 structures file after its brief. The carrier is opaque +// here so the quotient relation is not represented as a raw `{positive, +// negative}` record that would make non-canonical aliases observable. +// +// CONSTRAINED-INHABITANCE GAP (tracked scaffold). Denotationally the +// Grothendieck group completion requires a commutative monoid. v4 does +// not yet have a parametric where-clause syntax such as +// ` where M : CommutativeMonoid<_>`. The only authored use here is +// `GroupCompletion`, and nat.dag already declares Nat's additive +// `CommutativeMonoid` instance, so the mathematical precondition +// holds for the actual construction. Dissolution trigger: when v4 lands +// constrained generic parameters / inhabitance bounds, tighten +// `GroupCompletion` to require the commutative-monoid witness. +// +// FIXED-WIDTH INTEGERS ARE WIDTH PROJECTIONS. A machine integer is not a +// sibling algebra authority beside abstract `Int` / `UInt`; it is the +// abstract carrier composed with the independent machine-width axis +// (`Compose>`, etc.). This preserves the D2 +// fact-flow shape from INVARIANTS P1: width rows alias the abstract +// carrier plus a width refinement, and no fixed-width row declares a +// separate OrderedRing/Semiring instance in this file. +// +// TIER-2 TOTALIZATION. Integer division and modulo are partial at divisor +// zero. Per STRUCTURE.md commitment #2, the totalized operations live +// here and return std/diagnostic.dag's ratified `Outcome` carrier +// (DECISIONS.md I), not a new Result type. The source location is call- +// site context (`at: Locus`), but the failure reason is intrinsic to the +// integer dimension: integer.dag declares the Symbol name-references +// `integer_div_by_zero` and `integer_modulo_by_zero`, then uses those +// names in the produced Diagnostic. + + +// ───────────────────────────────────────────────────────────────────── +// GroupCompletion — the carrier produced by completing a commutative +// monoid to an abelian group (Grothendieck construction). Opaque at this +// layer: the quotient / canonical representative is the carrier's own +// authority, not a field-level pair whose equivalence lives only in prose. +type GroupCompletion + + +// ───────────────────────────────────────────────────────────────────── +// Int / UInt — abstract integer carriers. +// +// `Int` is the signed carrier obtained by group-completing Nat's +// additive commutative monoid. `UInt` is Nat itself: unsigned integers +// require no additive inverse, so wrapping Nat would only drop facts +// nat.dag already provides. +type Int = GroupCompletion +type UInt = Nat + + +// ───────────────────────────────────────────────────────────────────── +// Fixed-width integer projections over std/machine.dag's width axis. +// +// `Compose` is the D2 projection carrier used by the +// fixed-width aliases. It is intentionally body-less: the dimension is a +// refinement axis, not a runtime field beside the numeric value. +type Compose + +type Int8 = Compose> +type Int16 = Compose> +type Int32 = Compose> +type Int64 = Compose> +type Int128 = Compose> + +type UInt8 = Compose> +type UInt16 = Compose> +type UInt32 = Compose> +type UInt64 = Compose> +type UInt128 = Compose> + +type IntPlatform = Compose> +type UIntPlatform = Compose> + + +// ───────────────────────────────────────────────────────────────────── +// Integer algebra operations. These functions bind the kernel-ambient +// integer operations of the opaque `GroupCompletion` carrier into +// the single algebra instance-values below. There is no exposed raw +// Nat-pair quotient helper: consumers reach the operations through +// `int_ordered_ring`, and partial divide/modulo stay behind Outcome. +fn int_add(a: Int, b: Int) -> Int { + a + b +} + +fn int_negate(a: Int) -> Int { + 0 - a +} + +fn int_mul(a: Int, b: Int) -> Int { + a * b +} + +fn int_compare(a: Int, b: Int) -> Ordering { + if a < b { + Less + } else { + if a == b { + Equal + } else { + Greater + } + } +} + +fn int_is_zero(a: Int) -> Bool { + a == 0 +} + + +// ───────────────────────────────────────────────────────────────────── +// Inhabitance — Int grounds in AbelianGroup and OrderedRing. +// +// The additive group is named separately because `OrderedRing.ring.add` +// consumes the same authority; it is not re-listed inside the ring. +data int_abelian_group: AbelianGroup = AbelianGroup { + group: Group { + monoid: Monoid { + semigroup: Semigroup { magma: Magma { op: int_add } }, + identity: 0 + }, + inverse: int_negate + } +} + +data int_ordered_ring: OrderedRing = OrderedRing { + ring: Ring { + add: int_abelian_group, + mul: Monoid { + semigroup: Semigroup { magma: Magma { op: int_mul } }, + identity: 1 + } + }, + compare: int_compare +} + + +// ───────────────────────────────────────────────────────────────────── +// Diagnostic construction for integer partial operations. The `reason` +// is a Symbol name-reference owned by this integer dimension; +// diagnostic.dag owns only the schema. +data integer_div_by_zero: Symbol = integer_div_by_zero +data integer_modulo_by_zero: Symbol = integer_modulo_by_zero + +fn integer_divide_by_zero_diagnostic(at: Locus) -> Diagnostic { + Diagnostic { + reason: integer_div_by_zero, + at: at, + correction: Unavailable { reason: AmbiguousIntent } + } +} + +fn integer_modulo_by_zero_diagnostic(at: Locus) -> Diagnostic { + Diagnostic { + reason: integer_modulo_by_zero, + at: at, + correction: Unavailable { reason: AmbiguousIntent } + } +} + + +// ───────────────────────────────────────────────────────────────────── +// Tier-2 totalized integer partial operations. +// +// `int_div` and `int_mod` are total functions: divisor zero produces a +// typed `Rejected { diagnostic }`; all other cases produce the value. +// There is no sentinel integer and no panic-shaped boundary. +fn int_div(a: Int, b: Int, at: Locus) -> Outcome { + if int_is_zero(a: b) { + Rejected { + diagnostic: integer_divide_by_zero_diagnostic(at: at) + } + } else { + Produced { value: a / b } + } +} + +fn int_mod(a: Int, b: Int, at: Locus) -> Outcome { + if int_is_zero(a: b) { + Rejected { + diagnostic: integer_modulo_by_zero_diagnostic(at: at) + } + } else { + Produced { value: a % b } + } +}