@@ -15,10 +15,13 @@ def safe_eval(expr: str, model: dict[str, bool]) -> bool:
1515 """Safely evaluate propositional logic expression with given model."""
1616 # Replace symbols (like P, Q) with their boolean values
1717 for sym , val in model .items ():
18- expr = re .sub (rf' \b{ sym } \b' , str (val ), expr )
18+ expr = re .sub (rf" \b{ sym } \b" , str (val ), expr )
1919 # Allow only True/False, and/or/not operators
2020 allowed = {"True" , "False" , "and" , "or" , "not" , "(" , ")" , " " }
21- if not all (token in allowed or token .isidentifier () or token in "()" for token in re .split (r'(\W+)' , expr )):
21+ if not all (
22+ token in allowed or token .isidentifier () or token in "()"
23+ for token in re .split (r"(\W+)" , expr )
24+ ):
2225 raise ValueError ("Unsafe expression detected" )
2326 return eval (expr , {"__builtins__" : {}}, {})
2427
@@ -43,7 +46,9 @@ def tt_entails(kb: list[str], query: str, symbols: list[str]) -> bool:
4346 model : dict [str , bool ] = dict (zip (symbols , values ))
4447 # Check if KB is true under this model
4548 # # If query is false in this model, KB does not entail query
46- if all (safe_eval (sentence , model ) for sentence in kb ) and not safe_eval (query , model ):
49+ if all (safe_eval (sentence , model ) for sentence in kb ) and not safe_eval (
50+ query , model
51+ ):
4752 return False
4853 return True
4954
0 commit comments