lemmascript 0.0.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +94 -3
- package/package.json +30 -20
- package/tools/dist/dafny-commands.js +98 -0
- package/tools/dist/dafny-emit.js +738 -0
- package/tools/dist/emit.js +253 -0
- package/tools/dist/extract.js +806 -0
- package/tools/dist/ir.js +7 -0
- package/tools/dist/lean-commands.js +35 -0
- package/tools/dist/lean-emit.js +393 -0
- package/tools/dist/lsc.js +119 -0
- package/tools/dist/rawir.js +10 -0
- package/tools/dist/resolve.js +717 -0
- package/tools/dist/specparser.js +305 -0
- package/tools/dist/transform.js +1091 -0
- package/tools/dist/typedir.js +7 -0
- package/tools/dist/types.js +71 -0
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -4
- package/src/index.ts +0 -1
- package/tsconfig.json +0 -14
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 midspiral
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,5 +1,96 @@
|
|
|
1
|
-
# LemmaScript
|
|
1
|
+
# LemmaScript (Tech Preview)
|
|
2
2
|
|
|
3
|
-
A verification toolchain for TypeScript.
|
|
3
|
+
A verification toolchain for TypeScript. Write ordinary TypeScript with `//@ ` specification annotations. The toolchain generates verifiable code from your TypeScript — either in Dafny or Lean 4 (with Velvet/Loom).
|
|
4
|
+
|
|
5
|
+
See [SPEC.md](SPEC.md) and [DESIGN.md](DESIGN.md).
|
|
6
|
+
|
|
7
|
+
This is a **Tech Preview**: the core idea is there, but support, semantics, and ergonomics are still evolving.
|
|
8
|
+
|
|
9
|
+
## Examples and Case Studies
|
|
10
|
+
|
|
11
|
+
Each example and case study is verified in Lean 4 and/or Dafny from the same annotated TypeScript source.
|
|
12
|
+
|
|
13
|
+
See the internal [examples](examples).
|
|
14
|
+
|
|
15
|
+
See the external case studies:
|
|
16
|
+
- **[colorwheel-lemmascript](https://github.com/midspiral/colorwheel-lemmascript/)** — verified color palette generator with mood + harmony constraints. 31 Lean proofs + 18 behavioral properties, 115 Dafny lemmas (invariant preservation, commutativity, NoOp completeness).
|
|
17
|
+
- **[clear-split-lemmascript](https://github.com/midspiral/clear-split-lemmascript/)** — greenfield verified expense splitting web app. Conservation theorem, invariant preservation, delta laws — all proven in both Lean (no sorry) and Dafny (56 lemmas).
|
|
18
|
+
- **[node-casbin-lemmascript](https://github.com/midspiral/node-casbin-lemmascript/blob/lemmascript/README_LemmaScript.md)** — brownfield verification of [node-casbin](https://github.com/casbin/node-casbin). 5 functions verified, 217 existing tests pass. End-to-end correctness and order independence for all 4 effect modes in both Lean and Dafny (39 lemmas).
|
|
19
|
+
- **[hono-lemmascript](https://github.com/midspiral/hono-lemmascript/blob/lemmascript/README_LemmaScript.md)** — brownfield verification of [hono](https://github.com/honojs/hono)'s security middleware. Two CVEs verified: IP restriction bypass ([CVE-2026-39409](https://github.com/honojs/hono/security/advisories/GHSA-3mpf-rcc7-5347)) and cookie name bypass ([CVE-2026-39410](https://github.com/honojs/hono/security/advisories/GHSA-r5rp-j6wh-rvv4)) — 51 Dafny lemmas. Cookie verification done in-place. Dafny only.
|
|
20
|
+
- **[charmchat](https://github.com/CHARM-BDF/charmchat/tree/lemma)** — in-progress verification of some sub-components, in Dafny.
|
|
21
|
+
|
|
22
|
+
## Setup
|
|
23
|
+
|
|
24
|
+
**Prerequisites:** Node.js >= 18. For the Lean backend: [elan](https://github.com/leanprover/elan). For the Dafny backend: [Dafny](https://github.com/dafny-lang/dafny) >= 4.x.
|
|
25
|
+
|
|
26
|
+
**Install from npm:**
|
|
27
|
+
|
|
28
|
+
```sh
|
|
29
|
+
npm install lemmascript
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
**Or from source:**
|
|
33
|
+
|
|
34
|
+
```sh
|
|
35
|
+
git clone https://github.com/midspiral/LemmaScript.git
|
|
36
|
+
cd LemmaScript && npm install && npm run build
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
**Lean backend** additionally requires the Loom and Velvet forks:
|
|
40
|
+
|
|
41
|
+
```sh
|
|
42
|
+
git clone https://github.com/namin/loom.git -b lemma ../loom
|
|
43
|
+
git clone https://github.com/namin/velvet.git -b lemma ../velvet
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Usage
|
|
47
|
+
|
|
48
|
+
### Dafny backend
|
|
49
|
+
|
|
50
|
+
```sh
|
|
51
|
+
npx lsc gen --backend=dafny src/myModule.ts
|
|
52
|
+
npx lsc check --backend=dafny src/myModule.ts
|
|
53
|
+
npx lsc regen --backend=dafny src/myModule.ts
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
The Dafny backend generates two files per TS source: `foo.dfy.gen` (always regeneratable) and `foo.dfy` (source of truth, with LLM/user proof additions). The diff between them must be additions-only.
|
|
57
|
+
|
|
58
|
+
### Lean backend
|
|
59
|
+
|
|
60
|
+
```sh
|
|
61
|
+
npx lsc gen src/myModule.ts
|
|
62
|
+
lake build
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## What's Supported
|
|
66
|
+
|
|
67
|
+
### Annotations
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
//@ requires arr.length > 0
|
|
71
|
+
//@ ensures \result >= -1 && \result < arr.length
|
|
72
|
+
//@ invariant 0 <= i && i <= arr.length
|
|
73
|
+
//@ decreases arr.length - i
|
|
74
|
+
//@ type i nat
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## File Structure
|
|
78
|
+
|
|
79
|
+
### Dafny backend
|
|
80
|
+
|
|
81
|
+
| File | Generated? | Purpose |
|
|
82
|
+
|------|-----------|---------|
|
|
83
|
+
| `foo.ts` | — | TypeScript source with `//@ ` annotations |
|
|
84
|
+
| `foo.dfy.gen` | Yes | Generated Dafny (merge base, always regeneratable) |
|
|
85
|
+
| `foo.dfy` | Yes (initial) | Annotated Dafny (gen + proof additions) |
|
|
86
|
+
|
|
87
|
+
### Lean backend
|
|
88
|
+
|
|
89
|
+
| File | Generated? | Purpose |
|
|
90
|
+
|------|-----------|---------|
|
|
91
|
+
| `foo.ts` | — | TypeScript source with `//@ ` annotations |
|
|
92
|
+
| `foo.types.lean` | Yes | Lean types, `namespace Pure` defs |
|
|
93
|
+
| `foo.spec.lean` | No | Ghost definitions, helper lemmas |
|
|
94
|
+
| `foo.def.lean` | Yes | Velvet method definitions |
|
|
95
|
+
| `foo.proof.lean` | No | `prove_correct` with proof tactics |
|
|
4
96
|
|
|
5
|
-
Coming soon from [Midspiral](https://github.com/midspiral).
|
package/package.json
CHANGED
|
@@ -1,27 +1,37 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lemmascript",
|
|
3
|
-
"version": "0.0
|
|
4
|
-
"description": "A verification toolchain for TypeScript",
|
|
5
|
-
"
|
|
6
|
-
"
|
|
7
|
-
|
|
8
|
-
"build": "tsc",
|
|
9
|
-
"dev": "tsc --watch",
|
|
10
|
-
"prepublishOnly": "npm run build"
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "A verification toolchain for TypeScript — generates Lean 4 or Dafny from annotated TS",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"lsc": "tools/dist/lsc.js"
|
|
11
8
|
},
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
"lean",
|
|
15
|
-
"verification",
|
|
16
|
-
"formal-verification",
|
|
17
|
-
"proof",
|
|
18
|
-
"toolchain"
|
|
9
|
+
"files": [
|
|
10
|
+
"tools/dist"
|
|
19
11
|
],
|
|
20
|
-
"
|
|
21
|
-
|
|
22
|
-
"
|
|
12
|
+
"scripts": {
|
|
13
|
+
"build": "tsc -p tools/tsconfig.json",
|
|
14
|
+
"prepublishOnly": "npm run build",
|
|
15
|
+
"typecheck": "tsc -p tools/tsconfig.json --noEmit"
|
|
16
|
+
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"ts-morph": "^25.0.0"
|
|
23
19
|
},
|
|
24
20
|
"devDependencies": {
|
|
25
|
-
"
|
|
26
|
-
|
|
21
|
+
"@types/node": "^25.5.0",
|
|
22
|
+
"tsx": "^4.0.0",
|
|
23
|
+
"typescript": "^5.7.0"
|
|
24
|
+
},
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "https://github.com/midspiral/LemmaScript"
|
|
29
|
+
},
|
|
30
|
+
"keywords": [
|
|
31
|
+
"verification",
|
|
32
|
+
"typescript",
|
|
33
|
+
"lean4",
|
|
34
|
+
"dafny",
|
|
35
|
+
"formal-methods"
|
|
36
|
+
]
|
|
27
37
|
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dafny backend commands: gen, check, regen.
|
|
3
|
+
*/
|
|
4
|
+
import { existsSync, readFileSync, writeFileSync, copyFileSync, unlinkSync } from "fs";
|
|
5
|
+
import { execSync } from "child_process";
|
|
6
|
+
import path from "path";
|
|
7
|
+
function writeGen(genPath, text) {
|
|
8
|
+
writeFileSync(genPath, text);
|
|
9
|
+
console.log(`Generated: ${genPath}`);
|
|
10
|
+
}
|
|
11
|
+
export function dafnyGen(genPath, dfyPath, text) {
|
|
12
|
+
writeGen(genPath, text);
|
|
13
|
+
if (!existsSync(dfyPath)) {
|
|
14
|
+
writeFileSync(dfyPath, text);
|
|
15
|
+
console.log(`Created: ${dfyPath}`);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
export function dafnyCheckDiff(genPath, dfyPath) {
|
|
19
|
+
if (!existsSync(dfyPath))
|
|
20
|
+
return true;
|
|
21
|
+
try {
|
|
22
|
+
const diff = execSync(`git diff --no-index -- "${genPath}" "${dfyPath}" 2>/dev/null || true`, { encoding: "utf-8" });
|
|
23
|
+
const deletions = diff.split("\n").filter(l => l.startsWith("-") && !l.startsWith("---"));
|
|
24
|
+
if (deletions.length > 0) {
|
|
25
|
+
console.error(`WARNING: ${path.basename(dfyPath)} has modifications to generated lines (not additions-only):`);
|
|
26
|
+
for (const d of deletions.slice(0, 5))
|
|
27
|
+
console.error(" " + d);
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
catch { /* diff not available */ }
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
export function dafnyVerify(dfyPath, dir, timeLimit) {
|
|
35
|
+
console.log("Running dafny verify...");
|
|
36
|
+
try {
|
|
37
|
+
const content = readFileSync(dfyPath, "utf-8");
|
|
38
|
+
const stdLibFlag = content.includes("import Std.") ? " --standard-libraries" : "";
|
|
39
|
+
const timeLimitFlag = timeLimit ? ` --verification-time-limit ${timeLimit}` : "";
|
|
40
|
+
execSync(`dafny verify${stdLibFlag}${timeLimitFlag} "${dfyPath}"`, { cwd: dir, stdio: "inherit" });
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
export function dafnyRegen(genPath, dfyPath, basePath, text, dir) {
|
|
48
|
+
// 1. Read old gen before overwriting (needed for base seeding)
|
|
49
|
+
const oldGen = existsSync(genPath) ? readFileSync(genPath, "utf-8") : "";
|
|
50
|
+
// 2. Always write new gen so user can inspect latest output
|
|
51
|
+
writeGen(genPath, text);
|
|
52
|
+
// 3. No .dfy yet — create dfy, verify, done
|
|
53
|
+
if (!existsSync(dfyPath)) {
|
|
54
|
+
writeFileSync(dfyPath, text);
|
|
55
|
+
console.log(`Created: ${path.basename(dfyPath)}`);
|
|
56
|
+
if (!dafnyVerify(dfyPath, dir)) {
|
|
57
|
+
console.error(`FAILED: ${path.basename(dfyPath)} verification failed on first run.`);
|
|
58
|
+
process.exit(1);
|
|
59
|
+
}
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
// 4. Determine anchor: base file if it exists (dirty state), otherwise old gen
|
|
63
|
+
const anchor = existsSync(basePath) ? readFileSync(basePath, "utf-8") : oldGen;
|
|
64
|
+
// 5. If gen changed, three-way merge
|
|
65
|
+
if (text !== anchor) {
|
|
66
|
+
const savedDfy = readFileSync(dfyPath, "utf-8");
|
|
67
|
+
if (!existsSync(basePath))
|
|
68
|
+
writeFileSync(basePath, anchor);
|
|
69
|
+
const mergedPath = dfyPath + ".merged";
|
|
70
|
+
console.log("Gen changed. Three-way merging...");
|
|
71
|
+
try {
|
|
72
|
+
execSync(`git merge-file "${dfyPath}" "${basePath}" "${genPath}"`, { stdio: "pipe" });
|
|
73
|
+
console.log(`Merged: ${path.basename(dfyPath)}`);
|
|
74
|
+
}
|
|
75
|
+
catch (e) {
|
|
76
|
+
if (e.status > 0) {
|
|
77
|
+
copyFileSync(dfyPath, mergedPath);
|
|
78
|
+
writeFileSync(dfyPath, savedDfy);
|
|
79
|
+
console.error(`CONFLICT: ${path.basename(dfyPath)} — merge had conflicts, dfy restored. See ${path.basename(mergedPath)}`);
|
|
80
|
+
process.exit(1);
|
|
81
|
+
}
|
|
82
|
+
throw e;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
// 6. Check gen invariant (unconditional)
|
|
86
|
+
if (!dafnyCheckDiff(genPath, dfyPath)) {
|
|
87
|
+
console.error(`FAILED: ${path.basename(dfyPath)} has modifications to generated lines.`);
|
|
88
|
+
process.exit(1);
|
|
89
|
+
}
|
|
90
|
+
// 7. Verify
|
|
91
|
+
if (!dafnyVerify(dfyPath, dir)) {
|
|
92
|
+
console.error(`FAILED: ${path.basename(dfyPath)} verification failed.`);
|
|
93
|
+
process.exit(1);
|
|
94
|
+
}
|
|
95
|
+
// 8. Success — delete base (gen is now the anchor)
|
|
96
|
+
if (existsSync(basePath))
|
|
97
|
+
unlinkSync(basePath);
|
|
98
|
+
}
|