@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
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Cogitator Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,494 @@
|
|
|
1
|
+
# @cogitator-ai/neuro-symbolic
|
|
2
|
+
|
|
3
|
+
Neuro-symbolic AI package for hybrid neural-symbolic reasoning. Combines LLM-based understanding with formal methods for verifiable, explainable AI.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @cogitator-ai/neuro-symbolic
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
For Z3 constraint solving support:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
pnpm add z3-solver
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Features
|
|
18
|
+
|
|
19
|
+
- **Logic Programming** - Prolog-style rules with unification and SLD resolution
|
|
20
|
+
- **Knowledge Graph Queries** - SPARQL-like query language with natural language interface
|
|
21
|
+
- **Constraint Solving** - SAT/SMT solving with Z3 WASM or pure-TS fallback
|
|
22
|
+
- **Plan Verification** - PDDL-like action schemas with invariant checking and repair
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
## Quick Start
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
import {
|
|
30
|
+
createNeuroSymbolic,
|
|
31
|
+
ConstraintBuilder,
|
|
32
|
+
variable,
|
|
33
|
+
constant,
|
|
34
|
+
} from '@cogitator-ai/neuro-symbolic';
|
|
35
|
+
|
|
36
|
+
const ns = createNeuroSymbolic();
|
|
37
|
+
|
|
38
|
+
// Logic Programming
|
|
39
|
+
ns.loadLogicProgram(`
|
|
40
|
+
parent(tom, mary).
|
|
41
|
+
parent(mary, ann).
|
|
42
|
+
grandparent(X, Z) :- parent(X, Y), parent(Y, Z).
|
|
43
|
+
`);
|
|
44
|
+
|
|
45
|
+
const result = ns.queryLogic('grandparent(tom, X)?');
|
|
46
|
+
console.log(result.solutions); // X = ann
|
|
47
|
+
|
|
48
|
+
// Constraint Solving
|
|
49
|
+
const problem = ConstraintBuilder.create()
|
|
50
|
+
.int('x', 1, 10)
|
|
51
|
+
.int('y', 1, 10)
|
|
52
|
+
.assert(variable('x').add(variable('y')).eq(constant(15)))
|
|
53
|
+
.build();
|
|
54
|
+
|
|
55
|
+
const solution = await ns.solve(problem);
|
|
56
|
+
console.log(solution); // { x: 5, y: 10 } or similar
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
---
|
|
60
|
+
|
|
61
|
+
## Logic Programming
|
|
62
|
+
|
|
63
|
+
Prolog-style logic programming with backward chaining and unification.
|
|
64
|
+
|
|
65
|
+
### Loading Programs
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
import { createNeuroSymbolic } from '@cogitator-ai/neuro-symbolic';
|
|
69
|
+
|
|
70
|
+
const ns = createNeuroSymbolic();
|
|
71
|
+
|
|
72
|
+
// Load facts and rules
|
|
73
|
+
ns.loadLogicProgram(`
|
|
74
|
+
% Facts
|
|
75
|
+
human(socrates).
|
|
76
|
+
human(plato).
|
|
77
|
+
|
|
78
|
+
% Rules
|
|
79
|
+
mortal(X) :- human(X).
|
|
80
|
+
|
|
81
|
+
% Lists
|
|
82
|
+
append([], L, L).
|
|
83
|
+
append([H|T], L, [H|R]) :- append(T, L, R).
|
|
84
|
+
`);
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### Querying
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
// Simple query
|
|
91
|
+
const result = ns.queryLogic('mortal(socrates)?');
|
|
92
|
+
console.log(result.success); // true
|
|
93
|
+
|
|
94
|
+
// Query with variables
|
|
95
|
+
const result = ns.queryLogic('mortal(X)?');
|
|
96
|
+
for (const solution of result.solutions) {
|
|
97
|
+
console.log(solution.get('X')); // socrates, plato
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Multiple solutions
|
|
101
|
+
const result = ns.queryLogic('append(X, Y, [1,2,3])?');
|
|
102
|
+
// X=[], Y=[1,2,3]
|
|
103
|
+
// X=[1], Y=[2,3]
|
|
104
|
+
// X=[1,2], Y=[3]
|
|
105
|
+
// X=[1,2,3], Y=[]
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### Proof Trees
|
|
109
|
+
|
|
110
|
+
```typescript
|
|
111
|
+
import { formatProofTree, proofTreeToMermaid } from '@cogitator-ai/neuro-symbolic';
|
|
112
|
+
|
|
113
|
+
const result = ns.queryLogic('grandparent(tom, X)?');
|
|
114
|
+
console.log(formatProofTree(result.proofTree));
|
|
115
|
+
console.log(proofTreeToMermaid(result.proofTree)); // Mermaid diagram
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Built-in Predicates
|
|
119
|
+
|
|
120
|
+
| Predicate | Description |
|
|
121
|
+
| ---------------------------- | --------------------- |
|
|
122
|
+
| `is/2` | Arithmetic evaluation |
|
|
123
|
+
| `=/2` | Unification |
|
|
124
|
+
| `\+/1` | Negation as failure |
|
|
125
|
+
| `>/2`, `</2`, `>=/2`, `=</2` | Comparisons |
|
|
126
|
+
| `member/2` | List membership |
|
|
127
|
+
| `append/3` | List concatenation |
|
|
128
|
+
| `length/2` | List length |
|
|
129
|
+
| `reverse/2` | List reversal |
|
|
130
|
+
| `findall/3` | Collect all solutions |
|
|
131
|
+
| `!/0` | Cut |
|
|
132
|
+
|
|
133
|
+
---
|
|
134
|
+
|
|
135
|
+
## Knowledge Graph Queries
|
|
136
|
+
|
|
137
|
+
SPARQL-like query language with natural language interface.
|
|
138
|
+
|
|
139
|
+
### Query Builder
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
import { GraphQueryBuilder, variable, executeQuery } from '@cogitator-ai/neuro-symbolic';
|
|
143
|
+
|
|
144
|
+
const query = new GraphQueryBuilder()
|
|
145
|
+
.select()
|
|
146
|
+
.pattern(variable('person'), 'worksAt', 'Google')
|
|
147
|
+
.pattern(variable('person'), 'hasSkill', variable('skill'))
|
|
148
|
+
.filter('skill', 'contains', 'Python')
|
|
149
|
+
.orderBy('person', 'asc')
|
|
150
|
+
.limit(10)
|
|
151
|
+
.build();
|
|
152
|
+
|
|
153
|
+
const result = await executeQuery(query, { adapter: graphAdapter, agentId: 'agent-1' });
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### Natural Language Queries
|
|
157
|
+
|
|
158
|
+
```typescript
|
|
159
|
+
const result = await ns.askGraph('Who works at Google and knows Python?');
|
|
160
|
+
console.log(result.naturalLanguageResponse);
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### Reasoning Engine
|
|
164
|
+
|
|
165
|
+
```typescript
|
|
166
|
+
import { createReasoningEngine, findPath, multiHopQuery } from '@cogitator-ai/neuro-symbolic';
|
|
167
|
+
|
|
168
|
+
const engine = createReasoningEngine(graphAdapter);
|
|
169
|
+
|
|
170
|
+
// Find path between entities
|
|
171
|
+
const path = await findPath(graphAdapter, 'Alice', 'CompanyX', { maxHops: 3 });
|
|
172
|
+
|
|
173
|
+
// Multi-hop query
|
|
174
|
+
const results = await multiHopQuery(graphAdapter, 'Alice', ['worksAt', 'locatedIn'], {
|
|
175
|
+
maxHops: 2,
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
// Inference
|
|
179
|
+
const inferred = await engine.infer({
|
|
180
|
+
enableTransitivity: true,
|
|
181
|
+
enableInverse: true,
|
|
182
|
+
enableComposition: true,
|
|
183
|
+
});
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
---
|
|
187
|
+
|
|
188
|
+
## Constraint Solving
|
|
189
|
+
|
|
190
|
+
SAT/SMT solving with fluent DSL.
|
|
191
|
+
|
|
192
|
+
### Building Constraints
|
|
193
|
+
|
|
194
|
+
```typescript
|
|
195
|
+
import {
|
|
196
|
+
ConstraintBuilder,
|
|
197
|
+
variable,
|
|
198
|
+
constant,
|
|
199
|
+
and,
|
|
200
|
+
or,
|
|
201
|
+
not,
|
|
202
|
+
allDifferent,
|
|
203
|
+
} from '@cogitator-ai/neuro-symbolic';
|
|
204
|
+
|
|
205
|
+
const problem = ConstraintBuilder.create()
|
|
206
|
+
// Define variables
|
|
207
|
+
.bool('a')
|
|
208
|
+
.bool('b')
|
|
209
|
+
.int('x', 0, 100)
|
|
210
|
+
.int('y', 0, 100)
|
|
211
|
+
.real('z', 0.0, 1.0)
|
|
212
|
+
|
|
213
|
+
// Add constraints
|
|
214
|
+
.assert(variable('a').or(variable('b')))
|
|
215
|
+
.assert(variable('x').add(variable('y')).lte(constant(50)))
|
|
216
|
+
.assert(variable('z').mul(constant(2)).gt(constant(0.5)))
|
|
217
|
+
.assert(allDifferent(variable('x'), variable('y')))
|
|
218
|
+
|
|
219
|
+
// Optimization objective
|
|
220
|
+
.maximize(variable('x').add(variable('y')))
|
|
221
|
+
|
|
222
|
+
.build();
|
|
223
|
+
|
|
224
|
+
const result = await solve(problem);
|
|
225
|
+
if (result.status === 'sat') {
|
|
226
|
+
console.log(result.model.assignments);
|
|
227
|
+
}
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
### Solver Selection
|
|
231
|
+
|
|
232
|
+
```typescript
|
|
233
|
+
import { isZ3Available, createZ3Solver, createSimpleSATSolver } from '@cogitator-ai/neuro-symbolic';
|
|
234
|
+
|
|
235
|
+
// Check Z3 availability
|
|
236
|
+
if (await isZ3Available()) {
|
|
237
|
+
const solver = await createZ3Solver();
|
|
238
|
+
const result = await solver.solve(problem);
|
|
239
|
+
} else {
|
|
240
|
+
// Fallback to pure-TS solver
|
|
241
|
+
const solver = createSimpleSATSolver();
|
|
242
|
+
const result = solver.solve(problem);
|
|
243
|
+
}
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
### Expression Types
|
|
247
|
+
|
|
248
|
+
```typescript
|
|
249
|
+
// Arithmetic
|
|
250
|
+
variable('x').add(variable('y'));
|
|
251
|
+
variable('x').sub(constant(5));
|
|
252
|
+
variable('x').mul(constant(2));
|
|
253
|
+
variable('x').div(constant(3));
|
|
254
|
+
|
|
255
|
+
// Boolean
|
|
256
|
+
variable('a').and(variable('b'));
|
|
257
|
+
variable('a').or(variable('b'));
|
|
258
|
+
not(variable('a'));
|
|
259
|
+
variable('a').implies(variable('b'));
|
|
260
|
+
variable('a').iff(variable('b'));
|
|
261
|
+
|
|
262
|
+
// Comparisons
|
|
263
|
+
variable('x').eq(constant(10));
|
|
264
|
+
variable('x').neq(variable('y'));
|
|
265
|
+
variable('x').gt(constant(0));
|
|
266
|
+
variable('x').gte(constant(0));
|
|
267
|
+
variable('x').lt(constant(100));
|
|
268
|
+
variable('x').lte(constant(100));
|
|
269
|
+
|
|
270
|
+
// Global constraints
|
|
271
|
+
allDifferent(variable('x'), variable('y'), variable('z'));
|
|
272
|
+
atMost(2, variable('a'), variable('b'), variable('c'));
|
|
273
|
+
atLeast(1, variable('a'), variable('b'), variable('c'));
|
|
274
|
+
exactly(1, variable('a'), variable('b'), variable('c'));
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
---
|
|
278
|
+
|
|
279
|
+
## Plan Verification
|
|
280
|
+
|
|
281
|
+
PDDL-like planning with verification and repair.
|
|
282
|
+
|
|
283
|
+
### Action Schemas
|
|
284
|
+
|
|
285
|
+
```typescript
|
|
286
|
+
import { ActionSchemaBuilder, ActionRegistry } from '@cogitator-ai/neuro-symbolic';
|
|
287
|
+
|
|
288
|
+
const moveAction = new ActionSchemaBuilder('move')
|
|
289
|
+
.description('Move robot from one location to another')
|
|
290
|
+
.parameter('from', 'string', true)
|
|
291
|
+
.parameter('to', 'string', true)
|
|
292
|
+
.precondition({ type: 'simple', variable: 'robotAt', value: '${from}' })
|
|
293
|
+
.precondition({ type: 'comparison', variable: 'battery', operator: 'gt', value: 10 })
|
|
294
|
+
.effect({ type: 'assign', variable: 'robotAt', value: '${to}' })
|
|
295
|
+
.effect({ type: 'decrement', variable: 'battery', amount: 5 })
|
|
296
|
+
.cost(5)
|
|
297
|
+
.build();
|
|
298
|
+
|
|
299
|
+
const registry = new ActionRegistry();
|
|
300
|
+
registry.register(moveAction);
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
### Plan Validation
|
|
304
|
+
|
|
305
|
+
```typescript
|
|
306
|
+
import { validatePlan, formatValidationResult } from '@cogitator-ai/neuro-symbolic';
|
|
307
|
+
|
|
308
|
+
const plan = {
|
|
309
|
+
id: 'plan-1',
|
|
310
|
+
actions: [
|
|
311
|
+
{ id: 'a1', schemaName: 'move', parameters: { from: 'A', to: 'B' } },
|
|
312
|
+
{ id: 'a2', schemaName: 'move', parameters: { from: 'B', to: 'C' } },
|
|
313
|
+
],
|
|
314
|
+
initialState: { id: 's0', variables: { robotAt: 'A', battery: 100 } },
|
|
315
|
+
goalConditions: [{ type: 'simple', variable: 'robotAt', value: 'C' }],
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
const result = validatePlan(registry, plan);
|
|
319
|
+
console.log(formatValidationResult(result));
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
### Invariant Checking
|
|
323
|
+
|
|
324
|
+
```typescript
|
|
325
|
+
import { createInvariantChecker, formatInvariantResults } from '@cogitator-ai/neuro-symbolic';
|
|
326
|
+
|
|
327
|
+
const checker = createInvariantChecker(registry);
|
|
328
|
+
|
|
329
|
+
// Add safety properties
|
|
330
|
+
checker.addInvariant('battery-non-negative', {
|
|
331
|
+
type: 'comparison',
|
|
332
|
+
variable: 'battery',
|
|
333
|
+
operator: 'gte',
|
|
334
|
+
value: 0,
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
checker.addNever('collision', {
|
|
338
|
+
type: 'and',
|
|
339
|
+
conditions: [
|
|
340
|
+
{ type: 'simple', variable: 'robotAt', value: 'danger-zone' },
|
|
341
|
+
{ type: 'simple', variable: 'alarmActive', value: false },
|
|
342
|
+
],
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
checker.addEventually('goal-reached', {
|
|
346
|
+
type: 'simple',
|
|
347
|
+
variable: 'goalAchieved',
|
|
348
|
+
value: true,
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
const results = checker.checkPlan(plan);
|
|
352
|
+
console.log(formatInvariantResults(results));
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
### Plan Repair
|
|
356
|
+
|
|
357
|
+
```typescript
|
|
358
|
+
import { createPlanRepairer, formatRepairResult } from '@cogitator-ai/neuro-symbolic';
|
|
359
|
+
|
|
360
|
+
const repairer = createPlanRepairer(registry, {
|
|
361
|
+
maxInsertions: 3,
|
|
362
|
+
maxRemovals: 2,
|
|
363
|
+
maxIterations: 10,
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
const repairResult = repairer.repair(plan, validationResult);
|
|
367
|
+
if (repairResult.success) {
|
|
368
|
+
console.log('Repaired plan:', repairResult.repairedPlan);
|
|
369
|
+
} else {
|
|
370
|
+
console.log('Suggestions:', repairResult.suggestions);
|
|
371
|
+
}
|
|
372
|
+
```
|
|
373
|
+
|
|
374
|
+
---
|
|
375
|
+
|
|
376
|
+
## Main Orchestrator
|
|
377
|
+
|
|
378
|
+
The `NeuroSymbolic` class integrates all modules.
|
|
379
|
+
|
|
380
|
+
```typescript
|
|
381
|
+
import { createNeuroSymbolic } from '@cogitator-ai/neuro-symbolic';
|
|
382
|
+
|
|
383
|
+
const ns = createNeuroSymbolic({
|
|
384
|
+
graphAdapter: myGraphAdapter, // Optional: for knowledge graph queries
|
|
385
|
+
config: {
|
|
386
|
+
knowledgeGraph: {
|
|
387
|
+
enableNaturalLanguage: true,
|
|
388
|
+
defaultQueryLimit: 100,
|
|
389
|
+
},
|
|
390
|
+
logic: {
|
|
391
|
+
maxDepth: 50,
|
|
392
|
+
maxSolutions: 10,
|
|
393
|
+
timeout: 5000,
|
|
394
|
+
},
|
|
395
|
+
constraints: {
|
|
396
|
+
timeout: 10000,
|
|
397
|
+
solver: 'z3', // or 'simple-sat'
|
|
398
|
+
},
|
|
399
|
+
planning: {
|
|
400
|
+
maxPlanLength: 100,
|
|
401
|
+
enableRepair: true,
|
|
402
|
+
verifyInvariants: true,
|
|
403
|
+
},
|
|
404
|
+
},
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
// Logic
|
|
408
|
+
ns.loadLogicProgram('...');
|
|
409
|
+
const logicResult = ns.queryLogic('...');
|
|
410
|
+
|
|
411
|
+
// Constraints
|
|
412
|
+
const solverResult = await ns.solve(problem);
|
|
413
|
+
|
|
414
|
+
// Knowledge Graph
|
|
415
|
+
const graphResult = await ns.queryGraph(query);
|
|
416
|
+
const nlResult = await ns.askGraph('natural language question');
|
|
417
|
+
|
|
418
|
+
// Planning
|
|
419
|
+
ns.registerAction(actionSchema);
|
|
420
|
+
const validationResult = await ns.validatePlan(plan);
|
|
421
|
+
const repairResult = await ns.repairPlan(plan, validationResult);
|
|
422
|
+
```
|
|
423
|
+
|
|
424
|
+
---
|
|
425
|
+
|
|
426
|
+
## Module Imports
|
|
427
|
+
|
|
428
|
+
Each module can be imported separately:
|
|
429
|
+
|
|
430
|
+
```typescript
|
|
431
|
+
// Logic Programming
|
|
432
|
+
import {
|
|
433
|
+
KnowledgeBase,
|
|
434
|
+
SLDResolver,
|
|
435
|
+
parseQuery,
|
|
436
|
+
formatSolutions,
|
|
437
|
+
} from '@cogitator-ai/neuro-symbolic/logic';
|
|
438
|
+
|
|
439
|
+
// Knowledge Graph
|
|
440
|
+
import {
|
|
441
|
+
GraphQueryBuilder,
|
|
442
|
+
executeQuery,
|
|
443
|
+
ReasoningEngine,
|
|
444
|
+
} from '@cogitator-ai/neuro-symbolic/knowledge-graph';
|
|
445
|
+
|
|
446
|
+
// Constraints
|
|
447
|
+
import { ConstraintBuilder, solve, Z3WASMSolver } from '@cogitator-ai/neuro-symbolic/constraints';
|
|
448
|
+
|
|
449
|
+
// Planning
|
|
450
|
+
import {
|
|
451
|
+
ActionSchemaBuilder,
|
|
452
|
+
PlanValidator,
|
|
453
|
+
InvariantChecker,
|
|
454
|
+
} from '@cogitator-ai/neuro-symbolic/planning';
|
|
455
|
+
```
|
|
456
|
+
|
|
457
|
+
---
|
|
458
|
+
|
|
459
|
+
## Type Reference
|
|
460
|
+
|
|
461
|
+
```typescript
|
|
462
|
+
import type {
|
|
463
|
+
// Logic
|
|
464
|
+
Term,
|
|
465
|
+
Clause,
|
|
466
|
+
Substitution,
|
|
467
|
+
ProofTree,
|
|
468
|
+
LogicQueryResult,
|
|
469
|
+
|
|
470
|
+
// Knowledge Graph
|
|
471
|
+
GraphQuery,
|
|
472
|
+
GraphQueryResult,
|
|
473
|
+
NaturalLanguageQueryResult,
|
|
474
|
+
|
|
475
|
+
// Constraints
|
|
476
|
+
ConstraintProblem,
|
|
477
|
+
ConstraintVariable,
|
|
478
|
+
SolverResult,
|
|
479
|
+
|
|
480
|
+
// Planning
|
|
481
|
+
ActionSchema,
|
|
482
|
+
Plan,
|
|
483
|
+
PlanState,
|
|
484
|
+
PlanValidationResult,
|
|
485
|
+
SafetyProperty,
|
|
486
|
+
InvariantCheckResult,
|
|
487
|
+
} from '@cogitator-ai/types';
|
|
488
|
+
```
|
|
489
|
+
|
|
490
|
+
---
|
|
491
|
+
|
|
492
|
+
## License
|
|
493
|
+
|
|
494
|
+
MIT
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { Constraint, ConstraintExpression, ConstraintProblem } from '@cogitator-ai/types';
|
|
2
|
+
export declare class Expr {
|
|
3
|
+
readonly expression: ConstraintExpression;
|
|
4
|
+
constructor(expression: ConstraintExpression);
|
|
5
|
+
static variable(name: string): Expr;
|
|
6
|
+
static constant(value: boolean | number): Expr;
|
|
7
|
+
private binaryOp;
|
|
8
|
+
private unaryOp;
|
|
9
|
+
add(other: Expr | number): Expr;
|
|
10
|
+
sub(other: Expr | number): Expr;
|
|
11
|
+
mul(other: Expr | number): Expr;
|
|
12
|
+
div(other: Expr | number): Expr;
|
|
13
|
+
mod(other: Expr | number): Expr;
|
|
14
|
+
pow(other: Expr | number): Expr;
|
|
15
|
+
abs(): Expr;
|
|
16
|
+
eq(other: Expr | number | boolean): Expr;
|
|
17
|
+
neq(other: Expr | number | boolean): Expr;
|
|
18
|
+
gt(other: Expr | number): Expr;
|
|
19
|
+
gte(other: Expr | number): Expr;
|
|
20
|
+
lt(other: Expr | number): Expr;
|
|
21
|
+
lte(other: Expr | number): Expr;
|
|
22
|
+
and(other: Expr | boolean): Expr;
|
|
23
|
+
or(other: Expr | boolean): Expr;
|
|
24
|
+
not(): Expr;
|
|
25
|
+
implies(other: Expr | boolean): Expr;
|
|
26
|
+
iff(other: Expr | boolean): Expr;
|
|
27
|
+
min(other: Expr | number): Expr;
|
|
28
|
+
max(other: Expr | number): Expr;
|
|
29
|
+
}
|
|
30
|
+
export declare function variable(name: string): Expr;
|
|
31
|
+
export declare function constant(value: boolean | number): Expr;
|
|
32
|
+
export declare function and(...exprs: (Expr | boolean)[]): Expr;
|
|
33
|
+
export declare function or(...exprs: (Expr | boolean)[]): Expr;
|
|
34
|
+
export declare function not(expr: Expr | boolean): Expr;
|
|
35
|
+
export declare function implies(a: Expr | boolean, b: Expr | boolean): Expr;
|
|
36
|
+
export declare function iff(a: Expr | boolean, b: Expr | boolean): Expr;
|
|
37
|
+
export declare function ite(condition: Expr, thenExpr: Expr | number, elseExpr: Expr | number): Expr;
|
|
38
|
+
export declare function sum(...exprs: (Expr | number)[]): Expr;
|
|
39
|
+
export declare function product(...exprs: (Expr | number)[]): Expr;
|
|
40
|
+
export declare function allDifferent(...exprs: Expr[]): Expr;
|
|
41
|
+
export declare function atMost(k: number, ...exprs: Expr[]): Expr;
|
|
42
|
+
export declare function atLeast(k: number, ...exprs: Expr[]): Expr;
|
|
43
|
+
export declare function exactly(k: number, ...exprs: Expr[]): Expr;
|
|
44
|
+
export declare class ConstraintBuilder {
|
|
45
|
+
private variables;
|
|
46
|
+
private constraints;
|
|
47
|
+
private objective?;
|
|
48
|
+
private problemName?;
|
|
49
|
+
constructor(name?: string);
|
|
50
|
+
static create(name?: string): ConstraintBuilder;
|
|
51
|
+
bool(name: string): Expr;
|
|
52
|
+
int(name: string, min?: number, max?: number): Expr;
|
|
53
|
+
real(name: string, min?: number, max?: number): Expr;
|
|
54
|
+
bitvec(name: string, bitWidth: number): Expr;
|
|
55
|
+
boolArray(prefix: string, count: number): Expr[];
|
|
56
|
+
intArray(prefix: string, count: number, min?: number, max?: number): Expr[];
|
|
57
|
+
assert(expr: Expr, name?: string): this;
|
|
58
|
+
soft(expr: Expr, weight?: number, name?: string): this;
|
|
59
|
+
minimize(expr: Expr): this;
|
|
60
|
+
maximize(expr: Expr): this;
|
|
61
|
+
build(): ConstraintProblem;
|
|
62
|
+
clear(): this;
|
|
63
|
+
clone(): ConstraintBuilder;
|
|
64
|
+
}
|
|
65
|
+
export declare function expressionToString(expr: ConstraintExpression): string;
|
|
66
|
+
export declare function constraintToString(constraint: Constraint): string;
|
|
67
|
+
export declare function problemToString(problem: ConstraintProblem): string;
|
|
68
|
+
//# sourceMappingURL=dsl.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dsl.d.ts","sourceRoot":"","sources":["../../src/constraints/dsl.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,UAAU,EACV,oBAAoB,EAEpB,iBAAiB,EAElB,MAAM,qBAAqB,CAAC;AAG7B,qBAAa,IAAI;aACa,UAAU,EAAE,oBAAoB;gBAAhC,UAAU,EAAE,oBAAoB;IAE5D,MAAM,CAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAInC,MAAM,CAAC,Q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