In the textbook I'm using to prepare the logic exam says that first order logic may be used to implement axiomatize data structures. There is an example of that:
"Stack": uses a language that contains the predicates functions top, pop and push, and the constant nil (empty stack). It axioms are:
- $\forall s \forall x . top(push(s, x))=x$
- $\forall s \forall x . pop(push(s, x))=s$
- $\forall s . push(pop(s), top(s))=s$
Premise: that I didn't understand this example completely (there's no explanation about how can these axioms describe a stack).
Suppose I have to describe another data structure, for example a "Queue", what strategy do you follow to write the axioms of this data structure? Can you make an example of the axioms you would use?
EDIT: Using the @user21820 's answer as an example, I'm trying to answer my own question.
Strategy:
- Describe the language used: $(Queue, add, remove, element, nil)$ where
- $Queue$ is an unary predicate, $Queue(x)$ means $x \in Queue$;
- $add$ is a binary function. $add(q, e), q \in Queue$ returns the queue with a new element at the end of the queue;
- $remove$ is a binary function. $remove(q, e), q \in Queue$ returns the queue without the first element of the queue;
- $element$ is a unary function that returns the first element of the queue.
- $nil$ is a constant, means empty queue.
- Describe the axioms you need (they might be wrong):
- remove an element from a queue with one element return the empty queue;
- remove an element from a queue return the queue without the first element;
- add an element from the queue returns the queue with the element added;
- element function returns the first element inserted;
- Write the axioms:
- $remove(add(nil, x)) = nil$
- $\forall q \in Queue. \left(remove(add(q, x)) = ???\right)$
- $???$
- $???$
Here is an answer that applies if you do not allow the length function for stacks/queues.
Stacks
If you take away the axioms about the length of stacks, there are worlds that satisfy the axioms but have very weird behaviour. (I was writing a more detailed version of this section but lost the whole lot accidentally. I'm not going to rewrite it, sorry.)
In one world, $push([0,0],0) = nil$, and $pop(nil) = [0,0]$ and $top(nil) = 0$, and everything else is as usual. In another, every stack consists of finitely many items on top of $nil$, while $nil$ is actually an infinite stack of turtles all the way down, and of course $push(nil,) = pop(nil) = nil$. In a third, there are both finite stacks including $nil$ and infinite stacks that are eventually turtles all the way down, and the infinite turtle stack is not $nil$. If one varies the nature of the infinite stacks one can get interesting behaviour; it could be that every infinite stack is eventually an alternating stack $T$ of and at the bottom, such that $push(push(T,),) = T$ but still we have $push(s,x) \ne s$ for every stack $s$ and object $x$.
The best you can do if you do not have the length function is to add the following axioms:
At least this axiomatization allows you to prove or disprove every true sentence you can state over the original language. (If I'm not mistaken.)
It also prevents the weird worlds mentioned above, though there are other non-standard worlds that satisfy even this. One of them is as follows. Every stack is either a standard finite stack or a bi-directional infinite array with an index $p$ that is filled up to cell $p$ and is empty from cell $p+1$ onwards. Operations on the standard stacks are as usual. Operations on the non-standard stacks is done as if the filled cells form a stack with cell $p$ at the top, so pushing an item will fill cell $p+1$, while popping will erase cell $p$, and $p$ is incremented or decremented as appropriate. You can check that all the axioms are satisfied.
Queues
As with stacks, what you can axiomatize for queues is limited if you do not have a length function, but let's see what can be done.
First let's look at your attempt:
remove(add(nil,x))=nil
Invalid because you didn't specify $x$. The correct version is:
∀q∈Queue.(remove(add(q,x))=???)
You probably realize that you can't determine what's left behind.
Well for a non-empty queue the order of adding an item or removing the first does not matter.
???
In first-order logic you cannot specify the output type of a function.
Read and follow my example for stacks properly.
You're also missing axioms specifying the first element of a queue.
[Try before looking below.]
I think these axioms will allow you to prove any true sentence about queues in the given language. But they will not prevent a world with non-standard queues. Similar to the non-standard world at the end of the section about stacks, one can have a world where a non-standard queue is a queue with an infinite front and infinite back, each stored in an infinite array where the position of both the first and last items are tracked.