The first of these takes two Boolean values, and returns true if both of the input values are true and returns false otherwise. It is a binary operation, and we shall write it using the infix notation ∧, where the symbol ∧ can be read as “and”. So a ∧ b is true only when a and b are both true. We can describe the function corresponding to this operation as below (The operation ∧ is also known as conjunction).
function (infix) (a ∧ b on Bool) return in Bool
pre true.
post The returned value is true if both a = true and b = true, and is false otherwise.
Alternatively, we could give the semantics of ∧ by listing the returned value in each of four cases, giving the four possible combinations of input values. Using this approach, we could express the postcondition as follows.
post The returned value is c, where
- if a = true and b = true then c = true
- if a = true and b = false then c = false
- if a = false and b = true then c = false
- if a = false and b = false then c = false.
function (infix) (a ∨ b on Bool) return in Bool
pre true.
post The returned value is false if both a = false and b = false, and is true otherwise.
Activity 25
Give the semantics of ∨ by listing the returned value in each of four cases, giving the four possible combinations of input values.
Discussion
We can express the postcondition in the description above as follows.post The returned value is c, where
- if a = true and b = true then c = true
- if a = true and b = false then c = true
- if a = false and b = true then c = true
- if a = false and b = false then c = false.
Activity 26
- (a) Give a full description of the function NOT.
- (b) If a = true and b = false, evaluate each of:
- (i) a ∨ b;
- (ii) NOT(a ∨ b);
- (iii) a ∧ (a ∨ b).
- (i) a ∨ b;
Discussion
(a) A description is given below.function NOT(a in Bool) return in Bool
pre true.
post The returned value is false if a = true and is true if a = false.
(b) (i) Substituting for a and b, we have true ∨ false = true.
(ii) We should evaluate the term in brackets first: true ∨ false = true. Then NOT(true) = false. So NOT(a ∨ b) = false.
(iii) Again, evaluate the term in brackets first: (true ∨ false) = true. Then the given expression becomes true ∧ true which evaluates to true.
No comments:
Post a Comment