From df12b2a0082c251e25d12fc0f3df01bf4e101761 Mon Sep 17 00:00:00 2001 From: Alex Maldonado Date: Mon, 5 Oct 2026 11:58:22 +0000 Subject: [PATCH] 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 Claude-Session: https://claude.ai/code/session_01FgvbRdoEheAHqsRmr2WuKZ --- .gitignore | 2 + AGENTS.md | 30 + README.md | 54 + STATUS.md | 254 ++++ modules/align/affine-gap/golden/dafny.json | 12 + modules/align/affine-gap/proof/affine_gap.dfy | 29 + modules/align/affine-gap/ref/mojo/plain.mojo | 2 + modules/align/affine-gap/ref/python/plain.py | 2 + modules/align/affine-gap/routine.json | 44 + modules/align/affine-gap/routine.pseudo | 9 + modules/align/linear-gap/golden/dafny.json | 12 + modules/align/linear-gap/proof/linear_gap.dfy | 23 + modules/align/linear-gap/ref/mojo/plain.mojo | 2 + modules/align/linear-gap/ref/python/plain.py | 2 + modules/align/linear-gap/routine.json | 43 + modules/align/linear-gap/routine.pseudo | 7 + modules/align/match-score/golden/dafny.json | 12 + .../align/match-score/proof/match_score.dfy | 5 + modules/align/match-score/ref/mojo/plain.mojo | 4 + modules/align/match-score/ref/python/plain.py | 2 + modules/align/match-score/routine.json | 43 + modules/align/match-score/routine.pseudo | 10 + modules/align/module.json | 5 + .../ref/mojo/plain.mojo | 25 + .../ref/python/plain.py | 12 + .../ref/python/two-rows.py | 10 + .../align/needleman-wunsch-score/routine.json | 48 + .../needleman-wunsch-score/routine.pseudo | 27 + .../ref/mojo/multiply.mojo | 4 + .../ref/python/multiply.py | 4 + modules/md/lennard-jones-force/routine.json | 38 + modules/md/lennard-jones-force/routine.pseudo | 10 + modules/md/lennard-jones/golden/dafny.json | 12 + .../md/lennard-jones/proof/lennard_jones.dfy | 59 + modules/md/lennard-jones/ref/c/multiply.c | 5 + .../md/lennard-jones/ref/mojo/multiply.mojo | 4 + .../md/lennard-jones/ref/python/multiply.py | 4 + modules/md/lennard-jones/ref/python/powers.py | 2 + modules/md/lennard-jones/routine.json | 63 + modules/md/lennard-jones/routine.pseudo | 11 + modules/md/minimum-image/golden/dafny.json | 12 + .../md/minimum-image/proof/minimum_image.dfy | 25 + modules/md/minimum-image/ref/c/plain.c | 5 + modules/md/minimum-image/ref/mojo/plain.mojo | 5 + modules/md/minimum-image/ref/python/plain.py | 5 + modules/md/minimum-image/routine.json | 55 + modules/md/minimum-image/routine.pseudo | 8 + modules/md/module.json | 5 + modules/md/pair-distance/ref/mojo/plain.mojo | 10 + modules/md/pair-distance/ref/python/plain.py | 7 + modules/md/pair-distance/routine.json | 40 + modules/md/pair-distance/routine.pseudo | 11 + modules/numerics/kahan-sum/golden/dafny.json | 12 + .../numerics/kahan-sum/proof/kahan_sum.dfy | 26 + modules/numerics/kahan-sum/ref/c/plain.c | 10 + .../numerics/kahan-sum/ref/mojo/plain.mojo | 9 + .../numerics/kahan-sum/ref/python/plain.py | 9 + modules/numerics/kahan-sum/routine.json | 57 + modules/numerics/kahan-sum/routine.pseudo | 17 + .../linear-interp/ref/mojo/plain.mojo | 2 + .../linear-interp/ref/python/plain.py | 2 + modules/numerics/linear-interp/routine.json | 37 + modules/numerics/linear-interp/routine.pseudo | 7 + modules/numerics/module.json | 5 + .../numerics/trapezoid/ref/mojo/plain.mojo | 5 + .../numerics/trapezoid/ref/python/plain.py | 5 + modules/numerics/trapezoid/routine.json | 38 + modules/numerics/trapezoid/routine.pseudo | 11 + modules/seq/gc-content/golden/dafny.json | 12 + modules/seq/gc-content/proof/gc_content.dfy | 30 + modules/seq/gc-content/ref/mojo/plain.mojo | 7 + modules/seq/gc-content/ref/python/plain.py | 6 + modules/seq/gc-content/routine.json | 43 + modules/seq/gc-content/routine.pseudo | 12 + .../seq/hamming-distance/golden/dafny.json | 12 + .../proof/hamming_distance.dfy | 29 + .../seq/hamming-distance/ref/mojo/plain.mojo | 6 + .../seq/hamming-distance/ref/python/plain.py | 2 + modules/seq/hamming-distance/routine.json | 43 + modules/seq/hamming-distance/routine.pseudo | 15 + modules/seq/module.json | 5 + .../special/erf-approx/ref/mojo/plain.mojo | 11 + modules/special/erf-approx/ref/python/libm.py | 5 + .../special/erf-approx/ref/python/plain.py | 9 + modules/special/erf-approx/routine.json | 52 + modules/special/erf-approx/routine.pseudo | 15 + modules/special/module.json | 5 + modules/stats/mean/golden/dafny.json | 12 + modules/stats/mean/proof/mean.dfy | 49 + modules/stats/mean/ref/c/plain.c | 5 + modules/stats/mean/ref/mojo/plain.mojo | 5 + modules/stats/mean/ref/mojo/unrolled.mojo | 17 + modules/stats/mean/ref/python/fsum.py | 5 + modules/stats/mean/ref/python/plain.py | 5 + modules/stats/mean/routine.json | 69 ++ modules/stats/mean/routine.pseudo | 12 + modules/stats/module.json | 5 + modules/stats/variance/ref/mojo/plain.mojo | 9 + modules/stats/variance/ref/python/plain.py | 9 + .../stats/variance/ref/python/statistics.py | 5 + modules/stats/variance/routine.json | 46 + modules/stats/variance/routine.pseudo | 14 + status.json | 1053 +++++++++++++++++ 103 files changed, 3025 insertions(+) create mode 100644 .gitignore create mode 100644 AGENTS.md create mode 100644 README.md create mode 100644 STATUS.md create mode 100644 modules/align/affine-gap/golden/dafny.json create mode 100644 modules/align/affine-gap/proof/affine_gap.dfy create mode 100644 modules/align/affine-gap/ref/mojo/plain.mojo create mode 100644 modules/align/affine-gap/ref/python/plain.py create mode 100644 modules/align/affine-gap/routine.json create mode 100644 modules/align/affine-gap/routine.pseudo create mode 100644 modules/align/linear-gap/golden/dafny.json create mode 100644 modules/align/linear-gap/proof/linear_gap.dfy create mode 100644 modules/align/linear-gap/ref/mojo/plain.mojo create mode 100644 modules/align/linear-gap/ref/python/plain.py create mode 100644 modules/align/linear-gap/routine.json create mode 100644 modules/align/linear-gap/routine.pseudo create mode 100644 modules/align/match-score/golden/dafny.json create mode 100644 modules/align/match-score/proof/match_score.dfy create mode 100644 modules/align/match-score/ref/mojo/plain.mojo create mode 100644 modules/align/match-score/ref/python/plain.py create mode 100644 modules/align/match-score/routine.json create mode 100644 modules/align/match-score/routine.pseudo create mode 100644 modules/align/module.json create mode 100644 modules/align/needleman-wunsch-score/ref/mojo/plain.mojo create mode 100644 modules/align/needleman-wunsch-score/ref/python/plain.py create mode 100644 modules/align/needleman-wunsch-score/ref/python/two-rows.py create mode 100644 modules/align/needleman-wunsch-score/routine.json create mode 100644 modules/align/needleman-wunsch-score/routine.pseudo create mode 100644 modules/md/lennard-jones-force/ref/mojo/multiply.mojo create mode 100644 modules/md/lennard-jones-force/ref/python/multiply.py create mode 100644 modules/md/lennard-jones-force/routine.json create mode 100644 modules/md/lennard-jones-force/routine.pseudo create mode 100644 modules/md/lennard-jones/golden/dafny.json create mode 100644 modules/md/lennard-jones/proof/lennard_jones.dfy create mode 100644 modules/md/lennard-jones/ref/c/multiply.c create mode 100644 modules/md/lennard-jones/ref/mojo/multiply.mojo create mode 100644 modules/md/lennard-jones/ref/python/multiply.py create mode 100644 modules/md/lennard-jones/ref/python/powers.py create mode 100644 modules/md/lennard-jones/routine.json create mode 100644 modules/md/lennard-jones/routine.pseudo create mode 100644 modules/md/minimum-image/golden/dafny.json create mode 100644 modules/md/minimum-image/proof/minimum_image.dfy create mode 100644 modules/md/minimum-image/ref/c/plain.c create mode 100644 modules/md/minimum-image/ref/mojo/plain.mojo create mode 100644 modules/md/minimum-image/ref/python/plain.py create mode 100644 modules/md/minimum-image/routine.json create mode 100644 modules/md/minimum-image/routine.pseudo create mode 100644 modules/md/module.json create mode 100644 modules/md/pair-distance/ref/mojo/plain.mojo create mode 100644 modules/md/pair-distance/ref/python/plain.py create mode 100644 modules/md/pair-distance/routine.json create mode 100644 modules/md/pair-distance/routine.pseudo create mode 100644 modules/numerics/kahan-sum/golden/dafny.json create mode 100644 modules/numerics/kahan-sum/proof/kahan_sum.dfy create mode 100644 modules/numerics/kahan-sum/ref/c/plain.c create mode 100644 modules/numerics/kahan-sum/ref/mojo/plain.mojo create mode 100644 modules/numerics/kahan-sum/ref/python/plain.py create mode 100644 modules/numerics/kahan-sum/routine.json create mode 100644 modules/numerics/kahan-sum/routine.pseudo create mode 100644 modules/numerics/linear-interp/ref/mojo/plain.mojo create mode 100644 modules/numerics/linear-interp/ref/python/plain.py create mode 100644 modules/numerics/linear-interp/routine.json create mode 100644 modules/numerics/linear-interp/routine.pseudo create mode 100644 modules/numerics/module.json create mode 100644 modules/numerics/trapezoid/ref/mojo/plain.mojo create mode 100644 modules/numerics/trapezoid/ref/python/plain.py create mode 100644 modules/numerics/trapezoid/routine.json create mode 100644 modules/numerics/trapezoid/routine.pseudo create mode 100644 modules/seq/gc-content/golden/dafny.json create mode 100644 modules/seq/gc-content/proof/gc_content.dfy create mode 100644 modules/seq/gc-content/ref/mojo/plain.mojo create mode 100644 modules/seq/gc-content/ref/python/plain.py create mode 100644 modules/seq/gc-content/routine.json create mode 100644 modules/seq/gc-content/routine.pseudo create mode 100644 modules/seq/hamming-distance/golden/dafny.json create mode 100644 modules/seq/hamming-distance/proof/hamming_distance.dfy create mode 100644 modules/seq/hamming-distance/ref/mojo/plain.mojo create mode 100644 modules/seq/hamming-distance/ref/python/plain.py create mode 100644 modules/seq/hamming-distance/routine.json create mode 100644 modules/seq/hamming-distance/routine.pseudo create mode 100644 modules/seq/module.json create mode 100644 modules/special/erf-approx/ref/mojo/plain.mojo create mode 100644 modules/special/erf-approx/ref/python/libm.py create mode 100644 modules/special/erf-approx/ref/python/plain.py create mode 100644 modules/special/erf-approx/routine.json create mode 100644 modules/special/erf-approx/routine.pseudo create mode 100644 modules/special/module.json create mode 100644 modules/stats/mean/golden/dafny.json create mode 100644 modules/stats/mean/proof/mean.dfy create mode 100644 modules/stats/mean/ref/c/plain.c create mode 100644 modules/stats/mean/ref/mojo/plain.mojo create mode 100644 modules/stats/mean/ref/mojo/unrolled.mojo create mode 100644 modules/stats/mean/ref/python/fsum.py create mode 100644 modules/stats/mean/ref/python/plain.py create mode 100644 modules/stats/mean/routine.json create mode 100644 modules/stats/mean/routine.pseudo create mode 100644 modules/stats/module.json create mode 100644 modules/stats/variance/ref/mojo/plain.mojo create mode 100644 modules/stats/variance/ref/python/plain.py create mode 100644 modules/stats/variance/ref/python/statistics.py create mode 100644 modules/stats/variance/routine.json create mode 100644 modules/stats/variance/routine.pseudo create mode 100644 status.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..64777da --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +__pycache__/ +.DS_Store diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 0000000..0b94548 --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,30 @@ +# AGENTS.md + +Rules for adding or changing a routine of pplus-library. The checks are pplus-core's tasks: run them from a core checkout, where this repository is `library/`. + +## The gate + +```bash +pixi run library-check # in pplus-core: no check may fail +pixi run library-emit # then core's registry, committed in core +``` + +**A change is finished when `library-check` reports no failing check and `STATUS.md` shows what you expect.** A check may be `open` (the prover could not finish an obligation), `skipped` (a tool is missing, or core's Mojo interpreter declines a construct) or `unverified` (no stored compiler answer for this exact text yet); none of those is a failure, and each is listed in your summary. + +## Hard rules + +1. **One routine per file.** `routine.pseudo` holds USE lines and one FUNCTION or PROCEDURE, named as `routine.json` names it, and nothing else. A helper is a routine of its own, brought in with USE. That way every function in the library is checked on its own and appears once in generated Mojo. +2. **The pseudo is the specification.** The cases' results come from running the pseudo; every reference implementation is held to them. Never change a case's expected value by hand: `status.json` records it from the run. +3. **Do not edit generated files.** `status.json`, `STATUS.md` and everything under `golden/` are written by tools. A Mojo answer comes only from `pixi run library-verify-mojo`; a Dafny verdict only from `library-check` with the pinned Dafny. +4. **Contracts the prover can hold.** REQUIRE everything the routine needs. Write an ENSURE when the prover proves it (whole numbers, comparisons, lengths); a claim about the value of FLOAT arithmetic belongs in the Dafny proof over exact numbers, because rounding is opaque to the prover. An obligation left open is explained in the routine's comment (see `special.ErfApprox`). +5. **Every tolerance is explained.** A reference held to the pseudo within `rel` or `abs` says in its note why the answers differ (a different order of additions, a different method, an approximation's stated bound). +6. **Cite the method.** `sources` names where it comes from: a paper with its DOI, a textbook and section, a manual page. +7. **No new dependencies.** Python references use the standard library; C references use the C standard library and `-lm`; Mojo references use the standard library of the pinned Mojo. +8. **US English**, plain sentences, in comments, notes and summaries. + +## Adding a routine + +1. Make `modules///` with `routine.json` (name, summary, tags, sources, cases) and `routine.pseudo`. +2. Add references under `ref//.`: the function only, named in snake case after the routine (`LennardJones` is `lennard_jones`). A list parameter is a list in Python and Mojo, and an array and its length in C. Add each to `routine.json` with its note and any tolerance. +3. Add a Dafny proof under `proof/` when there is something worth proving over exact numbers, and name it in `routine.json`. +4. Run `pixi run library-check` and `pixi run library-emit` in core; commit this repository, then core with the new pointer and registry. diff --git a/README.md b/README.md new file mode 100644 index 0000000..75f0e4e --- /dev/null +++ b/README.md @@ -0,0 +1,54 @@ +# pplus-library + +Verified routines for Programming+, written once in pseudo and checked at every level: the cases run, the prover proves the contracts, the fuzzer finds no failure, the generated Mojo prints the same, every reference implementation (Python, C, Mojo, at more than one level of care or optimization) agrees with the pseudo on the cases, and, where it says something, a Dafny proof verifies the method over exact numbers. + +A program uses a routine by name, and a run steps over it unless asked to step in: + +``` +USE md +DECLARE R : FLOAT <- 1.5 +OUTPUT LennardJones(R, 0.238, 3.4), " ", MinimumImage(2.3, 1.0) +``` + +`USE md` brings in every routine of the module; `USE md.MinimumImage` brings in one. The prover uses each routine's REQUIRE and ENSURE lines at the call, so a program built from library routines is checked against what they promise, without proving them again. + +This repository is content. Its checks are pplus-core's tasks, and pplus-core pins it as the submodule `library/`. pplus-web gets it through core. + +## What is here + +``` +modules//module.json the module's name, title and summary +modules///routine.pseudo the routine: one FUNCTION or PROCEDURE, its contracts, USE lines for what it calls +modules///routine.json name, summary, tags, sources, cases, references, proof +modules///ref//. reference implementations +modules///proof/.dfy a Dafny proof, where one says something +modules///golden/ stored answers of real tools (written by tools only) +status.json, STATUS.md what every check found (written by pixi run library-check) +``` + +| Module | What it holds | +|---|---| +| `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, within 1.5e-7) | + +[STATUS.md](STATUS.md) is the current state of every check. + +## Checking it + +From a pplus-core checkout (the library is its submodule `library/`): + +```bash +pixi run library-check # every check of every routine; writes status.json, STATUS.md and the Dafny goldens +pixi run library-emit # regenerate core's registry (src/library/generated.ts) from this checkout +pixi run library-verify-mojo # ask the pinned Mojo compiler about every Mojo reference (network) +``` + +## Levels + +A reference's level says how it differs from the plainest translation of the pseudo: `plain` follows the pseudo step for step; other levels change the order of operations (`unrolled`, `fsum`), the method (`statistics`, `libm`, `two-rows`), or the arithmetic (`powers` against `multiply`). Each level is held to the pseudo's results exactly, or within a tolerance its `routine.json` states, and its note says why the tolerance is needed. C references run at `-O0` and `-O2`, so a level of optimization that changes an answer shows up. + +Read [AGENTS.md](AGENTS.md) before adding or changing a routine. diff --git a/STATUS.md b/STATUS.md new file mode 100644 index 0000000..6eb9556 --- /dev/null +++ b/STATUS.md @@ -0,0 +1,254 @@ +# Library status + + + +Each routine, at each level. *pass*: checked and agrees; *open*: the prover could not finish every obligation (nothing is wrong, nothing is proved); *skipped*: the tool is not available here, or it declines the construct; *unverified*: no stored answer for this exact text yet. + +## Sequence alignment (`align`) + +| Routine | Cases | Prover | Fuzz | Generated Mojo | References | Proof | +| --- | --- | --- | --- | --- | --- | --- | +| `align.AffineGapPenalty` | pass | pass | pass | pass | python/plain: cpython pass
mojo/plain: interpreter pass, godbolt unverified | dafny pass | +| `align.LinearGapPenalty` | pass | pass | pass | pass | python/plain: cpython pass
mojo/plain: interpreter pass, godbolt unverified | dafny pass | +| `align.MatchScore` | pass | pass | pass | pass | python/plain: cpython pass
mojo/plain: interpreter pass, godbolt unverified | dafny pass | +| `align.NeedlemanWunschScore` | pass | pass | pass | pass | python/plain: cpython pass
python/two-rows: cpython pass
mojo/plain: interpreter pass, godbolt unverified | · | + +## Molecular simulation (`md`) + +| Routine | Cases | Prover | Fuzz | Generated Mojo | References | Proof | +| --- | --- | --- | --- | --- | --- | --- | +| `md.LennardJones` | pass | pass | pass | pass | python/multiply: cpython pass
python/powers: cpython pass
c/multiply: cc -O0 pass, cc -O2 pass
mojo/multiply: interpreter pass, godbolt unverified | dafny pass | +| `md.LennardJonesForce` | pass | pass | pass | pass | python/multiply: cpython pass
mojo/multiply: interpreter pass, godbolt unverified | · | +| `md.MinimumImage` | pass | pass | pass | pass | python/plain: cpython pass
c/plain: cc -O0 pass, cc -O2 pass
mojo/plain: interpreter pass, godbolt unverified | dafny pass | +| `md.PairDistance` | pass | pass | pass | skipped | python/plain: cpython pass
mojo/plain: interpreter pass, godbolt unverified | · | + +## Numerics (`numerics`) + +| Routine | Cases | Prover | Fuzz | Generated Mojo | References | Proof | +| --- | --- | --- | --- | --- | --- | --- | +| `numerics.KahanSum` | pass | pass | pass | pass | python/plain: cpython pass
c/plain: cc -O0 pass, cc -O2 pass
mojo/plain: interpreter pass, godbolt unverified | dafny pass | +| `numerics.LinearInterp` | pass | pass | pass | pass | python/plain: cpython pass
mojo/plain: interpreter pass, godbolt unverified | · | +| `numerics.Trapezoid` | pass | pass | pass | pass | python/plain: cpython pass
mojo/plain: interpreter pass, godbolt unverified | · | + +## Sequences (`seq`) + +| Routine | Cases | Prover | Fuzz | Generated Mojo | References | Proof | +| --- | --- | --- | --- | --- | --- | --- | +| `seq.GCContent` | pass | pass | pass | pass | python/plain: cpython pass
mojo/plain: interpreter pass, godbolt unverified | dafny pass | +| `seq.HammingDistance` | pass | pass | pass | pass | python/plain: cpython pass
mojo/plain: interpreter pass, godbolt unverified | dafny pass | + +## Special functions (`special`) + +| Routine | Cases | Prover | Fuzz | Generated Mojo | References | Proof | +| --- | --- | --- | --- | --- | --- | --- | +| `special.ErfApprox` | pass | open | pass | skipped | python/plain: cpython pass
python/libm: cpython pass
mojo/plain: interpreter skipped, godbolt unverified | · | + +## Statistics (`stats`) + +| Routine | Cases | Prover | Fuzz | Generated Mojo | References | Proof | +| --- | --- | --- | --- | --- | --- | --- | +| `stats.Mean` | pass | pass | pass | pass | python/plain: cpython pass
python/fsum: cpython pass
c/plain: cc -O0 pass, cc -O2 pass
mojo/plain: interpreter pass, godbolt unverified
mojo/unrolled: interpreter pass, godbolt unverified | dafny pass | +| `stats.Variance` | pass | pass | pass | pass | python/plain: cpython pass
python/statistics: cpython pass
mojo/plain: interpreter pass, godbolt unverified | · | + +## Details + +### align.AffineGapPenalty + +- structure: pass. defines FUNCTION AffineGapPenalty +- pseudo: pass. 3 case(s) run +- prover: pass. all 1 obligation(s) proved +- fuzz: pass. no failure in 201 generated input(s) +- mojo: pass. the generated Mojo prints what the pseudo prints on every case, in core's interpreter +- python/plain, cpython: pass. agrees with the pseudo on 3 case(s) (exactly) +- mojo/plain, interpreter: pass. agrees with the pseudo on 3 case(s) in core's interpreter (exactly) +- mojo/plain, godbolt: unverified. no stored answer from mojo_1_1_0 for this program: pixi run library-verify-mojo +- proof: pass. Dafny 4.11.0: 3 verified, 0 errors + +### align.LinearGapPenalty + +- structure: pass. defines FUNCTION LinearGapPenalty +- pseudo: pass. 3 case(s) run +- prover: pass. all 1 obligation(s) proved +- fuzz: pass. no failure in 123 generated input(s) +- mojo: pass. the generated Mojo prints what the pseudo prints on every case, in core's interpreter +- python/plain, cpython: pass. agrees with the pseudo on 3 case(s) (exactly) +- mojo/plain, interpreter: pass. agrees with the pseudo on 3 case(s) in core's interpreter (exactly) +- mojo/plain, godbolt: unverified. no stored answer from mojo_1_1_0 for this program: pixi run library-verify-mojo +- proof: pass. Dafny 4.11.0: 2 verified, 0 errors + +### align.MatchScore + +- structure: pass. defines FUNCTION MatchScore +- pseudo: pass. 3 case(s) run +- prover: pass. all 2 obligation(s) proved +- fuzz: pass. no failure in 285 generated input(s) +- mojo: pass. the generated Mojo prints what the pseudo prints on every case, in core's interpreter +- python/plain, cpython: pass. agrees with the pseudo on 3 case(s) (exactly) +- mojo/plain, interpreter: pass. agrees with the pseudo on 3 case(s) in core's interpreter (exactly) +- mojo/plain, godbolt: unverified. no stored answer from mojo_1_1_0 for this program: pixi run library-verify-mojo +- proof: pass. Dafny 4.11.0: 1 verified, 0 errors + +### align.NeedlemanWunschScore + +- structure: pass. defines FUNCTION NeedlemanWunschScore, using align.MatchScore, align.LinearGapPenalty +- pseudo: pass. 4 case(s) run +- prover: pass. all 11 obligation(s) proved +- fuzz: pass. no failure in 300 generated input(s) +- mojo: pass. the generated Mojo prints what the pseudo prints on every case, in core's interpreter +- python/plain, cpython: pass. agrees with the pseudo on 4 case(s) (exactly) +- python/two-rows, cpython: pass. agrees with the pseudo on 4 case(s) (exactly) +- mojo/plain, interpreter: pass. agrees with the pseudo on 4 case(s) in core's interpreter (exactly) +- mojo/plain, godbolt: unverified. no stored answer from mojo_1_1_0 for this program: pixi run library-verify-mojo + +### md.LennardJones + +- structure: pass. defines FUNCTION LennardJones +- pseudo: pass. 4 case(s) run +- prover: pass. all 2 obligation(s) proved +- fuzz: pass. no failure in 252 generated input(s) +- mojo: pass. the generated Mojo prints what the pseudo prints on every case, in core's interpreter +- python/multiply, cpython: pass. agrees with the pseudo on 4 case(s) (exactly) +- python/powers, cpython: pass. agrees with the pseudo on 4 case(s) (within rel 1e-12, abs 0) +- c/multiply, cc -O0: pass. agrees with the pseudo on 4 case(s) (exactly) +- c/multiply, cc -O2: pass. agrees with the pseudo on 4 case(s) (exactly) +- mojo/multiply, interpreter: pass. agrees with the pseudo on 4 case(s) in core's interpreter (exactly) +- mojo/multiply, godbolt: unverified. no stored answer from mojo_1_1_0 for this program: pixi run library-verify-mojo +- proof: pass. Dafny 4.11.0: 14 verified, 0 errors + +### md.LennardJonesForce + +- structure: pass. defines FUNCTION LennardJonesForce +- pseudo: pass. 3 case(s) run +- prover: pass. all 3 obligation(s) proved +- fuzz: pass. no failure in 252 generated input(s) +- mojo: pass. the generated Mojo prints what the pseudo prints on every case, in core's interpreter +- python/multiply, cpython: pass. agrees with the pseudo on 3 case(s) (exactly) +- mojo/multiply, interpreter: pass. agrees with the pseudo on 3 case(s) in core's interpreter (exactly) +- mojo/multiply, godbolt: unverified. no stored answer from mojo_1_1_0 for this program: pixi run library-verify-mojo + +### md.MinimumImage + +- structure: pass. defines FUNCTION MinimumImage +- pseudo: pass. 5 case(s) run +- prover: pass. all 1 obligation(s) proved +- fuzz: pass. no failure in 241 generated input(s) +- mojo: pass. the generated Mojo prints what the pseudo prints on every case, in core's interpreter +- python/plain, cpython: pass. agrees with the pseudo on 5 case(s) (exactly) +- c/plain, cc -O0: pass. agrees with the pseudo on 5 case(s) (exactly) +- c/plain, cc -O2: pass. agrees with the pseudo on 5 case(s) (exactly) +- mojo/plain, interpreter: pass. agrees with the pseudo on 5 case(s) in core's interpreter (exactly) +- mojo/plain, godbolt: unverified. no stored answer from mojo_1_1_0 for this program: pixi run library-verify-mojo +- proof: pass. Dafny 4.11.0: 4 verified, 0 errors + +### md.PairDistance + +- structure: pass. defines FUNCTION PairDistance, using md.MinimumImage +- pseudo: pass. 3 case(s) run +- prover: pass. all 9 obligation(s) proved +- fuzz: pass. no failure in 4 generated input(s) +- mojo: skipped. core's Mojo interpreter declines hypot(...) as written here; the compiler's answer is needed +- python/plain, cpython: pass. agrees with the pseudo on 3 case(s) (within rel 1e-15, abs 0) +- mojo/plain, interpreter: pass. agrees with the pseudo on 3 case(s) in core's interpreter (within rel 1e-15, abs 0) +- mojo/plain, godbolt: unverified. no stored answer from mojo_1_1_0 for this program: pixi run library-verify-mojo + +### numerics.KahanSum + +- structure: pass. defines FUNCTION KahanSum +- pseudo: pass. 4 case(s) run +- prover: pass. all 1 obligation(s) proved +- fuzz: pass. no failure in 263 generated input(s) +- mojo: pass. the generated Mojo prints what the pseudo prints on every case, in core's interpreter +- python/plain, cpython: pass. agrees with the pseudo on 4 case(s) (exactly) +- c/plain, cc -O0: pass. agrees with the pseudo on 4 case(s) (exactly) +- c/plain, cc -O2: pass. agrees with the pseudo on 4 case(s) (exactly) +- mojo/plain, interpreter: pass. agrees with the pseudo on 4 case(s) in core's interpreter (exactly) +- mojo/plain, godbolt: unverified. no stored answer from mojo_1_1_0 for this program: pixi run library-verify-mojo +- proof: pass. Dafny 4.11.0: 2 verified, 0 errors + +### numerics.LinearInterp + +- structure: pass. defines FUNCTION LinearInterp +- pseudo: pass. 3 case(s) run +- prover: pass. all 1 obligation(s) proved +- fuzz: pass. no failure in 300 generated input(s) +- mojo: pass. the generated Mojo prints what the pseudo prints on every case, in core's interpreter +- python/plain, cpython: pass. agrees with the pseudo on 3 case(s) (exactly) +- mojo/plain, interpreter: pass. agrees with the pseudo on 3 case(s) in core's interpreter (exactly) +- mojo/plain, godbolt: unverified. no stored answer from mojo_1_1_0 for this program: pixi run library-verify-mojo + +### numerics.Trapezoid + +- structure: pass. defines FUNCTION Trapezoid +- pseudo: pass. 3 case(s) run +- prover: pass. all 4 obligation(s) proved +- fuzz: pass. no failure in 227 generated input(s) +- mojo: pass. the generated Mojo prints what the pseudo prints on every case, in core's interpreter +- python/plain, cpython: pass. agrees with the pseudo on 3 case(s) (exactly) +- mojo/plain, interpreter: pass. agrees with the pseudo on 3 case(s) in core's interpreter (exactly) +- mojo/plain, godbolt: unverified. no stored answer from mojo_1_1_0 for this program: pixi run library-verify-mojo + +### seq.GCContent + +- structure: pass. defines FUNCTION GCContent +- pseudo: pass. 3 case(s) run +- prover: pass. all 3 obligation(s) proved +- fuzz: pass. no failure in 469 generated input(s) +- mojo: pass. the generated Mojo prints what the pseudo prints on every case, in core's interpreter +- python/plain, cpython: pass. agrees with the pseudo on 3 case(s) (exactly) +- mojo/plain, interpreter: pass. agrees with the pseudo on 3 case(s) in core's interpreter (exactly) +- mojo/plain, godbolt: unverified. no stored answer from mojo_1_1_0 for this program: pixi run library-verify-mojo +- proof: pass. Dafny 4.11.0: 5 verified, 0 errors + +### seq.HammingDistance + +- structure: pass. defines FUNCTION HammingDistance +- pseudo: pass. 3 case(s) run +- prover: pass. all 5 obligation(s) proved +- fuzz: pass. no failure in 291 generated input(s) +- mojo: pass. the generated Mojo prints what the pseudo prints on every case, in core's interpreter +- python/plain, cpython: pass. agrees with the pseudo on 3 case(s) (exactly) +- mojo/plain, interpreter: pass. agrees with the pseudo on 3 case(s) in core's interpreter (exactly) +- mojo/plain, godbolt: unverified. no stored answer from mojo_1_1_0 for this program: pixi run library-verify-mojo +- proof: pass. Dafny 4.11.0: 5 verified, 0 errors + +### special.ErfApprox + +- structure: pass. defines FUNCTION ErfApprox +- pseudo: pass. 5 case(s) run +- prover: open. 0 of 1 obligation(s) proved; open: divide@12 (1.0 + 0.3275911 * A is not 0) +- fuzz: pass. no failure in 269 generated input(s) +- mojo: skipped. core's Mojo interpreter declines exp(...) as written here; the compiler's answer is needed +- python/plain, cpython: pass. agrees with the pseudo on 5 case(s) (exactly) +- python/libm, cpython: pass. agrees with the pseudo on 5 case(s) (within rel 0, abs 1.5e-7) +- mojo/plain, interpreter: skipped. core's Mojo interpreter declines exp(...) as written here +- mojo/plain, godbolt: unverified. no stored answer from mojo_1_1_0 for this program: pixi run library-verify-mojo + +### stats.Mean + +- structure: pass. defines FUNCTION Mean +- pseudo: pass. 5 case(s) run +- prover: pass. all 2 obligation(s) proved +- fuzz: pass. no failure in 262 generated input(s) +- mojo: pass. the generated Mojo prints what the pseudo prints on every case, in core's interpreter +- python/plain, cpython: pass. agrees with the pseudo on 5 case(s) (exactly) +- python/fsum, cpython: pass. agrees with the pseudo on 5 case(s) (within rel 1e-15, abs 0) +- c/plain, cc -O0: pass. agrees with the pseudo on 5 case(s) (exactly) +- c/plain, cc -O2: pass. agrees with the pseudo on 5 case(s) (exactly) +- mojo/plain, interpreter: pass. agrees with the pseudo on 5 case(s) in core's interpreter (exactly) +- mojo/plain, godbolt: unverified. no stored answer from mojo_1_1_0 for this program: pixi run library-verify-mojo +- mojo/unrolled, interpreter: pass. agrees with the pseudo on 5 case(s) in core's interpreter (within rel 1e-15, abs 0) +- mojo/unrolled, godbolt: unverified. no stored answer from mojo_1_1_0 for this program: pixi run library-verify-mojo +- proof: pass. Dafny 4.11.0: 9 verified, 0 errors + +### stats.Variance + +- structure: pass. defines FUNCTION Variance, using stats.Mean +- pseudo: pass. 3 case(s) run +- prover: pass. all 4 obligation(s) proved +- fuzz: pass. no failure in 221 generated input(s) +- mojo: pass. the generated Mojo prints what the pseudo prints on every case, in core's interpreter +- python/plain, cpython: pass. agrees with the pseudo on 3 case(s) (exactly) +- python/statistics, cpython: pass. agrees with the pseudo on 3 case(s) (within rel 1e-12, abs 0) +- mojo/plain, interpreter: pass. agrees with the pseudo on 3 case(s) in core's interpreter (exactly) +- mojo/plain, godbolt: unverified. no stored answer from mojo_1_1_0 for this program: pixi run library-verify-mojo + diff --git a/modules/align/affine-gap/golden/dafny.json b/modules/align/affine-gap/golden/dafny.json new file mode 100644 index 0000000..16b20e4 --- /dev/null +++ b/modules/align/affine-gap/golden/dafny.json @@ -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" +} diff --git a/modules/align/affine-gap/proof/affine_gap.dfy b/modules/align/affine-gap/proof/affine_gap.dfy new file mode 100644 index 0000000..2438a9e --- /dev/null +++ b/modules/align/affine-gap/proof/affine_gap.dfy @@ -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 +{ +} diff --git a/modules/align/affine-gap/ref/mojo/plain.mojo b/modules/align/affine-gap/ref/mojo/plain.mojo new file mode 100644 index 0000000..7637a0a --- /dev/null +++ b/modules/align/affine-gap/ref/mojo/plain.mojo @@ -0,0 +1,2 @@ +def affine_gap_penalty(length: Int, open_: Int, extend: Int) -> Int: + return open_ + (length - 1) * extend diff --git a/modules/align/affine-gap/ref/python/plain.py b/modules/align/affine-gap/ref/python/plain.py new file mode 100644 index 0000000..34d475a --- /dev/null +++ b/modules/align/affine-gap/ref/python/plain.py @@ -0,0 +1,2 @@ +def affine_gap_penalty(length, open_, extend): + return open_ + (length - 1) * extend diff --git a/modules/align/affine-gap/routine.json b/modules/align/affine-gap/routine.json new file mode 100644 index 0000000..02df149 --- /dev/null +++ b/modules/align/affine-gap/routine.json @@ -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." + } +} diff --git a/modules/align/affine-gap/routine.pseudo b/modules/align/affine-gap/routine.pseudo new file mode 100644 index 0000000..64d1738 --- /dev/null +++ b/modules/align/affine-gap/routine.pseudo @@ -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 diff --git a/modules/align/linear-gap/golden/dafny.json b/modules/align/linear-gap/golden/dafny.json new file mode 100644 index 0000000..09ec829 --- /dev/null +++ b/modules/align/linear-gap/golden/dafny.json @@ -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" +} diff --git a/modules/align/linear-gap/proof/linear_gap.dfy b/modules/align/linear-gap/proof/linear_gap.dfy new file mode 100644 index 0000000..c9cb68e --- /dev/null +++ b/modules/align/linear-gap/proof/linear_gap.dfy @@ -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; + } +} diff --git a/modules/align/linear-gap/ref/mojo/plain.mojo b/modules/align/linear-gap/ref/mojo/plain.mojo new file mode 100644 index 0000000..7505c0f --- /dev/null +++ b/modules/align/linear-gap/ref/mojo/plain.mojo @@ -0,0 +1,2 @@ +def linear_gap_penalty(length: Int, gap: Int) -> Int: + return length * gap diff --git a/modules/align/linear-gap/ref/python/plain.py b/modules/align/linear-gap/ref/python/plain.py new file mode 100644 index 0000000..0b38a85 --- /dev/null +++ b/modules/align/linear-gap/ref/python/plain.py @@ -0,0 +1,2 @@ +def linear_gap_penalty(length, gap): + return length * gap diff --git a/modules/align/linear-gap/routine.json b/modules/align/linear-gap/routine.json new file mode 100644 index 0000000..36bc0a2 --- /dev/null +++ b/modules/align/linear-gap/routine.json @@ -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." + } +} diff --git a/modules/align/linear-gap/routine.pseudo b/modules/align/linear-gap/routine.pseudo new file mode 100644 index 0000000..4859efa --- /dev/null +++ b/modules/align/linear-gap/routine.pseudo @@ -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 diff --git a/modules/align/match-score/golden/dafny.json b/modules/align/match-score/golden/dafny.json new file mode 100644 index 0000000..1a759e9 --- /dev/null +++ b/modules/align/match-score/golden/dafny.json @@ -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" +} diff --git a/modules/align/match-score/proof/match_score.dfy b/modules/align/match-score/proof/match_score.dfy new file mode 100644 index 0000000..2c9e7a1 --- /dev/null +++ b/modules/align/match-score/proof/match_score.dfy @@ -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; } +} diff --git a/modules/align/match-score/ref/mojo/plain.mojo b/modules/align/match-score/ref/mojo/plain.mojo new file mode 100644 index 0000000..4609273 --- /dev/null +++ b/modules/align/match-score/ref/mojo/plain.mojo @@ -0,0 +1,4 @@ +def match_score(a: String, b: String, match_: Int, mismatch: Int) -> Int: + if a == b: + return match_ + return mismatch diff --git a/modules/align/match-score/ref/python/plain.py b/modules/align/match-score/ref/python/plain.py new file mode 100644 index 0000000..e24290d --- /dev/null +++ b/modules/align/match-score/ref/python/plain.py @@ -0,0 +1,2 @@ +def match_score(a, b, match, mismatch): + return match if a == b else mismatch diff --git a/modules/align/match-score/routine.json b/modules/align/match-score/routine.json new file mode 100644 index 0000000..d36cd92 --- /dev/null +++ b/modules/align/match-score/routine.json @@ -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." + } +} diff --git a/modules/align/match-score/routine.pseudo b/modules/align/match-score/routine.pseudo new file mode 100644 index 0000000..820c785 --- /dev/null +++ b/modules/align/match-score/routine.pseudo @@ -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 diff --git a/modules/align/module.json b/modules/align/module.json new file mode 100644 index 0000000..e30acb6 --- /dev/null +++ b/modules/align/module.json @@ -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." +} diff --git a/modules/align/needleman-wunsch-score/ref/mojo/plain.mojo b/modules/align/needleman-wunsch-score/ref/mojo/plain.mojo new file mode 100644 index 0000000..db0d1e4 --- /dev/null +++ b/modules/align/needleman-wunsch-score/ref/mojo/plain.mojo @@ -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] diff --git a/modules/align/needleman-wunsch-score/ref/python/plain.py b/modules/align/needleman-wunsch-score/ref/python/plain.py new file mode 100644 index 0000000..0897aa3 --- /dev/null +++ b/modules/align/needleman-wunsch-score/ref/python/plain.py @@ -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] diff --git a/modules/align/needleman-wunsch-score/ref/python/two-rows.py b/modules/align/needleman-wunsch-score/ref/python/two-rows.py new file mode 100644 index 0000000..00dabbb --- /dev/null +++ b/modules/align/needleman-wunsch-score/ref/python/two-rows.py @@ -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] diff --git a/modules/align/needleman-wunsch-score/routine.json b/modules/align/needleman-wunsch-score/routine.json new file mode 100644 index 0000000..1c0401c --- /dev/null +++ b/modules/align/needleman-wunsch-score/routine.json @@ -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." + } + ] +} diff --git a/modules/align/needleman-wunsch-score/routine.pseudo b/modules/align/needleman-wunsch-score/routine.pseudo new file mode 100644 index 0000000..e02dcd4 --- /dev/null +++ b/modules/align/needleman-wunsch-score/routine.pseudo @@ -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 diff --git a/modules/md/lennard-jones-force/ref/mojo/multiply.mojo b/modules/md/lennard-jones-force/ref/mojo/multiply.mojo new file mode 100644 index 0000000..48c9152 --- /dev/null +++ b/modules/md/lennard-jones-force/ref/mojo/multiply.mojo @@ -0,0 +1,4 @@ +def lennard_jones_force(r: Float64, epsilon: Float64, sigma: Float64) -> Float64: + var s2 = (sigma / r) * (sigma / r) + var s6 = s2 * s2 * s2 + return 24.0 * epsilon * (2.0 * s6 * s6 - s6) / r diff --git a/modules/md/lennard-jones-force/ref/python/multiply.py b/modules/md/lennard-jones-force/ref/python/multiply.py new file mode 100644 index 0000000..66bba90 --- /dev/null +++ b/modules/md/lennard-jones-force/ref/python/multiply.py @@ -0,0 +1,4 @@ +def lennard_jones_force(r, epsilon, sigma): + s2 = (sigma / r) * (sigma / r) + s6 = s2 * s2 * s2 + return 24.0 * epsilon * (2.0 * s6 * s6 - s6) / r diff --git a/modules/md/lennard-jones-force/routine.json b/modules/md/lennard-jones-force/routine.json new file mode 100644 index 0000000..ec3b051 --- /dev/null +++ b/modules/md/lennard-jones-force/routine.json @@ -0,0 +1,38 @@ +{ + "name": "LennardJonesForce", + "summary": "The force between two particles at distance R under the Lennard-Jones potential: positive pushes them apart.", + "tags": [ + "molecular dynamics", + "force" + ], + "sources": [ + { + "title": "Allen and Tildesley, Computer Simulation of Liquids, section 1.4" + } + ], + "cases": [ + { + "call": "LennardJonesForce(1.0, 1.0, 1.0)" + }, + { + "call": "LennardJonesForce(1.122462048309373, 1.0, 1.0)" + }, + { + "call": "LennardJonesForce(2.5, 0.238, 3.4)" + } + ], + "references": [ + { + "lang": "python", + "level": "multiply", + "file": "ref/python/multiply.py", + "note": "The same multiplications." + }, + { + "lang": "mojo", + "level": "multiply", + "file": "ref/mojo/multiply.mojo", + "note": "The same multiplications in Mojo." + } + ] +} diff --git a/modules/md/lennard-jones-force/routine.pseudo b/modules/md/lennard-jones-force/routine.pseudo new file mode 100644 index 0000000..f42355b --- /dev/null +++ b/modules/md/lennard-jones-force/routine.pseudo @@ -0,0 +1,10 @@ +// LennardJonesForce: the force between two particles at distance R under +// the Lennard-Jones potential, F(R) = -dV/dR +// = 24 Epsilon / R (2 (Sigma / R)^12 - (Sigma / R)^6). +// Positive pushes the particles apart; it is 0 at the bottom of the well. +FUNCTION LennardJonesForce(R : FLOAT, Epsilon : FLOAT, Sigma : FLOAT) RETURNS FLOAT + REQUIRE R > 0.0 + DECLARE S2 : FLOAT <- (Sigma / R) * (Sigma / R) + DECLARE S6 : FLOAT <- S2 * S2 * S2 + RETURN 24.0 * Epsilon * (2.0 * S6 * S6 - S6) / R +ENDFUNCTION diff --git a/modules/md/lennard-jones/golden/dafny.json b/modules/md/lennard-jones/golden/dafny.json new file mode 100644 index 0000000..5eb0cad --- /dev/null +++ b/modules/md/lennard-jones/golden/dafny.json @@ -0,0 +1,12 @@ +{ + "tool": "dafny", + "version": "4.11.0", + "flags": [ + "verify", + "--resource-limit", + "20000000" + ], + "source_sha256": "0dc08be0ccc13bef1bbad5bc4fa8efc54c39005c803c770a79fec941913ffb86", + "verdict": "verified", + "summary": "14 verified, 0 errors" +} diff --git a/modules/md/lennard-jones/proof/lennard_jones.dfy b/modules/md/lennard-jones/proof/lennard_jones.dfy new file mode 100644 index 0000000..d934e83 --- /dev/null +++ b/modules/md/lennard-jones/proof/lennard_jones.dfy @@ -0,0 +1,59 @@ +function Pow(x: real, n: nat): real +{ + if n == 0 then 1.0 else x * Pow(x, n - 1) +} + +function LJ(r: real, epsilon: real, sigma: real): real + requires r > 0.0 +{ + 4.0 * epsilon * (Pow(sigma / r, 12) - Pow(sigma / r, 6)) +} + +lemma PowAdd(x: real, a: nat, b: nat) + ensures Pow(x, a + b) == Pow(x, a) * Pow(x, b) +{ + if a > 0 { + PowAdd(x, a - 1, b); + assert Pow(x, a + b) == x * Pow(x, a - 1 + b); + } +} + +lemma Pow2(x: real) + ensures Pow(x, 2) == x * x +{ + assert Pow(x, 1) == x; +} + +lemma Pow6(s: real) + ensures Pow(s, 6) == (s * s) * (s * s) * (s * s) +{ + Pow2(s); + PowAdd(s, 2, 2); + PowAdd(s, 4, 2); +} + +lemma Pow12(s: real) + ensures Pow(s, 12) == Pow(s, 6) * Pow(s, 6) +{ + PowAdd(s, 6, 6); +} + +method LennardJones(r: real, epsilon: real, sigma: real) returns (v: real) + requires r > 0.0 + ensures v == LJ(r, epsilon, sigma) +{ + var s := sigma / r; + var s2 := s * s; + var s6 := s2 * s2 * s2; + Pow6(s); + Pow12(s); + v := 4.0 * epsilon * (s6 * s6 - s6); +} + +// The potential crosses zero at R = Sigma. +lemma ZeroAtSigma(epsilon: real, sigma: real) + requires sigma > 0.0 + ensures LJ(sigma, epsilon, sigma) == 0.0 +{ + assert sigma / sigma == 1.0; +} diff --git a/modules/md/lennard-jones/ref/c/multiply.c b/modules/md/lennard-jones/ref/c/multiply.c new file mode 100644 index 0000000..2f78d39 --- /dev/null +++ b/modules/md/lennard-jones/ref/c/multiply.c @@ -0,0 +1,5 @@ +double lennard_jones(double r, double epsilon, double sigma) { + double s2 = (sigma / r) * (sigma / r); + double s6 = s2 * s2 * s2; + return 4.0 * epsilon * (s6 * s6 - s6); +} diff --git a/modules/md/lennard-jones/ref/mojo/multiply.mojo b/modules/md/lennard-jones/ref/mojo/multiply.mojo new file mode 100644 index 0000000..b8907ed --- /dev/null +++ b/modules/md/lennard-jones/ref/mojo/multiply.mojo @@ -0,0 +1,4 @@ +def lennard_jones(r: Float64, epsilon: Float64, sigma: Float64) -> Float64: + var s2 = (sigma / r) * (sigma / r) + var s6 = s2 * s2 * s2 + return 4.0 * epsilon * (s6 * s6 - s6) diff --git a/modules/md/lennard-jones/ref/python/multiply.py b/modules/md/lennard-jones/ref/python/multiply.py new file mode 100644 index 0000000..3a8bde5 --- /dev/null +++ b/modules/md/lennard-jones/ref/python/multiply.py @@ -0,0 +1,4 @@ +def lennard_jones(r, epsilon, sigma): + s2 = (sigma / r) * (sigma / r) + s6 = s2 * s2 * s2 + return 4.0 * epsilon * (s6 * s6 - s6) diff --git a/modules/md/lennard-jones/ref/python/powers.py b/modules/md/lennard-jones/ref/python/powers.py new file mode 100644 index 0000000..02f51fb --- /dev/null +++ b/modules/md/lennard-jones/ref/python/powers.py @@ -0,0 +1,2 @@ +def lennard_jones(r, epsilon, sigma): + return 4.0 * epsilon * ((sigma / r) ** 12 - (sigma / r) ** 6) diff --git a/modules/md/lennard-jones/routine.json b/modules/md/lennard-jones/routine.json new file mode 100644 index 0000000..ae77e7c --- /dev/null +++ b/modules/md/lennard-jones/routine.json @@ -0,0 +1,63 @@ +{ + "name": "LennardJones", + "summary": "The Lennard-Jones pair potential at distance R, with well depth Epsilon and zero-crossing distance Sigma.", + "tags": [ + "molecular dynamics", + "pair potential" + ], + "sources": [ + { + "title": "Jones, On the determination of molecular fields II, Proc. R. Soc. A 106 (1924)", + "url": "https://doi.org/10.1098/rspa.1924.0082" + }, + { + "title": "Allen and Tildesley, Computer Simulation of Liquids, section 1.4" + } + ], + "cases": [ + { + "call": "LennardJones(1.0, 1.0, 1.0)" + }, + { + "call": "LennardJones(1.122462048309373, 1.0, 1.0)" + }, + { + "call": "LennardJones(2.5, 0.238, 3.4)" + }, + { + "call": "LennardJones(0.9, 1.0, 1.0)" + } + ], + "references": [ + { + "lang": "python", + "level": "multiply", + "file": "ref/python/multiply.py", + "note": "The same multiplications." + }, + { + "lang": "python", + "level": "powers", + "file": "ref/python/powers.py", + "note": "The textbook formula with ** 12 and ** 6: the same function, rounded differently, held to agree to 12 digits.", + "rel": 1e-12 + }, + { + "lang": "c", + "level": "multiply", + "file": "ref/c/multiply.c", + "note": "The same multiplications in C." + }, + { + "lang": "mojo", + "level": "multiply", + "file": "ref/mojo/multiply.mojo", + "note": "The same multiplications in Mojo." + } + ], + "proof": { + "lang": "dafny", + "file": "proof/lennard_jones.dfy", + "note": "Over real numbers the multiplications compute the textbook formula exactly, and V(Sigma) = 0." + } +} diff --git a/modules/md/lennard-jones/routine.pseudo b/modules/md/lennard-jones/routine.pseudo new file mode 100644 index 0000000..6deb2b5 --- /dev/null +++ b/modules/md/lennard-jones/routine.pseudo @@ -0,0 +1,11 @@ +// LennardJones: the Lennard-Jones pair potential at distance R, +// V(R) = 4 Epsilon ((Sigma / R)^12 - (Sigma / R)^6). Epsilon is the depth of +// the well, reached at R = 2^(1/6) Sigma, and V is 0 at R = Sigma. The powers +// are built by multiplication from (Sigma / R)^2, as simulation codes do, +// which is faster than ^ and rounds a little differently. +FUNCTION LennardJones(R : FLOAT, Epsilon : FLOAT, Sigma : FLOAT) RETURNS FLOAT + REQUIRE R > 0.0 + DECLARE S2 : FLOAT <- (Sigma / R) * (Sigma / R) + DECLARE S6 : FLOAT <- S2 * S2 * S2 + RETURN 4.0 * Epsilon * (S6 * S6 - S6) +ENDFUNCTION diff --git a/modules/md/minimum-image/golden/dafny.json b/modules/md/minimum-image/golden/dafny.json new file mode 100644 index 0000000..3f8142e --- /dev/null +++ b/modules/md/minimum-image/golden/dafny.json @@ -0,0 +1,12 @@ +{ + "tool": "dafny", + "version": "4.11.0", + "flags": [ + "verify", + "--resource-limit", + "20000000" + ], + "source_sha256": "9881217a957b2d8700997063d9bda129337d7c266a287aeb76a0881a1b0b1b08", + "verdict": "verified", + "summary": "4 verified, 0 errors" +} diff --git a/modules/md/minimum-image/proof/minimum_image.dfy b/modules/md/minimum-image/proof/minimum_image.dfy new file mode 100644 index 0000000..f433d8c --- /dev/null +++ b/modules/md/minimum-image/proof/minimum_image.dfy @@ -0,0 +1,25 @@ +// MinimumImage, over real numbers: the image lies in [-box / 2, box / 2) and is a whole number of box lengths from dx. +lemma ScaledBounds(g: real, box: real) + requires box > 0.0 + requires -0.5 <= g < 0.5 + ensures -box / 2.0 <= g * box < box / 2.0 +{ + assert (g + 0.5) * box >= 0.0; + assert (0.5 - g) * box > 0.0; +} + +method MinimumImage(dx: real, box: real) returns (r: real, k: int) + requires box > 0.0 + ensures r == dx - box * (k as real) + ensures -box / 2.0 <= r < box / 2.0 +{ + var q := dx / box + 0.5; + k := q.Floor; + assert (k as real) <= q < (k as real) + 1.0; + var g := q - (k as real) - 0.5; + assert -0.5 <= g < 0.5; + assert dx == (q - 0.5) * box; + r := dx - box * (k as real); + assert r == g * box; + ScaledBounds(g, box); +} diff --git a/modules/md/minimum-image/ref/c/plain.c b/modules/md/minimum-image/ref/c/plain.c new file mode 100644 index 0000000..2717303 --- /dev/null +++ b/modules/md/minimum-image/ref/c/plain.c @@ -0,0 +1,5 @@ +#include + +double minimum_image(double dx, double box) { + return dx - box * floor(dx / box + 0.5); +} diff --git a/modules/md/minimum-image/ref/mojo/plain.mojo b/modules/md/minimum-image/ref/mojo/plain.mojo new file mode 100644 index 0000000..d982898 --- /dev/null +++ b/modules/md/minimum-image/ref/mojo/plain.mojo @@ -0,0 +1,5 @@ +from std.math import floor + + +def minimum_image(dx: Float64, box: Float64) -> Float64: + return dx - box * floor(dx / box + 0.5) diff --git a/modules/md/minimum-image/ref/python/plain.py b/modules/md/minimum-image/ref/python/plain.py new file mode 100644 index 0000000..226e84b --- /dev/null +++ b/modules/md/minimum-image/ref/python/plain.py @@ -0,0 +1,5 @@ +import math + + +def minimum_image(dx, box): + return dx - box * math.floor(dx / box + 0.5) diff --git a/modules/md/minimum-image/routine.json b/modules/md/minimum-image/routine.json new file mode 100644 index 0000000..2ccce0e --- /dev/null +++ b/modules/md/minimum-image/routine.json @@ -0,0 +1,55 @@ +{ + "name": "MinimumImage", + "summary": "A displacement brought to its nearest periodic image in a box of length Box: the minimum image convention.", + "tags": [ + "molecular dynamics", + "periodic boundary conditions" + ], + "sources": [ + { + "title": "Allen and Tildesley, Computer Simulation of Liquids, section 1.5.2" + } + ], + "cases": [ + { + "call": "MinimumImage(0.75, 1.0)" + }, + { + "call": "MinimumImage(-0.75, 1.0)" + }, + { + "call": "MinimumImage(2.3, 1.0)" + }, + { + "call": "MinimumImage(0.5, 1.0)" + }, + { + "call": "MinimumImage(-12.0, 10.0)" + } + ], + "references": [ + { + "lang": "python", + "level": "plain", + "file": "ref/python/plain.py", + "note": "The same formula with math.floor." + }, + { + "lang": "c", + "level": "plain", + "file": "ref/c/plain.c", + "note": "The same formula with floor from math.h." + }, + { + "lang": "mojo", + "level": "plain", + "file": "ref/mojo/plain.mojo", + "note": "The same formula in Mojo, with floor from std.math." + } + ], + "proof": { + "lang": "dafny", + "file": "proof/minimum_image.dfy", + "note": "Over real numbers the result lies in [-Box / 2, Box / 2) and differs from Dx by a whole number of box lengths." + } +} diff --git a/modules/md/minimum-image/routine.pseudo b/modules/md/minimum-image/routine.pseudo new file mode 100644 index 0000000..0e8966a --- /dev/null +++ b/modules/md/minimum-image/routine.pseudo @@ -0,0 +1,8 @@ +// MinimumImage: the displacement Dx brought to its nearest periodic image in +// a box of length Box, the minimum image convention: Dx minus the whole +// number of box lengths that brings it into [-Box / 2, Box / 2). FLOOR of +// Dx / Box + 0.5 rounds halves upward, so exactly half a box maps to -Box / 2. +FUNCTION MinimumImage(Dx : FLOAT, Box : FLOAT) RETURNS FLOAT + REQUIRE Box > 0.0 + RETURN Dx - Box * FLOOR(Dx / Box + 0.5) +ENDFUNCTION diff --git a/modules/md/module.json b/modules/md/module.json new file mode 100644 index 0000000..9c09d81 --- /dev/null +++ b/modules/md/module.json @@ -0,0 +1,5 @@ +{ + "name": "md", + "title": "Molecular simulation", + "summary": "Pair potentials, forces and periodic boxes, the inner loop of molecular dynamics." +} diff --git a/modules/md/pair-distance/ref/mojo/plain.mojo b/modules/md/pair-distance/ref/mojo/plain.mojo new file mode 100644 index 0000000..5434e0a --- /dev/null +++ b/modules/md/pair-distance/ref/mojo/plain.mojo @@ -0,0 +1,10 @@ +from std.math import floor, sqrt + + +def pair_distance(a: List[Float64], b: List[Float64], box: Float64) -> Float64: + var total: Float64 = 0.0 + for i in range(3): + var d = a[i] - b[i] + d = d - box * floor(d / box + 0.5) + total = total + d * d + return sqrt(total) diff --git a/modules/md/pair-distance/ref/python/plain.py b/modules/md/pair-distance/ref/python/plain.py new file mode 100644 index 0000000..fef26c9 --- /dev/null +++ b/modules/md/pair-distance/ref/python/plain.py @@ -0,0 +1,7 @@ +import math + + +def pair_distance(a, b, box): + d = [x - y for x, y in zip(a, b)] + d = [v - box * math.floor(v / box + 0.5) for v in d] + return math.hypot(*d) diff --git a/modules/md/pair-distance/routine.json b/modules/md/pair-distance/routine.json new file mode 100644 index 0000000..e222ede --- /dev/null +++ b/modules/md/pair-distance/routine.json @@ -0,0 +1,40 @@ +{ + "name": "PairDistance", + "summary": "The distance between two points in a cubic periodic box, each coordinate difference taken by the minimum image convention.", + "tags": [ + "molecular dynamics", + "periodic boundary conditions" + ], + "sources": [ + { + "title": "Allen and Tildesley, Computer Simulation of Liquids, section 1.5.2" + } + ], + "cases": [ + { + "call": "PairDistance([0.1, 0.1, 0.1], [0.9, 0.9, 0.9], 1.0)" + }, + { + "call": "PairDistance([0.0, 0.0, 0.0], [3.0, 4.0, 0.0], 20.0)" + }, + { + "call": "PairDistance([1.0, 2.0, 3.0], [1.0, 2.0, 3.0], 5.0)" + } + ], + "references": [ + { + "lang": "python", + "level": "plain", + "file": "ref/python/plain.py", + "note": "The same steps, with math.hypot of all three differences at once.", + "rel": 1e-15 + }, + { + "lang": "mojo", + "level": "plain", + "file": "ref/mojo/plain.mojo", + "note": "The same steps in Mojo, with the square root of the sum of squares.", + "rel": 1e-15 + } + ] +} diff --git a/modules/md/pair-distance/routine.pseudo b/modules/md/pair-distance/routine.pseudo new file mode 100644 index 0000000..fbf1648 --- /dev/null +++ b/modules/md/pair-distance/routine.pseudo @@ -0,0 +1,11 @@ +// PairDistance: the distance between two points in a cubic periodic box of +// side Box, each coordinate difference taken by the minimum image +// convention. HYPOT adds the squares without overflowing on the way. +USE md.MinimumImage +FUNCTION PairDistance(A : LIST OF FLOAT, B : LIST OF FLOAT, Box : FLOAT) RETURNS FLOAT + REQUIRE LENGTH(A) == 3 AND LENGTH(B) == 3 AND Box > 0.0 + DECLARE Dx : FLOAT <- MinimumImage(A[0] - B[0], Box) + DECLARE Dy : FLOAT <- MinimumImage(A[1] - B[1], Box) + DECLARE Dz : FLOAT <- MinimumImage(A[2] - B[2], Box) + RETURN HYPOT(HYPOT(Dx, Dy), Dz) +ENDFUNCTION diff --git a/modules/numerics/kahan-sum/golden/dafny.json b/modules/numerics/kahan-sum/golden/dafny.json new file mode 100644 index 0000000..06c4784 --- /dev/null +++ b/modules/numerics/kahan-sum/golden/dafny.json @@ -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" +} diff --git a/modules/numerics/kahan-sum/proof/kahan_sum.dfy b/modules/numerics/kahan-sum/proof/kahan_sum.dfy new file mode 100644 index 0000000..c51a238 --- /dev/null +++ b/modules/numerics/kahan-sum/proof/kahan_sum.dfy @@ -0,0 +1,26 @@ +function Sum(x: seq): 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) 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; +} diff --git a/modules/numerics/kahan-sum/ref/c/plain.c b/modules/numerics/kahan-sum/ref/c/plain.c new file mode 100644 index 0000000..406c380 --- /dev/null +++ b/modules/numerics/kahan-sum/ref/c/plain.c @@ -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; +} diff --git a/modules/numerics/kahan-sum/ref/mojo/plain.mojo b/modules/numerics/kahan-sum/ref/mojo/plain.mojo new file mode 100644 index 0000000..4a77995 --- /dev/null +++ b/modules/numerics/kahan-sum/ref/mojo/plain.mojo @@ -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 diff --git a/modules/numerics/kahan-sum/ref/python/plain.py b/modules/numerics/kahan-sum/ref/python/plain.py new file mode 100644 index 0000000..de648a9 --- /dev/null +++ b/modules/numerics/kahan-sum/ref/python/plain.py @@ -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 diff --git a/modules/numerics/kahan-sum/routine.json b/modules/numerics/kahan-sum/routine.json new file mode 100644 index 0000000..1d90aeb --- /dev/null +++ b/modules/numerics/kahan-sum/routine.json @@ -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." + } +} diff --git a/modules/numerics/kahan-sum/routine.pseudo b/modules/numerics/kahan-sum/routine.pseudo new file mode 100644 index 0000000..60c1824 --- /dev/null +++ b/modules/numerics/kahan-sum/routine.pseudo @@ -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 diff --git a/modules/numerics/linear-interp/ref/mojo/plain.mojo b/modules/numerics/linear-interp/ref/mojo/plain.mojo new file mode 100644 index 0000000..016df22 --- /dev/null +++ b/modules/numerics/linear-interp/ref/mojo/plain.mojo @@ -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) diff --git a/modules/numerics/linear-interp/ref/python/plain.py b/modules/numerics/linear-interp/ref/python/plain.py new file mode 100644 index 0000000..1b558f8 --- /dev/null +++ b/modules/numerics/linear-interp/ref/python/plain.py @@ -0,0 +1,2 @@ +def linear_interp(x0, y0, x1, y1, x): + return y0 + (y1 - y0) * (x - x0) / (x1 - x0) diff --git a/modules/numerics/linear-interp/routine.json b/modules/numerics/linear-interp/routine.json new file mode 100644 index 0000000..ccafe88 --- /dev/null +++ b/modules/numerics/linear-interp/routine.json @@ -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." + } + ] +} diff --git a/modules/numerics/linear-interp/routine.pseudo b/modules/numerics/linear-interp/routine.pseudo new file mode 100644 index 0000000..f99a1fc --- /dev/null +++ b/modules/numerics/linear-interp/routine.pseudo @@ -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 diff --git a/modules/numerics/module.json b/modules/numerics/module.json new file mode 100644 index 0000000..be17b8b --- /dev/null +++ b/modules/numerics/module.json @@ -0,0 +1,5 @@ +{ + "name": "numerics", + "title": "Numerics", + "summary": "Summing, interpolating and integrating with FLOAT values, and what rounding does to each." +} diff --git a/modules/numerics/trapezoid/ref/mojo/plain.mojo b/modules/numerics/trapezoid/ref/mojo/plain.mojo new file mode 100644 index 0000000..8acdbc6 --- /dev/null +++ b/modules/numerics/trapezoid/ref/mojo/plain.mojo @@ -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 diff --git a/modules/numerics/trapezoid/ref/python/plain.py b/modules/numerics/trapezoid/ref/python/plain.py new file mode 100644 index 0000000..a519d63 --- /dev/null +++ b/modules/numerics/trapezoid/ref/python/plain.py @@ -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 diff --git a/modules/numerics/trapezoid/routine.json b/modules/numerics/trapezoid/routine.json new file mode 100644 index 0000000..114dee9 --- /dev/null +++ b/modules/numerics/trapezoid/routine.json @@ -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." + } + ] +} diff --git a/modules/numerics/trapezoid/routine.pseudo b/modules/numerics/trapezoid/routine.pseudo new file mode 100644 index 0000000..d15d1ff --- /dev/null +++ b/modules/numerics/trapezoid/routine.pseudo @@ -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 diff --git a/modules/seq/gc-content/golden/dafny.json b/modules/seq/gc-content/golden/dafny.json new file mode 100644 index 0000000..ec3e1fd --- /dev/null +++ b/modules/seq/gc-content/golden/dafny.json @@ -0,0 +1,12 @@ +{ + "tool": "dafny", + "version": "4.11.0", + "flags": [ + "verify", + "--resource-limit", + "20000000" + ], + "source_sha256": "f2c5d94943f4b176c99c71e1073bb1c6a8ec6d2eb560d03526aaa3476b9162b1", + "verdict": "verified", + "summary": "5 verified, 0 errors" +} diff --git a/modules/seq/gc-content/proof/gc_content.dfy b/modules/seq/gc-content/proof/gc_content.dfy new file mode 100644 index 0000000..243b80b --- /dev/null +++ b/modules/seq/gc-content/proof/gc_content.dfy @@ -0,0 +1,30 @@ +function GC(s: string, n: nat): nat + requires n <= |s| +{ + if n == 0 then 0 else GC(s, n - 1) + (if s[n - 1] == 'G' || s[n - 1] == 'C' then 1 else 0) +} + +lemma GCAtMost(s: string, n: nat) + requires n <= |s| + ensures GC(s, n) <= n +{ + if n > 0 { GCAtMost(s, n - 1); } +} + +method GCContent(s: string) returns (f: real) + requires |s| > 0 + ensures f == (GC(s, |s|) as real) / (|s| as real) + ensures 0.0 <= f <= 1.0 +{ + var count := 0; + var i := 0; + while i < |s| + invariant 0 <= i <= |s| + invariant count == GC(s, i) + { + if s[i] == 'G' || s[i] == 'C' { count := count + 1; } + i := i + 1; + } + GCAtMost(s, |s|); + f := (count as real) / (|s| as real); +} diff --git a/modules/seq/gc-content/ref/mojo/plain.mojo b/modules/seq/gc-content/ref/mojo/plain.mojo new file mode 100644 index 0000000..b740df0 --- /dev/null +++ b/modules/seq/gc-content/ref/mojo/plain.mojo @@ -0,0 +1,7 @@ +def gc_content(s: String) -> Float64: + var count = 0 + for i in range(s.count_codepoints()): + var c = String(s[codepoint=i]) + if c == "G" or c == "C": + count = count + 1 + return Float64(count) / Float64(s.count_codepoints()) diff --git a/modules/seq/gc-content/ref/python/plain.py b/modules/seq/gc-content/ref/python/plain.py new file mode 100644 index 0000000..f37705e --- /dev/null +++ b/modules/seq/gc-content/ref/python/plain.py @@ -0,0 +1,6 @@ +def gc_content(s): + count = 0 + for c in s: + if c == "G" or c == "C": + count += 1 + return count / len(s) diff --git a/modules/seq/gc-content/routine.json b/modules/seq/gc-content/routine.json new file mode 100644 index 0000000..99439da --- /dev/null +++ b/modules/seq/gc-content/routine.json @@ -0,0 +1,43 @@ +{ + "name": "GCContent", + "summary": "The fraction of the letters of a sequence that are G or C.", + "tags": [ + "sequences", + "counting" + ], + "sources": [ + { + "title": "Any molecular biology text: GC content" + } + ], + "cases": [ + { + "call": "GCContent(\"GATTACA\")" + }, + { + "call": "GCContent(\"GGCC\")" + }, + { + "call": "GCContent(\"ATAT\")" + } + ], + "references": [ + { + "lang": "python", + "level": "plain", + "file": "ref/python/plain.py", + "note": "The same count." + }, + { + "lang": "mojo", + "level": "plain", + "file": "ref/mojo/plain.mojo", + "note": "The same count in Mojo." + } + ], + "proof": { + "lang": "dafny", + "file": "proof/gc_content.dfy", + "note": "The count never exceeds the length, so the fraction lies in [0, 1]." + } +} diff --git a/modules/seq/gc-content/routine.pseudo b/modules/seq/gc-content/routine.pseudo new file mode 100644 index 0000000..aaf56c4 --- /dev/null +++ b/modules/seq/gc-content/routine.pseudo @@ -0,0 +1,12 @@ +// GCContent: the fraction of the letters of S that are G or C. Other letters +// (N for an unknown base, lowercase) count in the length and not in the count. +FUNCTION GCContent(S : STRING) RETURNS FLOAT + REQUIRE LENGTH(S) > 0 + DECLARE Count : INTEGER <- 0 + FOR I <- 0 TO LENGTH(S) + IF S[I] == 'G' OR S[I] == 'C' THEN + Count <- Count + 1 + ENDIF + NEXT I + RETURN Count / LENGTH(S) +ENDFUNCTION diff --git a/modules/seq/hamming-distance/golden/dafny.json b/modules/seq/hamming-distance/golden/dafny.json new file mode 100644 index 0000000..4b19b63 --- /dev/null +++ b/modules/seq/hamming-distance/golden/dafny.json @@ -0,0 +1,12 @@ +{ + "tool": "dafny", + "version": "4.11.0", + "flags": [ + "verify", + "--resource-limit", + "20000000" + ], + "source_sha256": "0a1be187ee17e37bf36220aa4c326a907dd0e39eb38710cd77ed05bed2479950", + "verdict": "verified", + "summary": "5 verified, 0 errors" +} diff --git a/modules/seq/hamming-distance/proof/hamming_distance.dfy b/modules/seq/hamming-distance/proof/hamming_distance.dfy new file mode 100644 index 0000000..68bb2a6 --- /dev/null +++ b/modules/seq/hamming-distance/proof/hamming_distance.dfy @@ -0,0 +1,29 @@ +function Mismatches(s: string, t: string, n: nat): nat + requires n <= |s| && n <= |t| +{ + if n == 0 then 0 else Mismatches(s, t, n - 1) + (if s[n - 1] != t[n - 1] then 1 else 0) +} + +lemma MismatchesAtMost(s: string, t: string, n: nat) + requires n <= |s| && n <= |t| + ensures Mismatches(s, t, n) <= n +{ + if n > 0 { MismatchesAtMost(s, t, n - 1); } +} + +method HammingDistance(s: string, t: string) returns (count: int) + requires |s| == |t| + ensures count == Mismatches(s, t, |s|) + ensures 0 <= count <= |s| +{ + count := 0; + var i := 0; + while i < |s| + invariant 0 <= i <= |s| + invariant count == Mismatches(s, t, i) + { + if s[i] != t[i] { count := count + 1; } + i := i + 1; + } + MismatchesAtMost(s, t, |s|); +} diff --git a/modules/seq/hamming-distance/ref/mojo/plain.mojo b/modules/seq/hamming-distance/ref/mojo/plain.mojo new file mode 100644 index 0000000..ea6264e --- /dev/null +++ b/modules/seq/hamming-distance/ref/mojo/plain.mojo @@ -0,0 +1,6 @@ +def hamming_distance(s: String, t: String) -> Int: + var count = 0 + for i in range(s.count_codepoints()): + if String(s[codepoint=i]) != String(t[codepoint=i]): + count = count + 1 + return count diff --git a/modules/seq/hamming-distance/ref/python/plain.py b/modules/seq/hamming-distance/ref/python/plain.py new file mode 100644 index 0000000..c007491 --- /dev/null +++ b/modules/seq/hamming-distance/ref/python/plain.py @@ -0,0 +1,2 @@ +def hamming_distance(s, t): + return sum(1 for a, b in zip(s, t) if a != b) diff --git a/modules/seq/hamming-distance/routine.json b/modules/seq/hamming-distance/routine.json new file mode 100644 index 0000000..e3d3e0a --- /dev/null +++ b/modules/seq/hamming-distance/routine.json @@ -0,0 +1,43 @@ +{ + "name": "HammingDistance", + "summary": "How many positions of two sequences of the same length hold different letters.", + "tags": [ + "sequences", + "distance" + ], + "sources": [ + { + "title": "Hamming, Error detecting and error correcting codes, Bell System Technical Journal 29 (1950)" + } + ], + "cases": [ + { + "call": "HammingDistance(\"GATTACA\", \"GACTATA\")" + }, + { + "call": "HammingDistance(\"\", \"\")" + }, + { + "call": "HammingDistance(\"AAAA\", \"TTTT\")" + } + ], + "references": [ + { + "lang": "python", + "level": "plain", + "file": "ref/python/plain.py", + "note": "The same count." + }, + { + "lang": "mojo", + "level": "plain", + "file": "ref/mojo/plain.mojo", + "note": "The same count in Mojo." + } + ], + "proof": { + "lang": "dafny", + "file": "proof/hamming_distance.dfy", + "note": "The loop computes the number of mismatched positions, which lies in [0, |S|]." + } +} diff --git a/modules/seq/hamming-distance/routine.pseudo b/modules/seq/hamming-distance/routine.pseudo new file mode 100644 index 0000000..1066940 --- /dev/null +++ b/modules/seq/hamming-distance/routine.pseudo @@ -0,0 +1,15 @@ +// HammingDistance: how many positions of two sequences of the same length +// hold different letters. The INVARIANT is what makes the ENSURE provable: +// after I positions, at most I mismatches can have been counted. +FUNCTION HammingDistance(S : STRING, T : STRING) RETURNS INTEGER + REQUIRE LENGTH(S) == LENGTH(T) + ENSURE 0 <= RESULT AND RESULT <= LENGTH(S) + DECLARE Count : INTEGER <- 0 + FOR I <- 0 TO LENGTH(S) + INVARIANT 0 <= Count AND Count <= I + IF S[I] != T[I] THEN + Count <- Count + 1 + ENDIF + NEXT I + RETURN Count +ENDFUNCTION diff --git a/modules/seq/module.json b/modules/seq/module.json new file mode 100644 index 0000000..b78f790 --- /dev/null +++ b/modules/seq/module.json @@ -0,0 +1,5 @@ +{ + "name": "seq", + "title": "Sequences", + "summary": "Counting and comparing the letters of biological sequences." +} diff --git a/modules/special/erf-approx/ref/mojo/plain.mojo b/modules/special/erf-approx/ref/mojo/plain.mojo new file mode 100644 index 0000000..b3a3412 --- /dev/null +++ b/modules/special/erf-approx/ref/mojo/plain.mojo @@ -0,0 +1,11 @@ +from std.math import exp + + +def erf_approx(x: Float64) -> Float64: + var sign: Float64 = 1.0 + if x < 0.0: + sign = -1.0 + var a = abs(x) + var t = 1.0 / (1.0 + 0.3275911 * a) + var poly = ((((1.061405429 * t - 1.453152027) * t + 1.421413741) * t - 0.284496736) * t + 0.254829592) * t + return sign * (1.0 - poly * exp(-a * a)) diff --git a/modules/special/erf-approx/ref/python/libm.py b/modules/special/erf-approx/ref/python/libm.py new file mode 100644 index 0000000..c67cf8e --- /dev/null +++ b/modules/special/erf-approx/ref/python/libm.py @@ -0,0 +1,5 @@ +import math + + +def erf_approx(x): + return math.erf(x) diff --git a/modules/special/erf-approx/ref/python/plain.py b/modules/special/erf-approx/ref/python/plain.py new file mode 100644 index 0000000..d46a157 --- /dev/null +++ b/modules/special/erf-approx/ref/python/plain.py @@ -0,0 +1,9 @@ +import math + + +def erf_approx(x): + sign = -1.0 if x < 0.0 else 1.0 + a = abs(x) + t = 1.0 / (1.0 + 0.3275911 * a) + poly = ((((1.061405429 * t - 1.453152027) * t + 1.421413741) * t - 0.284496736) * t + 0.254829592) * t + return sign * (1.0 - poly * math.exp(-a * a)) diff --git a/modules/special/erf-approx/routine.json b/modules/special/erf-approx/routine.json new file mode 100644 index 0000000..2634b60 --- /dev/null +++ b/modules/special/erf-approx/routine.json @@ -0,0 +1,52 @@ +{ + "name": "ErfApprox", + "summary": "The error function by Abramowitz and Stegun's formula 7.1.26, within 1.5e-7 of the exact value everywhere.", + "tags": [ + "special functions", + "approximation", + "statistics" + ], + "sources": [ + { + "title": "Abramowitz and Stegun, Handbook of Mathematical Functions, formula 7.1.26" + } + ], + "cases": [ + { + "call": "ErfApprox(0.0)" + }, + { + "call": "ErfApprox(0.5)" + }, + { + "call": "ErfApprox(-1.0)" + }, + { + "call": "ErfApprox(2.0)" + }, + { + "call": "ErfApprox(3.5)" + } + ], + "references": [ + { + "lang": "python", + "level": "plain", + "file": "ref/python/plain.py", + "note": "The same formula." + }, + { + "lang": "python", + "level": "libm", + "file": "ref/python/libm.py", + "note": "math.erf, the C library's correctly rounded erf: the approximation must stay within its stated 1.5e-7 of it.", + "abs": 1.5e-07 + }, + { + "lang": "mojo", + "level": "plain", + "file": "ref/mojo/plain.mojo", + "note": "The same formula in Mojo." + } + ] +} diff --git a/modules/special/erf-approx/routine.pseudo b/modules/special/erf-approx/routine.pseudo new file mode 100644 index 0000000..2bad827 --- /dev/null +++ b/modules/special/erf-approx/routine.pseudo @@ -0,0 +1,15 @@ +// ErfApprox: the error function by Abramowitz and Stegun's formula 7.1.26, +// with an absolute error below 1.5e-7 for every X. It is an approximation +// with a stated bound, not the correctly rounded erf: the libm reference +// holds it to that bound. ERF(X) for X < 0 is -ERF(-X), so the sign is set +// aside first. +FUNCTION ErfApprox(X : FLOAT) RETURNS FLOAT + DECLARE Sign : FLOAT <- 1.0 + IF X < 0.0 THEN + Sign <- -1.0 + ENDIF + DECLARE A : FLOAT <- ABS(X) + DECLARE T : FLOAT <- 1.0 / (1.0 + 0.3275911 * A) + DECLARE Poly : FLOAT <- ((((1.061405429 * T - 1.453152027) * T + 1.421413741) * T - 0.284496736) * T + 0.254829592) * T + RETURN Sign * (1.0 - Poly * EXP(-A * A)) +ENDFUNCTION diff --git a/modules/special/module.json b/modules/special/module.json new file mode 100644 index 0000000..7300d33 --- /dev/null +++ b/modules/special/module.json @@ -0,0 +1,5 @@ +{ + "name": "special", + "title": "Special functions", + "summary": "Functions with no exact built-in, as approximations with a stated error bound." +} diff --git a/modules/stats/mean/golden/dafny.json b/modules/stats/mean/golden/dafny.json new file mode 100644 index 0000000..f3df99a --- /dev/null +++ b/modules/stats/mean/golden/dafny.json @@ -0,0 +1,12 @@ +{ + "tool": "dafny", + "version": "4.11.0", + "flags": [ + "verify", + "--resource-limit", + "20000000" + ], + "source_sha256": "2c1cfe5cfd7f6a1f1d930e526ab91e381efef2b847ff56cef529d111c7e97fad", + "verdict": "verified", + "summary": "9 verified, 0 errors" +} diff --git a/modules/stats/mean/proof/mean.dfy b/modules/stats/mean/proof/mean.dfy new file mode 100644 index 0000000..0ee569e --- /dev/null +++ b/modules/stats/mean/proof/mean.dfy @@ -0,0 +1,49 @@ +// Mean, over real numbers. The FLOAT version rounds; this one does not. +function Sum(x: seq): real +{ + if |x| == 0 then 0.0 else Sum(x[..|x| - 1]) + x[|x| - 1] +} + +method Mean(x: seq) returns (m: real) + requires |x| > 0 + ensures m == Sum(x) / (|x| as real) +{ + var total := 0.0; + var i := 0; + while i < |x| + invariant 0 <= i <= |x| + invariant total == Sum(x[..i]) + { + assert x[..i + 1][..i] == x[..i]; + total := total + x[i]; + i := i + 1; + } + assert x[..|x|] == x; + m := total / (|x| as real); +} + +// Every value at most hi: the sum is at most |x| * hi. +lemma SumAtMost(x: seq, hi: real) + requires forall i :: 0 <= i < |x| ==> x[i] <= hi + ensures Sum(x) <= (|x| as real) * hi +{ + if |x| > 0 { SumAtMost(x[..|x| - 1], hi); } +} + +// Every value at least lo: the sum is at least |x| * lo. +lemma SumAtLeast(x: seq, lo: real) + requires forall i :: 0 <= i < |x| ==> lo <= x[i] + ensures (|x| as real) * lo <= Sum(x) +{ + if |x| > 0 { SumAtLeast(x[..|x| - 1], lo); } +} + +// In exact arithmetic the mean lies between any lower and upper bound of the values. +lemma MeanBetween(x: seq, lo: real, hi: real) + requires |x| > 0 + requires forall i :: 0 <= i < |x| ==> lo <= x[i] <= hi + ensures lo <= Sum(x) / (|x| as real) <= hi +{ + SumAtMost(x, hi); + SumAtLeast(x, lo); +} diff --git a/modules/stats/mean/ref/c/plain.c b/modules/stats/mean/ref/c/plain.c new file mode 100644 index 0000000..02e866c --- /dev/null +++ b/modules/stats/mean/ref/c/plain.c @@ -0,0 +1,5 @@ +double mean(const double *x, long n) { + double total = 0.0; + for (long i = 0; i < n; i++) total += x[i]; + return total / (double)n; +} diff --git a/modules/stats/mean/ref/mojo/plain.mojo b/modules/stats/mean/ref/mojo/plain.mojo new file mode 100644 index 0000000..bf8a55d --- /dev/null +++ b/modules/stats/mean/ref/mojo/plain.mojo @@ -0,0 +1,5 @@ +def mean(x: List[Float64]) -> Float64: + var total: Float64 = 0.0 + for i in range(len(x)): + total = total + x[i] + return total / Float64(len(x)) diff --git a/modules/stats/mean/ref/mojo/unrolled.mojo b/modules/stats/mean/ref/mojo/unrolled.mojo new file mode 100644 index 0000000..984d176 --- /dev/null +++ b/modules/stats/mean/ref/mojo/unrolled.mojo @@ -0,0 +1,17 @@ +def mean(x: List[Float64]) -> Float64: + var s0: Float64 = 0.0 + var s1: Float64 = 0.0 + var s2: Float64 = 0.0 + var s3: Float64 = 0.0 + var n = len(x) + var i = 0 + while i + 4 <= n: + s0 = s0 + x[i] + s1 = s1 + x[i + 1] + s2 = s2 + x[i + 2] + s3 = s3 + x[i + 3] + i = i + 4 + while i < n: + s0 = s0 + x[i] + i = i + 1 + return ((s0 + s1) + (s2 + s3)) / Float64(n) diff --git a/modules/stats/mean/ref/python/fsum.py b/modules/stats/mean/ref/python/fsum.py new file mode 100644 index 0000000..c9b2b4d --- /dev/null +++ b/modules/stats/mean/ref/python/fsum.py @@ -0,0 +1,5 @@ +import math + + +def mean(x): + return math.fsum(x) / len(x) diff --git a/modules/stats/mean/ref/python/plain.py b/modules/stats/mean/ref/python/plain.py new file mode 100644 index 0000000..78febf2 --- /dev/null +++ b/modules/stats/mean/ref/python/plain.py @@ -0,0 +1,5 @@ +def mean(x): + total = 0.0 + for v in x: + total += v + return total / len(x) diff --git a/modules/stats/mean/routine.json b/modules/stats/mean/routine.json new file mode 100644 index 0000000..434b2a3 --- /dev/null +++ b/modules/stats/mean/routine.json @@ -0,0 +1,69 @@ +{ + "name": "Mean", + "summary": "The arithmetic mean of a list of numbers.", + "tags": [ + "statistics", + "summation" + ], + "sources": [ + { + "title": "Any statistics text: the sample mean, the sum divided by the count" + } + ], + "cases": [ + { + "call": "Mean([1.0, 2.0, 3.0, 4.0])" + }, + { + "call": "Mean([0.1, 0.1, 0.1])" + }, + { + "call": "Mean([-5.0, 5.0])" + }, + { + "call": "Mean([1.0e300, 1.0e300])" + }, + { + "call": "Mean([2.5])" + } + ], + "references": [ + { + "lang": "python", + "level": "plain", + "file": "ref/python/plain.py", + "note": "The same loop: one running total, added to in order." + }, + { + "lang": "python", + "level": "fsum", + "file": "ref/python/fsum.py", + "note": "math.fsum rounds the sum once, exactly; the plain loop rounds at every step, so the two can differ in the last digit.", + "rel": 1e-15 + }, + { + "lang": "c", + "level": "plain", + "file": "ref/c/plain.c", + "note": "The same loop in C. Without -ffast-math the compiler keeps the additions in order at every optimization level." + }, + { + "lang": "mojo", + "level": "plain", + "file": "ref/mojo/plain.mojo", + "note": "The same loop in Mojo." + }, + { + "lang": "mojo", + "level": "unrolled", + "file": "ref/mojo/unrolled.mojo", + "note": "Four running totals, as a vectorizing compiler keeps them: faster on long lists, and the additions happen in a different order, so the last digit can differ.", + "rel": 1e-15 + } + ], + "proof": { + "lang": "dafny", + "file": "proof/mean.dfy", + "note": "Over real numbers: the loop computes Sum(X) / |X|, and the mean lies between the smallest and largest value (rounding can push the FLOAT result just outside, as Mean([0.1, 0.1, 0.1]) shows)." + } +} diff --git a/modules/stats/mean/routine.pseudo b/modules/stats/mean/routine.pseudo new file mode 100644 index 0000000..d62a164 --- /dev/null +++ b/modules/stats/mean/routine.pseudo @@ -0,0 +1,12 @@ +// Mean: the arithmetic mean of a list of numbers. +// The running total is a FLOAT, rounded at every addition, so the mean of +// [0.1, 0.1, 0.1] comes out a little above 0.1. For long lists of values of +// very different sizes, sum with numerics.KahanSum instead. +FUNCTION Mean(X : LIST OF FLOAT) RETURNS FLOAT + REQUIRE LENGTH(X) > 0 + DECLARE Total : FLOAT <- 0.0 + FOR I <- 0 TO LENGTH(X) + Total <- Total + X[I] + NEXT I + RETURN Total / LENGTH(X) +ENDFUNCTION diff --git a/modules/stats/module.json b/modules/stats/module.json new file mode 100644 index 0000000..f5a7799 --- /dev/null +++ b/modules/stats/module.json @@ -0,0 +1,5 @@ +{ + "name": "stats", + "title": "Statistics", + "summary": "Summaries of a list of measurements: where it sits and how far it spreads." +} diff --git a/modules/stats/variance/ref/mojo/plain.mojo b/modules/stats/variance/ref/mojo/plain.mojo new file mode 100644 index 0000000..d0bc34e --- /dev/null +++ b/modules/stats/variance/ref/mojo/plain.mojo @@ -0,0 +1,9 @@ +def variance(x: List[Float64]) -> Float64: + var m: Float64 = 0.0 + for i in range(len(x)): + m = m + x[i] + m = m / Float64(len(x)) + var total: Float64 = 0.0 + for i in range(len(x)): + total = total + (x[i] - m) * (x[i] - m) + return total / Float64(len(x) - 1) diff --git a/modules/stats/variance/ref/python/plain.py b/modules/stats/variance/ref/python/plain.py new file mode 100644 index 0000000..1e20db8 --- /dev/null +++ b/modules/stats/variance/ref/python/plain.py @@ -0,0 +1,9 @@ +def variance(x): + m = 0.0 + for v in x: + m += v + m /= len(x) + total = 0.0 + for v in x: + total += (v - m) * (v - m) + return total / (len(x) - 1) diff --git a/modules/stats/variance/ref/python/statistics.py b/modules/stats/variance/ref/python/statistics.py new file mode 100644 index 0000000..0c3e2e7 --- /dev/null +++ b/modules/stats/variance/ref/python/statistics.py @@ -0,0 +1,5 @@ +import statistics + + +def variance(x): + return statistics.variance(x) diff --git a/modules/stats/variance/routine.json b/modules/stats/variance/routine.json new file mode 100644 index 0000000..da1ef61 --- /dev/null +++ b/modules/stats/variance/routine.json @@ -0,0 +1,46 @@ +{ + "name": "Variance", + "summary": "The sample variance of a list of numbers (dividing by N - 1), computed in two passes.", + "tags": [ + "statistics", + "cancellation" + ], + "sources": [ + { + "title": "Chan, Golub and LeVeque, Algorithms for computing the sample variance (1983)", + "url": "https://doi.org/10.1080/00031305.1983.10483115" + } + ], + "cases": [ + { + "call": "Variance([2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0])" + }, + { + "call": "Variance([1.0, 2.0])" + }, + { + "call": "Variance([1.0e9 + 4.0, 1.0e9 + 7.0, 1.0e9 + 13.0, 1.0e9 + 16.0])" + } + ], + "references": [ + { + "lang": "python", + "level": "plain", + "file": "ref/python/plain.py", + "note": "The same two passes." + }, + { + "lang": "python", + "level": "statistics", + "file": "ref/python/statistics.py", + "note": "The standard library's statistics.variance, which works with exact fractions inside: a different method, held to agree to 12 digits.", + "rel": 1e-12 + }, + { + "lang": "mojo", + "level": "plain", + "file": "ref/mojo/plain.mojo", + "note": "The same two passes in Mojo." + } + ] +} diff --git a/modules/stats/variance/routine.pseudo b/modules/stats/variance/routine.pseudo new file mode 100644 index 0000000..de93978 --- /dev/null +++ b/modules/stats/variance/routine.pseudo @@ -0,0 +1,14 @@ +// Variance: the sample variance (dividing by N - 1), in two passes: the mean +// first, then the squared distances from it. The one-pass formula +// (SUM X^2 - N * MEAN^2) / (N - 1) subtracts two nearly equal large numbers +// and loses every digit when the values sit far from 0, as the third case shows. +USE stats.Mean +FUNCTION Variance(X : LIST OF FLOAT) RETURNS FLOAT + REQUIRE LENGTH(X) >= 2 + DECLARE M : FLOAT <- Mean(X) + DECLARE Total : FLOAT <- 0.0 + FOR I <- 0 TO LENGTH(X) + Total <- Total + (X[I] - M) * (X[I] - M) + NEXT I + RETURN Total / (LENGTH(X) - 1) +ENDFUNCTION diff --git a/status.json b/status.json new file mode 100644 index 0000000..187992e --- /dev/null +++ b/status.json @@ -0,0 +1,1053 @@ +{ + "comment": "Written by pixi run library-check in pplus-core. Do not edit.", + "routines": { + "align.AffineGapPenalty": { + "checks": { + "structure": { + "status": "pass", + "detail": "defines FUNCTION AffineGapPenalty" + }, + "pseudo": { + "status": "pass", + "detail": "3 case(s) run" + }, + "prover": { + "status": "pass", + "detail": "all 1 obligation(s) proved" + }, + "fuzz": { + "status": "pass", + "detail": "no failure in 201 generated input(s)" + }, + "mojo": { + "status": "pass", + "detail": "the generated Mojo prints what the pseudo prints on every case, in core's interpreter" + } + }, + "cases": [ + { + "call": "AffineGapPenalty(1, -10, -1)", + "result": "-10" + }, + { + "call": "AffineGapPenalty(5, -10, -1)", + "result": "-14" + }, + { + "call": "AffineGapPenalty(3, -2, -2)", + "result": "-6" + } + ], + "references": { + "python/plain": { + "cpython": { + "status": "pass", + "detail": "agrees with the pseudo on 3 case(s) (exactly)" + } + }, + "mojo/plain": { + "interpreter": { + "status": "pass", + "detail": "agrees with the pseudo on 3 case(s) in core's interpreter (exactly)" + }, + "godbolt": { + "status": "unverified", + "detail": "no stored answer from mojo_1_1_0 for this program: pixi run library-verify-mojo" + } + } + }, + "proof": { + "status": "pass", + "detail": "Dafny 4.11.0: 3 verified, 0 errors" + } + }, + "align.LinearGapPenalty": { + "checks": { + "structure": { + "status": "pass", + "detail": "defines FUNCTION LinearGapPenalty" + }, + "pseudo": { + "status": "pass", + "detail": "3 case(s) run" + }, + "prover": { + "status": "pass", + "detail": "all 1 obligation(s) proved" + }, + "fuzz": { + "status": "pass", + "detail": "no failure in 123 generated input(s)" + }, + "mojo": { + "status": "pass", + "detail": "the generated Mojo prints what the pseudo prints on every case, in core's interpreter" + } + }, + "cases": [ + { + "call": "LinearGapPenalty(3, -2)", + "result": "-6" + }, + { + "call": "LinearGapPenalty(0, -2)", + "result": "0" + }, + { + "call": "LinearGapPenalty(10, -1)", + "result": "-10" + } + ], + "references": { + "python/plain": { + "cpython": { + "status": "pass", + "detail": "agrees with the pseudo on 3 case(s) (exactly)" + } + }, + "mojo/plain": { + "interpreter": { + "status": "pass", + "detail": "agrees with the pseudo on 3 case(s) in core's interpreter (exactly)" + }, + "godbolt": { + "status": "unverified", + "detail": "no stored answer from mojo_1_1_0 for this program: pixi run library-verify-mojo" + } + } + }, + "proof": { + "status": "pass", + "detail": "Dafny 4.11.0: 2 verified, 0 errors" + } + }, + "align.MatchScore": { + "checks": { + "structure": { + "status": "pass", + "detail": "defines FUNCTION MatchScore" + }, + "pseudo": { + "status": "pass", + "detail": "3 case(s) run" + }, + "prover": { + "status": "pass", + "detail": "all 2 obligation(s) proved" + }, + "fuzz": { + "status": "pass", + "detail": "no failure in 285 generated input(s)" + }, + "mojo": { + "status": "pass", + "detail": "the generated Mojo prints what the pseudo prints on every case, in core's interpreter" + } + }, + "cases": [ + { + "call": "MatchScore('A', 'A', 1, -1)", + "result": "1" + }, + { + "call": "MatchScore('A', 'G', 1, -1)", + "result": "-1" + }, + { + "call": "MatchScore('T', 'T', 5, -4)", + "result": "5" + } + ], + "references": { + "python/plain": { + "cpython": { + "status": "pass", + "detail": "agrees with the pseudo on 3 case(s) (exactly)" + } + }, + "mojo/plain": { + "interpreter": { + "status": "pass", + "detail": "agrees with the pseudo on 3 case(s) in core's interpreter (exactly)" + }, + "godbolt": { + "status": "unverified", + "detail": "no stored answer from mojo_1_1_0 for this program: pixi run library-verify-mojo" + } + } + }, + "proof": { + "status": "pass", + "detail": "Dafny 4.11.0: 1 verified, 0 errors" + } + }, + "align.NeedlemanWunschScore": { + "checks": { + "structure": { + "status": "pass", + "detail": "defines FUNCTION NeedlemanWunschScore, using align.MatchScore, align.LinearGapPenalty" + }, + "pseudo": { + "status": "pass", + "detail": "4 case(s) run" + }, + "prover": { + "status": "pass", + "detail": "all 11 obligation(s) proved" + }, + "fuzz": { + "status": "pass", + "detail": "no failure in 300 generated input(s)" + }, + "mojo": { + "status": "pass", + "detail": "the generated Mojo prints what the pseudo prints on every case, in core's interpreter" + } + }, + "cases": [ + { + "call": "NeedlemanWunschScore(\"GATTACA\", \"GCATGCU\", 1, -1, -1)", + "result": "0" + }, + { + "call": "NeedlemanWunschScore(\"ACGT\", \"ACGT\", 1, -1, -2)", + "result": "4" + }, + { + "call": "NeedlemanWunschScore(\"\", \"ACG\", 1, -1, -2)", + "result": "-6" + }, + { + "call": "NeedlemanWunschScore(\"AAAA\", \"TTTT\", 2, -3, -1)", + "result": "-8" + } + ], + "references": { + "python/plain": { + "cpython": { + "status": "pass", + "detail": "agrees with the pseudo on 4 case(s) (exactly)" + } + }, + "python/two-rows": { + "cpython": { + "status": "pass", + "detail": "agrees with the pseudo on 4 case(s) (exactly)" + } + }, + "mojo/plain": { + "interpreter": { + "status": "pass", + "detail": "agrees with the pseudo on 4 case(s) in core's interpreter (exactly)" + }, + "godbolt": { + "status": "unverified", + "detail": "no stored answer from mojo_1_1_0 for this program: pixi run library-verify-mojo" + } + } + } + }, + "md.LennardJones": { + "checks": { + "structure": { + "status": "pass", + "detail": "defines FUNCTION LennardJones" + }, + "pseudo": { + "status": "pass", + "detail": "4 case(s) run" + }, + "prover": { + "status": "pass", + "detail": "all 2 obligation(s) proved" + }, + "fuzz": { + "status": "pass", + "detail": "no failure in 252 generated input(s)" + }, + "mojo": { + "status": "pass", + "detail": "the generated Mojo prints what the pseudo prints on every case, in core's interpreter" + } + }, + "cases": [ + { + "call": "LennardJones(1.0, 1.0, 1.0)", + "result": "0.0" + }, + { + "call": "LennardJones(1.122462048309373, 1.0, 1.0)", + "result": "-1.0" + }, + { + "call": "LennardJones(2.5, 0.238, 3.4)", + "result": "32.09189752256677" + }, + { + "call": "LennardJones(0.9, 1.0, 1.0)", + "result": "6.6361189532529234" + } + ], + "references": { + "python/multiply": { + "cpython": { + "status": "pass", + "detail": "agrees with the pseudo on 4 case(s) (exactly)" + } + }, + "python/powers": { + "cpython": { + "status": "pass", + "detail": "agrees with the pseudo on 4 case(s) (within rel 1e-12, abs 0)" + } + }, + "c/multiply": { + "cc -O0": { + "status": "pass", + "detail": "agrees with the pseudo on 4 case(s) (exactly)" + }, + "cc -O2": { + "status": "pass", + "detail": "agrees with the pseudo on 4 case(s) (exactly)" + } + }, + "mojo/multiply": { + "interpreter": { + "status": "pass", + "detail": "agrees with the pseudo on 4 case(s) in core's interpreter (exactly)" + }, + "godbolt": { + "status": "unverified", + "detail": "no stored answer from mojo_1_1_0 for this program: pixi run library-verify-mojo" + } + } + }, + "proof": { + "status": "pass", + "detail": "Dafny 4.11.0: 14 verified, 0 errors" + } + }, + "md.LennardJonesForce": { + "checks": { + "structure": { + "status": "pass", + "detail": "defines FUNCTION LennardJonesForce" + }, + "pseudo": { + "status": "pass", + "detail": "3 case(s) run" + }, + "prover": { + "status": "pass", + "detail": "all 3 obligation(s) proved" + }, + "fuzz": { + "status": "pass", + "detail": "no failure in 252 generated input(s)" + }, + "mojo": { + "status": "pass", + "detail": "the generated Mojo prints what the pseudo prints on every case, in core's interpreter" + } + }, + "cases": [ + { + "call": "LennardJonesForce(1.0, 1.0, 1.0)", + "result": "24.0" + }, + { + "call": "LennardJonesForce(1.122462048309373, 1.0, 1.0)", + "result": "-2.3738310467722616e-15" + }, + { + "call": "LennardJonesForce(2.5, 0.238, 3.4)", + "result": "168.49822326347663" + } + ], + "references": { + "python/multiply": { + "cpython": { + "status": "pass", + "detail": "agrees with the pseudo on 3 case(s) (exactly)" + } + }, + "mojo/multiply": { + "interpreter": { + "status": "pass", + "detail": "agrees with the pseudo on 3 case(s) in core's interpreter (exactly)" + }, + "godbolt": { + "status": "unverified", + "detail": "no stored answer from mojo_1_1_0 for this program: pixi run library-verify-mojo" + } + } + } + }, + "md.MinimumImage": { + "checks": { + "structure": { + "status": "pass", + "detail": "defines FUNCTION MinimumImage" + }, + "pseudo": { + "status": "pass", + "detail": "5 case(s) run" + }, + "prover": { + "status": "pass", + "detail": "all 1 obligation(s) proved" + }, + "fuzz": { + "status": "pass", + "detail": "no failure in 241 generated input(s)" + }, + "mojo": { + "status": "pass", + "detail": "the generated Mojo prints what the pseudo prints on every case, in core's interpreter" + } + }, + "cases": [ + { + "call": "MinimumImage(0.75, 1.0)", + "result": "-0.25" + }, + { + "call": "MinimumImage(-0.75, 1.0)", + "result": "0.25" + }, + { + "call": "MinimumImage(2.3, 1.0)", + "result": "0.2999999999999998" + }, + { + "call": "MinimumImage(0.5, 1.0)", + "result": "-0.5" + }, + { + "call": "MinimumImage(-12.0, 10.0)", + "result": "-2.0" + } + ], + "references": { + "python/plain": { + "cpython": { + "status": "pass", + "detail": "agrees with the pseudo on 5 case(s) (exactly)" + } + }, + "c/plain": { + "cc -O0": { + "status": "pass", + "detail": "agrees with the pseudo on 5 case(s) (exactly)" + }, + "cc -O2": { + "status": "pass", + "detail": "agrees with the pseudo on 5 case(s) (exactly)" + } + }, + "mojo/plain": { + "interpreter": { + "status": "pass", + "detail": "agrees with the pseudo on 5 case(s) in core's interpreter (exactly)" + }, + "godbolt": { + "status": "unverified", + "detail": "no stored answer from mojo_1_1_0 for this program: pixi run library-verify-mojo" + } + } + }, + "proof": { + "status": "pass", + "detail": "Dafny 4.11.0: 4 verified, 0 errors" + } + }, + "md.PairDistance": { + "checks": { + "structure": { + "status": "pass", + "detail": "defines FUNCTION PairDistance, using md.MinimumImage" + }, + "pseudo": { + "status": "pass", + "detail": "3 case(s) run" + }, + "prover": { + "status": "pass", + "detail": "all 9 obligation(s) proved" + }, + "fuzz": { + "status": "pass", + "detail": "no failure in 4 generated input(s)" + }, + "mojo": { + "status": "skipped", + "detail": "core's Mojo interpreter declines hypot(...) as written here; the compiler's answer is needed" + } + }, + "cases": [ + { + "call": "PairDistance([0.1, 0.1, 0.1], [0.9, 0.9, 0.9], 1.0)", + "result": "0.34641016151377535" + }, + { + "call": "PairDistance([0.0, 0.0, 0.0], [3.0, 4.0, 0.0], 20.0)", + "result": "5.0" + }, + { + "call": "PairDistance([1.0, 2.0, 3.0], [1.0, 2.0, 3.0], 5.0)", + "result": "0.0" + } + ], + "references": { + "python/plain": { + "cpython": { + "status": "pass", + "detail": "agrees with the pseudo on 3 case(s) (within rel 1e-15, abs 0)" + } + }, + "mojo/plain": { + "interpreter": { + "status": "pass", + "detail": "agrees with the pseudo on 3 case(s) in core's interpreter (within rel 1e-15, abs 0)" + }, + "godbolt": { + "status": "unverified", + "detail": "no stored answer from mojo_1_1_0 for this program: pixi run library-verify-mojo" + } + } + } + }, + "numerics.KahanSum": { + "checks": { + "structure": { + "status": "pass", + "detail": "defines FUNCTION KahanSum" + }, + "pseudo": { + "status": "pass", + "detail": "4 case(s) run" + }, + "prover": { + "status": "pass", + "detail": "all 1 obligation(s) proved" + }, + "fuzz": { + "status": "pass", + "detail": "no failure in 263 generated input(s)" + }, + "mojo": { + "status": "pass", + "detail": "the generated Mojo prints what the pseudo prints on every case, in core's interpreter" + } + }, + "cases": [ + { + "call": "KahanSum([1.0, 2.0, 3.0])", + "result": "6.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])", + "result": "1.000000000000001" + }, + { + "call": "KahanSum([0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1])", + "result": "1.0" + }, + { + "call": "KahanSum([])", + "result": "0.0" + } + ], + "references": { + "python/plain": { + "cpython": { + "status": "pass", + "detail": "agrees with the pseudo on 4 case(s) (exactly)" + } + }, + "c/plain": { + "cc -O0": { + "status": "pass", + "detail": "agrees with the pseudo on 4 case(s) (exactly)" + }, + "cc -O2": { + "status": "pass", + "detail": "agrees with the pseudo on 4 case(s) (exactly)" + } + }, + "mojo/plain": { + "interpreter": { + "status": "pass", + "detail": "agrees with the pseudo on 4 case(s) in core's interpreter (exactly)" + }, + "godbolt": { + "status": "unverified", + "detail": "no stored answer from mojo_1_1_0 for this program: pixi run library-verify-mojo" + } + } + }, + "proof": { + "status": "pass", + "detail": "Dafny 4.11.0: 2 verified, 0 errors" + } + }, + "numerics.LinearInterp": { + "checks": { + "structure": { + "status": "pass", + "detail": "defines FUNCTION LinearInterp" + }, + "pseudo": { + "status": "pass", + "detail": "3 case(s) run" + }, + "prover": { + "status": "pass", + "detail": "all 1 obligation(s) proved" + }, + "fuzz": { + "status": "pass", + "detail": "no failure in 300 generated input(s)" + }, + "mojo": { + "status": "pass", + "detail": "the generated Mojo prints what the pseudo prints on every case, in core's interpreter" + } + }, + "cases": [ + { + "call": "LinearInterp(0.0, 0.0, 10.0, 5.0, 4.0)", + "result": "2.0" + }, + { + "call": "LinearInterp(1.0, 2.0, 3.0, 2.0, 2.5)", + "result": "2.0" + }, + { + "call": "LinearInterp(0.0, 1.0, 1.0, 3.0, 2.0)", + "result": "5.0" + } + ], + "references": { + "python/plain": { + "cpython": { + "status": "pass", + "detail": "agrees with the pseudo on 3 case(s) (exactly)" + } + }, + "mojo/plain": { + "interpreter": { + "status": "pass", + "detail": "agrees with the pseudo on 3 case(s) in core's interpreter (exactly)" + }, + "godbolt": { + "status": "unverified", + "detail": "no stored answer from mojo_1_1_0 for this program: pixi run library-verify-mojo" + } + } + } + }, + "numerics.Trapezoid": { + "checks": { + "structure": { + "status": "pass", + "detail": "defines FUNCTION Trapezoid" + }, + "pseudo": { + "status": "pass", + "detail": "3 case(s) run" + }, + "prover": { + "status": "pass", + "detail": "all 4 obligation(s) proved" + }, + "fuzz": { + "status": "pass", + "detail": "no failure in 227 generated input(s)" + }, + "mojo": { + "status": "pass", + "detail": "the generated Mojo prints what the pseudo prints on every case, in core's interpreter" + } + }, + "cases": [ + { + "call": "Trapezoid([0.0, 1.0, 4.0, 9.0, 16.0], 1.0)", + "result": "22.0" + }, + { + "call": "Trapezoid([1.0, 1.0], 0.5)", + "result": "0.5" + }, + { + "call": "Trapezoid([0.0, 0.25, 1.0], 0.5)", + "result": "0.375" + } + ], + "references": { + "python/plain": { + "cpython": { + "status": "pass", + "detail": "agrees with the pseudo on 3 case(s) (exactly)" + } + }, + "mojo/plain": { + "interpreter": { + "status": "pass", + "detail": "agrees with the pseudo on 3 case(s) in core's interpreter (exactly)" + }, + "godbolt": { + "status": "unverified", + "detail": "no stored answer from mojo_1_1_0 for this program: pixi run library-verify-mojo" + } + } + } + }, + "seq.GCContent": { + "checks": { + "structure": { + "status": "pass", + "detail": "defines FUNCTION GCContent" + }, + "pseudo": { + "status": "pass", + "detail": "3 case(s) run" + }, + "prover": { + "status": "pass", + "detail": "all 3 obligation(s) proved" + }, + "fuzz": { + "status": "pass", + "detail": "no failure in 469 generated input(s)" + }, + "mojo": { + "status": "pass", + "detail": "the generated Mojo prints what the pseudo prints on every case, in core's interpreter" + } + }, + "cases": [ + { + "call": "GCContent(\"GATTACA\")", + "result": "0.2857142857142857" + }, + { + "call": "GCContent(\"GGCC\")", + "result": "1.0" + }, + { + "call": "GCContent(\"ATAT\")", + "result": "0.0" + } + ], + "references": { + "python/plain": { + "cpython": { + "status": "pass", + "detail": "agrees with the pseudo on 3 case(s) (exactly)" + } + }, + "mojo/plain": { + "interpreter": { + "status": "pass", + "detail": "agrees with the pseudo on 3 case(s) in core's interpreter (exactly)" + }, + "godbolt": { + "status": "unverified", + "detail": "no stored answer from mojo_1_1_0 for this program: pixi run library-verify-mojo" + } + } + }, + "proof": { + "status": "pass", + "detail": "Dafny 4.11.0: 5 verified, 0 errors" + } + }, + "seq.HammingDistance": { + "checks": { + "structure": { + "status": "pass", + "detail": "defines FUNCTION HammingDistance" + }, + "pseudo": { + "status": "pass", + "detail": "3 case(s) run" + }, + "prover": { + "status": "pass", + "detail": "all 5 obligation(s) proved" + }, + "fuzz": { + "status": "pass", + "detail": "no failure in 291 generated input(s)" + }, + "mojo": { + "status": "pass", + "detail": "the generated Mojo prints what the pseudo prints on every case, in core's interpreter" + } + }, + "cases": [ + { + "call": "HammingDistance(\"GATTACA\", \"GACTATA\")", + "result": "2" + }, + { + "call": "HammingDistance(\"\", \"\")", + "result": "0" + }, + { + "call": "HammingDistance(\"AAAA\", \"TTTT\")", + "result": "4" + } + ], + "references": { + "python/plain": { + "cpython": { + "status": "pass", + "detail": "agrees with the pseudo on 3 case(s) (exactly)" + } + }, + "mojo/plain": { + "interpreter": { + "status": "pass", + "detail": "agrees with the pseudo on 3 case(s) in core's interpreter (exactly)" + }, + "godbolt": { + "status": "unverified", + "detail": "no stored answer from mojo_1_1_0 for this program: pixi run library-verify-mojo" + } + } + }, + "proof": { + "status": "pass", + "detail": "Dafny 4.11.0: 5 verified, 0 errors" + } + }, + "special.ErfApprox": { + "checks": { + "structure": { + "status": "pass", + "detail": "defines FUNCTION ErfApprox" + }, + "pseudo": { + "status": "pass", + "detail": "5 case(s) run" + }, + "prover": { + "status": "open", + "detail": "0 of 1 obligation(s) proved; open: divide@12 (1.0 + 0.3275911 * A is not 0)" + }, + "fuzz": { + "status": "pass", + "detail": "no failure in 269 generated input(s)" + }, + "mojo": { + "status": "skipped", + "detail": "core's Mojo interpreter declines exp(...) as written here; the compiler's answer is needed" + } + }, + "cases": [ + { + "call": "ErfApprox(0.0)", + "result": "9.999999717180685e-10" + }, + { + "call": "ErfApprox(0.5)", + "result": "0.5205000163047472" + }, + { + "call": "ErfApprox(-1.0)", + "result": "-0.8427006897475899" + }, + { + "call": "ErfApprox(2.0)", + "result": "0.9953221395812188" + }, + { + "call": "ErfApprox(3.5)", + "result": "0.999999255782864" + } + ], + "references": { + "python/plain": { + "cpython": { + "status": "pass", + "detail": "agrees with the pseudo on 5 case(s) (exactly)" + } + }, + "python/libm": { + "cpython": { + "status": "pass", + "detail": "agrees with the pseudo on 5 case(s) (within rel 0, abs 1.5e-7)" + } + }, + "mojo/plain": { + "interpreter": { + "status": "skipped", + "detail": "core's Mojo interpreter declines exp(...) as written here" + }, + "godbolt": { + "status": "unverified", + "detail": "no stored answer from mojo_1_1_0 for this program: pixi run library-verify-mojo" + } + } + } + }, + "stats.Mean": { + "checks": { + "structure": { + "status": "pass", + "detail": "defines FUNCTION Mean" + }, + "pseudo": { + "status": "pass", + "detail": "5 case(s) run" + }, + "prover": { + "status": "pass", + "detail": "all 2 obligation(s) proved" + }, + "fuzz": { + "status": "pass", + "detail": "no failure in 262 generated input(s)" + }, + "mojo": { + "status": "pass", + "detail": "the generated Mojo prints what the pseudo prints on every case, in core's interpreter" + } + }, + "cases": [ + { + "call": "Mean([1.0, 2.0, 3.0, 4.0])", + "result": "2.5" + }, + { + "call": "Mean([0.1, 0.1, 0.1])", + "result": "0.10000000000000002" + }, + { + "call": "Mean([-5.0, 5.0])", + "result": "0.0" + }, + { + "call": "Mean([1.0e300, 1.0e300])", + "result": "1.0e300" + }, + { + "call": "Mean([2.5])", + "result": "2.5" + } + ], + "references": { + "python/plain": { + "cpython": { + "status": "pass", + "detail": "agrees with the pseudo on 5 case(s) (exactly)" + } + }, + "python/fsum": { + "cpython": { + "status": "pass", + "detail": "agrees with the pseudo on 5 case(s) (within rel 1e-15, abs 0)" + } + }, + "c/plain": { + "cc -O0": { + "status": "pass", + "detail": "agrees with the pseudo on 5 case(s) (exactly)" + }, + "cc -O2": { + "status": "pass", + "detail": "agrees with the pseudo on 5 case(s) (exactly)" + } + }, + "mojo/plain": { + "interpreter": { + "status": "pass", + "detail": "agrees with the pseudo on 5 case(s) in core's interpreter (exactly)" + }, + "godbolt": { + "status": "unverified", + "detail": "no stored answer from mojo_1_1_0 for this program: pixi run library-verify-mojo" + } + }, + "mojo/unrolled": { + "interpreter": { + "status": "pass", + "detail": "agrees with the pseudo on 5 case(s) in core's interpreter (within rel 1e-15, abs 0)" + }, + "godbolt": { + "status": "unverified", + "detail": "no stored answer from mojo_1_1_0 for this program: pixi run library-verify-mojo" + } + } + }, + "proof": { + "status": "pass", + "detail": "Dafny 4.11.0: 9 verified, 0 errors" + } + }, + "stats.Variance": { + "checks": { + "structure": { + "status": "pass", + "detail": "defines FUNCTION Variance, using stats.Mean" + }, + "pseudo": { + "status": "pass", + "detail": "3 case(s) run" + }, + "prover": { + "status": "pass", + "detail": "all 4 obligation(s) proved" + }, + "fuzz": { + "status": "pass", + "detail": "no failure in 221 generated input(s)" + }, + "mojo": { + "status": "pass", + "detail": "the generated Mojo prints what the pseudo prints on every case, in core's interpreter" + } + }, + "cases": [ + { + "call": "Variance([2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0])", + "result": "4.571428571428571" + }, + { + "call": "Variance([1.0, 2.0])", + "result": "0.5" + }, + { + "call": "Variance([1.0e9 + 4.0, 1.0e9 + 7.0, 1.0e9 + 13.0, 1.0e9 + 16.0])", + "result": "30.0" + } + ], + "references": { + "python/plain": { + "cpython": { + "status": "pass", + "detail": "agrees with the pseudo on 3 case(s) (exactly)" + } + }, + "python/statistics": { + "cpython": { + "status": "pass", + "detail": "agrees with the pseudo on 3 case(s) (within rel 1e-12, abs 0)" + } + }, + "mojo/plain": { + "interpreter": { + "status": "pass", + "detail": "agrees with the pseudo on 3 case(s) in core's interpreter (exactly)" + }, + "godbolt": { + "status": "unverified", + "detail": "no stored answer from mojo_1_1_0 for this program: pixi run library-verify-mojo" + } + } + } + } + } +}