Skip to content

Add quantity-kind checks, primary lookup and scoped registers - #106

Merged
suleman-uzair merged 7 commits into
mainfrom
feat/f40-quantity-kinds-registers
Nov 11, 2026
Merged

suleman-uzair merged 7 commits into
mainfrom
feat/f40-quantity-kinds-registers

Conversation

@suleman-uzair

Copy link
Copy Markdown
Member

Add data-declared quantity kinds, primary quantity lookup, and block-scoped caller registers. Typed arithmetic checks the dataset equivalence class, primary lookup reads the explicit flag, and register definitions validate factors, offsets and dimensions against canonical data.

Untyped callers keep their existing behavior. Mixed compatible kinds retain the left operand’s kind. The primary lookup API leaves existing render selection unchanged.

Validation: 143 focused examples pass after merging the current main, including exact division, difference-scale preservation, kind checks, register validation and Opal boot. Full/platform CI qualifies this PR head. The associated 251 reference vectors are in unitsml-testsuite PR #4. No corrected UnitsDB data pin or registry release is included.

suleman-uzair added 7 commits November 11, 2026 16:49
Quantity accepts an optional kind: (a UnitsDB quantity id or name, or
:primary). Typed operands of + and - must be the same quantity or share the
UnitsDB equivalence_class, so work + heat is legal and torque + energy
raises QuantityKindMismatchError although both have dimension L2MT-2. A kind
whose dimension differs from the unit's raises DimensionMismatchError.
Untyped quantities keep the dimension-only gating.

QuantityKind.primary_for selects a unit's primary quantity from the
units-schema primary flag, never from reference order, and fails
explicitly when the data flags none or several.

Conversion.with_register applies a validated caller register (exact
factor, optional affine offset, declared dimensions) to every conversion
in the block, including operand alignment in + and -. Entries that
disagree with UnitsDB canonical data, declare other dimensions or carry a
non-positive or inexact factor raise InvalidUnitRegisterError.

Render paths that read the first quantity reference are unchanged.
A caller register makes a unit with no UnitsDB decomposition (cwt (US))
convertible, so its declared dimensions now also feed the dimension gate
of + and -: 2 cwt (US) + 3 m raises DimensionMismatchError instead of
succeeding through the register factor.
Resolve the scalar-division conflict in Quantity#/ by keeping main's
divide_values (PR 104) and this branch's kind: propagation.
record_factor and unit_dimensions now carry the path of unit identities
through si_derived_bases/root_units and raise ConversionError naming the
cycle when a unit repeats. UnitsDB gram-force (NISTu196) lists kilo x
itself as its root unit, so 1 gf and 1 kp to N overflowed the stack; they
now fail in milliseconds. Cycle-free factors and dimensions are memoized
per loaded database, unit and power.
with_register validated the new register inside a method-level ensure,
so a rejected nested registration ran the ensure before 'previous' was
captured and cleared the outer register. Validation now completes before
the thread state changes, and the ensure restores only what this call
installed.
primary_for took a unit's only reference whenever no reference was
flagged true, including one explicitly marked primary: false. A sole
reference is now taken only when its flag is absent (UnitsDB 2.2.0 flags
multi-reference units only); an explicit false raises
PrimaryQuantityError. Render selection is unchanged.
@suleman-uzair
suleman-uzair marked this pull request as ready for review November 11, 2026 17:35
@suleman-uzair
suleman-uzair merged commit ceb1e79 into main Nov 11, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant