Reference Sheet for CO141 Reasoning about Programs Spring 2017

1

Stylised Proofs for Reasoning 1. Write out and name each given formula.

Reasoning about Haskell Programs

1.1

2. Write out and name each formula to be shown.

Mathematical Induction

Principle of Mathematical Induction: For any P ⊆ N:

3. Plan out the proof and name intermediate results. P (0) ∧ ∀k : N. [P (k) → P (k + 1)] → ∀N.P (n)

4. Justify each step of the proof.

i.e. to prove by induction, we prove a base case and an inductive step. General Technique: For any P ⊆ Z and any m : Z:

We use the following methods to plan out a proof for P : 1. Contradiction i.e. show ¬P → false.

P (m) ∧ ∀k ≥ m. [P (k) → P (k + 1)] → ∀n ≥ m.P (n)

2. If P = Q ∧ R show both Q and R. 3. If P = Q ∨ R show either Q or R.

1.2

4. If P = Q → R assume Q and show R.

Strong Induction P (0) ∧ ∀k : N. [∀j ∈ {0..k} .P (j) → P (k + 1)] → ∀n : N.P (n)

5. If P = ¬Q show Q → false.

Note: for some problems, it may be necessary to split the inductive step into cases. E.g. k = 0 or k 6= 0. Mathematical induction and strong induction are equivalent.

6. If P = ∀xQ (x) take arbitrary c and show Q (c). 7. If P = ∃xQ (x) find some c and show Q (c).

1.3

We use the following methods to justify our proof: 1. If false holds then P holds.

Structural Induction over Haskell Data Types

We generalise the concept of predecessor and successor. Example 1 : Structural Induction Principle over Lists:

2. If Q ∧ R holds then Q and R both hold.

P ([]) ∧ ∀vs : [T]∀v : T. [P (vs) → P (v : vs)] → ∀xs : [T].P (xs)

3. If Q ∨ R holds we do case analysis assuming each in turn. 4. If Q → R holds and Q holds then R holds.

Example 2 : Structural Induction Principle over Data BExp = Tr | Fl | BNt BExp | BAnd BExp BExp:

5. If ∀xQ (x) holds then Q (c) holds for any c. 6. If ∃xQ (x) holds then Q (c) holds for some c.

P (Tr) ∧ P (Fl) ∧ ∀b : BExp. [P (b) → P (BNt b)] ∧ ∀b1, b2 : BExp. [P (b1) ∧ P (b2) → P (BAnd b1 b2)] → ∀b : BExp.P (b)

7. We can apply any lemma / equivalence given or proven earlier. 1

Proof Methods

Here it is much simpler to derive the inductive principle from the definition of Odd, rather than from the definition of SN :

1. Invent an Auxiliary Lemma.

Q (Succ Zero) ∧ ∀n ∈ SN . [Odd (n) ∧ Q (n) → Q (Succ (Succ n))]

2. Strengthen the original property. E.g. rewrite ∀is : [Int].sum is = sum tr is 0 as ∀k : Int∀is : [Int].k + sum is = sum tr is k.

1.4

→ ∀n ∈ SN . [Odd (n) → Q (n)]

Induction over Recursively Defined Structures

Functions

Example: Consider the Haskell function:

DM (i , j ) = DM ’ DM ’ (i , j , cnt , | acc + j > | otherwise

Sets, relations and functionas can be defined inductively, which leads to inductive principles. Sets Example: Consider the set of ordered lists, OL ⊆ N∗ :

(i , j , 0 , 0) acc ) i = ( cnt , i - acc ) = DM ’ (i , j , cnt +1 , acc + j )

We can define this inductively as follows:

1. [] ∈ OL

1. ∀i, j ∈ Z. [DM (i, j) = DM0 (i, j, 0, 0)]

2. ∀i ∈ N.i : [] ∈ OL

2. ∀i, j, cnt, acc ∈ Z. [acc + j > i → DM0 (i, j, cnt, acc) = (cnt, i − acc)]

3. ∀i, j ∈ N∀js ∈ N∗ . [i ≤ j ∧ j : js ∈ L → i : j : js ∈ OL]

3. ∀i, j, cnt, acc ∈ Z.[acc + j ≤ i ∧ DM0 (i, j, cnt + 1, acc + j) = (k1, k2)



For a property Q ⊆ N , we get the inductive principle

→ DM0 (i, j, cnt, acc) = (k1, k2)]

Q ([]) ∧ ∀i ∈ N.Q (i : []) ∧

For a predicate Q ∈ Z × Z × Z × Z × Z × Z, we get the following inductive principle for DM’:

∀i, j ∈ N∀js ∈ N∗ . [i ≤ j ∧ j : js ∈ OL ∧ Q (j : js) → Q (i : j : js)] → ∀ns ∈ OL.Q (ns)

∀i, j, cnt, acc ∈ Z. [acc + j > i → Q (i, j, cnt, acc, cnt, i − acc)] ∧ Relations

∀i, j, cnt, acc, k1, k2 ∈ Z.[acc + j ≤ i ∧ DM0 (i, j, cnt + 1, acc + j) = (k1, k2) ∧

Example 1 : Consider the strictly less than relation, SL ⊆ N × N:

Q (i, j, cnt + 1, acc + j, k1, k2) → Q (i, j, cnt, acc, k1, k2)]

1. ∀k ∈ N.SL (0, k + 1)

→ ∀i, j, cnt, acc, k1, k2 : Z. [DM0 (i, j, cnt, acc) = (k1, k2) → Q (i, j, cnt, acc, k1, k2)]

2. ∀m, n ∈ N. [SL (m, n) → SL (m + 1, n + 1)] For a property Q ⊆ N × N, we get the inductive principle

2

∀k ∈ N.Q (0, k + 1) ∧ ∀m, n ∈ N. [SL (m, n) ∧ Q (m, n) → Q (m + 1, n + 1)] → ∀m, n ∈ N. [SL (m, n) → Q (m, n)]

2.1

Reasoning about Java Programs Program Specifications

Pre-Conditions, Mid-Conditions, Post-Conditions

Example 2 : Consider the set of natural numbers, SN :

1. Pre-condition: Must be proven in order to call function, an assumption that code in method can make.

1. Zero ∈ SN 2. ∀n. [n ∈ SN → Succ n ∈ SN ]

2. Mid-condition: Assumption made at specific point in code, must be guaranteed by preceeding code and can be assumed by subsequent code.

and the predicate Odd (SN ): 1. Odd (Succ Zero)

3. Post-condition: Expected to hold after the code has been executed (assuming termination and that precondition held).

2. ∀n ∈ SN . [Odd (n) → Odd (Succ (Succ n))] 2

Example: Consider the Java code:

2.2

Conditional Branches

type method ( type x1 , . . ., type xn ) // PRE : P (x1 , . . . , xn ) // POST : Q(x1 , . . . , xn ) { code1 // MID : R(x1 , . . . , xn ) code2 // MID : S(x1 , . . . , xn ) code3 }

We can assume the pre-condition and the if else condition. Have to show postcondition holds on borth branches of the code. Example: Consider the Java code: // PRE : true if ( x >= y ) { // MID : x0 ≥ y0 res = x ; // MID : res = x0 ∧ x0 ≥ y0 } else { // MID : y0 > x0 res = y ; // MID : res = y0 ∧ y0 > x0 } // MID : res = max{x0 , y0 }

Note: if we choose to introduce new (value) variables in our conditions, there is an implicit universal quantification over the whole specification. Here we need to prove: 1. P (x1 , . . . , xn ) ∧ code1 → R (x1 , . . . , xn ) 2. R (x1 , . . . , xn ) ∧ code2 → S (x1 , . . . , xn )

2.3

3. S (x1 , . . . , xn ) ∧ code3 → Q (x1 , . . . , xn )

Method Calls Need to show that the precondition is met before the method call, then can assume postcondition will hold afterwards. We make necessary substitutions in order to prove our assertions. Example: Consider the java method:

Program Variables 1. x refers to the value of x before code is executed.

Recursion

2. a ≈ b means a is identical to b.

1 2 3 4 5 6 7 8 9 10 11 12 13 14

3. a [x..y) means the elements of a from index x up to (but not including) y.

We need to prove:

2. x0 refers to its value after code is executed, shouldn’t be present in assertions. 3. x0 refers to its original value, as passed into the method. We use r to refer to the return value of a method. Arrays 1. a ∼ b means a is a permutation of b.

int sumAux ( int [] a , int i ) // PRE : a 6= null ∧ 0 ≤P i ≤ a.length // POST : a ≈ a0 ∧ r = a[i..a.length) { if ( i == a . length ) { // MID : a ≈ a0 ∧ i = a.length return 0; } else { // MID : a ≈ a0 ∧ a 6= null ∧ 0 ≤ i < a.length int val = a [ i ] + sumAux (a , i +1); // MID : a ≈ a0 ∧ val = a[i..a.length) return val ; } }

1. Line 6 : Show mid-condition holds: a0 6= null ∧ 0 ≤ i ≤ a0 .length ∧ i = a0 .length ∧ a0 ≈ a0 → a0 ≈ a0 ∧ i = a0 .length.

4. Sorted (a) means a is sorted. 5. min (a) is the smallest element in a.

2. Line 7 : Show post-condition holds: a ≈ a0 ∧ i = a.length ∧ r = 0 → a ≈ P a0 ∧ r = a [i..a.length).

6. max (a) is the largest element in a. 3

3. Line 9 : Show mid-condition holds:a0 6= null ∧ 0 ≤ i ≤ a0 .length ∧ i 6= a0 .length ∧ a0 ≈ a0 → a0 ≈ a0 ∧ a0 6= null ∧ 0 ≤ i < a0 .length.

We need to prove: 1. Invariant holds before loop is entered.

4. Line 10 : Show pre-condition for called method holds: a ≈ a0 ∧ a 6= null ∧ 0 ≤ i < a.length → a 6= null ∧ 0 ≤ i + 1 ≤ a.length.

P [a 7→ a0 ] ∧ res = 0 ∧ i = 0 ∧ a ≈ a0 →I

5. Line 11 : Show holds: a ≈ a0 ∧ a 6= null ∧ 0 ≤ i < a.length∧ Pmid-condition 0 0 0 a ≈ a ∧ r = a [i + 1..a .length) ∧ val0 = a[i] + r → a0 ≈ a0 ∧ val0 = P 0 0 a [i..a .length). P 6. Line 12 : Show post-condition holds: a ≈ a0 ∧ val = a [i..a.length) ∧ P r = val → a ≈ a0 ∧ r = a [i..a.length).

2. Loop body re-establishes invariant. I ∧ i < a.length ∧ res0 = res + a[i] ∧ a0 [i] = res0 ∧ i0 = i + 1∧ ∀k ∈ [0..a.length) \ {i} . [a0 [k] = a[k]] → I [a 7→ a0 , i 7→ i0 , res 7→ res0 ]

Blue statements come from the pre-condition or previous mid-condition, green statements implicitly from code, red statements explicitly from code and purple statements from the post-condition of a called method.

2.4

3. Mid-condition holds straight after loop. I ∧ i ≥ a.length

Iteration

→M Invariant To prove a property holds throughout the loop, we need to prove that the invariant holds before entering the loop, and is preserved by the loop body (including at termination). The invariant and ¬cond can be used to prove the following mid-condition.

4. Loop terminates. I ∧ i < a.length ∧ res0 = res + a[i] ∧ a0 [i] = res0 ∧ i0 = i + 1∧ ∀k ∈ [0..a.length) \ {i} . [a0 [k] = a[k]]

Variant To prove a loop will terminate, we find an integer expression which is bounded below, and decreases in every loop iteration. Example: Consider the java method: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

→ V ≥ 0 ∧ V [a 7→ a0 , i 7→ i0 , res 7→ res0 ] < V 5. Post-condition established.

int culSum ( int [] a ) // PRE P : a 6= null P // POST Q: a.length = a0 .length ∧ r = P a0 [0..a.length) ∧ ∀k ∈ [0..a.length) . [a[k] = a0 [0..k + 1)] { int res = 0; int i = 0; // INV I : a 6= null i ≤ a.length∧ P ∧ a.length = a0 .length ∧ 0 ≤P res = a0 [0..i) ∧ ∀k ∈ [0..i) . [a[k] = a0 [0..k + 1)] ∧ ∀k ∈ [i..a.length) . [a[k] = a0 [k]] // VAR V : a.length − i while ( i < a . length ) { res = res + a [ i ]; a [ i ] = res ; i ++; } P // MID M : a.length = a0 .length ∧ resP = a0 [0..a.length)∧ ∀k ∈ [0..a.length) . [a[k] = a0 [0..k + 1)] return res ; }

M ∧ r = res →Q 6. Array accesses are legal. I ∧ i < a.length → 0 ≤ i < a.length

4

Reference Sheet for CO141 Reasoning about Programs - GitHub

General Technique: For any P ⊆ Z and any m : Z: P (m) ∧ ∀k ≥ m. [P (k) → P (k + 1)] → ∀n ≥ m.P (n). 1.2 Strong Induction. P (0) ∧ ∀k : N. [∀j ∈ {0..k} .

242KB Sizes 3 Downloads 278 Views

Recommend Documents

Oolite Reference Sheet - GitHub
will shut down, requiring a cool-down period before it ... 10 Fuel Scoop ... V2 & Creative Commons License: BY - NC - SA 3.0 Oolite Website: http:/www. ..... A discontinued fighter design finding a new life in the professional racing circuit.

Reference Sheet for CO140 Logic - GitHub
Free Variable Variable which is not bound (this includes variables which do not appear in A!). Sentence Formula with no free variables. ... domain of M, dom (M).

Reference Sheet for CO120.3 Programming III - GitHub
GBB. B d˜rief en enum type th—t represents fl—gs for renderingF. B. B i—™h ˜it represents — different fl—gF …se ˜itwise —nd. B to ™he™k if — fl—g is setF. BG enum render•fl—g {. GBB „he —m˜ient fl—g @˜it HAF BG

Reference Sheet for C112 Hardware - GitHub
Page 1 ... We might be able to make a considerable simplification by considering max- terms (0s) instead of minterms. • Don't cares (X) can ... Noise Margin. Fan out The number of inputs to which the output of a gate is connected. • Since 1. R.

Reference Sheet for CO120.2 Programming II - GitHub
Implementing Interfaces Use notation: @Override when a class method im- ... Style: usually a class extends an abstract class (with constructor and fields).

Reference Sheet for CO130 Databases - GitHub
create table actor_cars ( .... Table. Relational Expression. Views. Tuple. Row. Attribute. Column. Domain .... end of free space, location and size of each record.

Reasoning about faulty quantum programs
tum computation, such as the superoperator quantum circuits of Aharonov et al. [1]. That is a ...... Prob(r = 1) ⩾ c + c¯p(t1 − 1) = cp + c¯pt1 where ti = 〈w ,Piw 〉.

Reference Sheet for CO142.1 Discrete Mathematics I - GitHub
Products For arbitrary sets A and B: 1. Ordered ... Identity idA = {〈x, y〉 ∈ A2|x = y}. Composition .... Identity: The function idA : A → A is defined as idA (a) = a. 3.

Reference Sheet for CO142.2 Discrete Mathematics II - GitHub
Connected: there is a path joining any two nodes. .... and merge two components .... Merge sort can be parallelised by executing recursive calls in parallel. 2.

Statistical Reasoning for Public Health - GitHub
HAS SUCCESSFULLY COMPLETED THE JOHNS HOPKINS UNIVERSITY'S ... PLEASE NOTE: THE ONLINE OFFERING OF THIS CLASS DOES NOT ...

Location Reference Sheet for writers.pdf
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. Location ...

Reference Sheet for CO120.1 Programming I
Stops execution and displays error message. 9 Types and Common ... multiple times or to clean up code. ... You should spend time planning your answer (on ...

CSS3 Cheat Sheet - GitHub
Border Radius vendor prefix required for iOS

gitchangelog Cheat Sheet - GitHub
new: test: added a bunch of test around user usability of feature X. fix: typo in spelling my name in comment. !minor. By Delqvs cheatography.com/delqvs/. Published 14th August, 2017. Last updated 14th August, 2017. Page 1 of 1. Sponsored by ApolloPa

Rely/Guarantee Reasoning for Asynchronous Programs
Application development environments for smartphone ... AJAX, high-performance systems software (e.g., nginx, Chromium, Tor), as well as embedded systems ...

Reference Manual - GitHub
for the simulation of the electron cloud buildup in particle accelerators. 1 Input files .... points of the longitudinal beam profile of sec- ondary beams.

NetBSD reference card - GitHub
To monitor various informations of your NetBSD box you ... ifconfig_if assigns an IP or other on that network in- ... pkg_admin fetch-pkg-vulnerabilities download.

Machine Learning Cheat Sheet - GitHub
get lost in the middle way of the derivation process. This cheat sheet ... 3. 2.2. A brief review of probability theory . . . . 3. 2.2.1. Basic concepts . . . . . . . . . . . . . . 3 ...... pdf of standard normal π ... call it classifier) or a decis

LIKWID | quick reference - GitHub
likwid-memsweeper Sweep memory of NUMA domains and evict cache lines from the last level cache likwid-setFrequencies Control the CPU frequency and ...

J1a SwapForth Reference - GitHub
application. After installing the icestorm tools, you can .... The SwapForth shell is a Python program that runs on the host PC. It has a number of advantages over ...

GABotS Reference Manual - GitHub
Apr 9, 2002 - MainWindow (Main widget for the GABots app). 23. Random ..... Main class for simple Genetic Algorithm used in the program. ز ذظ .

About Amgen Fact Sheet 08.11.09
pioneer since 1980, Amgen was one of the first companies to realize the new science's promise by bringing novel medicines from lab, to manufacturing plant, ...

RTOS Threading Cheat Sheet - GitHub
If the UART is enabled, it causes a data frame to start transmitting with the parameters indicated in the. UARTLCRH register. Data continues to be transmitted ...

R Markdown : : CHEAT SHEET - GitHub
Word, or RTF documents; html or pdf based slides ... stop render when errors occur (FALSE) (default = FALSE) .... colortheme. Beamer color theme to use. X css.