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
by RS  admin@robinsnyder.com : 1024 x 640


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. Whatever value was in variable v before is lost (forever).

4. Step 1
Evaluate expression to a value

5. Step 2
Replace variable with 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;


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
TestHow 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
Programming fallacyFallacy: 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.

Information sign More: Edsger Dijkstra

11. Quantum computing in brief
Bit and qubitQuantum computing:

Dunbar number [exponential speedup not clearly defined, like entanglement]qc-11

12. Quantum computing analogies
Bit and qubitQuantum computing analogies: pick the best way

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
DONEMany students have trouble working backwards, using algebra, and realizing when the proof is done.
Stepwise refinement - numberedStair analogy:

16. When is it done?
Penrose steps 6Many 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.

DONEAside: 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?

Information sign More: Penrose steps: how it is done
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).

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.

Information sign More: George Armitage Miller

21. Viewpoint
Viewpoints:

22. Changing code
Cat in a boxMoving 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.

Information sign More: The XOR operation and one-time pads

26. Swapping values
Parallel assignment: (syntax varies)
x, y = y, x


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.

Information sign More: Procedure to swap values

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

by RS  admin@robinsnyder.com : 1024 x 640