eyemind 0.9.1 → 0.9.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/LE-SPEC.md +198 -27
- package/LS-SPEC.md +1 -1
- package/README.md +6 -4
- package/package.json +1 -1
- package/src/engine.js +257 -60
- package/src/formats.js +14 -7
- package/src/le.js +991 -88
- package/src/program.js +7 -0
- package/src/terms.js +236 -0
- package/src/version.js +1 -1
package/src/program.js
CHANGED
|
@@ -36,6 +36,7 @@ export class Clause {
|
|
|
36
36
|
this.scopeIds = [];
|
|
37
37
|
this.extraTerms = [];
|
|
38
38
|
this.renaming = null; // how to rename it apart, worked out once it is used often
|
|
39
|
+
this.citation = null; // clause(Id), cited by the facts it derives, made once it derives one
|
|
39
40
|
this.uses = 0;
|
|
40
41
|
this.witnessed = null;
|
|
41
42
|
}
|
|
@@ -501,8 +502,14 @@ function stratify(clauses, stratifying) {
|
|
|
501
502
|
const named = !(depGoal instanceof Var);
|
|
502
503
|
if (!named) dynamic.add(clause.id);
|
|
503
504
|
const candidates = named ? headCandidates(index.get(key(depGoal)), depGoal) : everyHead;
|
|
505
|
+
// A source fact depends on nothing, so its rank stays 0: one matching
|
|
506
|
+
// fact ranks the goal as all of them would.
|
|
507
|
+
let factMatched = false;
|
|
504
508
|
for (const [candidateClause, head] of candidates) {
|
|
509
|
+
const fact = !candidateClause.forward && !candidateClause.body.length && candidateClause.parentId === null;
|
|
510
|
+
if (fact && factMatched) continue;
|
|
505
511
|
if (unify(freshTerm(depGoal, 'dependency'), freshTerm(head, 'head'), new Env())) {
|
|
512
|
+
if (fact) factMatched = true;
|
|
506
513
|
const edge = [clause.id, candidateClause.id, closed];
|
|
507
514
|
edges.push(edge);
|
|
508
515
|
if (!outgoing.has(clause.id)) outgoing.set(clause.id, []);
|
package/src/terms.js
CHANGED
|
@@ -42,6 +42,9 @@ export class Struct {
|
|
|
42
42
|
constructor(name, args) {
|
|
43
43
|
this.name = name;
|
|
44
44
|
this.args = args;
|
|
45
|
+
// The number the TermIds numbering gave this term, which never changes.
|
|
46
|
+
this.numbering = null;
|
|
47
|
+
this.number = -1;
|
|
45
48
|
}
|
|
46
49
|
|
|
47
50
|
*[Symbol.iterator]() {
|
|
@@ -278,6 +281,39 @@ export function unify(left, right, env) {
|
|
|
278
281
|
return true;
|
|
279
282
|
}
|
|
280
283
|
|
|
284
|
+
// Unify a term with a ground one, such as a fact. No variable can occur in a
|
|
285
|
+
// ground term, so this skips the occurs check, and every binding it makes
|
|
286
|
+
// is to a part of the ground term.
|
|
287
|
+
export function match(term, ground, env) {
|
|
288
|
+
const pending = [term, ground];
|
|
289
|
+
while (pending.length) {
|
|
290
|
+
const b = pending.pop();
|
|
291
|
+
let a = pending.pop();
|
|
292
|
+
if (a instanceof Var) a = deref(a, env);
|
|
293
|
+
if (a === b) continue;
|
|
294
|
+
if (a instanceof Var) {
|
|
295
|
+
env.bind(a, b);
|
|
296
|
+
} else if (a instanceof Struct) {
|
|
297
|
+
if (!(b instanceof Struct) || a.name !== b.name || a.args.length !== b.args.length) return false;
|
|
298
|
+
const argsA = a.args;
|
|
299
|
+
const argsB = b.args;
|
|
300
|
+
for (let i = argsA.length - 1; i >= 0; i--) {
|
|
301
|
+
const argA = argsA[i];
|
|
302
|
+
const argB = argsB[i];
|
|
303
|
+
if (argA === argB) continue;
|
|
304
|
+
if (!(argA instanceof Var) && !(argA instanceof Struct)) {
|
|
305
|
+
if (!sameConstant(argA, argB)) return false;
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
pending.push(argA, argB);
|
|
309
|
+
}
|
|
310
|
+
} else if (b instanceof Struct || !sameConstant(a, b)) {
|
|
311
|
+
return false;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
return true;
|
|
315
|
+
}
|
|
316
|
+
|
|
281
317
|
// Rename a term's variables apart. A subterm without variables is shared.
|
|
282
318
|
// unique says no other variable of the run gets this suffix (see Var).
|
|
283
319
|
export function freshTerm(term, suffix, names = new Map(), unique = false) {
|
|
@@ -482,6 +518,206 @@ export function identity(term) {
|
|
|
482
518
|
return out.join('\u0001');
|
|
483
519
|
}
|
|
484
520
|
|
|
521
|
+
// Small integers for ground terms, so that two terms get one number exactly
|
|
522
|
+
// when identity() keys them alike: hash-consing. Atoms and numbers are kept
|
|
523
|
+
// in maps; a compound is a row of name, arity and argument numbers in typed
|
|
524
|
+
// arrays, found again through an open-addressing table of row numbers, so
|
|
525
|
+
// keying a fact builds no string and no object. Integers, floats, 0.0 and
|
|
526
|
+
// -0.0 stay apart. A term holding a variable has no number: -1.
|
|
527
|
+
export class TermIds {
|
|
528
|
+
constructor() {
|
|
529
|
+
this.count = 0;
|
|
530
|
+
this.atoms = new Map();
|
|
531
|
+
this.ints = new Map();
|
|
532
|
+
this.floats = new Map();
|
|
533
|
+
this.negativeZero = -1;
|
|
534
|
+
// Rows of compounds, by number: name, arity and where their arguments
|
|
535
|
+
// start in args. Atoms and numbers have rows too, left unused.
|
|
536
|
+
this.names = new Int32Array(1024);
|
|
537
|
+
this.arities = new Int32Array(1024);
|
|
538
|
+
this.starts = new Int32Array(1024);
|
|
539
|
+
this.args = new Int32Array(4096);
|
|
540
|
+
this.used = 0;
|
|
541
|
+
// The table holds number + 1 of each compound, 0 marking a free slot.
|
|
542
|
+
this.table = new Int32Array(2048);
|
|
543
|
+
this.filled = 0;
|
|
544
|
+
this.stack = [];
|
|
545
|
+
this.ids = [];
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
next() {
|
|
549
|
+
const id = this.count++;
|
|
550
|
+
if (id === this.names.length) {
|
|
551
|
+
this.names = grown(this.names);
|
|
552
|
+
this.arities = grown(this.arities);
|
|
553
|
+
this.starts = grown(this.starts);
|
|
554
|
+
}
|
|
555
|
+
this.arities[id] = -1;
|
|
556
|
+
return id;
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
leaf(term) {
|
|
560
|
+
let map;
|
|
561
|
+
if (typeof term === 'string') map = this.atoms;
|
|
562
|
+
else if (typeof term === 'bigint') map = this.ints;
|
|
563
|
+
else if (Object.is(term, -0)) return this.negativeZero < 0 ? (this.negativeZero = this.next()) : this.negativeZero;
|
|
564
|
+
else map = this.floats;
|
|
565
|
+
let id = map.get(term);
|
|
566
|
+
if (id === undefined) map.set(term, id = this.next());
|
|
567
|
+
return id;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
// The table slot of a compound with this name number and these argument
|
|
571
|
+
// numbers, the last arity entries of ids: the slot holding it, or the free
|
|
572
|
+
// slot it would take.
|
|
573
|
+
slot(nameId, arity, ids) {
|
|
574
|
+
const base = ids.length - arity;
|
|
575
|
+
let hash = Math.imul(nameId ^ 0x9e3779b9, 0x85ebca6b) ^ arity;
|
|
576
|
+
for (let i = base; i < ids.length; i++) hash = Math.imul(hash ^ ids[i], 0x01000193);
|
|
577
|
+
const mask = this.table.length - 1;
|
|
578
|
+
let slot = (hash ^ (hash >>> 15)) & mask;
|
|
579
|
+
for (let entry; (entry = this.table[slot]) !== 0; slot = (slot + 1) & mask) {
|
|
580
|
+
const id = entry - 1;
|
|
581
|
+
if (this.names[id] !== nameId || this.arities[id] !== arity) continue;
|
|
582
|
+
const start = this.starts[id];
|
|
583
|
+
let same = true;
|
|
584
|
+
for (let i = 0; i < arity && same; i++) same = this.args[start + i] === ids[base + i];
|
|
585
|
+
if (same) return slot;
|
|
586
|
+
}
|
|
587
|
+
return slot;
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
// The number of a compound with this name and these argument numbers,
|
|
591
|
+
// the last arity entries of ids, which it takes off.
|
|
592
|
+
compound(name, arity, ids) {
|
|
593
|
+
const nameId = this.leaf(name);
|
|
594
|
+
const base = ids.length - arity;
|
|
595
|
+
const slot = this.slot(nameId, arity, ids);
|
|
596
|
+
if (this.table[slot] !== 0) {
|
|
597
|
+
ids.length = base;
|
|
598
|
+
return this.table[slot] - 1;
|
|
599
|
+
}
|
|
600
|
+
const id = this.next();
|
|
601
|
+
this.names[id] = nameId;
|
|
602
|
+
this.arities[id] = arity;
|
|
603
|
+
while (this.used + arity > this.args.length) this.args = grown(this.args);
|
|
604
|
+
this.starts[id] = this.used;
|
|
605
|
+
for (let i = base; i < ids.length; i++) this.args[this.used++] = ids[i];
|
|
606
|
+
ids.length = base;
|
|
607
|
+
this.table[slot] = id + 1;
|
|
608
|
+
if (++this.filled * 2 > this.table.length) this.rehash();
|
|
609
|
+
return id;
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
rehash() {
|
|
613
|
+
const table = new Int32Array(this.table.length * 2);
|
|
614
|
+
const mask = table.length - 1;
|
|
615
|
+
for (const entry of this.table) {
|
|
616
|
+
if (entry === 0) continue;
|
|
617
|
+
const id = entry - 1;
|
|
618
|
+
const start = this.starts[id];
|
|
619
|
+
let hash = Math.imul(this.names[id] ^ 0x9e3779b9, 0x85ebca6b) ^ this.arities[id];
|
|
620
|
+
for (let i = 0; i < this.arities[id]; i++) hash = Math.imul(hash ^ this.args[start + i], 0x01000193);
|
|
621
|
+
let slot = (hash ^ (hash >>> 15)) & mask;
|
|
622
|
+
while (table[slot] !== 0) slot = (slot + 1) & mask;
|
|
623
|
+
table[slot] = entry;
|
|
624
|
+
}
|
|
625
|
+
this.table = table;
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
// The number a term has, read through env, without numbering anything: -1
|
|
629
|
+
// when it holds an unbound variable, -2 when it has no number yet.
|
|
630
|
+
find(term, env) {
|
|
631
|
+
const { stack, ids } = this;
|
|
632
|
+
stack.length = 0;
|
|
633
|
+
ids.length = 0;
|
|
634
|
+
stack.push(term, false);
|
|
635
|
+
while (stack.length) {
|
|
636
|
+
const done = stack.pop();
|
|
637
|
+
let item = stack.pop();
|
|
638
|
+
if (done) {
|
|
639
|
+
const nameId = this.atoms.get(item.name);
|
|
640
|
+
const arity = item.args.length;
|
|
641
|
+
const slot = nameId === undefined ? -1 : this.slot(nameId, arity, ids);
|
|
642
|
+
if (slot < 0 || this.table[slot] === 0) {
|
|
643
|
+
stack.length = 0;
|
|
644
|
+
return -2;
|
|
645
|
+
}
|
|
646
|
+
ids.length -= arity;
|
|
647
|
+
ids.push(this.table[slot] - 1);
|
|
648
|
+
continue;
|
|
649
|
+
}
|
|
650
|
+
if (item instanceof Var) {
|
|
651
|
+
item = deref(item, env);
|
|
652
|
+
if (item instanceof Var) {
|
|
653
|
+
stack.length = 0;
|
|
654
|
+
return -1;
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
if (item instanceof Struct) {
|
|
658
|
+
if (item.numbering === this) {
|
|
659
|
+
ids.push(item.number);
|
|
660
|
+
continue;
|
|
661
|
+
}
|
|
662
|
+
stack.push(item, true);
|
|
663
|
+
for (let i = item.args.length - 1; i >= 0; i--) stack.push(item.args[i], false);
|
|
664
|
+
continue;
|
|
665
|
+
}
|
|
666
|
+
let id;
|
|
667
|
+
if (typeof item === 'string') id = this.atoms.get(item);
|
|
668
|
+
else if (typeof item === 'bigint') id = this.ints.get(item);
|
|
669
|
+
else if (Object.is(item, -0)) id = this.negativeZero < 0 ? undefined : this.negativeZero;
|
|
670
|
+
else id = this.floats.get(item);
|
|
671
|
+
if (id === undefined) {
|
|
672
|
+
stack.length = 0;
|
|
673
|
+
return -2;
|
|
674
|
+
}
|
|
675
|
+
ids.push(id);
|
|
676
|
+
}
|
|
677
|
+
return ids.pop();
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
// The number of a ground term, or -1 when it holds a variable. Compounds
|
|
681
|
+
// are numbered after their arguments, without recursion, so a long list
|
|
682
|
+
// cannot overflow the stack.
|
|
683
|
+
of(term) {
|
|
684
|
+
if (!(term instanceof Struct)) return term instanceof Var ? -1 : this.leaf(term);
|
|
685
|
+
const { stack, ids } = this;
|
|
686
|
+
stack.length = 0;
|
|
687
|
+
ids.length = 0;
|
|
688
|
+
stack.push(term, false);
|
|
689
|
+
while (stack.length) {
|
|
690
|
+
const done = stack.pop();
|
|
691
|
+
const item = stack.pop();
|
|
692
|
+
if (done) {
|
|
693
|
+
const id = this.compound(item.name, item.args.length, ids);
|
|
694
|
+
item.numbering = this;
|
|
695
|
+
item.number = id;
|
|
696
|
+
ids.push(id);
|
|
697
|
+
} else if (item instanceof Struct) {
|
|
698
|
+
if (item.numbering === this) {
|
|
699
|
+
ids.push(item.number);
|
|
700
|
+
continue;
|
|
701
|
+
}
|
|
702
|
+
stack.push(item, true);
|
|
703
|
+
for (let i = item.args.length - 1; i >= 0; i--) stack.push(item.args[i], false);
|
|
704
|
+
} else if (item instanceof Var) {
|
|
705
|
+
stack.length = 0;
|
|
706
|
+
return -1;
|
|
707
|
+
} else {
|
|
708
|
+
ids.push(this.leaf(item));
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
return ids.pop();
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
function grown(array) {
|
|
716
|
+
const larger = new Int32Array(array.length * 2);
|
|
717
|
+
larger.set(array);
|
|
718
|
+
return larger;
|
|
719
|
+
}
|
|
720
|
+
|
|
485
721
|
// The key an argument index files a term under: an atom or a number is its
|
|
486
722
|
// own key, an IRI uses its string, and another small ground compound the text
|
|
487
723
|
// of its structure. Signed zeros share a key, as they unify. A variable, a
|
package/src/version.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// The release this checkout is; ./release keeps it in step with package.json.
|
|
2
|
-
export const VERSION = '0.9.
|
|
2
|
+
export const VERSION = '0.9.3';
|