-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconditionals.py
More file actions
78 lines (63 loc) · 2.52 KB
/
conditionals.py
File metadata and controls
78 lines (63 loc) · 2.52 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
from statement import Statement
class Conditional:
"""
A class providing methods to evaluate conditional statements.
"""
@staticmethod
def if_then(p, q):
"""
Evaluates the conditional statement of the form: p --> q.
:param p: The hypothesis for the given statement.
:param q: The conclusion for the given statement.
:return: The boolean value of the conditional statement.
"""
if not p:
return True
elif p and q:
return True
return False
@staticmethod
def if_and_only_if(p, q):
"""
Evaluates the conditional statement of the form: p <--> q.
:param p: The hypothesis for the given statement.
:param q: The conclusion for the given statement.
:return: The boolean value of the conditional statement.
"""
if Conditional.if_then(p, q) and Conditional.if_then(q, p):
return True
return False
@staticmethod
def for_all(p, q, domain, num_vars):
"""
Iterates over a given domain, checking to see if the universal statement of the forms:
For all x in the Domain, if P(x) --> Q(x), or
For all x,y in the Domain, if P(x,y) --> Q(x,y).
:param p: The hypotheses for the given statement, P.
:param q: The conclusion for the given statement, Q.
:param domain: A collection object depicting the domain we want to test the conditional against.
:param num_vars: The number of variables to consider. Choose between 1 and 2.
:return: True if the universal conditional is true, False otherwise
"""
if num_vars == 1:
for x in domain:
p.x = x
q.x = x
s = Statement(p.expression.replace('x', str(x)))
t = Statement(q.expression.replace('x', str(x)))
if not Conditional.if_then(s, t):
return False
elif num_vars == 2:
for x in domain:
p.x = x
q.x = x
for y in domain:
p.y = y
q.y = y
s = Statement(p.expression.replace('x', str(x)))
t = Statement(q.expression.replace('x', str(x)))
s = Statement(s.expression.replace('y', str(y)))
t = Statement(t.expression.replace('y', str(y)))
if not Conditional.if_then(s, t):
return False
return True