/
formula.js
282 lines (251 loc) · 7.54 KB
/
formula.js
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
/*
This file contains generic functions related to formula processing (parsing, substitution, printing etc.)
There are also unrelated bits, such as translations, which have to do with global state.
Languages are global dicts, current language is saved in the "clang", and refresh_dict should be called to apply changes.
*/
var land = '/\\', lor = '\\/', lcond = '->', liff = '<->', lnot = '~', lfals = '#', lreit='re',
lintro = 'i', lelim = 'e', lall = 'A', lexists = 'E', lasm = 'assump.', ltrue = 'T';
var operator_info = {
[lfals]: {
synonyms: ["#", "_|_"],
latex: "\\bot",
html: "⊥",
},
[ltrue]: {
synonyms: ["T"],
latex: "\\top",
html: "⊤",
},
[land]: {
synonyms: ["/\\", "&" , "&&" , "*" , "."],
latex: "\\wedge",
html: "∧",
},
[lor]: {
synonyms: ["\\/", "|" , "||" , "+" , ","],
latex: "\\vee",
html: "∨",
},
[lcond]: {
synonyms: ["->", "=>", ">"],
latex: "\\to",
html: "→",
},
[liff]: {
synonyms: ["<->", "<=>" , "=" , "<>"],
latex: "\\leftrightarrow",
html: "↔",
},
[lnot]: {
synonyms: ["¬" , "~" , "-"],
latex: "\\neg",
html: "¬",
},
[lall]: {
synonyms: ["A", "@"],
latex: "\\forall",
html: "∀",
},
[lexists]: {
synonyms: ["E"],
latex: "\\exists",
html: "∃",
},
};
function synonym_base(symb) {
for (const [k, info] of Object.entries(operator_info))
if (info.synonyms.includes(symb) )
return k ;
throw "uknown symbol " + symb;
}
var latex = Object.fromEntries(
Object.entries(operator_info).map(
([key, info]) => [key, info.latex]
)
);
latex[lasm] = '\\text{assump.}';
latex[lreit] = '\\text{re.}';
var html = Object.fromEntries(
Object.entries(operator_info).map(
([key, info]) => [key, info.html]
)
);
html[lreit] = 're.';
html[lasm] = 'assump.';
function subst_inst_of(inst, schema, transl, abusive /* if t/x and have subformula QxP(t), reject */)
{
if (inst.op !== schema.op)
return false;
if (inst.rel)
{
if (inst.rel != schema.rel || inst.vs.length != schema.vs.length)
return false;
for (let [i, obj] of inst.vs.entries())
{
if (transl.ignore_transl)
{
if (obj != schema.vs[i])
return false;
}
else
{
if (schema.vs[i] == transl.v)
{
if (transl.c == null)
{
transl.c = obj;
}
else if (transl.c != obj && (transl.total || obj != schema.vs[i]))
{
return false;
}
}
else if (schema.vs[i] != obj)
return false;
}
}
return true;
}
switch (inst.op) {
case lfals: case ltrue:
return true;
case lnot:
return subst_inst_of(inst.e, schema.e, transl, abusive);
case land: case lor: case lcond: case liff:
return subst_inst_of(inst.e1, schema.e1, transl, abusive) && subst_inst_of(inst.e2, schema.e2, transl, abusive);
case lall: case lexists:
if (inst.v !== schema.v)
return false;
if (transl.ignore_transl || inst.v === transl.v) {
let old_ignore_transl = transl.ignore_transl;
transl.ignore_transl = true;
let istrue = subst_inst_of(inst.e, schema.e, transl, abusive);
if (abusive && has_constant(schema.e, transl.c))
return false;
transl.ignore_transl = old_ignore_transl;
return istrue;
}
return subst_inst_of(inst.e, schema.e, transl, abusive);
}
}
function formulas_eql(f, g)
{
return subst_inst_of(f, g, { ignore_transl: true }, false);
}
function deep_copy(frm, to) {
if (!to)
return JSON.parse(JSON.stringify(frm));
else
Object.assign(to, JSON.parse(JSON.stringify(frm)));
}
function has_constant(form, c, not_consts)
{
if (!not_consts)
not_consts = [];
if (form.rel)
{
return form.vs.includes(c);
}
switch (form.op) {
case lfals: case ltrue:
return false;
case lall: lexists:
not_consts = not_consts.slice();
not_consts.push(form.v);
return has_constant(form.e, c, not_consts);
case lnot:
return has_constant(form.e, c, not_consts);
case land: case lor: case lcond: case liff:
return has_constant(form.e1, c, not_consts) || has_constant(form.e2, c, not_consts);
}
}
function subst(form, v, c)
{
if (form.rel)
{
form.vs = form.vs.map(x => x == v ? c : x);
}
switch (form.op) {
case lfals: case ltrue:
return;
case lall: case lexists: case lnot:
subst(form.e, v, c);
return;
case land: case lor: case lcond: case liff:
subst(form.e1, v, c);
subst(form.e2, v, c);
return;
}
}
function formula_terms(form, bound)
{
if (!bound)
bound = [];
if (form.rel)
{
return form.vs.filter(x => ! bound.includes(x));
}
switch (form.op) {
case lfals: case ltrue:
return [];
case lall: case lexists:
return formula_terms(form.e, bound.concat(form.v));
case lnot:
return formula_terms(form.e, bound);
case land: case lor: case lcond: case liff:
return formula_terms(form.e1, bound).concat(formula_terms(form.e2, bound));
}
}
var hr = {
'asm': 'pretp.',
'e': 'i',
'i': 'u',
'reit': 'op.',
}, en = {
'asm': 'assump.',
'e': 'e',
'i': 'i',
'reit': 're.',
};
var clang = en;
function refresh_lang() {
//lintro = window.clang['intro'];
//lelim = window.clang['elim'];
//lasm = window.clang['asm'];
latex[lreit] = '\\text{' + window.clang['reit'] + '}';
html[lreit] = window.clang['reit'];
latex[lasm] = '\\text{' + window.clang['asm'] + '}';
html[lasm] = window.clang['asm'];
}
function html_str_formula(form, depth) {
let str_formula = html_str_formula;
if (!depth) depth = 0;
if (form.rel) {
return form.rel + form.vs.join("");
}
if (form.v)
return html[form.op] + " " + form.v + " " + str_formula(form.e, depth + 1);
if (form.e)
return html[form.op] + str_formula(form.e, depth + 1);
if (form.e1)
return (depth > 0 ? '(' : '') + str_formula(form.e1, depth + 1) + ' ' + html[form.op] + ' ' + str_formula(form.e2, depth + 1) + (depth > 0 ? ')' : '');
if (form.op === lfals || form.op === ltrue)
return html[form.op];
return form;
}
function latex_str_formula(form, depth) {
let str_formula = latex_str_formula;
if (!depth) depth = 0;
if (form.rel) {
return form.rel + form.vs.join("");
}
if (form.v)
return latex[form.op] + " " + form.v + str_formula(form.e, depth + 1);
if (form.e)
return latex[form.op] + " " + str_formula(form.e, depth + 1);
if (form.e1)
return (depth > 0 ? '(' : '') + str_formula(form.e1, depth + 1) + ' ' + latex[form.op] + ' ' + str_formula(form.e2, depth + 1) + (depth > 0 ? ')' : '');
if (form.op === lfals || form.op === ltrue)
return latex[form.op];
return form;
}