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 -- 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
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?
This is not a bug, it's a Feature[TM]! Notice the prompt says "Stacks>". This means that you are "in" the module Stacks, and so can see everything visible to a top-level function in the module Stacks. If you write another file, say Main.hs, containing just the single line "import Stacks", then do ":l Main", you will find Hugs session for: /usr/groups/haskell/lib/hugs/lib/Prelude.hs Stacks.hs Main.hs Type :? for help Main> StackC [1] ERROR: Undefined constructor function "StackC" Main> which I think is what you wanted. HTH. --KW 8-) -- Keith Wansbrough <kw217@cl.cam.ac.uk> http://www.cl.cam.ac.uk/users/kw217/ Cambridge University Computer Laboratory.
Keith Wansbrough wrote:
This is not a bug, it's a Feature[TM]! Notice the prompt says "Stacks>".
Minutes after I sent you my message I noticed that and I supposed your answer was going to be that :-)
which I think is what you wanted.
Exactly. Again, thank you very much. -- Angel Herranz-Nieva (FI-UPM) pho: +34 913367452 mailto:aherranz@fi.upm.es http://lml.ls.fi.upm.es/~angel
participants (2)
-
Keith Wansbrough -
Ángel Herranz-Nieva