<s> """<STR_LIT>""" <EOL> from __future__ import print_function , division <EOL> from sympy . logic . boolalg import And , Not , conjuncts , to_cnf <EOL> from sympy . core . compatibility import ordered <EOL> from sympy . core . sympify import sympify <EOL> def literal_symbol ( literal ) : <EOL> """<STR_LIT>""" <EOL> if literal is True or literal is False : <EOL> return literal <EOL> try : <EOL> if literal . is_Symbol : <EOL> return literal <EOL> if literal . is_Not : <EOL> return literal_symbol ( literal . args [ <NUM_LIT:0> ] ) <EOL> else : <EOL> raise ValueError <EOL> except ( AttributeError , ValueError ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def satisfiable ( expr , algorithm = "<STR_LIT>" , all_models = False ) : <EOL> """<STR_LIT>""" <EOL> expr = to_cnf ( expr ) <EOL> if algorithm == "<STR_LIT>" : <EOL> from sympy . logic . algorithms . dpll import dpll_satisfiable <EOL> return dpll_satisfiable ( expr ) <EOL> elif algorithm == "<STR_LIT>" : <EOL> from sympy . logic . algorithms . dpll2 import dpll_satisfiable <EOL> return dpll_satisfiable ( expr , all_models ) <EOL> raise NotImplementedError <EOL> def valid ( expr ) : <EOL> """<STR_LIT>""" <EOL> return not satisfiable ( Not ( expr ) ) <EOL> def pl_true ( expr , model = { } , deep = False ) : <EOL> """<STR_LIT>""" <EOL> from sympy . core . symbol import Symbol <EOL> from sympy . logic . boolalg import BooleanFunction <EOL> boolean = ( True , False ) <EOL> def _validate ( expr ) : <EOL> if isinstance ( expr , Symbol ) or expr in boolean : <EOL> return True <EOL> if not isinstance ( expr , BooleanFunction ) : <EOL> return False <EOL> return all ( _validate ( arg ) for arg in expr . args ) <EOL> if expr in boolean : <EOL> return expr <EOL> expr = sympify ( expr ) <EOL> if not _validate ( expr ) : <EOL> raise ValueError ( "<STR_LIT>" % expr ) <EOL> model = dict ( ( k , v ) for k , v in model . items ( ) if v in boolean ) <EOL> result = expr . subs ( model ) <EOL> if result in boolean : <EOL> return bool ( result ) <EOL> if deep : <EOL> model = dict ( ( k , True ) for k in result . atoms ( ) ) <EOL> if pl_true ( result , model ) : <EOL> if valid ( result ) : <EOL> return True <EOL> else : <EOL> if not satisfiable ( result ) : <EOL> return False <EOL> return None <EOL> def entails ( expr , formula_set = { } ) : <EOL> """<STR_LIT>""" <EOL> formula_set = list ( formula_set ) <EOL> formula_set . append ( Not ( expr ) ) <EOL> return not satisfiable ( And ( * formula_set ) ) <EOL> class KB ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , sentence = None ) : <EOL> self . clauses_ = set ( ) <EOL> if sentence : <EOL> self . tell ( sentence ) <EOL> def tell ( self , sentence ) : <EOL> raise NotImplementedError <EOL> def ask ( self , query ) : <EOL> raise NotImplementedError <EOL> def retract ( self , sentence ) : <EOL> raise NotImplementedError <EOL> @ property <EOL> def clauses ( self ) : <EOL> return list ( ordered ( self . clauses_ ) ) <EOL> class PropKB ( KB ) : <EOL> """<STR_LIT>""" <EOL> def tell ( self , sentence ) : <EOL> """<STR_LIT>""" <EOL> for c in conjuncts ( to_cnf ( sentence ) ) : <EOL> self . clauses_ . add ( c ) <EOL> def ask ( self , query ) : <EOL> """<STR_LIT>""" <EOL> return entails ( query , self . clauses_ ) <EOL> def retract ( self , sentence ) : <EOL> """<STR_LIT>""" <EOL> for c in conjuncts ( to_cnf ( sentence ) ) : <EOL> self . clauses_ . discard ( c ) </s>
