eyeprolog 1.4.6 → 1.4.9

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.
@@ -2,7 +2,7 @@
2
2
 
3
3
  Wide-audience companion explanations for selected runnable examples.
4
4
 
5
- - [Introduction to EyeProlog](https://eyereasoner.github.io/eyeprolog/examples/deck/introduction-to-eyeprolog) — a short slide deck introducing facts, rules, answers, proofs, ISO conformance, JavaScript embedding, and RDF roundtrips.
5
+ - [Introduction to EyeProlog](https://eyereasoner.github.io/eyeprolog/examples/deck/introduction-to-eyeprolog) — a guided slide deck introducing facts, rules, answers, proofs, CLP(Z), CLP(B), ISO conformance, JavaScript embedding, RDF roundtrips, and a categorized path through the example corpus.
6
6
  - [Cross-organization data sharing](https://eyereasoner.github.io/eyeprolog/examples/deck/cross-organization-data-sharing) — ODRL/DPV sharing decisions with permit, deny, review, safeguards, and obligations.
7
7
  - [Explainable EV-depot configuration](https://eyereasoner.github.io/eyeprolog/examples/deck/explainable-ev-depot-configuration) — reversible configuration rules that select a charger, explain blockers, and derive required changes.
8
8
  - [ODRL policy reasoning](https://eyereasoner.github.io/eyeprolog/examples/deck/odrl-policy-reasoning) — permissions, prohibitions, duties, conflicts, and defaults.
@@ -1,26 +1,24 @@
1
1
  ---
2
2
  marp: true
3
3
  title: Introduction to EyeProlog
4
- description: A short presentation introducing EyeProlog as a portable, inspectable ISO Prolog engine for answers, proofs, and RDF-backed reasoning.
4
+ description: A short presentation introducing EyeProlog as a portable, inspectable ISO Prolog engine for answers, proofs, constraints, and RDF-backed reasoning.
5
5
  ---
6
6
 
7
7
  # Introduction to EyeProlog
8
8
 
9
- Portable Prolog reasoning for answers, proofs, and knowledge graphs
9
+ Portable Prolog reasoning for answers, proofs, constraints, and knowledge graphs
10
+
11
+ EyeProlog is meant to be approachable from two directions at once: it is small enough to embed in JavaScript applications, yet explicit enough that a conclusion can be explained as ordinary facts and rules.
10
12
 
11
13
  ---
12
14
 
13
15
  ## What is EyeProlog?
14
16
 
15
- EyeProlog is a small ISO Prolog implementation for JavaScript.
17
+ EyeProlog is an ISO-oriented Prolog implementation for JavaScript.
16
18
 
17
- It turns explicit facts and rules into:
19
+ It turns explicit facts and rules into answers, variable bindings, checked example output, optional proof traces, and embeddable reasoning in Node.js or browsers.
18
20
 
19
- - answers;
20
- - variable bindings;
21
- - checked example output;
22
- - optional proof traces;
23
- - embeddable reasoning in Node.js and browsers.
21
+ The project’s emphasis is not only “can this query run?” but also “can we inspect what happened, test it again, and explain why the answer follows?” That makes EyeProlog useful for demos, documentation, knowledge-graph workflows, and applications that need a small transparent reasoning layer.
24
22
 
25
23
  ---
26
24
 
@@ -42,6 +40,8 @@ Then you ask questions:
42
40
  true.
43
41
  ```
44
42
 
43
+ The same rule can answer a yes/no question, enumerate solutions, or participate in a larger proof. That reuse is one of the reasons Prolog remains attractive for rule-heavy software.
44
+
45
45
  ---
46
46
 
47
47
  ## The core idea
@@ -56,7 +56,9 @@ facts + rules + query
56
56
  answers with reasons
57
57
  ```
58
58
 
59
- The same rule can answer a yes/no question, enumerate solutions, or participate in a larger proof.
59
+ A query is not just a function call. It is a logical request: “find values that make this relation true.” EyeProlog searches by unifying terms, trying clauses, and backtracking over alternatives.
60
+
61
+ Because the input remains ordinary Prolog text, the boundary between data, rules, and answers stays inspectable.
60
62
 
61
63
  ---
62
64
 
@@ -78,6 +80,8 @@ Try a list query:
78
80
  ; X = logic.
79
81
  ```
80
82
 
83
+ The semicolon asks for another answer. This small interaction already shows the Prolog execution model: a relation can produce more than one solution.
84
+
81
85
  ---
82
86
 
83
87
  ## Programs are ordinary text
@@ -97,6 +101,8 @@ Run it:
97
101
  eyeprolog examples/socrates.pl
98
102
  ```
99
103
 
104
+ The examples directory uses this same pattern at larger scale. Each runnable example has checked output, so examples are not just illustrative snippets; they are part of the release gate.
105
+
100
106
  ---
101
107
 
102
108
  ## Proofs are first-class
@@ -107,12 +113,9 @@ EyeProlog can show not only *what* was concluded, but *why*.
107
113
  eyeprolog --proof examples/socrates.pl
108
114
  ```
109
115
 
110
- Proof output is useful for:
116
+ Proof output is useful for debugging rules, explaining decisions, preserving audit trails, and keeping documentation honest. If a proof-producing example changes, the checked proof output changes too.
111
117
 
112
- - debugging rules;
113
- - explaining decisions;
114
- - regression tests;
115
- - audit trails.
118
+ This is especially helpful when rules encode policies, risk decisions, or derived knowledge where the explanation matters as much as the final answer.
116
119
 
117
120
  ---
118
121
 
@@ -120,23 +123,44 @@ Proof output is useful for:
120
123
 
121
124
  EyeProlog starts from ISO/IEC 13211-1 Prolog.
122
125
 
123
- That matters because facts, rules, terms, control, errors, streams, arithmetic, and meta-calls have an external reference point.
126
+ That matters because facts, rules, terms, control, errors, streams, arithmetic, and meta-calls have an external reference point. Strict ISO mode keeps that standardized core separate and testable.
127
+
128
+ Normal mode adds practical libraries and embedding features. The design goal is to keep extensions visible rather than hiding them inside an undocumented dialect.
129
+
130
+ ---
131
+
132
+ ## Constraints over integers: CLP(Z)
124
133
 
125
- Normal mode adds practical libraries and embedding features. Strict ISO mode keeps the standardized core separate and testable.
134
+ `library(clpz)` lets programs state relations over integers instead of manually enumerating arithmetic cases.
135
+
136
+ ```prolog
137
+ :- use_module(library(clpz)).
138
+
139
+ ?- X #>= 1, X #=< 4, Y #= X*X, labeling([X]).
140
+ X = 1, Y = 1
141
+ ; X = 2, Y = 4
142
+ ; X = 3, Y = 9
143
+ ; X = 4, Y = 16.
144
+ ```
145
+
146
+ This style is useful when you know the constraints before you know the values: scheduling, allocation, Sudoku, finite-domain puzzles, resource planning, and arithmetic search.
126
147
 
127
148
  ---
128
149
 
129
- ## Why JavaScript?
150
+ ## Constraints over Booleans: CLP(B)
130
151
 
131
- EyeProlog runs where modern applications already run:
152
+ `library(clpb)` provides Boolean constraints with satisfiability, cardinality, counting, and optimization predicates.
153
+
154
+ ```prolog
155
+ :- use_module(library(clpb)).
156
+
157
+ ?- sat(card([2], [A,B,C,D]) * (A =< C) * (B # D)),
158
+ labeling([A,B,C,D]).
159
+ ```
132
160
 
133
- - command-line tools;
134
- - npm packages;
135
- - browser playgrounds;
136
- - web workers;
137
- - JavaScript APIs.
161
+ The recent CLP(B) examples show several practical shapes: circuit verification, quorum constraints, feature-model counting, and weighted release planning.
138
162
 
139
- The aim is not to replace every Prolog system. It is to make portable Prolog reasoning easy to embed and inspect.
163
+ Boolean constraints are compact when the domain is “on/off”, “selected/not selected”, “permitted/denied”, or “feature enabled/disabled”.
140
164
 
141
165
  ---
142
166
 
@@ -154,56 +178,79 @@ RDF dataset
154
178
 
155
179
  RDF remains the interchange layer. Prolog remains the transparent reasoning layer.
156
180
 
181
+ This is useful when a knowledge graph needs explicit derivation rules, but the data should still move in and out as RDF rather than as a private in-memory structure.
182
+
157
183
  ---
158
184
 
159
- ## What this enables
185
+ ## Categorized examples: start here
160
186
 
161
- The example suite uses the same pattern for:
187
+ The example corpus is intentionally broad. A practical path through it is:
162
188
 
163
- - policy decisions;
164
- - incident response;
165
- - software supply-chain risk;
166
- - scientific evidence assessment;
167
- - EV-depot configuration;
168
- - symbiotic human/AI/knowledge-graph workflows.
189
+ - **First steps:** `socrates.pl`, `ancestor.pl`, `list-collection.pl`.
190
+ - **Algorithms:** graph reachability, Dijkstra, parser, FFT, SAT/DPLL.
191
+ - **Integer constraints:** CLP(Z) queens, Sudoku, resource allocation.
192
+ - **Boolean constraints:** CLP(B) circuits, cardinality, feature models, planning.
193
+ - **Policies and decisions:** access control, ODRL, GDPR, trust flow.
194
+ - **RDF roundtrips:** symbiotic knowledge graph and domain-specific RDF scenarios.
195
+ - **Proofs:** examples with checked `--proof` output.
169
196
 
170
- Each scenario keeps data, rules, answers, and materialized results as separate inspectable artifacts.
197
+ Use the [playground](https://eyereasoner.github.io/eyeprolog/playground) for quick exploration, or browse the [examples source tree](https://github.com/eyereasoner/eyeprolog/tree/main/examples) when you want to inspect the program and golden output side by side.
171
198
 
172
199
  ---
173
200
 
174
- ## Testing and conformance
201
+ ## Example path: constraints
202
+
203
+ For constraint reasoning, start with the small examples and move outward:
204
+
205
+ - `clpz-n-queens.pl` shows finite-domain search.
206
+ - `clpz-sudoku-9x9.pl` shows a familiar grid problem.
207
+ - `clpz-resource-allocation.pl` shows planning under resource limits.
208
+ - `clpb-boolean-circuit.pl` shows Boolean verification.
209
+ - `clpb-cardinality.pl` shows exact-count selection.
210
+ - `clpb-feature-model.pl` and `clpb-weighted-planning.pl` show product/release decisions.
211
+
212
+ Together these examples show that Prolog can describe a problem declaratively while constraint libraries do the propagation and search work.
213
+
214
+ ---
215
+
216
+ ## Example path: knowledge graphs
217
+
218
+ For RDF-backed reasoning, start with [Symbiotic Knowledge Graphs](https://eyereasoner.github.io/eyeprolog/examples/deck/symbiotic-knowledge-graphs).
175
219
 
176
- EyeProlog’s release gate includes:
220
+ Then explore the scenario decks:
177
221
 
178
- - file-based conformance cases;
179
- - strict ISO tests;
180
- - WG17 syntax cases;
181
- - regression tests;
182
- - runnable examples;
183
- - proof examples;
184
- - documentation synchronization checks.
222
+ - [Cross-organization data sharing](https://eyereasoner.github.io/eyeprolog/examples/deck/cross-organization-data-sharing)
223
+ - [Explainable EV-depot configuration](https://eyereasoner.github.io/eyeprolog/examples/deck/explainable-ev-depot-configuration)
224
+ - [Operational incident response](https://eyereasoner.github.io/eyeprolog/examples/deck/operational-incident-response)
225
+ - [SBOM vulnerability response](https://eyereasoner.github.io/eyeprolog/examples/deck/sbom-vulnerability-response)
226
+ - [Scientific evidence graph](https://eyereasoner.github.io/eyeprolog/examples/deck/scientific-evidence-graph)
227
+
228
+ Each scenario keeps source RDF, Prolog rules, generated Prolog, and output RDF as separate artifacts.
229
+
230
+ ---
231
+
232
+ ## Testing and conformance
233
+
234
+ EyeProlog’s release gate includes conformance cases, strict ISO tests, WG17 syntax cases, regression tests, runnable examples, proof examples, and documentation synchronization checks.
185
235
 
186
236
  The point is simple: behavior should be reproducible, not anecdotal.
187
237
 
238
+ The examples are also linted for avoidable singleton-variable warnings. This keeps example code clean for users who run examples through stricter tooling or compare with other Prolog systems.
239
+
188
240
  ---
189
241
 
190
242
  ## When EyeProlog fits
191
243
 
192
- EyeProlog is a good fit when a project needs:
244
+ EyeProlog is a good fit when a project needs explicit rules, inspectable conclusions, portable Prolog syntax, an embeddable JavaScript runtime, checked examples and proofs, and a clean boundary between data, rules, and results.
193
245
 
194
- - explicit rules;
195
- - inspectable conclusions;
196
- - portable Prolog syntax;
197
- - embeddable JavaScript runtime;
198
- - checked examples and proofs;
199
- - a clean boundary between data, rules, and results.
246
+ It is not trying to replace high-performance native Prolog systems. Its niche is transparent reasoning that can live close to JavaScript applications, documentation, and web-accessible demos.
200
247
 
201
248
  ---
202
249
 
203
250
  ## Where to go next
204
251
 
205
252
  - Read [project README](https://github.com/eyereasoner/eyeprolog#readme) for setup and links.
206
- - Read [Why EyeProlog?](https://eyereasoner.github.io/eyeprolog/why-eyeprolog) for the design motivation.
207
253
  - Open [EyeProlog playground](https://eyereasoner.github.io/eyeprolog/playground) to run examples in a browser.
208
- - Explore [runnable examples](https://github.com/eyereasoner/eyeprolog/tree/main/examples) for runnable programs.
254
+ - Browse [runnable examples](https://github.com/eyereasoner/eyeprolog/tree/main/examples) by category or filename.
255
+ - Read [Why EyeProlog?](https://eyereasoner.github.io/eyeprolog/why-eyeprolog) for the design motivation.
209
256
  - Read [The Art of EyeProlog](https://eyereasoner.github.io/eyeprolog/the-art-of-eyeprolog) for the full reference.
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.4.6",
6
+ "version": "1.4.9",
7
7
  "description": "EyeProlog turns facts and rules into answers and proofs.",
8
8
  "type": "module",
9
9
  "main": "./index.js",
package/src/cli.js CHANGED
@@ -154,6 +154,7 @@ export async function main(argv) {
154
154
  isoStrict: options.isoStrict,
155
155
  autoload: options.autoload,
156
156
  autoloadGoals: options.goals,
157
+ onWarning: printSourceWarning,
157
158
  });
158
159
 
159
160
  const portabilityFailures = program.interopPortabilityWarnings ?? [];
@@ -271,6 +272,13 @@ function readStdin() {
271
272
  });
272
273
  }
273
274
 
275
+
276
+ function printSourceWarning(warning) {
277
+ if (warning.kind !== 'singleton') return;
278
+ process.stderr.write(`Warning: singleton: ${warning.name}, near ${warning.filename}:${warning.line}
279
+ `);
280
+ }
281
+
274
282
  function printWarnings(program) {
275
283
  for (const warning of program.interopPortabilityWarnings ?? []) {
276
284
  if (warning.kind === 'library') {
package/src/parser.js CHANGED
@@ -246,6 +246,8 @@ class Parser {
246
246
  this.anonymous = 0;
247
247
  this.variables = new Map();
248
248
  this.sourceMetadata = options.sourceMetadata !== false;
249
+ this.onWarning = typeof options.onWarning === 'function' ? options.onWarning : null;
250
+ this.currentSourceTermLine = 1;
249
251
  this.strictIso = options.isoStrict === true;
250
252
  this.parserFlagState = options.parserFlagState ?? {
251
253
  doubleQuotes: options.doubleQuotes ?? 'chars',
@@ -1010,9 +1012,14 @@ class Parser {
1010
1012
  parseProgram(emit = null) {
1011
1013
  const clauses = emit ? null : [];
1012
1014
  let clauseNumber = 0;
1013
- const accept = emit ?? ((clause) => clauses.push(clause));
1015
+ const rawAccept = emit ?? ((clause) => clauses.push(clause));
1016
+ const accept = (item) => {
1017
+ reportClauseSingletonWarnings(item, this.onWarning, this.filename, this.currentSourceTermLine);
1018
+ rawAccept(item);
1019
+ };
1014
1020
  while (this.token.type !== TOK.EOF) {
1015
1021
  const line = this.token.line;
1022
+ this.currentSourceTermLine = line;
1016
1023
  // Prefix operator notation needs one program-level distinction so
1017
1024
  // a comma in `?- A, B.` remains inside the query rather than outside the
1018
1025
  // prefix term. Ordinary functional notation is parsed as a term and then
@@ -1226,6 +1233,29 @@ function listAtomNames(term) {
1226
1233
  }
1227
1234
 
1228
1235
 
1236
+ function collectClauseVariableOccurrences(term, counts) {
1237
+ if (term == null) return;
1238
+ if (term.type === 'var') {
1239
+ counts.set(term.name, (counts.get(term.name) ?? 0) + 1);
1240
+ return;
1241
+ }
1242
+ if (term.type === COMPOUND) {
1243
+ for (const arg of term.args) collectClauseVariableOccurrences(arg, counts);
1244
+ }
1245
+ }
1246
+
1247
+ function reportClauseSingletonWarnings(clause, onWarning, filename, line) {
1248
+ if (typeof onWarning !== 'function' || clause?.kind === 'quad') return;
1249
+ const counts = new Map();
1250
+ collectClauseVariableOccurrences(clause.head, counts);
1251
+ for (const goal of clause.body ?? []) collectClauseVariableOccurrences(goal, counts);
1252
+ for (const [name, count] of counts) {
1253
+ if (count !== 1 || String(name).startsWith('_')) continue;
1254
+ onWarning({ kind: 'singleton', name, filename: filename ?? '<input>', line });
1255
+ }
1256
+ }
1257
+
1258
+
1229
1259
  export function parseClauses(source, options = {}) {
1230
1260
  const ownsParserFlagState = options.parserFlagState == null;
1231
1261
  const initialDoubleQuotes = options.doubleQuotes ?? 'chars';
@@ -1282,6 +1312,7 @@ function parseClausesFastNoSource(source, emit = null, emitBinary = null, option
1282
1312
  const accept = emit ?? ((clause) => clauses.push(clause));
1283
1313
  let anonymous = 0;
1284
1314
  let chunk = '';
1315
+ let chunkStartLine = 1;
1285
1316
 
1286
1317
  const cached = (cache, key, create) => {
1287
1318
  const existing = cache.get(key);
@@ -1466,12 +1497,42 @@ function parseClausesFastNoSource(source, emit = null, emitBinary = null, option
1466
1497
  return -1;
1467
1498
  };
1468
1499
 
1469
- const emitFastBinaryRange = (text, start, end) => {
1500
+ const reportFastBinarySingletonWarnings = (head, body, line) => {
1501
+ if (typeof options.onWarning !== 'function') return;
1502
+ const variables = [];
1503
+ const add = (type, name) => {
1504
+ if (type !== 'var' || String(name).startsWith('_')) return;
1505
+ variables.push(name);
1506
+ };
1507
+ add(head.arg0Type, head.arg0Name);
1508
+ add(head.arg1Type, head.arg1Name);
1509
+ if (body != null) {
1510
+ add(body.arg0Type, body.arg0Name);
1511
+ add(body.arg1Type, body.arg1Name);
1512
+ }
1513
+ for (let i = 0; i < variables.length; i++) {
1514
+ let count = 0;
1515
+ for (let j = 0; j < variables.length; j++) {
1516
+ if (variables[i] === variables[j]) count++;
1517
+ }
1518
+ if (count === 1) {
1519
+ options.onWarning({
1520
+ kind: 'singleton',
1521
+ name: variables[i],
1522
+ filename: options.filename ?? '<input>',
1523
+ line,
1524
+ });
1525
+ }
1526
+ }
1527
+ };
1528
+
1529
+ const emitFastBinaryRange = (text, start, end, line) => {
1470
1530
  if (!emitBinary || start >= end || text.charCodeAt(end - 1) !== 46) return false;
1471
1531
  const termEnd = end - 1;
1472
1532
  const rule = findRuleInRange(text, start, termEnd);
1473
1533
  if (rule < 0) {
1474
1534
  if (!parseBinaryRawRange(text, start, termEnd, rawHead)) return false;
1535
+ reportFastBinarySingletonWarnings(rawHead, null, line);
1475
1536
  emitBinary(rawHead.name,
1476
1537
  rawHead.arg0Type, rawHead.arg0Name, rawHead.arg1Type, rawHead.arg1Name,
1477
1538
  null, null, null, null, null);
@@ -1480,6 +1541,7 @@ function parseClausesFastNoSource(source, emit = null, emitBinary = null, option
1480
1541
  if (findRuleInRange(text, rule + 2, termEnd) >= 0 ||
1481
1542
  !parseBinaryRawRange(text, start, rule, rawHead) ||
1482
1543
  !parseBinaryRawRange(text, rule + 2, termEnd, rawBody)) return false;
1544
+ reportFastBinarySingletonWarnings(rawHead, rawBody, line);
1483
1545
  emitBinary(rawHead.name,
1484
1546
  rawHead.arg0Type, rawHead.arg0Name, rawHead.arg1Type, rawHead.arg1Name,
1485
1547
  rawBody.name, rawBody.arg0Type, rawBody.arg0Name, rawBody.arg1Type, rawBody.arg1Name);
@@ -1541,6 +1603,7 @@ function parseClausesFastNoSource(source, emit = null, emitBinary = null, option
1541
1603
 
1542
1604
  const flush = () => {
1543
1605
  const text = chunk.trim();
1606
+ const sourceLine = chunkStartLine;
1544
1607
  chunk = '';
1545
1608
  if (!text) return true;
1546
1609
  // Once a preparation-time char_conversion/2 mapping is active, every
@@ -1549,11 +1612,15 @@ function parseClausesFastNoSource(source, emit = null, emitBinary = null, option
1549
1612
  // here would silently bypass Convc for otherwise simple clauses.
1550
1613
  const simple = preparationConversionActive() ? null : parseSimple(text);
1551
1614
  if (simple) {
1615
+ reportClauseSingletonWarnings(simple, options.onWarning, options.filename ?? '<input>', sourceLine);
1552
1616
  accept(simple);
1553
1617
  return true;
1554
1618
  }
1555
1619
  try {
1556
- const parsed = new Parser(text, { ...options, sourceMetadata: false }).parseProgram();
1620
+ const nestedWarning = typeof options.onWarning === 'function'
1621
+ ? (warning) => options.onWarning({ ...warning, line: warning.line + sourceLine - 1 })
1622
+ : null;
1623
+ const parsed = new Parser(text, { ...options, sourceMetadata: false, onWarning: nestedWarning }).parseProgram();
1557
1624
  for (const clause of parsed) accept(clause);
1558
1625
  return true;
1559
1626
  } catch (_) {
@@ -1562,6 +1629,7 @@ function parseClausesFastNoSource(source, emit = null, emitBinary = null, option
1562
1629
  };
1563
1630
 
1564
1631
  let lineStart = 0;
1632
+ let lineNumber = 1;
1565
1633
  while (lineStart <= source.length) {
1566
1634
  let lineEnd = source.indexOf('\n', lineStart);
1567
1635
  if (lineEnd < 0) lineEnd = source.length;
@@ -1571,17 +1639,21 @@ function parseClausesFastNoSource(source, emit = null, emitBinary = null, option
1571
1639
  [contentStart, contentEnd] = trimRange(source, contentStart, contentEnd);
1572
1640
  if (contentStart < contentEnd && source.charCodeAt(contentStart) !== 37) {
1573
1641
  if (!chunk && source.charCodeAt(contentEnd - 1) === 46 && !preparationConversionActive()) {
1574
- if (emitFastBinaryRange(source, contentStart, contentEnd)) {
1642
+ if (emitFastBinaryRange(source, contentStart, contentEnd, lineNumber)) {
1575
1643
  // The program builder accepted a compact binary clause directly.
1576
1644
  } else {
1577
1645
  const simple = parseFastRange(source, contentStart, contentEnd);
1578
- if (simple) accept(simple);
1579
- else {
1646
+ if (simple) {
1647
+ reportClauseSingletonWarnings(simple, options.onWarning, options.filename ?? '<input>', lineNumber);
1648
+ accept(simple);
1649
+ } else {
1650
+ chunkStartLine = lineNumber;
1580
1651
  chunk = source.slice(lineStart, lineEnd) + '\n';
1581
1652
  if (!flush()) return null;
1582
1653
  }
1583
1654
  }
1584
1655
  } else {
1656
+ if (!chunk) chunkStartLine = lineNumber;
1585
1657
  chunk += source.slice(lineStart, lineEnd) + '\n';
1586
1658
  if (source.charCodeAt(contentEnd - 1) === 46) {
1587
1659
  if (!flush()) return null;
@@ -1590,6 +1662,7 @@ function parseClausesFastNoSource(source, emit = null, emitBinary = null, option
1590
1662
  }
1591
1663
  if (lineEnd === source.length) break;
1592
1664
  lineStart = lineEnd + 1;
1665
+ lineNumber++;
1593
1666
  }
1594
1667
  if (chunk.trim() && !flush()) return null;
1595
1668
  return clauses ?? true;
package/src/program.js CHANGED
@@ -1390,7 +1390,7 @@ function readModuleSource(designation, options) {
1390
1390
  return {
1391
1391
  name,
1392
1392
  text: registered.source,
1393
- options: { ...options, filename: registered.filename, baseDir: 'src/lib' },
1393
+ options: { ...options, filename: registered.filename, baseDir: 'src/lib', onWarning: null },
1394
1394
  };
1395
1395
  }
1396
1396
  if (designation.type !== ATOM) throw new PrologError('type_error(source_sink)', designation);
@@ -214,6 +214,11 @@ function runProgramExample(programFile, filename, options) {
214
214
  const expectedExit = text.match(/^%\s*expect-exit:\s*(\d+)\s*$/m);
215
215
  const program = Program.parseSources([{ text, filename }], {
216
216
  sourceMetadata: options.proof,
217
+ onWarning: (warning) => {
218
+ if (warning.kind === 'singleton') {
219
+ process.stderr.write(`Warning: singleton: ${warning.name}, near ${warning.filename}:${warning.line}\n`);
220
+ }
221
+ },
217
222
  });
218
223
  try {
219
224
  const result = run(program, { ...options, goals: goalsInProgramOrder(program, text) });
@@ -45,7 +45,7 @@ import {
45
45
  variantTerms,
46
46
  parseProgramText,
47
47
  } from '../src/index.js';
48
- import { ISO_OPERATOR_DEFINITIONS, parseGoalText, parseNumberTokenText } from '../src/parser.js';
48
+ import { ISO_OPERATOR_DEFINITIONS, parseGoalText, parseNumberTokenText, tryParseClausesFastInto } from '../src/parser.js';
49
49
  import { PrologError, formalErrorTerm } from '../src/iso.js';
50
50
  import { compareTerms } from '../src/term.js';
51
51
  import { formatTermForWrite } from '../src/write.js';
@@ -4062,6 +4062,38 @@ child.stdin.write(\`consult(${consultedAtom}).\\n\`);
4062
4062
  assertIncludes(result.stderr, '/(member, 2)', 'stderr');
4063
4063
  },
4064
4064
  },
4065
+ {
4066
+ name: 'CLI reports singleton source variables without a corpus-wide warning scan',
4067
+ run: () => {
4068
+ const input = 'p(X, Once, _Ignored, _) :- q(X).\nq(a).\n';
4069
+ const result = runCli(['-'], { input });
4070
+ assertEqual(result.status, 0, 'exit status');
4071
+ assertEqual(result.stdout, '', 'stdout');
4072
+ assertEqual(result.stderr, 'Warning: singleton: Once, near <stdin>:1\n', 'stderr');
4073
+ },
4074
+ },
4075
+ {
4076
+ name: 'singleton warnings preserve the compact binary parser path',
4077
+ run: () => {
4078
+ const clauses = [];
4079
+ const binary = [];
4080
+ const warnings = [];
4081
+ const parsed = tryParseClausesFastInto(
4082
+ 'p(X,Y) :- q(X,a).\n',
4083
+ (clause) => clauses.push(clause),
4084
+ (...args) => binary.push(args),
4085
+ { filename: 'fast.pl', onWarning: (warning) => warnings.push(warning) },
4086
+ );
4087
+ assertEqual(parsed, true, 'fast parse result');
4088
+ assertEqual(clauses.length, 0, 'materialized clause count');
4089
+ assertEqual(binary.length, 1, 'direct binary clause count');
4090
+ assertEqual(warnings.length, 1, 'warning count');
4091
+ assertEqual(warnings[0].kind, 'singleton', 'warning kind');
4092
+ assertEqual(warnings[0].name, 'Y', 'warning variable');
4093
+ assertEqual(warnings[0].filename, 'fast.pl', 'warning filename');
4094
+ assertEqual(warnings[0].line, 1, 'warning line');
4095
+ },
4096
+ },
4065
4097
  {
4066
4098
  name: '--warnings flags explicit library(prologue) dependencies',
4067
4099
  run: () => {
@@ -4647,6 +4679,7 @@ child.stdin.write(\`consult(${consultedAtom}).\\n\`);
4647
4679
  }
4648
4680
 
4649
4681
 
4682
+
4650
4683
  function documentationSyncCases() {
4651
4684
  return [
4652
4685
  {