TorchLean API

FloatLib.Kernels.FixedWord.IntegerSquareRoot.Proof

Verified native-word integer square root #

sqrtIter_toNat identifies the native Newton iteration with Nat.sqrt.iter, and sqrt_toNat proves that its initialization computes the floor square root. The native average cannot overflow. The runtime module proves termination by a strictly decreasing guess.

natSqrt_eq_sqrtNat installs a compiler substitution for Nat.sqrt, using this implementation below 2^64 and the arbitrary-precision definition otherwise.

@[simp]
theorem FloatLib.Numerics.FixedWord.IntegerSquareRoot.average_toNat (left right : UInt64) :
(average left right).toNat = (left.toNat + right.toNat) / 2

The overflow-free native average has its ordinary natural-number value.

@[simp]

The native Newton iteration is exactly Lean's natural-number iteration.

@[simp]

Native-word integer square root agrees exactly with Nat.sqrt.

The bounded native implementation preserves natural-number square root.

@[csimp]

The compiler uses native-word square root whenever the input fits in one word.

This @[csimp] theorem is global: every module that imports it, in particular everything that imports FloatLib.Kernels, compiles Nat.sqrt to sqrtNat, including uses unrelated to FloatLib. The substitution is sound by sqrtNat_eq_sqrt, and the fallback branch of sqrtNat is the unchanged logical definition, so only inputs below 2^64 take a different code path.