// Stage 6: pseudo -> matho. For the programs that are equations, the generated LaTeX must read, lower back to // pseudo that behaves like the original, and be a fixed point; every other program is refused with the construct // and its line. The equation sets are the ones tests/matho.test.mjs checks by hand-worked values. import test from 'node:test'; import assert from 'node:assert/strict'; import { programs, EQUATIONS } from './helpers/sync-programs.mjs'; import { studyOf, runPseudo } from '../dist/services/pseudo.js'; import { parseMatho } from '../dist/services/matho.js'; import { pseudoToMatho } from '../dist/sync/pseudo-to-matho.js'; globalThis.localStorage = { getItem() { return null; }, setItem() { }, removeItem() { } }; /** * Equations the reader lowers but this generator does not write back, and why. An equation leaves this list * when it round-trips; the test fails if a listed equation does, so the list can only shrink. */ const KNOWN_GAPS = { 'matrix-row': 'the row index i is an input that only subscripts, and the reader recognizes that only for a left side written y_i, with the function name carrying the subscript', 'choose': 'the reader names the second factorial counter K2 (K is the input), and a loop counter has to be a single letter in matho', }; const SUPPORTED_AT_LEAST = 24; // raise as matho learns more forms; counted below const num = (line) => { const m = /returned (.*)$/.exec(line ?? ''); return m ? m[1] : null; }; function run(pseudo, call, input = []) { const st = studyOf(pseudo); assert.ok(st.ok, `pseudo should study: ${st.diagnostics.filter((d) => d.severity === 'error').map((d) => d.message).join(' | ')}`); const t = runPseudo(st, { call, input, maxSteps: 200000 }); return { value: t.error ? `error:${t.error.kind}` : num(t.output[t.output.length - 1]), out: t.error ? [] : t.output.slice(0, -1) }; } const same = (a, b) => a.value === b.value || (!Number.isNaN(Number(a.value)) && !Number.isNaN(Number(b.value)) && Math.abs(Number(a.value) - Number(b.value)) <= 1e-9 * Math.max(1, Math.abs(Number(a.value)))) && JSON.stringify(a.out) === JSON.stringify(b.out); const lowerPseudo = (latex) => { const r = parseMatho(latex); assert.ok(r.ok, `the generated equations should read: ${latex}\n${r.diagnostics.map((d) => d.message).join(' | ')}`); return r.program.pseudo; }; const results = new Map(); test('every equation set: the LaTeX reads, lowers to pseudo that behaves like the original, and is a fixed point; or it is a listed gap', () => { const problems = []; let ok = 0; for (const [id, [src, call]] of Object.entries(EQUATIONS)) { const first = parseMatho(src); assert.ok(first.ok, `${id}: the fixture should lower`); const pseudo = first.program.pseudo; const m = pseudoToMatho(pseudo); results.set(id, m); const known = KNOWN_GAPS[id]; if ('unsupported' in m) { if (!known) problems.push(`${id}: ${m.unsupported.map((d) => d.message).join(' | ')}`); continue; } if (known) { problems.push(`${id}: now round-trips; take it off KNOWN_GAPS (${known})`); continue; } ok++; // reads, and same behavior: the original equations' pseudo and the pseudo of the written equations let again; try { again = lowerPseudo(m.source); } catch (e) { problems.push(`${id}: ${e.message}`); continue; } const a = run(pseudo, call); const b = run(again, call); if (!same(a, b)) problems.push(`${id}: behavior differs: ${JSON.stringify(a)} vs ${JSON.stringify(b)}`); // fixed point const m2 = pseudoToMatho(again); if ('unsupported' in m2) { problems.push(`${id}: the lowered pseudo is no longer supported: ${m2.unsupported[0].message}`); continue; } if (m2.source !== m.source) problems.push(`${id}: not a fixed point:\n${m.source}\nvs\n${m2.source}`); } assert.deepEqual(problems, []); assert.ok(ok >= SUPPORTED_AT_LEAST, `${ok} equation sets round-trip, fewer than ${SUPPORTED_AT_LEAST}`); }); test('every exercise and corpus program: accepted and checked the same way, or refused with a reason and a line', () => { let accepted = 0; for (const p of programs()) { if (!studyOf(p.pseudo).ok) continue; const m = pseudoToMatho(p.pseudo); if ('unsupported' in m) { assert.ok(m.unsupported.length > 0, p.id); for (const d of m.unsupported) { assert.ok(Number.isInteger(d.line) && d.line >= 1 && d.line <= p.pseudo.split('\n').length, `${p.id}: line ${d.line}`); assert.ok(d.message.length > 20, p.id); } continue; } accepted++; const again = lowerPseudo(m.source); if (p.call) assert.ok(same(run(p.pseudo, p.call), run(again, p.call)), `${p.id}: behavior differs`); const m2 = pseudoToMatho(again); assert.ok(!('unsupported' in m2) && m2.source === m.source, `${p.id}: not a fixed point`); } assert.ok(accepted >= 3, `${accepted} exercise, corpus, and fixture programs are equations, fewer than 3`); }); test('a program that is not an equation is refused with the construct and its line', () => { const ask = (pseudo) => { const r = pseudoToMatho(pseudo); assert.ok('unsupported' in r, pseudo); return r.unsupported.map((d) => d.message).join(' | '); }; assert.match(ask(`FUNCTION COUNT_DOWN(N : INTEGER) RETURNS INTEGER DECLARE K : INTEGER K <- N WHILE K > 0 K <- K - 1 ENDWHILE RETURN K ENDFUNCTION `), /matho has no form for a WHILE loop \(line 4\)/); assert.match(ask(`FUNCTION F(N : INTEGER) RETURNS INTEGER OUTPUT N RETURN N ENDFUNCTION `), /OUTPUT.*\(line 2\)/); assert.match(ask(`FUNCTION SHOUT(T : STRING) RETURNS STRING RETURN T ENDFUNCTION `), /(answers STRING|not a number).*\(line 1\)/); assert.match(ask(`FUNCTION F(N : INTEGER) RETURNS INTEGER IF N > 0 THEN IF N > 1 THEN RETURN 2 ELSE RETURN 1 ENDIF ELSE RETURN 0 ENDIF ENDFUNCTION `), /nested IF.*\(line 3\)/); assert.match(ask('OUTPUT 1 + 2\n'), /steps outside a function/); assert.match(ask('FUNCTION F(\n'), /./); }); test('names: SIGMA is \\sigma, X_BAR is \\bar{x}, a single letter is lower case, and a hint restores the student\'s own spelling', () => { const pseudo = `FUNCTION X_BAR(N : INTEGER, X : LIST OF FLOAT) RETURNS FLOAT DECLARE Total : FLOAT Total <- 0.0 FOR I <- 0 TO N Total <- Total + X[I] NEXT I RETURN 1 / N * Total ENDFUNCTION FUNCTION SIGMA(V : FLOAT) RETURNS FLOAT RETURN V * V ENDFUNCTION `; const plain = pseudoToMatho(pseudo); assert.ok(!('unsupported' in plain), JSON.stringify(plain)); assert.match(plain.source, /^\\bar\{x\} = /m); assert.match(plain.source, /^\\sigma = /m); assert.match(plain.source, /x_\{i\}/); const hinted = pseudoToMatho(pseudo, { names: { X_BAR: '\\overline{x}' } }); assert.ok(!('unsupported' in hinted), JSON.stringify(hinted)); assert.match(hinted.source, /^\\overline\{x\} = /m); assert.equal(parseMatho(hinted.source).program.defs[0].name, 'X_BAR'); }); test('a hint that does not lower back to the function is not trusted', () => { const r = pseudoToMatho(`FUNCTION SQUARE(V : FLOAT) RETURNS FLOAT RETURN V * V ENDFUNCTION `, { names: { SQUARE: 'q' } }); assert.ok('unsupported' in r); assert.match(r.unsupported[0].message, /SQUARE comes back as/); }); test('the line map has one entry per output line and points inside the pseudo', () => { for (const [id, [src]] of Object.entries(EQUATIONS)) { const m = results.get(id) ?? pseudoToMatho(parseMatho(src).program.pseudo); if ('unsupported' in m) continue; const lines = m.source.replace(/\n$/, '').split('\n'); assert.equal(m.map.length, lines.length, id); const n = parseMatho(src).program.pseudo.split('\n').length; for (const x of m.map) assert.ok(x === null || (Number.isInteger(x) && x >= 1 && x <= n), `${id}: ${x}`); } }); test('a sum from 1 reads x_i for the pseudo\'s X[I - 1], and a sum from 0 reads x_i for X[I]', () => { const from1 = pseudoToMatho(parseMatho(EQUATIONS['sum-from-one'][0]).program.pseudo); assert.match(from1.source, /\\sum_\{i=1\}\^\{n\} x_\{i\}/); const from0 = pseudoToMatho(parseMatho(EQUATIONS['mean'][0]).program.pseudo); assert.match(from0.source, /\\sum_\{i=0\}\^\{n-1\} x_\{i\}/); // L[I] in a loop the reader would shift is not an equation it can say const r = pseudoToMatho(`FUNCTION S(N : INTEGER, L : LIST OF FLOAT) RETURNS FLOAT DECLARE Total : FLOAT Total <- 0.0 FOR I <- 1 TO N + 1 Total <- Total + L[I] NEXT I RETURN Total ENDFUNCTION `); assert.ok('unsupported' in r); assert.match(r.unsupported[0].message, /starts at 1/); }); /** Pseudo a student wrote by hand (not lowered from matho) that is still an equation: the same three checks. */ const HAND = { 'whole-number-types': [`FUNCTION AREA(W : INTEGER, H : INTEGER) RETURNS INTEGER RETURN W * H ENDFUNCTION `, 'AREA(3, 4)'], 'elseif-chain': [`FUNCTION SIGN(X : FLOAT) RETURNS INTEGER IF X < 0 THEN RETURN -1 ELSEIF X == 0 THEN RETURN 0 ENDIF RETURN 1 ENDFUNCTION `, 'SIGN(-2.5)'], 'sum-of-squares-to-n': [`FUNCTION SQUARES(N : INTEGER) RETURNS INTEGER DECLARE Total : INTEGER Total <- 0 FOR K <- 1 TO N + 1 Total <- Total + K * K NEXT K RETURN Total ENDFUNCTION `, 'SQUARES(4)'], 'mean-of-a-list': [`FUNCTION MEAN(L : LIST OF FLOAT) RETURNS FLOAT DECLARE Total : FLOAT Total <- 0.0 FOR I <- 0 TO LENGTH(L) Total <- Total + L[I] NEXT I RETURN Total / LENGTH(L) ENDFUNCTION `, 'MEAN([1.0, 2.0, 6.0])'], }; test('pseudo written by hand that is an equation: accepted when matho can say it, and held to the same three checks', () => { const said = []; for (const [id, [pseudo, call]] of Object.entries(HAND)) { const m = pseudoToMatho(pseudo); if ('unsupported' in m) { assert.equal(id, 'mean-of-a-list', `${id}: ${m.unsupported[0].message}`); continue; } said.push(id); const again = lowerPseudo(m.source); assert.ok(same(run(pseudo, call), run(again, call)), `${id}: ${JSON.stringify(run(pseudo, call))} vs ${JSON.stringify(run(again, call))}`); const m2 = pseudoToMatho(again); assert.ok(!('unsupported' in m2) && m2.source === m.source, `${id}: not a fixed point`); } assert.deepEqual(said, ['whole-number-types', 'elseif-chain', 'sum-of-squares-to-n']); });