occam-verify-cli 0.0.262
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/README.md +7 -0
- package/bin/abbreviations.js +11 -0
- package/bin/antecedent.js +19 -0
- package/bin/axiom.js +52 -0
- package/bin/combinator.js +27 -0
- package/bin/conclusion.js +19 -0
- package/bin/consequent.js +19 -0
- package/bin/constants.js +9 -0
- package/bin/constructor.js +43 -0
- package/bin/context/file.js +257 -0
- package/bin/context/metaproof.js +90 -0
- package/bin/context/package/fileSystem.js +111 -0
- package/bin/context/package.js +221 -0
- package/bin/context/proof.js +93 -0
- package/bin/fileNames.js +17 -0
- package/bin/loadPackageContexts.js +53 -0
- package/bin/main.js +25 -0
- package/bin/metaAntecedent.js +19 -0
- package/bin/metaConsequent.js +19 -0
- package/bin/metaSubstitution.js +24 -0
- package/bin/options.js +9 -0
- package/bin/premise.js +19 -0
- package/bin/rule.js +64 -0
- package/bin/ruleNames.js +19 -0
- package/bin/type.js +120 -0
- package/bin/utilities/array.js +97 -0
- package/bin/utilities/fileSystem.js +97 -0
- package/bin/utilities/query.js +92 -0
- package/bin/utilities/rule.js +279 -0
- package/bin/utilities/string.js +51 -0
- package/bin/variable.js +54 -0
- package/bin/verify/antecedent.js +35 -0
- package/bin/verify/axiom/IndicativeConditional.js +54 -0
- package/bin/verify/axiom/unqualifiedStatement.js +40 -0
- package/bin/verify/axiom.js +37 -0
- package/bin/verify/conclusion.js +32 -0
- package/bin/verify/consequent.js +32 -0
- package/bin/verify/declaration/combinator.js +17 -0
- package/bin/verify/declaration/constructor.js +19 -0
- package/bin/verify/declaration/topLevel.js +61 -0
- package/bin/verify/declaration/type.js +21 -0
- package/bin/verify/declaration/variable.js +19 -0
- package/bin/verify/equality.js +52 -0
- package/bin/verify/file.js +43 -0
- package/bin/verify/files.js +46 -0
- package/bin/verify/label.js +27 -0
- package/bin/verify/labels.js +17 -0
- package/bin/verify/metaAntecedent.js +35 -0
- package/bin/verify/metaConsequent.js +32 -0
- package/bin/verify/metaDerivation.js +105 -0
- package/bin/verify/metaproof.js +46 -0
- package/bin/verify/metastatement/qualified.js +43 -0
- package/bin/verify/metastatement/unqualified.js +40 -0
- package/bin/verify/package.js +81 -0
- package/bin/verify/premise.js +35 -0
- package/bin/verify/rule/inferenceConditional.js +54 -0
- package/bin/verify/rule/unqualifiedMetastatement.js +40 -0
- package/bin/verify/rule.js +61 -0
- package/bin/verify/statement/qualified.js +17 -0
- package/bin/verify/statement/unqualified.js +27 -0
- package/bin/verify/statement.js +47 -0
- package/bin/verify/statementAsCombinator.js +140 -0
- package/bin/verify/term.js +183 -0
- package/bin/verify/termAsConstructor.js +173 -0
- package/bin/verify/termAsVariable.js +41 -0
- package/bin/verify/type.js +53 -0
- package/bin/verify/typeAssertion.js +69 -0
- package/bin/verify/variable.js +55 -0
- package/index.js +9 -0
- package/license.txt +48 -0
- package/package.json +24 -0
- package/verify.js +13 -0
package/bin/type.js
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
class Type {
|
|
4
|
+
constructor(name, superType) {
|
|
5
|
+
this.name = name;
|
|
6
|
+
this.superType = superType;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
getName() {
|
|
10
|
+
return this.name;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
getSuperType() {
|
|
14
|
+
return this.superType;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
isEqualTo(type) {
|
|
18
|
+
const equalToType = (this === type);
|
|
19
|
+
|
|
20
|
+
return equalToType;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
isSubTypeOf(type) {
|
|
24
|
+
let subTypeOfType = false;
|
|
25
|
+
|
|
26
|
+
if (this.superType === type) {
|
|
27
|
+
subTypeOfType = true;
|
|
28
|
+
} else {
|
|
29
|
+
if (this.superType !== null) {
|
|
30
|
+
subTypeOfType = this.superType.isSubTypeOf(type);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return subTypeOfType;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
isSuperTypeOf(type) {
|
|
38
|
+
let superTypeOfType = false;
|
|
39
|
+
|
|
40
|
+
const superType = type.getSuperType();
|
|
41
|
+
|
|
42
|
+
if (superType === this) {
|
|
43
|
+
superTypeOfType = true;
|
|
44
|
+
} else {
|
|
45
|
+
if (superType !== null) {
|
|
46
|
+
superTypeOfType = superType.isSuperTypeOf(this);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return superTypeOfType;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
isEqualToOrSubTypeOf(type) {
|
|
54
|
+
const equalToType = this.isEqualTo(type),
|
|
55
|
+
subTypeOfType = this.isSubTypeOf(type),
|
|
56
|
+
equalToTypeOrSubTypeOf = (equalToType || subTypeOfType);
|
|
57
|
+
|
|
58
|
+
return equalToTypeOrSubTypeOf;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
isEqualToOrSuperTypeOf(type) {
|
|
62
|
+
const equalToType = this.isEqualTo(type),
|
|
63
|
+
superTypeOfType = this.isSuperTypeOf(type),
|
|
64
|
+
equalToTypeOrSuperTypeOf = (equalToType || superTypeOfType);
|
|
65
|
+
|
|
66
|
+
return equalToTypeOrSuperTypeOf;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
isEqualToSubTypeOfOrSuperTypeOf(type) {
|
|
70
|
+
const equalToType = this.isEqualTo(type),
|
|
71
|
+
subTypeOfType = this.isSubTypeOf(type),
|
|
72
|
+
superTypeOfType = this.isSuperTypeOf(type),
|
|
73
|
+
equalToTypeSubTypeOfOrSuperTypeOf = (equalToType || subTypeOfType || superTypeOfType);
|
|
74
|
+
|
|
75
|
+
return equalToTypeSubTypeOfOrSuperTypeOf;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
matchName(name) {
|
|
79
|
+
const matchesName = (this.name === name);
|
|
80
|
+
|
|
81
|
+
return matchesName;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
matchTypeName(typeName) {
|
|
85
|
+
const matchesTypeName = (this.name === typeName);
|
|
86
|
+
|
|
87
|
+
return matchesTypeName;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
asString(noSuperType = (this.superType === null)) {
|
|
91
|
+
let string;
|
|
92
|
+
|
|
93
|
+
if (noSuperType) {
|
|
94
|
+
string = `${this.name}`;
|
|
95
|
+
} else {
|
|
96
|
+
const superTypeName = this.superType.getName();
|
|
97
|
+
|
|
98
|
+
string = `${this.name}:${superTypeName}`;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return string;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
static fromTypeName(typeName) {
|
|
105
|
+
const name = typeName, ///
|
|
106
|
+
superType = null,
|
|
107
|
+
type = new Type(name, superType);
|
|
108
|
+
|
|
109
|
+
return type;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
static fromTypeNameAndSuperType(typeName, superType) {
|
|
113
|
+
const name = typeName, ///
|
|
114
|
+
type = new Type(name, superType);
|
|
115
|
+
|
|
116
|
+
return type;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
module.exports = Type;
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { arrayUtilities } = require("necessary");
|
|
4
|
+
|
|
5
|
+
const { push, prune, first, second, filter } = arrayUtilities;
|
|
6
|
+
|
|
7
|
+
function leftDifference(arrayA, arrayB) {
|
|
8
|
+
filter(arrayA, (elementA) => {
|
|
9
|
+
const arrayBIncludesElementA = arrayB.includes(elementA);
|
|
10
|
+
|
|
11
|
+
if (!arrayBIncludesElementA) {
|
|
12
|
+
return true;
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function someCombination(array, callback) {
|
|
18
|
+
const combinations = combinationsFromArray(array),
|
|
19
|
+
found = combinations.some((combination) => {
|
|
20
|
+
const array = combination, ///
|
|
21
|
+
passed = callback(array);
|
|
22
|
+
|
|
23
|
+
if (passed) {
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
return found;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
module.exports = {
|
|
32
|
+
push,
|
|
33
|
+
prune,
|
|
34
|
+
first,
|
|
35
|
+
second,
|
|
36
|
+
filter,
|
|
37
|
+
leftDifference,
|
|
38
|
+
someCombination
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
function combinationsFromArray(array) {
|
|
42
|
+
const arrayLength = array.length,
|
|
43
|
+
indexCombinations = indexCombinationsFromArrayLength(arrayLength),
|
|
44
|
+
combinations = indexCombinations.map((indexCombination) => {
|
|
45
|
+
const elements = [];
|
|
46
|
+
|
|
47
|
+
for (let position = 0; position < arrayLength; position++) {
|
|
48
|
+
const index = indexCombination[position],
|
|
49
|
+
element = array[index];
|
|
50
|
+
|
|
51
|
+
elements.push(element);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const combination = elements; ///
|
|
55
|
+
|
|
56
|
+
return combination;
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
return combinations;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function indexCombinationsFromArrayLength(arrayLength) {
|
|
63
|
+
let indexCombinations;
|
|
64
|
+
|
|
65
|
+
if (arrayLength === 0) {
|
|
66
|
+
indexCombinations = [];
|
|
67
|
+
} else if (arrayLength === 1) {
|
|
68
|
+
const indexCombination = [
|
|
69
|
+
0
|
|
70
|
+
];
|
|
71
|
+
|
|
72
|
+
indexCombinations = [
|
|
73
|
+
indexCombination
|
|
74
|
+
];
|
|
75
|
+
} else {
|
|
76
|
+
const innerArrayLength = arrayLength - 1,
|
|
77
|
+
innerIndexCombinations = indexCombinationsFromArrayLength(innerArrayLength);
|
|
78
|
+
|
|
79
|
+
indexCombinations = [];
|
|
80
|
+
|
|
81
|
+
innerIndexCombinations.forEach((innerIndexCombination) => {
|
|
82
|
+
const outerIndex = innerArrayLength; ///
|
|
83
|
+
|
|
84
|
+
for (let position = innerArrayLength; position >= 0; position--) {
|
|
85
|
+
const indexCombination = innerIndexCombination.slice(),
|
|
86
|
+
start = position, ///
|
|
87
|
+
deleteCount = 0;
|
|
88
|
+
|
|
89
|
+
indexCombination.splice(start, deleteCount, outerIndex);
|
|
90
|
+
|
|
91
|
+
indexCombinations.push(indexCombination);
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return indexCombinations;
|
|
97
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const fs = require("fs");
|
|
4
|
+
|
|
5
|
+
const { Query } = require("occam-dom"),
|
|
6
|
+
{ filePathUtilities } = require("occam-open-cli"),
|
|
7
|
+
{ fileSystemUtilities } = require("necessary"),
|
|
8
|
+
{ MetaJSONLexer, MetaJSONParser } = require("occam-grammars");
|
|
9
|
+
|
|
10
|
+
const { isFilePathFlorenceFilePath } = filePathUtilities,
|
|
11
|
+
{ readFile, isEntryFile, isEntryDirectory, checkFileExists } = fileSystemUtilities;
|
|
12
|
+
|
|
13
|
+
const metaJSONLexer = MetaJSONLexer.fromNothing(),
|
|
14
|
+
metaJSONParser = MetaJSONParser.fromNothing(),
|
|
15
|
+
dependencyStringLiteralNodesQuery = Query.fromExpression("//dependency//@string-literal");
|
|
16
|
+
|
|
17
|
+
function filePathsFromPackageName(packageName) {
|
|
18
|
+
const directoryName = packageName, ///
|
|
19
|
+
filePaths = readFilePaths(directoryName, (filePath) => {
|
|
20
|
+
const filePathFlorenceFilePath = isFilePathFlorenceFilePath(filePath);
|
|
21
|
+
|
|
22
|
+
if (filePathFlorenceFilePath) {
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
return filePaths;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function dependencyPackageNamesFromPackageName(packageName) {
|
|
31
|
+
let dependencyPackageNames = [];
|
|
32
|
+
|
|
33
|
+
const directoryName = packageName, ///
|
|
34
|
+
metaJSONFilePath = `${directoryName}/meta.json`,
|
|
35
|
+
metaJSONFileExists = checkFileExists(metaJSONFilePath);
|
|
36
|
+
|
|
37
|
+
if (metaJSONFileExists) {
|
|
38
|
+
const metaJSONFileContent = readFile(metaJSONFilePath),
|
|
39
|
+
content = metaJSONFileContent, ///
|
|
40
|
+
tokens = metaJSONLexer.tokenise(content),
|
|
41
|
+
node = metaJSONParser.parse(tokens),
|
|
42
|
+
dependencyStringLiteralNodes = dependencyStringLiteralNodesQuery.execute(node);
|
|
43
|
+
|
|
44
|
+
dependencyPackageNames = dependencyStringLiteralNodes.map((dependencyStringLiteralNode) => {
|
|
45
|
+
const dependencyStringLiteralNodeContent = dependencyStringLiteralNode.getContent(),
|
|
46
|
+
dependencyPackageName = trimDoubleQuotes(dependencyStringLiteralNodeContent); ///
|
|
47
|
+
|
|
48
|
+
return dependencyPackageName;
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return dependencyPackageNames;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
module.exports = {
|
|
56
|
+
filePathsFromPackageName,
|
|
57
|
+
dependencyPackageNamesFromPackageName
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
function trimDoubleQuotes(content) { return content.replace(/(^"|"$)/g, ""); } ///
|
|
61
|
+
|
|
62
|
+
function readFilePaths(directoryPath, test, filePaths = []) {
|
|
63
|
+
const subEntryNames = fs.readdirSync(directoryPath);
|
|
64
|
+
|
|
65
|
+
subEntryNames.forEach((subEntryName) => {
|
|
66
|
+
const subEntryNameHiddenName = isNameHiddenName(subEntryName);
|
|
67
|
+
|
|
68
|
+
if (!subEntryNameHiddenName) {
|
|
69
|
+
const subEntryPath = `${directoryPath}/${subEntryName}`,
|
|
70
|
+
subEntryFile = isEntryFile(subEntryPath),
|
|
71
|
+
subEntryDirectory = isEntryDirectory(subEntryPath);
|
|
72
|
+
|
|
73
|
+
if (subEntryFile) {
|
|
74
|
+
const filePath = subEntryPath, ///
|
|
75
|
+
pass = test(filePath);
|
|
76
|
+
|
|
77
|
+
if (pass) {
|
|
78
|
+
filePaths.push(filePath);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (subEntryDirectory) {
|
|
83
|
+
const subDirectoryPath = subEntryPath; ///
|
|
84
|
+
|
|
85
|
+
readFilePaths(subDirectoryPath, test, filePaths);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
return filePaths;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function isNameHiddenName(name) {
|
|
94
|
+
const nameHiddenName = /^\..+/.test(name);
|
|
95
|
+
|
|
96
|
+
return nameHiddenName;
|
|
97
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { Query } = require("occam-dom");
|
|
4
|
+
|
|
5
|
+
const typeTerminalNodeQuery = nodeQuery("/type/@type"),
|
|
6
|
+
labelNameTerminalNodeQuery = nodeQuery("/label/@name"),
|
|
7
|
+
variableNameTerminalNodeQuery = nodeQuery("/variable/@name"),
|
|
8
|
+
referenceNameTerminalNodeQuery = nodeQuery("/reference/@name"),
|
|
9
|
+
metavariableNameTerminalNodeQuery = nodeQuery("/metavariable/@name");
|
|
10
|
+
|
|
11
|
+
function nodeQuery(expression) {
|
|
12
|
+
const query = Query.fromExpression(expression);
|
|
13
|
+
|
|
14
|
+
return function(node) {
|
|
15
|
+
if (node !== null) {
|
|
16
|
+
const nodes = query.execute(node);
|
|
17
|
+
|
|
18
|
+
node = nodes.shift() || null; ///
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return node;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function nodesQuery(expression) {
|
|
26
|
+
const query = Query.fromExpression(expression);
|
|
27
|
+
|
|
28
|
+
return function(node) {
|
|
29
|
+
let nodes = null;
|
|
30
|
+
|
|
31
|
+
if (node !== null) {
|
|
32
|
+
nodes = query.execute(node);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return nodes;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function typeNameFromTypeNode(typeNode) {
|
|
40
|
+
let typeName = null;
|
|
41
|
+
|
|
42
|
+
if (typeNode !== null) {
|
|
43
|
+
const typeTerminalNode = typeTerminalNodeQuery(typeNode),
|
|
44
|
+
typeTerminalNodeContent = typeTerminalNode.getContent();
|
|
45
|
+
|
|
46
|
+
typeName = typeTerminalNodeContent; ///
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return typeName;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function labelNameFromLabelNode(labelNode) {
|
|
53
|
+
const labelNameTerminalNode = labelNameTerminalNodeQuery(labelNode),
|
|
54
|
+
labelNameTerminalNodeContent = labelNameTerminalNode.getContent(),
|
|
55
|
+
labelName = labelNameTerminalNodeContent; ///
|
|
56
|
+
|
|
57
|
+
return labelName;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function variableNameFromVariableNode(variableNode) {
|
|
61
|
+
const variableNameTerminalNode = variableNameTerminalNodeQuery(variableNode),
|
|
62
|
+
variableNameTerminalNodeContent = variableNameTerminalNode.getContent(),
|
|
63
|
+
variableName = variableNameTerminalNodeContent; ///
|
|
64
|
+
|
|
65
|
+
return variableName;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function referenceNameFromReferenceNode(referenceNode) {
|
|
69
|
+
const referenceNameTerminalNode = referenceNameTerminalNodeQuery(referenceNode),
|
|
70
|
+
referenceNameTerminalNodeContent = referenceNameTerminalNode.getContent(),
|
|
71
|
+
referenceName = referenceNameTerminalNodeContent; ///
|
|
72
|
+
|
|
73
|
+
return referenceName;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function metavariableNameFromMetavariableNode(metavariableNode) {
|
|
77
|
+
const metavariableNameTerminalNode = metavariableNameTerminalNodeQuery(metavariableNode),
|
|
78
|
+
metavariableNameTerminalNodeContent = metavariableNameTerminalNode.getContent(),
|
|
79
|
+
metavariableName = metavariableNameTerminalNodeContent; ///
|
|
80
|
+
|
|
81
|
+
return metavariableName;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
module.exports = {
|
|
85
|
+
nodeQuery,
|
|
86
|
+
nodesQuery,
|
|
87
|
+
typeNameFromTypeNode,
|
|
88
|
+
labelNameFromLabelNode,
|
|
89
|
+
variableNameFromVariableNode,
|
|
90
|
+
referenceNameFromReferenceNode,
|
|
91
|
+
metavariableNameFromMetavariableNode
|
|
92
|
+
};
|
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const MetaSubstitution = require("../metaSubstitution");
|
|
4
|
+
|
|
5
|
+
const { nodeAsString } = require("../utilities/string"),
|
|
6
|
+
{ prune, someCombination } = require("../utilities/array"),
|
|
7
|
+
{ METAVARIABLE_RULE_NAME } = require("../ruleNames"),
|
|
8
|
+
{ metavariableNameFromMetavariableNode } = require("../utilities/query");
|
|
9
|
+
|
|
10
|
+
function matchRule(rule, metastatementNode, context) {
|
|
11
|
+
let metastatementNodes = context.getMetastatementNodes();
|
|
12
|
+
|
|
13
|
+
const premiseMetastatementNodes = rule.getPremiseMetastatementNodes(),
|
|
14
|
+
conclusionMetastatementNode = rule.getConclusionMetastatementNode(),
|
|
15
|
+
premiseMetastatementNodesLength = premiseMetastatementNodes.length,
|
|
16
|
+
start = -premiseMetastatementNodesLength;
|
|
17
|
+
|
|
18
|
+
metastatementNodes = metastatementNodes.slice(start); ///
|
|
19
|
+
|
|
20
|
+
const ruleMatches = someCombination(metastatementNodes, (metastatementNodes) => {
|
|
21
|
+
const premisesMatchConclusion = matchPremisesAndConclusion(premiseMetastatementNodes, conclusionMetastatementNode, metastatementNodes, metastatementNode, context);
|
|
22
|
+
|
|
23
|
+
if (premisesMatchConclusion) {
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
return ruleMatches;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
module.exports = {
|
|
32
|
+
matchRule
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
function matchPremisesAndConclusion(premiseMetastatementNodes, conclusionMetastatementNode, metastatementNodes, metastatementNode, context) {
|
|
36
|
+
let premisesMatchConclusion = false;
|
|
37
|
+
|
|
38
|
+
const metaSubstitutions = [],
|
|
39
|
+
premisesMatch = matchPremises(premiseMetastatementNodes, metastatementNodes, metaSubstitutions, context);
|
|
40
|
+
|
|
41
|
+
if (premisesMatch) {
|
|
42
|
+
const conclusionNonTerminalNode = matchConclusion(conclusionMetastatementNode, metaSubstitutions), ///
|
|
43
|
+
conclusionMetastatementString = nodeAsString(conclusionNonTerminalNode),
|
|
44
|
+
metastatementString = nodeAsString(metastatementNode);
|
|
45
|
+
|
|
46
|
+
premisesMatchConclusion = (metastatementString === conclusionMetastatementString);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return premisesMatchConclusion;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function matchPremises(premiseMetastatementNodes, metastatementNodes, metaSubstitutions) {
|
|
53
|
+
const premisesMatches = premiseMetastatementNodes.every((premiseMetastatementNode) => {
|
|
54
|
+
const premiseMatches = matchPremise(premiseMetastatementNode, metastatementNodes, metaSubstitutions);
|
|
55
|
+
|
|
56
|
+
if (premiseMatches) {
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
return premisesMatches;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function matchPremise(premiseMetastatementNode, metastatementNodes, metaSubstitutions) {
|
|
65
|
+
const metastatementNode = prune(metastatementNodes, (metastatementNode) => {
|
|
66
|
+
const nonTerminalNode = metastatementNode, ///
|
|
67
|
+
premiseNonTerminalNode = premiseMetastatementNode, ///
|
|
68
|
+
premiseNonTerminalNodeMatches = matchPremiseNonTerminalNode(premiseNonTerminalNode, nonTerminalNode, metaSubstitutions);
|
|
69
|
+
|
|
70
|
+
if (!premiseNonTerminalNodeMatches) {
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
}) || null;
|
|
74
|
+
|
|
75
|
+
const premiseMatches = (metastatementNode !== null);
|
|
76
|
+
|
|
77
|
+
return premiseMatches;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function matchPremiseNode(premiseNode, node, metaSubstitutions) {
|
|
81
|
+
let premiseNodeMatches = false;
|
|
82
|
+
|
|
83
|
+
const nodeTerminalNode = node.isTerminalNode(),
|
|
84
|
+
ruleNodeTerminalNode = premiseNode.isTerminalNode();
|
|
85
|
+
|
|
86
|
+
if (nodeTerminalNode === ruleNodeTerminalNode) {
|
|
87
|
+
if (nodeTerminalNode) {
|
|
88
|
+
const terminalNode = node, ///
|
|
89
|
+
premiseTerminalNode = premiseNode, ///
|
|
90
|
+
premiseTerminalNodeMatches = matchPremiseTerminalNode(premiseTerminalNode, terminalNode, metaSubstitutions);
|
|
91
|
+
|
|
92
|
+
premiseNodeMatches = premiseTerminalNodeMatches; ///
|
|
93
|
+
} else {
|
|
94
|
+
const nonTerminalNode = node, ///
|
|
95
|
+
premiseNonTerminalNode = premiseNode, ///
|
|
96
|
+
premiseNonTerminalNodeMatches = matchPremiseNonTerminalNode(premiseNonTerminalNode, nonTerminalNode, metaSubstitutions);
|
|
97
|
+
|
|
98
|
+
premiseNodeMatches = premiseNonTerminalNodeMatches; ///
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return premiseNodeMatches;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function matchPremiseChildNodes(premiseChildNodes, childNodes, metaSubstitutions) {
|
|
106
|
+
let premiseChildNodesMatches = false;
|
|
107
|
+
|
|
108
|
+
const childNodesLength = childNodes.length,
|
|
109
|
+
premiseChildNodesLength = premiseChildNodes.length;
|
|
110
|
+
|
|
111
|
+
if (childNodesLength === premiseChildNodesLength) {
|
|
112
|
+
premiseChildNodesMatches = childNodes.every((childNode, index) => {
|
|
113
|
+
const premiseChildNode = premiseChildNodes[index],
|
|
114
|
+
premiseNode = premiseChildNode, ///
|
|
115
|
+
node = childNode, ///
|
|
116
|
+
premiseNodeMatches = matchPremiseNode(premiseNode, node, metaSubstitutions);
|
|
117
|
+
|
|
118
|
+
if (premiseNodeMatches) {
|
|
119
|
+
return true;
|
|
120
|
+
}
|
|
121
|
+
})
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return premiseChildNodesMatches;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function matchPremiseMetavariable(premiseMetavariableNode, nonTerminalNode, metaSubstitutions) {
|
|
128
|
+
let premiseMetavariableMatches = false;
|
|
129
|
+
|
|
130
|
+
const premiseMetavariableName = metavariableNameFromMetavariableNode(premiseMetavariableNode),
|
|
131
|
+
nonTerminalString = nodeAsString(nonTerminalNode),
|
|
132
|
+
metaSubstitution = metaSubstitutions.find((metaSubstitution) => {
|
|
133
|
+
const metavariableName = metaSubstitution.getMetavariableName();
|
|
134
|
+
|
|
135
|
+
if (metavariableName === premiseMetavariableName) {
|
|
136
|
+
return true;
|
|
137
|
+
}
|
|
138
|
+
}) || null;
|
|
139
|
+
|
|
140
|
+
if (metaSubstitution !== null) {
|
|
141
|
+
const metaSubstitutionNonTerminalNode = metaSubstitution.getNonTerminalNode(),
|
|
142
|
+
metaSubstitutionNonTerminalString = nodeAsString(metaSubstitutionNonTerminalNode);
|
|
143
|
+
|
|
144
|
+
if (nonTerminalString === metaSubstitutionNonTerminalString) {
|
|
145
|
+
premiseMetavariableMatches = true;
|
|
146
|
+
}
|
|
147
|
+
} else {
|
|
148
|
+
const metavariableName = premiseMetavariableName, ///
|
|
149
|
+
metaSubstitution = MetaSubstitution.fromMetavariableNameAndNonTerminalNode(metavariableName, nonTerminalNode);
|
|
150
|
+
|
|
151
|
+
metaSubstitutions.push(metaSubstitution);
|
|
152
|
+
|
|
153
|
+
premiseMetavariableMatches = true;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return premiseMetavariableMatches;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function matchPremiseTerminalNode(premiseTerminalNode, terminalNode, metaSubstitutions) {
|
|
160
|
+
let premiseTerminalNodeMatches = false;
|
|
161
|
+
|
|
162
|
+
const matches = premiseTerminalNode.match(terminalNode);
|
|
163
|
+
|
|
164
|
+
if (matches) {
|
|
165
|
+
premiseTerminalNodeMatches = true;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return premiseTerminalNodeMatches;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function matchPremiseNonTerminalNode(premiseNonTerminalNode, nonTerminalNode, metaSubstitutions) {
|
|
172
|
+
let premiseNonTerminalNodeMatches = false;
|
|
173
|
+
|
|
174
|
+
const premiseRuleName = premiseNonTerminalNode.getRuleName(); ///
|
|
175
|
+
|
|
176
|
+
switch (premiseRuleName) {
|
|
177
|
+
case METAVARIABLE_RULE_NAME: {
|
|
178
|
+
const premiseMetavariableNode = premiseNonTerminalNode, ///
|
|
179
|
+
premiseMetavariableMatches = matchPremiseMetavariable(premiseMetavariableNode, nonTerminalNode, metaSubstitutions);
|
|
180
|
+
|
|
181
|
+
premiseNonTerminalNodeMatches = premiseMetavariableMatches;
|
|
182
|
+
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
default: {
|
|
187
|
+
const ruleName = nonTerminalNode.getRuleName();
|
|
188
|
+
|
|
189
|
+
if (ruleName === premiseRuleName) {
|
|
190
|
+
const childNodes = nonTerminalNode.getChildNodes(),
|
|
191
|
+
premiseChildNodes = premiseNonTerminalNode.getChildNodes(),
|
|
192
|
+
premiseChildNodesMatches = matchPremiseChildNodes(premiseChildNodes, childNodes, metaSubstitutions);
|
|
193
|
+
|
|
194
|
+
premiseNonTerminalNodeMatches = premiseChildNodesMatches; ///
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
return premiseNonTerminalNodeMatches;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function matchConclusion(conclusionMetastatementNode, metaSubstitutions) {
|
|
203
|
+
let conclusionNonTerminalNode = conclusionMetastatementNode; ///
|
|
204
|
+
|
|
205
|
+
conclusionNonTerminalNode = conclusionNonTerminalNode.clone();
|
|
206
|
+
|
|
207
|
+
conclusionNonTerminalNode = matchConclusionNonTerminalNode(conclusionNonTerminalNode, metaSubstitutions);
|
|
208
|
+
|
|
209
|
+
return conclusionNonTerminalNode;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function matchConclusionNode(conclusionNode, metaSubstitutions) {
|
|
213
|
+
const conclusionNodeNonTerminalNode = conclusionNode.isNonTerminalNode();
|
|
214
|
+
|
|
215
|
+
if (conclusionNodeNonTerminalNode) {
|
|
216
|
+
let conclusionNonTerminalNode = conclusionNode; ///
|
|
217
|
+
|
|
218
|
+
conclusionNonTerminalNode = matchConclusionNonTerminalNode(conclusionNonTerminalNode, metaSubstitutions);
|
|
219
|
+
|
|
220
|
+
conclusionNode = conclusionNonTerminalNode; ///
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
return conclusionNode;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function matchConclusionChildNodes(conclusionChildNodes, metaSubstitutions) {
|
|
227
|
+
conclusionChildNodes = conclusionChildNodes.map((conclusionChildNode) => {
|
|
228
|
+
let conclusionNode = conclusionChildNode; ///
|
|
229
|
+
|
|
230
|
+
conclusionNode = matchConclusionNode(conclusionNode, metaSubstitutions);
|
|
231
|
+
|
|
232
|
+
conclusionChildNode = conclusionNode; ///
|
|
233
|
+
|
|
234
|
+
return conclusionChildNode;
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
return conclusionChildNodes;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function matchConclusionNonTerminalNode(conclusinNonTerminalNode, metaSubstitutions) {
|
|
241
|
+
const ruleName = conclusinNonTerminalNode.getRuleName();
|
|
242
|
+
|
|
243
|
+
if (ruleName === METAVARIABLE_RULE_NAME) {
|
|
244
|
+
let conclusionMetaVariableNode = conclusinNonTerminalNode; ///
|
|
245
|
+
|
|
246
|
+
conclusinNonTerminalNode = matchConclusionMetavariableNode(conclusionMetaVariableNode, metaSubstitutions);
|
|
247
|
+
} else {
|
|
248
|
+
let childNodes = conclusinNonTerminalNode.getChildNodes();
|
|
249
|
+
|
|
250
|
+
let conclusionChildNodes = childNodes; ///
|
|
251
|
+
|
|
252
|
+
conclusionChildNodes = matchConclusionChildNodes(conclusionChildNodes, metaSubstitutions);
|
|
253
|
+
|
|
254
|
+
childNodes = conclusionChildNodes; ///
|
|
255
|
+
|
|
256
|
+
conclusinNonTerminalNode.setChildNodes(childNodes);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
return conclusinNonTerminalNode;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function matchConclusionMetavariableNode(conclusionMetavariableNode, metaSubstitutions) {
|
|
263
|
+
const conclusionMetavariableName = metavariableNameFromMetavariableNode(conclusionMetavariableNode),
|
|
264
|
+
metaSubstitution = metaSubstitutions.find((metaSubstitution) => {
|
|
265
|
+
const metavariableName = metaSubstitution.getMetavariableName();
|
|
266
|
+
|
|
267
|
+
if (metavariableName === conclusionMetavariableName) {
|
|
268
|
+
return true;
|
|
269
|
+
}
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
let nonTerminalNode = metaSubstitution.getNonTerminalNode(); ///
|
|
273
|
+
|
|
274
|
+
nonTerminalNode = nonTerminalNode.clone(); ///
|
|
275
|
+
|
|
276
|
+
const conclusionNonTerminalNode = nonTerminalNode; ///
|
|
277
|
+
|
|
278
|
+
return conclusionNonTerminalNode;
|
|
279
|
+
}
|