This page does not represent the most current semester of this course; it is present merely as an archive.
In any expression system with more than one operator, there is a need to clarify the meaning of a multi-operator system. For example, is equal to ( plus ) or ( minus )?
In arithmetic, an order of operations is sufficient to clarify meaning. However, as more complicated structures appear it no longer suffices. For example, in the code
PythonJava | |
---|---|
for(int x : numbers) { sum += x; } |
for x in numbers: sum += x |
which operator happens first: +=
or for
? The answer is neither: they work together iteratively.
A versatile tool for understanding expressions, even complicated ones with components like loops, is the notion of a main operator. Any expression with an operator in it can be expressed as a single main operator with smaller expressions as its operands.
Every operator has some scope: a portion of the expression that it directly modifies. Logic has operators with three different ways of determining scope.
These have two operands, which comprise the operators scope. Examples include , , , , etc.
Logic does not define an order of operations for binary operators: is invalid notation; you must write either or . The only time parentheses can be omitted is when the operators are associative1, as for example in .
Programming languages have parallels to many logical operators and do have operator precedence for them, so sometimes when logic is written by computer scientists they omit a few required parentheses and assume programming precedence rules apply. We won’t use that kind of omission in this class.
The scope of a q quantifier is the entire expression that follows its terminating dot, though its scope cannot escape from parentheses. Thus, means not .
There are actually two competing notations for quantifiers. MCS and most other computing texts use a dot after the quantifier and the from here until the end
scope, as described above. ∀x and some other formal logic texts write the quantifier like a function instead, following it with parentheses that define its scope.
We only use the dot-notation quantifiers in this class.
The main operator of any expression is the operator whose scope is the entire expression.
When turning logic into English, the following process will always work:
Logic | Example English template |
---|---|
it is not the case that | |
both and | |
either or or both | |
either or but not both | |
if then | |
if and only if | |
for each it is the case that | |
there exists some such that | |
there is no such that |
Convert the following to simple clear English
Predicate | Meaning |
---|---|
is a program | |
is an input to program | |
program solves input as part of working on input |
The main operator is ,
For each it is the case that …
The remaining logic is
The main operator is
For each it is the case that if … then …
The antecedent is , which is just is a program
For each it is the case that if is a program then …
We can simplify that English:
For each program it is the case that …
The consequent is
The main operator is ,
For each program it is the case that there exists some such that …
We can simplify that English:
For each program there exists some such that …
The remaining logic is
The main operator is ,
For each program there exists some such that both … and …
The first conjunct is , which is just is an input to
For each program there exists some such that both is an input to and …
We can simplify that English:
For each program has some input such that …
The second conjunct is
The main operator is ,
For each program has some input such that for each it is the case that …
The remaining logic is
The main operator is
For each program has some input such that for each it is the case that if … then …
The antecedent is , which is just is an input to
;
For each program has some input such that for each it is the case that if is an input to then …
We can simplify that English:
For each program has some input such that for each input it is the case that …
The consequent is , which is program solves input as part of working on input
;
For each program has some input such that for each input it is the case that program solves input as part of working on input .
We can simplify that English:
For each program has some input such that for each input program solves input as part of working on input .
We have a full English sentence: For each program has some input such that for each input program solves input as part of working on input .
But we’d rather not use variable in English.
The variable is the only program, so we can replace it with the program
or the like: Every program has some input such that for every input , the program solves as part of working on .
Since and are both inputs, the simple the input
approach will not work, meaning we need more creativity in removing them from the phrase. A few examples:
baseinput: an input whose solution is part of every other inputs’ solution.
When turning English into logic, the following process will always work:
When doing this, be sure to look for implicit quantifiers. is often omitted by making a general statement
instead; may be as subtle as the use of a
instead of the
.
Also be careful about adding and if-then statements. I’ll marry someone who proposes
means . I’ll marry and proposes
not . I’ll marry if proposes
; the latter is trivially true as long as there is someone somewhere who will never propose.
Identify the main operator of each of the following and re-write the sentence to use that operator as a symbol.
Convert the best program is never the cheapest program
to logic
This is a statement about all programs.
if ’s the cheapest, then is not the best
This is an if-then statement.
(’s the cheapest) ( is not the best)
The antecedent is is the cheapest program
.
This is about the absence of something (nothing is cheaper than ).
is cheaper than
This is about cost. Which is not part of logic. So we turn it into a predicate:
Predicate | Meaning |
---|---|
is cheaper than |
.
The consequent is is not the best program
.
This is about the existence of something (something is better than ).
is better than
This is about goodness. Which is not part of logic. So we turn it into a predicate:
Predicate | Meaning |
---|---|
is better than |
.
Putting it all together:
Predicate | Meaning |
---|---|
is cheaper than | |
is better than |
Convert the best program is never the cheapest program
to logic
This is a statement about all programs.
’s the best
and is not the cheapest
are never both true
This is an not-and statement.
’s the best is the cheapest
The first conjunct is is the best program
.
This is about the absence of something (nothing is better than ).
is better than
This is about goodness. Which is not part of logic. So we turn it into a predicate:
Predicate | Meaning |
---|---|
is better than |
.
The second conjunct is is cheapest program
.
This is about the absence of something (nothing is cheaper than ).
is cheaper than
This is about cost. Which is not part of logic. So we turn it into a predicate:
Predicate | Meaning |
---|---|
is cheaper than |
.
Putting it all together:
Predicate | Meaning |
---|---|
is cheaper than | |
is better than |
We could optionally apply De Morgan and double negation:
to get every program either has another better than it or another cheaper than it
, which is also equivalent to our starting statement.
Some associative operator sequences are non-intuitive; for example, and are equivalent and hence can be written as . You don’t need to knwo this, though, as it is always permitted to include parentheses even with associative operators.↩︎
first ↩︎
(it works) (I’ll buy it)↩︎
. (I’ll buy if works)↩︎
. (I’ll buy and works)↩︎
. (I’ll buy and works)↩︎
. (if passes all tests then is ready for release↩︎
(a program passes all tests) (you are missing some tests)↩︎
. (if is good then passes at least one test)↩︎
. (every good program passes )↩︎
(You’ll need an IDE to solve this) (You’ll need a debugger to solve this)↩︎
. (if is a floating-point value then is either normalized, denormalized, infinite, or NaN)↩︎
(this variable is normalized) (this variable is denormalized) (this variable is infinite) (this variable is NaN)↩︎
. (if is a floating-point value then is a number)↩︎
. ( is a floating-point value and is missing)↩︎
(using a float-point value) (a mistake)↩︎