Abstract Data Types
Abstract Data Types
Rational
/* value definition */
abstract typedef <integer, integer> RATIONAL;
condition RATIONAL[1] != 0;
/* operator definition */
abstract RATIONAL makerational(a,b)
int a, b;
precondition b != 0;
postcondition makerational[0] == a;
makerational[1] == b;
abstract RATIONAL add(a, b)
RATIONAL a, b;
postcondition add[0] == a[0]*b[1] + b[0]*a[1];
add[1] == a[1]*b[1];
abstract RATIONAL mult(a, b)
RATIONAL a, b;
postcondition mult[0] == a[0]*b[0];
mult[1] == a[1]*b[1];
abstract RATIONAL equal(a, b)
RATIONAL a, b;
postcondition equal == (a[0]*b[1] == b[0]*a[1]);
Array
/* value definition */
abstract typedef <<eltype, ub>> ARRTYPE(ub, eltype);
/* operator definition */
abstract eltype extract(a, i)
ARRTYPE(ub, eltype) a;
int i;
precondition 0 <= i < ub;
postcondition extract == ai;
abstract empty(l)
LIST(eltype) l;
postcondition: empty = (len(l) == 0);
abstract eltype remove(l) // Which type of remove is this? From head or from tail?
LIST(eltype) l;
precondition: empty(l) == FALSE;
postcondition: x = first(l);
l = sub(l, 1, len(l) – 1);
remove = x;
Stack
abstract typedef <<eltype>> STACK(eltype);
abstract empty(s)
STACK(eltype) s;
postcondition: empty == (len(s) == 0);
abstract eltype pop(s) // removing from the head of the sequence (LIFO)
STACK(eltype) s;
precondition: empty(s) == FALSE;
postcondition: pop = first(s);
s = sub(s, 1, len(s) – 1);
Queue
abstract typedef <<eltype>> QUEUE(eltype);
abstract empty(q)
QUEUE(eltype) q;
postcondition: empty == (len(q) == 0);
abstract enqueue(q, elt) // inserting at the end (tail) of the sequence (FIFO)
QUEUE(eltype) q;
eltypeelt;
postcondition: q = q + <elt>;
abstract eltype dequeue(q) // removing from the head of the sequence (FIFO)
QUEUE(eltype) q;
precondition: empty(q) == FALSE;
postcondition: remove = first(q);
q = sub(q, 1, len() – 1);