Untitled

""" Sage has many built-in functions, boolean variables, the boolean operators and, or, not and a facility to add more functions. In this Notebook we shall demonstrate some simple logic operations in Sage """ 
       
#booleans a = true; b = false; a and b; a or b; not a and not b; not (a and b); 
       
False
True
False
True
#building your own logic operator def xor(a,b): if (a==b): answer = false else: answer = true return answer 
       
xor(a,b) 
       
True
xor(a, not b) 
       
False
def implies(a ,b): if (a == true) and (b == false): answer = false else: answer = true return answer 
       
implies(a,b) 
       
False
implies(true, false) 
       
False
""" A demonstration of solving a logical puzzle using sage. This problem is taken from K. Rosen's book. Five friends have access to a chat room. Can you determine who is chatting if the following information is known? 1. Either Kevin or Heather or both are chating. 2. Either Randy or Vijay are chatting but not both. 3. If Abby is chatting then so is Randy. 4. Vijay and Kevin are either both are chatting or neither. 5. If Heather is chatting then so are Abby and Kevin To solve the problem we define five propositions name is chatting """ 
       
A = true; H = true; K = true; V = true; R = true 
       
""" The five statements are represented by the followin boolean expression: (K or H) and (xor(R,V)) and (implies (A, R)) and (not xor(K, V)) and (implies(H, (A and K)) Now we wish to check for which truth assignments this boolean expression is true. """ 
       
for A in (true, false): for H in (true, false): for K in (true, false): for V in (true, false): for R in (true, false): for A in (true, false): if (K or H) and xor(R,V) and (implies (A, R)) and (not (xor(K, V))) and (implies(H, (A and K))) ==true: print "Abbey :", A, "Heather: ", H, "Vijay= ", V, "Randy = ", R, "Kevin = ", K 
       
Abbey : False Heather:  False Vijay=  True Randy =  False Kevin = 
True
Abbey : False Heather:  False Vijay=  True Randy =  False Kevin = 
True