-
-
Notifications
You must be signed in to change notification settings - Fork 47.4k
Added tt entails algorithm #12902
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Added tt entails algorithm #12902
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
969b313
Add TT-ENTAILS algorithm (propositional logic) with examples
mahidhiman12 518f003
Rename file to lowercase without dash to follow repo conventions
mahidhiman12 62d616f
Add TT-ENTAILS algorithm with examples and references
mahidhiman12 fe06a03
Fix ruff/pre-commit issues: docstrings, typing, imports, nested if, s…
mahidhiman12 91e6fe6
add TT-entails algorithm
mahidhiman12 e06f29f
add TT-entails algorithm
mahidhiman12 67e9df9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
""" | ||
TT-ENTAILS Algorithm (Propositional Logic) | ||
Reference: [Russell & Norvig, Artificial Intelligence: A Modern Approach, Ch. 7](https://aima.cs.berkeley.edu/) | ||
Wikipedia: [Entailment](https://en.wikipedia.org/wiki/Entailment) | ||
|
||
This algorithm checks if a knowledge base (KB) entails a query sentence (a) | ||
using truth tables. Returns True if KB entails a, False otherwise. | ||
""" | ||
|
||
import itertools | ||
import re | ||
|
||
|
||
def safe_eval(expr: str, model: dict[str, bool]) -> bool: | ||
"""Safely evaluate propositional logic expression with given model.""" | ||
# Replace symbols (like P, Q) with their boolean values | ||
for sym, val in model.items(): | ||
expr = re.sub(rf"\b{sym}\b", str(val), expr) | ||
# Allow only True/False, and/or/not operators | ||
allowed = {"True", "False", "and", "or", "not", "(", ")", " "} | ||
if not all( | ||
token in allowed or token.isidentifier() or token in "()" | ||
for token in re.split(r"(\W+)", expr) | ||
): | ||
raise ValueError("Unsafe expression detected") | ||
return eval(expr, {"__builtins__": {}}, {}) | ||
|
||
|
||
def tt_entails(kb: list[str], query: str, symbols: list[str]) -> bool: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file |
||
""" | ||
Check if the knowledge base entails the query using truth tables. | ||
|
||
Args: | ||
kb (List[str]): List of propositional sentences in KB as strings | ||
query (str): Query sentence to test entailment | ||
symbols (List[str]): List of all propositional symbols used | ||
|
||
Returns: | ||
bool: True if KB entails query, False otherwise | ||
|
||
Example: | ||
tt_entails(["P or Q"], "Q", ["P","Q"]) | ||
|
||
""" | ||
for values in itertools.product([True, False], repeat=len(symbols)): | ||
model: dict[str, bool] = dict(zip(symbols, values)) | ||
# Check if KB is true under this model | ||
# # If query is false in this model, KB does not entail query | ||
if all(safe_eval(sentence, model) for sentence in kb) and not safe_eval( | ||
query, model | ||
): | ||
return False | ||
return True | ||
|
||
|
||
# Example usage | ||
if __name__ == "__main__": | ||
# Example 1: KB entails query → should return True | ||
symbols = ["P", "Q"] | ||
kb = ["P or Q", "not P or Q"] # KB says P or Q is True, and not P or Q is True | ||
query = "Q" # Query: Is Q True? | ||
print("Does KB entail query? : ", tt_entails(kb, query, symbols)) | ||
|
||
# Example 2: KB does NOT entail query → should return False | ||
symbols2 = ["P", "Q"] | ||
kb2 = ["P"] # KB says only P is True | ||
query2 = "Q" # Query asks if Q is True | ||
print("Does KB2 entail query2? : ", tt_entails(kb2, query2, symbols2)) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file
machine_learning/ttentails.py
, please provide doctest for the functionsafe_eval