-
Notifications
You must be signed in to change notification settings - Fork 0
/
syllogisms.py
333 lines (290 loc) · 12.2 KB
/
syllogisms.py
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
from itertools import product
class Proposition:
first: str
second: str
terms: tuple
copula: str
def __init__(self, first: str, copula: str, second: str):
self.first = first
self.second = second
self.terms = (first, second)
assert copula in {'a','e', 'i', 'o'}, print(f'Invalid copula "{copula}"')
self.copula = copula
def __repr__(self):
if self.copula == 'a':
return f'Everything that is a(n) {self.first} is a(n) {self.second}'
elif self.copula == 'e':
return f'Nothing that is a(n) {self.first} is a(n) {self.second}'
elif self.copula == 'i':
return f'Some things that are a(n) {self.first} are a(n) {self.second}'
else:
return f'Not all things that are a(n) {self.first} are a(n) {self.second}'
def __eq__(self, other):
return isinstance(other, Proposition) and str(self) == str(other)
def contrary(self):
if self.copula == 'a':
copula = 'e'
elif self.copula == 'e':
copula = 'a'
elif self.copula == 'i':
copula = 'o'
else:
copula = 'i'
return Proposition(self.first, copula, self.second)
def contradiction(self):
if self.copula == 'a':
copula = 'o'
elif self.copula == 'e':
copula = 'i'
elif self.copula == 'i':
copula = 'e'
else:
copula = 'a'
return Proposition(self.first, copula, self.second)
def subaltern(self):
if self.copula == 'a':
copula = 'i'
elif self.copula == 'e':
copula = 'o'
elif self.copula == 'i':
copula = 'a'
else:
copula = 'e'
return Proposition(self.first, copula, self.second)
class Syllogism:
major: Proposition
minor: Proposition
conclusion: Proposition
subject: str
predicate: str
middle_term: str
figure: str
mood: str
lines: list
figure1 = {
('a', 'a', 'a'): 'barbara',
('e', 'a', 'e'): 'celarent',
('a', 'i', 'i'): 'darii',
('e', 'i', 'o'): 'ferio',
('a', 'a', 'i'): 'barbari',
('e', 'a', 'o'): 'celaront'}
figure2 = {
('e', 'a', 'e'): 'cesare',
('a', 'e', 'e'): 'camestres',
('e', 'i', 'o'): 'festino',
('a', 'o', 'o'): 'baroco',
('e', 'a', 'o'): 'cesaro',
('a', 'e', 'o'): 'camestros'}
figure3 = {
('a', 'i', 'i'): 'datisi',
('i', 'a', 'i'): 'disamis',
('e', 'i', 'o'): 'ferison',
('o', 'a', 'o'): 'bocardo',
('e', 'a', 'o'): 'felapton',
('a', 'a', 'i'): 'darapti'}
figure4 = {
('a', 'e', 'e'): 'calemes',
('i', 'a', 'i'): 'dimatis',
('e', 'i', 'o'): 'fresison',
('a', 'e', 'o'): 'calemos',
('e', 'a', 'o'): 'fesapo',
('a', 'a', 'i'): 'bamalip'}
moods = {'barbara': ('1', ('a', 'a', 'a')),
'celarent': ('1', ('e', 'a', 'e')),
'darii': ('1', ('a', 'i', 'i')),
'ferio': ('1', ('e', 'i', 'o')),
'barbari': ('1', ('a', 'a', 'i')),
'celaront': ('1', ('e', 'a', 'o')),
'cesare': ('2', ('e', 'a', 'e')),
'camestres': ('2', ('a', 'e', 'e')),
'festino': ('2', ('e', 'i', 'o')),
'baroco': ('2', ('a', 'o', 'o')),
'cesaro': ('2', ('e', 'a', 'o')),
'camestros': ('2', ('a', 'e', 'o')),
'datisi': ('3', ('a', 'i', 'i')),
'disamis': ('3', ('i', 'a', 'i')),
'ferison': ('3', ('e', 'i', 'o')),
'bocardo': ('3', ('o', 'a', 'o')),
'felapton': ('3', ('e', 'a', 'o')),
'darapti': ('3', ('a', 'a', 'i')),
'calemes': ('4', ('a', 'e', 'e')),
'dimatis': ('4', ('i', 'a', 'i')),
'fresison': ('4', ('e', 'i', 'o')),
'calemos': ('4', ('a', 'e', 'o')),
'fesapo': ('4', ('e', 'a', 'o')),
'bamalip': ('4', ('a', 'a', 'i'))}
def __init__(self, major: Proposition, minor: Proposition, conclusion=None):
self.major = major
self.minor = minor
#determine figure
if major.first == minor.second:
self.figure = '1'
self.subject = minor.first
self.predicate = major.second
self.middle_term = major.first
elif major.second == minor.second:
self.figure = '2'
self.subject = minor.first
self.predicate = major.first
self.middle_term = major.second
elif major.first == minor.first:
self.figure = '3'
self.subject = minor.second
self.predicate = major.second
self.middle_term = major.first
else:
self.figure = '4'
self.subject = minor.second
self.predicate = major.first
self.middle_term = major.second
if conclusion:
self.conclusion = conclusion
else:
print('Pick a conclusion from the following:')
for copula in ['a', 'e', 'i', 'o']:
print(f'{copula}: {Proposition(self.subject, copula, self.predicate)}')
copula = str(input())
assert copula in {'a', 'e', 'i', 'o'}, print('invalid copula')
self.conclusion = Proposition(self.subject, copula, self.predicate)
self.lines = (self.major, self.minor, self.conclusion)
#determine mood
self.copulae = (self.major.copula, self.minor.copula, self.conclusion.copula)
if self.figure == '1':
if self.copulae in Syllogism.figure1:
self.mood = Syllogism.figure1[self.copulae]
else:
self.mood = ''.join([self.figure, *[line.copula for line in self.lines]])
elif self.figure == '2':
if self.copulae in Syllogism.figure2:
self.mood = Syllogism.figure2[self.copulae]
else:
self.mood = ''.join([self.figure, *[line.copula for line in self.lines]])
elif self.figure == '3':
if self.copulae in Syllogism.figure3:
self.mood = Syllogism.figure3[self.copulae]
else:
self.mood = ''.join([self.figure, *[line.copula for line in self.lines]])
elif self.figure == '4':
if self.copulae in Syllogism.figure4:
self.mood = Syllogism.figure4[self.copulae]
else:
self.mood = ''.join([self.figure, *[line.copula for line in self.lines]])
def __repr__(self):
return f"{self.major},\n{self.minor},\n\tTherefore,\n{self.conclusion}."
def __eq__(self, other):
if isinstance(other, Syllogism):
return self.major == other.major and self.minor == other.minor and self.conclusion == other.conclusion
return False
def is_valid(self):
if self.mood in Syllogism.moods:
if self.figure == '1':
return self.major.first == self.minor.second and self.major.second == self.conclusion.second and self.minor.first == self.conclusion.first
elif self.figure == '2':
return self.major.second == self.minor.second and self.major.first == self.conclusion.second and self.minor.first == self.conclusion.first
elif self.figure == '3':
return self.major.first == self.minor.first and self.major.second == self.conclusion.second and self.minor.second == self.conclusion.first
else:
return self.major.first == self.conclusion.second and self.major.second == self.minor.first and self.minor.second == self.conclusion.first
return False
def from_terms_figure_and_copulae(subject='S', middle_term='M', predicate='P', figure='1', copulae=('a','a','a')):
assert figure in {'1','2','3','4'}
assert len(copulae) == 3 and all(i in {'a','e','i','o'} for i in copulae)
if figure == '1':
major = Proposition(middle_term, copulae[0], predicate)
minor = Proposition(subject, copulae[1], middle_term)
elif figure == '2':
major = Proposition(predicate, copulae[0], middle_term)
minor = Proposition(subject, copulae[1], middle_term)
elif figure == '3':
major = Proposition(middle_term, copulae[0], predicate)
minor = Proposition(middle_term, copulae[1], subject)
else:
major = Proposition(predicate, copulae[0], middle_term)
minor = Proposition(middle_term, copulae[1], subject)
conclusion = Proposition(subject, copulae[2], predicate)
return Syllogism(major, minor, conclusion)
def from_terms_and_mood(subject, middle_term, predicate, mood):
assert mood.lower() in Syllogism.moods
figure, copulae = Syllogism.moods[mood.lower()]
return Syllogism.from_terms_figure_and_copulae(subject, middle_term, predicate, figure, copulae)
def from_m_and_f(mood_and_figure):
if mood_and_figure.lower() in Syllogism.moods:
figure, copulae = Syllogism.moods[mood_and_figure.lower()]
else:
assert len(mood_and_figure) == 4
if mood_and_figure[0].isnumeric():
assert mood_and_figure[1:].isalpha()
figure = mood_and_figure[0]
copulae = list(mood_and_figure[1:])
elif mood_and_figure[-1].isnumeric():
assert mood_and_figure[:-1].isalpha()
figure = mood_and_figure[-1]
copulae = list(mood_and_figure[:-1])
return Syllogism.from_terms_figure_and_copulae('S', 'M', 'P', figure, copulae)
def to_propositional_logic(self):
pass
def to_predicate_logic(self):
pass
def major_contraposition(self):
conclusion = self.conclusion.contradiction()
major = self.major.contradiction()
if major.second in self.minor.terms:
major_contraposition = Syllogism(self.minor, conclusion, major)
else:
assert major.second in conclusion.terms
major_contraposition = Syllogism(conclusion, self.minor, major)
return major_contraposition
def minor_contraposition(self):
conclusion = self.conclusion.contradiction()
#minor_contraposition
minor = self.minor.contradiction()
if minor.second in self.major.terms:
minor_contraposition = Syllogism(self.major, conclusion, minor)
else:
assert minor.second in self.conclusion.terms
minor_contraposition = Syllogism(conclusion, self.major, minor)
return minor_contraposition
def contrapositions(self):
conclusion = self.conclusion.contradiction()
#major_contraposition
major = self.major.contradiction()
if major.second in self.minor.terms:
major_contraposition = Syllogism(self.minor, conclusion, major)
else:
assert major.second in conclusion.terms
major_contraposition = Syllogism(conclusion, self.minor, major)
#minor_contraposition
minor = self.minor.contradiction()
if minor.second in self.major.terms:
minor_contraposition = Syllogism(self.major, conclusion, minor)
else:
assert minor.second in self.conclusion.terms
minor_contraposition = Syllogism(conclusion, self.major, minor)
return major_contraposition, minor_contraposition
def converse(self):
new_lines = []
#premises
for line in (self.major, self.minor):
if line.copula in {'e','i'}:
new_lines.append(Proposition(line.second, line.copula, line.first))
else:
new_lines.append(line)
#conclusion
if self.conclusion.copula in {'e', 'i'}:
#exchange the premises
new_lines = new_lines[::-1]
new_lines.append(Proposition(self.conclusion.second, self.conclusion.copula, self.conclusion.first))
else:
new_lines.append(self.conclusion)
return Syllogism(*new_lines)
def obverse(self):
if self.major.second == self.minor.second:
major = self.major.contrary()
minor = self.minor.contrary()
return Syllogism(major, minor, self.conclusion)
elif self.major.second == self.conclusion.second:
major = self.major.contrary()
conclusion = self.conclusion.contrary()
return Syllogism(major, self.minor, conclusion)
else:
return self