TorchLean API

FloatLib.Floats.ExecFloat.Conversion.Proof

Proof contracts for explicit conversion and mixed arithmetic #

The preparation theorems compose the supplied decoder, exact-domain operations, and destination quantization relation. They establish one quantizer call on successful preparation and preserve preparation failures. Numerical preservation by ExactMap, arithmetic instances, and the context-dependent addition and subtraction hooks requires the separate family-specific laws described in Conversion.Core.

Runtime-only clients may import Conversion.Runtime; the public Conversion module exports both execution and these contracts.

@[simp]

The merged inexact flag is raised exactly when either input raised it.

@[simp]

The merged overflow flag is raised exactly when either input raised it.

@[simp]

The merged underflow flag is raised exactly when either input raised it.

@[simp]

The merged saturation flag is raised exactly when either input raised it.

@[simp]

The merged wrapping flag is raised exactly when either input raised it.

@[simp]

The merged special-value flag is raised exactly when either input raised it.

@[simp]

An empty first status contributes no flags.

@[simp]

An empty second status contributes no flags.

theorem FloatLib.Floats.ExecFloat.ConversionStatus.merge_assoc (first second third : ConversionStatus) :
(first.merge second).merge third = first.merge (second.merge third)

Status accumulation is associative, so grouping a successful pipeline is irrelevant.

@[simp]
theorem FloatLib.Floats.ExecFloat.ConversionOutcome.map_success {α : Type u} {β : Type v} (f : αβ) (value : α) (status : ConversionStatus) :
map f (success value status) = success (f value) status

Mapping a successful outcome transforms only its value.

@[simp]
theorem FloatLib.Floats.ExecFloat.ConversionOutcome.map_failure {α : Type u} {β : Type v} (f : αβ) (reason : ConversionFailure) :
map f (failure reason) = failure reason

Mapping a failed outcome preserves its failure reason.

@[simp]
theorem FloatLib.Floats.ExecFloat.ConversionOutcome.bind_success {α : Type u} {β : Type v} (value : α) (status : ConversionStatus) (next : αConversionOutcome β) :
(success value status).bind next = match next value with | success nextValue nextStatus => success nextValue (status.merge nextStatus) | failure reason => failure reason

Binding a successful outcome runs the continuation and accumulates successful status.

@[simp]
theorem FloatLib.Floats.ExecFloat.ConversionOutcome.bind_failure {α : Type u} {β : Type v} (reason : ConversionFailure) (next : αConversionOutcome β) :
(failure reason).bind next = failure reason

Binding a failed outcome does not run the continuation.

theorem FloatLib.Floats.ExecFloat.ConversionOutcome.bind_success_empty {α : Type u} {β : Type v} (value : α) (next : αConversionOutcome β) :
(success value).bind next = next value

A successful outcome with no prior flags binds exactly like direct continuation application.

@[simp]
theorem FloatLib.Floats.ExecFloat.ConversionOutcome.value?_success {α : Type u} (value : α) (status : ConversionStatus) :
(success value status).value? = some value

Extracting the value of a successful outcome returns that value.

@[simp]

A failed outcome contains no successful value.

@[simp]
theorem FloatLib.Floats.ExecFloat.ConversionOutcome.status?_success {α : Type u} (value : α) (status : ConversionStatus) :
(success value status).status? = some status

Extracting status from a successful outcome returns its stored flags.

@[simp]

A failed outcome contains no successful status.

@[simp]

A successful outcome contains no failure reason.

@[simp]

Extracting failure information returns the stored reason.

@[simp]
theorem FloatLib.Floats.ExecFloat.ConversionOutcome.map_id {α : Type u} (outcome : ConversionOutcome α) :
map id outcome = outcome

Mapping the identity function leaves an outcome unchanged.

@[simp]
theorem FloatLib.Floats.ExecFloat.ConversionOutcome.map_comp {α : Type u} {β : Type v} (f : αβ) {γ : Type w} (g : βγ) (outcome : ConversionOutcome α) :
map g (map f outcome) = map (g f) outcome

Success-value maps compose without changing status or failure information.

@[simp]
theorem FloatLib.Floats.ExecFloat.ConversionOutcome.value?_map {α : Type u} {β : Type v} (f : αβ) (outcome : ConversionOutcome α) :
(map f outcome).value? = Option.map f outcome.value?

Extracting a value after map agrees with mapping the extracted option.

@[simp]
theorem FloatLib.Floats.ExecFloat.ConversionOutcome.status?_map {α : Type u} {β : Type v} (f : αβ) (outcome : ConversionOutcome α) :
(map f outcome).status? = outcome.status?

Mapping a successful value does not alter its status.

@[simp]
theorem FloatLib.Floats.ExecFloat.ConversionOutcome.failure?_map {α : Type u} {β : Type v} (f : αβ) (outcome : ConversionOutcome α) :
(map f outcome).failure? = outcome.failure?

Mapping a successful value does not alter a failure reason.

theorem FloatLib.Floats.ExecFloat.ConversionOutcome.bind_assoc {α : Type u} {β : Type v} {γ : Type w} (outcome : ConversionOutcome α) (next : αConversionOutcome β) (last : βConversionOutcome γ) :
(outcome.bind next).bind last = outcome.bind fun (value : α) => (next value).bind last

Grouping consecutive successful binds does not change values, failures, or accumulated flags.

@[simp]

Decoding a natural number for conversion produces that exact finite value.

@[simp]

Decoding an integer for conversion produces that exact finite value.

@[simp]

Decoding a rational for conversion introduces no rounding.

@[simp]

Decoding a dyadic value for conversion preserves its exact binary representation.

theorem FloatLib.Floats.ExecFloat.ExactDecoder.decodeTo_eq {Source : Type u} {SourceExact : Type v} {TargetExact : Type w} [ExactDecoder Source SourceExact] [embedding : ExactMap SourceExact TargetExact] (value : Source) :

decodeTo is exact decoding followed by the selected finite-value embedding.

@[simp]
theorem FloatLib.Floats.ExecFloat.ExactDecoder.decodeTo_self {Source : Type u} {SourceExact : Type v} [ExactDecoder Source SourceExact] (value : Source) :
decodeTo value = run value

Decoding into the source's own exact domain performs no semantic transformation.

theorem FloatLib.Floats.ExecFloat.Quantizer.spec_quantize {Destination : Type u} {Exact : Type v} [self : Quantizer Destination Exact] (context : Context Destination) (value : Numerics.NumericalValue Exact) :
spec context value (quantize context value)

The executable destination quantizer satisfies its declared relation.

theorem FloatLib.Floats.ExecFloat.spec_convertWith {Source : Type u} {target : Type v} {SourceExact : Type w} {TargetExact : Type x} [ExactDecoder Source SourceExact] [ExactMap SourceExact TargetExact] [quantizer : Quantizer target TargetExact] (value : Source) (context : Quantizer.Context target) :
Quantizer.spec context (ExactDecoder.decodeTo value) (convertWith value context)

Explicit generic conversion satisfies the destination's declared quantization relation.

theorem FloatLib.Floats.ExecFloat.spec_convert {Source : Type u} {target : Type v} {SourceExact : Type w} {TargetExact : Type x} [ExactDecoder Source SourceExact] [ExactMap SourceExact TargetExact] [quantizer : Quantizer target TargetExact] [defaults : DefaultQuantizer target TargetExact] (value : Source) :

Default-context generic conversion satisfies the declared quantization relation.

theorem FloatLib.Floats.ExecFloat.spec_castWith {F : Type u} [Numerics.EncodedFormat F] {target : Type v} {SourceExact : Type w} {TargetExact : Type x} [ExactDecoder (ExecFloat F) SourceExact] [ExactMap SourceExact TargetExact] [quantizer : Quantizer target TargetExact] (value : ExecFloat F) (context : Quantizer.Context target) :
Quantizer.spec context (ExactDecoder.decodeTo value) (value.castWith context)

Explicit ExecFloat conversion satisfies the destination's declared relation.

theorem FloatLib.Floats.ExecFloat.spec_cast {F : Type u} [Numerics.EncodedFormat F] {target : Type v} {SourceExact : Type w} {TargetExact : Type x} [ExactDecoder (ExecFloat F) SourceExact] [ExactMap SourceExact TargetExact] [quantizer : Quantizer target TargetExact] [defaults : DefaultQuantizer target TargetExact] (value : ExecFloat F) :

Default-context ExecFloat conversion satisfies the declared relation.

@[simp]

A finite observation passes through preparation unchanged.

@[simp]

Infinity is rejected at the supplied operand position.

@[simp]

An exceptional observation is rejected at the supplied operand position.

@[simp]

Finite exact division rejects any denominator satisfying the domain's zero predicate.

@[simp]

Finite exact division applies the domain's / when the denominator is not zero.

@[simp]

Finite exact division reports division by zero exactly for declared-zero denominators.

@[simp]
theorem FloatLib.Floats.ExecFloat.Conversion.quantizePrepared_ok {Destination : Type u} {Exact : Type v} [quantizer : Quantizer Destination Exact] (context : Quantizer.Context Destination) (value : Numerics.NumericalValue Exact) :
quantizePrepared context (Except.ok value) = Quantizer.run context value

A successfully prepared exact observation is passed to the destination quantizer.

@[simp]
theorem FloatLib.Floats.ExecFloat.Conversion.quantizePrepared_error {Destination : Type u} {Exact : Type v} [quantizer : Quantizer Destination Exact] (context : Quantizer.Context Destination) (reason : ConversionFailure) :

A preparation failure reaches the public outcome unchanged.

@[simp]
theorem FloatLib.Floats.ExecFloat.Conversion.preparedSpec_ok {Destination : Type u} {Exact : Type v} [quantizer : Quantizer Destination Exact] (context : Quantizer.Context Destination) (value : Numerics.NumericalValue Exact) (outcome : ConversionOutcome Destination) :
PreparedSpec context (Except.ok value) outcome Quantizer.spec context value outcome

On successful preparation, PreparedSpec is exactly the destination relation.

@[simp]
theorem FloatLib.Floats.ExecFloat.Conversion.preparedSpec_error {Destination : Type u} {Exact : Type v} [quantizer : Quantizer Destination Exact] (context : Quantizer.Context Destination) (reason : ConversionFailure) (outcome : ConversionOutcome Destination) :
PreparedSpec context (Except.error reason) outcome outcome = ConversionOutcome.failure reason

On preparation failure, PreparedSpec requires the same public failure.

theorem FloatLib.Floats.ExecFloat.Conversion.preparedSpec_quantizePrepared {Destination : Type u} {Exact : Type v} [quantizer : Quantizer Destination Exact] (context : Quantizer.Context Destination) (prepared : Except ConversionFailure (Numerics.NumericalValue Exact)) :
PreparedSpec context prepared (quantizePrepared context prepared)

Preparation followed by the executable quantizer satisfies PreparedSpec.

theorem FloatLib.Floats.ExecFloat.ExactExpression.operand_eq_ok_of_decodeTo_eq_finite {Source : Type u} {SourceExact : Type v} {Exact : Type w} [ExactDecoder Source SourceExact] [ExactMap SourceExact Exact] (index : ) (source : Source) (value : Exact) (hdecode : ExactDecoder.decodeTo source = Numerics.NumericalValue.finite value) :
operand index source = Except.ok value

A finite decoded operand enters an exact expression unchanged.

theorem FloatLib.Floats.ExecFloat.ExactExpression.operand_eq_error_of_decodeTo_eq_infinity {Source : Type u} {SourceExact : Type v} {Exact : Type w} [ExactDecoder Source SourceExact] [ExactMap SourceExact Exact] (index : ) (source : Source) (negative : Bool) (hdecode : ExactDecoder.decodeTo source = Numerics.NumericalValue.infinity negative) :

A decoded infinity identifies the numbered operand that made the expression invalid.

theorem FloatLib.Floats.ExecFloat.ExactExpression.operand_eq_error_of_decodeTo_eq_exceptional {Source : Type u} {SourceExact : Type v} {Exact : Type w} [ExactDecoder Source SourceExact] [ExactMap SourceExact Exact] (index : ) (source : Source) (value : Numerics.ExceptionalValue) (hdecode : ExactDecoder.decodeTo source = Numerics.NumericalValue.exceptional value) :

A decoded exceptional value identifies the numbered operand that caused rejection.

@[simp]

A successful exact expression becomes a finite input to the sole quantization step.

@[simp]

An exact-expression failure is preserved before quantization.

@[simp]
theorem FloatLib.Floats.ExecFloat.ExactExpression.div_of_is_zero {Exact : Type u} [Zero Exact] [self : Numerics.ExactZero Exact] [Div Exact] (numerator denominator : Exact) (hdenominator : Numerics.ExactZero.isZero denominator) :

Checked exact division rejects any denominator satisfying the domain's zero predicate.

@[simp]
theorem FloatLib.Floats.ExecFloat.ExactExpression.div_zero {Exact : Type u} [Zero Exact] [self : Numerics.ExactZero Exact] [Div Exact] (numerator : Exact) :

Checked exact division reports division by canonical zero.

@[simp]
theorem FloatLib.Floats.ExecFloat.ExactExpression.div_of_not_is_zero {Exact : Type u} [Zero Exact] [self : Numerics.ExactZero Exact] [Div Exact] (numerator denominator : Exact) (hdenominator : ¬Numerics.ExactZero.isZero denominator) :
div numerator denominator = Except.ok (numerator / denominator)

Checked exact division applies the exact domain's / when the denominator is nonzero.

The premise uses the exact domain's declared zero proposition. A domain such as SignedRat can therefore recognize several zero representations and prove a bridge from that proposition to its numerical meaning.

@[simp]
theorem FloatLib.Floats.ExecFloat.ExactExpression.div_eq_error_iff {Exact : Type u} [Zero Exact] [self : Numerics.ExactZero Exact] [Div Exact] (numerator denominator : Exact) :

Checked exact division reports division by zero exactly for declared-zero denominators.

theorem FloatLib.Floats.ExecFloat.ConversionProof.preparedSpec_roundOnceWith {result : Type w} {Exact : Type z} [quantizer : Quantizer result Exact] (context : Quantizer.Context result) (expression : ExactExpression Exact) :
Conversion.PreparedSpec context expression.toPrepared (roundOnceWith context expression)

An exact expression followed by one explicit-context quantization satisfies the destination's declared relation, or returns the expression's failure unchanged.

theorem FloatLib.Floats.ExecFloat.ConversionProof.preparedSpec_roundOnce {result : Type w} {Exact : Type z} [quantizer : Quantizer result Exact] [defaults : DefaultQuantizer result Exact] (expression : ExactExpression Exact) :

An exact expression followed by the default quantization context satisfies the same composed contract.

@[simp]
theorem FloatLib.Floats.ExecFloat.ConversionProof.roundOnceWith_ok {result : Type w} {Exact : Type z} [quantizer : Quantizer result Exact] (context : Quantizer.Context result) (value : Exact) :

A successful expression is sent directly to the quantizer as one finite exact value.

@[simp]
theorem FloatLib.Floats.ExecFloat.ConversionProof.roundOnceWith_error {result : Type w} {Exact : Type z} [quantizer : Quantizer result Exact] (context : Quantizer.Context result) (reason : ConversionFailure) :

An exact-expression failure bypasses the destination quantizer unchanged.

theorem FloatLib.Floats.ExecFloat.ConversionProof.preparedSpec_addAsWith {Left : Type u} {Right : Type v} {result : Type w} {LeftExact : Type x} {RightExact : Type y} {Exact : Type z} [ExactDecoder Left LeftExact] [ExactMap LeftExact Exact] [ExactDecoder Right RightExact] [ExactMap RightExact Exact] [Add Exact] [quantizer : Quantizer result Exact] (context : Quantizer.Context result) (left : Left) (right : Right) :

The context's addition hook followed by one quantization satisfies the composed relation.

theorem FloatLib.Floats.ExecFloat.ConversionProof.preparedSpec_addAs {Left : Type u} {Right : Type v} {result : Type w} {LeftExact : Type x} {RightExact : Type y} {Exact : Type z} [ExactDecoder Left LeftExact] [ExactMap LeftExact Exact] [ExactDecoder Right RightExact] [ExactMap RightExact Exact] [Add Exact] [quantizer : Quantizer result Exact] [defaults : DefaultQuantizer result Exact] (left : Left) (right : Right) :

The default-context addition hook satisfies the same composed relation.

theorem FloatLib.Floats.ExecFloat.ConversionProof.preparedSpec_subAsWith {Left : Type u} {Right : Type v} {result : Type w} {LeftExact : Type x} {RightExact : Type y} {Exact : Type z} [ExactDecoder Left LeftExact] [ExactMap LeftExact Exact] [ExactDecoder Right RightExact] [ExactMap RightExact Exact] [Sub Exact] [quantizer : Quantizer result Exact] (context : Quantizer.Context result) (left : Left) (right : Right) :

The context's subtraction hook followed by one quantization satisfies the composed relation.

theorem FloatLib.Floats.ExecFloat.ConversionProof.preparedSpec_subAs {Left : Type u} {Right : Type v} {result : Type w} {LeftExact : Type x} {RightExact : Type y} {Exact : Type z} [ExactDecoder Left LeftExact] [ExactMap LeftExact Exact] [ExactDecoder Right RightExact] [ExactMap RightExact Exact] [Sub Exact] [quantizer : Quantizer result Exact] [defaults : DefaultQuantizer result Exact] (left : Left) (right : Right) :

The default-context subtraction hook satisfies the same composed relation.

theorem FloatLib.Floats.ExecFloat.ConversionProof.preparedSpec_mulAsWith {Left : Type u} {Right : Type v} {result : Type w} {LeftExact : Type x} {RightExact : Type y} {Exact : Type z} [ExactDecoder Left LeftExact] [ExactMap LeftExact Exact] [ExactDecoder Right RightExact] [ExactMap RightExact Exact] [Mul Exact] [quantizer : Quantizer result Exact] (context : Quantizer.Context result) (left : Left) (right : Right) :
Conversion.PreparedSpec context (Conversion.prepareBinary (fun (x1 x2 : Exact) => x1 * x2) (ExactDecoder.decodeTo left) (ExactDecoder.decodeTo right)) (mulAsWith context left right)

Exact multiplication followed by one quantization satisfies the composed relational contract.

theorem FloatLib.Floats.ExecFloat.ConversionProof.preparedSpec_mulAs {Left : Type u} {Right : Type v} {result : Type w} {LeftExact : Type x} {RightExact : Type y} {Exact : Type z} [ExactDecoder Left LeftExact] [ExactMap LeftExact Exact] [ExactDecoder Right RightExact] [ExactMap RightExact Exact] [Mul Exact] [quantizer : Quantizer result Exact] [defaults : DefaultQuantizer result Exact] (left : Left) (right : Right) :

Default-context exact multiplication satisfies the composed relational contract.

theorem FloatLib.Floats.ExecFloat.ConversionProof.preparedSpec_divAsWith {Left : Type u} {Right : Type v} {result : Type w} {LeftExact : Type x} {RightExact : Type y} {Exact : Type z} [ExactDecoder Left LeftExact] [ExactMap LeftExact Exact] [ExactDecoder Right RightExact] [ExactMap RightExact Exact] [Zero Exact] [Numerics.ExactZero Exact] [Div Exact] [quantizer : Quantizer result Exact] (context : Quantizer.Context result) (left : Left) (right : Right) :

Exact division followed by one quantization satisfies the composed relational contract.

theorem FloatLib.Floats.ExecFloat.ConversionProof.preparedSpec_divAs {Left : Type u} {Right : Type v} {result : Type w} {LeftExact : Type x} {RightExact : Type y} {Exact : Type z} [ExactDecoder Left LeftExact] [ExactMap LeftExact Exact] [ExactDecoder Right RightExact] [ExactMap RightExact Exact] [Zero Exact] [Numerics.ExactZero Exact] [Div Exact] [quantizer : Quantizer result Exact] [defaults : DefaultQuantizer result Exact] (left : Left) (right : Right) :

Default-context exact division satisfies the same composed relational contract.

theorem FloatLib.Floats.ExecFloat.ConversionProof.preparedSpec_fmaAsWith {Left : Type u} {Right : Type v} {result : Type w} {LeftExact : Type x} {RightExact : Type y} {Exact : Type z} {Addend : Type a} {AddendExact : Type b} [ExactDecoder Left LeftExact] [ExactMap LeftExact Exact] [ExactDecoder Right RightExact] [ExactMap RightExact Exact] [ExactDecoder Addend AddendExact] [ExactMap AddendExact Exact] [Mul Exact] [Add Exact] [quantizer : Quantizer result Exact] (context : Quantizer.Context result) (left : Left) (right : Right) (addend : Addend) :

Exact fused multiply-add followed by one quantization satisfies the composed contract.

theorem FloatLib.Floats.ExecFloat.ConversionProof.preparedSpec_fmaAs {Left : Type u} {Right : Type v} {result : Type w} {LeftExact : Type x} {RightExact : Type y} {Exact : Type z} {Addend : Type a} {AddendExact : Type b} [ExactDecoder Left LeftExact] [ExactMap LeftExact Exact] [ExactDecoder Right RightExact] [ExactMap RightExact Exact] [ExactDecoder Addend AddendExact] [ExactMap AddendExact Exact] [Mul Exact] [Add Exact] [quantizer : Quantizer result Exact] [defaults : DefaultQuantizer result Exact] (left : Left) (right : Right) (addend : Addend) :

Default-context exact fused multiply-add satisfies the same composed relational contract.