eyeprolog 1.1.22 → 1.1.24

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/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.1.22",
6
+ "version": "1.1.24",
7
7
  "description": "EyeProlog turns facts and rules into answers and proofs.",
8
8
  "type": "module",
9
9
  "main": "./index.js",
package/src/iso.js CHANGED
@@ -1784,7 +1784,8 @@ function* phraseBuiltin({ solver, goal, env }) {
1784
1784
  }
1785
1785
  }
1786
1786
  function formalErrorTerm(error) {
1787
- if (error.formalTerm != null) return compound('error', [error.formalTerm, atom('eyeprolog')]);
1787
+ const context = error.contextTerm ?? atom('eyeprolog');
1788
+ if (error.formalTerm != null) return compound('error', [error.formalTerm, context]);
1788
1789
  const parse = (text) => {
1789
1790
  const open = text.indexOf('(');
1790
1791
  if (open === -1) return atom(text);
@@ -1811,7 +1812,7 @@ function formalErrorTerm(error) {
1811
1812
  formal = compound(formal.name, [error.culprit]);
1812
1813
  }
1813
1814
  }
1814
- return compound('error', [formal, atom('eyeprolog')]);
1815
+ return compound('error', [formal, context]);
1815
1816
  }
1816
1817
  function* catchBuiltin({ solver, goal, env }) {
1817
1818
  const child = solver.cloneForInnerGoal();
package/src/parser.js CHANGED
@@ -39,6 +39,7 @@ const graphicAtomChars = '#$&*+-./<=>@^~\\:';
39
39
  // canonical notation. Commas remain separators except inside parentheses.
40
40
  const INFIX_OPERATORS = new Map([
41
41
  [':-', { precedence: 1, associativity: 'none' }],
42
+ ['?-', { precedence: 1, associativity: 'none' }], // quad label extension
42
43
  ['-->', { precedence: 1, associativity: 'none' }],
43
44
  ['|', { precedence: 96, associativity: 'right' }],
44
45
  [';', { precedence: 101, associativity: 'right' }],
@@ -77,14 +78,15 @@ const INFIX_OPERATORS = new Map([
77
78
  ['^', { precedence: 1001, associativity: 'right' }],
78
79
  ]);
79
80
  const PREFIX_OPERATORS = new Map([
80
- ['\\+', 301],
81
- ['+', 1001],
82
- ['-', 1001],
83
- ['\\', 1001],
81
+ ['?-', { precedence: 1, strict: true }],
82
+ ['\\+', { precedence: 301, strict: false }],
83
+ ['+', { precedence: 1001, strict: false }],
84
+ ['-', { precedence: 1001, strict: false }],
85
+ ['\\', { precedence: 1001, strict: false }],
84
86
  ]);
85
87
 
86
88
  export const ISO_OPERATOR_DEFINITIONS = [
87
- [1200, 'xfx', ':-'], [1200, 'fx', ':-'], [1200, 'xfx', '-->'],
89
+ [1200, 'xfx', ':-'], [1200, 'fx', ':-'], [1200, 'fx', '?-'], [1200, 'xfx', '-->'],
88
90
  [1105, 'xfy', '|'],
89
91
  [1100, 'xfy', ';'], [1050, 'xfy', '->'], [1000, 'xfy', ','],
90
92
  [900, 'fy', '\\+'],
@@ -97,6 +99,13 @@ export const ISO_OPERATOR_DEFINITIONS = [
97
99
  [200, 'fy', '+'], [200, 'fy', '-'], [200, 'fy', '\\'],
98
100
  ];
99
101
 
102
+ // EyeProlog's embedded quad syntax permits an optional label before `?-`.
103
+ // That makes `?-` an implementation-specific xfx operator in addition to its
104
+ // ISO 1200 fx definition.
105
+ export const QUAD_OPERATOR_DEFINITIONS = [
106
+ [1200, 'xfx', '?-'],
107
+ ];
108
+
100
109
  const CLPZ_OPERATOR_DEFINITIONS = [
101
110
  [760, 'yfx', '#<==>'], [750, 'xfy', '#==>'], [750, 'yfx', '#<=='],
102
111
  [740, 'yfx', '#\\/'], [730, 'yfx', '#\\'], [720, 'yfx', '#/\\'],
@@ -133,9 +142,7 @@ function defineParserOperator(state, priority, specifier, name) {
133
142
  export function createParserOperatorState(definitions = [], includeDefaults = true) {
134
143
  const state = {
135
144
  infixOperators: includeDefaults ? new Map(INFIX_OPERATORS) : new Map(),
136
- prefixOperators: includeDefaults
137
- ? new Map([...PREFIX_OPERATORS].map(([name, precedence]) => [name, { precedence, strict: false }]))
138
- : new Map(),
145
+ prefixOperators: includeDefaults ? new Map(PREFIX_OPERATORS) : new Map(),
139
146
  postfixOperators: new Map(),
140
147
  };
141
148
  for (const definition of definitions) {
package/src/program.js CHANGED
@@ -4,6 +4,7 @@ import { ATOM, COMPOUND, VAR, Env, atom, compound, deref, flattenConjunction, is
4
4
  import { formatTermForWrite } from './write.js';
5
5
  import {
6
6
  ISO_OPERATOR_DEFINITIONS,
7
+ QUAD_OPERATOR_DEFINITIONS,
7
8
  createParserOperatorState,
8
9
  parseClauses,
9
10
  parseClausesInto,
@@ -101,8 +102,10 @@ export class Program {
101
102
  this.moduleMetaPredicates = new Map();
102
103
  this.dynamicPredicates = new Set();
103
104
  this.operators = new Map();
104
- for (const [priority, specifier, name] of ISO_OPERATOR_DEFINITIONS) {
105
- this.defineOperator(priority, specifier, name);
105
+ for (const definitions of [ISO_OPERATOR_DEFINITIONS, QUAD_OPERATOR_DEFINITIONS]) {
106
+ for (const [priority, specifier, name] of definitions) {
107
+ this.defineOperator(priority, specifier, name);
108
+ }
106
109
  }
107
110
  this.initializations = [];
108
111
  this.quads = [];
package/src/repl.js CHANGED
@@ -415,21 +415,43 @@ function formatError(engine, state, error) {
415
415
  let generated = 0;
416
416
  if (error.formalTerm != null) {
417
417
  collectUnboundVariables(engine, error.formalTerm, env, variableNames, () => `_${letterName(generated++)}`);
418
+ if (error.contextTerm != null) {
419
+ collectUnboundVariables(engine, error.contextTerm, env, variableNames, () => `_${letterName(generated++)}`);
420
+ }
418
421
  const formal = engine.formatTermForWrite(error.formalTerm, env, {
419
422
  quoted: true,
420
423
  operators: [...state.program.operators.values()],
421
424
  variableNames,
422
425
  });
426
+ if (error.contextTerm != null) {
427
+ const context = engine.formatTermForWrite(error.contextTerm, env, {
428
+ quoted: true,
429
+ operators: [...state.program.operators.values()],
430
+ variableNames,
431
+ });
432
+ return `error(${formal}, ${context}).`;
433
+ }
423
434
  return `error(${formal}).`;
424
435
  }
425
436
  if (error.culprit != null) {
426
437
  collectUnboundVariables(engine, error.culprit, env, variableNames, () => `_${letterName(generated++)}`);
427
438
  }
439
+ if (error.contextTerm != null) {
440
+ collectUnboundVariables(engine, error.contextTerm, env, variableNames, () => `_${letterName(generated++)}`);
441
+ }
428
442
  const culprit = error.culprit == null ? '' : `, ${engine.formatTermForWrite(error.culprit, env, {
429
443
  quoted: true,
430
444
  operators: [...state.program.operators.values()],
431
445
  variableNames,
432
446
  })}`;
447
+ if (error.contextTerm != null) {
448
+ const context = engine.formatTermForWrite(error.contextTerm, env, {
449
+ quoted: true,
450
+ operators: [...state.program.operators.values()],
451
+ variableNames,
452
+ });
453
+ return `error(${error.formal}${culprit}, ${context}).`;
454
+ }
433
455
  return `error(${error.formal}${culprit}).`;
434
456
  }
435
457
  const message = error?.message ?? String(error);
package/src/solver.js CHANGED
@@ -1,7 +1,7 @@
1
1
  // Depth-first EyeProlog solver with builtin dispatch, memoization, and guarded recursion handling.
2
2
  // Most semantic decisions still flow through unification; optimizations only select candidates earlier.
3
3
  import {
4
- COMPOUND, Env, compound, copyResolved, deref, flattenConjunction, freshTerm,
4
+ COMPOUND, Env, compound, copyResolved, deref, emptyList, flattenConjunction, freshTerm,
5
5
  numberTerm, numberTextFromDouble, termIsGround, termToString, unify, variantTerms,
6
6
  } from './term.js';
7
7
  import { PrologError } from './iso.js';
@@ -24,8 +24,11 @@ export function nextFreshId() {
24
24
  }
25
25
 
26
26
  function raiseOccursCheckError(left, right, env) {
27
- const error = new PrologError('occurs_check');
28
- error.formalTerm = copyResolved(compound('occurs_check', [left, right]), env);
27
+ // occurs_check=error is an implementation-specific STO diagnostic. Report
28
+ // the unrepresentable cyclic result through the standard error envelope.
29
+ // Keep the implementation-defined context empty for stable, portable output.
30
+ const error = new PrologError('representation_error(term)');
31
+ error.contextTerm = emptyList();
29
32
  throw error;
30
33
  }
31
34
 
@@ -5,3 +5,10 @@
5
5
  operator_atoms(Priority, Specifier, List) :-
6
6
  current_op(Priority, Specifier, :-),
7
7
  List = [:-,-].
8
+
9
+ % ISO 6.3.4.4, Table 7: ?- is a predefined 1200 fx operator.
10
+
11
+ %% goal: query_prefix_operator(ok)
12
+
13
+ query_prefix_operator(ok) :-
14
+ current_op(1200, fx, ?-).
@@ -1,2 +1,3 @@
1
1
  operator_atoms(1200, xfx, [':-', '-']).
2
2
  operator_atoms(1200, fx, [':-', '-']).
3
+ query_prefix_operator(ok).
@@ -523,7 +523,7 @@ c4 ?- call((!;1)).
523
523
  },
524
524
  },
525
525
  {
526
- name: 'occurs_check error mode detects STO while ISO occurs-check unification still fails',
526
+ name: 'occurs_check error mode reports representation_error(term) while ISO occurs-check unification still fails',
527
527
  run: () => {
528
528
  const filename = path.join(tmp, `occurs-check-${++tmpCounter}.pl`);
529
529
  fs.writeFileSync(filename, ':- set_prolog_flag(occurs_check, error).\nsame(X, X).\n');
@@ -544,11 +544,11 @@ c4 ?- call((!;1)).
544
544
  assertEqual(result.stdout,
545
545
  '?- Mode = true.\n' +
546
546
  '?- true.\n' +
547
- '?- error(occurs_check(_A, f(_A))).\n' +
548
- '?- E = error(occurs_check(_A, g(_A)), eyeprolog).\n' +
547
+ '?- error(representation_error(term), []).\n' +
548
+ '?- E = error(representation_error(term), []).\n' +
549
549
  '?- false.\n' +
550
550
  '?- true.\n' +
551
- '?- error(occurs_check(_A, k(_A))).\n' +
551
+ '?- error(representation_error(term), []).\n' +
552
552
  '?- true.\n' +
553
553
  '?- false.\n' +
554
554
  '?- ',
@@ -950,6 +950,32 @@ c4 ?- call((!;1)).
950
950
  assertEqual(run(source, { goal: 'operator_argument(ok)' }).stdout, 'operator_argument(ok).\n', 'operator argument syntax');
951
951
  },
952
952
  },
953
+ {
954
+ name: 'ISO query operator and quad infix extension are visible through current_op/3',
955
+ run: () => {
956
+ assertEqual(
957
+ run('', { goal: 'current_op(Priority, Specifier, ?-)' }).stdout,
958
+ "current_op(1200, fx, '?-').\ncurrent_op(1200, xfx, '?-').\n",
959
+ 'query operator definitions',
960
+ );
961
+ assertEqual(
962
+ run('', { goal: 'current_op(1200, fx, ?-)' }).stdout,
963
+ "current_op(1200, fx, '?-').\n",
964
+ 'ISO query prefix operator',
965
+ );
966
+ assertEqual(
967
+ run('', { goal: 'current_op(1200, xfx, ?-)' }).stdout,
968
+ "current_op(1200, xfx, '?-').\n",
969
+ 'quad query infix operator',
970
+ );
971
+ const prefix = parseGoalText('(?- true)');
972
+ assertEqual(prefix.name, '?-', 'prefix query functor');
973
+ assertEqual(prefix.arity, 1, 'prefix query arity');
974
+ const infix = parseGoalText('(label ?- true)');
975
+ assertEqual(infix.name, '?-', 'quad query functor');
976
+ assertEqual(infix.arity, 2, 'quad query arity');
977
+ },
978
+ },
953
979
  {
954
980
  name: 'term input keeps dotted operators intact and uses program operators',
955
981
  run: () => {
@@ -785,15 +785,22 @@ subject-to-occurs-check (STO). EyeProlog's default remains the sound finite-tree
785
785
  behavior above. For diagnosis, EyeProlog additionally provides the
786
786
  implementation-specific flag `occurs_check`; setting it to `error` turns a
787
787
  normal unification that would otherwise fail because of the occurs check into
788
- an exception:
788
+ a representation error:
789
789
 
790
790
  ```eyeprolog
791
791
  :- set_prolog_flag(occurs_check, error).
792
792
 
793
793
  sto_example :- X = wrapper(X).
794
- % error(occurs_check(_A, wrapper(_A)))
794
+ % error(representation_error(term), [])
795
795
  ```
796
796
 
797
+ The ISO error mechanism wraps an error term together with an
798
+ implementation-defined context term. For this implementation-specific STO
799
+ diagnostic EyeProlog uses `representation_error(term)` and currently uses the
800
+ empty list `[]` as that context. This reports that the cyclic result of a
801
+ succeeding STO unification cannot be represented by EyeProlog's finite-tree
802
+ term model, without exposing a non-standard `occurs_check/2` error term.
803
+
797
804
  The supported values are `true` (the default finite-tree behavior) and `error`
798
805
  (STO detection). EyeProlog deliberately does not provide `occurs_check=false`,
799
806
  because its term model does not construct cyclic terms. The ISO predicate
@@ -5124,7 +5131,13 @@ parsing of subsequent text, place them before their first use. ISO argument
5124
5131
  syntax also permits an atom that is currently an operator to appear directly
5125
5132
  as a functional argument or list element, so forms such as
5126
5133
  `current_op(Priority, Specifier, :-)` and `[:-,-]` are valid without quoting
5127
- or parenthesizing those operator atoms.
5134
+ or parenthesizing those operator atoms. The ISO initial operator table also
5135
+ contains `?-` at priority 1200 with specifier `fx`, so
5136
+ `current_op(1200, fx, ?-)` succeeds. EyeProlog's embedded quad syntax permits
5137
+ an optional label before the query marker (`Label ?- Query.`), so while quad
5138
+ syntax is supported it additionally exposes `?-` at priority 1200 with
5139
+ specifier `xfx` as an implementation-specific operator. Consequently
5140
+ `current_op(Priority, Specifier, ?-)` enumerates both definitions.
5128
5141
 
5129
5142
  Run [`iso-dynamic-database.pl`](https://github.com/eyereasoner/eyeprolog/blob/main/examples/iso-dynamic-database.pl)
5130
5143
  for an explicitly stateful queue and
@@ -5290,8 +5303,10 @@ clause ends in a period. The grammar above gives the canonical term shapes.
5290
5303
  The initial operator table contains the following ISO-style operators, all
5291
5304
  lowered to ordinary compound terms:
5292
5305
 
5293
- - prefix: `\+`, unary `+`, unary `-`, and `\`;
5306
+ - prefix: ISO `?-`, `\+`, unary `+`, unary `-`, and `\`;
5294
5307
  - control: `,`, `;`, and `->`;
5308
+ - quad syntax extension: `?-` is also a priority-1200 `xfx` operator so a
5309
+ label may precede a quad query;
5295
5310
  - grammar rules: `-->` and the Part 3 alternative `|`;
5296
5311
  - unification and comparison: `=`, `\=`, `==`, `\==`, `@<`, `@=<`, `@>`,
5297
5312
  `@>=`, `is`, `=:=`, `=\=`, `<`, `=<`, `>`, and `>=`;