Skip to content
Snippets Groups Projects
Commit 06e7ac96 authored by Jenny Zhang's avatar Jenny Zhang
Browse files

Added containExpNode in expAsit.js in order to complement axiom rule in ndRules.js with tests

parent 89296f03
No related branches found
No related tags found
No related merge requests found
...@@ -15,6 +15,28 @@ function isExpNode(obj) { ...@@ -15,6 +15,28 @@ function isExpNode(obj) {
'right' in obj; 'right' in obj;
} }
function containExpNode(asts, ast) {
for (let i = 0; i < asts.length; i++) {
if (toEqual(ast, asts[i])) {
return true;
}
}
return false;
}
function toEqual(node1, node2) {
if (node1.type !== node2.type) {
return false;
}
if (node1.type === 'symbol') {
return node1.value === node2.value;
}
return toEqual(node1.left, node2.left) && toEqual(node1.right, node2.right);
}
const precedence = { const precedence = {
'imply': 1, 'imply': 1,
...@@ -207,5 +229,6 @@ ast.astToString = astToString; ...@@ -207,5 +229,6 @@ ast.astToString = astToString;
ast.astToLaTeX = astToLaTeX; ast.astToLaTeX = astToLaTeX;
ast.astsToLateX = astsToLateX; ast.astsToLateX = astsToLateX;
ast.astsToString = astsToString; ast.astsToString = astsToString;
ast.containExpNode = containExpNode;
module.exports = ast; // Export the AST module module.exports = ast; // Export the AST module
\ No newline at end of file
...@@ -173,6 +173,19 @@ function contradiction(input) { ...@@ -173,6 +173,19 @@ function contradiction(input) {
return [output]; return [output];
} }
// input list of list of ast, return list of sequents
function axiom(input) {
const givens = input[0]; // list type, given list of sequent
const conclusion = input[1][0]; //list type of sequent
if (ast.containExpNode(givens, conclusion)) {
return [];
} else {
throw new Error("Invalid application for axiom rule: The list of formulas in left hand side of turnstile must contain the formula in right hand side of turnstile, to apply the imply introduction rule.");
}
}
module.exports = { module.exports = {
negationIntroduction, negationIntroduction,
negationElimination, negationElimination,
...@@ -184,5 +197,6 @@ module.exports = { ...@@ -184,5 +197,6 @@ module.exports = {
orElimination, orElimination,
implyIntroduction, implyIntroduction,
implyElimination, implyElimination,
contradiction contradiction,
axiom
}; };
...@@ -9,7 +9,8 @@ const { negationIntroduction, ...@@ -9,7 +9,8 @@ const { negationIntroduction,
orElimination, orElimination,
implyIntroduction, implyIntroduction,
implyElimination, implyElimination,
contradiction } = require('../../src/utils/ndRules'); contradiction,
axiom } = require('../../src/utils/ndRules');
const { parse } = require("../../src/utils/parser"); const { parse } = require("../../src/utils/parser");
...@@ -218,3 +219,21 @@ test('Contradiction rule should return the correct premises', () => { ...@@ -218,3 +219,21 @@ test('Contradiction rule should return the correct premises', () => {
expect(output).toEqual([parse("!A |- false")]); expect(output).toEqual([parse("!A |- false")]);
}); });
//axiom
test('Axiom rule should return the correct premises', () => {
const input = parse("B, A |- A");
const output = axiom(input);
expect(output).toEqual([parse("")]);
});
test('Axiom should throw an error for invalid application', () => {
const input = parse("|- !A & B");
expect(() => axiom(input)).toThrow("Invalid application for axiom rule: The list of formulas in left hand side of turnstile must contain the formula in right hand side of turnstile, to apply the imply introduction rule.");
});
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment