Let \(p\) be a propositional variable representing the statement “All cats are cute”, and \(q\) be a propositional variable representing the statement “All dogs are cute”.
Suppose we have a set \(P\) of computer programs that are each meant to solve the same task. Some of the programs are written in the Python programming language, and some of the programs are written in a programming language that is not Python. Some of the programs correctly solve the task, and others do not.
Let’s define the following predicates:
\[ \begin{aligned} \mathit{Python}(x): \text{$x$ is a Python program}, \qquad \text{where $x \in P$} \\ \mathit{Correct}(x): \text{$x$ solves the task correctly}, \qquad \text{where $x \in P$} \end{aligned} \]
Translate each statement below from English into predicate logic, or vice versa.
Program \(my\_prog\) is correct and is written in Python. (\(my\_prog\) is an element of \(P\))
At least one incorrect program is written in Python.
\(\forall x \in P, \mathit{Python}(x) \land \mathit{Correct}(x)\)
Now let’s practice translating these statements into Python expressions. First, our set-up:
programs
which refers to a
set of values representing computer programs. (You can imagine these are
strings, but the exact data type doesn’t matter for this exercise.)my_prog
which refers to one
of these programs (e.g., my_prog in programs
is
True
).is_python
and
is_correct
, which each take a “program” value and returns a
bool
representing whether that program “is a Python
program” and “is a correct program”, respectively.Given these Python variables and functions, write expressions to
express each of the statements from Question 2 in the previous exercise.
The first one should use the my_prog
variable, and the
other two will require using any
/all
and
comprehensions.
Program \(my\_prog\) is correct and is written in Python.
At least one incorrect program is written in Python.
\(\forall x \in P,~\mathit{Python}(x) \land \mathit{Correct}(x)\)
Using the same set and predicates as Exercise 1 Question 2, translate the following statements into predicate logic.
Every Python program is correct.
No Python program is correct.
def longest_cool_string(strings: list) -> int:
"""Return the length of the longest given string that contains the substring 'cool'.
You may ASSUME that:
- at least one string in strings contains the substring 'cool'
>>> longest_cool_string(['cool beans', 'hello', 'David is cool'])
"""
Suppose we have a Python variable strings
that
refers to a set of strings. Translate the following English statement
into a Python expression (using variable strings
and one of
any
/all
):
At least one string in
strings
contains the substring'cool'
.
Write a Python function that, given three boolean values \(p\), \(q\), and \(r\), returns the value of the propositional formula \(((p \Rightarrow q) \land r) \Leftrightarrow (p \Rightarrow (q \land r))\). We have begun the Function Design Recipe for you.
def propositional_formula(p: bool, q: bool, r: bool) -> bool:
"""Return the value of ((p ⇒ q) ∧ r) ⇔ (p ⇒ (q ∧ r)).
>>> propositional_formula(True, False, False)
True
>>> propositional_formula(False, False, False)
False
"""
Here are some additional statements that you can use to practice your translations. For each one, translate it twice: into English/symbolic logic, and then into a Python expression, using the same variables/functions we introduced earlier in this worksheet.
\(\forall x \in P,~\lnot \mathit{Python}(x) \land \mathit{Correct}(x)\)
Every incorrect program is written in Python.
\(\lnot \big(\forall x \in P,~\mathit{Correct}(x) \Rightarrow \mathit{Python}(x)\big)\)
\(\forall x \in P,~\lnot \mathit{Python}(x) \Leftrightarrow \mathit{Correct}(x)\)
So far, we have seen quantifiers only as the leftmost components of our formulas. However, because all predicate statements have truth values (i.e., are either \(\TRUE\) or \(\FALSE\)), they too can be combined using the standard propositional operators. Let’s see some examples of this.
Using the same predicates as before, translate the following statement into English.
\[\Big( \forall x \in P,~\mathit{Python}(x) \Rightarrow \mathit{Correct}(x) \Big) \lor \Big( \forall y \in P,~\mathit{Python}(y) \Rightarrow \lnot \mathit{Correct}(y) \Big)\]
Again using the same predicates as before, translate the following statement into predicate logic. “If at least one Python program is correct, then all Python programs are correct.”