-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogic_test.py
More file actions
566 lines (439 loc) · 16.2 KB
/
logic_test.py
File metadata and controls
566 lines (439 loc) · 16.2 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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
import pytest
from logic import *
# Symbols -------------------------------------
def test_symbol_equality():
propositional_symbol = Symbol('prop symbol')
propositional_symbol2 = Symbol('prop symbols')
assert propositional_symbol.__eq__(propositional_symbol)
assert not propositional_symbol.__eq__(propositional_symbol2)
def test_symbol_repr():
rain = Symbol('rain')
assert repr(rain) == 'rain'
def test_symbol_class():
prop_symbol = Symbol('prop')
assert isinstance(prop_symbol, Sentence)
assert isinstance(prop_symbol, Symbol)
def test_symbol_evaluate():
rain = Symbol('rain')
# evaluate returns bool(model[self.name])
assert rain.evaluate({'rain': True, 'sunny': False}) == True
assert rain.evaluate({'rain': False, 'sunny': True}) == False
def test_symbol_formula():
rain = Symbol('rain')
# returns string representation of symbol
assert rain.formula() == 'rain'
def test_symbol_symbols():
rain = Symbol('rain')
assert isinstance(rain.symbols(), set)
assert rain.symbols() == {'rain'}
# Not Logical Connective -------------------------------------
def test_not_repr():
rain = Symbol('rain')
not_connective = Not(rain)
assert repr(not_connective) == 'Not(rain)'
def test_not_class():
rain = Symbol('rain')
not_connective = Not(rain)
assert isinstance(not_connective, Sentence)
assert isinstance(not_connective, Not)
def test_not_operand():
rain = Symbol('rain')
not_connective = Not(rain)
assert not_connective.operand == rain
def test_not_evaluate():
# TESTS Symbol FN
rain = Symbol('rain')
sunny = Symbol('sunny')
not_connective = Not(rain)
model = {'rain': True, 'sunny': False}
# calls operands evaluate fn
# operand = rain = Symbol('rain')
# evaluate(self, model) --> bool(modes[rain.name])
# inverses the logic !! evaluate(self, model)
# Not(rain) == False
assert not_connective.evaluate(model) == False
model = {'rain': True, 'sunny': False}
# calls operands evaluate fn
# operand = rain = Symbol('rain')
# operand = sunny = Symbol('sunny')
# evaluate(self, model) --> bool(modes[rain.name])
# rain = true, sun = false
# true && false
# inverses the logic !! evaluate(self, model)
not_connective_with_and = Not(And(rain, sunny))
assert not_connective_with_and.evaluate(model) == True
def test_not_formula():
rain = Symbol('rain')
not_connective = Not(rain)
# returns string repr of connective and symbol
assert not_connective.formula() == '¬rain'
def test_not_symbols():
# TESTS Symbol FN
rain = Symbol('rain')
not_connective = Not(rain)
# calls operands symbols fn
assert isinstance(not_connective.symbols(), set)
assert not_connective.symbols() == {'rain'}
# And Logical Connective -------------------------------------
# repr()
def test_and_connective_repr():
rain = Symbol('rain')
wet = Symbol('wet')
and_connective = And(rain, wet)
assert repr(and_connective) == 'And(rain, wet)'
# And class type
def test_and_connective_class():
rain = Symbol('rain')
wet = Symbol('wet')
and_connective = And(rain, wet)
assert isinstance(and_connective, Sentence)
assert isinstance(and_connective, And)
# And conjuncts()
def test_and_connective_conjuncts():
rain = Symbol('rain')
wet = Symbol('wet')
and_connective = And(rain, wet)
assert and_connective.conjuncts == [rain, wet]
# And evaluate()
def test_and_connective_evaluate():
# TESTS Symbol FN
rain = Symbol('rain')
sunny = Symbol('sunny')
and_connective = And(rain, sunny)
model = {'rain': True, 'sunny': False}
# calls evaluate on each conjunct
# ands them together
# Rain and Sun
assert and_connective.evaluate(model) == False
wet = Symbol('wet')
and_connective = And(rain, wet)
model = {'rain': True, 'wet': True}
# Rain and Wet
assert and_connective.evaluate(model) == True
and_connective = And(rain, Not(wet))
model = {'rain': True, 'wet': True}
# Rain and Not Wet
assert and_connective.evaluate(model) == False
# And formula()
def test_and_connective_formula():
rain = Symbol('rain')
wet = Symbol('wet')
and_connective = And(rain, wet)
# returns string repr of connective and symbol
assert and_connective.formula() == 'rain ∧ wet'
and_connective = And(rain, Not(wet))
assert and_connective.formula() == 'rain ∧ (¬wet)'
# And symbols()
def test_and_connective_symbols():
# TESTS Symbol FN
rain = Symbol('rain')
wet = Symbol('wet')
sunny = Symbol('sunny')
and_connective = And(rain, wet, sunny)
# calls symbols fn on each conjunctive
assert isinstance(and_connective.symbols(), set)
assert and_connective.symbols() == {'rain', 'wet', 'sunny'}
# And add()
def test_and_connective_add():
# TESTS Symbol FN
rain = Symbol('rain')
wet = Symbol('wet')
sunny = Symbol('sunny')
and_connective = And(rain, wet)
and_connective.add(sunny)
assert and_connective.symbols() == {'rain', 'wet', 'sunny'}
# Or Logical Connective -------------------------------------
# repr()
def test_or_connective_repr():
rain = Symbol('rain')
wet = Symbol('wet')
or_connective = Or(rain, wet)
assert repr(or_connective) == 'Or(rain, wet)'
or_connective = Or(rain, Not(wet))
assert repr(or_connective) == 'Or(rain, Not(wet))'
# # class type
def test_or_connective_class():
rain = Symbol('rain')
wet = Symbol('wet')
or_connective = Or(rain, wet)
assert isinstance(or_connective, Sentence)
assert isinstance(or_connective, Or)
# # disjuncts()
def test_or_connective_disjuncts():
rain = Symbol('rain')
wet = Symbol('wet')
or_connective = Or(rain, wet)
assert or_connective.disjuncts == [rain, wet]
or_connective = Or(rain, Not(wet))
assert or_connective.disjuncts == [rain, Not(wet)]
# # or evaluate()
def test_or_connective_evaluate():
# TESTS Symbol FN
rain = Symbol('rain')
sunny = Symbol('sunny')
or_connective = Or(rain, sunny)
model = {'rain': True, 'sunny': False}
# calls evaluate on each disjunct
# ands them together
# Rain or Sun
assert or_connective.evaluate(model) == True
wet = Symbol('wet')
or_connective = Or(rain, wet)
model = {'rain': False, 'wet': False}
# Rain or Wet
assert or_connective.evaluate(model) == False
wet = Symbol('wet')
or_connective = Or(rain, Not(wet))
model = {'rain': False, 'wet': False}
# Rain or Wet
assert or_connective.evaluate(model) == True
wet = Symbol('wet')
or_connective = Or(rain, Not(wet))
model = {'rain': True, 'wet': True}
# Rain or Wet
assert or_connective.evaluate(model) == True
# # or formula()
def test_or_connective_formula():
rain = Symbol('rain')
wet = Symbol('wet')
or_connective = Or(rain, wet)
# returns string repr of connective and symbol
assert or_connective.formula() == 'rain ∨ wet'
or_connective = Or(rain, Not(wet))
# returns string repr of connective and symbol
assert or_connective.formula() == 'rain ∨ (¬wet)'
# # or symbols()
def test_or_connective_symbols():
# TESTS Symbol FN
rain = Symbol('rain')
wet = Symbol('wet')
sunny = Symbol('sunny')
or_connective = Or(rain, wet, sunny)
# calls symbols fn on each conjunctive
assert isinstance(or_connective.symbols(), set)
assert or_connective.symbols() == {'rain', 'wet', 'sunny'}
or_connective = Or(rain, wet, Not(sunny))
assert or_connective.symbols() == {'rain', 'wet', 'sunny'}
# Implication Logical Connective -------------------------------------
# implication repr()
def test_implication_connective_repr():
rain = Symbol('rain')
wet = Symbol('wet')
implication_connective = Implication(rain, wet)
assert repr(implication_connective) == 'Implication(rain, wet)'
implication_connective = Implication(rain, Not(wet))
assert repr(implication_connective) == 'Implication(rain, Not(wet))'
# implication class type
def test_implication_connective_class():
rain = Symbol('rain')
wet = Symbol('wet')
implication_connective = Implication(rain, wet)
assert isinstance(implication_connective, Sentence)
assert isinstance(implication_connective, Implication)
# implication antecedent_consequent()
def test_implication_connective_antecedent_consequent():
rain = Symbol('rain')
wet = Symbol('wet')
implication_connective = Implication(rain, wet)
assert implication_connective.antecedent == rain
assert implication_connective.consequent == wet
implication_connective = Implication(rain, Not(wet))
assert implication_connective.antecedent == rain
assert implication_connective.consequent == Not(wet)
# evaluate()
def implication test_implication_connective_evaluate():
# TESTS Symbol FN
rain = Symbol('rain')
wet = Symbol('wet')
implication_connective = Implication(rain, wet)
model = {'rain': True, 'wet': True}
# Rain implies Wet ?
# step one evaluate model:
# antecedent:rain=True, consequent:wet=True
# invert and or together:
# !(antecedent:rain=True) or (wet=True)
assert implication_connective.evaluate(model) == True
# Rain implies not wet ?
model = {'rain': True, 'wet': False}
# step one evaluate model:
# antecedent:rain=True, consequent:wet=False
# invert and or together:
# !(antecedent:rain=True) or (wet=False)
assert implication_connective.evaluate(model) == False
# Not rain implies wet
# Returns true because its not raining so sentence is irrelavant
model = {'rain': False, 'wet': True}
# step one evaluate model:
# antecedent:rain=False, consequent:wet=True
# invert and or together:
# !(antecedent:rain=False) or (wet=True)
assert implication_connective.evaluate(model) == True
# implication formula()
def test_implication_connective_formula():
rain = Symbol('rain')
wet = Symbol('wet')
implication_connective = Implication(rain, wet)
# returns string repr of connective and symbol
assert implication_connective.formula() == 'rain => wet'
implication_connective = Implication(rain, Not(wet))
# returns string repr of connective and symbol
assert implication_connective.formula() == 'rain => (¬wet)'
# implication symbols()
def test_implication_connective_symbols():
# TESTS Symbol FN
rain = Symbol('rain')
wet = Symbol('wet')
sunny = Symbol('sunny')
implication_connective = Implication(rain, wet)
# calls symbols fn on each conjunctive
assert isinstance(implication_connective.symbols(), set)
assert implication_connective.symbols() == {'rain', 'wet'}
implication_connective = Implication(rain, Not(wet))
assert implication_connective.symbols() == {'rain', 'wet'}
# Implication Logical Connective -------------------------------------
# biconditional repr()
def test_biconditional_connective_repr():
rain = Symbol('rain')
wet = Symbol('wet')
biconditional_connective = Biconditional(rain, wet)
assert repr(biconditional_connective) == 'Biconditional(rain, wet)'
biconditional_connective = Biconditional(rain, Not(wet))
assert repr(biconditional_connective) == 'Biconditional(rain, Not(wet))'
# biconditional class type
def test_biconditional_connective_class():
rain = Symbol('rain')
wet = Symbol('wet')
biconditional_connective = Biconditional(rain, wet)
assert isinstance(biconditional_connective, Sentence)
assert isinstance(biconditional_connective, Biconditional)
# biconditional antecedent_consequent()
def test_biconditional_connective_antecedent_consequent():
rain = Symbol('rain')
wet = Symbol('wet')
biconditional_connective = Biconditional(rain, wet)
assert biconditional_connective.left == rain
assert biconditional_connective.right == wet
biconditional_connective = Biconditional(rain, Not(wet))
assert biconditional_connective.left == rain
assert biconditional_connective.right == Not(wet)
# biconditional evaluate()
def test_biconditional_connective_evaluate():
# TESTS Symbol FN
rain = Symbol('rain')
wet = Symbol('wet')
biconditional_connective = Biconditional(rain, wet)
model = {'rain': True, 'wet': True}
# Its only raining if its wet and its only wet if its raining
# Its not raining unless it is wet
# Its not wet unless it is wet
# step one evaluate model:
# left:rain=True, right:wet=True
# invert and or together:
# (left:rain=True) and (wet=True)
# or
# !(left:rain=True) and !(wet=True)
assert biconditional_connective.evaluate(model) == True
model = {'rain': False, 'wet': False}
# Its only raining if its wet and its only wet if its raining
# Its not raining unless it is wet
# Its not wet unless it is wet
# invert and or together:
# (left:rain=False) and (wet=False)
# or
# !(left:rain=False) and !(wet=False)
assert biconditional_connective.evaluate(model) == True
# Its only raining if its wet and its only wet if its raining
# Its not raining unless it is wet
# Its not wet unless it is wet
model = {'rain': True, 'wet': False}
# step one evaluate model:
# left:rain=True, right:wet=False
# invert and or together:
# (left:rain=True) and (wet=False)
# or
# !(left:rain=True) and !(wet=False)
assert biconditional_connective.evaluate(model) == False
# formula()
def test_biconditional_connective_formula():
rain = Symbol('rain')
wet = Symbol('wet')
biconditional_connective = Biconditional(rain, wet)
# returns string repr of connective and symbol
assert biconditional_connective.formula() == 'rain <=> wet'
biconditional_connective = Biconditional(rain, Not(wet))
# returns string repr of connective and symbol
assert biconditional_connective.formula() == 'rain <=> (¬wet)'
# symbols()
def test_biconditional_connective_symbols():
# TESTS Symbol FN
rain = Symbol('rain')
wet = Symbol('wet')
sunny = Symbol('sunny')
biconditional_connective = Biconditional(rain, And(wet, sunny))
# calls symbols fn on each conjunctive
assert isinstance(biconditional_connective.symbols(), set)
assert biconditional_connective.symbols() == {'rain', 'wet', 'sunny'}
biconditional_connective = Biconditional(rain, Not(wet))
assert biconditional_connective.symbols() == {'rain', 'wet'}
# Model Check -------------------------------------
# def test_cmodel_check():
# # rain = Symbol('rain')
# # wet = Symbol('wet')
# # knowlege = And(
# # rain,
# # wet
# # )
# # assert model_check(knowlege, rain) == True
# # knowlege = And(
# # Not(rain),
# # wet
# # )
# # assert model_check(knowlege, rain) == False
# # assert model_check(knowlege, wet) == True
# # knowlege = And(
# # Implication(rain, wet),
# # rain
# # )
# # assert model_check(knowlege, rain) == True
# # assert model_check(knowlege, wet) == True
# # one = Implication(And(knight, knave), Not(knight))
# # two = Implication(And(knight, knave), knave)
# # isKnave
# # { 'is a knave': True, 'is a knight': True } => knave
# # { 'is a knave': True, 'is a knight': False }
# # { 'is a knave': False, 'is a knight': False }
# # { 'is a knave': False, 'is a knight': True }
# # knight = Symbol('is a knight')
# # knave = Symbol('is a knave')
# # knowlege = And(
# # Implication(And(knight, knave), knave),
# # And(knight, knave)
# # )
# # model_check(knowlege, knave)
# # model_check(knowlege, knight)
# rain = Symbol('rain')
# sun = Symbol('sun')
# wet = Symbol('wet')
# knowlege = And(
# Biconditional(rain, sun),
# rain
# )
# knowlege = And(
# Implication(And(sun, rain), rain),
# # Implication(And(sun, rain), Not(sun)),
# # sun,
# # rain
# )
# # model_check(knowlege, sun)
# print(model_check(knowlege, rain))
# print(model_check(knowlege, sun))
# assert True == False
def test_cmodel_check():
knight = Symbol('is a knight')
knave = Symbol('is a knave')
knowlege = And(
Not(And(knight, knave))
)
model_check(knowlege, knave)
# model_check(knowlege, knight)
assert True == False