This repository was archived by the owner on Dec 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtableaux.py
More file actions
265 lines (230 loc) · 7.59 KB
/
tableaux.py
File metadata and controls
265 lines (230 loc) · 7.59 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
# -*- coding: utf-8 -*-
"""
Created on Thu Oct 15 19:32:44 2020
@author: dleyv
"""
from random import choice
##############################################################################
# Variables globales
##############################################################################
# Crea los conectivos
conectivos = ['Y', 'O', '>', '-']
binarios = ['Y', 'O', '>', '<->']
negacion = ['-']
# Crea las letras minúsculas a-z
letrasProposicionales = [chr(x) for x in range(97, 123)]
# inicializa la lista de interpretaciones
listaInterpsVerdaderas = []
# inicializa la lista de hojas
listaHojas = []
##############################################################################
# Definición de objeto tree y funciones de árboles
##############################################################################
class Tree(object):
def __init__(self, label, left, right):
self.left = left
self.right = right
self.label = label
def Inorder(f):
# Imprime una formula como cadena dada una formula como arbol
# Input: tree, que es una formula de logica proposicional
# Output: string de la formula
if (f.right == None):
return f.label
elif f.label == '-':
return f.label + Inorder(f.right)
else:
return "(" + Inorder(f.left) + f.label + Inorder(f.right) + ")"
def String2Tree(A):
# Crea una formula como tree dada una formula como cadena escrita en notacion polaca inversa
# Input: - A, lista de caracteres con una formula escrita en notacion polaca inversa
# - letrasProposicionales, lista de letras proposicionales
# - conectivos, lista de conectivos
# Output: formula como tree
pila = []
for c in A:
# print("Examinando " + str(c))
if c in letrasProposicionales:
# print(u"El símbolo es letra proposicional")
pila.append(Tree(c, None, None))
elif c == '-':
# print("Negamos")
formulaAux = Tree(c, None, pila[-1])
del pila[-1]
pila.append(formulaAux)
elif c in conectivos:
# print("Unimos mediante conectivo")
formulaAux = Tree(c, pila[-1], pila[-2])
del pila[-1]
del pila[-1]
pila.append(formulaAux)
else:
print(u"Hay un problema: el símbolo " + str(c) + " no se reconoce")
return pila[-1]
def Inorder2Tree(A):
if len(A) == 1:
return Tree(A[0], None, None)
elif A[0] == '-':
return Tree(A[0], None, Inorder2Tree(A[1:]))
elif A[0] == "(":
counter = 0 #Contador de parentesis
for i in range(1, len(A)):
if A[i] == "(":
counter += 1
elif A[i] == ")":
counter -=1
elif (A[i] in ['Y', 'O', '>', '=']) and (counter == 0):
return Tree(A[i], Inorder2Tree(A[1:i]), Inorder2Tree(A[i + 1:-1]))
else:
return -1
##############################################################################
# Definición de funciones de tableaux
##############################################################################
def imprime_hoja(H):
cadena = "{"
primero = True
for f in H:
if primero == True:
primero = False
else:
cadena += ", "
cadena += Inorder(f)
return cadena + "}"
def imprime_listaHojas(L):
for h in L:
print(imprime_hoja(h))
def complemento(l):
# Esta función devuelve el complemento de un literal
# Input: l, un literal
# Output: x, un literal
if (l.label == '-'):
return l.right
else:
return Tree('-',None,l)
def par_complementario(l):
# Esta función determina si una lista de solo literales
# contiene un par complementario
# Input: l, una lista de literales
# Output: True/False
for i in l:
indices = [x for x in l if x != i]
for j in indices:
if (Inorder(i) == Inorder(complemento(j))):
return True
return False
def es_literal(f):
# Esta función determina si el árbol f es un literal
# Input: f, una fórmula como árbol
# Output: True/False
if f.label in binarios:
return False
elif f.label in negacion:
f = f.right
if f.label in negacion:
return False
return es_literal(f)
else:
return True
def no_literales(l):
# Esta función determina si una lista de fórmulas contiene
# solo literales
# Input: l, una lista de fórmulas como árboles
# Output: None/f, tal que f no es literal
for i in l:
if (es_literal(i) == False):
return i
return None
def clasificacion(f):
# clasifica una fórmula como alfa o beta
# Input: f, una fórmula como árbol
# Output: string de la clasificación de la formula
if f.label == '-':
#alfa
if f.right.label == '-':
return '1alfa'
elif f.right.label == 'O':
return '3alfa'
elif f.right.label == '>':
return '4alfa'
#beta
elif f.right.label == 'Y' :
return '1beta'
else:
return "Error en la clasificacion."
elif f.label == 'Y':
return '2alfa'
elif f.label == 'O':
return '2beta'
elif f.label == '>':
return '3beta'
else:
return "Error en la clasificacion."
def clasifica_y_extiende(f, h):
# Extiende listaHojas de acuerdo a la regla respectiva
# Input: f, una fórmula como árbol
# h, una hoja (lista de fórmulas como árboles)
# Output: no tiene output, pues modifica la variable global listaHojas
global listaHojas
print("Formula:", Inorder(f))
print("Hoja:", imprime_hoja(h))
assert(f in h), "La formula no esta en la lista!"
clase = clasificacion(f)
print("Clasificada como:", clase)
assert(clase != None), "Formula incorrecta " + imprime_hoja(h)
if clase == '1alfa':
aux = [x for x in h if x != f] + [f.right.right]
listaHojas.remove(h)
listaHojas.append(aux)
elif clase == '2alfa':
aux = [x for x in h if x!=f] + [f.left, f.right]
listaHojas.remove(h)
listaHojas.append(aux)
elif clase == '3alfa':
aux = [x for x in h if x!=f] + [Tree('-', None, f.right.right), Tree('-', None, f.right.left)]
listaHojas.remove(h)
listaHojas.append(aux)
elif clase == '4alfa':
aux = [x for x in h if x!=f] + [f.right.left, Tree('-', None, f.right.right)]
listaHojas.remove(h)
listaHojas.append(aux)
elif clase == '1beta':
aux1 = [x for x in h if x!=f] + [Tree('-', None, f.right.right)]
aux2 = [x for x in h if x!=f] + [Tree('-', None, f.right.left)]
listaHojas.remove(h)
listaHojas.append(aux1)
listaHojas.append(aux2)
elif clase == '2beta':
aux1 = [x for x in h if x!=f] + [f.right]
aux2 = [x for x in h if x!=f] + [f.left]
listaHojas.remove(h)
listaHojas.append(aux1)
listaHojas.append(aux2)
elif clase == '3beta':
aux1 = [x for x in h if x!=f] + [f.right]
aux2 = [x for x in h if x!=f] + [Tree('-', None, f.left)]
# Aqui el resto de casos
def Tableaux(f):
# Algoritmo de creacion de tableau a partir de lista_hojas
# Imput: - f, una fórmula como string en notación polaca inversa
# Output: interpretaciones: lista de listas de literales que hacen
# verdadera a f
global listaHojas
global listaInterpsVerdaderas
A = String2Tree(f)
print(u'La fórmula introducida es:\n', Inorder(A))
listaHojas = [[A]]
while (len(listaHojas) > 0):
h = choice(listaHojas)
print("Trabajando con hoja:\n", imprime_hoja(h))
x = no_literales(h)
if x == None:
if par_complementario(h):
listaHojas.remove(h)
else:
listaInterpsVerdaderas.append(h)
listaHojas.remove(h)
else:
clasifica_y_extiende(x, h)
return listaInterpsVerdaderas
sz = [Tree('-',None,Tree('p',None,None)),Tree('p',None,None),Tree('-',None,Tree('q',None,None)),Tree('q',None,None)]
print(no_literales(sz))