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,47 @@
1
+ import type { Atom, GroundAtom } from "../types/atom.js";
2
+ import type { KnowledgeBase } from "../knowledge-base/knowledge-base.js";
3
+ import type { QueryAllJudgment, QueryJudgment } from "../types/judgment.js";
4
+ import type { PredicateSchema } from "../types/schema.js";
5
+ /**
6
+ * Evaluate one grounded atom against a knowledge base.
7
+ *
8
+ * @remarks
9
+ * `query` names exactly one proposition — it is not a first-match shorthand
10
+ * for {@link queryAll}.
11
+ *
12
+ * @returns A four-valued {@link Judgment} — `true`, `false`, `unknown`, or
13
+ * `conflicted` — carrying the proof-support tree for the winning value.
14
+ *
15
+ * @throws {@link ProgrammerError} subclasses when the program shape is
16
+ * invalid, e.g. rule bodies using unbound variables or unconstructive
17
+ * recursion.
18
+ *
19
+ * @example
20
+ * ```ts
21
+ * kb.asserts(passedMedicalSchool({ person: alice }), { source: "registry" });
22
+ * kb.asserts(hasActiveLicense({ person: alice }), { source: "license_board" });
23
+ * kb.rule(doctor({ person: P })).iff(
24
+ * and(passedMedicalSchool({ person: P }), hasActiveLicense({ person: P })),
25
+ * );
26
+ *
27
+ * const judgment = query(kb, doctor({ person: alice }));
28
+ * judgment.value; // "true"
29
+ * ```
30
+ */
31
+ export declare function query<Schema extends PredicateSchema>(kb: KnowledgeBase, atom: GroundAtom<Schema>): QueryJudgment<Schema>;
32
+ /**
33
+ * Evaluate an atom pattern against a knowledge base, yielding one judgment
34
+ * per matching ground atom — zero, one, or many.
35
+ *
36
+ * @returns One {@link Judgment} per match, each carrying the variable
37
+ * bindings that ground the pattern.
38
+ *
39
+ * @example
40
+ * ```ts
41
+ * for (const judgment of queryAll(kb, doctor({ person: P }))) {
42
+ * judgment.value; // "true" | "false" | "unknown" | "conflicted"
43
+ * judgment.bindings; // e.g. [[P, alice]]
44
+ * }
45
+ * ```
46
+ */
47
+ export declare function queryAll<Schema extends PredicateSchema>(kb: KnowledgeBase, pattern: Atom<Schema>): QueryAllJudgment<Schema>;
@@ -0,0 +1,12 @@
1
+ import type { Atom, GroundAtom } from "../types/atom.js";
2
+ import type { PredicateDeclaration } from "../types/predicate.js";
3
+ import type { AnyRule } from "../types/rule.js";
4
+ /** Query-local candidate tables. Recursive predicates reach a fixed point together. */
5
+ export declare class CandidateSearch {
6
+ private readonly groups;
7
+ private readonly tables;
8
+ constructor(rules: readonly AnyRule[]);
9
+ candidates(atom: Atom, seeds: readonly GroundAtom[], derive: () => readonly GroundAtom[]): GroundAtom[];
10
+ sameComponent(left: PredicateDeclaration, right: PredicateDeclaration): boolean;
11
+ get isEvaluating(): boolean;
12
+ }
@@ -0,0 +1,6 @@
1
+ import type { GoalValue } from "../types/common.js";
2
+ export declare function rankGoalValue(value: GoalValue): 0 | 1 | 2 | 3;
3
+ export declare function minGoalValue(...values: readonly GoalValue[]): GoalValue;
4
+ export declare function maxGoalValue(...values: readonly GoalValue[]): GoalValue;
5
+ export declare function negateGoalValue(value: GoalValue): GoalValue;
6
+ export declare function negateAsFailureGoalValue(value: GoalValue): GoalValue;
@@ -0,0 +1,10 @@
1
+ import type { Atom, GroundAtom } from "../types/atom.js";
2
+ import type { AnyRule, Rule } from "../types/rule.js";
3
+ import type { PredicateSchema } from "../types/schema.js";
4
+ import type { Var } from "../types/variable.js";
5
+ export declare function matchRuleForAtom<Schema extends PredicateSchema>(rule: AnyRule, atom: GroundAtom<Schema>): Readonly<{
6
+ rule: Rule<Schema, unknown>;
7
+ bindings: ReadonlyMap<Var, unknown>;
8
+ }> | undefined;
9
+ export declare function matchPattern<Schema extends PredicateSchema>(pattern: Atom<Schema>, atom: GroundAtom<Schema>): ReadonlyMap<Var, unknown> | undefined;
10
+ export declare function groundAtom<Schema extends PredicateSchema>(atom: Atom<Schema>, bindings: ReadonlyMap<Var, unknown>): GroundAtom<Schema>;
@@ -0,0 +1,3 @@
1
+ import type { PredicateSchema } from "../../types/schema.js";
2
+ import type { QueryValidationContext } from "./context.js";
3
+ export declare function assertConstructiveRecursion<Schema extends PredicateSchema>(context: QueryValidationContext<Schema>): void;
@@ -0,0 +1,3 @@
1
+ import type { PredicateSchema } from "../../types/schema.js";
2
+ import type { QueryValidationContext } from "./context.js";
3
+ export declare function assertConstructiveVariableUse<Schema extends PredicateSchema>(context: QueryValidationContext<Schema>): void;
@@ -0,0 +1,10 @@
1
+ import type { Atom } from "../../types/atom.js";
2
+ import type { AnyLiteral } from "../../types/literal.js";
3
+ import type { AnyRule } from "../../types/rule.js";
4
+ import type { PredicateSchema } from "../../types/schema.js";
5
+ export type QueryValidationContext<Schema extends PredicateSchema = PredicateSchema> = Readonly<{
6
+ literals: readonly AnyLiteral[];
7
+ rules: readonly AnyRule[];
8
+ atom: Atom<Schema>;
9
+ }>;
10
+ export type QueryValidation = <Schema extends PredicateSchema>(context: QueryValidationContext<Schema>) => void;
@@ -0,0 +1 @@
1
+ export { validateQuery } from "./validate-query.js";
@@ -0,0 +1,27 @@
1
+ import type { AnyLiteral } from "../../types/literal.js";
2
+ import type { PredicateDeclaration } from "../../types/predicate.js";
3
+ import type { AnyRule } from "../../types/rule.js";
4
+ import type { Var } from "../../types/variable.js";
5
+ type EdgeKind = "positive" | "explicit-not" | "closed-world-not" | "aggregate" | "primitive-ungrounded";
6
+ export type Edge = Readonly<{
7
+ from: PredicateDeclaration;
8
+ to: PredicateDeclaration;
9
+ kind: EdgeKind;
10
+ }>;
11
+ export declare function ruleEdges(rule: AnyRule): Edge[];
12
+ export declare function reachablePredicates(start: PredicateDeclaration, edges: readonly Edge[]): Set<PredicateDeclaration>;
13
+ export declare function isNegativeLiteral(value: unknown): value is AnyLiteral & {
14
+ sign: "-";
15
+ };
16
+ export declare function isAggregateFormula(value: unknown): value is Readonly<{
17
+ kind: "sum" | "count";
18
+ args: readonly unknown[];
19
+ }>;
20
+ export declare function isCompleteWhenOption(value: unknown): value is Readonly<{
21
+ completeWhen: unknown;
22
+ }>;
23
+ export declare function isPrimitiveFormula(value: unknown): boolean;
24
+ export declare function bindVariables(value: unknown, bound: Set<Var>): void;
25
+ export declare function uniqueVariables(variables: readonly Var[]): Var[];
26
+ export declare function variablesIn(value: unknown): Var[];
27
+ export {};
@@ -0,0 +1,6 @@
1
+ import type { Atom } from "../../types/atom.js";
2
+ import type { AnyLiteral } from "../../types/literal.js";
3
+ import type { AnyRule } from "../../types/rule.js";
4
+ import type { PredicateSchema } from "../../types/schema.js";
5
+ /** Assert that a query is inside the solver's supported semantic fragment. */
6
+ export declare function validateQuery<Schema extends PredicateSchema>(literals: readonly AnyLiteral[], rules: readonly AnyRule[], atom: Atom<Schema>): void;
@@ -0,0 +1,6 @@
1
+ import type { Atom } from "../types/atom.js";
2
+ import type { QueryAllJudgment } from "../types/judgment.js";
3
+ import type { AnyLiteral } from "../types/literal.js";
4
+ import type { AnyRule } from "../types/rule.js";
5
+ import type { PredicateSchema } from "../types/schema.js";
6
+ export declare function solveQueryAll<Schema extends PredicateSchema>(literals: readonly AnyLiteral[], rules: readonly AnyRule[], atom: Atom<Schema>): QueryAllJudgment<Schema>;
@@ -0,0 +1,6 @@
1
+ import type { GroundAtom } from "../types/atom.js";
2
+ import type { AnyLiteral } from "../types/literal.js";
3
+ import type { QueryJudgment } from "../types/judgment.js";
4
+ import type { AnyRule } from "../types/rule.js";
5
+ import type { PredicateSchema } from "../types/schema.js";
6
+ export declare function solveQuery<Schema extends PredicateSchema>(literals: readonly AnyLiteral[], rules: readonly AnyRule[], atom: GroundAtom<Schema>): QueryJudgment<Schema>;
@@ -0,0 +1,6 @@
1
+ /** JSON-compatible annotation data carried without interpretation by core. */
2
+ export type AnnotationValue = null | boolean | number | string | readonly AnnotationValue[] | {
3
+ readonly [key: string]: AnnotationValue;
4
+ };
5
+ /** Application-defined, serializable annotations. */
6
+ export type Annotations = Readonly<Record<string, AnnotationValue>>;
@@ -0,0 +1,55 @@
1
+ import type { InferRuntime, PredicateSchema } from "./schema.js";
2
+ import type { Var } from "./variable.js";
3
+ import type { PredicateDeclaration } from "./predicate.js";
4
+ /** An atom whose exact schema has been intentionally erased. */
5
+ export type AnyAtom = Readonly<{
6
+ predicate: PredicateDeclaration;
7
+ fields: Readonly<Record<string, unknown>>;
8
+ }>;
9
+ /** Static groundedness state carried by an atom type. */
10
+ type AtomState = "ground" | "non-ground";
11
+ /** Field values for a fully grounded atom: no logic variables are present. */
12
+ export type GroundFields<Schema extends PredicateSchema = PredicateSchema> = Readonly<{
13
+ [Name in keyof Schema]: InferRuntime<Schema[Name]>;
14
+ }>;
15
+ /** Field values for a pattern atom: concrete values or logic variables. */
16
+ export type PatternFields<Schema extends PredicateSchema = PredicateSchema> = Readonly<{
17
+ [Name in keyof Schema]: InferRuntime<Schema[Name]> | Var;
18
+ }>;
19
+ /** Resolve the field map type for an atom based on its groundedness state. */
20
+ type AtomFields<Schema extends PredicateSchema, State extends AtomState> = State extends "ground" ? GroundFields<Schema> : PatternFields<Schema>;
21
+ /** Shared atom representation parameterized by schema and groundedness. */
22
+ type AtomForState<Schema extends PredicateSchema, State extends AtomState> = Readonly<{
23
+ predicate: PredicateDeclaration<Schema>;
24
+ fields: AtomFields<Schema, State>;
25
+ }>;
26
+ /** An atom whose fields are all concrete runtime values. */
27
+ export type GroundAtom<Schema extends PredicateSchema = PredicateSchema> = AtomForState<Schema, "ground">;
28
+ /** An atom pattern that may contain one or more logic variables. */
29
+ export type NonGroundAtom<Schema extends PredicateSchema = PredicateSchema> = AtomForState<Schema, "non-ground">;
30
+ /** An atom whose groundedness has not yet been narrowed by a runtime guard. */
31
+ export type Atom<Schema extends PredicateSchema = PredicateSchema> = GroundAtom<Schema> | NonGroundAtom<Schema>;
32
+ /** Return whether an atom has no logic variables in its fields. */
33
+ export declare function isGroundAtom<Schema extends PredicateSchema>(atom: Atom<Schema>): atom is GroundAtom<Schema>;
34
+ /** Assert that an atom has no logic variables in its fields. */
35
+ export declare function assertGroundAtom<Schema extends PredicateSchema>(atom: Atom<Schema>): asserts atom is GroundAtom<Schema>;
36
+ /** Return whether an unknown value has the runtime shape of an atom. */
37
+ export declare function isAtomLike(value: unknown): value is AnyAtom;
38
+ /** Assert that an unknown value has the runtime shape of an atom. */
39
+ export declare function assertAtomLike(value: unknown): asserts value is AnyAtom;
40
+ /**
41
+ * Narrow a schema-erased atom to a predicate by declaration identity and field
42
+ * shape. Identity is the real guarantor: an atom carrying this predicate was
43
+ * built by its constructor, so its fields already satisfy the schema.
44
+ */
45
+ export declare function isAtomForPredicate<Schema extends PredicateSchema>(atom: AnyAtom, predicate: PredicateDeclaration<Schema>): atom is Atom<Schema>;
46
+ /** Return whether two atoms have the same predicate and field values. */
47
+ export declare function atomEquals(left: AnyAtom, right: AnyAtom): boolean;
48
+ /**
49
+ * Return whether two atom field values denote the same thing: entities by
50
+ * type + id, everything else (primitives, variables) by reference identity.
51
+ */
52
+ export declare function fieldEquals(left: unknown, right: unknown): boolean;
53
+ /** Canonical JSON-safe token for a field value, for use in string identity keys. */
54
+ export declare function fieldIdentityToken(value: unknown): unknown;
55
+ export {};
@@ -0,0 +1,7 @@
1
+ import type { Var } from "./variable.js";
2
+ /** Four-valued result for an evaluated proposition or goal. */
3
+ export type GoalValue = "false" | "unknown" | "conflicted" | "true";
4
+ /** One variable assignment in scope for an evaluated goal. */
5
+ export type Binding = readonly [variable: Var, value: unknown];
6
+ /** Ordered variable assignments in scope for an evaluated goal. */
7
+ export type Bindings = readonly Binding[];
@@ -0,0 +1,18 @@
1
+ import type { Annotations } from "./annotations.js";
2
+ /**
3
+ * Provenance metadata attached to accepted evidence literals: `source` names
4
+ * where the fact came from, `citation` quotes or references the supporting
5
+ * material, and `justification` records the asserter's stated reason.
6
+ */
7
+ export type EvidenceMetadata = Readonly<{
8
+ /** Name of the system or document the fact came from. */
9
+ source?: string;
10
+ /** Quotation or reference into the supporting material. */
11
+ citation?: string;
12
+ /** The asserter's stated reason for accepting the fact. */
13
+ justification?: string;
14
+ /** Named numeric scores; core stores these without combining or interpreting them. */
15
+ scores?: Readonly<Record<string, number>>;
16
+ /** Application-specific annotations, retained on supporting evidence in judgments. */
17
+ annotations?: Annotations;
18
+ }>;
@@ -0,0 +1,103 @@
1
+ import type { AnyAtom, Atom, GroundAtom } from "./atom.js";
2
+ import type { PredicateSchema } from "./schema.js";
3
+ import type { Var } from "./variable.js";
4
+ import type { Entity, Scalar, Term } from "../dsl/types.js";
5
+ /** A resolved value or variable used inside normalized rule goals. */
6
+ export type GoalExpression<Value = Scalar | Entity> = Term<Value>;
7
+ /** A normalized atom goal: evaluate an atom's goal value. */
8
+ export type AtomGoal<Schema extends PredicateSchema = PredicateSchema> = Readonly<{
9
+ readonly kind: "atom";
10
+ readonly atom: Atom<Schema>;
11
+ }>;
12
+ /** A normalized atom goal whose exact schema has been intentionally erased. */
13
+ export type AnyAtomGoal = Readonly<{
14
+ readonly kind: "atom";
15
+ readonly atom: AnyAtom;
16
+ }>;
17
+ /** A normalized atom goal whose atom is fully grounded. */
18
+ export type GroundAtomGoal<Schema extends PredicateSchema = PredicateSchema> = Readonly<{
19
+ readonly kind: "atom";
20
+ readonly atom: GroundAtom<Schema>;
21
+ }>;
22
+ /** Require explicit negative support for a goal. */
23
+ export type NotGoal = Readonly<{
24
+ readonly kind: "not";
25
+ readonly goal: Goal;
26
+ }>;
27
+ /** Prove every child goal. */
28
+ export type AndGoal = Readonly<{
29
+ readonly kind: "and";
30
+ readonly goals: readonly Goal[];
31
+ }>;
32
+ /** Prove at least one child goal. */
33
+ export type OrGoal = Readonly<{
34
+ readonly kind: "or";
35
+ readonly goals: readonly Goal[];
36
+ }>;
37
+ /** Existentially quantify variables for a child goal. */
38
+ export type ExistsGoal = Readonly<{
39
+ readonly kind: "exists";
40
+ readonly variables: readonly Var[];
41
+ readonly goal: Goal;
42
+ }>;
43
+ /** Bind a variable to a value before proving a child goal. */
44
+ export type LetGoal = Readonly<{
45
+ readonly kind: "let";
46
+ readonly variable: Var;
47
+ readonly value: GoalExpression;
48
+ readonly goal: Goal;
49
+ }>;
50
+ /** Arithmetic goal binding the sum of two expressions. */
51
+ export type AddGoal = ArithmeticGoal<"add">;
52
+ /** Arithmetic goal binding the difference of two expressions. */
53
+ export type SubGoal = ArithmeticGoal<"sub">;
54
+ /** Arithmetic goal binding the product of two expressions. */
55
+ export type MulGoal = ArithmeticGoal<"mul">;
56
+ /** Arithmetic goal binding the quotient of two expressions. */
57
+ export type DivGoal = ArithmeticGoal<"div">;
58
+ /** Bind the result of a primitive arithmetic operation. */
59
+ export type ArithmeticGoal<Kind extends "add" | "sub" | "mul" | "div"> = Readonly<{
60
+ readonly kind: Kind;
61
+ readonly bind: Var;
62
+ readonly left: GoalExpression<Scalar>;
63
+ readonly right: GoalExpression<Scalar>;
64
+ }>;
65
+ /** Comparison goal requiring the operands to be equal. */
66
+ export type EqGoal = ComparisonGoal<"eq">;
67
+ /** Comparison goal requiring the operands to differ. */
68
+ export type NeqGoal = ComparisonGoal<"neq">;
69
+ /** Comparison goal requiring the left operand to be strictly less than the right. */
70
+ export type LtGoal = ComparisonGoal<"lt">;
71
+ /** Comparison goal requiring the left operand to be at most the right. */
72
+ export type LteGoal = ComparisonGoal<"lte">;
73
+ /** Comparison goal requiring the left operand to be strictly greater than the right. */
74
+ export type GtGoal = ComparisonGoal<"gt">;
75
+ /** Comparison goal requiring the left operand to be at least the right. */
76
+ export type GteGoal = ComparisonGoal<"gte">;
77
+ /** Compare two value-producing expressions. */
78
+ export type ComparisonGoal<Kind extends "eq" | "neq" | "lt" | "lte" | "gt" | "gte"> = Readonly<{
79
+ readonly kind: Kind;
80
+ readonly left: GoalExpression<Scalar>;
81
+ readonly right: GoalExpression<Scalar>;
82
+ }>;
83
+ /** Sum a numeric expression over rows matched by a body goal. */
84
+ export type SumGoal = Readonly<{
85
+ readonly kind: "sum";
86
+ readonly bind: Var;
87
+ readonly value: GoalExpression<number | bigint>;
88
+ readonly where: Goal;
89
+ readonly completeWhen?: Goal;
90
+ }>;
91
+ /** Count rows matched by a body goal. */
92
+ export type CountGoal = Readonly<{
93
+ readonly kind: "count";
94
+ readonly bind: Var;
95
+ readonly where: Goal;
96
+ readonly completeWhen?: Goal;
97
+ }>;
98
+ /** A normalized rule-body target that the solver/proof system can evaluate. */
99
+ export type Goal = AnyAtomGoal | NotGoal | AndGoal | OrGoal | ExistsGoal | LetGoal | AddGoal | SubGoal | MulGoal | DivGoal | EqGoal | NeqGoal | LtGoal | LteGoal | GtGoal | GteGoal | SumGoal | CountGoal;
100
+ /** A goal evaluated by recursively evaluating one or more child goals. */
101
+ export type CompoundGoal = NotGoal | AndGoal | OrGoal | ExistsGoal | LetGoal;
102
+ /** A goal evaluated directly by a primitive operator. */
103
+ export type PrimitiveGoal = AddGoal | SubGoal | MulGoal | DivGoal | EqGoal | NeqGoal | LtGoal | LteGoal | GtGoal | GteGoal | SumGoal | CountGoal;
@@ -0,0 +1,137 @@
1
+ import type { AnyAtomGoal, AtomGoal, CompoundGoal, Goal, GroundAtomGoal, PrimitiveGoal } from "./goal.js";
2
+ import type { AnyLiteral, Literal } from "./literal.js";
3
+ import type { AnyRule, Rule } from "./rule.js";
4
+ import type { Bindings } from "./common.js";
5
+ import type { PredicateSchema } from "./schema.js";
6
+ /** Support from stored evidence for an atom goal. */
7
+ export type EvidenceSupport<Schema extends PredicateSchema = PredicateSchema> = Readonly<{
8
+ readonly source: "evidence";
9
+ readonly literals: readonly Literal<Schema>[];
10
+ }>;
11
+ /** Support from absence in a complete closed-world relation. */
12
+ export type ClosedWorldAbsenceSupport = Readonly<{
13
+ readonly source: "closed-world-absence";
14
+ }>;
15
+ /** Support from firing a rule for an atom goal. */
16
+ export type RuleSupport<Schema extends PredicateSchema = PredicateSchema> = Readonly<{
17
+ readonly source: "rule";
18
+ readonly rule: Rule<Schema, unknown>;
19
+ readonly body: Judgment;
20
+ }>;
21
+ /** Support from stored evidence after atom schema erasure. */
22
+ export type ErasedEvidenceSupport = Readonly<{
23
+ readonly source: "evidence";
24
+ readonly literals: readonly AnyLiteral[];
25
+ }>;
26
+ /** Support from firing a rule after atom schema erasure. */
27
+ export type ErasedRuleSupport = Readonly<{
28
+ readonly source: "rule";
29
+ readonly rule: AnyRule;
30
+ readonly body: Judgment;
31
+ }>;
32
+ /** Support from recursively evaluating child goals of a compound goal. */
33
+ export type SubgoalsSupport = Readonly<{
34
+ readonly source: "subgoals";
35
+ readonly judgments: readonly Judgment[];
36
+ }>;
37
+ /** Support from directly evaluating a primitive goal. */
38
+ export type PrimitiveSupport = Readonly<{
39
+ readonly source: "primitive";
40
+ readonly evaluation: PrimitiveEvaluation;
41
+ }>;
42
+ /** A variable required by a primitive has no runtime binding on this branch. */
43
+ export type MissingBinding = Readonly<{
44
+ readonly kind: "missing-binding";
45
+ readonly variable: unknown;
46
+ }>;
47
+ /** Uniform summary for primitive operations, including aggregates and comparisons. */
48
+ export type PrimitiveEvaluation = Readonly<{
49
+ readonly operation: string;
50
+ readonly operands: readonly unknown[];
51
+ readonly value: "true" | "false" | "unknown" | "conflicted";
52
+ readonly result?: unknown;
53
+ readonly results?: Bindings;
54
+ readonly range?: NumericRange;
55
+ readonly uncertainty?: readonly PrimitiveUncertainty[];
56
+ }>;
57
+ /** Numeric lower/upper bound produced or consumed by a primitive operation. */
58
+ export type NumericRange = Readonly<{
59
+ readonly lower: number | bigint;
60
+ readonly upper: number | bigint | "infinity";
61
+ }>;
62
+ /** Why a primitive evaluation is not exact. */
63
+ export type PrimitiveUncertainty = Readonly<{
64
+ readonly kind: "conflict";
65
+ readonly count?: number;
66
+ }> | Readonly<{
67
+ readonly kind: "unknown";
68
+ readonly count?: number;
69
+ }> | MissingBinding | Readonly<{
70
+ readonly kind: "open-world";
71
+ }>;
72
+ /** Support from directly evaluating an arithmetic binding goal. */
73
+ export type ArithmeticPrimitiveSupport = Readonly<{
74
+ readonly source: "primitive";
75
+ readonly operands: readonly [left: unknown, right: unknown];
76
+ readonly results: Bindings;
77
+ }>;
78
+ /** Support from directly evaluating a comparison goal. */
79
+ export type ComparisonPrimitiveSupport = Readonly<{
80
+ readonly source: "primitive";
81
+ readonly operands: readonly [left: unknown, right: unknown];
82
+ }>;
83
+ /** Support from directly evaluating a sum aggregate goal. */
84
+ export type SumPrimitiveSupport = Readonly<{
85
+ readonly source: "primitive";
86
+ readonly values: readonly unknown[];
87
+ readonly results: Bindings;
88
+ }>;
89
+ /** Support from directly evaluating a count aggregate goal. */
90
+ export type CountPrimitiveSupport = Readonly<{
91
+ readonly source: "primitive";
92
+ readonly matched: number;
93
+ readonly results: Bindings;
94
+ }>;
95
+ /** Proof support for a primitive goal, narrowed to the support shape for the goal's kind. */
96
+ export type PrimitiveSupportFor<GoalType extends PrimitiveGoal> = PrimitiveSupport | (GoalType["kind"] extends "add" | "sub" | "mul" | "div" ? ArithmeticPrimitiveSupport : GoalType["kind"] extends "eq" | "neq" | "lt" | "lte" | "gt" | "gte" ? ComparisonPrimitiveSupport : GoalType["kind"] extends "sum" ? SumPrimitiveSupport : GoalType["kind"] extends "count" ? CountPrimitiveSupport : never);
97
+ /** Sources of support allowed for a specific goal shape. */
98
+ export type JudgmentSupportFor<GoalType extends Goal> = GoalType extends AtomGoal<infer Schema> ? EvidenceSupport<Schema> | ClosedWorldAbsenceSupport | RuleSupport<Schema> : GoalType extends AnyAtomGoal ? ErasedEvidenceSupport | ClosedWorldAbsenceSupport | ErasedRuleSupport : GoalType extends PrimitiveGoal ? PrimitiveSupportFor<GoalType> : GoalType extends CompoundGoal ? SubgoalsSupport : never;
99
+ /** The evaluated goal and in-scope variable bindings shared by every judgment. */
100
+ export type JudgmentBase<GoalType extends Goal> = Readonly<{
101
+ readonly goal: GoalType;
102
+ readonly bindings: Bindings;
103
+ }>;
104
+ /**
105
+ * Positive and negative supports must match the judgment's goal value.
106
+ *
107
+ * `inconclusive` holds derivations that could not settle the value: at atom
108
+ * goals, rule attempts whose bodies came back unknown; at compound and negated
109
+ * goals whose value is unknown, the constituent subproofs. Unknown compounds
110
+ * always carry their subgoals, so an unknown verdict remains traversable down
111
+ * to the leaf atoms that are actually undetermined.
112
+ */
113
+ export type Judgment<GoalType extends Goal = Goal> = (JudgmentBase<GoalType> & {
114
+ readonly value: "true";
115
+ readonly positive: readonly JudgmentSupportFor<GoalType>[];
116
+ readonly negative?: never;
117
+ readonly inconclusive?: readonly JudgmentSupportFor<GoalType>[];
118
+ }) | (JudgmentBase<GoalType> & {
119
+ readonly value: "false";
120
+ readonly positive?: never;
121
+ readonly negative: readonly JudgmentSupportFor<GoalType>[];
122
+ readonly inconclusive?: readonly JudgmentSupportFor<GoalType>[];
123
+ }) | (JudgmentBase<GoalType> & {
124
+ readonly value: "conflicted";
125
+ readonly positive: readonly JudgmentSupportFor<GoalType>[];
126
+ readonly negative: readonly JudgmentSupportFor<GoalType>[];
127
+ readonly inconclusive?: readonly JudgmentSupportFor<GoalType>[];
128
+ }) | (JudgmentBase<GoalType> & {
129
+ readonly value: "unknown";
130
+ readonly positive?: never;
131
+ readonly negative?: never;
132
+ readonly inconclusive?: readonly JudgmentSupportFor<GoalType>[];
133
+ });
134
+ /** Query result for one grounded atom goal. */
135
+ export type QueryJudgment<Schema extends PredicateSchema = PredicateSchema> = Judgment<GroundAtomGoal<Schema>>;
136
+ /** Query-all result containing zero or more grounded atom judgments. */
137
+ export type QueryAllJudgment<Schema extends PredicateSchema = PredicateSchema> = readonly QueryJudgment<Schema>[];
@@ -0,0 +1,25 @@
1
+ import type { AnyAtom, Atom } from "./atom.js";
2
+ import type { EvidenceMetadata } from "./evidence-metadata.js";
3
+ import type { PredicateSchema } from "./schema.js";
4
+ /** Evidence polarity for an accepted literal. */
5
+ export type LiteralSign = "+" | "-";
6
+ /** A literal whose exact atom schema has been intentionally erased. */
7
+ export type AnyLiteral = Readonly<{
8
+ sign: LiteralSign;
9
+ atom: AnyAtom;
10
+ metadata?: EvidenceMetadata;
11
+ }>;
12
+ /** Positive support for an atom. */
13
+ export type PositiveLiteral<Schema extends PredicateSchema = PredicateSchema> = Readonly<{
14
+ sign: "+";
15
+ atom: Atom<Schema>;
16
+ metadata?: EvidenceMetadata;
17
+ }>;
18
+ /** Negative support for an atom. */
19
+ export type NegativeLiteral<Schema extends PredicateSchema = PredicateSchema> = Readonly<{
20
+ sign: "-";
21
+ atom: Atom<Schema>;
22
+ metadata?: EvidenceMetadata;
23
+ }>;
24
+ /** A signed atom stored as explicit accepted evidence. */
25
+ export type Literal<Schema extends PredicateSchema = PredicateSchema> = PositiveLiteral<Schema> | NegativeLiteral<Schema>;
@@ -0,0 +1,35 @@
1
+ import type { Atom, GroundAtom, GroundFields, PatternFields } from "./atom.js";
2
+ import type { PredicateSchema } from "./schema.js";
3
+ import type { Annotations } from "./annotations.js";
4
+ /** Domain guidance for interpreting a predicate, available to evidence producers. */
5
+ export type PredicateMetadata = Readonly<{
6
+ description: string;
7
+ instructions?: string;
8
+ parameters?: Readonly<Record<string, string>>;
9
+ annotations?: Annotations;
10
+ }>;
11
+ /** Whether absence of evidence counts as falsity (`closed`) or unknown (`open`). */
12
+ export type PredicateWorld = "open" | "closed";
13
+ /** Predicate metadata carried by atoms and literals. */
14
+ export type PredicateDeclaration<Schema extends PredicateSchema = PredicateSchema> = Readonly<{
15
+ readonly name: string;
16
+ readonly params: Schema;
17
+ readonly paramNames: readonly (keyof Schema & string)[];
18
+ readonly world: PredicateWorld;
19
+ readonly metadata?: PredicateMetadata;
20
+ }>;
21
+ /** A callable predicate declaration that constructs schema-typed atoms. */
22
+ export type PredicateBase<Schema extends PredicateSchema = PredicateSchema> = PredicateDeclaration<Schema> & {
23
+ (fields: GroundFields<Schema>): GroundAtom<Schema>;
24
+ (fields: PatternFields<Schema>): Atom<Schema>;
25
+ };
26
+ /** A predicate whose unlisted atoms are unknown rather than false. */
27
+ export type OpenWorldPredicate<Schema extends PredicateSchema = PredicateSchema> = PredicateBase<Schema> & {
28
+ readonly world: "open";
29
+ };
30
+ /** A predicate whose relation is complete: absence of evidence means false. */
31
+ export type ClosedWorldPredicate<Schema extends PredicateSchema = PredicateSchema> = PredicateBase<Schema> & {
32
+ readonly world: "closed";
33
+ };
34
+ /** Any declared predicate, open- or closed-world. */
35
+ export type Predicate<Schema extends PredicateSchema = PredicateSchema> = OpenWorldPredicate<Schema> | ClosedWorldPredicate<Schema>;
@@ -0,0 +1,16 @@
1
+ import type { AnyLiteral, Literal } from "./literal.js";
2
+ import type { PredicateSchema } from "./schema.js";
3
+ /** Rule semantics: `if` states a sufficient condition, `iff` a definition. */
4
+ export type RuleKind = "if" | "iff";
5
+ /** A logical rule with either sufficient or definitional semantics. */
6
+ export type Rule<HeadSchema extends PredicateSchema, Body> = Readonly<{
7
+ kind: RuleKind;
8
+ head: Literal<HeadSchema>;
9
+ body: Body;
10
+ }>;
11
+ /** A rule whose exact head schema and body shape are intentionally erased. */
12
+ export type AnyRule = Readonly<{
13
+ readonly kind: RuleKind;
14
+ readonly head: AnyLiteral;
15
+ readonly body: unknown;
16
+ }>;
@@ -0,0 +1,33 @@
1
+ /**
2
+ * A domain entity carries its own identity: the same type with the same id
3
+ * is the same entity, however many times it is constructed. Whoever mints
4
+ * entities owns the uniqueness of ids, exactly like a primary key.
5
+ */
6
+ export type Entity = Readonly<{
7
+ id: string;
8
+ }>;
9
+ /** An explicit finite domain of string values for a predicate field. */
10
+ export type EnumType<Value extends string = string> = Readonly<{
11
+ kind: "enum";
12
+ values: readonly Value[];
13
+ }>;
14
+ /** Runtime constructor used to validate a predicate field value. */
15
+ export type RuntimeConstructor<T> = (abstract new (...args: never[]) => T) & Readonly<{
16
+ prototype: T;
17
+ name: string;
18
+ }>;
19
+ /** Any runtime validator accepted in a predicate schema field. */
20
+ export type RuntimeType<T> = EnumType | RuntimeConstructor<T & Entity> | BigIntConstructor | BooleanConstructor | DateConstructor | NumberConstructor | StringConstructor;
21
+ /** A predicate schema maps field names to runtime validators/constructors. */
22
+ export type PredicateSchema = Readonly<Record<string, BigIntConstructor | BooleanConstructor | DateConstructor | NumberConstructor | StringConstructor | EnumType | ((abstract new (...args: never[]) => Entity) & Readonly<{
23
+ prototype: Entity;
24
+ name: string;
25
+ }>)>>;
26
+ /**
27
+ * Convert a runtime constructor in a predicate schema to its TypeScript value
28
+ * type. A schema-erased constructor (the bare `RuntimeConstructor<Entity>`)
29
+ * infers `unknown`, so erased atoms keep permissive field types.
30
+ */
31
+ export type InferRuntime<T> = T extends EnumType<infer Value> ? Value : T extends StringConstructor ? string : T extends NumberConstructor ? number : T extends BooleanConstructor ? boolean : T extends BigIntConstructor ? bigint : T extends DateConstructor ? Date : T extends {
32
+ prototype: infer Value;
33
+ } ? RuntimeConstructor<Entity> extends T ? unknown : Value : never;
@@ -0,0 +1,13 @@
1
+ /** A logic variable placeholder used in rule heads, rule bodies, and query patterns. */
2
+ export declare class Var<Name extends string = string> {
3
+ /** Display name used when formatting goals, bindings, and explanations. */
4
+ readonly name: Name;
5
+ /**
6
+ * Create a variable with a display name.
7
+ *
8
+ * @throws Error when the name is empty or only whitespace.
9
+ */
10
+ constructor(name: Name);
11
+ }
12
+ /** Return whether a runtime value is a logic variable. */
13
+ export declare function isVar(value: unknown): value is Var;
@@ -0,0 +1,4 @@
1
+ export declare class AssertionError extends Error {
2
+ constructor(message?: string);
3
+ }
4
+ export declare function assert(condition: boolean, message?: string): asserts condition;
@@ -0,0 +1,2 @@
1
+ export type OneOrMany<Value> = Value | readonly Value[];
2
+ export declare function normalizeToList<Value>(value: OneOrMany<Value>): readonly Value[];
@@ -0,0 +1,7 @@
1
+ import type { Chunker, RecursiveChunkOptions } from "./types.js";
2
+ /**
3
+ * Construct a bounded splitter that prefers paragraph, line, and word boundaries.
4
+ * Ranges are produced lazily and never split a Unicode grapheme. Custom measures
5
+ * are checked on exact substrings; a grapheme that cannot fit raises RangeError.
6
+ */
7
+ export declare function recursive(options: RecursiveChunkOptions): Chunker;