class DatalogEngine { constructor() { this.facts = {}; this.rules = {};…
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
class DatalogEngine { constructor() { this.facts = {}; this.rules = {}; this.resultsCache = {}; }
addFact(predicate, ...args) { if (!this.facts[predicate]) { this.facts[predicate] = []; } this.facts[predicate].push(args); }
addRule(headPredicate, headArgs, body) { if (!this.rules[headPredicate]) { this.rules[headPredicate] = []; } this.rules[headPredicate].push({ headArgs, body }); }
query(queryPredicate, ...queryArgs) { const cacheKey = JSON.stringify([queryPredicate, queryArgs]); if (this.resultsCache[cacheKey]) { return this.resultsCache[cacheKey]; }
let results = [];
// Check if there are direct facts matching the query
const facts = this.facts[queryPredicate] || [];
for (let factArgs of facts) {
const substitution = this.unify(queryArgs, factArgs);
if (substitution) {
results.push(this.applySubstitution(substitution, queryArgs));
}
}
// Apply rules iteratively until no more changes
let changed = true;
while (changed) {
changed = false;
// Use a stack to process rules
const ruleStack = [...(this.rules[queryPredicate] || [])];
while (ruleStack.length) {
const { headArgs, body } = ruleStack.pop();
for (let substitution of this.evaluateRule(body, {})) {
// Apply the substitution to the head arguments of the rule
const newResult = this.applySubstitution(substitution, headArgs);
// If the result matches the query args (after applying the substitution), add it to the results
if (this.unify(queryArgs, newResult)) {
if (
!results.find(
(result) =>
JSON.stringify(result) === JSON.stringify(newResult),
)
) {
results.push(newResult);
changed = true;
}
}
}
}
}
// Deduplicate results
results = Array.from(new Set(results.map(JSON.stringify))).map(JSON.parse);
this.resultsCache[cacheKey] = results;
return results;
}
unify(args1, args2, substitution = {}) { // Check if the arguments match for (let i = 0; i < args1.length; i++) { const arg1 = args1[i]; const arg2 = args2[i]; if (typeof arg1 === "string" && arg1.startsWith("?")) { // arg1 is a variable if (arg1 in substitution) { // Variable is already assigned, check if it matches with arg2 if (substitution[arg1] !== arg2) { return null; } } else { // Assign the variable substitution[arg1] = arg2; } } else if (typeof arg2 === "string" && arg2.startsWith("?")) { // arg2 is a variable if (arg2 in substitution) { // Variable is already assigned, check if it matches with arg1 if (substitution[arg2] !== arg1) { return null; } } else { // Assign the variable substitution[arg2] = arg1; } } else if (arg1 !== arg2) { // Both arguments are constants and they don't match return null; } } return substitution; }
applySubstitution(substitution, args) { return args.map((arg) => substitution[arg] || arg); }
evaluateRule(body, initialSubstitution) { if (body.length === 0) { return [initialSubstitution]; }
const [firstClause, ...restClauses] = body;
const [predicate, ...clauseArgs] = firstClause;
let newSubstitutions = [];
if (predicate in this.rules) {
for (let rule of this.rules[predicate]) {
const { headArgs, body } = rule;
for (let substitution of this.evaluateRule(body, initialSubstitution)) {
// const newResult = this.applySubstitution(substitution, headArgs);
for (let sub of this.evaluateRule(restClauses, substitution)) {
newSubstitutions.push(sub);
}
}
}
} else {
const substitutedArgs = this.applySubstitution(
initialSubstitution,
clauseArgs,
);
const facts = this.facts[predicate] || [];
for (let factArgs of facts) {
const substitution = this.unify(substitutedArgs, factArgs, {
...initialSubstitution,
});
if (substitution) {
for (let sub of this.evaluateRule(restClauses, substitution)) {
newSubstitutions.push(sub);
}
}
}
}
return newSubstitutions;
} }
const datalog = new DatalogEngine();
datalog.addFact("group", "group1", "org1"); datalog.addFact("group", "group2", "org1");
// Direct membership to organization datalog.addFact("direct_member", "user1", "org1"); datalog.addFact("direct_member", "user2", "org1");
// managers / administrator datalog.addFact("manager", "user3", "group1"); datalog.addFact("administrator", "user2", "group1");
// Rules datalog.addRule( "in_group", ["?user", "?group"], [ ["direct_member", "?user", "?org"], ["group", "?group", "?org"], ], );
datalog.addRule( "admin", ["?user", "?group"], [["administrator", "?user", "?group"]], );
datalog.addRule("admin", ["?user", "?group"], [["manager", "?user", "?group"]]);
datalog.addRule( "admin", ["?user", "?group"], [["in_group", "?user", "?group"]], );
// Query const group = datalog.query("in_group", "?", "group1"); const admins = datalog.query("admin", "?", "group1");
how would we implement negation
Negation in Datalog is implemented using "Not" operator. The "Not" operator is used to represent the negative facts in Datalog. When a fact is negated, it means that the fact is not true.
To implement negation in your DatalogEngine class, you would need to extend your query, unify and evaluateRule methods to handle the "Not" operator.
Here's a simple way to do it:
Add a method to process negation:
processNegation(predicate, args, substitution = {}) {
const substitutedArgs = this.applySubstitution(substitution, args);
const facts = this.facts[predicate] || [];
for (let factArgs of facts) {
const substitutionResult = this.unify(substitutedArgs, factArgs, {...substitution});
if (substitutionResult) {
// If we find a match, that means the negation failed, so we return null
return null;
}
}
// If we didn't find any matching facts, that means the negation is true
return substitution;
}
Modify your evaluateRule to handle negation:
evaluateRule(body, initialSubstitution) {
if (body.length === 0) {
return [initialSubstitution];
}
const [firstClause, ...restClauses] = body;
const [predicate, ...clauseArgs] = firstClause;
if (predicate.startsWith('Not')) {
const newSubstitution = this.processNegation(predicate.slice(3), clauseArgs, initialSubstitution);
if (newSubstitution === null) {
return [];
} else {
return this.evaluateRule(restClauses, newSubstitution);
}
}
// Rest of the function remains the same...
}
Now you can use negation in your rules:
datalog.addRule(
"not_admin",
["?user", "?group"],
[["Not", "admin", "?user", "?group"]],
);
This rule states that a user is not an admin of a group if there is no fact stating that the user is an admin of the group.