isEmpty()
x=5
y=7
push(x)
push(y)
pop() = x
|
Odd. It doesn't actually define any semantics for push, it just exemplifies part of its behavior.
For example, these implementations conform to that specification:
1 2 3 4 5 6 7 8
|
void queue::push(int x){
if (this->internal!=5)
this->internal=x;
}
int queue::pop(){
return this->internal;
}
|
Something more traditional would be more like
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
push(this: [Int], value: Int){
modifies this;
//The new this is equal to the old one with value prepended to it.
ensures this == value : pre(this);
}
pop(this: [Int]) = result: Int{
//this must not be empty.
requires |this| > 0;
modifies this;
//result is the last element of the unmodified this.
ensures result == pre(this)[|pre(this)| - 1];
//The new this includes everything in the old one except the last element.
ensures this ++ [result] == pre(this);
}
|
So, would it be fair to say the specification is something that's proven by how the program works? |
What you prove is not the specification itself. The specification is an arbitrary entity you define. You could specify operations on a queue that behave like random number generators. It's probably not a good idea, but you can do it.
What you prove is whether a given program conforms to some specification.
if I demonstrate in the queue template I built that when two values are pushed, and then the function to pop is called, the first value pushed is returned, would that constitute proof of correctness? |
For your example, yes, which is why I argue that the specification is incomplete. There's any number of implementations that don't match one's idea of a queue and yet conform to that specification (unless there's some implicit information I'm unaware of).
For my example, it's a little more complicated. You need to prove that, for any state of the program that meets the preconditions, the functions leave the program in
the a state that meets the postconditions.
Note however that mine is an example for
formal specification. I'm not sure on the difference between the two.