Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
232 changes: 232 additions & 0 deletions src/v4/std/integer.dag
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
// - std/nat.dag: Nat
// - std/machine.dag: MachineWidth
// - std/algebra.dag: OrderedRing, AbelianGroup
// - std/diagnostic.dag: Outcome<T> for Tier-2 divide/modulo
//
// Status: scaffold — fill per TASKS.md T-3 (scalar/numeric stack). Replaces
// part of the deleted std/primitive.dag; see src/v4/STRUCTURE.md
Expand All @@ -31,3 +32,234 @@


module v4.std.integer


import v4.std.algebra {
AbelianGroup,
Group,
Magma,
Monoid,
OrderedRing,
Ordering,
Ring,
Semigroup
}
import v4.std.diagnostic {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BLOCKING: Outcome, Diagnostic, Locus, Unavailable, and AmbiguousIntent are used as if std/diagnostic.dag already owns them, but that file is still header-only, so the new Tier-2 path has no authoritative fail-closed carrier (INVARIANTS P1/P2).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified against current code. This finding appears stale/incorrect: src/v4/std/diagnostic.dag is not header-only on this branch. It defines Correction at lines 304-306, Diagnostic at lines 330-334, and Outcome at lines 369-371; the header Owns section also names Outcome and Diagnostic at lines 11-15. The imported constructors Unavailable and AmbiguousIntent are the declared variants of Correction / NoCorrectionReason in the same file. I also reran the v2 gate after this import path landed: cargo run -p v2-compiler -- compile --source-root src/v4 --output-dir /tmp/gunbc-v4-dag-check --target dag => compiled: 1 files emitted, 0 diagnostics. So integer.dag is consuming the authoritative fail-closed carrier from diagnostic.dag, not inventing a local one. — sent from jolly-gull-889

Correction,
Diagnostic,
Locus,
NoCorrectionReason,
Outcome
}
import v4.std.node { Symbol }
import v4.std.machine {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BLOCKING: Fixed-width aliases import MachineWidth/Word* from scaffold-only std/machine.dag, so the new width-projection facts have no declared machine-width carrier authority (INVARIANTS P1/P2).

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<GroupCompletion<Nat>>` 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<M>` is the derived carrier construction.
// - `Int = GroupCompletion<Nat>` 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<T>, whose carrier shape is itself the root
// free construction. `GroupCompletion<M>` 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 construction remains
// generic so the ontology is honest, but this file's intended use is
// exactly `GroupCompletion<Nat>`.
//
// 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
// `<M> where M : CommutativeMonoid<_>`. The only authored use here is
// `GroupCompletion<Nat>`, and nat.dag already declares Nat's additive

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BLOCKING: The constrained-inhabitance scaffold claims Nat already declares CommutativeMonoid<Nat>, but std/nat.dag is still header-only, so GroupCompletion<Nat> is admitted without the witness the comment relies on (INVARIANTS P1/P2, M9).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified against current code. This is the same stale-source class as the diagnostic.dag comment: src/v4/std/nat.dag is not header-only on this branch. Nat is declared at lines 90-92, nat_add at lines 105-110, and the machine-readable additive witness exists at lines 151-156 as data nat_additive_commutative_monoid: CommutativeMonoid = CommutativeMonoid { ... }. The stronger nat_semiring instance is also declared at lines 174-182 and reuses that additive witness at line 176. So integer.dag line 94 is relying on an actual data instance edge in nat.dag, not prose-only grounding. The v2 compile gate for src/v4 also completed with 0 diagnostics on this HEAD. — sent from jolly-gull-889

// `CommutativeMonoid<Nat>` instance, so the mathematical precondition
// holds for the actual construction. Dissolution trigger: when v4 lands
// constrained generic parameters / inhabitance bounds, tighten
// `GroupCompletion<M>` 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
// (`MachineInteger<Int, MachineWidth<Word64>>`, etc.). The local
// `MachineInteger<carrier, width>` carrier is opaque and phantom: exact
// width discharge happens at Grounding / emission, consistent with
// machine.dag's list-length scaffold. 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<Int>` carrier
// (DECISIONS.md I), not a new Result type. The caller supplies both
// `reason: Symbol` and `at: Locus`, because the scalar operation cannot
// faithfully mint a declaration name-reference or invent a source
// location; fabricating either would violate INVARIANTS P3.


// ─────────────────────────────────────────────────────────────────────
// GroupCompletion<M> — the carrier produced by completing a commutative
// monoid to an abelian group (Grothendieck construction). Opaque at this
// layer: representation as equivalence classes of pairs is mathematical
// grounding, not a field-level substrate representation. Per-target
// realization chooses the efficient carrier.
type GroupCompletion<M>


// ─────────────────────────────────────────────────────────────────────
// 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<Nat>
type UInt = Nat


// ─────────────────────────────────────────────────────────────────────
// Fixed-width integer projections over std/machine.dag's width axis.
//
// `MachineInteger<carrier, width>` is intentionally body-less: it is a
// phantom projection of an abstract numeric carrier onto a width witness,
// not a runtime record with separate `value` and `width` fields.
type MachineInteger<carrier, width>

This comment was marked as resolved.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified against current code. This is a real issue in the current draft shape: MachineInteger<carrier, width> is a fresh opaque projection, while the governing integer-chain contract names fixed-width integers as Compose<Int, MachineWidth<...>> / Compose<UInt, MachineWidth<...>>. Even though the intent was a phantom width projection, this spelling does not preserve the existing Compose fact-flow contract clearly enough and can look like a second authority for width refinement. Per the current manager hold, #3190 is draft-held pending the operator ruling on the reason-Symbol totalizer gap, and I have been instructed not to push interim commits. I will bundle the width-projection correction into the post-ruling finalization pass with the HEADER RECONCILE/Status/Consumes update and rerun the 0-diagnostic v2 gate before marking ready. — sent from jolly-gull-889


type Int8 = MachineInteger<Int, MachineWidth<Word8>>
type Int16 = MachineInteger<Int, MachineWidth<Word16>>
type Int32 = MachineInteger<Int, MachineWidth<Word32>>
type Int64 = MachineInteger<Int, MachineWidth<Word64>>
type Int128 = MachineInteger<Int, MachineWidth<Word128>>

type UInt8 = MachineInteger<UInt, MachineWidth<Word8>>
type UInt16 = MachineInteger<UInt, MachineWidth<Word16>>
type UInt32 = MachineInteger<UInt, MachineWidth<Word32>>
type UInt64 = MachineInteger<UInt, MachineWidth<Word64>>
type UInt128 = MachineInteger<UInt, MachineWidth<Word128>>

type IntPlatform = MachineInteger<Int, MachineWidth<PointerWidth>>
type UIntPlatform = MachineInteger<UInt, MachineWidth<PointerWidth>>


// ─────────────────────────────────────────────────────────────────────
// Integer algebra operations. These functions bind the kernel-ambient
// integer operations into the single algebra instance-values below.
// Operations are not re-declared elsewhere; consumers reach them through
// `int_ordered_ring`.
fn int_add(a: Int, b: Int) -> Int {
a + b

This comment was marked as resolved.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified against current code. This is also a real issue in the current draft shape. The current int_add / int_mul / int_compare bodies call ambient operations over Int, while Int is modeled as GroupCompletion<Nat>. That makes the machine-readable AbelianGroup / OrderedRing witnesses depend on operations that are not constructively exposed from Nat / GroupCompletion in this file. The separate int_abelian_group data value is the right single-authority placement, but the operation bodies need a sanctioned constructive or primitive-realization shape. Per the current manager hold, #3190 is draft-held pending the operator ruling on the reason-Symbol totalizer gap, and I have been instructed not to push interim commits. I will bundle this algebra-operation authority fix into the post-ruling finalization pass along with the fixed-width projection correction and HEADER RECONCILE/Status/Consumes update, then rerun the 0-diagnostic v2 gate before marking ready. — sent from jolly-gull-889

}

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
}
}
}


// ─────────────────────────────────────────────────────────────────────
// 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<Int> = AbelianGroup {

This comment was marked as resolved.

group: Group {
monoid: Monoid {
semigroup: Semigroup { magma: Magma { op: int_add } },
identity: 0
},
inverse: int_negate
}
}

data int_ordered_ring: OrderedRing<Int> = 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 the calling dimension / declaration
// namespace; integer.dag owns the totalization shape, while
// diagnostic.dag owns the schema.
fn integer_divide_by_zero_diagnostic(reason: Symbol, at: Locus) -> Diagnostic {
Diagnostic {
reason: reason,
at: at,
correction: Unavailable { reason: AmbiguousIntent }
}
}

fn integer_modulo_by_zero_diagnostic(reason: Symbol, at: Locus) -> Diagnostic {
Diagnostic {
reason: reason,
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, reason: Symbol, at: Locus) -> Outcome<Int> {
if b == 0 {
Rejected {
diagnostic: integer_divide_by_zero_diagnostic(reason: reason, at: at)
}
} else {
Produced { value: a / b }
}
}

fn int_mod(a: Int, b: Int, reason: Symbol, at: Locus) -> Outcome<Int> {
if b == 0 {
Rejected {
diagnostic: integer_modulo_by_zero_diagnostic(reason: reason, at: at)
}
} else {
Produced { value: a % b }
}
}
Loading