Truth in R

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.

The basic connectives

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.

2Disjunction (OR)

How do you form a disjunction, an OR (∨), in R? Write the disjunction of P and Q, then run it.

HintR’s OR is a single vertical bar, usually above the Enter key.

3Conjunction (AND)

How do you form a conjunction, an AND (∧), in R? Write the conjunction of P and Q, then run it.

HintR’s AND is a single ampersand.

4Negation (NOT)

How do you negate a sentence, NOT (¬), in R? Negate Q, then run it.

HintR’s NOT is an exclamation mark, written in front of the sentence.

Precedence and the conditional (IF–THEN)

5Precedence predict first

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.

6Conditional (IF–THEN)

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

Hint 1P → Q is false only when P is TRUE and Q is FALSE.
Hint 2So P → Q is TRUE whenever P is FALSE, or Q is TRUE. Write that sentence with the connectives you have.

Truth tables and equivalence

7Every possible assignment predict first

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.

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

8Equivalence, and what == actually does predict first

Two expressions, every possible input. Are they logically equivalent?

If time allows

9Three more you’ll meet in filtering code

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.