TorchLean API

FloatLib.Floats.Formats.Posit.Quire.Arithmetic.Proof

Correctness of standard posit quire operations #

The executable quire operations agree with their exact coefficient, dyadic, and rational semantics. The refinement theorems cover these operations from Posit Standard (2022), §5.11:

The runtime definitions live in Arithmetic.Runtime. Keeping the refinement layer separate lets execution-only clients use the integer and dyadic kernels without importing the full proof graph.

References #

@[simp]

Converting posit NaR to a quire produces quire NaR.

@[simp]

Converting posit zero to a quire produces quire zero.

@[simp]

Quire negation fixes NaR.

@[simp]

Quire negation fixes zero.

@[simp]

Quire absolute value fixes NaR.

@[simp]

Quire absolute value fixes zero.

@[simp]
theorem FloatLib.Floats.Formats.Posit.Quire.Model.qAddQ_nar_left (format : Format) (right : Model format) :
(nar format).qAddQ right = nar format

NaR propagates from the left operand of quire addition.

@[simp]
theorem FloatLib.Floats.Formats.Posit.Quire.Model.qAddQ_nar_right (format : Format) (left : Model format) :
left.qAddQ (nar format) = nar format

NaR propagates from the right operand of quire addition.

@[simp]
theorem FloatLib.Floats.Formats.Posit.Quire.Model.qSubQ_nar_left (format : Format) (right : Model format) :
(nar format).qSubQ right = nar format

NaR propagates from the left operand of quire subtraction.

@[simp]
theorem FloatLib.Floats.Formats.Posit.Quire.Model.qSubQ_nar_right (format : Format) (left : Model format) :
left.qSubQ (nar format) = nar format

NaR propagates from the right operand of quire subtraction.

@[simp]

Converting quire NaR back to a posit produces posit NaR.

@[simp]

Converting quire zero back to a posit produces posit zero.

Exact coefficient and rational refinement theorems #

The standard quire scale is no greater than the decoded scale of any nonzero finite posit.

For widths of at least three this follows from the format-wide regime bound. The two-bit standard format is the only arithmetic boundary case: its two ordinary nonzero words are 1 and -1, both at scale zero.

Every ordinary posit dyadic aligns exactly with the least-significant bit of its associated quire.

This supplies the conversion kernel's scale condition at every width, including the two-bit boundary format.

Every ordinary decoded posit significand fits below its complete encoded modulus.

The dyadic scale of every ordinary posit is below four times its encoded width.

theorem FloatLib.Floats.Formats.Posit.Quire.Model.conversionShift_le_twelve_bits {format : Format} (value : Posit.Model format) (dyadic : Numerics.Dyadic) (hvalue : value.toDyadic? = some dyadic) :
(dyadic.exponent - scaleExponent format).toNat 12 * format.bits

Aligning an ordinary posit to the quire scale requires at most twelve shift bits per posit bit.

This linear bound suffices to prove that conversion fits in the standard 16n-bit quire.

theorem FloatLib.Floats.Formats.Posit.Quire.Model.natAbs_coefficientOfDyadic_lt_halfRange {format : Format} (value : Posit.Model format) (dyadic : Numerics.Dyadic) (hvalue : value.toDyadic? = some dyadic) :
(coefficientOfDyadic format dyadic).natAbs < 2 ^ (width format - 1)

The magnitude of the integer coefficient produced by posit-to-quire alignment is strictly below the quire's signed half-range.

theorem FloatLib.Floats.Formats.Posit.Quire.Model.coefficientOfDyadic_ordinary {format : Format} (value : Posit.Model format) (dyadic : Numerics.Dyadic) (hvalue : value.toDyadic? = some dyadic) :

Every ordinary posit converts to an ordinary quire coefficient without overflow.

Ordinary quire negation stores the exact negated coefficient.

Ordinary quire negation refines exact rational negation.

theorem FloatLib.Floats.Formats.Posit.Quire.Model.coefficient_qAddQ_of_ordinary {format : Format} (left right : Model format) (hleft : OrdinaryCoefficient format left.coefficient) (hright : OrdinaryCoefficient format right.coefficient) (hresult : OrdinaryCoefficient format (left.coefficient + right.coefficient)) :
(left.qAddQ right).coefficient = left.coefficient + right.coefficient

Successful exact quire addition stores the mathematical coefficient sum.

theorem FloatLib.Floats.Formats.Posit.Quire.Model.toRat?_qAddQ_of_ordinary {format : Format} (left right : Model format) (hleft : OrdinaryCoefficient format left.coefficient) (hright : OrdinaryCoefficient format right.coefficient) (hresult : OrdinaryCoefficient format (left.coefficient + right.coefficient)) :
(left.qAddQ right).toRat? = some (Rat.ofInt left.coefficient * 2 ^ scaleExponent format + Rat.ofInt right.coefficient * 2 ^ scaleExponent format)

Successful exact quire addition refines rational addition at the shared fixed scale.

theorem FloatLib.Floats.Formats.Posit.Quire.Model.coefficient_qSubQ_of_ordinary {format : Format} (left right : Model format) (hleft : OrdinaryCoefficient format left.coefficient) (hright : OrdinaryCoefficient format right.coefficient) (hresult : OrdinaryCoefficient format (left.coefficient - right.coefficient)) :
(left.qSubQ right).coefficient = left.coefficient - right.coefficient

Successful exact quire subtraction stores the mathematical coefficient difference.

theorem FloatLib.Floats.Formats.Posit.Quire.Model.toRat?_qSubQ_of_ordinary {format : Format} (left right : Model format) (hleft : OrdinaryCoefficient format left.coefficient) (hright : OrdinaryCoefficient format right.coefficient) (hresult : OrdinaryCoefficient format (left.coefficient - right.coefficient)) :
(left.qSubQ right).toRat? = some (Rat.ofInt left.coefficient * 2 ^ scaleExponent format - Rat.ofInt right.coefficient * 2 ^ scaleExponent format)

Successful exact quire subtraction refines rational subtraction at the shared fixed scale.

@[simp]
theorem FloatLib.Floats.Formats.Posit.Quire.Model.addDyadic_nar (format : Format) (increment : Numerics.Dyadic) :
(nar format).addDyadic increment = nar format

The dyadic accumulation kernel propagates quire NaR.

theorem FloatLib.Floats.Formats.Posit.Quire.Model.addDyadic_eq_nar_of_exponent_lt {format : Format} (accumulator : Model format) (increment : Numerics.Dyadic) (hexponent : increment.exponent < scaleExponent format) :
accumulator.addDyadic increment = nar format

An increment whose stored exponent is below the quire scale produces quire NaR.

Otherwise coefficientOfDyadic would clamp the negative shift to zero. The guard does not inspect trailing zeros in the significand, so it may reject a dyadic whose value is nevertheless aligned.

theorem FloatLib.Floats.Formats.Posit.Quire.Model.addDyadic_eq_ofCoefficient {format : Format} (accumulator : Model format) (increment : Numerics.Dyadic) (haccumulator : OrdinaryCoefficient format accumulator.coefficient) (hscale : scaleExponent format increment.exponent) :
accumulator.addDyadic increment = ofCoefficient format (accumulator.coefficient + coefficientOfDyadic format increment)

Once the accumulator is ordinary and the increment is aligned, the kernel is exactly ofCoefficient applied to the coefficient sum.

theorem FloatLib.Floats.Formats.Posit.Quire.Model.addDyadic_eq_nar_of_not_ordinary {format : Format} (accumulator : Model format) (increment : Numerics.Dyadic) (haccumulator : OrdinaryCoefficient format accumulator.coefficient) (hscale : scaleExponent format increment.exponent) (hresult : ¬OrdinaryCoefficient format (accumulator.coefficient + coefficientOfDyadic format increment)) :
accumulator.addDyadic increment = nar format

An aligned increment whose exact coefficient sum overflows produces quire NaR.

theorem FloatLib.Floats.Formats.Posit.Quire.Model.coefficient_addDyadic_of_ordinary {format : Format} (accumulator : Model format) (increment : Numerics.Dyadic) (haccumulator : OrdinaryCoefficient format accumulator.coefficient) (hscale : scaleExponent format increment.exponent) (hresult : OrdinaryCoefficient format (accumulator.coefficient + coefficientOfDyadic format increment)) :
(accumulator.addDyadic increment).coefficient = accumulator.coefficient + coefficientOfDyadic format increment

Successful dyadic accumulation stores the exact coefficient sum.

theorem FloatLib.Floats.Formats.Posit.Quire.Model.toRat?_addDyadic_of_ordinary {format : Format} (accumulator : Model format) (increment : Numerics.Dyadic) (haccumulator : OrdinaryCoefficient format accumulator.coefficient) (hscale : scaleExponent format increment.exponent) (hresult : OrdinaryCoefficient format (accumulator.coefficient + coefficientOfDyadic format increment)) :
(accumulator.addDyadic increment).toRat? = some (Rat.ofInt accumulator.coefficient * 2 ^ scaleExponent format + increment.toRat)

The shared dyadic accumulation kernel refines exact rational addition whenever the dyadic aligns to the quire scale and the resulting coefficient is ordinary.

theorem FloatLib.Floats.Formats.Posit.Quire.Model.toRat?_qAddP_of_ordinary {format : Format} (accumulator : Model format) (addend : Posit.Model format) (dyadic : Numerics.Dyadic) (haddend : addend.toDyadic? = some dyadic) (haccumulator : OrdinaryCoefficient format accumulator.coefficient) (hresult : OrdinaryCoefficient format (accumulator.coefficient + coefficientOfDyadic format dyadic)) :
(accumulator.qAddP addend).toRat? = some (Rat.ofInt accumulator.coefficient * 2 ^ scaleExponent format + dyadic.toRat)

Adding an ordinary posit to an ordinary quire refines exact rational addition whenever the resulting fixed-point coefficient remains ordinary.

Decoded posits satisfy the scale condition automatically. The remaining range hypothesis excludes overflow and the reserved NaR coefficient.

theorem FloatLib.Floats.Formats.Posit.Quire.Model.coefficient_qAddP_of_ordinary {format : Format} (accumulator : Model format) (addend : Posit.Model format) (dyadic : Numerics.Dyadic) (haddend : addend.toDyadic? = some dyadic) (haccumulator : OrdinaryCoefficient format accumulator.coefficient) (hresult : OrdinaryCoefficient format (accumulator.coefficient + coefficientOfDyadic format dyadic)) :
(accumulator.qAddP addend).coefficient = accumulator.coefficient + coefficientOfDyadic format dyadic

Successful posit accumulation stores the exact coefficient sum.

theorem FloatLib.Floats.Formats.Posit.Quire.Model.qAddP_eq_nar_of_not_ordinary {format : Format} (accumulator : Model format) (addend : Posit.Model format) (dyadic : Numerics.Dyadic) (haddend : addend.toDyadic? = some dyadic) (haccumulator : OrdinaryCoefficient format accumulator.coefficient) (hresult : ¬OrdinaryCoefficient format (accumulator.coefficient + coefficientOfDyadic format dyadic)) :
accumulator.qAddP addend = nar format

Adding an ordinary posit whose exact coefficient sum overflows the quire produces NaR.

theorem FloatLib.Floats.Formats.Posit.Quire.Model.toRat?_qSubP_of_ordinary {format : Format} (accumulator : Model format) (subtrahend : Posit.Model format) (dyadic : Numerics.Dyadic) (hsubtrahend : subtrahend.toDyadic? = some dyadic) (haccumulator : OrdinaryCoefficient format accumulator.coefficient) (hresult : OrdinaryCoefficient format (accumulator.coefficient + coefficientOfDyadic format dyadic.neg)) :
(accumulator.qSubP subtrahend).toRat? = some (Rat.ofInt accumulator.coefficient * 2 ^ scaleExponent format - dyadic.toRat)

Subtracting an ordinary posit from an ordinary quire refines exact rational subtraction whenever the resulting fixed-point coefficient remains ordinary.

As for addition, decoded posits align with the associated quire. The remaining range hypothesis excludes overflow and the reserved NaR coefficient.

Every ordinary posit dyadic lies at or above the smallest positive posit exponent.

Zero uses the exact carrier's conventional exponent zero. A finite nonzero value uses the global representation bound proved from the mandatory negative-regime terminator.

The associated quire scale is twice the smallest positive posit exponent.

theorem FloatLib.Floats.Formats.Posit.Quire.Model.scaleExponent_le_mul_exponent_of_toDyadic?_eq_some {format : Format} (left right : Posit.Model format) (leftDyadic rightDyadic : Numerics.Dyadic) (hleft : left.toDyadic? = some leftDyadic) (hright : right.toDyadic? = some rightDyadic) :
scaleExponent format (leftDyadic.mul rightDyadic).exponent

Every exact product of two ordinary posits aligns with the associated quire scale.

theorem FloatLib.Floats.Formats.Posit.Quire.Model.toRat?_qMulAdd_of_ordinary {format : Format} (accumulator : Model format) (left right : Posit.Model format) (leftDyadic rightDyadic : Numerics.Dyadic) (hleft : left.toDyadic? = some leftDyadic) (hright : right.toDyadic? = some rightDyadic) (haccumulator : OrdinaryCoefficient format accumulator.coefficient) (hresult : OrdinaryCoefficient format (accumulator.coefficient + coefficientOfDyadic format (leftDyadic.mul rightDyadic))) :
(accumulator.qMulAdd left right).toRat? = some (Rat.ofInt accumulator.coefficient * 2 ^ scaleExponent format + leftDyadic.toRat * rightDyadic.toRat)

Exact fused product accumulation refines rational multiplication followed by exact quire addition whenever the resulting coefficient is ordinary.

theorem FloatLib.Floats.Formats.Posit.Quire.Model.coefficient_qMulAdd_of_ordinary {format : Format} (accumulator : Model format) (left right : Posit.Model format) (leftDyadic rightDyadic : Numerics.Dyadic) (hleft : left.toDyadic? = some leftDyadic) (hright : right.toDyadic? = some rightDyadic) (haccumulator : OrdinaryCoefficient format accumulator.coefficient) (hresult : OrdinaryCoefficient format (accumulator.coefficient + coefficientOfDyadic format (leftDyadic.mul rightDyadic))) :
(accumulator.qMulAdd left right).coefficient = accumulator.coefficient + coefficientOfDyadic format (leftDyadic.mul rightDyadic)

Successful fused product accumulation stores the exact coefficient sum.

theorem FloatLib.Floats.Formats.Posit.Quire.Model.qMulAdd_eq_nar_of_not_ordinary {format : Format} (accumulator : Model format) (left right : Posit.Model format) (leftDyadic rightDyadic : Numerics.Dyadic) (hleft : left.toDyadic? = some leftDyadic) (hright : right.toDyadic? = some rightDyadic) (haccumulator : OrdinaryCoefficient format accumulator.coefficient) (hresult : ¬OrdinaryCoefficient format (accumulator.coefficient + coefficientOfDyadic format (leftDyadic.mul rightDyadic))) :
accumulator.qMulAdd left right = nar format

Accumulating an exact product whose coefficient sum overflows the quire produces NaR.

Together with toRat?_qMulAdd_of_ordinary this characterizes qMulAdd on ordinary inputs: the result is exact while the coefficient stays ordinary and NaR as soon as it does not.

theorem FloatLib.Floats.Formats.Posit.Quire.Model.toRat?_qMulSub_of_ordinary {format : Format} (accumulator : Model format) (left right : Posit.Model format) (leftDyadic rightDyadic : Numerics.Dyadic) (hleft : left.toDyadic? = some leftDyadic) (hright : right.toDyadic? = some rightDyadic) (haccumulator : OrdinaryCoefficient format accumulator.coefficient) (hresult : OrdinaryCoefficient format (accumulator.coefficient + coefficientOfDyadic format (leftDyadic.mul rightDyadic).neg)) :
(accumulator.qMulSub left right).toRat? = some (Rat.ofInt accumulator.coefficient * 2 ^ scaleExponent format - leftDyadic.toRat * rightDyadic.toRat)

Exact fused product subtraction refines rational subtraction whenever the resulting coefficient is ordinary.

theorem FloatLib.Floats.Formats.Posit.Quire.Model.toRat?_pToQ_of_invariants {format : Format} (value : Posit.Model format) (dyadic : Numerics.Dyadic) (hvalue : value.toDyadic? = some dyadic) (hscale : scaleExponent format dyadic.exponent) (hcoefficient : OrdinaryCoefficient format (coefficientOfDyadic format dyadic)) :
(pToQ value).toRat? = value.toRat?

Posit-to-quire conversion preserves exact rational meaning when the standard scale and quire-range invariants are supplied.

toRat?_pToQ supplies both invariants for every ordinary posit. This local form also allows specialized decoders to supply their own alignment and range proofs.

Posit-to-quire conversion preserves exact rational meaning for every Posit Standard (2022) value.

NaR maps to quire NaR. Every ordinary value is proved to align with the standard quire scale and to lie strictly inside its signed coefficient range, so this theorem has no caller-supplied side conditions.

theorem FloatLib.Floats.Formats.Posit.Quire.Model.qToP_eq_roundRat {format : Format} (value : Model format) :
value.qToP = match value.toRat? with | some rational => Model.roundRat format rational | none => Posit.Model.nar format

Quire-to-posit conversion agrees with Posit Standard (2022) rational rounding.

An ordinary quire rounds its exact rational value once; the reserved quire NaR maps to the unique posit NaR instead of being assigned a finite or infinite numerical value.