@under18dev/veritas-solver-lean 0.0.0-stage → 0.1.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 +202 -0
- package/README.md +12 -2
- package/dist/check.d.ts +60 -0
- package/dist/check.d.ts.map +1 -0
- package/dist/check.js +369 -0
- package/dist/check.js.map +1 -0
- package/dist/detect.d.ts +20 -0
- package/dist/detect.d.ts.map +1 -0
- package/dist/detect.js +70 -0
- package/dist/detect.js.map +1 -0
- package/dist/emit.d.ts +26 -0
- package/dist/emit.d.ts.map +1 -0
- package/dist/emit.js +236 -0
- package/dist/emit.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/prove.d.ts +43 -0
- package/dist/prove.d.ts.map +1 -0
- package/dist/prove.js +189 -0
- package/dist/prove.js.map +1 -0
- package/package.json +27 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @under18dev/veritas-solver-lean
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Lean 4 translation and kernel checking for Veritas properties
|
|
4
|
+
|
|
5
|
+
Part of [Veritas](https://github.com/under18dev/veritas), specification-aware test verification for TypeScript/JavaScript projects. Most users install the main package, which provides the `veritas` and `veritas-mcp` commands and the Node API:
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install -D @under18dev/veritas
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
This package is an internal building block of that package; its API may change between minor versions. Source: [`packages/solver-lean`](https://github.com/under18dev/veritas/tree/main/packages/solver-lean).
|
|
12
|
+
|
|
13
|
+
Licensed under Apache-2.0.
|
package/dist/check.d.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { type LeanInstallation, type LeanToolOptions } from './detect.js';
|
|
3
|
+
export declare const LeanCheckStatus: z.ZodEnum<{
|
|
4
|
+
kernel_checked: "kernel_checked";
|
|
5
|
+
rejected: "rejected";
|
|
6
|
+
failed: "failed";
|
|
7
|
+
timeout: "timeout";
|
|
8
|
+
unavailable: "unavailable";
|
|
9
|
+
error: "error";
|
|
10
|
+
}>;
|
|
11
|
+
export type LeanCheckStatus = z.infer<typeof LeanCheckStatus>;
|
|
12
|
+
/** Result of building a Lean project and auditing the axioms of the requested theorems. */
|
|
13
|
+
export declare const LeanCheck: z.ZodObject<{
|
|
14
|
+
status: z.ZodEnum<{
|
|
15
|
+
kernel_checked: "kernel_checked";
|
|
16
|
+
rejected: "rejected";
|
|
17
|
+
failed: "failed";
|
|
18
|
+
timeout: "timeout";
|
|
19
|
+
unavailable: "unavailable";
|
|
20
|
+
error: "error";
|
|
21
|
+
}>;
|
|
22
|
+
axioms: z.ZodRecord<z.ZodString, z.ZodArray<z.ZodString>>;
|
|
23
|
+
reasons: z.ZodArray<z.ZodString>;
|
|
24
|
+
stdout: z.ZodString;
|
|
25
|
+
stderr: z.ZodString;
|
|
26
|
+
durationMs: z.ZodNumber;
|
|
27
|
+
leanVersion: z.ZodNullable<z.ZodString>;
|
|
28
|
+
}, z.core.$strip>;
|
|
29
|
+
export type LeanCheck = z.infer<typeof LeanCheck>;
|
|
30
|
+
/** Lean's standard axioms. Anything else (sorryAx, native_decide's Lean.ofReduceBool, ad-hoc axioms) is rejected. */
|
|
31
|
+
export declare const ALLOWED_AXIOMS: readonly ["propext", "Classical.choice", "Quot.sound"];
|
|
32
|
+
/**
|
|
33
|
+
* Source tokens that are never accepted. Besides proof holes, extra axioms and trusted-code escapes, this
|
|
34
|
+
* includes meta-programming and import-time code, which could otherwise rewrite how the audit itself reports.
|
|
35
|
+
*/
|
|
36
|
+
export declare const FORBIDDEN_TOKENS: readonly ["sorry", "admit", "axiom", "native_decide", "unsafe", "implemented_by", "extern", "opaque", "initialize", "builtin_initialize", "macro", "macro_rules", "elab", "elab_rules", "syntax", "run_cmd", "run_elab", "run_meta", "#eval", "#exit", "#reduce"];
|
|
37
|
+
export interface CheckLeanProjectOptions extends LeanToolOptions {
|
|
38
|
+
theorems: string[];
|
|
39
|
+
timeoutMs: number;
|
|
40
|
+
signal?: AbortSignal;
|
|
41
|
+
/** A previous `detectLean` result, to avoid probing the toolchain again. */
|
|
42
|
+
installation?: LeanInstallation;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Lean source with comments (`--`, nested `/- -/`), string literals (`"..."`, raw `r#"..."#`) and character
|
|
46
|
+
* literals replaced by spaces, keeping every newline so offsets map to the same lines. When unsure it keeps the
|
|
47
|
+
* text: that can only make the check stricter, never hide code from it.
|
|
48
|
+
*/
|
|
49
|
+
export declare function stripLean(source: string): string;
|
|
50
|
+
/** Parse `#print axioms` output; repeated reports for one theorem are merged, never overwritten. */
|
|
51
|
+
export declare function parseAxioms(output: string): Record<string, string[]>;
|
|
52
|
+
/**
|
|
53
|
+
* Build a Lean project with `lake build` and audit `#print axioms` for each theorem.
|
|
54
|
+
* `kernel_checked` only if the sources pass the token scan, the build succeeds and every theorem's
|
|
55
|
+
* axioms are a subset of {propext, Classical.choice, Quot.sound}.
|
|
56
|
+
* When the scan rejects the sources, Lean still runs (if available) so the axioms are reported,
|
|
57
|
+
* but the result stays `rejected` whatever Lean says.
|
|
58
|
+
*/
|
|
59
|
+
export declare function checkLeanProject(dir: string, options: CheckLeanProjectOptions): Promise<LeanCheck>;
|
|
60
|
+
//# sourceMappingURL=check.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"check.d.ts","sourceRoot":"","sources":["../src/check.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAc,KAAK,gBAAgB,EAAE,KAAK,eAAe,EAAE,MAAM,aAAa,CAAC;AAEtF,eAAO,MAAM,eAAe;;;;;;;EAO1B,CAAC;AACH,MAAM,MAAM,eAAe,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,eAAe,CAAC,CAAC;AAE9D,2FAA2F;AAC3F,eAAO,MAAM,SAAS;;;;;;;;;;;;;;;iBAUpB,CAAC;AACH,MAAM,MAAM,SAAS,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,SAAS,CAAC,CAAC;AAElD,qHAAqH;AACrH,eAAO,MAAM,cAAc,wDAAyD,CAAC;AAErF;;;GAGG;AACH,eAAO,MAAM,gBAAgB,mQAsBnB,CAAC;AAoBX,MAAM,WAAW,uBAAwB,SAAQ,eAAe;IAC9D,QAAQ,EAAE,MAAM,EAAE,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,4EAA4E;IAC5E,YAAY,CAAC,EAAE,gBAAgB,CAAC;CACjC;AAuED;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAgEhD;AAUD,oGAAoG;AACpG,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,CAsBpE;AAWD;;;;;;GAMG;AACH,wBAAsB,gBAAgB,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,uBAAuB,GAAG,OAAO,CAAC,SAAS,CAAC,CA6KxG"}
|
package/dist/check.js
ADDED
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
import { randomBytes } from 'node:crypto';
|
|
2
|
+
import { lstatSync, readdirSync, readFileSync } from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { runCommand } from '@under18dev/veritas-core';
|
|
5
|
+
import { z } from 'zod';
|
|
6
|
+
import { detectLean } from './detect.js';
|
|
7
|
+
export const LeanCheckStatus = z.enum([
|
|
8
|
+
'kernel_checked',
|
|
9
|
+
'rejected',
|
|
10
|
+
'failed',
|
|
11
|
+
'timeout',
|
|
12
|
+
'unavailable',
|
|
13
|
+
'error',
|
|
14
|
+
]);
|
|
15
|
+
/** Result of building a Lean project and auditing the axioms of the requested theorems. */
|
|
16
|
+
export const LeanCheck = z.object({
|
|
17
|
+
status: LeanCheckStatus,
|
|
18
|
+
/** Axioms each requested theorem depends on, as reported by `#print axioms` (empty if never reached). */
|
|
19
|
+
axioms: z.record(z.string(), z.array(z.string())),
|
|
20
|
+
reasons: z.array(z.string()),
|
|
21
|
+
/** Redacted and capped. */
|
|
22
|
+
stdout: z.string(),
|
|
23
|
+
stderr: z.string(),
|
|
24
|
+
durationMs: z.number().nonnegative(),
|
|
25
|
+
leanVersion: z.string().nullable(),
|
|
26
|
+
});
|
|
27
|
+
/** Lean's standard axioms. Anything else (sorryAx, native_decide's Lean.ofReduceBool, ad-hoc axioms) is rejected. */
|
|
28
|
+
export const ALLOWED_AXIOMS = ['propext', 'Classical.choice', 'Quot.sound'];
|
|
29
|
+
/**
|
|
30
|
+
* Source tokens that are never accepted. Besides proof holes, extra axioms and trusted-code escapes, this
|
|
31
|
+
* includes meta-programming and import-time code, which could otherwise rewrite how the audit itself reports.
|
|
32
|
+
*/
|
|
33
|
+
export const FORBIDDEN_TOKENS = [
|
|
34
|
+
'sorry',
|
|
35
|
+
'admit',
|
|
36
|
+
'axiom',
|
|
37
|
+
'native_decide',
|
|
38
|
+
'unsafe',
|
|
39
|
+
'implemented_by',
|
|
40
|
+
'extern',
|
|
41
|
+
'opaque',
|
|
42
|
+
'initialize',
|
|
43
|
+
'builtin_initialize',
|
|
44
|
+
'macro',
|
|
45
|
+
'macro_rules',
|
|
46
|
+
'elab',
|
|
47
|
+
'elab_rules',
|
|
48
|
+
'syntax',
|
|
49
|
+
'run_cmd',
|
|
50
|
+
'run_elab',
|
|
51
|
+
'run_meta',
|
|
52
|
+
'#eval',
|
|
53
|
+
'#exit',
|
|
54
|
+
'#reduce',
|
|
55
|
+
];
|
|
56
|
+
/** `set_option` keys a proof may use; any other option (debug, compiler, kernel switches) is refused. */
|
|
57
|
+
const ALLOWED_OPTIONS = new Set(['maxHeartbeats', 'maxRecDepth', 'linter.unusedVariables', 'autoImplicit']);
|
|
58
|
+
/** Library roots a checked project may import besides its own modules. */
|
|
59
|
+
const ALLOWED_IMPORT_ROOTS = new Set(['Init', 'Std']);
|
|
60
|
+
const FORBIDDEN = new RegExp(`(?<![A-Za-z0-9_'.])(${FORBIDDEN_TOKENS.map((t) => t.replace('.', '\\.')).join('|')})(?![A-Za-z0-9_'])`, 'g');
|
|
61
|
+
/** Settings that skip the kernel or pass arbitrary options to Lean, in sources or in the Lake configuration. */
|
|
62
|
+
const KERNEL_BYPASS = /skipKernelTC|\b(moreLeanArgs|weakLeanArgs|leanOptions|moreServerOptions)\b/g;
|
|
63
|
+
const THEOREM = /^[A-Za-z_][A-Za-z0-9_]*(\.[A-Za-z_][A-Za-z0-9_]*)*$/;
|
|
64
|
+
const MODULE_SEGMENT = /^[\p{L}_][\p{L}\p{N}_]*$/u;
|
|
65
|
+
const OUTPUT_CAP = 64 * 1024;
|
|
66
|
+
const SKIP_DIRS = new Set(['.lake', '.git', 'node_modules']);
|
|
67
|
+
function scanSources(dir) {
|
|
68
|
+
const modules = [];
|
|
69
|
+
const findings = [];
|
|
70
|
+
const leanTexts = [];
|
|
71
|
+
const walk = (rel) => {
|
|
72
|
+
for (const entry of readdirSync(path.join(dir, rel), { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
|
|
73
|
+
const relPath = rel ? `${rel}/${entry.name}` : entry.name;
|
|
74
|
+
// Checked before skipping tool directories: lake writes into `.lake`, so a link there leaves the project.
|
|
75
|
+
if (lstatSync(path.join(dir, relPath)).isSymbolicLink()) {
|
|
76
|
+
findings.push(`${relPath}: symbolic links are not accepted in a Lean project`);
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
if (SKIP_DIRS.has(entry.name))
|
|
80
|
+
continue;
|
|
81
|
+
if (entry.isDirectory()) {
|
|
82
|
+
walk(relPath);
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
const isLean = entry.name.endsWith('.lean');
|
|
86
|
+
if (!isLean && relPath !== 'lakefile.toml')
|
|
87
|
+
continue;
|
|
88
|
+
const raw = readFileSync(path.join(dir, relPath), 'utf8');
|
|
89
|
+
// Lean sources are judged without comments and string literals, over the whole file, so a command split
|
|
90
|
+
// across lines is seen and a word in a comment is not. The Lake configuration is judged as written.
|
|
91
|
+
const text = isLean ? stripLean(raw) : raw;
|
|
92
|
+
const lineAt = (offset) => text.slice(0, offset).split('\n').length;
|
|
93
|
+
const tokens = [...(isLean ? text.matchAll(FORBIDDEN) : []), ...text.matchAll(KERNEL_BYPASS)];
|
|
94
|
+
for (const match of tokens)
|
|
95
|
+
findings.push(`${relPath}:${lineAt(match.index ?? 0)}: forbidden token \`${match[0]}\``);
|
|
96
|
+
if (!isLean)
|
|
97
|
+
continue;
|
|
98
|
+
if (relPath === 'lakefile.lean')
|
|
99
|
+
continue;
|
|
100
|
+
leanTexts.push({ relPath, text });
|
|
101
|
+
const segments = relPath.slice(0, -'.lean'.length).split('/');
|
|
102
|
+
if (segments.every((s) => MODULE_SEGMENT.test(s)))
|
|
103
|
+
modules.push(segments.join('.'));
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
walk('');
|
|
107
|
+
// Imports: only the project's own modules and the core library; options: only the allowlisted ones.
|
|
108
|
+
for (const { relPath, text } of leanTexts) {
|
|
109
|
+
const lineAt = (offset) => text.slice(0, offset).split('\n').length;
|
|
110
|
+
for (const m of text.matchAll(/(?<![\p{L}\p{N}_'.])import\s+(?:runtime\s+)?(\S+)/gu)) {
|
|
111
|
+
const mod = (m[1] ?? '').replace(/[«»]/g, '');
|
|
112
|
+
if (!modules.includes(mod) && !ALLOWED_IMPORT_ROOTS.has(mod.split('.')[0] ?? ''))
|
|
113
|
+
findings.push(`${relPath}:${lineAt(m.index ?? 0)}: import of \`${mod}\` is not allowed (only the project's modules, Init and Std)`);
|
|
114
|
+
}
|
|
115
|
+
for (const m of text.matchAll(/(?<![\p{L}\p{N}_'.])set_option\s+(\S+)/gu))
|
|
116
|
+
if (!ALLOWED_OPTIONS.has(m[1] ?? ''))
|
|
117
|
+
findings.push(`${relPath}:${lineAt(m.index ?? 0)}: \`set_option ${m[1]}\` is not allowed`);
|
|
118
|
+
}
|
|
119
|
+
return { modules, findings };
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Lean source with comments (`--`, nested `/- -/`), string literals (`"..."`, raw `r#"..."#`) and character
|
|
123
|
+
* literals replaced by spaces, keeping every newline so offsets map to the same lines. When unsure it keeps the
|
|
124
|
+
* text: that can only make the check stricter, never hide code from it.
|
|
125
|
+
*/
|
|
126
|
+
export function stripLean(source) {
|
|
127
|
+
const out = source.split('');
|
|
128
|
+
const blank = (from, to) => {
|
|
129
|
+
for (let k = from; k < to && k < out.length; k++)
|
|
130
|
+
if (out[k] !== '\n')
|
|
131
|
+
out[k] = ' ';
|
|
132
|
+
};
|
|
133
|
+
const identChar = (ch) => ch !== undefined && /[\p{L}\p{N}_'!?]/u.test(ch);
|
|
134
|
+
let i = 0;
|
|
135
|
+
while (i < source.length) {
|
|
136
|
+
const ch = source[i];
|
|
137
|
+
const next = source[i + 1];
|
|
138
|
+
if (ch === '-' && next === '-') {
|
|
139
|
+
const end = source.indexOf('\n', i);
|
|
140
|
+
const stop = end === -1 ? source.length : end;
|
|
141
|
+
blank(i, stop);
|
|
142
|
+
i = stop;
|
|
143
|
+
}
|
|
144
|
+
else if (ch === '/' && next === '-') {
|
|
145
|
+
let depth = 0;
|
|
146
|
+
let j = i;
|
|
147
|
+
while (j < source.length) {
|
|
148
|
+
if (source[j] === '/' && source[j + 1] === '-') {
|
|
149
|
+
depth++;
|
|
150
|
+
j += 2;
|
|
151
|
+
}
|
|
152
|
+
else if (source[j] === '-' && source[j + 1] === '/') {
|
|
153
|
+
depth--;
|
|
154
|
+
j += 2;
|
|
155
|
+
if (depth === 0)
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
else
|
|
159
|
+
j++;
|
|
160
|
+
}
|
|
161
|
+
blank(i, j);
|
|
162
|
+
i = j;
|
|
163
|
+
}
|
|
164
|
+
else if (ch === 'r' && !identChar(source[i - 1]) && /^r#*"/.test(source.slice(i, i + 260))) {
|
|
165
|
+
const hashes = (/^r(#*)"/.exec(source.slice(i, i + 260))?.[1] ?? '').length;
|
|
166
|
+
const close = `"${'#'.repeat(hashes)}`;
|
|
167
|
+
const end = source.indexOf(close, i + 2 + hashes);
|
|
168
|
+
if (end === -1)
|
|
169
|
+
break; // unterminated: keep the rest as code
|
|
170
|
+
blank(i, end + close.length);
|
|
171
|
+
i = end + close.length;
|
|
172
|
+
}
|
|
173
|
+
else if (ch === '"') {
|
|
174
|
+
let j = i + 1;
|
|
175
|
+
while (j < source.length && source[j] !== '"')
|
|
176
|
+
j += source[j] === '\\' ? 2 : 1;
|
|
177
|
+
if (j >= source.length)
|
|
178
|
+
break; // unterminated: keep the rest as code
|
|
179
|
+
blank(i, j + 1);
|
|
180
|
+
i = j + 1;
|
|
181
|
+
}
|
|
182
|
+
else if (ch === "'" && !identChar(source[i - 1])) {
|
|
183
|
+
// A character literal such as '"' or '\n'; a prime inside an identifier (x') is not one.
|
|
184
|
+
const literal = /^'(?:\\(?:x[0-9a-fA-F]{2}|u[0-9a-fA-F]{4}|.)|[^\\'\n])'/u.exec(source.slice(i, i + 12));
|
|
185
|
+
if (literal) {
|
|
186
|
+
blank(i, i + literal[0].length);
|
|
187
|
+
i += literal[0].length;
|
|
188
|
+
}
|
|
189
|
+
else
|
|
190
|
+
i++;
|
|
191
|
+
}
|
|
192
|
+
else
|
|
193
|
+
i++;
|
|
194
|
+
}
|
|
195
|
+
return out.join('');
|
|
196
|
+
}
|
|
197
|
+
function cap(text) {
|
|
198
|
+
return text.length <= OUTPUT_CAP
|
|
199
|
+
? text
|
|
200
|
+
: `${text.slice(0, OUTPUT_CAP)}\n[... ${text.length - OUTPUT_CAP} characters truncated]`;
|
|
201
|
+
}
|
|
202
|
+
const stripQuotes = (name) => name.replace(/[«»]/g, '');
|
|
203
|
+
/** Parse `#print axioms` output; repeated reports for one theorem are merged, never overwritten. */
|
|
204
|
+
export function parseAxioms(output) {
|
|
205
|
+
const result = {};
|
|
206
|
+
const add = (name, axioms) => {
|
|
207
|
+
const key = stripQuotes(name);
|
|
208
|
+
result[key] ??= new Set();
|
|
209
|
+
for (const a of axioms)
|
|
210
|
+
result[key].add(stripQuotes(a));
|
|
211
|
+
};
|
|
212
|
+
for (const m of output.matchAll(/'([^'\n]+)' depends on axioms: \[([^\]]*)\]/g)) {
|
|
213
|
+
add(m[1], m[2]
|
|
214
|
+
.split(',')
|
|
215
|
+
.map((a) => a.trim())
|
|
216
|
+
.filter(Boolean));
|
|
217
|
+
}
|
|
218
|
+
for (const m of output.matchAll(/'([^'\n]+)' does not depend on any axioms/g))
|
|
219
|
+
add(m[1], []);
|
|
220
|
+
return Object.fromEntries(Object.entries(result).map(([k, v]) => [k, [...v].sort()]));
|
|
221
|
+
}
|
|
222
|
+
function describeAxiom(name) {
|
|
223
|
+
if (name === 'sorryAx')
|
|
224
|
+
return 'sorryAx (the proof contains `sorry`/`admit` or an elaboration error)';
|
|
225
|
+
if (name === 'Lean.ofReduceBool' || name === 'Lean.trustCompiler' || /\._native\.|native_decide/.test(name))
|
|
226
|
+
return `${name} (native_decide / compiler-trusted evaluation)`;
|
|
227
|
+
return `${name} (not one of Lean's standard axioms)`;
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Build a Lean project with `lake build` and audit `#print axioms` for each theorem.
|
|
231
|
+
* `kernel_checked` only if the sources pass the token scan, the build succeeds and every theorem's
|
|
232
|
+
* axioms are a subset of {propext, Classical.choice, Quot.sound}.
|
|
233
|
+
* When the scan rejects the sources, Lean still runs (if available) so the axioms are reported,
|
|
234
|
+
* but the result stays `rejected` whatever Lean says.
|
|
235
|
+
*/
|
|
236
|
+
export async function checkLeanProject(dir, options) {
|
|
237
|
+
const started = Date.now();
|
|
238
|
+
const reasons = [];
|
|
239
|
+
let stdout = '';
|
|
240
|
+
let stderr = '';
|
|
241
|
+
const result = (status, axioms, leanVersion) => LeanCheck.parse({
|
|
242
|
+
status,
|
|
243
|
+
axioms,
|
|
244
|
+
reasons,
|
|
245
|
+
stdout: cap(stdout),
|
|
246
|
+
stderr: cap(stderr),
|
|
247
|
+
durationMs: Date.now() - started,
|
|
248
|
+
leanVersion,
|
|
249
|
+
});
|
|
250
|
+
if (options.theorems.length === 0) {
|
|
251
|
+
reasons.push('no theorems to check');
|
|
252
|
+
return result('error', {}, null);
|
|
253
|
+
}
|
|
254
|
+
const badName = options.theorems.find((t) => !THEOREM.test(t));
|
|
255
|
+
if (badName !== undefined) {
|
|
256
|
+
reasons.push(`invalid theorem name ${JSON.stringify(badName)}`);
|
|
257
|
+
return result('error', {}, null);
|
|
258
|
+
}
|
|
259
|
+
let scan;
|
|
260
|
+
try {
|
|
261
|
+
scan = scanSources(dir);
|
|
262
|
+
}
|
|
263
|
+
catch (error) {
|
|
264
|
+
reasons.push(`cannot read the project: ${error.message}`);
|
|
265
|
+
return result('error', {}, null);
|
|
266
|
+
}
|
|
267
|
+
const rejectedBySource = scan.findings.length > 0;
|
|
268
|
+
reasons.push(...scan.findings);
|
|
269
|
+
if (scan.modules.length === 0) {
|
|
270
|
+
reasons.push('no Lean modules found');
|
|
271
|
+
return result(rejectedBySource ? 'rejected' : 'error', {}, null);
|
|
272
|
+
}
|
|
273
|
+
const lean = options.installation ?? (await detectLean(options));
|
|
274
|
+
if (!lean.available || !lean.lake) {
|
|
275
|
+
reasons.push(`Lean unavailable: ${lean.detail}`);
|
|
276
|
+
return result(rejectedBySource ? 'rejected' : 'unavailable', {}, lean.version);
|
|
277
|
+
}
|
|
278
|
+
const binDir = path.dirname(lean.lean);
|
|
279
|
+
const env = { PATH: [binDir, process.env.PATH ?? ''].join(path.delimiter) };
|
|
280
|
+
const run = (args, timeoutMs, input) => runCommand(lean.lake, args, {
|
|
281
|
+
cwd: dir,
|
|
282
|
+
timeoutMs: Math.max(1, timeoutMs),
|
|
283
|
+
env,
|
|
284
|
+
maxOutputBytes: 2 * 1024 * 1024,
|
|
285
|
+
...(options.signal ? { signal: options.signal } : {}),
|
|
286
|
+
...(input !== undefined ? { input } : {}),
|
|
287
|
+
});
|
|
288
|
+
const failure = (step, r) => {
|
|
289
|
+
if (r.timedOut) {
|
|
290
|
+
reasons.push(`${step} timed out after ${r.durationMs} ms`);
|
|
291
|
+
return 'timeout';
|
|
292
|
+
}
|
|
293
|
+
if (options.signal?.aborted) {
|
|
294
|
+
reasons.push(`${step} was cancelled`);
|
|
295
|
+
return 'error';
|
|
296
|
+
}
|
|
297
|
+
if (r.spawnError) {
|
|
298
|
+
reasons.push(`${step} could not start: ${r.spawnError}`);
|
|
299
|
+
return 'error';
|
|
300
|
+
}
|
|
301
|
+
if (r.exitCode !== 0) {
|
|
302
|
+
reasons.push(`${step} failed with exit code ${r.exitCode ?? `signal ${r.signal}`}`);
|
|
303
|
+
return 'failed';
|
|
304
|
+
}
|
|
305
|
+
return null;
|
|
306
|
+
};
|
|
307
|
+
const final = (status, axioms = {}) => result(rejectedBySource ? 'rejected' : status, axioms, lean.version);
|
|
308
|
+
const build = await run(['build'], options.timeoutMs);
|
|
309
|
+
stdout += `$ lake build\n${build.stdout}`;
|
|
310
|
+
stderr += build.stderr;
|
|
311
|
+
const buildFailure = failure('lake build', build);
|
|
312
|
+
if (buildFailure) {
|
|
313
|
+
const errors = `${build.stdout}\n${build.stderr}`
|
|
314
|
+
.split('\n')
|
|
315
|
+
.filter((l) => /error/i.test(l))
|
|
316
|
+
.slice(0, 20);
|
|
317
|
+
reasons.push(...errors.map((l) => l.trim()));
|
|
318
|
+
return final(buildFailure);
|
|
319
|
+
}
|
|
320
|
+
const remaining = options.timeoutMs - (Date.now() - started);
|
|
321
|
+
if (remaining <= 0) {
|
|
322
|
+
reasons.push('time budget exhausted before the axiom audit');
|
|
323
|
+
return final('timeout');
|
|
324
|
+
}
|
|
325
|
+
const sentinel = `veritas-axiom-audit-${randomBytes(12).toString('hex')}`;
|
|
326
|
+
const audit = [
|
|
327
|
+
...scan.modules.map((m) => `import ${m}`),
|
|
328
|
+
...options.theorems.map((t) => `#print axioms ${t
|
|
329
|
+
.split('.')
|
|
330
|
+
.map((s) => `«${s}»`)
|
|
331
|
+
.join('.')}`),
|
|
332
|
+
`#print "${sentinel}"`,
|
|
333
|
+
'',
|
|
334
|
+
].join('\n');
|
|
335
|
+
const check = await run(['env', 'lean', '--stdin'], remaining, audit);
|
|
336
|
+
stdout += `\n$ lake env lean --stdin (axiom audit)\n${check.stdout}`;
|
|
337
|
+
stderr += check.stderr;
|
|
338
|
+
const checkFailure = failure('axiom audit', check);
|
|
339
|
+
const axioms = parseAxioms(check.stdout);
|
|
340
|
+
const relevant = Object.fromEntries(options.theorems.flatMap((t) => (axioms[t] ? [[t, axioms[t]]] : [])));
|
|
341
|
+
if (checkFailure) {
|
|
342
|
+
reasons.push(...`${check.stdout}\n${check.stderr}`
|
|
343
|
+
.split('\n')
|
|
344
|
+
.filter((l) => /error/i.test(l))
|
|
345
|
+
.slice(0, 20)
|
|
346
|
+
.map((l) => l.trim()));
|
|
347
|
+
return final(checkFailure, relevant);
|
|
348
|
+
}
|
|
349
|
+
if (!check.stdout.includes(sentinel)) {
|
|
350
|
+
reasons.push('the axiom audit did not run to completion');
|
|
351
|
+
return final('failed', relevant);
|
|
352
|
+
}
|
|
353
|
+
let missing = false;
|
|
354
|
+
let disallowed = false;
|
|
355
|
+
for (const theorem of options.theorems) {
|
|
356
|
+
if (!(theorem in axioms)) {
|
|
357
|
+
reasons.push(`no axiom report for ${theorem}`);
|
|
358
|
+
missing = true;
|
|
359
|
+
continue;
|
|
360
|
+
}
|
|
361
|
+
const extra = (axioms[theorem] ?? []).filter((a) => !ALLOWED_AXIOMS.includes(a));
|
|
362
|
+
if (extra.length > 0) {
|
|
363
|
+
reasons.push(`${theorem} depends on ${extra.map(describeAxiom).join(', ')}`);
|
|
364
|
+
disallowed = true;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
return final(disallowed ? 'rejected' : missing ? 'failed' : 'kernel_checked', relevant);
|
|
368
|
+
}
|
|
369
|
+
//# sourceMappingURL=check.js.map
|