Commits

tomo cocoa  committed c11d6de

implemented Boolean Algebra

  • Participants
  • Parent commits e6f17fd

Comments (0)

Files changed (2)

File logics/prop.py

     return prop._eval(variables)
 
 
+def equivalent(prop1, prop2):
+    return canonicalize_DNF(prop1) == canonicalize_DNF(prop2)
+
+
 def remove_if_then_iff(prop):
     if type(prop) == Constant:
         return prop
     def __invert__(self):
         return Not(self)
 
+    def __xor__(self, other):
+        assert isinstance(other, Prop)
+        return Xor(self, other)
+
 
 class Constant(Prop):
     """2-singletons"""
         self.varnames = left.varnames + right.varnames
 
     def _eval(self, variables):
-        return not eval(self.left) or eval(self.right)
+        return not eval(self.left, variables) or eval(self.right, variables)
 
     def __str__(self):
         return '({0}) => ({1})'.format(str(self.left), str(self.right))
         self.varnames = left.varnames + right.varnames
 
     def _eval(self, variables):
-        return eval(self.left) == eval(self.right)
+        return eval(self.left, variables) == eval(self.right, variables)
 
     def __str__(self):
         return '({0}) <=> ({1})'.format(str(self.left), str(self.right))
         return False
 
 
+class Xor(Prop):
+
+    def __init__(self, left, right):
+        self.left = left
+        self.right = right
+        self.varnames = left.varnames + right.varnames
+
+    def _eval(self, variables):
+        return eval(self.left, variables) ^ eval(self.right, variables)
+
+    def __str__(self):
+        return '({0}) (+) ({1})'.format(str(self.left), str(self.right))
+
+    def __repr__(self):
+        return '<logics.prop.Xor expr: {0}>'.format(str(self))
+
+    def __eq__(self):
+        if type(self) == type(other):
+            return self.left == other.left and self.right == other.right
+        return False
+
+
 class UndefinedVariableError(Exception):
 
     def __init__(self, variable):
 from six import with_metaclass
 from func import gcd
 import group
+import logics.prop as lp
 
 class Ring(group.Group):
 
 
 class BooleanAlgebra(CommutativeRing):
 
-    def __init__(self, value=set()):
+    def __init__(self, value=lp.F):
         self.value = value
 
     def __str__(self):
 
     def __eq__(self, other):
         if type(self) == type(other):
-            return self.value == other.value
+            return lp.equivalent(self.value, other.value)
         else:
             return False
 
     def __add__(self, other):
         if type(self) == type(other):
-            return self.__class__(self.value ^ other.value)
+            return BooleanAlgebra(self.value ^ other.value)
         else:
             return other.__radd__(self)
 
-    def __radd__(self, other):
-        pass
+    def __mul__(self, other):
+        if type(self) == type(other):
+            retrun BooleanAlgebra(self.value & other.value)
+        else:
+            return other.__radd__(self)
 
     def __neg__(self):
-        pass
+        return BooleanAlgebra(lp.Not(self))