In Haskell two features of the language are commonly used to implement abstract data types.
Features of Algebraic Data Types
Features of Abstract Data Types
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.
push :: a -> Stack a -> Stack a top :: Stack a -> a pop :: Stack a -> Stack a emptyStack:: Stack a empty :: Stack a -> Bool
enQ :: a -> Queue a -> Queue a deQ :: Queue a -> (a,Queue q) emptyQ :: Queue a isEmpty:: Queue a -> Bool
add :: a -> PQ a -> PQ a min :: PQ a -> Maybe(a) deletemin:: PQ a -> Maybe(PQ a) emptyPQ :: PQ a -> Bool
(+):: Num t => t -> t -> t (*):: Num t => t -> t -> t 0:: Num t -> tetc.
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
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
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:
pop(push a s) == s top(push a s) == a not((push a s) == emptyStack)
fst(deQ(enQ a emptyQ)) == a deQ(enQ a (enQ b q)) == deQ(enQ b q)
Back to the Daily Record.
Back to the class web-page.