This library is in flux. APIs may change without notice. Generated from source with koruc 0.1.7 on 11/4/2026.

Quantity

import std/quantity

std/quantity — quantity calculus over arbitrary-precision decimals,

quantity.kz · 30 tors· 20 internal

std/quantity — quantity calculus over arbitrary-precision decimals, · 41 more lines
std/quantity — quantity calculus over arbitrary-precision decimals, taint-sourced like std/num. Port source: isa-physics `plex/lib/structural_arithmetic/tensor_quantity.py` (Q = dimension + magnitude + uncertainty) and the isar-proofs `ISAR/QuantityKernel.lean` (DimExpr free abelian group over the 7 SI bases, MetricExpr exact-rational layer, propagateVarAdd/Mul linearized variance laws plus the shared-source covariance term). A `qty` is ONE canonical marked string: "<num>|<dim>|<unc>[@<cov>]" <num>: std/num canonical decimal e.g. "9.81e0" <dim>: all 7 SI exps, fixed order "m^1 s^-2 kg^0 A^0 K^0 mol^0 cd^0" <unc>: num canon or "-" e.g. "1e-1" / "1e-1@bias" <cov>: shared-source tag; absent "-" Canonical => quantity equivalence is string equality (`qty:equiv`). A "-" uncertainty is "0e0" internally: no uncertainty contributes zero variance, and pack writes "-" back out. Covariance model (linearized, GUM-adjacent): `unc@<group>` asserts the whole sigma stems from shared source <group>. A leading '-' on the tag marks a negated instance (what qty:neg produces): same group opposite sign is a FULLY ANTI-CORRELATED source, and the covariance term flips sign — a−b sharing a bias cancels it. Same group + sign adds 2·u_a·u_b; different groups => | mixed-cov — the correlation is unknowable, so the op refuses the axis instead of silently assuming independence. One-sided tags propagate conservatively: independent sigma, tag carried forward. Taint: same shape as std/num — `ingest`/`ingest-owned` issue <external!>, `parse`/`lit` discharge it into <exact!>, ops consume <!exact> linearly, `dup` forks, `free` deallocates, `render` swaps to <std/io:allocated!>. `qty:mag` unwraps the magnitude INTO the number tower as <std/num:exact!> — the lawful crossing between the two marks; `qty:unpack` decomposes the pack into marked fields, `qty:pack` reassembles it. Boundary vs impl: the zig procs below are ONLY mark-boundary machinery — ingest/split/pack/unpack issue and consume marks, dim-* compose the canonical dim vector, cov-* carry the shared-source policy. Every arithmetic law is a Koru impl (`~name = ...`) calling std/num tors — the decimal core lives in ONE place (num.kz) and ops compose through the tor mechanism: data crosses boundaries, logic does not.

Phantom lifecycles

Derived from the phantom labels in the declarations below — state! issues an obligation the compiler will chase, !state discharges it, a bare state holds it without moving it. Nothing here is hand-drawn.

string 10 states external!external!allocated!exact!exact!sexpr!

ingest

<external!> koru_std/quantity.kz:312

ingest-owned

<!allocated><external!> koru_std/quantity.kz:318

split

<!external><std.num:external!><std.io:allocated!> koru_std/quantity.kz:327

parse

<!external><exact!><std.io:allocated!> koru_std/quantity.kz:362

lit-sym

<std.sym:!sexpr><exact!> koru_std/quantity.kz:391

pack

<std.num:!exact><std.io:!allocated><exact!> koru_std/quantity.kz:421

pack-sym

<std.sym:!sexpr><std.num:!exact><std.io:!allocated><exact!> koru_std/quantity.kz:429

unpack

<!exact><std.num:exact!><std.io:allocated!><std.sym:sexpr!> koru_std/quantity.kz:461

unit-dim

<std.num:external!><std.io:allocated!> koru_std/quantity.kz:486

dim-compose

<std.io:allocated><std.io:allocated!> koru_std/quantity.kz:531

dim-invert

<std.io:allocated><std.io:allocated!> koru_std/quantity.kz:543

dim-scale

<std.io:allocated><std.io:allocated!> koru_std/quantity.kz:553

dim-eq

<std.io:allocated> koru_std/quantity.kz:563

cov-join

<std.io:allocated><std.io:allocated!> koru_std/quantity.kz:579

cov-flip

<std.io:allocated><std.io:allocated!> koru_std/quantity.kz:606

add

<!exact><exact!> koru_std/quantity.kz:668

neg

<!exact><exact!> koru_std/quantity.kz:699

sub

<!exact><exact!> koru_std/quantity.kz:705

mul

<!exact><exact!> koru_std/quantity.kz:776

div

<!exact><exact!> koru_std/quantity.kz:862

pow

<!exact><exact!> koru_std/quantity.kz:929

mag

<!exact><std.num:exact!><std.sym:sexpr!> koru_std/quantity.kz:967

dim

<!exact><std.io:allocated!> koru_std/quantity.kz:976

unc

<!exact><std.io:allocated!> koru_std/quantity.kz:983

cov

<!exact><std.io:allocated!> koru_std/quantity.kz:990

render

<!exact><std.io:allocated!> koru_std/quantity.kz:1010