Physlib

Physlib.Units.UnitSystem

Unit systems, parametrised in a dimension basis

A **unit system** is the everyday physics object: a rule fixing one concrete unit for each base quantity. SI fixes the metre for length, the second for time, the kilogram for mass, …; CGS fixes the centimetre, second and gram. Choosing a system gives every quantity a numerical value; changing systems rescales those values.

This module makes that notion work for *any* dimension basis `B` (matching the basis-generic `Dimension B`), while keeping units *typed*: the unit in the length slot has type `LengthUnit`, the one in the time slot `TimeUnit`, so the checker rejects a mass unit in a length slot.

* `UnitMagnitudeCatalog B` — the **menu** for basis `B`. For each base dimension `b : B` it gives a *type* `Unit b` of admissible units — so a dimension may offer many (length: metre, kilometre, micrometre, …) — and a *function* `mag` assigning each unit its one positive-real magnitude. The menu lists the options and picks none; many unit systems share one catalog. * `UnitSystem B` — the **pick**: a function choosing *exactly one* unit per base dimension. A single unit system therefore fixes one length unit — metre *or* kilometre, never both — so metre and kilometre are two menu entries selected by two different `UnitSystem`s over the same catalog. SI and CGS are two such picks. * `UnitSystem.toScale : UnitSystem B → UnitScale B` — forget the unit *types*, keep only the magnitudes, landing in the bare magnitude layer `UnitScale B` where the scaling homomorphism `UnitScale.dimScale` is proved once.

**Menu vs. pick.** The catalog is a menu, not a choice: it says which units exist and what each weighs, but selects none — a `UnitSystem` makes the selection. And in PhysLib a base unit *is* a positive real (`{ val : ℝ // 0 < val }`), i.e. its own magnitude, so the magnitude comes fixed with the unit; there is no separate "assign a magnitude" step. Hence metre, micrometre and kilometre are three *different* units on the one length menu, and "SI in metres" versus "the same in micrometres" are two different `UnitSystem`s over the **same** catalog — related by `UnitScale.dimScale`, which computes the 10⁶ factor between them. (The multiplicity one might expect from "many magnitudes" lives here, among the unit systems, not in many catalogs.)

The `LTMCTDimensionBase` catalog recovers PhysLib's five named unit types, and `LTMCTUnitChoices ≃ UnitSystem LTMCTDimensionBase` exhibits the bespoke five-field record as that instance. Scaling laws are **not** re-proved here — they live once on `UnitScale B`, and the typed layer is a thin faithful wrapper projecting onto it via `toScale`.

This is the typed, basis-generic layer discussed as "Option D" in the review of the dimension-parametrisation PR; it is the only design that keeps *both* basis-genericity and typed-unit safety.

The `LTMCTDimensionBase` catalog

The default basis's `UnitMagnitudeCatalog` recovers exactly PhysLib's five named typed unit types, so `UnitSystem LTMCTDimensionBase` is the typed five-slot unit system and `u .length : LengthUnit`, `LengthUnit.meters`, … all still work — and a `MassUnit` cannot be placed in the length slot.

`LTMCTUnitChoices` is `UnitSystem LTMCTDimensionBase`

The bespoke five-field record and the generic unit system over the default basis carry the same data.

The bespoke scaling law is the generic fold

`LTMCTUnitChoices.dimScale` — the five explicit `rpow` factors written out by hand in `Basic.lean` — is exactly the generic `UnitScale.dimScale` fold (a `Finset.prod` over the basis) at the `LTMCTDimensionBase` instance, applied to `toScale`. This is what makes the typed layer a *faithful* wrapper rather than a second, independent statement of the scaling law: there is one source of truth, the generic fold on `UnitScale B`.

9 declarations

definition

Unit system over a dimension basis BB

Given a dimension basis BB and a unit magnitude catalog that provides a type of units Unit(b)\text{Unit}(b) for each base dimension bBb \in B, a **unit system** is defined as a function uu that maps each base dimension bBb \in B to a specific unit u(b)u(b) of type Unit(b)\text{Unit}(b). This structure serves as the formal representation of physical unit systems such as SI or CGS, ensuring that for every base dimension, exactly one unit is chosen from the available options in the catalog.

theorem

Extensionality of Unit Systems: (b,u(b)=v(b))    u=v(\forall b, u(b) = v(b)) \implies u = v

Let BB be a dimension basis. For any two unit systems uu and vv over BB, if they select the same unit for every base dimension bBb \in B (i.e., u(b)=v(b)u(b) = v(b) for all bBb \in B), then the unit systems are equal (u=vu = v).

definition

Unit scale of a unit system uu

The function maps a unit system uu over a dimension basis BB to its corresponding `UnitScale B`. For each base dimension bBb \in B, the resulting scale value is the numerical magnitude mag(u(b))\text{mag}(u(b)) of the unit u(b)u(b) chosen by the system. This transformation effectively "forgets" the specific unit types associated with the system uu and retains only their positive real magnitudes.

theorem

The scale value (toScale(u)).scale(b)(\text{toScale}(u)).\text{scale}(b) equals the magnitude mag(u(b))\text{mag}(u(b))

Let BB be a dimension basis and uu be a unit system over BB. For any base dimension bBb \in B, the numerical scale value of the unit system at bb, denoted by (toScale(u)).scale(b)(\text{toScale}(u)).\text{scale}(b), is equal to the magnitude of the unit u(b)u(b) chosen by the system for that dimension, denoted by mag(u(b))\text{mag}(u(b)).

instance

Unit magnitude catalog for the LTMCT dimension basis

The `UnitMagnitudeCatalog` instance for the basis of physical dimensions consisting of length, time, mass, charge, and temperature (LTMCT). For each dimension in the basis b{length, time, mass, charge, temperature}b \in \{ \text{length, time, mass, charge, temperature} \}, the catalog specifies the set of available units as the types `LengthUnit`, `TimeUnit`, `MassUnit`, `ChargeUnit`, and `TemperatureUnit`, respectively. The magnitude function mag\text{mag} maps a unit uu to its underlying numerical value in R>0\mathbb{R}_{>0}.

definition

LTMCTUnitChoicesUnitSystem LTMCTDimensionBase\text{LTMCTUnitChoices} \simeq \text{UnitSystem LTMCTDimensionBase}

The equivalence LTMCTUnitChoicesUnitSystem LTMCTDimensionBase\text{LTMCTUnitChoices} \simeq \text{UnitSystem LTMCTDimensionBase} establishes that the bespoke record type containing five fields (length, time, mass, charge, and temperature) and the generic unit system defined over the LTMCT dimension basis represent the same data. The mapping identifies the record fields u.lengthu.\text{length}, u.timeu.\text{time}, u.massu.\text{mass}, u.chargeu.\text{charge}, and u.temperatureu.\text{temperature} with the values of a function ff applied to the corresponding elements of the basis LTMCTDimensionBase={length,time,mass,charge,temperature}\text{LTMCTDimensionBase} = \{\text{length}, \text{time}, \text{mass}, \text{charge}, \text{temperature}\}.

theorem

(equivUnitSystem u).toScale=u.toScale(\text{equivUnitSystem } u).\text{toScale} = u.\text{toScale}

For any choice of base units uu represented by the record type `LTMCTUnitChoices`, the `UnitScale` obtained by viewing uu as a generic unit system (via the equivalence equivUnitSystem:LTMCTUnitChoicesUnitSystem LTMCTDimensionBase\text{equivUnitSystem}: \text{LTMCTUnitChoices} \simeq \text{UnitSystem LTMCTDimensionBase}) and then projecting it to its numerical magnitudes is equal to the scale obtained through the specific `toScale` function defined for `LTMCTUnitChoices`. That is, (equivUnitSystem(u)).toScale=u.toScale(\text{equivUnitSystem}(u)).\text{toScale} = u.\text{toScale}.

theorem

The product over `LTMCTDimensionBase` equals the product of its five components

Let MM be a commutative monoid and f:LTMCTDimensionBaseMf: \text{LTMCTDimensionBase} \to M be a function. The product of f(b)f(b) over all elements bb in the set of base dimensions LTMCTDimensionBase={length,time,mass,charge,temperature}\text{LTMCTDimensionBase} = \{\text{length}, \text{time}, \text{mass}, \text{charge}, \text{temperature}\} is equal to the product of the function values for each specific dimension in their canonical order: bLTMCTDimensionBasef(b)=f(length)f(time)f(mass)f(charge)f(temperature)\prod_{b \in \text{LTMCTDimensionBase}} f(b) = f(\text{length}) \cdot f(\text{time}) \cdot f(\text{mass}) \cdot f(\text{charge}) \cdot f(\text{temperature})

theorem

Bespoke LTMCT scaling factor equals generic magnitude-layer scaling

For any two unit systems u1u_1 and u2u_2 represented as `LTMCTUnitChoices` (which define specific units for length, time, mass, charge, and temperature) and for any physical dimension dd in the LTMCTLTMCT basis, the bespoke scaling factor u1.dimScale(u2,d)u_1.\text{dimScale}(u_2, d) is equal to the generic scaling factor UnitScale.dimScale(toScale(u1),toScale(u2),d)\text{UnitScale.dimScale}(\text{toScale}(u_1), \text{toScale}(u_2), d) calculated at the magnitude layer. This result establishes that the explicitly defined five-factor scaling law matches the general basis-generic product definition.