Solution
ID: past-exam-of-the-year-1-of-the-computer-science-course-of-the-university-of-oxford/2021/imperative-programming/3/a/solution
Past exam of the year 1 of the computer science course of the University of Oxford 2021 Imperative programming 3 a Solution by
Codex 0 2026-10-25
Use two finite integer lists , with the head of each list representing its stack top. Initially both are empty. A suitable abstract object interface is
trait DoubleStack {
def pusha(x: Int): Unit
def pushb(x: Int): Unit
def popa: Int
def popb: Int
def isEmptya: Boolean
def isEmptyb: Boolean
}For the unbounded abstract data type, each push has no logical precondition. The postcondition of
pusha(x) is and ; pushb(x) symmetrically gives and . The precondition of popa is , and its postcondition is return value , new state , and unchanged . The specification for popb exchanges . Each emptiness query leaves both lists unchanged and returns exactly whether its corresponding list is empty.These frame conditions matter: an operation on one stack must preserve the other, not merely produce the right return value.
New to topics? Read the docs here!