Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Propositional Model Checking using Python

The document outlines a Python implementation of a propositional model-checking algorithm that verifies the truth of Boolean expressions under various truth assignments. It details the steps for identifying variables, generating truth assignments, evaluating expressions, and classifying the formula as a tautology, satisfiable, or contradiction. An example usage of the program demonstrates its functionality and outputs a truth table along with the classification result.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Propositional Model Checking using Python

The document outlines a Python implementation of a propositional model-checking algorithm that verifies the truth of Boolean expressions under various truth assignments. It details the steps for identifying variables, generating truth assignments, evaluating expressions, and classifying the formula as a tautology, satisfiable, or contradiction. An example usage of the program demonstrates its functionality and outputs a truth table along with the classification result.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Propositional Model Checking using Python

Aim

To implement a simple propositional model-checking algorithm using Python that verifies


whether a given propositional formula is true under different truth assignments.

Algorithm for Propositional Model Checking

Input:

A Boolean expression with propositional variables (e.g., "(p or q) and (not p or q)").

Steps:

1. Identify Variables:
o Extract all unique propositional variables (p, q, r, ...) from the given Boolean
expression.
2. Generate Truth Assignments:
o Create all possible combinations of True and False values for the extracted
variables.
3. Evaluate Expression:
o Substitute each truth assignment into the Boolean expression.
o Compute the truth value using Python's eval() function.
4. Output Truth Table:
o Display all possible truth assignments along with the corresponding evaluation
results.
5. Classify the Formula:
o If the formula is True for all truth assignments → Tautology.
o If the formula is True for at least one truth assignment but not all → Satisfiable
but not a Tautology.
o If the formula is False for all truth assignments → Contradiction.
6. Output the Final Result.

Program:
from itertools import product
# Function to evaluate a Boolean expression under a given truth assignment
def evaluate_expression(expression, variables, values):
env = dict(zip(variables, values)) # Map variables to their truth values
return eval(expression, {}, env) # Evaluate the expression using the assigned values

# Function to perform propositional model checking


def propositional_model_check(expression):
valid_vars = {'p', 'q', 'r'} # Define valid propositional variables
variables = sorted(set(c for c in expression if c in valid_vars)) # Extract unique variables
truth_assignments = list(product([False, True], repeat=len(variables))) # Generate truth table
assignments

results = [evaluate_expression(expression, variables, assignment) for assignment in


truth_assignments]

# Display the Truth Table


print("Truth Table:")
print(" | ".join(variables) + " | Result")
print("-" * (len(variables) * 4 + 9))
for assignment, result in zip(truth_assignments, results):
print(" | ".join(str(int(val)) for val in assignment) + f" | {int(result)}")

# Determine the classification of the Boolean formula


if all(results):
return "The formula is a Tautology (always True)."
elif any(results):
return "The formula is Satisfiable but not a Tautology."
else:
return "The formula is a Contradiction (always False)."

# Example Usage
expression = "(p or q) and (not p or q)" # Example Boolean formula
result = propositional_model_check(expression)
print("\nResult:", result)

Output

Truth Table:
p | q | Result
-------------------
0|0| 0
0|1| 1
1|0| 1
1|1| 1

Result: The formula is Satisfiable but not a Tautology.

Result:

Thus the program to implement Propositional Model checking Algorithm is implemented and

executed successfully.

You might also like