eyelang 1.3.1 → 1.3.2
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 +1 -1
- package/docs/guide.md +7 -1
- package/docs/language-reference.md +28 -0
- package/index.d.ts +2 -0
- package/package.json +1 -1
- package/src/program.js +48 -11
- package/test/conformance/cases/104_mode_determinism_declarations.eye +11 -0
- package/test/conformance/expected/104_mode_determinism_declarations.eye +4 -0
- package/test/run-regression.mjs +19 -0
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
[](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
|
|
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,8 +568,14 @@ 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
|
-
|
|
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
|
|
|
@@ -550,6 +550,33 @@ materialize(reason, 2).
|
|
|
550
550
|
|
|
551
551
|
`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
552
|
|
|
553
|
+
### 11.3 Advisory modes and determinism
|
|
554
|
+
|
|
555
|
+
```eyelang
|
|
556
|
+
mode(path, 2, [in, out]).
|
|
557
|
+
det(root, 1).
|
|
558
|
+
semidet(edge, 2).
|
|
559
|
+
```
|
|
560
|
+
|
|
561
|
+
`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.
|
|
562
|
+
|
|
563
|
+
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:
|
|
564
|
+
|
|
565
|
+
- `in`: the argument is expected to be supplied by the caller;
|
|
566
|
+
- `out`: the argument is expected to be produced by the predicate;
|
|
567
|
+
- `any`: no portable mode commitment is made for that argument.
|
|
568
|
+
|
|
569
|
+
`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.
|
|
570
|
+
|
|
571
|
+
Example:
|
|
572
|
+
|
|
573
|
+
```eyelang
|
|
574
|
+
mode(member, 2, [out, in]).
|
|
575
|
+
semidet(member, 2).
|
|
576
|
+
```
|
|
577
|
+
|
|
578
|
+
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.
|
|
579
|
+
|
|
553
580
|
## 12. Eyelang Sockets
|
|
554
581
|
|
|
555
582
|
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 +675,7 @@ A conforming eyelang implementation supports the standard language described abo
|
|
|
648
675
|
- the standard built-ins listed in section 9;
|
|
649
676
|
- `table/2` declarations;
|
|
650
677
|
- `materialize/2` declarations;
|
|
678
|
+
- advisory `mode/3`, `det/2`, and `semidet/2` declarations;
|
|
651
679
|
- default derived output;
|
|
652
680
|
- explanation output when the host exposes proof output.
|
|
653
681
|
|
package/index.d.ts
CHANGED
package/package.json
CHANGED
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
|
|
74
|
-
|
|
75
|
-
if (
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
const group = this.groups.get(key);
|
|
79
|
-
if (
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
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);
|
|
@@ -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).
|
package/test/run-regression.mjs
CHANGED
|
@@ -616,6 +616,25 @@ function whiteBoxCases() {
|
|
|
616
616
|
assertEqual(group.tabled, false, 'memoize/2 is not a table declaration');
|
|
617
617
|
},
|
|
618
618
|
},
|
|
619
|
+
{
|
|
620
|
+
name: 'mode and determinism declarations annotate predicate groups',
|
|
621
|
+
run: () => {
|
|
622
|
+
const program = Program.parse('mode(path, 2, [in, out]).\ndet(path, 2).\nedge(a, b).\npath(?x, ?y) :- edge(?x, ?y).\n');
|
|
623
|
+
const group = program.findGroup('path', 2);
|
|
624
|
+
assertEqual(Boolean(group), true, 'path/2 group exists');
|
|
625
|
+
assertEqual(group.mode.join(','), 'in,out', 'path/2 mode');
|
|
626
|
+
assertEqual(group.determinism, 'det', 'path/2 determinism');
|
|
627
|
+
},
|
|
628
|
+
},
|
|
629
|
+
{
|
|
630
|
+
name: 'semidet declaration annotates predicate groups',
|
|
631
|
+
run: () => {
|
|
632
|
+
const program = Program.parse('semidet(edge, 2).\nedge(a, b).\n');
|
|
633
|
+
const group = program.findGroup('edge', 2);
|
|
634
|
+
assertEqual(Boolean(group), true, 'edge/2 group exists');
|
|
635
|
+
assertEqual(group.determinism, 'semidet', 'edge/2 determinism');
|
|
636
|
+
},
|
|
637
|
+
},
|
|
619
638
|
{
|
|
620
639
|
name: 'challenging examples keep dynamic-programming predicates tabled',
|
|
621
640
|
run: () => {
|