Start pplus-library: 16 verified routines in six modules

Routines written once in pseudo and checked at every level, so programs
can use them by name instead of rewriting and re-verifying them:

- stats: Mean, Variance (two-pass)
- numerics: KahanSum, LinearInterp, Trapezoid
- align: LinearGapPenalty, AffineGapPenalty, MatchScore,
  NeedlemanWunschScore
- md: LennardJones, LennardJonesForce, MinimumImage, PairDistance
- seq: GCContent, HammingDistance
- special: ErfApprox (Abramowitz and Stegun 7.1.26)

Each has contracts, cases, reference implementations in Python, Mojo
and (for the numerical ones) C at more than one level, and nine have a
Dafny proof over exact numbers. status.json and STATUS.md are written by
pplus-core's library-check: every check passes or is open/unverified as
listed there. The Mojo references still need the pinned compiler's
answers (library-verify-mojo, network).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FgvbRdoEheAHqsRmr2WuKZ
This commit is contained in:
alexmandClaude committed 2026-10-05 11:58:22 +00:00
commit df12b2a008
103 files changed
+3025

No files matched your search

@@ -0,0 +1,12 @@
{
"tool": "dafny",
"version": "4.11.0",
"flags": [
"verify",
"--resource-limit",
"20000000"
],
"source_sha256": "c4ce4ea3415a128e81393b13176e9cbf9f1266c14e2fe3d7588dacdc38cf259a",
"verdict": "verified",
"summary": "2 verified, 0 errors"
}
@@ -0,0 +1,26 @@
function Sum(x: seq<real>): real
{
if |x| == 0 then 0.0 else Sum(x[..|x| - 1]) + x[|x| - 1]
}
// In exact arithmetic Kahan summation is the plain sum, and its compensation is always 0.
method KahanSum(x: seq<real>) returns (total: real)
ensures total == Sum(x)
{
total := 0.0;
var c := 0.0;
var i := 0;
while i < |x|
invariant 0 <= i <= |x|
invariant total == Sum(x[..i])
invariant c == 0.0
{
assert x[..i + 1][..i] == x[..i];
var y := x[i] - c;
var t := total + y;
c := (t - total) - y;
total := t;
i := i + 1;
}
assert x[..|x|] == x;
}
+10
View File
@@ -0,0 +1,10 @@
double kahan_sum(const double *x, long n) {
double total = 0.0, c = 0.0;
for (long i = 0; i < n; i++) {
double y = x[i] - c;
double t = total + y;
c = (t - total) - y;
total = t;
}
return total;
}
@@ -0,0 +1,9 @@
def kahan_sum(x: List[Float64]) -> Float64:
var total: Float64 = 0.0
var c: Float64 = 0.0
for i in range(len(x)):
var y = x[i] - c
var t = total + y
c = (t - total) - y
total = t
return total
@@ -0,0 +1,9 @@
def kahan_sum(x):
total = 0.0
c = 0.0
for v in x:
y = v - c
t = total + y
c = (t - total) - y
total = t
return total
+57
View File
@@ -0,0 +1,57 @@
{
"name": "KahanSum",
"summary": "The sum of a list of FLOAT values by compensated summation, whose error does not grow with the length of the list.",
"tags": [
"summation",
"rounding",
"precision"
],
"sources": [
{
"title": "Kahan, Further remarks on reducing truncation errors, CACM 8(1), 1965",
"url": "https://doi.org/10.1145/363707.363723"
},
{
"title": "Higham, Accuracy and Stability of Numerical Algorithms, section 4.3"
}
],
"cases": [
{
"call": "KahanSum([1.0, 2.0, 3.0])"
},
{
"call": "KahanSum([1.0, 1.0e-16, 1.0e-16, 1.0e-16, 1.0e-16, 1.0e-16, 1.0e-16, 1.0e-16, 1.0e-16, 1.0e-16, 1.0e-16])"
},
{
"call": "KahanSum([0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1])"
},
{
"call": "KahanSum([])"
}
],
"references": [
{
"lang": "python",
"level": "plain",
"file": "ref/python/plain.py",
"note": "The same compensated loop."
},
{
"lang": "c",
"level": "plain",
"file": "ref/c/plain.c",
"note": "The same loop in C. It survives -O2; it would not survive -ffast-math, which lets the compiler treat (t - total) - y as 0."
},
{
"lang": "mojo",
"level": "plain",
"file": "ref/mojo/plain.mojo",
"note": "The same loop in Mojo."
}
],
"proof": {
"lang": "dafny",
"file": "proof/kahan_sum.dfy",
"note": "Over real numbers the compensation C stays 0 and the result is exactly the sum: what Kahan summation corrects is rounding, and nothing else."
}
}
+17
View File
@@ -0,0 +1,17 @@
// KahanSum: the sum of a list of FLOAT values by compensated summation
// (Kahan, 1965). C holds the low-order part the last addition lost, and the
// next addition puts it back, so the error does not grow with the length of
// the list. In exact arithmetic C is always 0 and this is the plain sum: the
// compensation exists only because of rounding. A compiler allowed to
// reorder FLOAT arithmetic (-ffast-math) can simplify C away.
FUNCTION KahanSum(X : LIST OF FLOAT) RETURNS FLOAT
DECLARE Total : FLOAT <- 0.0
DECLARE C : FLOAT <- 0.0
FOR I <- 0 TO LENGTH(X)
DECLARE Y : FLOAT <- X[I] - C
DECLARE T : FLOAT <- Total + Y
C <- (T - Total) - Y
Total <- T
NEXT I
RETURN Total
ENDFUNCTION