-
Notifications
You must be signed in to change notification settings - Fork 0
/
gentzen.go
155 lines (132 loc) · 3.94 KB
/
gentzen.go
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
package main
import (
"fmt"
)
// checks for validity of an expression`
func (exp Expression) Proove() bool {
var seqs, queue = make(Sequents, 0), make(Sequents, 0)
queue = append(queue, Sequent{Expressions{}, Expressions{exp}})
if !_optMap["--validity-only"] {
fmt.Println("Proof Tree:")
fmt.Println(queue.Printseqs())
}
for curLvl, nxtLvl := 1, 0; len(queue) > 0; {
top := queue[0]
if len(queue) == 1 {
queue = []Sequent{}
} else {
queue = queue[1:]
}
curLvl--
if top.IsLowestForm() == false {
decSeqs := top.Decompose()
queue = append(queue, decSeqs...)
nxtLvl += len(decSeqs)
} else {
seqs = append(seqs, top)
}
if !_optMap["--validity-only"] {
if curLvl == 0 {
fmt.Println(queue.Printseqs())
curLvl, nxtLvl = nxtLvl, 0
}
}
}
if !_optMap["--validity-only"] {
fmt.Println("Final Sequents:")
fmt.Println(seqs.Printseqs(), "\n")
}
res := true
for i := 0; i < len(seqs); i++ {
res = res && seqs[i].HasContradiction()
}
return res
}
// decompose a sequent by using one of the 8 rules
func (seq Sequent) Decompose() Sequents {
ant, con := seq.ant, seq.con
// right rules without fanout (right-NOT, right-OR, right-IMPLICATION)
for i, exp := range con {
newCon := con[:i].Append(con[i+1:]...)
if exp.etype == 2 && exp.mid == _not {
return Sequents{Sequent{ant.AppendUnique(*(exp.right)), newCon}}
} else if exp.etype == 3 && exp.mid == _or {
return Sequents{Sequent{ant, newCon.AppendUnique(*(exp.left), *(exp.right))}}
} else if exp.etype == 3 && exp.mid == _imp {
newCon1 := newCon.AppendUnique(*(exp.right))
newAnt := ant.AppendUnique(*(exp.left))
return Sequents{Sequent{newAnt, newCon1}}
}
}
// left rules without fanout (left-NOT, left-AND)
for i, exp := range ant {
newAnt := ant[:i].Append(ant[i+1:]...)
if exp.etype == 2 && exp.mid == _not {
return Sequents{Sequent{newAnt, con.AppendUnique(*(exp.right))}}
} else if exp.etype == 3 && exp.mid == _and {
return Sequents{Sequent{newAnt.AppendUnique(*(exp.left), *(exp.right)), con}}
}
}
// right rules with fanout (right-AND)
for i, exp := range con {
newCon := con[:i].Append(con[i+1:]...)
if exp.etype == 3 && exp.mid == _and {
newCon1 := newCon.AppendUnique(*(exp.left))
newCon2 := newCon.AppendUnique(*(exp.right))
return Sequents{Sequent{ant, newCon1}, Sequent{ant, newCon2}}
}
}
// left rules with fanout (left-OR, left-IMPLICATION)
for i, exp := range ant {
newAnt := ant[:i].Append(ant[i+1:]...)
if exp.etype == 3 && exp.mid == _or {
newAnt1 := newAnt.AppendUnique(*(exp.left))
newAnt2 := newAnt.AppendUnique(*(exp.right))
return Sequents{Sequent{newAnt1, con}, Sequent{newAnt2, con}}
} else if exp.etype == 3 && exp.mid == _imp {
newAnt1 := newAnt.AppendUnique(*(exp.right))
newCon2 := con.AppendUnique(*(exp.left))
return Sequents{Sequent{newAnt, newCon2}, Sequent{newAnt1, con}}
}
}
return Sequents{}
}
// returns true if the sequent has a contradiction
// the sequent must be in the lowest form (not decomposable further)
func (seq Sequent) HasContradiction() (res bool) {
res = false
ant, con := seq.ant, seq.con
antTerms := make(map[string]bool)
for i := 0; i < len(ant); i++ {
if ant[i].mid == _false {
res = true
return
}
antTerms[ant[i].mid] = true
}
for i := 0; i < len(con); i++ {
if antTerms[con[i].mid] == true || con[i].mid == _true {
res = true
return
}
}
return
}
// returns true if a sequent cannot be decomposed further
func (seq Sequent) IsLowestForm() (res bool) {
res = true
ant, con := seq.ant, seq.con
for i := 0; i < len(ant); i++ {
if ant[i].etype != 1 {
res = false
return
}
}
for i := 0; i < len(con); i++ {
if con[i].etype != 1 {
res = false
return
}
}
return
}