1

Part of Queue.hs:

module Queue ( Queue, emptyQueue, isEmptyQueue, enQueue, deQueue ) where

data Queue a = EmptyQueue | Single a | Q a ( Queue a ) a deriving (Show, Eq)

emptyQueue :: Queue a
emptyQueue = EmptyQueue

enQueue :: a -> Queue a -> Queue a
enQueue x EmptyQueue = Single x
enQueue x (Single q) = Q q (EmptyQueue) x
enQueue x (Q qf q qb) = Q qf (enQueue qb q) x

I was using print, see if them correct one by one. Now I want to test them using HUint.

Main.hs:

module Main where
import Queue 
import Test.HUnit

test1 = TestCase $ assertEqual "" (Single 'a'::Queue Char) $ enQueue 'a' emptyQueue
tests = TestList [TestLabel "test1" test1]

main = do runTestTT tests

Here's what I got:

*Queue> :r
[2 of 2] Compiling Main             ( Main.hs, interpreted )

Main.hs:5:36: Not in scope: data constructor ‘Single’
Failed, modules loaded: Queue.

So how can I fix it?

liubiantao
  • 1,311
  • 2
  • 12
  • 13
  • 1
    How are you exporting the `Queue` type from `Queue.hs`? If you have `module Queue (Queue) where ...` it wouldn't be exporting the constructors, you'd need `module Queue (Queue(..)) where` to export all constructors of the `Queue` type. – bheklilr Feb 13 '15 at 05:29
  • @bheklilr I've edit questions. – liubiantao Feb 13 '15 at 06:09
  • 1
    The error still isn't fixed. Add ``(..)`` to the second Queue in Queue.hs – ThreeFx Feb 13 '15 at 06:31
  • 1
    To complete the other two comments: `module Queue ( Queue(..), …)`. – Zeta Feb 13 '15 at 07:26

1 Answers1

1

Seems the Queue datatype should be abstract and opaque, so exposing all constructors is not a good idea. However, you can split your implementation into two submodules:

module Queue.Internal where --expose all internal stuff

data Queue = <...>


module Queue (Queue, emptyQueue, <...>) where --expose user API only

import Queue.Internal


module Queue.Tests where

import Queue.Internal --to get access to „guts“

test = <...>


module Main where

import Queue --to just use the type

Some library types (like ByteStrings) are made this way.

Yuuri
  • 1,858
  • 1
  • 16
  • 26
  • The file Queue.hs is assignment, only can add implementation below it. Seems ByteString is a good idea, I've tried it, but how could I convert the result of (enQueue 'a' emptyQueue) to ByteString? – liubiantao Feb 13 '15 at 14:55
  • @liubiantao I meant not to use ByteString, but to check how it is organized internally. Well, if you have only a single module, you have to export all `Queue` constructors (as it was already suggested) or write tests in the Queue module and export a test suit. – Yuuri Feb 14 '15 at 12:14