visualproof 0.8.1 → 0.9.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/README.md +10 -8
- package/dist/assets/tips/tour-library.webp +0 -0
- package/dist/assets/tips/tour-source.webp +0 -0
- package/dist/assets/tips/tour.json +90 -90
- package/dist/extractor/Extract.lean +34 -11
- package/dist/server/agent.js +19 -7
- package/dist/server/agentexport.js +2 -1
- package/dist/server/agentfollow.js +13 -3
- package/dist/server/agentlean.js +91 -1
- package/dist/server/agentnotes.js +6 -4
- package/dist/server/agentread.js +33 -1
- package/dist/server/app.js +1 -1
- package/dist/server/appgraph.js +24 -0
- package/dist/server/boardspots.js +1 -1
- package/dist/server/goals.js +224 -0
- package/dist/server/hover.js +281 -8
- package/dist/server/live.js +14 -0
- package/dist/server/main.js +3 -2
- package/dist/server/mcptools.js +11 -5
- package/dist/server/options.js +17 -2
- package/dist/server/readonlygate.js +2 -1
- package/dist/server/reviewapi.js +5 -2
- package/dist/server/status.js +43 -5
- package/dist/shared/build.js +9 -0
- package/dist/shared/collab.js +14 -2
- package/dist/shared/declfind.js +23 -3
- package/dist/shared/goals.js +309 -0
- package/dist/shared/hygiene.js +18 -0
- package/dist/shared/noteplace.js +45 -19
- package/dist/shared/scope.js +74 -45
- package/dist/shared/sharesnap.js +4 -2
- package/dist/web/bundle/app.js +2271 -581
- package/dist/web/index.html +1 -1
- package/dist/web/src/highlight.js +53 -3
- package/dist/web/style.css +148 -0
- package/docs/agent-api/README.md +1 -1
- package/docs/agent-api/mcp.md +1 -1
- package/docs/agent-api/notes.md +3 -2
- package/docs/agent-api/reading.md +38 -2
- package/docs/agent-api/words.md +3 -2
- package/package.json +1 -1
package/dist/server/agentlean.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
// What Lean itself says of a name, the endpoints of server/agent.ts that ask Lean's language server
|
|
2
2
|
// (server/hover.ts): its hover (describe) and what `#print` shows of it (print), asked where a module
|
|
3
3
|
// of the project's graph is imported, and only there — the module is chosen from the graph, and only
|
|
4
|
-
// a name that passes isDeclName is ever written into Lean source.
|
|
4
|
+
// a name that passes isDeclName is ever written into Lean source. And the goals at a place of a
|
|
5
|
+
// file (goals), as the InfoView shows them: only the place, numbers, reaches Lean.
|
|
5
6
|
import { isDeclName } from "./hover.js";
|
|
7
|
+
import { excerptOf } from "../shared/excerpt.js";
|
|
8
|
+
import { goalText } from "../shared/goals.js";
|
|
6
9
|
import { View } from "./agentgraph.js";
|
|
7
10
|
import { clampInt, fail, flag, ok } from "./agent.js";
|
|
8
11
|
/** The name Lean is asked about and the module imported to ask it: `module` when given (a module
|
|
@@ -142,3 +145,90 @@ export const MAX_WAIT = 60_000;
|
|
|
142
145
|
/** the most of what `#print` says that an answer carries, in UTF-16 units: a theorem's proof term,
|
|
143
146
|
* printed with `proofs=1`, can run to megabytes */
|
|
144
147
|
export const MAX_PRINT = 50_000;
|
|
148
|
+
/** The goals at a place, as the InfoView shows them (ReviewSpec, "The InfoView"; the owner's decision
|
|
149
|
+
* of 2026-11-01), from Lean's language server: a line of a declaration's text (`name`) or of a
|
|
150
|
+
* module's file (`module`), and a column; each goal as Lean writes it. Only the place (numbers) and
|
|
151
|
+
* the module, one of the graph's, reach Lean. The build's text alone: a file changed since it was
|
|
152
|
+
* compiled has none. */
|
|
153
|
+
export async function goals(api, a) {
|
|
154
|
+
const v = api.graph();
|
|
155
|
+
if (!(v instanceof View))
|
|
156
|
+
return v;
|
|
157
|
+
if (!api.lean?.goals)
|
|
158
|
+
return fail(503, "There is no Lean to ask here.", "GET /api/agent/source gives the declaration's text.");
|
|
159
|
+
const q = a.query;
|
|
160
|
+
const name = q.get("name")?.trim() || null;
|
|
161
|
+
const given = q.get("module")?.trim() || null;
|
|
162
|
+
const line = Number(q.get("line"));
|
|
163
|
+
if (!Number.isInteger(line) || line < 1)
|
|
164
|
+
return fail(400, "line is missing, or not a line.", "Give line=<n>, from 1: of the declaration's text with name, of the file with module.");
|
|
165
|
+
const colRaw = q.get("column");
|
|
166
|
+
const column = colRaw === null || colRaw === "" ? null : Number(colRaw);
|
|
167
|
+
if (column !== null && (!Number.isInteger(column) || column < 1 || column > 100_000))
|
|
168
|
+
return fail(400, "column is not a column.", "Give column=<n>, from 1, or leave it out: where the line's code starts.");
|
|
169
|
+
// the file: the declaration's module's, or the module given
|
|
170
|
+
let module;
|
|
171
|
+
let start = 0;
|
|
172
|
+
let end = null;
|
|
173
|
+
if (name) {
|
|
174
|
+
const i = api.find(v, name);
|
|
175
|
+
if (typeof i !== "number")
|
|
176
|
+
return i;
|
|
177
|
+
const n = v.g.nodes[i];
|
|
178
|
+
if (!n.range)
|
|
179
|
+
return fail(404, `The graph records no place for ${n.name} in its file.`, "GET /api/agent/print gives what Lean elaborated.");
|
|
180
|
+
module = n.module;
|
|
181
|
+
const f = v.g.isExt(i) ? api.outsideText(module) : api.file(v, module);
|
|
182
|
+
if (!f)
|
|
183
|
+
return fail(404, `The file of ${module} cannot be read here.`, "GET /api/agent/declaration gives its statement.");
|
|
184
|
+
const at = excerptOf(f.lines, f.edited, n.name, n.range);
|
|
185
|
+
if (!at)
|
|
186
|
+
return fail(404, `${n.name} is no longer in ${f.rel}.`, "Wait for the build; GET /api/agent/declaration gives the compiled statement.");
|
|
187
|
+
start = at.start;
|
|
188
|
+
end = at.end;
|
|
189
|
+
}
|
|
190
|
+
else if (given) {
|
|
191
|
+
if (!isDeclName(given) || (!v.modules.has(given) && !api.world.outsideFile?.(given)))
|
|
192
|
+
return fail(404, `${given.slice(0, 300)} is not a module of the graph.`, "Give a module of the project's graph, or name=<a declaration> and a line of its text.");
|
|
193
|
+
module = given;
|
|
194
|
+
}
|
|
195
|
+
else
|
|
196
|
+
return fail(400, "name or module is missing.", "Give name=<a declaration> and line=<a line of its text, from 1>; or module=<a module> and line=<a line of its file>.");
|
|
197
|
+
// the file as Lean is given it: the project's, or a package's by its resolved path
|
|
198
|
+
const own = v.modules.get(module);
|
|
199
|
+
const out = own ? null : api.world.outsideFile?.(module) ?? null;
|
|
200
|
+
const f = own ? api.file(v, module) : api.outsideText(module);
|
|
201
|
+
if (!f || (!own && !out))
|
|
202
|
+
return fail(404, `The file of ${module} cannot be read here.`, "GET /api/agent/source gives a declaration's text.");
|
|
203
|
+
if (f.edited)
|
|
204
|
+
return fail(409, out ? `${out.file.package.name} is at another revision than the graph was read at: its goals would be another text's than Lean checked.` : `${f.rel} has changed since it was compiled: its goals would be the present text's, not what Lean checked.`, "Wait for the build; GET /api/agent/source gives the text.");
|
|
205
|
+
const fileLine = start + line - 1;
|
|
206
|
+
if (fileLine >= f.lines.length || (end !== null && fileLine > end))
|
|
207
|
+
return fail(400, `line ${line} is past the ${name ? `text of ${name} (${end - start + 1} lines)` : `file (${f.lines.length} lines)`}.`, name ? "Lines count from 1, the declaration's first line, as source gives its text." : "Lines count from 1.");
|
|
208
|
+
const text = f.lines[fileLine];
|
|
209
|
+
const ch = column !== null ? Math.min(column - 1, text.length) : /^\s*/.exec(text)[0].length;
|
|
210
|
+
const file = out ? { name: `${out.file.package.name}: ${out.file.file}`, path: out.file.path } : f.rel;
|
|
211
|
+
const r = await api.lean.goals(file, f.lines.join("\n"), f.hash, fileLine, ch, clampInt(q.get("wait"), 20_000, 1000, MAX_WAIT));
|
|
212
|
+
if (r.pending)
|
|
213
|
+
return fail(503, `Lean is still elaborating ${f.rel}: line ${r.pending.line.toLocaleString("en")} of ${r.pending.of.toLocaleString("en")}.`, "Ask again in a few seconds (Retry-After); the first question about a file can take a minute.", { elaborated: r.pending, retryAfter: 5 }, { "retry-after": "5" });
|
|
214
|
+
if (r.error)
|
|
215
|
+
return fail(502, `Lean could not answer: ${r.error}`, "GET /api/agent/source gives the text; /api/agent/print what Lean elaborated.");
|
|
216
|
+
// a tactic where Lean has a tactic state there: elsewhere the line is no tactic's
|
|
217
|
+
const t = r.tactic && r.tactic.to > r.tactic.from && (r.before || r.after) ? text.slice(r.tactic.from, r.tactic.to) : null;
|
|
218
|
+
return ok({
|
|
219
|
+
...(name ? { name } : {}),
|
|
220
|
+
module,
|
|
221
|
+
file: f.rel,
|
|
222
|
+
line: fileLine + 1,
|
|
223
|
+
column: ch + 1,
|
|
224
|
+
text,
|
|
225
|
+
tactic: t,
|
|
226
|
+
before: r.before ? r.before.map(goalText) : null,
|
|
227
|
+
after: r.after ? r.after.map(goalText) : null,
|
|
228
|
+
// after a tactic that holds a proof of its own: the goals where the proof goes on, that line's
|
|
229
|
+
// (the owner's decision of 2026-11-01)
|
|
230
|
+
...(r.afterFrom !== undefined && r.after ? { afterFrom: r.afterFrom + 1 } : {}),
|
|
231
|
+
expected: r.term ? goalText({ hyps: r.term.hyps, type: r.term.type }) : null,
|
|
232
|
+
...(r.widgets ? { widgets: r.widgets, note: "A widget is here: its JavaScript is the project's code, and VisualProof never runs it. A person sees it in their editor." } : {}),
|
|
233
|
+
});
|
|
234
|
+
}
|
|
@@ -67,7 +67,9 @@ function spotShown(s, B) {
|
|
|
67
67
|
if (s.at === "world")
|
|
68
68
|
return { spot: [s.x, s.y] };
|
|
69
69
|
const about = !s.about ? null : "links" in s.about ? { links: s.about.links } : { clustering: s.about.mode, cluster: s.about.key };
|
|
70
|
-
|
|
70
|
+
// `gone`: the declaration it is attached to, which this build does not have (the owner's decision
|
|
71
|
+
// of 2026-10-30)
|
|
72
|
+
return { ring: s.angle, about, ...(s.gone ? { gone: s.gone } : {}) };
|
|
71
73
|
}
|
|
72
74
|
/** what `why` says of a note of the project on a board, for an agent */
|
|
73
75
|
const WHY_WORDS = {
|
|
@@ -97,7 +99,7 @@ export function noteEndpoints(world, refuse) {
|
|
|
97
99
|
};
|
|
98
100
|
const graph = () => graphOf(world) ?? fail(503, "The graph has not been read yet.", "The server is reading the project; ask again in a few seconds.", { retryAfter: 5 }, { "retry-after": "5" });
|
|
99
101
|
/** whether a key is a cluster of the graph on show by a clustering, and whether a name is one of
|
|
100
|
-
* its declarations (
|
|
102
|
+
* its declarations (a note attached to one that is gone is on the ring) */
|
|
101
103
|
const keys = new WeakMap();
|
|
102
104
|
const exists = (g) => {
|
|
103
105
|
const f = (m, key) => {
|
|
@@ -179,7 +181,7 @@ export function noteEndpoints(world, refuse) {
|
|
|
179
181
|
}
|
|
180
182
|
const s = n;
|
|
181
183
|
const index = new Map(B?.L.clusters.map((c, k) => [c.key, k]) ?? []);
|
|
182
|
-
return { ...base, view, clustering: s.place.mode, here: B ? spotShown(viewSpot(s, B.nb, (key) => index.get(key) ?? -1), B) : null };
|
|
184
|
+
return { ...base, view, clustering: s.place.mode, here: B ? spotShown(viewSpot(s, B.nb, (key) => index.get(key) ?? -1, exists(graphOf(world).g)), B) : null };
|
|
183
185
|
};
|
|
184
186
|
/** Apply operations to view `v`'s notes, or the project's, as the agent's, through the path a
|
|
185
187
|
* page's take. */
|
|
@@ -336,7 +338,7 @@ export function noteEndpoints(world, refuse) {
|
|
|
336
338
|
* (its spot in its cluster, else its point on the ring). */
|
|
337
339
|
const restOn = (kind, n, B) => {
|
|
338
340
|
const index = new Map(B.L.clusters.map((c, k) => [c.key, k]));
|
|
339
|
-
const s = kind === "project" ? projectHere(n, B)?.spot : viewSpot(n, B.nb, (key) => index.get(key) ?? -1);
|
|
341
|
+
const s = kind === "project" ? projectHere(n, B)?.spot : viewSpot(n, B.nb, (key) => index.get(key) ?? -1, exists(graphOf(world).g));
|
|
340
342
|
if (!s)
|
|
341
343
|
return null;
|
|
342
344
|
// attached to a declaration: at its offset from the point, a place on the board, as the page
|
package/dist/server/agentread.js
CHANGED
|
@@ -18,7 +18,10 @@ import { clampInt, fail, flag, ok } from "./agent.js";
|
|
|
18
18
|
/** A module of the project (AgentSpec, after the trial of decision 7): its file's doc comments
|
|
19
19
|
* (`/-! … -/`, where a project keeps the reasons behind its definitions) and every declaration
|
|
20
20
|
* in it in the order of the file, so that what sits beside a declaration — a variant, a weaker
|
|
21
|
-
* form — can be found without reading the file.
|
|
21
|
+
* form — can be found without reading the file. With `lines`, the file itself, as the source
|
|
22
|
+
* window's File mode shows it (the owner's decision of 2026-11-01): a few hundred lines at a time,
|
|
23
|
+
* a module of the project's, or outside it one an outside declaration of the graph is in (the
|
|
24
|
+
* source window's bounds). */
|
|
22
25
|
export function moduleGet(api, a) {
|
|
23
26
|
const v = api.graph();
|
|
24
27
|
if (!(v instanceof View))
|
|
@@ -33,7 +36,15 @@ export function moduleGet(api, a) {
|
|
|
33
36
|
}
|
|
34
37
|
if (!name)
|
|
35
38
|
return fail(400, "name is missing.", "Give name=<a module of the project> (a declaration's module, as GET declaration says it), or of=<a declaration>.");
|
|
39
|
+
const want = a.query.get("lines");
|
|
40
|
+
const span = want === null ? null : readSpan(want);
|
|
41
|
+
if (want !== null && !span)
|
|
42
|
+
return fail(400, `lines is ${JSON.stringify(want.slice(0, 40))}: give lines=<from>-<to>, from 1, at most ${MAX_FILE_LINES} lines.`, "lines=1-400 gives the file's first 400 lines.");
|
|
36
43
|
const m = v.modules.get(name);
|
|
44
|
+
// outside the project, the file alone, of a module an outside declaration of the graph is in
|
|
45
|
+
const out = !m && span ? api.outsideText(name) : null;
|
|
46
|
+
if (out)
|
|
47
|
+
return ok({ name, file: out.rel, outside: out.outside, compiled: !out.edited, ...fileLines(out.lines, span) });
|
|
37
48
|
if (!m) {
|
|
38
49
|
const last = name.split(".").pop().toLowerCase();
|
|
39
50
|
const near = [...v.modules.keys()].filter((x) => x.toLowerCase().split(".").pop() === last || x.toLowerCase() === name.toLowerCase()).slice(0, 5);
|
|
@@ -81,6 +92,7 @@ export function moduleGet(api, a) {
|
|
|
81
92
|
name,
|
|
82
93
|
file: m.file,
|
|
83
94
|
...(f ? { compiled: !f.edited, ...(f.edited ? { note: "The file changed since it was compiled: its doc comments are read from the present text, its declarations are the compiled ones." } : {}) } : {}),
|
|
95
|
+
...(f && span ? fileLines(f.lines, span) : {}),
|
|
84
96
|
imports: m.imports,
|
|
85
97
|
importedBy: [...v.modules].filter(([, x]) => x.imports.includes(name)).map(([k]) => k).sort(),
|
|
86
98
|
docs,
|
|
@@ -92,6 +104,26 @@ export function moduleGet(api, a) {
|
|
|
92
104
|
}),
|
|
93
105
|
});
|
|
94
106
|
}
|
|
107
|
+
/** the most lines of a file one request reads (`module`, `lines`) */
|
|
108
|
+
export const MAX_FILE_LINES = 400;
|
|
109
|
+
/** `lines=<from>-<to>` read: lines from 1, at most MAX_FILE_LINES of them; `<from>` alone, from
|
|
110
|
+
* there on. Null where it is not that. */
|
|
111
|
+
function readSpan(x) {
|
|
112
|
+
const m = /^(\d{1,7})(?:-(\d{1,7}))?$/.exec(x.trim());
|
|
113
|
+
if (!m)
|
|
114
|
+
return null;
|
|
115
|
+
const from = Number(m[1]), to = m[2] === undefined ? from + MAX_FILE_LINES - 1 : Number(m[2]);
|
|
116
|
+
return from >= 1 && to >= from && to - from < MAX_FILE_LINES ? { from, to } : null;
|
|
117
|
+
}
|
|
118
|
+
/** Lines `from`–`to` of a file (from 1), cut at its end: the text, where it is, and how to ask on. */
|
|
119
|
+
function fileLines(lines, { from, to }) {
|
|
120
|
+
const last = Math.min(to, lines.length);
|
|
121
|
+
return {
|
|
122
|
+
lines: { from, to: last, of: lines.length },
|
|
123
|
+
text: from > lines.length ? "" : lines.slice(from - 1, last).join("\n"),
|
|
124
|
+
...(last < lines.length ? { more: `lines=${last + 1}-${Math.min(lines.length, last + MAX_FILE_LINES)}` } : {}),
|
|
125
|
+
};
|
|
126
|
+
}
|
|
95
127
|
export function searchGet(api, a) {
|
|
96
128
|
const v = api.graph();
|
|
97
129
|
if (!(v instanceof View))
|
package/dist/server/app.js
CHANGED
|
@@ -367,7 +367,7 @@ export class App {
|
|
|
367
367
|
return git(this, res);
|
|
368
368
|
if (path === "/api/meta" || path === "/api/edges")
|
|
369
369
|
return graphParts(this, req, res, url);
|
|
370
|
-
if (path === "/api/hover" || path === "/api/hover/open" || path === "/api/hover/names" || path === "/api/hover/describe" || path === "/api/hover/print")
|
|
370
|
+
if (path === "/api/hover" || path === "/api/hover/open" || path === "/api/hover/names" || path === "/api/hover/describe" || path === "/api/hover/print" || path === "/api/hover/goals" || path === "/api/hover/goals/info")
|
|
371
371
|
return hover(this, req, res, url, reader);
|
|
372
372
|
if (path === "/api/source")
|
|
373
373
|
return source(this, res, url);
|
package/dist/server/appgraph.js
CHANGED
|
@@ -6,11 +6,13 @@ import { gitSource } from "./history.js";
|
|
|
6
6
|
import { readFileSync } from "node:fs";
|
|
7
7
|
import { join } from "node:path";
|
|
8
8
|
import { isDeclName } from "./hover.js";
|
|
9
|
+
import { MAX_TAGS } from "./goals.js";
|
|
9
10
|
import { outsideSource } from "./outside.js";
|
|
10
11
|
import { lakeTextHash } from "./status.js";
|
|
11
12
|
import { finalAnswer } from "./readonlygate.js";
|
|
12
13
|
import { slimDeploy } from "../shared/deploy.js";
|
|
13
14
|
import { packedGz, sendGz, sendJson } from "./send.js";
|
|
15
|
+
import { isCompiledText } from "../shared/build.js";
|
|
14
16
|
/** with Lean's language server as it is (server/hover.ts): what is open, what crashed
|
|
15
17
|
* a read-only reader is told how the server is, not its log nor where its review is kept
|
|
16
18
|
* `?brief`: the build status without its files, which the page reads by GET /api/build */
|
|
@@ -145,6 +147,28 @@ export async function hover(app, req, res, url, reader) {
|
|
|
145
147
|
const want = url.searchParams.get("hash");
|
|
146
148
|
if (want && want !== hash)
|
|
147
149
|
return sendJson(res, { stale: true });
|
|
150
|
+
if (path === "/api/hover/goals" || path === "/api/hover/goals/info") {
|
|
151
|
+
// the InfoView (ReviewSpec, "The InfoView"; the owner's decisions of 2026-11-01): of the text the
|
|
152
|
+
// graph was read from alone. A file changed since it was compiled, or a package at another
|
|
153
|
+
// revision than the graph was read at, has none: its goals would be another text's than Lean
|
|
154
|
+
// checked. Only the place (numbers) and, for a subterm, its number in the answer reach Lean
|
|
155
|
+
const compiled = out ? undefined : live.compiledOf(mod);
|
|
156
|
+
if (out?.changed)
|
|
157
|
+
return reply({ off: `${out.file.package.name} is at another revision than when the graph was read: its goals would be another text's than Lean checked.` });
|
|
158
|
+
if (!out && !isCompiledText(compiled, hash))
|
|
159
|
+
return reply({ off: compiled ? "This file has changed since it was compiled: its goals would be the present text's, not what Lean checked." : "Nothing records which text this file was compiled from (a build restored from Lake's cache, the file modified since): its goals could be another text's than Lean checked." });
|
|
160
|
+
const gl = Number(url.searchParams.get("line")), gc = Number(url.searchParams.get("character"));
|
|
161
|
+
if (!Number.isInteger(gl) || !Number.isInteger(gc) || gl < 0 || gc < 0 || gl > 10_000_000 || gc > 100_000)
|
|
162
|
+
return sendJson(res, { error: "position" }, 400);
|
|
163
|
+
// a place Lean has not reached yet is said so after a moment, and asked again: the pane says how
|
|
164
|
+
// far Lean is meanwhile
|
|
165
|
+
if (path === "/api/hover/goals")
|
|
166
|
+
return reply(await app.hover.goals(file, text, hash, gl, gc, 2000));
|
|
167
|
+
const k = Number(url.searchParams.get("k"));
|
|
168
|
+
if (!Number.isInteger(k) || k < 0 || k >= MAX_TAGS)
|
|
169
|
+
return sendJson(res, { error: "k: a subterm's number in the answer" }, 400);
|
|
170
|
+
return reply(await app.hover.subterm(file, text, hash, gl, gc, k, 6000));
|
|
171
|
+
}
|
|
148
172
|
if (path === "/api/hover/names") {
|
|
149
173
|
// the positions of the names in a declaration's source, as JSON [[line, character], …]
|
|
150
174
|
const r = await app.json(req, 1 << 20);
|
|
@@ -151,7 +151,7 @@ export function boardSpots(b) {
|
|
|
151
151
|
place(n, "project", why ? itemSpot(n, nb, exists, linkedOn(n, nb, links)) : null);
|
|
152
152
|
}
|
|
153
153
|
for (const n of rec.notes ?? [])
|
|
154
|
-
place(n, "view", viewSpot(n, nb, (key) => index.get(key) ?? -1));
|
|
154
|
+
place(n, "view", viewSpot(n, nb, (key) => index.get(key) ?? -1, exists));
|
|
155
155
|
// in the order they were written, which is the order of those level on the board: the notes that
|
|
156
156
|
// point nowhere on the ring, at its top
|
|
157
157
|
notes.sort((x, y) => (x.note.at < y.note.at ? -1 : x.note.at > y.note.at ? 1 : 0));
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
// Lean's answers about goals, read for the InfoView (ReviewSpec, "The InfoView"; shared/goals.ts):
|
|
2
|
+
// its plain ones (`$/lean/plainGoal`, `$/lean/plainTermGoal`) and its interactive ones, through its
|
|
3
|
+
// RPC (`Lean.Widget.getInteractiveGoals`, `getInteractiveTermGoal`), whose text is tagged with what
|
|
4
|
+
// each subterm is: a reference into the RPC session (`{p: "12"}`), which Lean answers
|
|
5
|
+
// `infoToInteractive` about (the subterm's type and doc). A page is never given a reference: each
|
|
6
|
+
// subterm is numbered in the answer (`k`), and the server asks Lean by the reference it holds for
|
|
7
|
+
// that number. What is read is checked and bounded: a goal whose text runs past what a page shows
|
|
8
|
+
// is cut, and past a number of subterms the rest is text alone.
|
|
9
|
+
import { CHANGES } from "../shared/goals.js";
|
|
10
|
+
import { parsePlainGoal } from "../shared/goals.js";
|
|
11
|
+
/** the most subterms numbered in one answer: past it, the rest is text */
|
|
12
|
+
export const MAX_TAGS = 20_000;
|
|
13
|
+
/** the most characters of goals one answer carries, all its texts together */
|
|
14
|
+
export const MAX_TEXT = 400_000;
|
|
15
|
+
/** how deep a tagged text is read: deeper, it is text */
|
|
16
|
+
const MAX_DEPTH = 400;
|
|
17
|
+
const isRef = (x) => !!x && typeof x === "object" && typeof x.p === "string" && x.p.length <= 64;
|
|
18
|
+
/** What one answer reads: the references its subterms are numbered by (`refs[k]`), and what is
|
|
19
|
+
* left of its bounds. */
|
|
20
|
+
export class Reading {
|
|
21
|
+
refs = [];
|
|
22
|
+
text = 0;
|
|
23
|
+
cut = false;
|
|
24
|
+
/** Lean's tagged text (`CodeWithInfos`: `{text}`, `{append: […]}`, `{tag: [info, text]}`) read
|
|
25
|
+
* as the page's; a subterm numbered by its reference. */
|
|
26
|
+
code(c, depth = 0) {
|
|
27
|
+
if (!c || typeof c !== "object")
|
|
28
|
+
return "";
|
|
29
|
+
if (depth > MAX_DEPTH)
|
|
30
|
+
return this.take(plainText(c));
|
|
31
|
+
const o = c;
|
|
32
|
+
if (typeof o.text === "string")
|
|
33
|
+
return this.take(o.text);
|
|
34
|
+
if (Array.isArray(o.append)) {
|
|
35
|
+
const parts = o.append.map((x) => this.code(x, depth + 1)).filter((x) => x !== "");
|
|
36
|
+
return parts.length === 1 ? parts[0] : parts;
|
|
37
|
+
}
|
|
38
|
+
if (Array.isArray(o.tag) && o.tag.length === 2) {
|
|
39
|
+
const [info, inner] = o.tag;
|
|
40
|
+
const t = this.code(inner, depth + 1);
|
|
41
|
+
const ref = info && typeof info === "object" ? info.info : null;
|
|
42
|
+
if (!isRef(ref) || this.refs.length >= MAX_TAGS || this.cut)
|
|
43
|
+
return t;
|
|
44
|
+
const d = info.diffStatus;
|
|
45
|
+
this.refs.push(ref);
|
|
46
|
+
return { k: this.refs.length - 1, t, ...(CHANGES.includes(d) ? { d: d } : {}) };
|
|
47
|
+
}
|
|
48
|
+
return "";
|
|
49
|
+
}
|
|
50
|
+
/** a text, within what is left of the answer's */
|
|
51
|
+
take(s) {
|
|
52
|
+
if (this.cut)
|
|
53
|
+
return "";
|
|
54
|
+
const left = MAX_TEXT - this.text;
|
|
55
|
+
if (s.length <= left) {
|
|
56
|
+
this.text += s.length;
|
|
57
|
+
return s;
|
|
58
|
+
}
|
|
59
|
+
this.cut = true;
|
|
60
|
+
this.text = MAX_TEXT;
|
|
61
|
+
return s.slice(0, Math.max(0, left)) + "…";
|
|
62
|
+
}
|
|
63
|
+
/** A hypothesis bundle as Lean gives it (`InteractiveHypothesisBundle`). */
|
|
64
|
+
hyp(h) {
|
|
65
|
+
if (!h || typeof h !== "object")
|
|
66
|
+
return null;
|
|
67
|
+
const o = h;
|
|
68
|
+
const names = Array.isArray(o.names) ? o.names.filter((n) => typeof n === "string").map((n) => n.slice(0, 300)) : [];
|
|
69
|
+
if (!names.length)
|
|
70
|
+
return null;
|
|
71
|
+
const d = o.isInserted === true ? "inserted" : o.isRemoved === true ? "removed" : undefined;
|
|
72
|
+
return { names, type: this.code(o.type), ...(o.val ? { val: this.code(o.val) } : {}), ...(d ? { d } : {}) };
|
|
73
|
+
}
|
|
74
|
+
/** A goal as Lean gives it (`InteractiveGoal`). */
|
|
75
|
+
goal(g) {
|
|
76
|
+
if (!g || typeof g !== "object")
|
|
77
|
+
return null;
|
|
78
|
+
const o = g;
|
|
79
|
+
const hyps = (Array.isArray(o.hyps) ? o.hyps : []).map((h) => this.hyp(h)).filter((h) => h !== null);
|
|
80
|
+
const d = o.isInserted === true ? "inserted" : o.isRemoved === true ? "removed" : undefined;
|
|
81
|
+
const prefix = typeof o.goalPrefix === "string" && o.goalPrefix.length <= 8 ? o.goalPrefix : "⊢ ";
|
|
82
|
+
return { ...(typeof o.userName === "string" && o.userName ? { case: o.userName.slice(0, 300) } : {}), hyps, type: this.code(o.type), prefix, ...(d ? { d } : {}) };
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
/** Lean's interactive goals (`InteractiveGoals`, `{goals}`), or null where it has none there. */
|
|
86
|
+
export function readInteractiveGoals(r, reading) {
|
|
87
|
+
if (!r || typeof r !== "object" || !Array.isArray(r.goals))
|
|
88
|
+
return null;
|
|
89
|
+
return r.goals.map((g) => reading.goal(g)).filter((g) => g !== null);
|
|
90
|
+
}
|
|
91
|
+
/** Lean's interactive term goal (`InteractiveTermGoal`: hypotheses, type, range), or null. */
|
|
92
|
+
export function readInteractiveTermGoal(r, reading) {
|
|
93
|
+
if (!r || typeof r !== "object")
|
|
94
|
+
return null;
|
|
95
|
+
const o = r;
|
|
96
|
+
const range = readRange(o.range);
|
|
97
|
+
if (!range)
|
|
98
|
+
return null;
|
|
99
|
+
const hyps = (Array.isArray(o.hyps) ? o.hyps : []).map((h) => reading.hyp(h)).filter((h) => h !== null);
|
|
100
|
+
return { hyps, type: reading.code(o.type), range };
|
|
101
|
+
}
|
|
102
|
+
/** Lean's plain goals (`PlainGoal`: `{goals: [text], rendered}`), or null. */
|
|
103
|
+
export function readPlainGoals(r) {
|
|
104
|
+
if (!r || typeof r !== "object" || !Array.isArray(r.goals))
|
|
105
|
+
return null;
|
|
106
|
+
let left = MAX_TEXT;
|
|
107
|
+
return r.goals
|
|
108
|
+
.filter((g) => typeof g === "string")
|
|
109
|
+
.map((g) => {
|
|
110
|
+
const s = g.length > left ? g.slice(0, Math.max(0, left)) + "…" : g;
|
|
111
|
+
left = Math.max(0, left - g.length);
|
|
112
|
+
return parsePlainGoal(s);
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
/** Lean's plain term goal (`PlainTermGoal`: `{goal: text, range}`), or null. */
|
|
116
|
+
export function readPlainTermGoal(r) {
|
|
117
|
+
if (!r || typeof r !== "object")
|
|
118
|
+
return null;
|
|
119
|
+
const o = r;
|
|
120
|
+
const range = readRange(o.range);
|
|
121
|
+
if (typeof o.goal !== "string" || !range)
|
|
122
|
+
return null;
|
|
123
|
+
const g = parsePlainGoal(o.goal.slice(0, MAX_TEXT));
|
|
124
|
+
return { hyps: g.hyps, type: g.type, range };
|
|
125
|
+
}
|
|
126
|
+
/** An LSP range as [line, character, endLine, endCharacter]. */
|
|
127
|
+
function readRange(r) {
|
|
128
|
+
const o = r;
|
|
129
|
+
const ns = [o?.start?.line, o?.start?.character, o?.end?.line, o?.end?.character];
|
|
130
|
+
return ns.every((n) => Number.isInteger(n) && n >= 0) ? ns : null;
|
|
131
|
+
}
|
|
132
|
+
/** What Lean says of a subterm (`InfoPopup`: its type, the term with its implicit arguments, its
|
|
133
|
+
* constant's doc), as text. */
|
|
134
|
+
export function readInfo(r) {
|
|
135
|
+
if (!r || typeof r !== "object")
|
|
136
|
+
return {};
|
|
137
|
+
const o = r;
|
|
138
|
+
const text = (c) => {
|
|
139
|
+
const reading = new Reading();
|
|
140
|
+
const t = reading.code(c);
|
|
141
|
+
const flat = (x) => (typeof x === "string" ? x : Array.isArray(x) ? x.map(flat).join("") : flat(x.t));
|
|
142
|
+
return flat(t);
|
|
143
|
+
};
|
|
144
|
+
return {
|
|
145
|
+
...(o.exprExplicit ? { expr: text(o.exprExplicit) } : {}),
|
|
146
|
+
...(o.type ? { type: text(o.type) } : {}),
|
|
147
|
+
...(typeof o.doc === "string" && o.doc ? { doc: o.doc.slice(0, 20_000) } : {}),
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
/** How many widgets Lean has at a place (`GetWidgetsResponse`, `{widgets: […]}`): only counted. */
|
|
151
|
+
export function readWidgets(r) {
|
|
152
|
+
const ws = r && typeof r === "object" ? r.widgets : null;
|
|
153
|
+
return Array.isArray(ws) ? ws.length : 0;
|
|
154
|
+
}
|
|
155
|
+
/** Every reference Lean gave in an answer, to release it when the answer is let go: walked with a
|
|
156
|
+
* stack of its own, however deep the answer is. */
|
|
157
|
+
export function refsIn(x) {
|
|
158
|
+
const out = [];
|
|
159
|
+
const stack = [x];
|
|
160
|
+
while (stack.length) {
|
|
161
|
+
const y = stack.pop();
|
|
162
|
+
if (!y || typeof y !== "object")
|
|
163
|
+
continue;
|
|
164
|
+
if (Array.isArray(y)) {
|
|
165
|
+
for (const z of y)
|
|
166
|
+
stack.push(z);
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
const o = y;
|
|
170
|
+
if (isRef(o) && Object.keys(o).length === 1)
|
|
171
|
+
out.push({ p: o.p });
|
|
172
|
+
else
|
|
173
|
+
for (const v of Object.values(o))
|
|
174
|
+
stack.push(v);
|
|
175
|
+
}
|
|
176
|
+
return out;
|
|
177
|
+
}
|
|
178
|
+
/** A tagged text's characters alone, read with a stack of its own: what is deeper than a page
|
|
179
|
+
* reads tags in. */
|
|
180
|
+
function plainText(c) {
|
|
181
|
+
let out = "";
|
|
182
|
+
const stack = [c];
|
|
183
|
+
while (stack.length) {
|
|
184
|
+
const y = stack.pop();
|
|
185
|
+
if (!y || typeof y !== "object")
|
|
186
|
+
continue;
|
|
187
|
+
const o = y;
|
|
188
|
+
if (typeof o.text === "string")
|
|
189
|
+
out += o.text;
|
|
190
|
+
else if (Array.isArray(o.append))
|
|
191
|
+
for (let i = o.append.length - 1; i >= 0; i--)
|
|
192
|
+
stack.push(o.append[i]);
|
|
193
|
+
else if (Array.isArray(o.tag) && o.tag.length === 2)
|
|
194
|
+
stack.push(o.tag[1]);
|
|
195
|
+
}
|
|
196
|
+
return out;
|
|
197
|
+
}
|
|
198
|
+
/** The goals after a tactic that holds a block of its own, as Lean gives them at the start of the
|
|
199
|
+
* next line (shared/goals.ts, afterLine): there Lean's diff is about the tactic that line begins,
|
|
200
|
+
* not this one. It is taken off, and what this tactic added is marked instead: a hypothesis none of
|
|
201
|
+
* the goals before had, by its name and its type. */
|
|
202
|
+
export function afterTheLine(before, after) {
|
|
203
|
+
const had = new Set((before ?? []).flatMap((g) => g.hyps.flatMap((h) => h.names.map((n) => `${n} : ${plain(h.type)}`))));
|
|
204
|
+
return after.map((g) => ({
|
|
205
|
+
...(g.case ? { case: g.case } : {}),
|
|
206
|
+
hyps: g.hyps.map((h) => ({
|
|
207
|
+
names: h.names,
|
|
208
|
+
type: undiff(h.type),
|
|
209
|
+
...(h.val !== undefined ? { val: undiff(h.val) } : {}),
|
|
210
|
+
...(h.names.some((n) => !had.has(`${n} : ${plain(h.type)}`)) && before ? { d: "inserted" } : {}),
|
|
211
|
+
})),
|
|
212
|
+
type: undiff(g.type),
|
|
213
|
+
...(g.prefix ? { prefix: g.prefix } : {}),
|
|
214
|
+
}));
|
|
215
|
+
}
|
|
216
|
+
/** A tagged text without Lean's diff marks, its subterms numbered as they were. */
|
|
217
|
+
function undiff(t) {
|
|
218
|
+
if (typeof t === "string")
|
|
219
|
+
return t;
|
|
220
|
+
if (Array.isArray(t))
|
|
221
|
+
return t.map(undiff);
|
|
222
|
+
return { k: t.k, t: undiff(t.t) };
|
|
223
|
+
}
|
|
224
|
+
const plain = (t) => (typeof t === "string" ? t : Array.isArray(t) ? t.map(plain).join("") : plain(t.t));
|