Abstract data types
Ángel Herranz-Nieva
aherranz@fi.upm.es
Tue, 31 Oct 2000 12:27:54 +0100
This is a multi-part message in MIME format.
--Boundary_(ID_+U6u5xKFziZnAjYAXzE/7w)
Content-type: text/plain; charset=us-ascii
Content-transfer-encoding: 7bit
Hello,
we give a course on data structures at School of Computer
Science (Technical University of Madrid) and we use Hugs as
the Haskell interpreter in such a course. Our problem has to
do with hiding concrete types representing abstract types.
I have attached our module for the abstract data type Stack
(we follow the Haskell 98 report and don't want to go into
1.4 compatibility). We expected the representation of stacks
were hidden but Hugs let us write an expression with the
"hidden" constructor StackC:
Stack> :l Stacks
Reading file "Stacks.hs":
Parsing....................
Dependency analysis........
Type checking..............
Compiling..................
Hugs session for:
/opt/lib/hugs/lib/Prelude.hs
Stacks.hs
Stacks> StackC [1]
StackC [1]
Stacks>
This problem occurs only in the top level, if we program a
client module importing the module Stacks and we try to
write a expression using directly StackC then Hughs gives an
error message (great!).
This is a bug in the top level, isn't it?
Thank you very much in advance.
--
Angel Herranz-Nieva (FI-UPM)
pho: +34 913367452
mailto:aherranz@fi.upm.es
http://lml.ls.fi.upm.es/~angel
--Boundary_(ID_+U6u5xKFziZnAjYAXzE/7w)
Content-type: text/plain; charset=us-ascii; name=Stacks.hs
Content-transfer-encoding: 7bit
Content-disposition: inline; filename=Stacks.hs
-- Stacks.hs: an abstract data type for stacks
module Stacks (Stack, emptyStack, push, pop, topOf, isEmpty) where
newtype Stack a = StackC [a]
deriving Show
emptyStack :: Stack a
push :: a -> Stack a -> Stack a
pop :: Stack a -> Stack a
topOf :: Stack a -> a
isEmpty :: Stack a -> Bool
emptyStack = StackC []
push x (StackC p) = StackC (x:p)
pop (StackC []) = error "pop: empty stack"
pop (StackC (_:xs)) = StackC xs
topOf (StackC []) = error "topOf: empty stack"
topOf (StackC (x:_)) = x
isEmpty (StackC p) = null p
--Boundary_(ID_+U6u5xKFziZnAjYAXzE/7w)--