Send
Close Add comments:
(status displays here)
Got it! This site "robinsnyder.com" uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website. Note: This appears on each machine/browser from which this site is accessed.
Assignment statement and swaps
1. Assignment statement and swaps
2. Assignment statement and swaps
1. Assignment statement
2. Evaluate expression to a value
3. Replace variable with value
The
assignment statement, with the
destructive update as a
side-effect of
execution, causes many problems in programming.
3. Assignment statement
v = e;
Command execution of an assignment statement is a two step process.
1. Evaluate the expression e on the RHS (Right Hand Side) to a value.
2. Replace the previous value of variable v on the LHS (Left Hand Side) with the new value.
Whatever value was in variable
v before is lost (forever).
4. Step 1
1. Evaluate the expression e on the RHS to a value.
5. Step 2
2. Replace the previous value of variable v on the LHS with the new value.
Whatever value was in variable
v before is lost (forever).
6. Swapping values
Will the following code fragment correctly swap the values that are in integer variables
x and
y? Why or why not?
x = y;
y = x;
Explain your reasoning.
7. Explanation
Did you think like a machine to reason about your answer?
x = y;
y = x;
Thinking like a machine is thinking operationally.
How would you explain your reasoning to a student learning programming?
8. Testing
How could you test your program to make sure it is correct?
x = y;
y = x;
Would a flow chart help?
It is only
two assignment statements. How hard could that be?
9. Testing
Fallacy:
One can test a program to insure it is correct.
Let us adapt Dijkstra's argument from 1972 to this problem.
For two
64 bit integers, there are
2128 possible ways to do the swap code.
There are only about
280 microseconds in
16 billion years.
So it would take about
250,000,000,000,000(about
250 trillion) computers about
16 billion years to go through all the ways of swapping two
64 bit values.
Testing (brute-force) only helps find errors! It does not show that program code is correct.
10. Computer bugs
As I have now said many times and written in many places: program testing can be quite effective for showing the presence of bugs, but is hopelessly inadequate for showing their absence. Edsger Dijkstra (computer scientist)
Dijkstra, E. (1976).
A discipline of programming. Englewood Cliffs, NJ: Prentice-Hall., 20.
11. Quantum computing in brief
Quantum computing:
much faster than conventional computers
best for problems that allow probabilistic solutions
cannot solve all problems - despite the hype
[exponential speedup not clearly defined, like entanglement]qc-11
12. Quantum computing analogies
Quantum computing analogies: pick the best way
walk on foot: pencil and paper
drive by car : conventional computer (go most anywhere)
fly by jet : quantum computers (does not go anywhere, sometimes impractical)
no way to get to Mars, nearest star, etc.
13. Correctness
Let us show/use
program verification techniques to determine correctness.
The precondition and postcondition are as follows.
// { pre: (x = a) and (y = b) }
... code to swap values ...
// { post: (x = b) and (y = a) }
14. Apply the rules
Apply the
axiomatic semantics proof rule by working backwards in a top-down backward-chaining manner.
// { pre: 5: (x = a) and (y = b) }
// Meet in the middle (are there contradictions at this point?)
//{ 4: y = b = a = x }
// { 3: (x = a) and (y = b) and (y = a) }
// { 2: (y = b) and (y = a) }
x = y;
// { 1: (x = b) and (x = a) }
y = x;
// { post: 0: (x = b) and (y = a) }
Algebra is critical to proving program fragments correct. Is knowing algebra important to learning programming?
15. Work backwards
Many students have trouble working backwards, using algebra, and realizing when the proof is done.
1. Substitute
2. Simplify
Stair analogy:
Top-down: work backward from top step (last statement): 5 if 4 if 3 if 2 if 1
Bottom-up: work forward from bottom step (first statement): 1 then 2 then 3 then 4 then 5
16. When is it done?
Many students tend to be very used to thinking like a machine and not algebraically using symbol manipulation.
They may spin their wheels confused. It is like climbing the Penrose steps.
Aside: When is the program (or software) done? Think laser eye surgery.
17. Penrose steps: how it is done
1. Penrose steps 1
2. Penrose steps 2
3. Penrose steps 3
4. Penrose steps 4
5. Penrose steps 5
6. Penrose steps 6
The Penrose never-ending steps illusion, also called the stairs illusion, cannot exist in reality as depicted.
How is the Penrose steps illusion created?
Interpretation: The statements
x = y;
y = x;
will correctly swap the values in
x and
y if and only if x and
y have the same initial values.
This result follows without thinking operationally (like a machine). We only have to apply the rules and interpret the results.
18. Algebra
From the axiomatic rules used to prove program code correct, one should see that algebra is an integral part of that process.
Thus, the ability to algebra, as in substitute and simplify, is an important and implicit part of the programming process.
19. Programming
Much of programming reduces to learning
code invariant transformation rules and
abstraction rules and applying them in a
iterative refinement process - often without thinking about what they mean.
That is the purpose of
algebra. That is, to transform mathematical expressions
without thinking about what they mean (in an operational sense).
1. Substitute
2. Simplify
20. Miller's Law
Remember Miller's Law that one can only keep about up to 7 things in memory at once.
Programmers who rigidly enforce this force you to go through many screens, each of which has only a few choices. One can quickly lose track of where things are in the interface.
21. Viewpoint
Viewpoints:
Most users: How can I get the software to do this.
Some programmers: How can I make the software do this.
22. Changing code
Moving code around using invariant transformations is to a programmer who has memorized that code and thinks like a computer, like moving things around in a house where a cat lives. The cat's memory map of where everything is located needs to get relearned.
23. Problem types
1. Think like a computer:
Here is some code using loop, if, etc. What is the output?
2. Thinking like a computer not required:
Here are four code fragments. Which one can compute a different output?
24. Problem comparison
Think like a computer:
Yes: What is the output of each code fragment?
No: Which code fragment can compute a different output?
Assume all variables are integers and declared (omitted).
|
i = 0;
while (i != 8) {
i += 1;
print(i);
}
|
h = 0;
while (h != 8) {
print(h);
h += 1;
}
|
k = 1;
while (k != 8) {
print(k);
k += 1;
}
|
The
for loop has very subtle semantics that vary from language to language. Best to avoid except for
1 to
n or
0 to
n-1. Python does not have a
for loop.
25. Assignment statements
What do the following assignment statements do? That is, what is their effect.
x = y - x;
y = y - x;
x = x + y;
Explain how you arrived at your conclusion.
The exclusive or operation can be used for the same effect.
26. Swapping values
Parallel assignment: (syntax varies)
x, y = y, x
Lua
JavaScript (since 1.7)
PHP
Python
PowerShell
Ruby
27. Procedures
A procedure can be used to swap values.
This handles the noncomputer-checked redundancy.
Reference parameters are needed. Some languages do not have reference parameters.
28. Procedure using reference parameters
Here is the C code [#1]
29. Output
Here is the output of the C code.
30. Macro expansion
A procedure can be considered a textual macro that is expanded in-line with the parameters.
Macro expansion can be used such as is found in C, C++, Julia, etc.
Redundancy is generated but is handled by the computer.
31. Macro expansion
Here is the C code [#2]
Note: Due to the replacement of swap(x1,y2) with a block (delimited by curly braces), a terminating semicolon can be used or not used.
32. Output
Here is the output of the C code.
33. Text formatter
A text formatter approach uses macros to expand text in-line. Somewhat like
AOP = Aspect Oriented Computing
@ MACRO(swap(a,b)=[a, b = b, a;])
Another way:
@ MACRO(swap(a,b,t)=[t = a;
a = b;
b = t;])
Combined way:
@ MACRO(swap(a,b,t)=[@ IFDEF(t,y [t = a;
a = b;
b = t;]
,n [a, b = b, a;])])
34. End of page