-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargv1.2.py
More file actions
211 lines (199 loc) · 5.96 KB
/
Copy pathargv1.2.py
File metadata and controls
211 lines (199 loc) · 5.96 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
#tree definition
class parsetree(object):
def __init__(self):
self.top = None
self.left = None
self.right = None
#inputs premises and conclusion
nop=int(input("Number of premises "))
premises=input("")
nop -=1
for i in range(nop):
a=input("")
premises="("+premises+")."+"("+a+")"
starter = parsetree()
ctarter = parsetree()
noc=int(input("Number of conclusions "))
conclusion = input("")
noc -=1
for i in range(noc):
a=input("")
conclusion="("+conclusion+")."+"("+a+")"
variables = []
#identifies all of the variables
for i in (premises+conclusion):
if (i.isalpha()):
if(i in variables):
continue
else:
variables.append(i)
else:
continue
#remembers the index of the variable in the truthtable
index = {}
for i in variables:
index[i]=variables.index(i)
#generates all possible combination of the truthvalues of the variables
truthposs = []
possconst = 2**len(variables)
for i in range(possconst):
truthposs.append([int(x) for x in bin(i)[2:].zfill(len(variables))])
#stores colum of every variable inside the truthable
truthtable = {}
#print(index)
#print(truthposs)
for i in variables:
truthtable[i] = []
truthtable["("+i+")"] = []
for j in range(possconst):
#print(i,j,possconst)
truthtable[i].append(truthposs[j][index[i]])
truthtable["("+i+")"].append(truthposs[j][index[i]])
# parsing function
def bracketfinder(tree,premises):
if (len(premises)==1):
tree.top = premises
tree.left = None
tree.right = None
#print("returned")
return tree
elif(premises[0]=="!"):
#print("maagic point",premises)
tree.top="^"
tree.left=parsetree()
tree.left.top="!"
tree.right = bracketfinder(parsetree(),premises[2:len(premises)-1])
return tree
stack = 0
i=0
start=[]
stop=[]
while (i<len(premises)):
if (premises[i] == "("):
if(stack ==0):
start.append(i)
stack +=1
elif (premises[i]==")"):
stack -=1
if(stack ==0):
stop.append(i)
i +=1
if(len(start)<2):
tree.top = premises[start[0]+1:stop[0]]
else:
tree.top = premises[stop[0]+1]
#print("start",tree.top)
#print(start[0],stop[0])
tree.left=bracketfinder(parsetree(),premises[start[0]+1:stop[0]])
#print("left",tree.left.top)
#print(start[1],stop[1])
tree.right=bracketfinder(parsetree(),premises[start[1]+1:stop[1]])
#print("right",tree.right.top)
return tree
#parses premises and conclusion
starter=bracketfinder(starter,premises)
ctarter=bracketfinder(ctarter,conclusion)
#depth first search to solve the parse tree
stepbystep = []
stepsolver = parsetree()
def solv(tree):
newtree = parsetree()
if(tree.top !="^"):
#print(tree.top)
newtree.top = "("+tree.left.top+tree.top+tree.right.top+")"
else:
newtree.top = "(!"+tree.right.top+")"
stepbystep.append(newtree.top)
st = newtree.top
#print(newtree.top)
if(tree.top == "."):
#tree.left.top AND tree.right.top
#st=tree.left.top+"."+tree.right.top
truthtable[st]=[]
for i in range(possconst):
b=tree.left.top
c=tree.right.top
#print(type(truthtable[b][i]))
a = truthtable[b][i]*truthtable[c][i]
truthtable[st].append(a)
elif(tree.top == "+"):
#tree.left.top OR tree.right.top
#st=tree.left.top+"+"+tree.right.top
truthtable[st]=[]
for i in range(possconst):
b=tree.left.top
c=tree.right.top
a=0
if(truthtable[b][i]+truthtable[c][i]):
a=1
truthtable[st].append(a)
elif(tree.top == "^"):
#NOT tree.right.top
#st="!"+tree.right.top
#print("here also")
truthtable[st]=[]
for i in range(possconst):
a= truthtable[tree.right.top][i]^1
truthtable[st].append(a)
tree.top = newtree.top
tree.left = None
tree.right = None
#tells solv function what to solve
def enumer(tree):
if (tree.left is not None):
#print("\t"*level+"son")
enumer(tree.left)
#print("\t"*level+tree.top)
if(tree.right is not None):
#print("\t"*level+ "daughter")
enumer(tree.right)
if(tree.top in truthtable):
#print("treetop",tree.top)
tree.top = "("+tree.top+")"
return 1
elif (tree.top in [".","+","^"]):
solv(tree)
#print("here you go big son",starter.top)
enumer(starter)
#print("here you go son",ctarter.top)
enumer(ctarter)
gg ="("+premises+")"
bb="("+conclusion+")"
#kind of check whether the parsing and solving were successfull
if(not(gg in truthtable and bb in truthtable)):
print("parse error")
exit()
contradiction =0
i=0
#compares the columns of premises and conclusion
counterexamples=[]
while (i<possconst):
if(truthtable[gg][i]==1):
if(conclusion in truthtable):
if(truthtable[conclusion][i]!=1):
contradiction=1
counterexample.append(i)
else:
if(truthtable[bb][i]!=1):
contradiction=1
counterexample.append(i)
i+=1
if(contradiction):
print("the arguement is false :D")
else:
print("wow the argument is cool")
count = 97
for i in stepbystep:
print(chr(count),"=",i)
count +=1
#for i in truthtable:
# print(chr(count),"=",i)
# count +=1
for i in range(97,count):
print("|"+chr(i)+"|",end=" ")
print(possconst)
for j in range(possconst):
print("",end="\n")
#print(Fore.RED+"asdfasdf")
for i in stepbystep:
print("|"+str(truthtable[i][j])+"|",end=" ")