@cogitator-ai/neuro-symbolic 0.1.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.
- package/LICENSE +21 -0
- package/README.md +494 -0
- package/dist/constraints/dsl.d.ts +68 -0
- package/dist/constraints/dsl.d.ts.map +1 -0
- package/dist/constraints/dsl.js +419 -0
- package/dist/constraints/dsl.js.map +1 -0
- package/dist/constraints/index.d.ts +7 -0
- package/dist/constraints/index.d.ts.map +1 -0
- package/dist/constraints/index.js +18 -0
- package/dist/constraints/index.js.map +1 -0
- package/dist/constraints/prompts.d.ts +44 -0
- package/dist/constraints/prompts.d.ts.map +1 -0
- package/dist/constraints/prompts.js +175 -0
- package/dist/constraints/prompts.js.map +1 -0
- package/dist/constraints/simple-sat-solver.d.ts +21 -0
- package/dist/constraints/simple-sat-solver.d.ts.map +1 -0
- package/dist/constraints/simple-sat-solver.js +378 -0
- package/dist/constraints/simple-sat-solver.js.map +1 -0
- package/dist/constraints/z3-wasm-solver.d.ts +20 -0
- package/dist/constraints/z3-wasm-solver.d.ts.map +1 -0
- package/dist/constraints/z3-wasm-solver.js +286 -0
- package/dist/constraints/z3-wasm-solver.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/knowledge-graph/index.d.ts +5 -0
- package/dist/knowledge-graph/index.d.ts.map +1 -0
- package/dist/knowledge-graph/index.js +5 -0
- package/dist/knowledge-graph/index.js.map +1 -0
- package/dist/knowledge-graph/natural-language-query.d.ts +42 -0
- package/dist/knowledge-graph/natural-language-query.d.ts.map +1 -0
- package/dist/knowledge-graph/natural-language-query.js +271 -0
- package/dist/knowledge-graph/natural-language-query.js.map +1 -0
- package/dist/knowledge-graph/prompts.d.ts +56 -0
- package/dist/knowledge-graph/prompts.d.ts.map +1 -0
- package/dist/knowledge-graph/prompts.js +198 -0
- package/dist/knowledge-graph/prompts.js.map +1 -0
- package/dist/knowledge-graph/query-language.d.ts +43 -0
- package/dist/knowledge-graph/query-language.d.ts.map +1 -0
- package/dist/knowledge-graph/query-language.js +569 -0
- package/dist/knowledge-graph/query-language.js.map +1 -0
- package/dist/knowledge-graph/reasoning-engine.d.ts +53 -0
- package/dist/knowledge-graph/reasoning-engine.d.ts.map +1 -0
- package/dist/knowledge-graph/reasoning-engine.js +337 -0
- package/dist/knowledge-graph/reasoning-engine.js.map +1 -0
- package/dist/logic/builtins.d.ts +10 -0
- package/dist/logic/builtins.d.ts.map +1 -0
- package/dist/logic/builtins.js +527 -0
- package/dist/logic/builtins.js.map +1 -0
- package/dist/logic/index.d.ts +8 -0
- package/dist/logic/index.d.ts.map +1 -0
- package/dist/logic/index.js +8 -0
- package/dist/logic/index.js.map +1 -0
- package/dist/logic/knowledge-base.d.ts +38 -0
- package/dist/logic/knowledge-base.d.ts.map +1 -0
- package/dist/logic/knowledge-base.js +205 -0
- package/dist/logic/knowledge-base.js.map +1 -0
- package/dist/logic/parser.d.ts +19 -0
- package/dist/logic/parser.d.ts.map +1 -0
- package/dist/logic/parser.js +466 -0
- package/dist/logic/parser.js.map +1 -0
- package/dist/logic/prompts.d.ts +55 -0
- package/dist/logic/prompts.d.ts.map +1 -0
- package/dist/logic/prompts.js +230 -0
- package/dist/logic/prompts.js.map +1 -0
- package/dist/logic/proof-tree.d.ts +30 -0
- package/dist/logic/proof-tree.d.ts.map +1 -0
- package/dist/logic/proof-tree.js +281 -0
- package/dist/logic/proof-tree.js.map +1 -0
- package/dist/logic/resolver.d.ts +16 -0
- package/dist/logic/resolver.d.ts.map +1 -0
- package/dist/logic/resolver.js +382 -0
- package/dist/logic/resolver.js.map +1 -0
- package/dist/logic/unification.d.ts +17 -0
- package/dist/logic/unification.d.ts.map +1 -0
- package/dist/logic/unification.js +257 -0
- package/dist/logic/unification.js.map +1 -0
- package/dist/orchestrator.d.ts +57 -0
- package/dist/orchestrator.d.ts.map +1 -0
- package/dist/orchestrator.js +293 -0
- package/dist/orchestrator.js.map +1 -0
- package/dist/planning/action-schema.d.ts +48 -0
- package/dist/planning/action-schema.d.ts.map +1 -0
- package/dist/planning/action-schema.js +358 -0
- package/dist/planning/action-schema.js.map +1 -0
- package/dist/planning/index.d.ts +6 -0
- package/dist/planning/index.d.ts.map +1 -0
- package/dist/planning/index.js +6 -0
- package/dist/planning/index.js.map +1 -0
- package/dist/planning/invariant-checker.d.ts +39 -0
- package/dist/planning/invariant-checker.d.ts.map +1 -0
- package/dist/planning/invariant-checker.js +293 -0
- package/dist/planning/invariant-checker.js.map +1 -0
- package/dist/planning/plan-repair.d.ts +38 -0
- package/dist/planning/plan-repair.d.ts.map +1 -0
- package/dist/planning/plan-repair.js +366 -0
- package/dist/planning/plan-repair.js.map +1 -0
- package/dist/planning/plan-validator.d.ts +31 -0
- package/dist/planning/plan-validator.d.ts.map +1 -0
- package/dist/planning/plan-validator.js +379 -0
- package/dist/planning/plan-validator.js.map +1 -0
- package/dist/planning/prompts.d.ts +50 -0
- package/dist/planning/prompts.d.ts.map +1 -0
- package/dist/planning/prompts.js +186 -0
- package/dist/planning/prompts.js.map +1 -0
- package/package.json +96 -0
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { termToString } from './unification';
|
|
2
|
+
export function createNLToQueryPrompt(context) {
|
|
3
|
+
const predicates = context.knowledgeBase.getPredicates();
|
|
4
|
+
const stats = context.knowledgeBase.getStats();
|
|
5
|
+
const systemPrompt = `You are a logic programming expert that converts natural language questions into Prolog-style queries.
|
|
6
|
+
|
|
7
|
+
Available predicates in the knowledge base:
|
|
8
|
+
${predicates.map((p) => ` - ${p}`).join('\n')}
|
|
9
|
+
|
|
10
|
+
Knowledge base stats:
|
|
11
|
+
- ${stats.factCount} facts
|
|
12
|
+
- ${stats.ruleCount} rules
|
|
13
|
+
|
|
14
|
+
Rules for generating queries:
|
|
15
|
+
1. Use only predicates that exist in the knowledge base
|
|
16
|
+
2. Use variable names that start with uppercase letters (X, Y, Person, Item, etc.)
|
|
17
|
+
3. For "who" questions, use a Person or X variable
|
|
18
|
+
4. For "what" questions, use appropriate variable names
|
|
19
|
+
5. For yes/no questions, generate a ground query if possible
|
|
20
|
+
6. Multiple conditions should be separated by commas
|
|
21
|
+
7. Use underscore (_) for anonymous/unused variables`;
|
|
22
|
+
const examples = context.examples || getDefaultExamples();
|
|
23
|
+
const examplesSection = examples.length > 0
|
|
24
|
+
? `\n\nExamples:\n${examples.map((e) => `Q: "${e.input}"\nA: ${e.output}`).join('\n\n')}`
|
|
25
|
+
: '';
|
|
26
|
+
return `${systemPrompt}${examplesSection}
|
|
27
|
+
|
|
28
|
+
Convert this natural language query to a Prolog-style query:
|
|
29
|
+
|
|
30
|
+
Q: "${context.naturalLanguageQuery}"
|
|
31
|
+
A:`;
|
|
32
|
+
}
|
|
33
|
+
function getDefaultExamples() {
|
|
34
|
+
return [
|
|
35
|
+
{ input: 'Who is the parent of John?', output: 'parent(X, john).' },
|
|
36
|
+
{ input: 'Is Mary the mother of John?', output: 'mother(mary, john).' },
|
|
37
|
+
{ input: 'Find all grandparents of Alice', output: 'grandparent(X, alice).' },
|
|
38
|
+
{ input: 'Who works at Google?', output: 'works_at(Person, google).' },
|
|
39
|
+
{ input: 'What is the capital of France?', output: 'capital(france, City).' },
|
|
40
|
+
{ input: 'Are Tom and Jerry siblings?', output: 'sibling(tom, jerry).' },
|
|
41
|
+
];
|
|
42
|
+
}
|
|
43
|
+
export function createNLToFactsPrompt(context) {
|
|
44
|
+
const predicateInfo = context.existingPredicates?.length
|
|
45
|
+
? `\n\nExisting predicates to use when applicable:\n${context.existingPredicates.map((p) => ` - ${p}`).join('\n')}`
|
|
46
|
+
: '';
|
|
47
|
+
const domainInfo = context.domain ? `\n\nDomain context: ${context.domain}` : '';
|
|
48
|
+
return `You are a logic programming expert that extracts facts from natural language text.
|
|
49
|
+
|
|
50
|
+
Convert the following text into Prolog-style facts.
|
|
51
|
+
|
|
52
|
+
Rules:
|
|
53
|
+
1. Use lowercase for atoms (constants)
|
|
54
|
+
2. Use descriptive predicate names (parent, owns, works_at, etc.)
|
|
55
|
+
3. Keep predicates consistent in arity
|
|
56
|
+
4. Extract all relevant relationships
|
|
57
|
+
5. Use snake_case for multi-word predicates
|
|
58
|
+
6. Each fact should be on a separate line
|
|
59
|
+
7. End each fact with a period${predicateInfo}${domainInfo}
|
|
60
|
+
|
|
61
|
+
Text:
|
|
62
|
+
"${context.naturalLanguageText}"
|
|
63
|
+
|
|
64
|
+
Facts:`;
|
|
65
|
+
}
|
|
66
|
+
export function createNLToRulesPrompt(context) {
|
|
67
|
+
const predicateInfo = context.existingPredicates?.length
|
|
68
|
+
? `\n\nExisting predicates:\n${context.existingPredicates.map((p) => ` - ${p}`).join('\n')}`
|
|
69
|
+
: '';
|
|
70
|
+
const rulesInfo = context.existingRules?.length
|
|
71
|
+
? `\n\nExisting rules:\n${context.existingRules.join('\n')}`
|
|
72
|
+
: '';
|
|
73
|
+
return `You are a logic programming expert that converts natural language rules into Prolog-style rules.
|
|
74
|
+
|
|
75
|
+
Convert the following natural language rule into a Prolog-style rule.
|
|
76
|
+
|
|
77
|
+
Rules:
|
|
78
|
+
1. Use :- for "if" relationships
|
|
79
|
+
2. Head of rule comes first, body after :-
|
|
80
|
+
3. Multiple conditions in body separated by commas
|
|
81
|
+
4. Variables start with uppercase
|
|
82
|
+
5. Use meaningful variable names
|
|
83
|
+
6. End with a period${predicateInfo}${rulesInfo}
|
|
84
|
+
|
|
85
|
+
Natural language rule:
|
|
86
|
+
"${context.naturalLanguageRule}"
|
|
87
|
+
|
|
88
|
+
Prolog rule:`;
|
|
89
|
+
}
|
|
90
|
+
export function createExplainResultPrompt(context) {
|
|
91
|
+
const queryStr = context.query.map(termToString).join(', ');
|
|
92
|
+
const solutionsStr = context.result.solutions
|
|
93
|
+
.map((s, i) => {
|
|
94
|
+
const bindings = [];
|
|
95
|
+
for (const [k, v] of s) {
|
|
96
|
+
if (!k.startsWith('_')) {
|
|
97
|
+
bindings.push(`${k} = ${termToString(v)}`);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return ` Solution ${i + 1}: ${bindings.length > 0 ? bindings.join(', ') : 'true'}`;
|
|
101
|
+
})
|
|
102
|
+
.join('\n');
|
|
103
|
+
return `You are explaining the results of a logic query to a user.
|
|
104
|
+
|
|
105
|
+
Query: ${queryStr}
|
|
106
|
+
|
|
107
|
+
${context.result.success
|
|
108
|
+
? `Found ${context.result.solutions.length} solution(s):
|
|
109
|
+
${solutionsStr}`
|
|
110
|
+
: 'Query failed - no solutions found.'}
|
|
111
|
+
|
|
112
|
+
Explain these results in natural language. Be clear and concise.
|
|
113
|
+
If there are multiple solutions, explain what they represent.
|
|
114
|
+
If the query failed, suggest why it might have failed.
|
|
115
|
+
|
|
116
|
+
Explanation:`;
|
|
117
|
+
}
|
|
118
|
+
export function createGenerateQueriesPrompt(context) {
|
|
119
|
+
const predicates = context.knowledgeBase.getPredicates();
|
|
120
|
+
const count = context.count || 5;
|
|
121
|
+
const topicInfo = context.topic ? ` about "${context.topic}"` : '';
|
|
122
|
+
return `You are generating interesting queries for a logic programming knowledge base.
|
|
123
|
+
|
|
124
|
+
Available predicates:
|
|
125
|
+
${predicates.map((p) => ` - ${p}`).join('\n')}
|
|
126
|
+
|
|
127
|
+
Generate ${count} interesting and diverse queries${topicInfo}.
|
|
128
|
+
|
|
129
|
+
Requirements:
|
|
130
|
+
1. Each query should be valid for the available predicates
|
|
131
|
+
2. Include a mix of simple and complex queries
|
|
132
|
+
3. Use variables where appropriate
|
|
133
|
+
4. Include at least one query that might return multiple solutions
|
|
134
|
+
5. Format: one query per line, ending with ?
|
|
135
|
+
|
|
136
|
+
Queries:`;
|
|
137
|
+
}
|
|
138
|
+
export function createDebugQueryPrompt(context) {
|
|
139
|
+
const queryStr = context.query.map(termToString).join(', ');
|
|
140
|
+
const predicates = context.knowledgeBase.getPredicates();
|
|
141
|
+
const kbSnapshot = context.knowledgeBase.toString().slice(0, 1000);
|
|
142
|
+
const expectedInfo = context.expectedResult ? `\nExpected result: ${context.expectedResult}` : '';
|
|
143
|
+
return `You are debugging a logic programming query that may have unexpected results.
|
|
144
|
+
|
|
145
|
+
Query: ${queryStr}?
|
|
146
|
+
Result: ${context.result.success ? 'success' : 'failure'} with ${context.result.solutions.length} solutions${expectedInfo}
|
|
147
|
+
|
|
148
|
+
Available predicates:
|
|
149
|
+
${predicates.map((p) => ` - ${p}`).join('\n')}
|
|
150
|
+
|
|
151
|
+
Knowledge base (first 1000 chars):
|
|
152
|
+
${kbSnapshot}
|
|
153
|
+
|
|
154
|
+
Analyze the query and explain:
|
|
155
|
+
1. Why the query produced this result
|
|
156
|
+
2. Possible issues with the query
|
|
157
|
+
3. Suggestions for fixing the query
|
|
158
|
+
|
|
159
|
+
Analysis:`;
|
|
160
|
+
}
|
|
161
|
+
export function formatQueryForLLM(query) {
|
|
162
|
+
return query.map(termToString).join(', ') + '?';
|
|
163
|
+
}
|
|
164
|
+
export function formatClauseForLLM(clause) {
|
|
165
|
+
const head = termToString(clause.head);
|
|
166
|
+
if (clause.body.length === 0) {
|
|
167
|
+
return `${head}.`;
|
|
168
|
+
}
|
|
169
|
+
const body = clause.body.map(termToString).join(', ');
|
|
170
|
+
return `${head} :- ${body}.`;
|
|
171
|
+
}
|
|
172
|
+
export function formatKnowledgeBaseForLLM(kb, maxClauses = 50) {
|
|
173
|
+
const clauses = kb.getAllClauses();
|
|
174
|
+
const sample = clauses.slice(0, maxClauses);
|
|
175
|
+
const remaining = clauses.length - sample.length;
|
|
176
|
+
let result = sample.map(formatClauseForLLM).join('\n');
|
|
177
|
+
if (remaining > 0) {
|
|
178
|
+
result += `\n... and ${remaining} more clauses`;
|
|
179
|
+
}
|
|
180
|
+
return result;
|
|
181
|
+
}
|
|
182
|
+
export function parseLLMQueryResponse(response) {
|
|
183
|
+
const lines = response
|
|
184
|
+
.split('\n')
|
|
185
|
+
.map((l) => l.trim())
|
|
186
|
+
.filter((l) => l.length > 0 && !l.startsWith('#') && !l.startsWith('//'));
|
|
187
|
+
const queries = [];
|
|
188
|
+
for (const line of lines) {
|
|
189
|
+
const cleaned = line.replace(/^\d+\.\s*/, '').trim();
|
|
190
|
+
if (cleaned.endsWith('?') || cleaned.endsWith('.')) {
|
|
191
|
+
queries.push(cleaned.endsWith('?') ? cleaned : cleaned.replace(/\.$/, '?'));
|
|
192
|
+
}
|
|
193
|
+
else if (cleaned.includes('(') && cleaned.includes(')')) {
|
|
194
|
+
queries.push(cleaned + '?');
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return {
|
|
198
|
+
success: queries.length > 0,
|
|
199
|
+
queries: queries.length > 0 ? queries : undefined,
|
|
200
|
+
error: queries.length === 0 ? 'No valid queries found in response' : undefined,
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
export function parseLLMFactsResponse(response) {
|
|
204
|
+
const lines = response
|
|
205
|
+
.split('\n')
|
|
206
|
+
.map((l) => l.trim())
|
|
207
|
+
.filter((l) => l.length > 0 && !l.startsWith('#') && !l.startsWith('//'));
|
|
208
|
+
const facts = [];
|
|
209
|
+
const rules = [];
|
|
210
|
+
for (const line of lines) {
|
|
211
|
+
const cleaned = line.replace(/^\d+\.\s*/, '').trim();
|
|
212
|
+
if (!cleaned.includes('(') || !cleaned.endsWith('.')) {
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
if (cleaned.includes(':-')) {
|
|
216
|
+
rules.push(cleaned);
|
|
217
|
+
}
|
|
218
|
+
else {
|
|
219
|
+
facts.push(cleaned);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
const success = facts.length > 0 || rules.length > 0;
|
|
223
|
+
return {
|
|
224
|
+
success,
|
|
225
|
+
facts: facts.length > 0 ? facts : undefined,
|
|
226
|
+
rules: rules.length > 0 ? rules : undefined,
|
|
227
|
+
error: success ? undefined : 'No valid facts or rules found in response',
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
//# sourceMappingURL=prompts.js.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AAI,CAAC,CAAC,CAAC;IAE5E,MAAM,KAAK,GAAa,EAAE,CAAC;IAC3B,MAAM,KAAK,GAAa,EAAE,CAAC;IAE3B,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,WAAW,EAAE,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;QAErD,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;YACrD,SAAS;QACX,CAAC;QAED,IAAI,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;YAC3B,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACtB,CAAC;aAAM,CAAC;YACN,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACtB,CAAC;IACH,CAAC;IAED,MAAM,OAAO,GAAG,KAAK,CAAC,MAAM,GAAG,CAAC,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC;IAErD,OAAO;QACL,OAAO;QACP,KAAK,EAAE,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS;QAC3C,KAAK,EAAE,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS;QAC3C,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,2CAA2C;KACzE,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { ProofNode, ProofTree, Term, Substitution } from '@cogitator-ai/types';
|
|
2
|
+
export interface ProofTreeOptions {
|
|
3
|
+
maxDepth?: number;
|
|
4
|
+
showSubstitutions?: boolean;
|
|
5
|
+
showClauseUsed?: boolean;
|
|
6
|
+
collapseFailures?: boolean;
|
|
7
|
+
indent?: string;
|
|
8
|
+
}
|
|
9
|
+
export declare function formatProofTree(tree: ProofTree, options?: ProofTreeOptions): string;
|
|
10
|
+
export interface ProofPath {
|
|
11
|
+
nodes: ProofNode[];
|
|
12
|
+
bindings: Substitution;
|
|
13
|
+
}
|
|
14
|
+
export declare function extractProofPaths(tree: ProofTree): ProofPath[];
|
|
15
|
+
export declare function formatProofPath(path: ProofPath, template?: Term): string;
|
|
16
|
+
export interface ProofStats {
|
|
17
|
+
totalNodes: number;
|
|
18
|
+
successNodes: number;
|
|
19
|
+
failureNodes: number;
|
|
20
|
+
cutNodes: number;
|
|
21
|
+
maxBranchingFactor: number;
|
|
22
|
+
avgBranchingFactor: number;
|
|
23
|
+
leafNodes: number;
|
|
24
|
+
uniquePredicates: Set<string>;
|
|
25
|
+
}
|
|
26
|
+
export declare function analyzeProofTree(tree: ProofTree): ProofStats;
|
|
27
|
+
export declare function pruneProofTree(tree: ProofTree, keepSuccessOnly?: boolean): ProofTree;
|
|
28
|
+
export declare function proofTreeToMermaid(tree: ProofTree): string;
|
|
29
|
+
export declare function proofTreeToJSON(tree: ProofTree): object;
|
|
30
|
+
//# sourceMappingURL=proof-tree.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"proof-tree.d.ts","sourceRoot":"","sources":["../../src/logic/proof-tree.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,SAAS,EAAE,IAAI,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAGpF,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAC5B,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAUD,wBAAgB,eAAe,CAAC,IAAI,EAAE,SAAS,EAAE,OAAO,GAAE,gBAAqB,GAAG,MAAM,CAiBvF;AA+ED,MAAM,WAAW,SAAS;IACxB,KAAK,EAAE,SAAS,EAAE,CAAC;IACnB,QAAQ,EAAE,YAAY,CAAC;CACxB;AAED,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,SAAS,GAAG,SAAS,EAAE,CAwB9D;AAED,wBAAgB,eAAe,CAAC,IAAI,EAAE,SAAS,EAAE,QAAQ,CAAC,EAAE,IAAI,GAAG,MAAM,CAsCxE;AAED,MAAM,WAAW,UAAU;IACzB,UAAU,EAAE,MAAM,CAAC;IACnB,YAAY,EAAE,MAAM,CAAC;IACrB,YAAY,EAAE,MAAM,CAAC;IACrB,QAAQ,EAAE,MAAM,CAAC;IACjB,kBAAkB,EAAE,MAAM,CAAC;IAC3B,kBAAkB,EAAE,MAAM,CAAC;IAC3B,SAAS,EAAE,MAAM,CAAC;IAClB,gBAAgB,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;CAC/B;AAED,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,SAAS,GAAG,UAAU,CAuD5D;AAED,wBAAgB,cAAc,CAAC,IAAI,EAAE,SAAS,EAAE,eAAe,GAAE,OAAc,GAAG,SAAS,CA6B1F;AAED,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,SAAS,GAAG,MAAM,CA6B1D;AAmBD,wBAAgB,eAAe,CAAC,IAAI,EAAE,SAAS,GAAG,MAAM,CAkCvD"}
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
import { termToString, applySubstitution } from './unification';
|
|
2
|
+
const defaultOptions = {
|
|
3
|
+
maxDepth: Infinity,
|
|
4
|
+
showSubstitutions: true,
|
|
5
|
+
showClauseUsed: true,
|
|
6
|
+
collapseFailures: false,
|
|
7
|
+
indent: ' ',
|
|
8
|
+
};
|
|
9
|
+
export function formatProofTree(tree, options = {}) {
|
|
10
|
+
const opts = { ...defaultOptions, ...options };
|
|
11
|
+
const lines = [];
|
|
12
|
+
lines.push('╔════════════════════════════════════════════════════════════════╗');
|
|
13
|
+
lines.push('║ PROOF TREE ║');
|
|
14
|
+
lines.push('╠════════════════════════════════════════════════════════════════╣');
|
|
15
|
+
lines.push(`║ Solutions: ${tree.solutions.length}`.padEnd(67) + '║');
|
|
16
|
+
lines.push(`║ Explored nodes: ${tree.exploredNodes}`.padEnd(67) + '║');
|
|
17
|
+
lines.push(`║ Max depth: ${tree.maxDepth}`.padEnd(67) + '║');
|
|
18
|
+
lines.push(`║ Duration: ${tree.duration}ms`.padEnd(67) + '║');
|
|
19
|
+
lines.push('╚════════════════════════════════════════════════════════════════╝');
|
|
20
|
+
lines.push('');
|
|
21
|
+
formatNode(tree.root, 0, '', true, lines, opts);
|
|
22
|
+
return lines.join('\n');
|
|
23
|
+
}
|
|
24
|
+
function formatNode(node, depth, prefix, isLast, lines, opts) {
|
|
25
|
+
if (depth > opts.maxDepth) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
if (opts.collapseFailures && node.status === 'failure' && node.children.length === 0) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
const statusIcon = getStatusIcon(node.status);
|
|
32
|
+
const connector = depth === 0 ? '' : isLast ? '└── ' : '├── ';
|
|
33
|
+
const goalStr = termToString(node.goal);
|
|
34
|
+
let line = `${prefix}${connector}${statusIcon} ${goalStr}`;
|
|
35
|
+
if (opts.showSubstitutions && node.substitution.size > 0) {
|
|
36
|
+
const relevantBindings = getRelevantBindings(node);
|
|
37
|
+
if (relevantBindings.length > 0) {
|
|
38
|
+
line += ` {${relevantBindings.join(', ')}}`;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (opts.showClauseUsed && node.clause) {
|
|
42
|
+
const clauseHead = termToString(node.clause.head);
|
|
43
|
+
if (node.clause.body.length > 0) {
|
|
44
|
+
const clauseBody = node.clause.body.map(termToString).join(', ');
|
|
45
|
+
line += ` ← ${clauseHead} :- ${clauseBody}`;
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
line += ` ← ${clauseHead}`;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
lines.push(line);
|
|
52
|
+
const childPrefix = prefix + (depth === 0 ? '' : isLast ? ' ' : '│ ');
|
|
53
|
+
for (let i = 0; i < node.children.length; i++) {
|
|
54
|
+
const child = node.children[i];
|
|
55
|
+
const isLastChild = i === node.children.length - 1;
|
|
56
|
+
formatNode(child, depth + 1, childPrefix, isLastChild, lines, opts);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function getStatusIcon(status) {
|
|
60
|
+
switch (status) {
|
|
61
|
+
case 'success':
|
|
62
|
+
return '✓';
|
|
63
|
+
case 'failure':
|
|
64
|
+
return '✗';
|
|
65
|
+
case 'pending':
|
|
66
|
+
return '○';
|
|
67
|
+
case 'cut':
|
|
68
|
+
return '!';
|
|
69
|
+
default:
|
|
70
|
+
return '?';
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
function getRelevantBindings(node) {
|
|
74
|
+
const bindings = [];
|
|
75
|
+
for (const [varName, term] of node.substitution) {
|
|
76
|
+
if (!varName.startsWith('_') && !varName.includes('_')) {
|
|
77
|
+
bindings.push(`${varName}=${termToString(term)}`);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return bindings;
|
|
81
|
+
}
|
|
82
|
+
export function extractProofPaths(tree) {
|
|
83
|
+
const paths = [];
|
|
84
|
+
function traverse(node, currentPath) {
|
|
85
|
+
currentPath.push(node);
|
|
86
|
+
if (node.status === 'success' && node.children.length === 0) {
|
|
87
|
+
paths.push({
|
|
88
|
+
nodes: [...currentPath],
|
|
89
|
+
bindings: node.substitution,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
for (const child of node.children) {
|
|
93
|
+
if (child.status === 'success' || child.status === 'cut') {
|
|
94
|
+
traverse(child, currentPath);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
currentPath.pop();
|
|
98
|
+
}
|
|
99
|
+
traverse(tree.root, []);
|
|
100
|
+
return paths;
|
|
101
|
+
}
|
|
102
|
+
export function formatProofPath(path, template) {
|
|
103
|
+
const lines = [];
|
|
104
|
+
lines.push('Proof path:');
|
|
105
|
+
for (let i = 0; i < path.nodes.length; i++) {
|
|
106
|
+
const node = path.nodes[i];
|
|
107
|
+
const indent = ' '.repeat(i);
|
|
108
|
+
const goalStr = termToString(node.goal);
|
|
109
|
+
if (node.clause) {
|
|
110
|
+
const clauseStr = node.clause.body.length > 0
|
|
111
|
+
? `${termToString(node.clause.head)} :- ${node.clause.body.map(termToString).join(', ')}`
|
|
112
|
+
: termToString(node.clause.head);
|
|
113
|
+
lines.push(`${indent}${goalStr} [using: ${clauseStr}]`);
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
lines.push(`${indent}${goalStr}`);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
if (path.bindings.size > 0) {
|
|
120
|
+
lines.push('');
|
|
121
|
+
lines.push('Bindings:');
|
|
122
|
+
for (const [varName, term] of path.bindings) {
|
|
123
|
+
if (!varName.startsWith('_')) {
|
|
124
|
+
lines.push(` ${varName} = ${termToString(term)}`);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
if (template) {
|
|
129
|
+
lines.push('');
|
|
130
|
+
const result = applySubstitution(template, path.bindings);
|
|
131
|
+
lines.push(`Result: ${termToString(result)}`);
|
|
132
|
+
}
|
|
133
|
+
return lines.join('\n');
|
|
134
|
+
}
|
|
135
|
+
export function analyzeProofTree(tree) {
|
|
136
|
+
const stats = {
|
|
137
|
+
totalNodes: 0,
|
|
138
|
+
successNodes: 0,
|
|
139
|
+
failureNodes: 0,
|
|
140
|
+
cutNodes: 0,
|
|
141
|
+
maxBranchingFactor: 0,
|
|
142
|
+
avgBranchingFactor: 0,
|
|
143
|
+
leafNodes: 0,
|
|
144
|
+
uniquePredicates: new Set(),
|
|
145
|
+
};
|
|
146
|
+
let totalBranching = 0;
|
|
147
|
+
let internalNodes = 0;
|
|
148
|
+
function traverse(node) {
|
|
149
|
+
stats.totalNodes++;
|
|
150
|
+
if (node.goal.functor) {
|
|
151
|
+
stats.uniquePredicates.add(`${node.goal.functor}/${node.goal.args.length}`);
|
|
152
|
+
}
|
|
153
|
+
switch (node.status) {
|
|
154
|
+
case 'success':
|
|
155
|
+
stats.successNodes++;
|
|
156
|
+
break;
|
|
157
|
+
case 'failure':
|
|
158
|
+
stats.failureNodes++;
|
|
159
|
+
break;
|
|
160
|
+
case 'cut':
|
|
161
|
+
stats.cutNodes++;
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
if (node.children.length === 0) {
|
|
165
|
+
stats.leafNodes++;
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
internalNodes++;
|
|
169
|
+
totalBranching += node.children.length;
|
|
170
|
+
if (node.children.length > stats.maxBranchingFactor) {
|
|
171
|
+
stats.maxBranchingFactor = node.children.length;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
for (const child of node.children) {
|
|
175
|
+
traverse(child);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
traverse(tree.root);
|
|
179
|
+
stats.avgBranchingFactor = internalNodes > 0 ? totalBranching / internalNodes : 0;
|
|
180
|
+
return stats;
|
|
181
|
+
}
|
|
182
|
+
export function pruneProofTree(tree, keepSuccessOnly = true) {
|
|
183
|
+
function pruneNode(node) {
|
|
184
|
+
if (keepSuccessOnly && node.status === 'failure') {
|
|
185
|
+
return null;
|
|
186
|
+
}
|
|
187
|
+
const prunedChildren = [];
|
|
188
|
+
for (const child of node.children) {
|
|
189
|
+
const pruned = pruneNode(child);
|
|
190
|
+
if (pruned) {
|
|
191
|
+
prunedChildren.push(pruned);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return {
|
|
195
|
+
...node,
|
|
196
|
+
children: prunedChildren,
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
const prunedRoot = pruneNode(tree.root);
|
|
200
|
+
return {
|
|
201
|
+
root: prunedRoot || tree.root,
|
|
202
|
+
solutions: tree.solutions,
|
|
203
|
+
exploredNodes: tree.exploredNodes,
|
|
204
|
+
maxDepth: tree.maxDepth,
|
|
205
|
+
duration: tree.duration,
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
export function proofTreeToMermaid(tree) {
|
|
209
|
+
const lines = ['graph TD'];
|
|
210
|
+
let nodeCounter = 0;
|
|
211
|
+
const nodeIds = new Map();
|
|
212
|
+
function getNodeId(node) {
|
|
213
|
+
if (!nodeIds.has(node)) {
|
|
214
|
+
nodeIds.set(node, `N${nodeCounter++}`);
|
|
215
|
+
}
|
|
216
|
+
return nodeIds.get(node);
|
|
217
|
+
}
|
|
218
|
+
function traverse(node) {
|
|
219
|
+
const id = getNodeId(node);
|
|
220
|
+
const label = escapeLabel(termToString(node.goal));
|
|
221
|
+
const style = getNodeStyle(node.status);
|
|
222
|
+
lines.push(` ${id}["${label}"]${style}`);
|
|
223
|
+
for (const child of node.children) {
|
|
224
|
+
const childId = getNodeId(child);
|
|
225
|
+
traverse(child);
|
|
226
|
+
lines.push(` ${id} --> ${childId}`);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
traverse(tree.root);
|
|
230
|
+
return lines.join('\n');
|
|
231
|
+
}
|
|
232
|
+
function escapeLabel(str) {
|
|
233
|
+
return str.replace(/"/g, '\\"').replace(/\n/g, ' ');
|
|
234
|
+
}
|
|
235
|
+
function getNodeStyle(status) {
|
|
236
|
+
switch (status) {
|
|
237
|
+
case 'success':
|
|
238
|
+
return ':::success';
|
|
239
|
+
case 'failure':
|
|
240
|
+
return ':::failure';
|
|
241
|
+
case 'cut':
|
|
242
|
+
return ':::cut';
|
|
243
|
+
default:
|
|
244
|
+
return '';
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
export function proofTreeToJSON(tree) {
|
|
248
|
+
function nodeToJSON(node) {
|
|
249
|
+
return {
|
|
250
|
+
id: node.id,
|
|
251
|
+
goal: termToString(node.goal),
|
|
252
|
+
status: node.status,
|
|
253
|
+
depth: node.depth,
|
|
254
|
+
clause: node.clause
|
|
255
|
+
? {
|
|
256
|
+
head: termToString(node.clause.head),
|
|
257
|
+
body: node.clause.body.map(termToString),
|
|
258
|
+
}
|
|
259
|
+
: null,
|
|
260
|
+
children: node.children.map(nodeToJSON),
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
return {
|
|
264
|
+
root: nodeToJSON(tree.root),
|
|
265
|
+
solutions: tree.solutions.map((s) => {
|
|
266
|
+
const obj = {};
|
|
267
|
+
for (const [k, v] of s) {
|
|
268
|
+
if (!k.startsWith('_')) {
|
|
269
|
+
obj[k] = termToString(v);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
return obj;
|
|
273
|
+
}),
|
|
274
|
+
stats: {
|
|
275
|
+
exploredNodes: tree.exploredNodes,
|
|
276
|
+
maxDepth: tree.maxDepth,
|
|
277
|
+
duration: tree.duration,
|
|
278
|
+
},
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
//# sourceMappingURL=proof-tree.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"proof-tree.js","sourceRoot":"","sources":["../../src/logic/proof-tree.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAUhE,MAAM,cAAc,GAA+B;IACjD,QAAQ,EAAE,QAAQ;IAClB,iBAAiB,EAAE,IAAI;IACvB,cAAc,EAAE,IAAI;IACpB,gBAAgB,EAAE,KAAK;IACvB,MAAM,EAAE,IAAI;CACb,CAAC;AAEF,MAAM,UAAU,eAAe,CAAC,IAAe,EAAE,UAA4B,EAAE;IAC7E,MAAM,IAAI,GAAG,EAAE,GAAG,cAAc,EAAE,GAAG,OAAO,EAAE,CAAC;IAC/C,MAAM,KAAK,GAAa,EAAE,CAAC;IAE3B,KAAK,CAAC,IAAI,CAAC,oEAAoE,CAAC,CAAC;IACjF,KAAK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CAAC;YACD,OAAO,GAAG,CAAC;QACb,CAAC,CAAC;QACF,KAAK,EAAE;YACL,aAAa,EAAE,IAAI,CAAC,aAAa;YACjC,QAAQ,EAAE,IAAI,CAAC,QAAQ;YACvB,QAAQ,EAAE,IAAI,CAAC,QAAQ;SACxB;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Term, CompoundTerm, LogicQueryResult, LogicProgrammingConfig } from '@cogitator-ai/types';
|
|
2
|
+
import { KnowledgeBase } from './knowledge-base';
|
|
3
|
+
export declare class SLDResolver {
|
|
4
|
+
private kb;
|
|
5
|
+
private config;
|
|
6
|
+
constructor(kb: KnowledgeBase, config?: Partial<LogicProgrammingConfig>);
|
|
7
|
+
query(goals: CompoundTerm[]): LogicQueryResult;
|
|
8
|
+
prove(goal: CompoundTerm): boolean;
|
|
9
|
+
findAll(goal: CompoundTerm, template: Term): Term[];
|
|
10
|
+
updateConfig(config: Partial<LogicProgrammingConfig>): void;
|
|
11
|
+
getKnowledgeBase(): KnowledgeBase;
|
|
12
|
+
}
|
|
13
|
+
export declare function createResolver(kb: KnowledgeBase, config?: Partial<LogicProgrammingConfig>): SLDResolver;
|
|
14
|
+
export declare function queryKnowledgeBase(kb: KnowledgeBase, queryString: string, config?: Partial<LogicProgrammingConfig>): LogicQueryResult;
|
|
15
|
+
export declare function formatSolutions(result: LogicQueryResult): string;
|
|
16
|
+
//# sourceMappingURL=resolver.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolver.d.ts","sourceRoot":"","sources":["../../src/logic/resolver.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,IAAI,EACJ,YAAY,EAKZ,gBAAgB,EAChB,sBAAsB,EACvB,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAkZjD,qBAAa,WAAW;IACtB,OAAO,CAAC,EAAE,CAAgB;IAC1B,OAAO,CAAC,MAAM,CAAmC;gBAErC,EAAE,EAAE,aAAa,EAAE,MAAM,GAAE,OAAO,CAAC,sBAAsB,CAAM;IAK3E,KAAK,CAAC,KAAK,EAAE,YAAY,EAAE,GAAG,gBAAgB;IAuD9C,KAAK,CAAC,IAAI,EAAE,YAAY,GAAG,OAAO;IAKlC,OAAO,CAAC,IAAI,EAAE,YAAY,EAAE,QAAQ,EAAE,IAAI,GAAG,IAAI,EAAE;IAKnD,YAAY,CAAC,MAAM,EAAE,OAAO,CAAC,sBAAsB,CAAC,GAAG,IAAI;IAI3D,gBAAgB,IAAI,aAAa;CAGlC;AAED,wBAAgB,cAAc,CAC5B,EAAE,EAAE,aAAa,EACjB,MAAM,CAAC,EAAE,OAAO,CAAC,sBAAsB,CAAC,GACvC,WAAW,CAEb;AAED,wBAAgB,kBAAkB,CAChC,EAAE,EAAE,aAAa,EACjB,WAAW,EAAE,MAAM,EACnB,MAAM,CAAC,EAAE,OAAO,CAAC,sBAAsB,CAAC,GACvC,gBAAgB,CAelB;AAED,wBAAgB,eAAe,CAAC,MAAM,EAAE,gBAAgB,GAAG,MAAM,CA+BhE"}
|