Upload
toannguyen
View
1
Download
0
Embed Size (px)
DESCRIPTION
Assignment 2 toán rời rạc 2 - khoa khoa học và kỹ thuật máy tính
Citation preview
Ho Chi Minh City University of TechnologyFaculty of Computer Science and Engineering
Discrete Mathematics IIASSIGNMENT 2
Instructions:- Describe each of the following programming problems using Propositional/
Predicate logics.- All expression should be in Conjunctive Normal Form (CNF).- Give one example for each problem that makes it fail. Show proof why it fail in
that case.- (Bonus) Using SPIN tool (http://spinroot.com) to model check any 02 described
problems. Describe a problem using the description language used by SPIN tool. Show command line. Show the result from SPIN tool.
Problems:1/ Return the maximum between two any numbers. (Already solved in the lecture.)
Example: the maximum of 3 and 5 is 5.Failure example: the maximum of 3 and 5 is 3. Proof:
2/ Return the absolute value of a number.
Example: the absolute value of 3 is 3, and that of -3 is 3Failure example: the absolute value of 3 is -3. Proof:
3/ Find the number of real solution (using real domain, not complex domain) for a quadratic function (ax2 + bx + c) giving its parameters a,b and c. Support that there is the square root function - sqrt.
Example: the quadratic function with a=0, b=2, c=1 (0x2 + 2x + 1) has only one solution; a=1, b=3, c=-1 (x2 + 3x - 1) has two solutions and a=5, b=2, c=9 (5x2 + 2x + 9) has no solution.Failure example: the function (5x2 + 2x + 9) has one solution. Proof:
4/ Find the maximum of an array/list of N numbers.
Example: 5 is the maximum of an array {1,2,5,-9,3,-2}Failure example: -9 is the maximum of an array {1,2,5,-9,3,-2}. Proof:
5/ Sort an array/list of N numbers.
/1 3
Example: the sorted version of {1,2,5,-9,3,-2} is {-9,-2,1,2,3,5}Failure example: the sorted version of {1,2,5,-9,3,-2} is {1,-2,2,3,5,9}. Proof:
6/ Reverse an array/list of N numbers (the order of elements is reversed).
Example: the reversion of {1,2,5,-9,3,-2} is {-2,3,-9,5,2,1} Failure example: the reversion of {1,2,5,-9,3,-2} is {1,2,3,5,-2,-9}. Proof:
Hint:
Theory:
Let P is the specification of the programming problem we want to solve.Let Q is the model (logic) of the program we want to verify with the specification P.The Q is said to be correct iff Q P.
There are some strategies to proof.
Problem 1: Return the maximum between two any numbers.
(in the lecture we already have P = (x>y z=x) ((x>y) z=y), so we need to rewrite it in CNF)
P = (x>y z=y) ((x>y) z=x) (z=x z=y)
Example:
let x=3, y=5, Q returns z=5, we have P[x/3, y/5] = (3>5 z=5) ((3>5) z=3) (z=3 z=5)
= (z=5) (z=3 z=5)= (z=5)
orQ[z/5] P[x/3, y/5](That is what we want)
Correct(Q P)
Incorrect(Q P)
Counter-example?
1/ P Q TRUE FALSE Some case(s) that make(s) it false
2/ P Q FALSE TRUE Some case(s) that make(s) it true
3/ P Q
/2 3
Failure example:
let x=3, y=5, Q return z=3, we have
*** Approach 1:
P[x/3, y/5] = (3>5 z=5) ((3>5) z=3) (z=3 z=5)= (z=5) (z=3 z=5)= (z=5)
Q[z/3] P[x/3, y/5](That is what we want to show)
*** Approach 2:
P[x/3, y/5] Q[z/3] = (3>5 z=5) ((3>5) z=3) (z=3 z=5) (z=3)= (z=5) (z=3 z=5) (z=3) = TRUE
Q[z/3] P[x/3, y/5](That is what we want to show)
*** Approach 3:P[x/3, y/5] Q[z/3] = ((3>5 z=5) ((3>5) z=3) (z=3 z=5)) (z=3)= ((3>5 z=5) ((3>5) z=3) (z=3 z=5)) (z=3)= ((3>5 z=5) (z=3)) (((3>5) z=3) (z=3)) ((z=3 z=5) (z=3))= P1 P2 P3
+ Case 1: P1 = ((3>5 z=5) (z=3)) is TRUE? => YES! + Case 2: P2 = (((3>5) z=3) (z=3)) is TRUE? + Case 3: P3 = ((z=3 z=5) (z=3)) is TRUE?
One of components of disjunctive is TRUE => TRUE
Q[z/3] P[x/3, y/5](That is what we want to show)
/3 3