From JavaScript
to programming languages
to logic
to the world
The problem:
Consider a big category of T/F examples
Is today a weekday or a weekend day?
Is this a marker or a pen?
Is this course about Python or JavaScript?
How many examples like that are there?
---
If we have a true<>/b> thing, an also a false thing,
then one or the other is true
shorthand: “true or false is true”
Agree or disagree:
“false or true is true”
“false or false is false”
“true or true is true”
“true OR false is true” is dry & unintuitive
JS for it is: _______________
It’s important in
JS
Programming languages generally
Understanding the world
Boolean Arithmetic; Truth Tables
Named after George Boole
We can’t list all possible OR statements and whether they are true or not
Too many!
(How many?)
Can’t list, so let’s generalize:
A true thing or a false thing is true
What are the other 3 general statements?
… and that’s how OR is defined by Boolean arithmetic!
We can say it more briefly:
T OR F = T
etc.
A good way to say it is with a …
Truth table
A B (A OR B)
T T T
T F _
F T _
F F _
Another way to say it: JS notation
How?
Is (3>7) OR (1<2) a true fact?
JS: (3>7) || (1<2) === true
Alternatively: (3>7) || (1<2)
Math: (3>7) v (1<2) = T
Alternatively: (3>7) v (1<2)
Alternatively: (3>7) + (1<2)
Informal: (3>7) or (1<2) is true
Alternatively: (3>7) or (1<2)
Hardware diagrams: see this
See this list of alternative symbols
Let’s check out a few others
What is:
(3<7) OR (1>2)?
Generalizes as T OR F
(3<7) OR (2>1)?
Generalizes as T OR T
(3<7) OR (3<7)?
Generalizes as T OR T
(3>7) OR (3<7)?
Generalizes as F OR T
(1>2) OR (5>7)?
Generalizes as F OR F
Further Comments
“It is sunny or it is raining”
May be true or false
Depends on the weather
“or” has an intuitive meaning
We can model the meaning abstractly:
A true thing or a true thing is true
A true thing or a false thing true
A false thing or a true thing is true
A false thing or a false thing is false
This is very general
Let’s call it “powerful”
It is less intuitive but …
It gets more intuitive after you think it over
Here is something that follows from the above:
It is perhaps not intuitive
But it is a natural consequence:
We define “or” mathematically by
true or true equals true
true or false equals true
false or true equals true
false or false equals false
In JavaScript:
true || true === true
true || false === true
false || true === true
false || false === false
I’m trying to have this make some sense
When I was a student they didn’t try and I was confused
Let’s do the same analysis for AND
The problem:
Consider a big category of intuitive examples
Is today a weekday and a weekend day?
Is this a marker and a pen?
Is this course about Python and JavaScript?
How many examples like that are there?
---
If we have a true thing, also a false thing,
then one and the other is false
shorthand: “true and false is false”
Agree or disagree:
“false and true is false”
“false and false is false”
“true and true is true”
“true AND false is false” is dry & unintuitive
JS for it is: _______________
It’s important in
JS
Programming languages generally
Understanding the world
It is *not* a grand philosophical statement
Boolean Arithmetic; Truth Tables
Named after George Boole
We can’t list all possible OR statements and whether they are true or not
Too many!
(How many?)
Can’t list, so let’s generalize:
A true thing and a false thing is false
What are the other 3 general statements?
… and that’s how AND is defined by Boolean arithmetic!
We can say it more briefly:
T AND F = F
etc.
A good way to say it is with a …
Truth table
A B (A OR B) (A AND B)
T T T _
T F _ _
F T _ _
F F _ _
Another way to say it: JS notation
How?
Is (3>7) AND (1<2) a true fact?
JS: (3>7) && (1<2) === false
Alternatively: (3>7) && (1<2)
Math: (3>7) ^ (1<2) = T
Alternatively: (3>7) ^ (1<2)
Alternatively: (3>7) * (1<2)
Alternatively: (3>7) · (1<2)
See this
Informal: (3>7) and (1<2) is false
Alternatively: (3>7) and (1<2)
Hardware diagrams: see this
Let’s check out a few others
What is:
(3<7) AND (1>2)?
Generalizes as T AND F
(3<7) AND (2>1)?
Generalizes as T AND T
(3<7) AND (3<7)?
Generalizes as T AND T
(3>7) AND (3<7)?
Generalizes as F AND T
(1>2) AND (5>7)?
Generalizes as F AND F
Further Comments
“It is cloudy and it is raining”
May be true or false
Depends on the weather
“and” has an intuitive meaning
We can model the meaning abstractly:
A true thing and a true thing is true
A true thing and a false thing false
A false thing and a true thing is false
A false thing and a false thing is false
This is very general
Let’s call it “powerful”
It is less intuitive but …
It gets more intuitive after you think it over
Here is something that follows from the above:
It is perhaps not intuitive
But it is a natural consequence:
We define “and” mathematically by
true and true equals true
true and false equals false
false and true equals false
false and false equals false
In JavaScript:
true && true === true
true && false === false
false && true === false
false && false === false
Again, the point is to try to have this make some sense
(As a student, I was confused)
Optional: Let’s do it again for XOR (??!)
(getting more complicated!)
Contrapositives
Big word but interesting idea
There is a claim that:
If A then B
=
If not B then not A
Example:
If you drive 5 miles, you’ll arrive
Is that equivalent to
“If you don’t arrive, you didn’t drive 5 miles”?
We could try a few examples
…and see!
Think of an example of your own
Do you agree the contrapositive rule works?
This is not particularly about JS
Does JS accurately model the math?
It’s all about the world
I’d call it one of the deeper truths
At least it is deeper than any one programming language
3-Valued Logic Systems
Comment:
A 3-valued logic is possible
What is the 3-valued truth table for “If A then B”?
What is the 2-valued table?
People want a 2-valued table
So we can build computers in the traditional way
Rounding out the “If-Then Controversies”
It has been claimed:
If A then B
…that is…
A implies B (aka “A -> B”)
Is the same as
B OR (NOT A)
That is, two hardware diagrams mean the same
Does the claim stand up to scrutiny?
We can check using
Intuition
The key difference is that
“B or not A” is a Boolean expression
whose value, true or false, depends on
the values of A and B
“A implies B” is (intuitively)
a claim about all value pairs (A,B) that could occur
A implies B is the same as saying
For all real world occurrences of (A,B)
B or not A holds
In other words, A implies B if
Whenever A and B are found
They do not violate the truth table for (B or not A)
In other words
B or not A
Can be true or false depending on A, B
A implies B
Is about the (A,B) relation
It is true if the (A,B) relation is consistent with the truth table
It is false if (A,B) can ever violate the truth table
A implies B is a kind of entailment
Truth tables?
JavaScript?
Postscript:
I was taught that implication and (B v !A) are logically the same
There are other ways to look at it
So: no single correct answer
If you want intuitive, go to 3-value logic, 3rd value is “undetermined” or something like that. It could be implemented in computer hardware since there are 3 natural values for a logic circuit, on, off, and floating. If we do not insist on intuitive, then why define A->B as true when (not A)? Here are 3 reasons:
1) All rules in a rule set like: rule 1 - “if starter chatters then battery is not giving enough juice”; rule 2 - “if turns over but won’t start then battery and starter motor are fine” are by definition supposed to be true. Therefore we want to make sure our logic model for them says they are true even when their premises are false, as they sometimes must be. The way to do that is to define implication as true if the premise is false.
2) Scientific experimentation relies on refutation and corroboration. For the hypothesis “all trees are green” (alternatively, “if X is a tree, then X is green”) a data point consisting of a yellow sneaker corroborates a little bit (“true”) and does not refute (falsify) the hypothesis. The 2-valued implication model (non-intuitive though it might be) accurately says that “If X is a tree, then X is green” is corroborated given an X that is a yellow sneaker. Similarly for a green sneaker.
3) The definition permits statements to work even when they are like “if today is a nice day then 2=2” because our intuition is that if something is always true, it must be implied by any premise, even if the premise is false. (This argument may break down for a statement like “If 3=4 then 2=2” where different people’s intuitions, if they even have an intuition, might differ.)
4) The definition permits contrapositives to work even when they are like “if today is a nice day then 2=2, so if 2!=2 then today is not a nice day”. If that did not work, unintuitive as it may seem, then contrapositives would not be entailed. Similarly for “if 2=2 then a tree is a tree, so if a tree is not a tree then 2!=2”. The reason we care is because we would like contrapositives to represent a valid inference since it is so useful in the non-pathological cases. (Complication? You can infer anything from a contradiction.)
Some more advanced topics:
JS exponentiation, the caret (“^”), bits, XOR, …