def __invert__(self): r""" Overload the ~ operator to not a statement. INPUT: - ``self`` -- calling object. This is the formula on the right side of the operator. OUTPUT: A boolean formula of the following form: ~``self`` EXAMPLES: This example shows how to negate a boolean formula. :: sage: import sage.logic.propcalc as propcalc sage: s = propcalc.formula("a&b") sage: ~s ~(a&b) """ exp = '~(' + self.__expression + ')' parse_tree, vars_order = logicparser.parse(exp) return BooleanFormula(exp, parse_tree, vars_order)
def add_statement(self, other, op): r""" Combine two formulas with the given operator. INPUT: - ``other`` -- instance of :class:`BooleanFormula`; this is the formula on the right of the operator - ``op`` -- a string; this is the operator used to combine the two formulas OUTPUT: The result as an instance of :class:`BooleanFormula`. EXAMPLES: This example shows how to create a new formula from two others:: sage: import sage.logic.propcalc as propcalc sage: s = propcalc.formula("a&b") sage: f = propcalc.formula("c^d") sage: s.add_statement(f, '|') (a&b)|(c^d) sage: s.add_statement(f, '->') (a&b)->(c^d) """ exp = '(' + self.__expression + ')' + op + '(' + other.__expression + ')' parse_tree, vars_order = logicparser.parse(exp) return BooleanFormula(exp, parse_tree, vars_order)
def add_statement(self, other, op): r""" This function takes two statements and combines them together with the given operator. INPUT: self -- the left hand side statement object. other -- the right hand side statement object. op -- the character of the operation to be performed. OUTPUT: Returns a new statement that is the first statement attached to the second statement by the operator op. EXAMPLES: sage: import sage.logic.propcalc as propcalc sage: s = propcalc.formula("a&b") sage: f = propcalc.formula("c^d") sage: s.add_statement(f, '|') (a&b)|(c^d) sage: s.add_statement(f, '->') (a&b)->(c^d) """ exp = '(' + self.__expression + ')' + op + '(' + other.__expression + ')' parse_tree, vars_order = logicparser.parse(exp) return BooleanFormula(exp, parse_tree, vars_order)
def convert_cnf_table(self): r""" Convert boolean formula to conjunctive normal form. INPUT: - ``self`` -- calling object OUTPUT: An instance of :class:`BooleanFormula` in conjunctive normal form EXAMPLES: This example illustrates how to convert a formula to cnf. :: sage: import sage.logic.propcalc as propcalc sage: s = propcalc.formula("a ^ b <-> c") sage: s.convert_cnf() sage: s (a|b|~c)&(a|~b|c)&(~a|b|c)&(~a|~b|~c) We now show that :meth:`convert_cnf` and :meth:`convert_cnf_table` are aliases. :: sage: t = propcalc.formula("a ^ b <-> c") sage: t.convert_cnf_table(); t (a|b|~c)&(a|~b|c)&(~a|b|c)&(~a|~b|~c) sage: t == s True .. NOTE:: This method creates the cnf parse tree by examining the logic table of the formula. Creating the table requires `O(2^n)` time where `n` is the number of variables in the formula. """ str = '' t = self.truthtable() table = t.get_table_list() vars = table[0] for row in table[1:]: if row[-1] is False: str += '(' for i in range(len(row) - 1): if row[i] is True: str += '~' str += vars[i] str += '|' str = str[:-1] + ')&' self.__expression = str[:-1] # in case of tautology if len(self.__expression) == 0: self.__expression = '(' + self.__vars_order[ 0] + '|~' + self.__vars_order[0] + ')' self.__tree, self.__vars_order = logicparser.parse(self.__expression)
def convert_cnf_table(self): r""" Convert boolean formula to conjunctive normal form. INPUT: - ``self`` -- calling object OUTPUT: An instance of :class:`BooleanFormula` in conjunctive normal form EXAMPLES:: This example illustrates how to convert a formula to cnf. :: sage: import sage.logic.propcalc as propcalc sage: s = propcalc.formula("a ^ b <-> c") sage: s.convert_cnf() sage: s (a|b|~c)&(a|~b|c)&(~a|b|c)&(~a|~b|~c) We now show that :meth:`convert_cnf` and :meth:`convert_cnf_table` are aliases. :: sage: t = propcalc.formula("a ^ b <-> c") sage: t.convert_cnf_table(); t (a|b|~c)&(a|~b|c)&(~a|b|c)&(~a|~b|~c) sage: t == s True .. NOTE:: This method creates the cnf parse tree by examining the logic table of the formula. Creating the table requires `O(2^n)` time where `n` is the number of variables in the formula. """ str = '' t = self.truthtable() table = t.get_table_list() vars = table[0] for row in table[1:]: if row[-1] == False: str += '(' for i in range(len(row) - 1): if row[i] == True: str += '~' str += vars[i] str += '|' str = str[:-1] + ')&' self.__expression = str[:-1] # in case of tautology if len(self.__expression) == 0: self.__expression = '(' + self.__vars_order[0] + '|~' + self.__vars_order[0] + ')' self.__tree, self.__vars_order = logicparser.parse(self.__expression)
def convert_cnf_table(self): r""" This function converts an instance of boolformula to conjunctive normal form. It does this by examining the truthtable of the formula, and thus takes `O(2^n)` time, where `n` is the number of variables. INPUT: - ``self`` -- the calling object. OUTPUT: An instance of :class:`BooleanFormula` with an identical truth table that is in conjunctive normal form. EXAMPLES:: sage: import sage.logic.propcalc as propcalc sage: s = propcalc.formula("a ^ b <-> c") sage: s.convert_cnf() sage: s (a|b|~c)&(a|~b|c)&(~a|b|c)&(~a|~b|~c) The methods :meth:`convert_cnf` and :meth:`convert_cnf_table` are aliases. :: sage: t = propcalc.formula("a ^ b <-> c") sage: t.convert_cnf_table(); t (a|b|~c)&(a|~b|c)&(~a|b|c)&(~a|~b|~c) sage: t == s True .. NOTE:: This method creates the cnf parse tree by examining the logic table of the formula. Creating the table requires `O(2^n)` time where `n` is the number of variables in the formula. """ str = '' t = self.truthtable() table = t.get_table_list() vars = table[0] for row in table[1:]: if(row[-1] == False): str += '(' for i in range(len(row) - 1): if(row[i] == True): str += '~' str += vars[i] str += '|' str = str[:-1] + ')&' self.__expression = str[:-1] #in case of tautology if(len(self.__expression) == 0): self.__expression = '(' + self.__vars_order[0] + '|~' + self.__vars_order[0] + ')' self.__tree, self.__vars_order = logicparser.parse(self.__expression)
def convert_cnf_table(self): r""" This function converts an instance of boolformula to conjunctive normal form. It does this by examining the truthtable of the formula, and thus takes O(2^n) time, where n is the number of variables. INPUT: self -- the calling object. OUTPUT: An instance of boolformula with an identical truth table that is in conjunctive normal form. EXAMPLES: sage: import sage.logic.propcalc as propcalc sage: s = propcalc.formula("a ^ b <-> c") sage: s.convert_cnf() sage: s (a|b|~c)&(a|~b|c)&(~a|b|c)&(~a|~b|~c) Ensure that convert_cnf() and convert_cnf_table() produce the same result. sage: t = propcalc.formula("a ^ b <-> c") sage: t.convert_cnf_table(); t (a|b|~c)&(a|~b|c)&(~a|b|c)&(~a|~b|~c) sage: t == s True NOTES: This method creates the cnf parse tree by examining the logic table of the formula. Creating the table requires O(2^n) time where n is the number of variables in the formula. """ str = '' t = self.truthtable() table = t.get_table_list() vars = table[0] for row in table[1:]: if(row[-1] == False): str += '(' for i in range(len(row) - 1): if(row[i] == True): str += '~' str += vars[i] str += '|' str = str[:-1] + ')&' self.__expression = str[:-1] #in case of tautology if(len(self.__expression) == 0): self.__expression = '(' + self.__vars_order[0] + '|~' + self.__vars_order[0] + ')' self.__tree, self.__vars_order = logicparser.parse(self.__expression)
def formula(s): r""" Return an instance of :class:`BooleanFormula` INPUT: - ``s`` -- a string that contains a logical expression. OUTPUT: An instance of :class:`BooleanFormula` EXAMPLES: This example illustrates ways to create a boolean formula. :: sage: import sage.logic.propcalc as propcalc sage: f = propcalc.formula("a&~b|c") sage: g = propcalc.formula("a^c<->b") sage: f&g|f ((a&~b|c)&(a^c<->b))|(a&~b|c) We now demonstrate some possible errors. :: sage: propcalc.formula("((a&b)") Traceback (most recent call last): ... SyntaxError: malformed statement sage: propcalc.formula("_a&b") Traceback (most recent call last): ... NameError: invalid variable name _a: identifiers must begin with a letter and contain only alphanumerics and underscores """ try: parse_tree, vars_order = logicparser.parse(s) f = boolformula.BooleanFormula(s, parse_tree, vars_order) f.truthtable(0, 1) except (KeyError, RuntimeError, IndexError, SyntaxError): msg = "malformed statement" raise SyntaxError(msg) return f
def formula(s): r""" Returns an instance of :class:`BooleanFormula <sage.logic.boolformula.BooleanFormula>` if possible, and throws a syntax error if not. INPUT: - ``s`` -- a string that contains a logical expression. OUTPUT: - An instance of :class:`BooleanFormula <sage.logic.boolformula.BooleanFormula>` representing the logical expression ``s``. EXAMPLES:: sage: import sage.logic.propcalc as propcalc sage: f = propcalc.formula("a&~b|c") sage: g = propcalc.formula("a^c<->b") sage: f&g|f ((a&~b|c)&(a^c<->b))|(a&~b|c) TESTS: There are a number of possible errors:: sage: propcalc.formula("((a&b)") Traceback (most recent call last): ... SyntaxError: malformed statement sage: propcalc.formula("_a&b") Traceback (most recent call last): ... NameError: invalid variable name _a: identifiers must begin with a letter and contain only alphanumerics and underscores """ try: parse_tree, vars_order = logicparser.parse(s) f = boolformula.BooleanFormula(s, parse_tree, vars_order) f.truthtable(0, 1) except (KeyError, RuntimeError, IndexError, SyntaxError): msg = "malformed statement" raise SyntaxError(msg) return f
def __invert__(self): r""" Overload the ``~`` operator to 'not' a statement. OUTPUT: A boolean formula of the form ``~self``. EXAMPLES: This example shows how to negate a boolean formula:: sage: import sage.logic.propcalc as propcalc sage: s = propcalc.formula("a&b") sage: ~s ~(a&b) """ exp = '~(' + self.__expression + ')' parse_tree, vars_order = logicparser.parse(exp) return BooleanFormula(exp, parse_tree, vars_order)
def __invert__(self): r""" Overloads the ~ operator to not a statement. INPUT: self -- the right hand side statement. other -- the left hand side statement. OUTPUT: Returns a new statement that is the first statement logically not'ed. EXAMPLES: sage: import sage.logic.propcalc as propcalc sage: s = propcalc.formula("a&b") sage: ~s ~(a&b) """ exp = '~(' + self.__expression + ')' parse_tree, vars_order = logicparser.parse(exp) return BooleanFormula(exp, parse_tree, vars_order)