-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDlCore.js
More file actions
404 lines (391 loc) · 15.5 KB
/
DlCore.js
File metadata and controls
404 lines (391 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
const DlOperator = {
And: 0, Or: 1, Nand: 2, Nor: 3, Imply: 4, Implied: 5, Nimply: 6, Nimplied: 7,
Equiv: 8, Xor: 9, Com: 10, App: 11, Not: 12, Nece: 13, Poss: 14, Obli: 15,
Perm: 16, Top: 17, Bot: 18
};
class DlCore {
static dlOperators = new Map([ ["\\and", DlOperator.And], ["\\or", DlOperator.Or], ["\\nand", DlOperator.Nand], ["\\nor", DlOperator.Nor], ["\\imply", DlOperator.Imply], ["\\implied", DlOperator.Implied], ["\\nimply", DlOperator.Nimply], ["\\nimplied", DlOperator.Nimplied], ["\\equiv", DlOperator.Equiv], ["\\xor", DlOperator.Xor], ["\\com", DlOperator.Com], ["\\app", DlOperator.App], ["\\not", DlOperator.Not], ["\\nece", DlOperator.Nece], ["\\poss", DlOperator.Poss], ["\\obli", DlOperator.Obli], ["\\perm", DlOperator.Perm], ["\\top", DlOperator.Top], ["\\bot", DlOperator.Bot] ]);
static dlOperatorsUnicode = new Map([ ["\\and", "∧"], ["\\or", "∨"], ["\\nand", "↑"], ["\\nor", "↓"], ["\\imply", "→"], ["\\implied", "←"], ["\\nimply", "↛"], ["\\nimplied", "↚"], ["\\equiv", "↔"], ["\\xor", "↮"], ["\\com", "↷"], ["\\app", "↝"], ["\\not", "¬"], ["\\nece", "□"], ["\\poss", "◇"], ["\\obli", "○"], ["\\perm", "⌔"], ["\\top", "⊤"], ["\\bot", "⊥"] ]);
// NOTE: In Bocheński notation \nimply and \nimplied are L and M, but in Łukasiewicz notation those are already taken by \nece and \poss, respectively.
static operators_luk = new Map([ ['K', DlOperator.And], ['A', DlOperator.Or], ['D', DlOperator.Nand], ['X', DlOperator.Nor], ['C', DlOperator.Imply], ['B', DlOperator.Implied], ['F', DlOperator.Nimply], ['G', DlOperator.Nimplied], ['E', DlOperator.Equiv], ['J', DlOperator.Xor], ['S', DlOperator.Com], ['U', DlOperator.App], ['N', DlOperator.Not], ['L', DlOperator.Nece], ['M', DlOperator.Poss], ['Z', DlOperator.Obli], ['P', DlOperator.Perm], ['V', DlOperator.Top], ['O', DlOperator.Bot] ]);
static operators_boc = new Map([ ['K', DlOperator.And], ['A', DlOperator.Or], ['D', DlOperator.Nand], ['X', DlOperator.Nor], ['C', DlOperator.Imply], ['B', DlOperator.Implied], ['L', DlOperator.Nimply], ['M', DlOperator.Nimplied], ['E', DlOperator.Equiv], ['J', DlOperator.Xor], ['S', DlOperator.Com], ['U', DlOperator.App], ['N', DlOperator.Not], ['H', DlOperator.Nece], ['I', DlOperator.Poss], ['Z', DlOperator.Obli], ['P', DlOperator.Perm], ['V', DlOperator.Top], ['O', DlOperator.Bot] ]);
static operatorNames_luk = new Map([ ["\\and", "K"], ["\\or", "A"], ["\\nand", "D"], ["\\nor", "X"], ["\\imply", "C"], ["\\implied", "B"], ["\\nimply", "F"], ["\\nimplied", "G"], ["\\equiv", "E"], ["\\xor", "J"], ["\\com", "S"], ["\\app", "U"], ["\\not", "N"], ["\\nece", "L"], ["\\poss", "M"], ["\\obli", "Z"], ["\\perm", "P"], ["\\top", "V"], ["\\bot", "O"] ]);
static operatorNames_boc = new Map([ ["\\and", "K"], ["\\or", "A"], ["\\nand", "D"], ["\\nor", "X"], ["\\imply", "C"], ["\\implied", "B"], ["\\nimply", "L"], ["\\nimplied", "M"], ["\\equiv", "E"], ["\\xor", "J"], ["\\com", "S"], ["\\app", "U"], ["\\not", "N"], ["\\nece", "H"], ["\\poss", "I"], ["\\obli", "Z"], ["\\perm", "P"], ["\\top", "V"], ["\\bot", "O"] ]);
static dlOperatorArity(op) {
switch (op) {
case DlOperator.Top:
case DlOperator.Bot:
return 0;
case DlOperator.Not:
case DlOperator.Nece:
case DlOperator.Poss:
case DlOperator.Obli:
case DlOperator.Perm:
return 1;
case DlOperator.And:
case DlOperator.Or:
case DlOperator.Nand:
case DlOperator.Nor:
case DlOperator.Imply:
case DlOperator.Implied:
case DlOperator.Nimply:
case DlOperator.Nimplied:
case DlOperator.Equiv:
case DlOperator.Xor:
case DlOperator.Com:
case DlOperator.App:
return 2;
default:
throw new Error(`DlCore::dlOperatorArity(): Unknown DlOperator ${op}.`);
}
}
static dlOperatorToString(op) {
switch (op) {
case DlOperator.And:
return "\\and";
case DlOperator.Or:
return "\\or";
case DlOperator.Nand:
return "\\nand";
case DlOperator.Nor:
return "\\nor";
case DlOperator.Imply:
return "\\imply";
case DlOperator.Implied:
return "\\implied";
case DlOperator.Nimply:
return "\\nimply";
case DlOperator.Nimplied:
return "\\nimplied";
case DlOperator.Equiv:
return "\\equiv";
case DlOperator.Xor:
return "\\xor";
case DlOperator.Com:
return "\\com";
case DlOperator.App:
return "\\app";
case DlOperator.Not:
return "\\not";
case DlOperator.Nece:
return "\\nece";
case DlOperator.Poss:
return "\\poss";
case DlOperator.Obli:
return "\\obli";
case DlOperator.Perm:
return "\\perm";
case DlOperator.Top:
return "\\top";
case DlOperator.Bot:
return "\\bot";
default:
throw new Error(`DlCore::dlOperatorToString(): Unknown DlOperator ${op}.`);
}
}
static traverseLeftToRightInOrder(formula, fVisit, fDown, fUp) {
const children = formula.getChildren();
switch (children.length) {
case 0:
fVisit(formula);
break;
case 1:
fVisit(formula);
fDown(formula, children[0]);
DlCore.traverseLeftToRightInOrder(children[0], fVisit, fDown, fUp);
fUp(children[0], formula);
break;
case 2:
fDown(formula, children[0]);
DlCore.traverseLeftToRightInOrder(children[0], fVisit, fDown, fUp);
fUp(children[0], formula);
fVisit(formula);
fDown(formula, children[1]);
DlCore.traverseLeftToRightInOrder(children[1], fVisit, fDown, fUp);
fUp(children[1], formula);
break;
default:
throw new Error(`DlCore::traverseLeftToRightInOrder(): There are too many children (${children.length}).`);
}
}
static formulaRepresentation_unicode(formula) {
let ss = '';
DlCore.traverseLeftToRightInOrder(formula, (node) => {
const op = DlCore.dlOperatorsUnicode.get(node.getValue());
ss += op === undefined ? node.getValue() : op;
}, (node, child) => {
if (child.getChildren().length >= 2)
ss += '(';
}, (child, node) => {
if (child.getChildren().length >= 2)
ss += ')';
} );
return ss;
}
static fromPolishNotation(output, input, prioritizeBochenski, debug) {
const operators = prioritizeBochenski ? DlCore.operators_boc : DlCore.operators_luk;
const stack = [];
for (let i = input.length - 1; i >= 0; i--) {
const c = input[i];
if (c === '>') { // unsupported variable ending
const start = input.lastIndexOf('<', i);
if (start === -1) {
if (debug)
console.error(`Parse error: Missing '<'.`);
return false;
} else if (start + 1 === i) {
if (debug)
console.error(`Parse error: Empty variable name in "<>".`);
return false;
} else if (debug && start + 2 === i && 'a' <= input[start + 1] && input[start + 1] <= 'z')
console.error(`Warning: Variable '${input[start + 1]}' from "<${input[start + 1]}>" might be merged. To circumvent this, avoid names 'a', 'b', ..., 'z' for variables that occur as 27th or later.`);
stack.push(new DlFormula(input.substring(start + 1, i))); // substr(start + 1, i - start - 1) = substring(start + 1, start + 1 + i - start - 1)
i = start;
} else {
const op = operators.get(c);
if (op === undefined)
stack.push(new DlFormula(c));
else { // NOTE: It is assumed that all operators are addressed by 'operators', everything else will be treated as a variable.
const arity = DlCore.dlOperatorArity(op);
if (stack.length < arity) {
if (debug)
console.error(`Parse error: Missing variable for '${c}' (alias ${DlCore.dlOperatorToString(op)}) at index ${i}.`);
return false;
}
switch (arity) {
case 0:
stack.push(new DlFormula(DlCore.dlOperatorToString(op)));
break;
case 1:
stack[stack.length - 1] = new DlFormula(DlCore.dlOperatorToString(op), [stack[stack.length - 1]]);
break;
case 2: {
const term = new DlFormula(DlCore.dlOperatorToString(op), [stack[stack.length - 1], stack[stack.length - 2]]);
stack.pop();
stack[stack.length - 1] = term;
break;
}
default:
throw new Error(`DlCore::fromPolishNotation(): Impossible arity (${arity}) for ${DlCore.dlOperatorToString(op)}.`);
}
}
}
}
if (stack.length !== 1) {
if (debug)
console.error(`Parse error: Missing or extra variables resulted in non-singleton stack ${stack}.`);
return false;
}
output.value = stack[0];
return true;
}
static fromPolishNotation_noRename(output, input, prioritizeBochenski, debug) {
const operators = prioritizeBochenski ? DlCore.operators_boc : DlCore.operators_luk;
const stack = [];
let varLast = -1;
for (let i = input.length - 1; i >= 0; i--) {
const c = input[i];
if (c === '.') { // separator of variables
if (varLast === -1) {
if (debug)
console.error(`Parse error: Separator '.' does not precede a variable at index ${i}.`);
return false;
}
stack.push(new DlFormula(input.substring(i + 1, varLast + 1))); // register completed variable ; substr(i + 1, varLast - i) = substring(i + 1, i + 1 + varLast - i)
varLast = -1;
} else {
const op = operators.get(c);
if (op === undefined) {
if (varLast === -1)
varLast = i;
} else { // It is assumed that all operators are addressed by 'operators', everything else will be treated as a variable.
if (varLast !== -1) {
stack.push(new DlFormula(input.substring(i + 1, varLast + 1))); // first register completed variable ; substr(i + 1, varLast - i) = substring(i + 1, i + 1 + varLast - i)
varLast = -1;
}
const arity = DlCore.dlOperatorArity(op);
if (stack.length < arity) {
if (debug)
console.error(`Parse error: Missing variable for '${c}' (alias ${DlCore.dlOperatorToString(op)}) at index ${i}.`);
return false;
}
switch (arity) {
case 0:
stack.push(new DlFormula(DlCore.dlOperatorToString(op)));
break;
case 1:
stack[stack.length - 1] = new DlFormula(DlCore.dlOperatorToString(op), [stack[stack.length - 1]]);
break;
case 2: {
const term = new DlFormula(DlCore.dlOperatorToString(op), [stack[stack.length - 1], stack[stack.length - 2]]);
stack.pop();
stack[stack.length - 1] = term;
break;
}
default:
throw new Error(`DlCore::fromPolishNotation(): Impossible arity (${arity}) for ${DlCore.dlOperatorToString(op)}.`);
}
}
}
}
if (varLast !== -1) // still need to register variable
stack.push(new DlFormula(input));
if (stack.length !== 1) {
if (debug)
console.error(`Parse error: Missing or extra variables resulted in non-singleton stack ${stack}.`);
return false;
}
output.value = stack[0];
return true;
}
static toPolishNotation(formula, prioritizeBochenski = false, customOperatorTranslation = new Map(), customVariableTranslation = new Map(), sequenceOfVarNames = ["p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o"]) {
const operatorNames = prioritizeBochenski ? DlCore.operatorNames_boc : DlCore.operatorNames_luk;
const operatorTranslations = customOperatorTranslation;
const variableTranslations = customVariableTranslation;
let nextVariableIndex = 0;
const recurse = (node) => {
const valToString = (s) => {
// 1. Operator names
const op_t = operatorTranslations.get(s);
if (op_t !== undefined)
return op_t;
const op_n = operatorNames.get(s);
if (op_n !== undefined) {
operatorTranslations.set(s, op_n);
return op_n;
}
if (DlCore.dlOperators.has(s)) {
const op_s = `<${s}>`;
operatorTranslations.set(s, op_s); // unsupported operator
return op_s;
}
// 2. Variable names
const var_t = variableTranslations.get(s);
if (var_t !== undefined)
return var_t;
if (nextVariableIndex >= sequenceOfVarNames.length) {
const var_s = `<${s}>`;
variableTranslations.set(s, var_s); // unsupported variable
return var_s;
}
const var_n = sequenceOfVarNames[nextVariableIndex++];
variableTranslations.set(s, var_n);
return var_n;
};
let str = valToString(node.getValue());
for (let i = 0; i < node.getChildren().length; i++) {
str += recurse(node.getChildren()[i]);
}
return str;
};
return recurse(formula);
}
static toPolishNotation_noRename(f, prioritizeBochenski) {
const operatorNames = prioritizeBochenski ? DlCore.operatorNames_boc : DlCore.operatorNames_luk;
const recurse = (node, startsWithVar, endsWithVar) => {
const valToString = (s) => {
// 1. Operator names
const op = operatorNames.get(s);
if (op !== undefined) {
startsWithVar.value = false;
return op;
}
// 2. Variable names
startsWithVar.value = true;
return s;
};
let str = valToString(node.getValue());
let prevEndsWithVar = startsWithVar.value;
for (let i = 0; i < node.getChildren().length; i++) {
let childStartsWithVar = { value: null };
let childEndsWithVar = { value: null };
let tmp = recurse(node.getChildren()[i], childStartsWithVar, childEndsWithVar);
str += prevEndsWithVar && childStartsWithVar.value ? "." + tmp : tmp;
prevEndsWithVar = childEndsWithVar.value;
}
endsWithVar.value = prevEndsWithVar;
return str;
};
let x = { value: null };
let y = { value: null };
return recurse(f, x, y);
}
// Extract the sequence of variables of the given DL-formula
static primitiveSequenceOfFormula(formula) {
const result = [];
const recurse = (formula) => {
if (formula.getChildren().length === 0) {
const value = formula.getValue();
if (value !== "\\bot" && value !== "\\top")
result.push(value);
} else
for (const subformula of formula.getChildren())
recurse(subformula);
};
recurse(formula);
return result;
}
static primitivesOfFormula_ordered(formula) {
const result = [];
const resultSequence = DlCore.primitiveSequenceOfFormula(formula);
const resultSet = new Set();
for (const name of resultSequence) {
let oldSize = resultSet.size;
if (resultSet.add(name).size === oldSize + 1)
result.push(name);
}
return result;
}
// Extract the set of variables of the given DL-formula
static primitivesOfFormula(formula) {
const result = new Set();
const recurse = (formula) => {
if (formula.getChildren().length === 0) {
const value = formula.getValue();
if (value !== "\\bot" && value !== "\\top")
result.add(value);
} else
for (const subformula of formula.getChildren())
recurse(subformula);
};
recurse(formula);
return result;
}
static toPolishNotation_numVars(f, prioritizeBochenski) {
const operatorNames = prioritizeBochenski ? DlCore.operatorNames_boc : DlCore.operatorNames_luk;
const variables = DlCore.primitivesOfFormula_ordered(f);
const variableTranslation = new Map();
let counter = 0;
for (const variable of variables) {
variableTranslation.set(variable, counter.toString());
counter++;
}
const recurse = (node, startsWithVar, endsWithVar) => {
const valToString = (s) => {
// 1. Operator names
const op = operatorNames.get(s);
if (op !== undefined) {
startsWithVar.value = false;
return op;
}
// 2. Variable names
startsWithVar.value = true;
return variableTranslation.get(s);
};
let str = valToString(node.getValue());
let prevEndsWithVar = startsWithVar.value;
for (let i = 0; i < node.getChildren().length; i++) {
let childStartsWithVar = { value: null };
let childEndsWithVar = { value: null };
let tmp = recurse(node.getChildren()[i], childStartsWithVar, childEndsWithVar);
str += prevEndsWithVar && childStartsWithVar.value ? "." + tmp : tmp;
prevEndsWithVar = childEndsWithVar.value;
}
endsWithVar.value = prevEndsWithVar;
return str;
};
let x = { value: null };
let y = { value: null };
return recurse(f, x, y);
}
}