CSE015 Lab 2 Solved

30.00 $

Category: Tags: , ,

Description

Rate this product

 

Introduction

In this assignment we make use of code written by others. For this lab you are provided with code that generates truth tables, which you can use for other exercises. To start, download the file logic.py from CatCourses and place it in your working directory. Among other things, the file defines a TruthTable object. Start a new project and import the TruthTable object from logic.py, using the following command:

from logic import TruthTable

You can now generate truth tables for any proposition. All you need to specify is the list of variables that appear in your propositions, and the propositions themselves.

Example             Generate a truth table for the proposition p∨q.

Solution We first note that there are two variables, p and q, which will be represented in Python as the list [’p’, ’q’]. We also need to represent the proposition itself, which is the Python string ’p or q’. Since the TruthTable object can generate a truth table with multiple expressions, we will provide the proposition as a list with a single element in it, [’p or q’]. We are now ready to generate the truth table:

myTable = TruthTable([’p’, ’q’], [’p or q’])

We can now display the truth table generated above: myTable.display()

The command above produces the following text:

p                  q                    p or q

———————–

0                  0                  0

  • 1 1
  • 0 1

1                  1                  1

You can also generate LATEX code for representing the table:

myTable.latex()

The above command produces the following text:

\begin{tabular}{|c|c|c|}

\hline

$p$ & $q$ & $p \lor q$ \\

\hline

0 & 0 & 0 \\

  • & 1 & 1 \\
  • & 0 & 1 \\

1 & 1 & 1 \\

\hline

\end{tabular}

The TruthTable object can also be called with multiple propositions. myTable = TruthTable([’p’, ’q’], [’p or q’, ’p and q’])

The command above generates one truth table with both propositions side by side:

p                  q                    p or q           p and q

——————————–

0                  0                  0 0
0                  1                  1 0
1                  0                  1 0
1                  1                  1 1

When using TruthTable, we need to specify all propositions and propositional variables as Python strings, as illustrated in the examples here. The TruthTable object supports the following logical connectives:

or (∨), and (∧), – (¬), -> (→), <-> (↔).

Note that when evaluating compound propositions with multiple operators TruthTable applies the precedence rules we saw in class. As always, if you are in doubt, use parentheses.

Warm Up

Write a Python program that prints out the truth tables for all logical connectives studied in class. There should be a separate truth table for the following:

a∧¬b

(a∧b) ∨¬c

Rules of Inference

Using the TruthTable object, write a program that verifies the tautologies associated with all the rules of inference we saw in class (repeated in the following for your convenience)

Rule Tautology Name
p→q p

∴q

((p→q) ∧p) →q Modus Ponens
¬q

p→q

∴¬p

(¬q∧ (p→q)) →¬p Modus Tollens
p→q q →r

∴p→r

((p→q) ∧ (q →r)) → (p→r) Hypothetical Syllogism
p∨q

¬p

∴q

((p∨q) ∧¬p) →q Disjunctive Syllogism
p

∴p∨q

p→ (p∨q) Addition
p∧q

∴p

(p∧q) →p Simplification
p

q

∴p∧q

((p) ∧ (q)) → (p∧q) Conjunction
p∨q ¬p∨r

∴q∨r

((p∨q) ∧ (¬p∨r)) → (q∨r) Resolution

 

  • Lab_02-2swsmh.zip