1Defining variables
Run this cell. You can swap in your own English sentences for P, Q and S: the code stays the same, because the connectives don’t care what the sentences say.
Logical connectives as code: evaluate sentences, build truth tables, and test two expressions for logical equivalence.
CS350 · Lesson 3
By the end of this workbook you should be able to write any sentence from the reading in R, and say which connectives R hands you and which ones you have to build yourself.
The names in brackets are the everyday words for each connective: disjunction is OR, conjunction is AND, negation is NOT, and the conditional is IF–THEN.
Run every cell. Where a cell asks you to predict first, write the prediction down before you run it.
Run this cell. You can swap in your own English sentences for P, Q and S: the code stays the same, because the connectives don’t care what the sentences say.
How do you form a disjunction, an OR (∨), in R? Write the disjunction of P and Q, then run it.
How do you form a conjunction, an AND (∧), in R? Write the conjunction of P and Q, then run it.
How do you negate a sentence, NOT (¬), in R? Negate Q, then run it.
Precedence is the order in which R applies the operators in an expression when there are no parentheses to say. Parentheses override it: whatever is inside them is worked out first.
Before you run anything: with P <- TRUE and Q <- FALSE, what do you expect !P | Q to be? What about !(P | Q)? Write both predictions down, then run the cell.
Both FALSE? That’s a coincidence of these values, and it hides the point.
Add Q <- TRUE as the first line of this cell and run it again: now the two answers differ.
Then run cell 1 again to put Q back to FALSE.
If you predicted the same value for both, you’ve just found a mistake that would otherwise surface later, in a sentence where it changes the answer.
Did you put Q back to FALSE? Cell 5 set Q <- TRUE. Run cell 1 again before going on, or the answers from here on will differ from your classmates’.
R gives you AND (&), OR (|) and NOT (!), but there is no operator for the conditional, the IF–THEN sentence (→). Build it. Recall when P → Q is false, and write an expression using only the connectives you have.
Watch out: P <= Q appears to work, because R turns TRUE and FALSE into 1 and 0. That’s arithmetic, not logic, and it will mislead anyone who adopts it. Use only &, | and !.
So far every cell has evaluated one row of a truth table: the row where P, Q and S happen to have the values you assigned. A truth table shows every row.
The code below is a worked example. It builds the full truth table for ¬(P ∨ Q), written !(P | Q) in R: NOT (P or Q). Run it first, and make sure you understand what each line does.
Then build your own, below the example, for the three-variable sentence ¬(P ∨ Q) ∧ S, written !(P | Q) & S. Use the example as your model: make a grid called tt3 that also includes S, add a column for the sentence, and print it. Keep the example’s lines as they are: cell 8 uses tt.
With the values from cell 1 the single-row answer is FALSE, but the table tells you why, and in which of the eight rows it is ever TRUE.
expand.grid takes a third argument for S, just like the ones for P and Q. Then build the column the same way as tt$sentence, using & for the AND.== actually does predict firstTwo expressions, every possible input. Are they logically equivalent?
Run these.
xor is exclusive or.
Everyday English “or” is ambiguous between inclusive (one, the other, or both) and exclusive (one or the other, not both).
| is always inclusive.