\( \newcommand{\NOT}{\neg} \newcommand{\AND}{\wedge} \newcommand{\OR}{\vee} \newcommand{\XOR}{\oplus} \newcommand{\IMP}{\Rightarrow} \newcommand{\IFF}{\Leftrightarrow} \newcommand{\TRUE}{\text{True}\xspace} \newcommand{\FALSE}{\text{False}\xspace} \newcommand{\IN}{\,{\in}\,} \newcommand{\NOTIN}{\,{\notin}\,} \newcommand{\TO}{\rightarrow} \newcommand{\DIV}{\mid} \newcommand{\NDIV}{\nmid} \newcommand{\MOD}[1]{\pmod{#1}} \newcommand{\MODS}[1]{\ (\text{mod}\ #1)} \newcommand{\N}{\mathbb N} \newcommand{\Z}{\mathbb Z} \newcommand{\Q}{\mathbb Q} \newcommand{\R}{\mathbb R} \newcommand{\C}{\mathbb C} \newcommand{\cA}{\mathcal A} \newcommand{\cB}{\mathcal B} \newcommand{\cC}{\mathcal C} \newcommand{\cD}{\mathcal D} \newcommand{\cE}{\mathcal E} \newcommand{\cF}{\mathcal F} \newcommand{\cG}{\mathcal G} \newcommand{\cH}{\mathcal H} \newcommand{\cI}{\mathcal I} \newcommand{\cJ}{\mathcal J} \newcommand{\cL}{\mathcal L} \newcommand{\cK}{\mathcal K} \newcommand{\cN}{\mathcal N} \newcommand{\cO}{\mathcal O} \newcommand{\cP}{\mathcal P} \newcommand{\cQ}{\mathcal Q} \newcommand{\cS}{\mathcal S} \newcommand{\cT}{\mathcal T} \newcommand{\cV}{\mathcal V} \newcommand{\cW}{\mathcal W} \newcommand{\cZ}{\mathcal Z} \newcommand{\emp}{\emptyset} \newcommand{\bs}{\backslash} \newcommand{\floor}[1]{\left \lfloor #1 \right \rfloor} \newcommand{\bigfloor}[1]{\Big \lfloor #1 \Big \rfloor} \newcommand{\ceil}[1]{\left \lceil #1 \right \rceil} \newcommand{\bigceil}[1]{\Big \lceil #1 \Big \rceil} \newcommand{\abs}[1]{\left | #1 \right |} \newcommand{\bigabs}[1]{\Big | #1 \Big |} \newcommand{\xspace}{} \newcommand{\proofheader}[1]{\underline{\textbf{#1}}} \)

CSC110 Lecture 3A: Introduction to Formal Logic

Exercise 1: Propositional and Predicate Logic

  1. 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”.

    1. Translate the statement \(p \Rightarrow q\) into English.
    1. What is the hypothesis of this implication?
    1. What is the conclusion of this implication?
    1. Write down an English translation of the converse of this implication.
    1. Write down an English translation of the contrapositive of this implication.
  2. 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.

    1. Program \(my\_prog\) is correct and is written in Python. (\(my\_prog\) is an element of \(P\))

    2. At least one incorrect program is written in Python.

    3. \(\forall x \in P, \mathit{Python}(x) \land \mathit{Correct}(x)\)

Exercise 2: Translating logical statements into Python

  1. Now let’s practice translating these statements into Python expressions. First, our set-up:

    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.

    1. Program \(my\_prog\) is correct and is written in Python.

    2. At least one incorrect program is written in Python.

    3. \(\forall x \in P,~\mathit{Python}(x) \land \mathit{Correct}(x)\)

Exercise 3: Filtering collections

  1. Using the same set and predicates as Exercise 1 Question 2, translate the following statements into predicate logic.

    1. Every Python program is correct.

    2. No Python program is correct.

    1. Given the following function: (1) complete its doctest example, and (2) implement it using a filtering comprehension.
    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'])
    
    
        """
    
    
    
    1. 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'.

Additional exercises

  1. 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
         """
  2. 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.

    1. \(\forall x \in P,~\lnot \mathit{Python}(x) \land \mathit{Correct}(x)\)

    2. Every incorrect program is written in Python.

    3. \(\lnot \big(\forall x \in P,~\mathit{Correct}(x) \Rightarrow \mathit{Python}(x)\big)\)

    4. \(\forall x \in P,~\lnot \mathit{Python}(x) \Leftrightarrow \mathit{Correct}(x)\)

  3. 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.

    1. 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)\]

    2. 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.”