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:
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": "5a0a04933596877e8f881204d2508c4fd6f4fc5a37aa4d0a0dce95ac4da45730",
|
||||
"verdict": "verified",
|
||||
"summary": "3 verified, 0 errors"
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
method AffineGapPenalty(length: int, open: int, extend: int) returns (result: int)
|
||||
requires length >= 1
|
||||
ensures result == open + (length - 1) * extend
|
||||
{
|
||||
result := open + (length - 1) * extend;
|
||||
}
|
||||
|
||||
// Position by position: the first position opens the gap, every later one extends it.
|
||||
method AffineByPositions(length: int, open: int, extend: int) returns (score: int)
|
||||
requires length >= 1
|
||||
ensures score == open + (length - 1) * extend
|
||||
{
|
||||
score := open;
|
||||
var i := 1;
|
||||
while i < length
|
||||
invariant 1 <= i <= length
|
||||
invariant score == open + (i - 1) * extend
|
||||
{
|
||||
score := score + extend;
|
||||
i := i + 1;
|
||||
}
|
||||
}
|
||||
|
||||
// With the same score for opening and extending, the affine model is the linear one.
|
||||
lemma AffineIsLinearWhenEqual(length: int, gap: int)
|
||||
requires length >= 1
|
||||
ensures gap + (length - 1) * gap == length * gap
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
def affine_gap_penalty(length: Int, open_: Int, extend: Int) -> Int:
|
||||
return open_ + (length - 1) * extend
|
||||
@@ -0,0 +1,2 @@
|
||||
def affine_gap_penalty(length, open_, extend):
|
||||
return open_ + (length - 1) * extend
|
||||
@@ -0,0 +1,44 @@
|
||||
{
|
||||
"name": "AffineGapPenalty",
|
||||
"summary": "The score of a gap of Length positions when opening a gap scores Open and each further position scores Extend.",
|
||||
"tags": [
|
||||
"alignment",
|
||||
"gap model"
|
||||
],
|
||||
"sources": [
|
||||
{
|
||||
"title": "Gotoh, An improved algorithm for matching biological sequences, J. Mol. Biol. 162 (1982)",
|
||||
"url": "https://doi.org/10.1016/0022-2836(82)90398-9"
|
||||
}
|
||||
],
|
||||
"cases": [
|
||||
{
|
||||
"call": "AffineGapPenalty(1, -10, -1)"
|
||||
},
|
||||
{
|
||||
"call": "AffineGapPenalty(5, -10, -1)"
|
||||
},
|
||||
{
|
||||
"call": "AffineGapPenalty(3, -2, -2)"
|
||||
}
|
||||
],
|
||||
"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."
|
||||
}
|
||||
],
|
||||
"proof": {
|
||||
"lang": "dafny",
|
||||
"file": "proof/affine_gap.dfy",
|
||||
"note": "The formula equals the score built position by position, and with Open == Extend it equals the linear model."
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// AffineGapPenalty: the score of a gap of Length positions under an affine
|
||||
// gap model (Gotoh, 1982): Open for the first position and Extend for each
|
||||
// one after it, so one long gap costs less than many short ones. With
|
||||
// Open == Extend it is the linear model.
|
||||
FUNCTION AffineGapPenalty(Length : INTEGER, Open : INTEGER, Extend : INTEGER) RETURNS INTEGER
|
||||
REQUIRE Length >= 1
|
||||
ENSURE RESULT == Open + (Length - 1) * Extend
|
||||
RETURN Open + (Length - 1) * Extend
|
||||
ENDFUNCTION
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"tool": "dafny",
|
||||
"version": "4.11.0",
|
||||
"flags": [
|
||||
"verify",
|
||||
"--resource-limit",
|
||||
"20000000"
|
||||
],
|
||||
"source_sha256": "1d61017b086bed77518f358dcbbc0fd011139347f77beb2d39479d9db7edb2de",
|
||||
"verdict": "verified",
|
||||
"summary": "2 verified, 0 errors"
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// A gap of `length` positions under a linear model: each position scores `gap`.
|
||||
method LinearGapPenalty(length: int, gap: int) returns (result: int)
|
||||
requires length >= 0
|
||||
ensures result == length * gap
|
||||
{
|
||||
result := length * gap;
|
||||
}
|
||||
|
||||
// The same score, built one position at a time: the product is the model, not a shortcut.
|
||||
method LinearGapByPositions(length: int, gap: int) returns (score: int)
|
||||
requires length >= 0
|
||||
ensures score == length * gap
|
||||
{
|
||||
score := 0;
|
||||
var i := 0;
|
||||
while i < length
|
||||
invariant 0 <= i <= length
|
||||
invariant score == i * gap
|
||||
{
|
||||
score := score + gap;
|
||||
i := i + 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
def linear_gap_penalty(length: Int, gap: Int) -> Int:
|
||||
return length * gap
|
||||
@@ -0,0 +1,2 @@
|
||||
def linear_gap_penalty(length, gap):
|
||||
return length * gap
|
||||
@@ -0,0 +1,43 @@
|
||||
{
|
||||
"name": "LinearGapPenalty",
|
||||
"summary": "The score of a gap of Length positions when every position of a gap scores Gap.",
|
||||
"tags": [
|
||||
"alignment",
|
||||
"gap model"
|
||||
],
|
||||
"sources": [
|
||||
{
|
||||
"title": "Durbin, Eddy, Krogh and Mitchison, Biological Sequence Analysis, section 2.2"
|
||||
}
|
||||
],
|
||||
"cases": [
|
||||
{
|
||||
"call": "LinearGapPenalty(3, -2)"
|
||||
},
|
||||
{
|
||||
"call": "LinearGapPenalty(0, -2)"
|
||||
},
|
||||
{
|
||||
"call": "LinearGapPenalty(10, -1)"
|
||||
}
|
||||
],
|
||||
"references": [
|
||||
{
|
||||
"lang": "python",
|
||||
"level": "plain",
|
||||
"file": "ref/python/plain.py",
|
||||
"note": "The same product."
|
||||
},
|
||||
{
|
||||
"lang": "mojo",
|
||||
"level": "plain",
|
||||
"file": "ref/mojo/plain.mojo",
|
||||
"note": "The same product in Mojo."
|
||||
}
|
||||
],
|
||||
"proof": {
|
||||
"lang": "dafny",
|
||||
"file": "proof/linear_gap.dfy",
|
||||
"note": "The product equals the score built position by position: each of the Length positions adds Gap."
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// LinearGapPenalty: the score of a gap of Length positions under a linear gap
|
||||
// model, where every position of a gap scores Gap (usually negative).
|
||||
FUNCTION LinearGapPenalty(Length : INTEGER, Gap : INTEGER) RETURNS INTEGER
|
||||
REQUIRE Length >= 0
|
||||
ENSURE RESULT == Length * Gap
|
||||
RETURN Length * Gap
|
||||
ENDFUNCTION
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"tool": "dafny",
|
||||
"version": "4.11.0",
|
||||
"flags": [
|
||||
"verify",
|
||||
"--resource-limit",
|
||||
"20000000"
|
||||
],
|
||||
"source_sha256": "374ce6664efd8802bb90e120a1107375c0ab607c9829987b26d73d5f5fd4b009",
|
||||
"verdict": "verified",
|
||||
"summary": "1 verified, 0 errors"
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
method MatchScore(a: char, b: char, match_: int, mismatch: int) returns (r: int)
|
||||
ensures (a == b && r == match_) || (a != b && r == mismatch)
|
||||
{
|
||||
if a == b { r := match_; } else { r := mismatch; }
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
def match_score(a: String, b: String, match_: Int, mismatch: Int) -> Int:
|
||||
if a == b:
|
||||
return match_
|
||||
return mismatch
|
||||
@@ -0,0 +1,2 @@
|
||||
def match_score(a, b, match, mismatch):
|
||||
return match if a == b else mismatch
|
||||
@@ -0,0 +1,43 @@
|
||||
{
|
||||
"name": "MatchScore",
|
||||
"summary": "The score of aligning letter A with letter B: Match when they are the same, Mismatch otherwise.",
|
||||
"tags": [
|
||||
"alignment",
|
||||
"scoring"
|
||||
],
|
||||
"sources": [
|
||||
{
|
||||
"title": "Durbin, Eddy, Krogh and Mitchison, Biological Sequence Analysis, section 2.2"
|
||||
}
|
||||
],
|
||||
"cases": [
|
||||
{
|
||||
"call": "MatchScore('A', 'A', 1, -1)"
|
||||
},
|
||||
{
|
||||
"call": "MatchScore('A', 'G', 1, -1)"
|
||||
},
|
||||
{
|
||||
"call": "MatchScore('T', 'T', 5, -4)"
|
||||
}
|
||||
],
|
||||
"references": [
|
||||
{
|
||||
"lang": "python",
|
||||
"level": "plain",
|
||||
"file": "ref/python/plain.py",
|
||||
"note": "The same test."
|
||||
},
|
||||
{
|
||||
"lang": "mojo",
|
||||
"level": "plain",
|
||||
"file": "ref/mojo/plain.mojo",
|
||||
"note": "The same test in Mojo; a letter is a String of one character."
|
||||
}
|
||||
],
|
||||
"proof": {
|
||||
"lang": "dafny",
|
||||
"file": "proof/match_score.dfy",
|
||||
"note": "The score is Match exactly when the letters agree."
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// MatchScore: the score of aligning letter A with letter B: Match when they
|
||||
// are the same letter, Mismatch otherwise. The simplest substitution score;
|
||||
// a substitution matrix such as BLOSUM62 replaces it for proteins.
|
||||
FUNCTION MatchScore(A : CHAR, B : CHAR, Match : INTEGER, Mismatch : INTEGER) RETURNS INTEGER
|
||||
ENSURE (A == B AND RESULT == Match) OR (A != B AND RESULT == Mismatch)
|
||||
IF A == B THEN
|
||||
RETURN Match
|
||||
ENDIF
|
||||
RETURN Mismatch
|
||||
ENDFUNCTION
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"name": "align",
|
||||
"title": "Sequence alignment",
|
||||
"summary": "Scores for aligning two sequences: gap models, letter scores, and global alignment by dynamic programming."
|
||||
}
|
||||
@@ -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
|
||||
Reference in new issue
Block a user