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
@@ -0,0 +1,2 @@
def linear_interp(x0: Float64, y0: Float64, x1: Float64, y1: Float64, x: Float64) -> Float64:
return y0 + (y1 - y0) * (x - x0) / (x1 - x0)
@@ -0,0 +1,2 @@
def linear_interp(x0, y0, x1, y1, x):
return y0 + (y1 - y0) * (x - x0) / (x1 - x0)
@@ -0,0 +1,37 @@
{
"name": "LinearInterp",
"summary": "The value at X on the straight line through (X0, Y0) and (X1, Y1).",
"tags": [
"interpolation"
],
"sources": [
{
"title": "Any numerical methods text: linear interpolation"
}
],
"cases": [
{
"call": "LinearInterp(0.0, 0.0, 10.0, 5.0, 4.0)"
},
{
"call": "LinearInterp(1.0, 2.0, 3.0, 2.0, 2.5)"
},
{
"call": "LinearInterp(0.0, 1.0, 1.0, 3.0, 2.0)"
}
],
"references": [
{
"lang": "python",
"level": "plain",
"file": "ref/python/plain.py",
"note": "The same formula."
},
{
"lang": "mojo",
"level": "plain",
"file": "ref/mojo/plain.mojo",
"note": "The same formula in Mojo."
}
]
}
@@ -0,0 +1,7 @@
// LinearInterp: the value at X on the straight line through (X0, Y0) and
// (X1, Y1). X may lie outside [X0, X1]: that is extrapolation, and the line
// carries on. The REQUIRE names the difference the division divides by.
FUNCTION LinearInterp(X0 : FLOAT, Y0 : FLOAT, X1 : FLOAT, Y1 : FLOAT, X : FLOAT) RETURNS FLOAT
REQUIRE X1 - X0 != 0.0
RETURN Y0 + (Y1 - Y0) * (X - X0) / (X1 - X0)
ENDFUNCTION
+5
View File
@@ -0,0 +1,5 @@
{
"name": "numerics",
"title": "Numerics",
"summary": "Summing, interpolating and integrating with FLOAT values, and what rounding does to each."
}
@@ -0,0 +1,5 @@
def trapezoid(y: List[Float64], h: Float64) -> Float64:
var total: Float64 = (y[0] + y[len(y) - 1]) / 2.0
for i in range(1, len(y) - 1):
total = total + y[i]
return total * h
@@ -0,0 +1,5 @@
def trapezoid(y, h):
total = (y[0] + y[len(y) - 1]) / 2.0
for i in range(1, len(y) - 1):
total += y[i]
return total * h
+38
View File
@@ -0,0 +1,38 @@
{
"name": "Trapezoid",
"summary": "The integral of samples Y taken H apart, by the trapezoidal rule.",
"tags": [
"integration",
"quadrature"
],
"sources": [
{
"title": "Any numerical methods text: the composite trapezoidal rule"
}
],
"cases": [
{
"call": "Trapezoid([0.0, 1.0, 4.0, 9.0, 16.0], 1.0)"
},
{
"call": "Trapezoid([1.0, 1.0], 0.5)"
},
{
"call": "Trapezoid([0.0, 0.25, 1.0], 0.5)"
}
],
"references": [
{
"lang": "python",
"level": "plain",
"file": "ref/python/plain.py",
"note": "The same sums."
},
{
"lang": "mojo",
"level": "plain",
"file": "ref/mojo/plain.mojo",
"note": "The same sums in Mojo."
}
]
}
+11
View File
@@ -0,0 +1,11 @@
// Trapezoid: the integral of samples Y taken H apart, by the composite
// trapezoidal rule: the ends count half, every inside sample counts whole.
// The error shrinks with H^2 for a smooth function.
FUNCTION Trapezoid(Y : LIST OF FLOAT, H : FLOAT) RETURNS FLOAT
REQUIRE LENGTH(Y) >= 2
DECLARE Total : FLOAT <- (Y[0] + Y[LENGTH(Y) - 1]) / 2.0
FOR I <- 1 TO LENGTH(Y) - 1
Total <- Total + Y[I]
NEXT I
RETURN Total * H
ENDFUNCTION