Three short exercises on one connective: when is “one or the other, but not both” true?
Setup
The object tt is a data frame with one row for every combination of truth values for P and Q. It is built for you by this code:
This is what tt contains. Two columns, P and Q, and four rows, numbered 1 to 4:
row
P
Q
1
TRUE
TRUE
2
FALSE
TRUE
3
TRUE
FALSE
4
FALSE
FALSE
Look inside tt
Run this cell as it is, then change the numbers and conditions and run it again. No answer is checked here, and there is no NA to replace.
tt[2, ] is row 2 (rows go before the comma, columns after it).
tt$P is the whole P column, a vector of four values; tt$P[2] is its second value.
tt[tt$P & tt$Q, ] keeps only the rows where both P and Q are TRUE.
1What does xor(P, Q) give in each row?
xor is R’s exclusive or. It takes two arguments and compares them row by row, just as & and | do.
Write your prediction in the box below.
In the cell, replace NA (the first word after x1 <-) with a call to xor(). Its first argument is the P column of tt and its second is the Q column, written tt$P and tt$Q. Leave the rest of the cell as it is.
Run the cell. x1 should be four values, one per row of tt, in the same order as the table above. The last line prints them next to tt.
1–3 sentences. Write this before you run the cell.
Hint 1xor takes two arguments, like & does.Hint 2Use tt$P and tt$Q as the two arguments, separated by a comma.
2Build XOR from AND, OR and NOT
Write an expression using only &, | and ! that gives the same four values as xor. Say it in words: one of them is true, and not both.
In the cell, replace NA (the first word after mine <-) with your expression. Build it from tt$P, tt$Q and only &, | and !; do not use xor(). Leave the rest of the cell as it is.
Run the cell. mine should be four values, one per row of tt, and the last line prints them next to tt.
Hint 1“At least one is true” is OR. “Not both are true” is the negation of AND.Hint 2Join those two sentences with &.
3Where do | and xor disagree?
Compare inclusive | with xor over the whole table. Agree with your group on which row it is.
In the cell, replace NA (the first word after n_diff <-) with a calculation that counts the rows where (P | Q) and xor(P, Q) give different values. Use the columns tt$P and tt$Q. Leave the other two lines as they are.
Run the cell. n_diff should be a single number. The table above it shows both columns side by side.
Hintsum() of a TRUE/FALSE vector counts the TRUEs. The comparison a != b is TRUE where they differ.
Exclusive or (⊕) is TRUE exactly when P and Q have different truth values: P ⊕ Q = (P ∨ Q) ∧ ¬(P ∧ Q).
Inclusive | and xor differ in one row only, the row where both are TRUE. R’s | is always inclusive.
Exit question
1–2 sentences.
Assumptions and what is left out
Simplification: P and Q are atomic, two-valued sentences, with no NA.
Interpretation: xor(P, Q) is TRUE exactly when P and Q have different truth values; it is built as (P | Q) & !(P & Q).
Interpretation: the contrast is between R’s | and xor(); English “or” is not argued here.
Data: none; tt is a four-row grid from expand.grid, with rows (TRUE,TRUE), (FALSE,TRUE), (TRUE,FALSE), (FALSE,FALSE).
Left out: XOR of three or more sentences, every other connective, NA handling and formal proof.