6.170 / Spring 2001 / Lecture 4 Card Answers

Handout L4-QA

Contents:


Requires and Effects Clauses

Question: What do "true" and "false" mean in requires and effects clauses?
Answer: A requires clause lists the preconditions that must be true in order for a method to function properly. The requires clause can be thought of as a list of boolean expressions that must all evaluate to true. (For instance, a requires clause could include x != 0 and y != null.) "true" and "false" are just other boolean expressions like these. If a requires clause is "true", then the requires clause holds for any call to the method, so the implementation cannot assume anything about the values of arguments, etc. If the requires clause is "false", then the requires clause never holds (it is never true). Since a method can do anything when the requires clause is violated, a requires clause of "false" means that a method can do anything.

The effects clause specifies the behavior of the method when its requires clause is satisfied. We can associate a truth value with an effects clause, which basically answers the question "Does the implementation have these effects?" In order for an implementation to satisfy its specification, the effects clause must be true. So, if an effects clause is "true", then any implementation satisfies this. In other words, the procedure can do anything it wants. If an effects clause is "false", then no implementation satisfies the specification.

Question: Could you explain the strongest requires/effects/modifies clauses?
Answer: As described above, a requires clause of "true" means that the implementation cannot assume anything about the arguments or program state. This is the strongest requires clause because any other requires clause will be satisfied by fewer program states. For instance, a requires clause of x != null (where x is some argument) will not be satisfied for any procedure call where x == null, while the requires clause of "true" would be satisfied in these calls.

The strongest effects clause is "false". If an effects clause is "false", then the specification can never be satisfied. So this is the hardest specification to satisfy.

The strongest modifies clause is "nothing". If a specification says that something can be modified, then it is also valid if the implementation modifies nothing. This implies that a modifies clause of "nothing" is the modifies clause that the fewest implementation can satisfy.

Question: What does it mean for a spec to be weaker or stronger?
Answer: Specification A is weaker than specification B if the implementations satisfying A are a superset of those satisfying B. For instance, if specification B describes a procedure that sorts integers in O(n log n) time, and specification A describes a procedure that sorts integers in O(n2) time, A is weaker than B. Any O(nlgn) sorting algorithm performs at least as well as an O(n2) sorting algorithm. So A describes all of the implementations admitted by B and more. A specification is stronger if that specification is harder to satisfy -- that is, a subset of the implementations can satisfy that specification.

Question: Can you re-explain requires => (modifies ^ effects)?
Answer: This is just a mathematical way to express the statement: If the requires clause of a procedure is satisfied, then the modifies and effects clauses will be satisfied. "=>" means "implies", and "^" means "and".

Inheritance

Question: Say that method a() in class A calls some private method p(). If a subclass B of A inherits the implementation of a(), will that method work correctly?
Answer: Yes. When the method a() is called on an instance of the B, the method call is dispatched to the implementation in A. This code belongs to A, which has access to method p().

If instead B overrides a() with code of its own, it could not call p().


Back to Index Card Answers.
Back to the 6.170 home page.
For problems or questions regarding this page, contact: 6.170-webmaster@mit.edu.
$Id: lec04-qa.html,v 1.3 2001/02/14 04:25:25 mistere Exp $