Send
Close Add comments:
(status displays here)
Got it! This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
Axiomatic semantics: assertions
1. Axiomatic semantics: assertions
2. Assertions
What does it mean to "assert" something?
3. Assertions
To assert means to claim that something is true.
An assertion is a logical formula, usually enclosed in curly braces "{" and "}", that is asserted to be true when control reaches that part of the program.
Originally, languages where curly braces are comments made this easy. Today, one can comment the curly braces or use another notation or just the word assert.
4. An assertion
The assertion
{ p }
means that when execution reaches this point of the program, then
p is (asserted to be) true.
In a program, such an assertion would often be written as a comment.
// { p }
5. Logical expressions
p is a logical expression, so p is either true or false.
6. True
What does
// { true }
mean?
7. True
true is always true. true can never be false. Therefore, the assertion
// { true }
can be written anywhere in a program. Usually this is not written explicitly, but derived in showing that there is no contradiction at a certain point in a program.
8. False
What does
// { false }
mean?
9. False
false is always false. false can never be true. So, an assertion of
// { false }
means that execution can never reach this part of the program.
10. Delphi
The
halt statement in Delphi Pascal (and some other languages) immediately halts the program.
halt;
{ false }
The
exit statement in Delphi Pascal (and other languages) immediately exits from the current procedure/function.
exit;
{ false }
11. C or C++
12. Skip statement
Python has a
pass statement that acts as a
skip statement.
// { p }
skip;
// { p }
What is true after the
skip statement is executed is true before the
skip statement is executed.
13. C/C++
A
skip statement, or
nop (no operation) instruction in assembly language, can be simulated with the following assignment statement.
// { p }
x = x;
// { p }
That is, the above assignment statement has the same semantics as the
skip statement. In fact, that is now the
nop instruction is usually implemented in microprocessors, as an assignment from a register to the same register (since the processor must always be doing something, even if nothing gets done).
There is an efficiency consideration to the above assignment statement, but correctness is not directly concerned with efficiency, another important area of computer science.
14. Conditions
Preconditions and postconditions:
// { pre: p }
skip;
// { post: q }
15. Precondition
A precondition states the initial conditions. That is, what you are assuming.
16. Postcondition
A postcondition is the goal. That is, the result you are seeking given the initial conditions.
17. Statements
Statements (i.e., commands) are used to accomplish the postcondition, given the precondition.
// { pre: p }
// { post: q }
18. Assertions
Most programming languages do not understand assertions.
Assertions can be written in code by making them comments.
// { p }
19. End of page