2
votes

I'm not really getting how to work with modules in Haskell, I'm really new to this language and so far I only know the most basic of things, such as creating a function and that sort of stuff. Right now I'm getting an error that says

Not in scope: data constructor 'Mat'

This is supposed to be the constructor for a newtype definition of a matrix. This is the module:

module Matrix (Matrix, fillWith, fromRule, numRows, numColumns, at, mtranspose, mmap) where
newtype Matrix a = Mat ((Int,Int), (Int,Int) -> a)

fillWith :: (Int,Int) -> a -> (Matrix a)
fillWith (n,m) k = Mat ((n,m), (\(_,_) -> k))

fromRule :: (Int,Int) -> ((Int,Int) -> a) -> (Matrix a)
fromRule (n,m) f = Mat ((n,m), f)

numRows :: (Matrix a) -> Int
numRows (Mat ((n,_),_)) = n

numColumns :: (Matrix a) -> Int
numColumns (Mat ((_,m),_)) = m

at :: (Matrix a) -> (Int, Int) -> a
at (Mat ((n,m), f)) (i,j)| (i > 0) && (j > 0) || (i <= n) && (j <= m) = f (i,j)

mtranspose :: (Matrix a) -> (Matrix a)
mtranspose (Mat ((n,m),f)) = (Mat ((m,n),\(j,i) -> f (i,j)))

mmap :: (a -> b) -> (Matrix a) -> (Matrix b)
mmap h (Mat ((n,m),f)) = (Mat ((n,m), h.f))

I'm calling it on my own module in this way:

module MatrixShow where
    import Matrix


    instance Matrix (Show a) => Show (Matrix a) where 
        show Mat ((x,y),(a,b)) = show 1

The show 1 is just a test. I'm not even sure what that

instance Matrix (Show a) => Show (Matrix a) means at all, they just gave us this code and then told us to figure it out without explaining what is happening in any of these things.

If anyone can help me I'd appreciate. I'm guessing that printing the contents of a matrix is very basic in Haskell, and I'm sure I'm making it more difficult than it should be, but still as a newcomer to this language I'm not really sure what I'm doing sometimes.

1

1 Answers

5
votes

Export the constructor:

module Matrix (Matrix(..), fillWith, fromRule, -- etc.
                --   ^^^^

By default, only the type is exported, preventing the other modules to access the constructor.

The line

instance Matrix (Show a) => Show (Matrix a) where

looks wrong to me. Is there some Matrix class around? More likely, it should read

instance (Show a) => Show (Matrix a) where

Also, the line

show Mat ((x,y),(a,b)) = show 1

is wrong. Its left hand side should look like

show (Mat ((x,y), f)) = ...