# You cannot do physics without mathematics. - PowerPoint PPT Presentation Download Presentation You cannot do physics without mathematics.

You cannot do physics without mathematics. Download Presentation ## You cannot do physics without mathematics.

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. You cannot do physics without mathematics.

2. You cannot do programming without predicate logic.

3. Programming 101: Know what you are doing!  Programming Fundamentals 16 Feliks Kluźniak

4. Two quotations: ”Once the routine compiles, put it into the debugger and step through each line of code. Make sure each line executes as you expect it to. You can find many errors by following this simple practice.” Steve McConnell, Code Complete, Microsoft Press, 1993 Know what you are doing!

5. Two quotations: ”Once the routine compiles, put it into the debugger and step through each line of code. Make sure each line executes as you expect it to. You can find many errors by following this simple practice.” Steve McConnell, Code Complete, Microsoft Press, 1993 ”Huh?” F. K., upon reading the above Know what you are doing!

6. Consider the following program: if c > d then c, d := d, c fi; if b > d then b, d := d, b fi; if b > c then b, c := c, b fi; if a > d then a, d := d, a fi; if a > c then a, c := c, a fi; if a > b then a, b := b, a fi i.e., the values are swapped Know what you are doing!

7. Consider the following program: if c > d then c, d := d, c fi; if b > d then b, d := d, b fi; if b > c then b, c := c, b fi; if a > d then a, d := d, a fi; if a > c then a, c := c, a fi; if a > b then a, b := b, a fi How many possible execution paths can this program take? Know what you are doing!

8. Consider the following program: if c > d then c, d := d, c fi; if b > d then b, d := d, b fi; if b > c then b, c := c, b fi; if a > d then a, d := d, a fi; if a > c then a, c := c, a fi; if a > b then a, b := b, a fi How many possible execution paths can this program take? 2 * 2 * 2 * 2 * 2 * 2 = 64 Know what you are doing!

9. Consider the following program: if c > d then c, d := d, c fi; if b > d then b, d := d, b fi; if b > c then b, c := c, b fi; if a > d then a, d := d, a fi; if a > c then a, c := c, a fi; if a > b then a, b := b, a fi How many possible execution paths can this program take? 2 * 2 * 2 * 2 * 2 * 2 = 64 What does the program do? Know what you are doing!

10. Consider the following program: if c > d then c, d := d, c fi; if b > d then b, d := d, b fi; if b > c then b, c := c, b fi; if a > d then a, d := d, a fi; if a > c then a, c := c, a fi; if a > b then a, b := b, a fi How many possible execution paths can this program take? 2 * 2 * 2 * 2 * 2 * 2 = 64 What does the program do? Have you tested 64 paths in your mind? Know what you are doing!

11. Consider the following program: if c > d then c, d := d, c fi; if b > d then b, d := d, b fi; if b > c then b, c := c, b fi; if a > d then a, d := d, a fi; if a > c then a, c := c, a fi; if a > b then a, b := b, a fi How many possible execution paths can this program take? 2 * 2 * 2 * 2 * 2 * 2 = 64 What does the program do? Have you tested 64 paths in your mind? Then how do you know? Know what you are doing!

12. Consider the following program: if c > d then c, d := d, c fi; if b > d then b, d := d, b fi; if b > c then b, c := c, b fi; if a > d then a, d := d, a fi; if a > c then a, c := c, a fi; if a > b then a, b := b, a fi How many possible execution paths can this program take? 2 * 2 * 2 * 2 * 2 * 2 = 64 What does the program do? Have you tested 64 paths in your mind? Then how do you know? You use assertions. Know what you are doing!

13. An assertion is a statement about the state of the program, associated with a particular point in the program. The statement often takes the form of a predicate. Know what you are doing!

14. An assertion is a statement about the state of the program, associated with a particular point in the program. The statement often takes the form of a predicate. An assertion associated with point p is correct if the state of the program satisfies the assertion whenever the execution of the program reaches point p. Know what you are doing!

15. An assertion is a statement about the state of the program, associated with a particular point in the program. The statement often takes the form of a predicate. An assertion associated with point p is correct if the state of the program satisfies the assertion whenever the execution of the program reaches point p. We will limit our attention to correct assertions, and present each assertion as a comment. The associated point is the point at which the comment is written. Know what you are doing!

16. The points which we associate with assertions are not arbitrary, but just those points at which the meaning of the various constructs of our programming language allows us to say something about the effects of the program. Usually a good point follows a certain statement. Know what you are doing!

17. The points which we associate with assertions are not arbitrary, but just those points at which the meaning of the various constructs of our programming language allows us to say something about the effects of the program. Usually a good point follows a certain statement. For example, only the red arrows indicate ”good” points here: if a > 0 then a := a - 1 fi Know what you are doing!

18. P: { a, b, c, d } = { A, B, C, D } where A, B, C, D are the original values of a, b, c, d. % P if c > d then c, d := d, c fi; if b > d then b, d := d, b fi; if b > c then b, c := c, b fi; if a > d then a, d := d, a fi; if a > c then a, c := c, a fi; if a > b then a, b := b, a fi NOTE: These are bags, not sets: there might be repetitions. Know what you are doing!

19. P: { a, b, c, d } = { A, B, C, D } where A, B, C, D are the original values of a, b, c, d. % P if c > d then c, d := d, c fi; % P and c =< d if b > d then b, d := d, b fi; if b > c then b, c := c, b fi; if a > d then a, d := d, a fi; if a > c then a, c := c, a fi; if a > b then a, b := b, a fi Know what you are doing!

20. P: { a, b, c, d } = { A, B, C, D } where A, B, C, D are the original values of a, b, c, d. % P if c > d then c, d := d, c fi; % P and c =< d if b > d then b, d := d, b fi; % P and c =< d and b =< d if b > c then b, c := c, b fi; if a > d then a, d := d, a fi; if a > c then a, c := c, a fi; if a > b then a, b := b, a fi Know what you are doing!

21. P: { a, b, c, d } = { A, B, C, D } where A, B, C, D are the original values of a, b, c, d. % P if c > d then c, d := d, c fi; % P and c =< d if b > d then b, d := d, b fi; % P and c =< d and b =< d if b > c then b, c := c, b fi; if a > d then a, d := d, a fi; if a > c then a, c := c, a fi; if a > b then a, b := b, a fi Careful! d might have changed! Know what you are doing!

22. P: { a, b, c, d } = { A, B, C, D } where A, B, C, D are the original values of a, b, c, d. % P if c > d then c, d := d, c fi; % P and c =< d if b > d then b, d := d, b fi; % P and c =< d and b =< d if b > c then b, c := c, b fi; if a > d then a, d := d, a fi; if a > c then a, c := c, a fi; if a > b then a, b := b, a fi c =< d and d < b implies c =< b Know what you are doing!

23. P: { a, b, c, d } = { A, B, C, D } where A, B, C, D are the original values of a, b, c, d. % P if c > d then c, d := d, c fi; % P and c =< d if b > d then b, d := d, b fi; % P and c =< d and b =< d if b > c then b, c := c, b fi; % P and c =< d and b =< c if a > d then a, d := d, a fi; if a > c then a, c := c, a fi; if a > b then a, b := b, a fi ? Know what you are doing!

24. P: { a, b, c, d } = { A, B, C, D } where A, B, C, D are the original values of a, b, c, d. % P if c > d then c, d := d, c fi; % P and c =< d if b > d then b, d := d, b fi; % P and c =< d and b =< d if b > c then b, c := c, b fi; % P and c =< d and b =< c if a > d then a, d := d, a fi; if a > c then a, c := c, a fi; if a > b then a, b := b, a fi because b =< d before the swap Know what you are doing!

25. P: { a, b, c, d } = { A, B, C, D } where A, B, C, D are the original values of a, b, c, d. % P if c > d then c, d := d, c fi; % P and c =< d if b > d then b, d := d, b fi; % P and c =< d and b =< d if b > c then b, c := c, b fi; % P and c =< d and b =< c if a > d then a, d := d, a fi; % P and c =< d and b =< c and a =< d if a > c then a, c := c, a fi; if a > b then a, b := b, a fi because d < a before the swap Know what you are doing!

26. P: { a, b, c, d } = { A, B, C, D } where A, B, C, D are the original values of a, b, c, d. % P if c > d then c, d := d, c fi; % P and c =< d if b > d then b, d := d, b fi; % P and c =< d and b =< d if b > c then b, c := c, b fi; % P and c =< d and b =< c if a > d then a, d := d, a fi; % P and c =< d and b =< c and a =< d if a > c then a, c := c, a fi; % P and c =< d and b =< c and a =< c if a > b then a, b := b, a fi because a =< d before the swap Know what you are doing!

27. P: { a, b, c, d } = { A, B, C, D } where A, B, C, D are the original values of a, b, c, d. % P if c > d then c, d := d, c fi; % P and c =< d if b > d then b, d := d, b fi; % P and c =< d and b =< d if b > c then b, c := c, b fi; % P and c =< d and b =< c if a > d then a, d := d, a fi; % P and c =< d and b =< c and a =< d if a > c then a, c := c, a fi; % P and c =< d and b =< c and a =< c if a > b then a, b := b, a fi because c < a before the swap Know what you are doing!

28. P: { a, b, c, d } = { A, B, C, D } where A, B, C, D are the original values of a, b, c, d. % P if c > d then c, d := d, c fi; % P and c =< d if b > d then b, d := d, b fi; % P and c =< d and b =< d if b > c then b, c := c, b fi; % P and c =< d and b =< c if a > d then a, d := d, a fi; % P and c =< d and b =< c and a =< d if a > c then a, c := c, a fi; % P and c =< d and b =< c and a =< c if a > b then a, b := b, a fi % P and c =< d and b =< c and a =< b because a =< c before the swap Know what you are doing!

29. P: { a, b, c, d } = { A, B, C, D } where A, B, C, D are the original values of a, b, c, d. % P if c > d then c, d := d, c fi; % P and c =< d if b > d then b, d := d, b fi; % P and c =< d and b =< d if b > c then b, c := c, b fi; % P and c =< d and b =< c if a > d then a, d := d, a fi; % P and c =< d and b =< c and a =< d if a > c then a, c := c, a fi; % P and c =< d and b =< c and a =< c if a > b then a, b := b, a fi % P and c =< d and b =< c and a =< b % P and a =< b =< c =< d Know what you are doing!

30. P: { a, b, c, d } = { A, B, C, D } where A, B, C, D are the original values of a, b, c, d. % P if c > d then c, d := d, c fi; if b > d then b, d := d, b fi; if b > c then b, c := c, b fi; if a > d then a, d := d, a fi; if a > c then a, c := c, a fi; if a > b then a, b := b, a fi % P and a =< b =< c =< d How many steps have we looked at? Know what you are doing!

31. P: { a, b, c, d } = { A, B, C, D } where A, B, C, D are the original values of a, b, c, d. % P if c > d then c, d := d, c fi; if b > d then b, d := d, b fi; if b > c then b, c := c, b fi; if a > d then a, d := d, a fi; if a > c then a, c := c, a fi; if a > b then a, b := b, a fi % P and a =< b =< c =< d How many steps have we looked at? 6 is much less than 64 ! Know what you are doing!

32. P: { a, b, c, d } = { A, B, C, D } where A, B, C, D are the original values of a, b, c, d. % P if c > d then c, d := d, c fi; if b > d then b, d := d, b fi; if b > c then b, c := c, b fi; if a > d then a, d := d, a fi; if a > c then a, c := c, a fi; if a > b then a, b := b, a fi % P and a =< b =< c =< d How many steps have we looked at? 6 is much less than 64 ! The number of steps is linear in program size, the number of paths grows more or less exponentially! Know what you are doing!

33. P: { a, b, c, d } = { A, B, C, D } where A, B, C, D are the original values of a, b, c, d. % P if c > d then c, d := d, c fi; if b > d then b, d := d, b fi; if b > c then b, c := c, b fi; if a > d then a, d := d, a fi; if a > c then a, c := c, a fi; if a > b then a, b := b, a fi % P and a =< b =< c =< d How many steps have we looked at? 6 is much less than 64 ! The number of steps is linear in program size, the number of paths grows more or less exponentially! Know what you are doing!

34. P: { a, b, c, d } = { A, B, C, D } where A, B, C, D are the original values of a, b, c, d. % P if c > d then c, d := d, c fi; if b > d then b, d := d, b fi; if b > c then b, c := c, b fi; if a > d then a, d := d, a fi; if a > c then a, c := c, a fi; if a > b then a, b := b, a fi % P and a =< b =< c =< d How many steps have we looked at? 6 is much less than 64 ! The number of steps is linear in program size, the number of paths grows more or less exponentially! So "operational’’ thinking is no good! Know what you are doing!

35. P: { a, b, c, d } = { A, B, C, D } where A, B, C, D are the original values of a, b, c, d. % P if c > d then c, d := d, c fi; if b > d then b, d := d, b fi; if b > c then b, c := c, b fi; if a > d then a, d := d, a fi; if a > c then a, c := c, a fi; if a > b then a, b := b, a fi % P and a =< b =< c =< d How many steps have we looked at? 6 is much less than 54 ! The number of steps is linear in program size, the number of paths grows more or less exponentially! Worse if we have loops! Know what you are doing!

36. Yes, but how do we reason about loops? Know what you are doing!

37. Consider the following problem: • An urn contains B black balls and W white balls. • As long as the urn contains at least two balls, we perform the following procedure: Know what you are doing!

38. Consider the following problem: • An urn contains B black balls and W white balls. • As long as the urn contains at least two balls, we perform the following procedure: • we take out two balls; Know what you are doing!

39. Consider the following problem: • An urn contains B black balls and W white balls. • As long as the urn contains at least two balls, we perform the following procedure: • we take out two balls; • if both are black or both are white, we throw them out; Know what you are doing!

40. Consider the following problem: • An urn contains B black balls and W white balls. • As long as the urn contains at least two balls, we perform the following procedure: • we take out two balls; • if both are black or both are white, we throw them out; • if one is black and one is white, we throw out the white one and return the black one to the urn. Know what you are doing!

41. Consider the following problem: • An urn contains B black balls and W white balls. • As long as the urn contains at least two balls, we perform the following procedure: • we take out two balls; • if both are black or both are white, we throw them out; • if one is black and one is white, we throw out the white one and return the black one to the urn. • Will this terminate? Know what you are doing!

42. Consider the following problem: • An urn contains B black balls and W white balls. • As long as the urn contains at least two balls, we perform the following procedure: • we take out two balls; • if both are black or both are white, we throw them out; • if one is black and one is white, we throw out the white one and return the black one to the urn. • How does the final state of the urn depend on BandW ? Know what you are doing!

43. How do we reason about loops? Know what you are doing!

44. How do we reason about loops? Well, let’s look at what we can do with assertions... Know what you are doing!

45. while B do S od Here are a few possible execution traces, with some assertions: Trace 1Trace 2 Trace 3 % P1 % P2 % P3 S S % Q1 % Q2 S % R1 S % S1 Know what you are doing!

46. while B do S od Here are a few possible execution traces, with some assertions: Trace 1Trace 2 Trace 3 % P1 % P2 % P3 S S % Q1 % Q2 S % R1 S % S1 Now, since we don’t know the number of iterations, assertion Q1 had better be the same as R1. Know what you are doing!

47. while B do S od Here are a few possible execution traces, with some assertions: Trace 1Trace 2 Trace 3 % P1 % P2 % P3 S S % Q1 % Q2 S % R1 S % S1 Now, since we don’t know the number of iterations, assertion Q1 had better be the same as R1. In other words, if Q1 is correct before we execute S, it must be correct after. And so we have… Know what you are doing!

48. while B do S od Here are a few possible execution traces, with some assertions: Trace 1Trace 2 Trace 3 % P1 % P2 % P3 S S % Q1 % Q2 S % Q1 S % S1 Now, since we don’t know the number of iterations, assertion Q1 had better be the same as R1. In other words, if Q1 is correct before we execute S, it must be correct after. And so we have… Know what you are doing!

49. while B do S od Here are a few possible execution traces, with some assertions: Trace 1Trace 2 Trace 3 % P1 % P2 % P3 S S % Q1 % Q2 S % Q1 S % S1 Now, since we don’t know the number of iterations, assertion Q1 had better be the same as R1. In other words, if Q1 is correct before we execute S, it must be correct after. Q1 must also hold after the loop terminates, but additionally we will have not B. Know what you are doing!

50. while B do S od Here are a few possible execution traces, with some assertions: Trace 1Trace 2 Trace 3 % P1 % P2 % P3 S S % Q1 % Q2 S % Q1 S % Q1 and not B Now, since we don’t know the number of iterations, assertion Q1 had better be the same as R1. In other words, if Q1 is correct before we execute S, it must be correct after. Q1 must also hold after the loop terminates, but additionally we will have not B. Know what you are doing!