factbag 0.0.0-alpha.0

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.
Files changed (77) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/LICENSE +202 -0
  3. package/README.md +44 -0
  4. package/dist/core/chunk-4IP7CXNV.js +86 -0
  5. package/dist/core/chunk-UVOCOH2I.js +41 -0
  6. package/dist/core/chunk-X3LSDJL4.js +59 -0
  7. package/dist/core/chunk-YF4PP42K.js +8 -0
  8. package/dist/core/dsl/enumeration.d.ts +3 -0
  9. package/dist/core/dsl/formula.d.ts +62 -0
  10. package/dist/core/dsl/index.d.ts +21 -0
  11. package/dist/core/dsl/index.js +132 -0
  12. package/dist/core/dsl/operators/aggregate.d.ts +30 -0
  13. package/dist/core/dsl/operators/arithmetic.d.ts +27 -0
  14. package/dist/core/dsl/operators/comparison.d.ts +13 -0
  15. package/dist/core/dsl/predicate.d.ts +33 -0
  16. package/dist/core/dsl/types.d.ts +59 -0
  17. package/dist/core/dsl/variable.d.ts +3 -0
  18. package/dist/core/exceptions/programmer-errors.d.ts +36 -0
  19. package/dist/core/explanation/explain.d.ts +20 -0
  20. package/dist/core/explanation/index.d.ts +8 -0
  21. package/dist/core/explanation/index.js +187 -0
  22. package/dist/core/explanation/types.d.ts +172 -0
  23. package/dist/core/knowledge-base/assertions/exclusive-definitions.d.ts +3 -0
  24. package/dist/core/knowledge-base/index.d.ts +13 -0
  25. package/dist/core/knowledge-base/index.js +196 -0
  26. package/dist/core/knowledge-base/knowledge-base.d.ts +81 -0
  27. package/dist/core/query/index.d.ts +10 -0
  28. package/dist/core/query/index.js +1947 -0
  29. package/dist/core/query/query.d.ts +47 -0
  30. package/dist/core/solver/candidate-search.d.ts +12 -0
  31. package/dist/core/solver/goal-value.d.ts +6 -0
  32. package/dist/core/solver/matching.d.ts +10 -0
  33. package/dist/core/solver/query-validation/constructive-recursion.d.ts +3 -0
  34. package/dist/core/solver/query-validation/constructive-variable-use.d.ts +3 -0
  35. package/dist/core/solver/query-validation/context.d.ts +10 -0
  36. package/dist/core/solver/query-validation/index.d.ts +1 -0
  37. package/dist/core/solver/query-validation/utils.d.ts +27 -0
  38. package/dist/core/solver/query-validation/validate-query.d.ts +6 -0
  39. package/dist/core/solver/solve-all.d.ts +6 -0
  40. package/dist/core/solver/solve.d.ts +6 -0
  41. package/dist/core/types/annotations.d.ts +6 -0
  42. package/dist/core/types/atom.d.ts +55 -0
  43. package/dist/core/types/common.d.ts +7 -0
  44. package/dist/core/types/evidence-metadata.d.ts +18 -0
  45. package/dist/core/types/goal.d.ts +103 -0
  46. package/dist/core/types/judgment.d.ts +137 -0
  47. package/dist/core/types/literal.d.ts +25 -0
  48. package/dist/core/types/predicate.d.ts +35 -0
  49. package/dist/core/types/rule.d.ts +16 -0
  50. package/dist/core/types/schema.d.ts +33 -0
  51. package/dist/core/types/variable.d.ts +13 -0
  52. package/dist/core/utils/assert.d.ts +4 -0
  53. package/dist/core/utils/normalize-to-list.d.ts +2 -0
  54. package/dist/corpus/chunkers.d.ts +7 -0
  55. package/dist/corpus/corpus.d.ts +41 -0
  56. package/dist/corpus/index.d.ts +8 -0
  57. package/dist/corpus/index.js +312 -0
  58. package/dist/corpus/records.d.ts +8 -0
  59. package/dist/corpus/text.d.ts +6 -0
  60. package/dist/corpus/types.d.ts +62 -0
  61. package/dist/guards/guarded-kb.d.ts +59 -0
  62. package/dist/guards/index.d.ts +7 -0
  63. package/dist/guards/index.js +121 -0
  64. package/dist/guards/types.d.ts +15 -0
  65. package/dist/jev/chunk-2UXHML67.js +206 -0
  66. package/dist/jev/chunk-M7RHUI2O.js +15 -0
  67. package/dist/jev/classifier.d.ts +28 -0
  68. package/dist/jev/classifier.js +171 -0
  69. package/dist/jev/client.d.ts +62 -0
  70. package/dist/jev/errors.d.ts +22 -0
  71. package/dist/jev/errors.js +6 -0
  72. package/dist/jev/jobs.d.ts +61 -0
  73. package/dist/jev/resolve.d.ts +82 -0
  74. package/dist/jev/resolve.js +99 -0
  75. package/dist/jev/tools.d.ts +9 -0
  76. package/dist/jev/types.d.ts +63 -0
  77. package/package.json +92 -0
@@ -0,0 +1,196 @@
1
+ import {
2
+ ExclusiveDefinitionProgrammerError,
3
+ PrimitiveArithmeticProgrammerError,
4
+ ProgrammerError,
5
+ UnsafeUnboundVariableProgrammerError
6
+ } from "../chunk-X3LSDJL4.js";
7
+ import {
8
+ isAtomLike
9
+ } from "../chunk-4IP7CXNV.js";
10
+ import "../chunk-UVOCOH2I.js";
11
+
12
+ // src/knowledge-base/assertions/exclusive-definitions.ts
13
+ function assertExclusiveDefinitions(rules) {
14
+ const rulesByPredicate = /* @__PURE__ */ new Map();
15
+ for (const rule of rules) {
16
+ const predicate = rule.head.atom.predicate;
17
+ const predicateRules = rulesByPredicate.get(predicate) ?? [];
18
+ predicateRules.push(rule);
19
+ rulesByPredicate.set(predicate, predicateRules);
20
+ }
21
+ for (const [predicate, predicateRules] of rulesByPredicate) {
22
+ if (predicateRules.some((rule) => rule.kind === "iff") && predicateRules.length > 1) {
23
+ throw new ExclusiveDefinitionProgrammerError(predicate, predicateRules);
24
+ }
25
+ }
26
+ }
27
+
28
+ // src/knowledge-base/knowledge-base.ts
29
+ var EntityRegistry = class {
30
+ entitiesByType = /* @__PURE__ */ new Map();
31
+ /** Register entities so queries can enumerate and resolve them by id. */
32
+ add(...entities) {
33
+ for (const entity of entities) {
34
+ const type = entity.constructor;
35
+ if (!isEntityConstructor(type)) {
36
+ continue;
37
+ }
38
+ let entitiesById = this.entitiesByType.get(type);
39
+ if (entitiesById === void 0) {
40
+ entitiesById = /* @__PURE__ */ new Map();
41
+ this.entitiesByType.set(type, entitiesById);
42
+ }
43
+ entitiesById.set(entity.id, entity);
44
+ }
45
+ }
46
+ /** Look up a registered entity by its type and id. */
47
+ get(type, id) {
48
+ if (!isEntityConstructor(type)) {
49
+ return void 0;
50
+ }
51
+ return this.entitiesByType.get(type)?.get(id);
52
+ }
53
+ /** List the ids of every registered entity of a type. */
54
+ ids(type) {
55
+ if (!isEntityConstructor(type)) {
56
+ return [];
57
+ }
58
+ return [...this.entitiesByType.get(type)?.keys() ?? []];
59
+ }
60
+ /** List all registered entities, retaining their original identities. */
61
+ all() {
62
+ return [...this.entitiesByType.values()].flatMap((entities) => [...entities.values()]);
63
+ }
64
+ };
65
+ function isEntityConstructor(type) {
66
+ return typeof type === "function" && type !== String && type !== Number && type !== Boolean && type !== BigInt && "prototype" in type && typeof type.name === "string";
67
+ }
68
+ var KnowledgeBase = class _KnowledgeBase {
69
+ /** Registry of known domain entities, used to enumerate closed domains. */
70
+ entities = new EntityRegistry();
71
+ storedLiterals = [];
72
+ storedRules = [];
73
+ /**
74
+ * Create an independent store with the same evidence, rules, and registered entities.
75
+ * Existing records retain their identities; additions and retractions affect only
76
+ * their own store. This is a structural copy, not a deep clone of domain objects.
77
+ */
78
+ fork() {
79
+ const fork = new _KnowledgeBase();
80
+ fork.entities.add(...this.entities.all());
81
+ fork.storedLiterals.push(...this.storedLiterals);
82
+ fork.storedRules.push(...this.storedRules);
83
+ return fork;
84
+ }
85
+ /**
86
+ * Accept positive evidence for an atom, returning the stored literal.
87
+ * Optional {@link EvidenceMetadata} records where the fact came from.
88
+ *
89
+ * @example
90
+ * ```ts
91
+ * kb.asserts(incomeFact({ person: alice, year: y2024, amountCents: 24_680_000 }), {
92
+ * source: "2024_w2",
93
+ * citation: "Wages, tips, other compensation: 246,800",
94
+ * });
95
+ * ```
96
+ */
97
+ asserts(atom, metadata) {
98
+ const literal = metadata === void 0 ? { sign: "+", atom } : { sign: "+", atom, metadata };
99
+ this.storedLiterals.push(literal);
100
+ return literal;
101
+ }
102
+ /**
103
+ * Accept negative evidence for an atom, returning the stored literal. A
104
+ * denial is explicit counter-evidence — stronger than mere absence.
105
+ */
106
+ denies(atom, metadata) {
107
+ const literal = metadata === void 0 ? { sign: "-", atom } : { sign: "-", atom, metadata };
108
+ this.storedLiterals.push(literal);
109
+ return literal;
110
+ }
111
+ /**
112
+ * Start declaring a rule for a head atom; finish with the builder's `if`
113
+ * (sufficient condition) or `iff` (exclusive definition).
114
+ *
115
+ * @throws {@link ExclusiveDefinitionProgrammerError} when the head
116
+ * predicate ends up with an `iff` definition alongside any other rule.
117
+ *
118
+ * @example
119
+ * ```ts
120
+ * kb.rule(doctor({ person: P })).iff(
121
+ * and(passedMedicalSchool({ person: P }), hasActiveLicense({ person: P })),
122
+ * );
123
+ * ```
124
+ */
125
+ rule(head) {
126
+ return {
127
+ if: (body) => this.addRule(head, "if", body),
128
+ iff: (body) => this.addRule(head, "iff", body)
129
+ };
130
+ }
131
+ /** Remove previously stored literals, matched by identity. */
132
+ retract(...literals) {
133
+ for (const literal of literals) {
134
+ const index = this.storedLiterals.indexOf(literal);
135
+ if (index !== -1) {
136
+ this.storedLiterals.splice(index, 1);
137
+ }
138
+ }
139
+ }
140
+ /** List stored evidence literals, optionally filtered. */
141
+ literals(filter) {
142
+ return filter === void 0 ? [...this.storedLiterals] : this.storedLiterals.filter(filter);
143
+ }
144
+ /** List stored rules. */
145
+ rules() {
146
+ return [...this.storedRules];
147
+ }
148
+ /** Predicates referenced by this KB's facts and rules, in discovery order. */
149
+ predicates() {
150
+ const predicates = /* @__PURE__ */ new Set();
151
+ const visited = /* @__PURE__ */ new Set();
152
+ function visit(value) {
153
+ if (typeof value !== "object" || value === null || visited.has(value)) {
154
+ return;
155
+ }
156
+ visited.add(value);
157
+ if (isAtomLike(value)) {
158
+ predicates.add(value.predicate);
159
+ return;
160
+ }
161
+ for (const child of Object.values(value)) {
162
+ visit(child);
163
+ }
164
+ }
165
+ for (const literal of this.storedLiterals) {
166
+ predicates.add(literal.atom.predicate);
167
+ }
168
+ for (const rule of this.storedRules) {
169
+ predicates.add(rule.head.atom.predicate);
170
+ visit(rule.body);
171
+ }
172
+ return [...predicates];
173
+ }
174
+ addRule(head, kind, body) {
175
+ const rule = { kind, head: { sign: "+", atom: head }, body };
176
+ this.storedRules.push(rule);
177
+ try {
178
+ this.assertValid();
179
+ } catch (error) {
180
+ this.storedRules.pop();
181
+ throw error;
182
+ }
183
+ return rule;
184
+ }
185
+ assertValid() {
186
+ assertExclusiveDefinitions(this.storedRules);
187
+ }
188
+ };
189
+ export {
190
+ EntityRegistry,
191
+ ExclusiveDefinitionProgrammerError,
192
+ KnowledgeBase,
193
+ PrimitiveArithmeticProgrammerError,
194
+ ProgrammerError,
195
+ UnsafeUnboundVariableProgrammerError
196
+ };
@@ -0,0 +1,81 @@
1
+ import type { Atom } from "../types/atom.js";
2
+ import type { PredicateDeclaration } from "../types/predicate.js";
3
+ import type { EvidenceMetadata } from "../types/evidence-metadata.js";
4
+ import type { AnyLiteral, NegativeLiteral, PositiveLiteral } from "../types/literal.js";
5
+ import type { Entity, PredicateSchema, RuntimeType } from "../types/schema.js";
6
+ import type { AnyRule } from "../types/rule.js";
7
+ import type { LiteralArgs, LiteralFilter, RuleBuilder } from "../dsl/types.js";
8
+ /** Registry of known domain entities, keyed by constructor and id. */
9
+ export declare class EntityRegistry {
10
+ private readonly entitiesByType;
11
+ /** Register entities so queries can enumerate and resolve them by id. */
12
+ add(...entities: readonly Entity[]): void;
13
+ /** Look up a registered entity by its type and id. */
14
+ get(type: RuntimeType<unknown>, id: string): Entity | undefined;
15
+ /** List the ids of every registered entity of a type. */
16
+ ids(type: RuntimeType<unknown>): readonly string[];
17
+ /** List all registered entities, retaining their original identities. */
18
+ all(): readonly Entity[];
19
+ }
20
+ /**
21
+ * The store of accepted evidence and derivation rules.
22
+ *
23
+ * Evidence enters as signed literals via {@link KnowledgeBase.asserts} and
24
+ * {@link KnowledgeBase.denies}; rules enter via the fluent
25
+ * {@link KnowledgeBase.rule} builder. Queries evaluate against this store.
26
+ */
27
+ export declare class KnowledgeBase {
28
+ /** Registry of known domain entities, used to enumerate closed domains. */
29
+ readonly entities: EntityRegistry;
30
+ private readonly storedLiterals;
31
+ private readonly storedRules;
32
+ /**
33
+ * Create an independent store with the same evidence, rules, and registered entities.
34
+ * Existing records retain their identities; additions and retractions affect only
35
+ * their own store. This is a structural copy, not a deep clone of domain objects.
36
+ */
37
+ fork(): KnowledgeBase;
38
+ /**
39
+ * Accept positive evidence for an atom, returning the stored literal.
40
+ * Optional {@link EvidenceMetadata} records where the fact came from.
41
+ *
42
+ * @example
43
+ * ```ts
44
+ * kb.asserts(incomeFact({ person: alice, year: y2024, amountCents: 24_680_000 }), {
45
+ * source: "2024_w2",
46
+ * citation: "Wages, tips, other compensation: 246,800",
47
+ * });
48
+ * ```
49
+ */
50
+ asserts<Schema extends PredicateSchema>(atom: Atom<Schema>, metadata?: EvidenceMetadata): PositiveLiteral<Schema>;
51
+ /**
52
+ * Accept negative evidence for an atom, returning the stored literal. A
53
+ * denial is explicit counter-evidence — stronger than mere absence.
54
+ */
55
+ denies<Schema extends PredicateSchema>(atom: Atom<Schema>, metadata?: EvidenceMetadata): NegativeLiteral<Schema>;
56
+ /**
57
+ * Start declaring a rule for a head atom; finish with the builder's `if`
58
+ * (sufficient condition) or `iff` (exclusive definition).
59
+ *
60
+ * @throws {@link ExclusiveDefinitionProgrammerError} when the head
61
+ * predicate ends up with an `iff` definition alongside any other rule.
62
+ *
63
+ * @example
64
+ * ```ts
65
+ * kb.rule(doctor({ person: P })).iff(
66
+ * and(passedMedicalSchool({ person: P }), hasActiveLicense({ person: P })),
67
+ * );
68
+ * ```
69
+ */
70
+ rule<Schema extends PredicateSchema>(head: Atom<Schema>): RuleBuilder<Schema>;
71
+ /** Remove previously stored literals, matched by identity. */
72
+ retract<const Literals extends readonly unknown[]>(...literals: Literals & LiteralArgs<Literals>): void;
73
+ /** List stored evidence literals, optionally filtered. */
74
+ literals(filter?: LiteralFilter): readonly AnyLiteral[];
75
+ /** List stored rules. */
76
+ rules(): readonly AnyRule[];
77
+ /** Predicates referenced by this KB's facts and rules, in discovery order. */
78
+ predicates(): readonly PredicateDeclaration[];
79
+ private addRule;
80
+ private assertValid;
81
+ }
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Query evaluation: four-valued judgments over ground atoms, carrying full
3
+ * proof-support trees that record how each value was reached.
4
+ *
5
+ * @packageDocumentation
6
+ */
7
+ export { query, queryAll } from "./query.js";
8
+ export type { Binding, Bindings, GoalValue } from "../types/common.js";
9
+ export type { AddGoal, AndGoal, AnyAtomGoal, ArithmeticGoal, AtomGoal, ComparisonGoal, CompoundGoal, CountGoal, DivGoal, EqGoal, ExistsGoal, Goal, GoalExpression, GroundAtomGoal, GtGoal, GteGoal, LetGoal, LtGoal, LteGoal, MulGoal, NeqGoal, NotGoal, OrGoal, PrimitiveGoal, SubGoal, SumGoal, } from "../types/goal.js";
10
+ export type { ArithmeticPrimitiveSupport, ClosedWorldAbsenceSupport, ComparisonPrimitiveSupport, CountPrimitiveSupport, ErasedEvidenceSupport, ErasedRuleSupport, EvidenceSupport, Judgment, JudgmentBase, JudgmentSupportFor, MissingBinding, NumericRange, PrimitiveEvaluation, PrimitiveSupport, PrimitiveSupportFor, PrimitiveUncertainty, QueryAllJudgment, QueryJudgment, RuleSupport, SubgoalsSupport, SumPrimitiveSupport, } from "../types/judgment.js";