eyelang 1.3.1 → 1.3.3

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 CHANGED
@@ -4,7 +4,7 @@
4
4
  [![DOI](https://img.shields.io/badge/DOI-10.5281%2Fzenodo.20761726-blue.svg)](https://doi.org/10.5281/zenodo.20761726)
5
5
 
6
6
  Eyelang is a small logic programming language for rules, goals, answers, and proofs.
7
- Its source syntax is Prolog-like Horn-clause syntax with a few deliberate eyelang choices, such as `?x` variables for N3/SPARQL-style readability and explicit `table(path, 2).` declarations for tabled predicates.
7
+ Its source syntax is Prolog-like Horn-clause syntax with a few deliberate eyelang choices, such as `?x` variables for N3/SPARQL-style readability, explicit `table(path, 2).` declarations for tabled predicates, and advisory `mode/3` declarations for host tooling.
8
8
  It grew out of logic-language experiments in the EYE/N3 reasoning tradition, but is packaged here as its own project.
9
9
 
10
10
  ## Install and run
package/docs/guide.md CHANGED
@@ -568,9 +568,15 @@ Ground facts use a fast path that avoids freshening and copying a rule body. Rec
568
568
  table(path, 2).
569
569
  ```
570
570
 
571
+ Predicates can also carry advisory mode and determinism declarations for documentation and host tooling:
571
572
 
572
- For large programs, keep helper predicates selective, bind arguments early, and declare focused output predicates with `materialize/2` when default output would otherwise solve broad helper goals.
573
+ ```eyelang
574
+ mode(path, 2, [in, out]).
575
+ semidet(edge, 2).
576
+ ```
577
+
578
+ For large programs, keep helper predicates selective, bind arguments early, document intended calling patterns with `mode/3` when helpful, and declare focused output predicates with `materialize/2` when default output would otherwise solve broad helper goals.
573
579
 
574
580
  ## Implementation limits
575
581
 
576
- Eyelang is intentionally smaller than ISO Prolog. It has no operators, zero-arity compound syntax, cut, modules, dynamic database updates, DCGs, or complete ISO library. Negation is negation-as-failure through `not/1`. Search is goal-directed and expected to be finite for the selected output goals. Output explanations are non-normative proof printouts and do not change answer semantics.
582
+ Eyelang is intentionally smaller than ISO Prolog. It has no operators, zero-arity compound syntax, cut, modules, dynamic database updates, DCGs, or complete ISO library. Arity-zero data is always written and read back as an atom, such as `nil`, never `nil()`. Negation is negation-as-failure through `not/1`. Search is goal-directed and expected to be finite for the selected output goals. Output explanations are non-normative proof printouts and do not change answer semantics.
@@ -222,7 +222,7 @@ Arity-zero data is written as an atom constant, not as a zero-arity compound:
222
222
  value(example, nil).
223
223
  ```
224
224
 
225
- The syntax `nil()` is intentionally rejected so eyelang source and read-back output use one representation for arity-zero data.
225
+ The syntax `nil()` is intentionally rejected so eyelang source and read-back output use one representation for arity-zero data. Host APIs SHOULD follow the same rule: constructing a term with an atom name and an empty argument list is canonicalized to the atom constant itself.
226
226
 
227
227
  ## 5. Terms
228
228
 
@@ -473,7 +473,7 @@ Context terms are data representations of atomic formulas and comma conjunctions
473
473
  | `holds(?context, ?name, ?args)` | Enumerates context members of any arity, exposing each member as atom constant `?name` plus a proper argument list `?args`. |
474
474
  | `functor(?term, ?name, ?arity)` | Decomposes a non-variable term into its name and arity. |
475
475
  | `arg(?index, ?term, ?arg)` | Extracts the 1-based argument of a compound term. |
476
- | `compound_name_arguments(?term, ?name, ?args)` | Decomposes a compound term or constructs one from an atom name and proper argument list. |
476
+ | `compound_name_arguments(?term, ?name, ?args)` | Decomposes a compound term, treats an atom as a zero-argument term, or constructs a term from an atom name and proper argument list. Empty `?args` constructs an atom. |
477
477
 
478
478
  Example:
479
479
 
@@ -483,6 +483,7 @@ holds((ready, name(alice, "Alice"), route(alice, bob, 7)), ?name, ?args).
483
483
  functor(route(alice, bob, 7), route, 3).
484
484
  arg(2, route(alice, bob, 7), bob).
485
485
  compound_name_arguments(?term, route, [alice, bob, 7]).
486
+ compound_name_arguments(nil, nil, []).
486
487
  ```
487
488
 
488
489
  The first goal can yield `holds((name(alice, "Alice"), knows(alice, bob)), name(alice, "Alice")).` The second can yield `holds((ready, name(alice, "Alice"), route(alice, bob, 7)), ready, []).`, `holds((ready, name(alice, "Alice"), route(alice, bob, 7)), name, [alice, "Alice"]).`, and `holds((ready, name(alice, "Alice"), route(alice, bob, 7)), route, [alice, bob, 7]).`
@@ -550,6 +551,33 @@ materialize(reason, 2).
550
551
 
551
552
  `materialize/2` affects host output selection only; it does not change the logical meaning of the program. Materialized output facts are not asserted as new source facts for subsequent output goals. A host MAY solve several materialized predicates in one solver run, and tabled predicate answers MAY be reused within that run, but this reuse is controlled by `table/2`, not by materialization.
552
553
 
554
+ ### 11.3 Advisory modes and determinism
555
+
556
+ ```eyelang
557
+ mode(path, 2, [in, out]).
558
+ det(root, 1).
559
+ semidet(edge, 2).
560
+ ```
561
+
562
+ `mode/3`, `det/2`, and `semidet/2` are advisory declarations. They describe a predicate group's intended calling pattern and determinism, but they do not change proof search or answer output. Because they are ordinary facts, programs may also query them.
563
+
564
+ For `mode(?name, ?arity, ?modes)`, the first argument MUST be an atom constant naming the predicate, the second argument MUST be a non-negative integer arity, and the third argument MUST be a proper list whose length is equal to the arity. Portable mode atoms are:
565
+
566
+ - `in`: the argument is expected to be supplied by the caller;
567
+ - `out`: the argument is expected to be produced by the predicate;
568
+ - `any`: no portable mode commitment is made for that argument.
569
+
570
+ `det(?name, ?arity)` declares that a predicate is intended to produce exactly one answer for calls in its documented modes. `semidet(?name, ?arity)` declares that a predicate is intended to produce zero or one answer. This specification does not require runtime enforcement; hosts MAY use these declarations for linting, documentation, indexing decisions, or editor support.
571
+
572
+ Example:
573
+
574
+ ```eyelang
575
+ mode(member, 2, [out, in]).
576
+ semidet(member, 2).
577
+ ```
578
+
579
+ The example documents the common checking/generation mode where the list is supplied and the member is enumerated. A future linting host could warn if a program calls `member/2` outside that intended mode, but a conforming solver still treats `mode/3` and `semidet/2` as ordinary facts plus metadata.
580
+
553
581
  ## 12. Eyelang Sockets
554
582
 
555
583
  A **eyelang Socket** is a declared semantic opening in a eyelang program where facts, rules, tools, datasets, or agents can plug in knowledge through an explicit contract while preserving eyelang-readable reasoning and explanations.
@@ -648,6 +676,7 @@ A conforming eyelang implementation supports the standard language described abo
648
676
  - the standard built-ins listed in section 9;
649
677
  - `table/2` declarations;
650
678
  - `materialize/2` declarations;
679
+ - advisory `mode/3`, `det/2`, and `semidet/2` declarations;
651
680
  - default derived output;
652
681
  - explanation output when the host exposes proof output.
653
682
 
package/index.d.ts CHANGED
@@ -40,6 +40,8 @@ export interface EyelangPredicateGroup {
40
40
  argIndexes: unknown[];
41
41
  pairIndexes: unknown[];
42
42
  tabled: boolean;
43
+ mode: string[] | null;
44
+ determinism: 'det' | 'semidet' | null;
43
45
  recursive: boolean;
44
46
  }
45
47
 
@@ -126,6 +128,7 @@ export function variable(name: string): Term;
126
128
  export function atom(name: string): Term;
127
129
  export function stringTerm(value: string): Term;
128
130
  export function numberTerm(value: string | number): Term;
131
+ /** Construct a compound term; an empty argument list is canonicalized to atom(name). */
129
132
  export function compound(name: string, args?: EyelangTerm[]): Term;
130
133
  export function emptyList(): Term;
131
134
  export function cons(head: EyelangTerm, tail: EyelangTerm): Term;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eyelang",
3
- "version": "1.3.1",
3
+ "version": "1.3.3",
4
4
  "description": "A small Prolog-like logic programming language for rules, goals, answers, and proofs.",
5
5
  "type": "module",
6
6
  "main": "./index.js",
@@ -20,7 +20,7 @@ function argReady(goal, env) {
20
20
 
21
21
  function compoundNameArgumentsReady(goal, env) {
22
22
  const term = deref(goal.args[0], env);
23
- if (term.type === 'compound') return true;
23
+ if (term.type === 'compound' || term.type === 'atom') return true;
24
24
  return term.type === 'var' && lexicalValue(goal.args[1], env) !== null && properListItems(goal.args[2], env) !== null;
25
25
  }
26
26
 
@@ -45,9 +45,10 @@ function* argBuiltin({ goal, env }) {
45
45
 
46
46
  function* compoundNameArguments({ goal, env }) {
47
47
  const term = deref(goal.args[0], env);
48
- if (term.type === 'compound') {
48
+ if (term.type === 'compound' || term.type === 'atom') {
49
49
  const next = env.clone();
50
- if (unify(goal.args[1], atom(term.name), next) && unify(goal.args[2], listFromItems(term.args), next)) yield next;
50
+ const args = term.type === 'compound' ? term.args : [];
51
+ if (unify(goal.args[1], atom(term.name), next) && unify(goal.args[2], listFromItems(args), next)) yield next;
51
52
  return;
52
53
  }
53
54
  if (term.type !== 'var') return;
package/src/program.js CHANGED
@@ -1,6 +1,6 @@
1
1
  // Program representation and clause indexing.
2
2
  // Indexes are deliberately conservative: they speed up common scalar arguments but never replace unification as the final check.
3
- import { ATOM, COMPOUND, Env, compound, deref, flattenConjunction, isScalar, termToString } from './term.js';
3
+ import { ATOM, COMPOUND, Env, compound, deref, flattenConjunction, isScalar, properListItems, termToString } from './term.js';
4
4
  import { parseClauses } from './parser.js';
5
5
 
6
6
  export class Program {
@@ -40,6 +40,8 @@ export class Program {
40
40
  argIndexes: Array.from({ length: arity }, () => ({ buckets: new Map(), fallback: [] })),
41
41
  pairIndexes: [],
42
42
  tabled: false,
43
+ mode: null,
44
+ determinism: null,
43
45
  recursive: false,
44
46
  };
45
47
  if (arity > 2) {
@@ -70,16 +72,31 @@ export class Program {
70
72
  applyDeclarations(options = {}) {
71
73
  for (const clause of this.clauses) {
72
74
  const h = clause.head;
73
- if (clause.body.length !== 0 || h.type !== COMPOUND || h.arity !== 2) continue;
74
- const [name, arity] = h.args;
75
- if (name.type !== ATOM || arity.type !== 'number') continue;
76
- const key = `${name.name}/${Number(arity.name)}`;
77
- if (h.name === 'table') {
78
- const group = this.groups.get(key);
79
- if (group) group.tabled = true;
80
- } else if (h.name === 'materialize') {
81
- this.hasMaterialize = true;
82
- this.materializedGroups.add(key);
75
+ if (clause.body.length !== 0 || h.type !== COMPOUND) continue;
76
+
77
+ if (h.arity === 2) {
78
+ const indicator = declarationIndicator(h.args[0], h.args[1]);
79
+ if (!indicator) continue;
80
+ const group = this.groups.get(indicator.key);
81
+ if (h.name === 'table') {
82
+ if (group) group.tabled = true;
83
+ } else if (h.name === 'materialize') {
84
+ this.hasMaterialize = true;
85
+ this.materializedGroups.add(indicator.key);
86
+ } else if ((h.name === 'det' || h.name === 'semidet') && group) {
87
+ group.determinism = h.name;
88
+ }
89
+ continue;
90
+ }
91
+
92
+ if (h.name === 'mode' && h.arity === 3) {
93
+ const indicator = declarationIndicator(h.args[0], h.args[1]);
94
+ if (!indicator) continue;
95
+ const modes = declarationModes(h.args[2]);
96
+ if (modes && modes.length === indicator.arity) {
97
+ const group = this.groups.get(indicator.key);
98
+ if (group) group.mode = modes;
99
+ }
83
100
  }
84
101
  }
85
102
  if (options.markRecursive !== false) this.markRecursivePredicates();
@@ -159,6 +176,26 @@ export class Program {
159
176
  }
160
177
  }
161
178
 
179
+
180
+ function declarationIndicator(name, arity) {
181
+ if (name?.type !== ATOM || arity?.type !== 'number') return null;
182
+ if (!/^\d+$/.test(arity.name)) return null;
183
+ const arityNumber = Number(arity.name);
184
+ return { name: name.name, arity: arityNumber, key: `${name.name}/${arityNumber}` };
185
+ }
186
+
187
+ function declarationModes(term) {
188
+ const items = properListItems(term, new Env());
189
+ if (!items) return null;
190
+ const modes = [];
191
+ for (const item of items) {
192
+ if (item.type !== ATOM) return null;
193
+ if (!['in', 'out', 'any'].includes(item.name)) return null;
194
+ modes.push(item.name);
195
+ }
196
+ return modes;
197
+ }
198
+
162
199
  function indexOne(index, arg, clause) {
163
200
  if (isScalar(arg)) {
164
201
  const bucket = index.buckets.get(arg.name);
package/src/term.js CHANGED
@@ -21,7 +21,7 @@ export const variable = (name) => new Term(VAR, name);
21
21
  export const atom = (name) => new Term(ATOM, name);
22
22
  export const stringTerm = (value) => new Term(STRING, value);
23
23
  export const numberTerm = (value) => new Term(NUMBER, value);
24
- export const compound = (name, args = []) => new Term(COMPOUND, name, args);
24
+ export const compound = (name, args = []) => args.length === 0 ? atom(name) : new Term(COMPOUND, name, args);
25
25
  export const emptyList = () => atom('[]');
26
26
  export const cons = (head, tail) => compound('.', [head, tail]);
27
27
 
@@ -113,17 +113,20 @@ export function unify(left, right, env) {
113
113
  }
114
114
 
115
115
  export function cloneTerm(term) {
116
+ if (term.type === COMPOUND && term.arity === 0) return atom(term.name);
116
117
  return new Term(term.type, term.name, term.args.map(cloneTerm));
117
118
  }
118
119
 
119
120
  export function freshTerm(term, suffix) {
120
121
  if (term.type === VAR) return variable(`${term.name}#${suffix}`);
122
+ if (term.type === COMPOUND && term.arity === 0) return atom(term.name);
121
123
  return new Term(term.type, term.name, term.args.map((arg) => freshTerm(arg, suffix)));
122
124
  }
123
125
 
124
126
  export function copyResolved(term, env) {
125
127
  const resolved = deref(term, env);
126
128
  if (resolved.type === VAR) return variable(resolved.name);
129
+ if (resolved.type === COMPOUND && resolved.arity === 0) return atom(resolved.name);
127
130
  return new Term(resolved.type, resolved.name, resolved.args.map((arg) => copyResolved(arg, env)));
128
131
  }
129
132
 
@@ -6,5 +6,6 @@ answer(functor_string, pair(?name, ?arity)) :- functor("hi", ?name, ?arity).
6
6
  answer(arg_nested, ?x) :- arg(1, path(edge(a, b), c), ?x).
7
7
  answer(compose_nested, ?x) :- compound_name_arguments(?x, outer, [inner(a), [b, c]]).
8
8
  answer(compose_atom_empty_args, ?x) :- compound_name_arguments(?x, z, []).
9
+ answer(decompose_atom_empty_args, pair(?name, ?args)) :- compound_name_arguments(z, ?name, ?args).
9
10
  answer(arg_zero_rejected, ok) :- not(arg(0, edge(a, b), ?x)).
10
11
  answer(arg_too_large_rejected, ok) :- not(arg(3, edge(a, b), ?x)).
@@ -0,0 +1,11 @@
1
+ % Reference 11.3: mode/3 and det/2 or semidet/2 are ordinary facts and advisory declarations.
2
+ mode(path, 2, [in, out]).
3
+ det(path, 2).
4
+ semidet(edge, 2).
5
+ materialize(answer, 2).
6
+ edge(a, b).
7
+ path(?x, ?y) :- edge(?x, ?y).
8
+ answer(mode_path, ?modes) :- mode(path, 2, ?modes).
9
+ answer(det_path, ok) :- det(path, 2).
10
+ answer(semidet_edge, ok) :- semidet(edge, 2).
11
+ answer(path, ?y) :- path(a, ?y).
@@ -4,5 +4,6 @@ answer(functor_string, pair("hi", 0)).
4
4
  answer(arg_nested, edge(a, b)).
5
5
  answer(compose_nested, outer(inner(a), [b, c])).
6
6
  answer(compose_atom_empty_args, z).
7
+ answer(decompose_atom_empty_args, pair(z, [])).
7
8
  answer(arg_zero_rejected, ok).
8
9
  answer(arg_too_large_rejected, ok).
@@ -0,0 +1,4 @@
1
+ answer(mode_path, [in, out]).
2
+ answer(det_path, ok).
3
+ answer(semidet_edge, ok).
4
+ answer(path, b).
@@ -337,6 +337,18 @@ function apiCases() {
337
337
  },
338
338
  },
339
339
 
340
+ {
341
+ name: 'compound factory canonicalizes zero arity to atoms',
342
+ run: () => {
343
+ const nil = compound('nil', []);
344
+ assertEqual(nil.type, 'atom', 'type');
345
+ assertEqual(nil.name, 'nil', 'name');
346
+ assertEqual(nil.arity, 0, 'arity');
347
+ assertEqual(termToString(nil, new Env(), true), 'nil', 'readback');
348
+ assertEqual(unify(nil, atom('nil'), new Env()), true, 'unifies with atom');
349
+ },
350
+ },
351
+
340
352
  {
341
353
  name: 'portable hash helpers match standard vectors',
342
354
  run: () => {
@@ -616,6 +628,25 @@ function whiteBoxCases() {
616
628
  assertEqual(group.tabled, false, 'memoize/2 is not a table declaration');
617
629
  },
618
630
  },
631
+ {
632
+ name: 'mode and determinism declarations annotate predicate groups',
633
+ run: () => {
634
+ const program = Program.parse('mode(path, 2, [in, out]).\ndet(path, 2).\nedge(a, b).\npath(?x, ?y) :- edge(?x, ?y).\n');
635
+ const group = program.findGroup('path', 2);
636
+ assertEqual(Boolean(group), true, 'path/2 group exists');
637
+ assertEqual(group.mode.join(','), 'in,out', 'path/2 mode');
638
+ assertEqual(group.determinism, 'det', 'path/2 determinism');
639
+ },
640
+ },
641
+ {
642
+ name: 'semidet declaration annotates predicate groups',
643
+ run: () => {
644
+ const program = Program.parse('semidet(edge, 2).\nedge(a, b).\n');
645
+ const group = program.findGroup('edge', 2);
646
+ assertEqual(Boolean(group), true, 'edge/2 group exists');
647
+ assertEqual(group.determinism, 'semidet', 'edge/2 determinism');
648
+ },
649
+ },
619
650
  {
620
651
  name: 'challenging examples keep dynamic-programming predicates tabled',
621
652
  run: () => {