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,25 @@
def needleman_wunsch_score(s: String, t: String, match_: Int, mismatch: Int, gap: Int) -> Int:
var n = s.count_codepoints()
var m = t.count_codepoints()
var d = List[List[Int]]()
for i in range(n + 1):
var row = List[Int]()
for j in range(m + 1):
row.append(0)
d.append(row^)
for i in range(n + 1):
d[i][0] = i * gap
for j in range(1, m + 1):
d[0][j] = j * gap
for i in range(1, n + 1):
for j in range(1, m + 1):
var score = mismatch
if String(s[codepoint=i - 1]) == String(t[codepoint=j - 1]):
score = match_
var best = d[i - 1][j - 1] + score
if d[i - 1][j] + gap > best:
best = d[i - 1][j] + gap
if d[i][j - 1] + gap > best:
best = d[i][j - 1] + gap
d[i][j] = best
return d[n][m]
@@ -0,0 +1,12 @@
def needleman_wunsch_score(s, t, match, mismatch, gap):
n, m = len(s), len(t)
d = [[0] * (m + 1) for _ in range(n + 1)]
for i in range(n + 1):
d[i][0] = i * gap
for j in range(1, m + 1):
d[0][j] = j * gap
for i in range(1, n + 1):
for j in range(1, m + 1):
diagonal = d[i - 1][j - 1] + (match if s[i - 1] == t[j - 1] else mismatch)
d[i][j] = max(diagonal, d[i - 1][j] + gap, d[i][j - 1] + gap)
return d[n][m]
@@ -0,0 +1,10 @@
def needleman_wunsch_score(s, t, match, mismatch, gap):
m = len(t)
prev = [j * gap for j in range(m + 1)]
for i in range(1, len(s) + 1):
cur = [i * gap] + [0] * m
for j in range(1, m + 1):
diagonal = prev[j - 1] + (match if s[i - 1] == t[j - 1] else mismatch)
cur[j] = max(diagonal, prev[j] + gap, cur[j - 1] + gap)
prev = cur
return prev[m]
@@ -0,0 +1,48 @@
{
"name": "NeedlemanWunschScore",
"summary": "The best score of a global alignment of two sequences with a linear gap model, by dynamic programming.",
"tags": [
"alignment",
"dynamic programming"
],
"sources": [
{
"title": "Needleman and Wunsch, A general method applicable to the search for similarities in the amino acid sequence of two proteins, J. Mol. Biol. 48 (1970)",
"url": "https://doi.org/10.1016/0022-2836(70)90057-4"
}
],
"cases": [
{
"call": "NeedlemanWunschScore(\"GATTACA\", \"GCATGCU\", 1, -1, -1)"
},
{
"call": "NeedlemanWunschScore(\"ACGT\", \"ACGT\", 1, -1, -2)"
},
{
"call": "NeedlemanWunschScore(\"\", \"ACG\", 1, -1, -2)"
},
{
"call": "NeedlemanWunschScore(\"AAAA\", \"TTTT\", 2, -3, -1)"
}
],
"references": [
{
"lang": "python",
"level": "plain",
"file": "ref/python/plain.py",
"note": "The same table, filled row by row."
},
{
"lang": "python",
"level": "two-rows",
"file": "ref/python/two-rows.py",
"note": "Only the previous row is kept: memory grows with the length of T alone, the first step toward Hirschberg's linear-space alignment."
},
{
"lang": "mojo",
"level": "plain",
"file": "ref/mojo/plain.mojo",
"note": "The same table in Mojo, as a list of rows."
}
]
}
@@ -0,0 +1,27 @@
// NeedlemanWunschScore: the best score of a global alignment of S and T
// (Needleman and Wunsch, 1970) with a linear gap model, by dynamic
// programming. D[I, J] is the best score of aligning the first I letters of
// S with the first J letters of T; each cell takes the best of three moves:
// align S[I - 1] with T[J - 1], or put a gap in one sequence or the other.
USE align.MatchScore
USE align.LinearGapPenalty
FUNCTION NeedlemanWunschScore(S : STRING, T : STRING, Match : INTEGER, Mismatch : INTEGER, Gap : INTEGER) RETURNS INTEGER
DECLARE N : INTEGER <- LENGTH(S)
DECLARE M : INTEGER <- LENGTH(T)
DECLARE D : ARRAY[N + 1, M + 1] OF INTEGER
FOR I <- 0 TO N + 1
D[I, 0] <- LinearGapPenalty(I, Gap)
NEXT I
FOR J <- 1 TO M + 1
D[0, J] <- LinearGapPenalty(J, Gap)
NEXT J
FOR I <- 1 TO N + 1
FOR J <- 1 TO M + 1
DECLARE Diagonal : INTEGER <- D[I - 1, J - 1] + MatchScore(S[I - 1], T[J - 1], Match, Mismatch)
DECLARE Down : INTEGER <- D[I - 1, J] + Gap
DECLARE Across : INTEGER <- D[I, J - 1] + Gap
D[I, J] <- MAX(Diagonal, Down, Across)
NEXT J
NEXT I
RETURN D[N, M]
ENDFUNCTION