-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSequentRules.ts
More file actions
396 lines (378 loc) · 17.8 KB
/
SequentRules.ts
File metadata and controls
396 lines (378 loc) · 17.8 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
/*
Rules of the sequent calculus
Copyright (C) 2025 Andreas Kovalski
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* For all rules : big greek letters are sequences of formulas, small greek letters are formulas. Sequences are also allowed to be empty
*/
const structuralRules = (function(){
function copy(antecedent : readonly Formula[], succedent : readonly Formula[]){
const formulaCopier : CopyCreator = new CopyCreator()
return new Sequent(
antecedent.slice().map(formula => formula.acceptVisitor(formulaCopier)),
succedent.slice().map(formula => formula.acceptVisitor(formulaCopier)),
)
}
/**
* |||
* |-|-|
* | **Premise:** | Gamma => Delta |
* | **Conclusion:** | phi, Gamma => Delta |
* @param conclusion The sequent before the rule use
* @returns The resulting premise by removing the left most formula
*/
function leftWeakening(conclusion : Sequent) : [Sequent]{
const withoutFormula = conclusion.antecedent.slice(1)
return [copy(withoutFormula, conclusion.succedent)]
}
/**
* |||
* |-|-|
* | **Premise:** | Gamma => Delta |
* | **Conclusion:** | Gamma => Delta, phi |
* @param conclusion The sequent before the rule use
* @returns The resulting premise by removing the right most formula
*/
function rightWeakening(conclusion : Sequent) : [Sequent]{
const withoutFormula = conclusion.succedent.slice(1)
return [copy(conclusion.antecedent, withoutFormula)]
}
/**
* |||
* |-|-|
* | **Premise:** | Gamma, phi, psi, Pi => Delta |
* | **Conclusion:** | Gamma, psi, phi, Pi => Delta |
* @param conclusion The sequent before the rule use
* @returns The resulting premise by switching the positions of 2 formula in the antecedent that are next to each other
*/
function leftExchange(conclusion : Sequent, leftFormulaIndex : number) : [Sequent]{
if(leftFormulaIndex >= conclusion.antecedent.length-1)throw new Error("Can't use exchange rules: No formula on the right")
const leftSequence = conclusion.antecedent.slice(0,leftFormulaIndex)
const rightSequent = conclusion.antecedent.slice(leftFormulaIndex+2, conclusion.antecedent.length)
const leftFormula = conclusion.antecedent[leftFormulaIndex]
const rightFormula = conclusion.antecedent[leftFormulaIndex+1]
return [copy([...leftSequence, rightFormula, leftFormula, ...rightSequent], conclusion.succedent)]
}
/**
* |||
* |-|-|
* | **Premise:** | Gamma => Delta, phi, psi, Lambda |
* | **Conclusion:** | Gamma => Delta, psi, phi, Lambda |
* @param conclusion The sequent before the rule use
* @returns The resulting premise by switching the positions of 2 formula in the succedent that are next to each other
*/
function rightExchange(conclusion : Sequent, leftFormulaIndex : number) : [Sequent]{
if(leftFormulaIndex >= conclusion.succedent.length-1)throw new Error("Can't use exchange rules: No formula on the right")
const leftSequence = conclusion.succedent.slice(0,leftFormulaIndex)
const rightSequent = conclusion.succedent.slice(leftFormulaIndex+2, conclusion.succedent.length)
const leftFormula = conclusion.succedent[leftFormulaIndex]
const rightFormula = conclusion.succedent[leftFormulaIndex+1]
return [copy(conclusion.antecedent, [...leftSequence, rightFormula, leftFormula, ...rightSequent])]
}
leftWeakening.toString = () => "Left Weakening Rule"
rightWeakening.toString = () => "Right Weakening Rule"
leftExchange.toString = () => "Left Exchange Rule"
rightExchange.toString = () => "Right Exchange Rule"
leftWeakening.proofName = "(We.l)"
rightWeakening.proofName = "(We.r)"
leftExchange.proofName = "(Ex.l)"
rightExchange.proofName = "(Ex.r)"
return {
leftWeakening,
rightWeakening,
leftExchange,
rightExchange
} as {leftWeakening : Rule, rightWeakening : Rule ,leftExchange : Rule ,rightExchange : Rule }
}())
type Rule = {(...params : any): Sequent[];toString: () => string;proofName: string;}
const proofHelper = (function(){
class ConnectiveFinder implements FormulaVisitor<boolean>{
visitImplication = (formula: Implication): boolean => true
visitConjunction = (formula: Conjunction): boolean => true
visitDisjunction = (formula: Disjunction): boolean => true
visitNegation = (formula: Negation): boolean => true
visitVariable = (formula: Variable): boolean => false
visitTruth = (formula: Truth): boolean => {throw new Error("Can't have Truth symbol in Sequents")}
visitFalsity = (formula: Falsity): boolean => {throw new Error("Can't have Falsity symbol in Sequents")}
}
/**
* For a sequent, applies a logical/propositional rule for one of it's formulas by visiting that formula
*
* The formula must be on the outside of it's cedent, so it's at index 0
*
* @todo instead of returning rules as functions, just immediatly return the premises.
* Don't ask me why I didn't do this the first time
*/
class RuleFinder implements FormulaVisitor<Rule>{
constructor(public conclusion : Sequent, public formulaInAntecedent : boolean){
}
private generateNameFunction(name : String){
return this.formulaInAntecedent ? () => "Left "+name+" Rule" : () => "Right "+name+" Rule"
}
private generateProofName(name : String){
return this.formulaInAntecedent ? `(${name}:l)` : `(${name}:r)`
}
/**
* |Left: | | |Right: |
* |:- |- |-|:- |
* | **Premise:** | Gamma => Delta, phi and psi, Gamma => Delta | | phi,Gamma => Delta. psi |
* | **Conclusion:** | phi -> psi, Gamma => Delta | | Gamma => Delta, phi -> psi |
*
* @param formula A formula at the outside of a cedent
* @returns A function returning the resulting premises
*/
visitImplication(formula: Implication): Rule {
if(this.formulaInAntecedent){
//left implication rule
const withoutFormula = this.conclusion.antecedent.slice(1)
const leftImplication = () => {
return [
copySequent(withoutFormula, [formula.left, ...this.conclusion.succedent]),
copySequent([formula.right, ...withoutFormula], this.conclusion.succedent)
]
}
leftImplication.toString = this.generateNameFunction("Implication")
leftImplication.proofName = this.generateProofName("\u2192")
return leftImplication
}
//right implication rule
const withoutFormula = this.conclusion.succedent.slice(1)
const rightImplication = () => [copySequent([formula.left, ...this.conclusion.antecedent], [ formula.right, ...withoutFormula])]
rightImplication.toString = this.generateNameFunction("Implication")
rightImplication.proofName = this.generateProofName("\u2192")
return rightImplication
}
/**
* |Left: | | |Right: |
* |:- |- |-|:- |
* | **Premise:** | phi, psi, Gamma => Delta | | phi, Gamma => Delta and psi, Gamma => Delta |
* | **Conclusion:** | phi & psi, Gamma => Delta | | Gamma => Delta, phi -> psi |
*
* @param formula A formula at the outside of a cedent
* @returns A function returning the resulting premises
*/
visitConjunction(formula: Conjunction): Rule {
if(this.formulaInAntecedent){
//left conjunction rule
const withoutFormula = this.conclusion.antecedent.slice(1)
const leftConjunction = () => [copySequent([formula.left, formula.right, ...withoutFormula], this.conclusion.succedent)]
leftConjunction.toString = this.generateNameFunction("Conjunction")
leftConjunction.proofName = this.generateProofName("\u2227")
return leftConjunction
}
//right conjunction rule
const withoutFormula = this.conclusion.succedent.slice(1)
const rightConjunction : Rule = () => [
copySequent(this.conclusion.antecedent,[formula.left, ...withoutFormula]),
copySequent(this.conclusion.antecedent,[formula.right, ...withoutFormula]),
]
rightConjunction.toString = this.generateNameFunction("Conjunction")
rightConjunction.proofName = this.generateProofName("\u2227")
return rightConjunction
}
/**
* |Left: | | |Right: |
* |:- |- |-|:- |
* | **Premise:** | phi, Gamma => Delta and psi, Gamma => Delta | | Gamma => Delta, psi, phi |
* | **Conclusion:** | phi \psi, Gamma => Delta | | Gamma => Delta, phi \| psi |
*
* @param formula A formula at the outside of a cedent
* @returns A function returning the resulting premises
*/
visitDisjunction(formula: Disjunction): Rule {
if(this.formulaInAntecedent){
//left disjunction rule
const withoutFormula = this.conclusion.antecedent.slice(1)
const leftDisjunction = () => [
copySequent([formula.left, ...withoutFormula], this.conclusion.succedent),
copySequent([formula.right, ...withoutFormula], this.conclusion.succedent)
]
leftDisjunction.toString = this.generateNameFunction("Disjunction")
leftDisjunction.proofName = this.generateProofName("\u2228")
return leftDisjunction
}
//right disjunction rule
const withoutFormula = this.conclusion.succedent.slice(1)
const rightDisjunction = () => [copySequent(this.conclusion.antecedent, [formula.left, formula.right, ...withoutFormula])]
rightDisjunction.toString = this.generateNameFunction("Disjunction")
rightDisjunction.proofName = this.generateProofName("\u2228")
return rightDisjunction
}
/**
* |Left: | | |Right: |
* |:- |- |-|:- |
* | **Premise:** | Gamma => Delta, phi | | phi, Gamma => Delta |
* | **Conclusion:** | -phi, Gamma => Delta | | Gamma => Delta, -phi|
*
* @param formula A formula at the outside of a cedent
* @returns A function returning the resulting premises
*/
visitNegation(formula: Negation): Rule {
if(this.formulaInAntecedent){
//left negation rule
const withoutFormula = this.conclusion.antecedent.slice(1)
const leftNegation = () => [copySequent(withoutFormula, [formula.formula, ...this.conclusion.succedent])]
leftNegation.toString = this.generateNameFunction("Negation")
leftNegation.proofName = this.generateProofName("\u00AC")
return leftNegation
}
//right negation rule
const withoutFormula = this.conclusion.succedent.slice(1)
const rightNegation = () => [copySequent([formula.formula, ...this.conclusion.antecedent], withoutFormula)]
rightNegation.toString = this.generateNameFunction("Negation")
rightNegation.proofName = this.generateProofName("\u00AC")
return rightNegation
}
visitVariable(formula: Variable): any {
throw new Error("not.")
}
visitTruth(formula: Truth): any {
throw new Error("not.")
}
visitFalsity(formula: Falsity): any {
throw new Error("not.")
}
}
const formulaCopier : CopyCreator = new CopyCreator()
/**
* Moves a formula in one of the cedents from {@link formulaIndex | its index} to the highest possible index using exchange rules.
*
* Since the succedent is "mirrored", this means that the formula is now on the outside, and propositional rules can be applied to it.
*
* @param sequent The node of a proof containing the sequent with a formula we want to move outside
* @param formulaIndex The index of the formula we want to move
* @param inAntecedent Determines if {@link formulaIndex} applies to the antecedent or the succedent
* @returns A ProofTree node resulting from applying the exchange rules a bunch of times
*/
function moveFormulaOutside(sequent : ProofTree, formulaIndex : number, inAntecedent : boolean){
return moveFormula(sequent, formulaIndex, inAntecedent, true)
}
/**
* Moves a formula in one of the cedents from {@link formulaIndex | its index} to index 0 using exchange rules
*
* Since the succedent is "mirrored", index 0 means that the formula is now next to the sequent arrow.
* This is helpful when the formula is a variable that's part of an initial axiom, since we can remove all other formulas in the cedents in one go
*
* @param sequent The node of a proof containing the sequent with a formula we want to move outside
* @param formulaIndex The index of the formula we want to move
* @param inAntecedent Determines if {@link formulaIndex} applies to the antecedent or the succedent
* @returns A ProofTree node resulting from applying the exchange rules a bunch of times
*/
function moveFormulaInside(sequent : ProofTree, formulaIndex : number, inAntecedent : boolean){
return moveFormula(sequent,
formulaIndex,
inAntecedent,
false
)
}
/**
* Helper function for {@link moveFormulaOutside} and {@link moveFormulaInside}
*/
function moveFormula(sequent : ProofTree, formulaIndex : number, inAntecedent : boolean, moveToOutside : boolean){
const neccesaryRule = inAntecedent ? structuralRules.leftExchange : structuralRules.rightExchange
const relativeSwapPosition = moveToOutside ? -1 : 0
const formulaShift = moveToOutside ? -1 : 1
const moveDecider = moveToOutside ? () => formulaIndex > 0 : (
inAntecedent ? () => formulaIndex < sequent.sequent.antecedent.length-1 : () => formulaIndex < sequent.sequent.succedent.length-1
)
let currentProofSequent = sequent
while(moveDecider()){
currentProofSequent.premiseOne = new ProofTree(neccesaryRule(currentProofSequent.sequent, formulaIndex + relativeSwapPosition)[0])
currentProofSequent.usedRule = neccesaryRule
currentProofSequent = currentProofSequent.premiseOne
formulaIndex += formulaShift
}
return currentProofSequent
}
/**
* For a sequent with only variables
* and the same variable on the most inner position in each cedent,
* this function creates the rest of the proof in {@link sequent} to get to the initial axiom
*
* @param sequent A sequent
* @throws An error if a formula that is not a variable can be found
*/
function deriveAxiom(sequent : ProofTree){
sequent.sequent.antecedent.every(formula => {if(!(formula instanceof Variable))throw new Error("antecedent must only have variables")})
sequent.sequent.succedent.every(formula => {if(!(formula instanceof Variable))throw new Error("succedent must only have variables")})
let currentProof = sequent
while(currentProof.sequent.antecedent.length > 1){
currentProof.usedRule = structuralRules.leftWeakening
const nextProof = new ProofTree(structuralRules.leftWeakening(currentProof.sequent)[0])
currentProof.premiseOne = nextProof
currentProof = nextProof
}
while(currentProof.sequent.succedent.length > 1){
currentProof.usedRule = structuralRules.rightWeakening
const nextProof = new ProofTree(structuralRules.rightWeakening(currentProof.sequent)[0])
currentProof.premiseOne = nextProof
currentProof = nextProof
}
}
/**
* Tries to find a formula in {@link sequent} that is a connective.
*
* Used to find out on which formula we want to use a propositional rule.
*
* @param sequent a sequent
* @returns If a connective can be found, then returns in which cedent it has been found,
* and the index of the connective in that specified cedent. Else ["no"]
*/
function hasConnectiveFormula(sequent : Sequent) : ["antecedent", number] | ["succedent", number] | ["no"]{
let position = sequent.antecedent.findIndex(formula => formula.acceptVisitor(new ConnectiveFinder))
if(position !== -1) return ["antecedent", position]
position = sequent.succedent.findIndex(formula => formula.acceptVisitor(new ConnectiveFinder))
if(position !== -1) return ["succedent", position]
return ["no"]
}
/**
* Finds the same variable in both cedents of {@link seq}.
*
* @param seq A sequent
* @returns Either the indices of a variable in the antecedent and the succedent,
* or undefined if no one variable could be found in both cedents
* @throws An error if a formula that is not a variable can be found in {@link seq}
*/
function findAxiom(seq : Sequent) : [number, number] | undefined{
seq.antecedent.every(formula => {if(!(formula instanceof Variable))throw new Error("antecedent must only have variables")})
seq.succedent.every(formula => {if(!(formula instanceof Variable))throw new Error("succedent must only have variables")})
const antecedentVariableNames = seq.antecedent.map(form => (form as Variable).name)
const succedentVariableNames = seq.succedent.map(form => (form as Variable).name)
for(let antIndex = antecedentVariableNames.length-1; antIndex >= 0; antIndex--){
for(let sucIndex = succedentVariableNames.length-1; sucIndex >= 0; sucIndex--){
if(antecedentVariableNames[antIndex] == succedentVariableNames[sucIndex])return [antIndex, sucIndex]
}
}
return undefined
}
/**
* @param antecedent The antecedent of a sequent that needs to be copied
* @param succedent The succedent of a sequent that needs to be copied
* @returns A sequent where both cedents are deep copies of the provided cedents
*/
function copySequent(antecedent : readonly Formula[], succedent : readonly Formula[]){
return new Sequent(
antecedent.slice().map(formula => formula.acceptVisitor(formulaCopier)),
succedent.slice().map(formula => formula.acceptVisitor(formulaCopier)),
)
}
return{
RuleFinder,
moveFormula,
hasConnectiveFormula,
findAxiomVariables: findAxiom,
moveFormulaInside,
moveFormulaOutside,
deriveAxiom
}
})()