Abstract Data Types


General Properties

An abstract datatype is an encapsulation mechanism. In general it is composed of several components It allows programmers to hide the details of an implementation, and to implement multiple different versions that might behave differently, especially with respect to resources used.

An example

In Haskell two features of the language are commonly used to implement abstract data types.


Comparing Abstract Data Types with Algebraic Data Types.

Features of Algebraic Data Types

Features of Abstract Data Types


Components of Abstract Data Types

Abtsratc data types are often described as having severaol components


Signatures

A Signature is a set of contracts (or types) that the operation of the abstract data type must export (i.e. provide implementations for). In some systems the signature also includes axioms that all implementations must obey.

Examples

Implementing using the class system is done through class and instance declarations.
class Stacklike t where
  push  :: a -> t a -> t a
  top   :: t a -> a
  pop   :: t a -> t a
  emptyStack:: t a
  empty :: t a -> Bool
Every instance is a separate implementation.
instance Stacklike [] where
  push x xs = x :xs
  top (x:xs) = x
  pop (x:xs) = xs
  emptyStack = []
  empty [] =  True
  empty (x:xs) = False

Implementing using the module system is done through manipulating what information is exported from a file.
module Stack(Stack(),push,top,pop,emptyStack,empty) where

data Stack a = EmptyStack | Push a (Stack a)

push  :: a -> Stack a -> Stack a
push x xs = Push x xs

top   :: Stack a -> a
top (Push x xs) = x

pop   :: Stack a -> Stack a
pop (Push x xs) = xs

emptyStack:: Stack a
emptyStack = EmptyStack

empty :: Stack a -> Bool
empty = EmptyStack
The Stack() means that the the type Stack is exported, but the constructors are not exported.


Algebraic Specification

An algebraic specification describes how an implementation should behave. It is generally written as a set of equivalencies. I.e. equations that state that two programs are equal. Normally equality between programs is described in terms of observational equivalence.

Two programs are observational equivalent if there are no program contexts that can distinguish them. A context is an additional set of commands or expressions surrounding the programs.

In terms of abstract data types contexts are often described as a series of calls to the operations or methods of the abstract type.

Some example axiomatic specifications follow:

Back to the Daily Record.

Back to the class web-page.