Matrix exercise - exercism test - all tests pass - submit gives error

bash $ exercism test
Running tests via stack test

matrix-1.3.0.9: unregistering (local file changes: src/Matrix.hs)
matrix> build (lib + test) with ghc-9.2.7
Preprocessing library for matrix-1.3.0.9…
Building library for matrix-1.3.0.9…
[2 of 2] Compiling Matrix
Preprocessing test suite ‘test’ for matrix-1.3.0.9…
Building test suite ‘test’ for matrix-1.3.0.9…
[1 of 2] Compiling Main [Matrix changed]
Linking .stack-work/dist/x86_64-linux/ghc-9.2.7/build/test/test …
matrix> copy/register
Installing library in /home/colin/exercism/haskell/matrix/.stack-work/install/x86_64-linux/47afc0288963800c23584c154290d38e772ba14b41ba2c472dc4ee37d127b285/9.2.7/lib/x86_64-linux-ghc-9.2.7/matrix-1.3.0.9-37rWMrdQUnJ8cvvQeI0p4h
Registering library for matrix-1.3.0.9…
matrix> test (suite: test)

extract row from one number matrix [:heavy_check_mark:]
can extract row [:heavy_check_mark:]
extract row where numbers have different widths [:heavy_check_mark:]
can extract row from non-square matrix with no corresponding column [:heavy_check_mark:]
extract column from one number matrix [:heavy_check_mark:]
can extract column [:heavy_check_mark:]
can extract column from non-square matrix with no corresponding row [:heavy_check_mark:]
extract column where numbers have different widths [:heavy_check_mark:]
shape [:heavy_check_mark:]
rows & cols [:heavy_check_mark:]
eq [:heavy_check_mark:]
fromList [:heavy_check_mark:]
transpose [:heavy_check_mark:]
reshape [:heavy_check_mark:]
shape of a reshaped matrix should match specified shape [:heavy_check_mark:]
+++ OK, passed 100 tests.
flatten [:heavy_check_mark:]

Finished in 0.0054 seconds
16 examples, 0 failures

matrix> Test suite test passed
Completed 2 action(s).

After submit via cli tests reports

An error occurred while running your tests. This might mean that there was an issue in our infrastructure, or it might mean that you have something in your code that’s causing our systems to break.

Please check your code, and if nothing seems to be wrong, try running the tests again.


I have checked my code, and nothing seems to be wrong. I tried resubmitting but obtained the same error.

Could you share your solution?

Of course:

module Matrix
    ( Matrix
    , cols
    , column
    , flatten
    , fromList
    , fromString
    , reshape
    , row
    , rows
    , shape
    , transpose
    ) where

import qualified Data.List as L
import qualified Data.Vector as V
import Data.Vector (Vector)

data Matrix a = Matrix [[a]] deriving (Eq, Show)

cols :: Matrix a -> Int
cols (Matrix ([])) = 0
cols (Matrix (x:_)) = length x

column :: Int -> Matrix a -> Vector a
column x matrix = row x $ transpose matrix

flatten :: Matrix a -> Vector a
flatten (Matrix xss) = V.fromList (concat xss)

fromList :: [[a]] -> Matrix a
fromList = Matrix

fromString :: Read a => String -> Matrix a
fromString xs = Matrix (stringToInts xs)

reshape :: (Int, Int) -> Matrix a -> Matrix a
reshape (r,c) (Matrix xss)
  | r < 0 || c < 0 = error "reshape: negative dimension"
  | length flat /= r * c = error "reshape: not corrent number of elements"
  | otherwise = Matrix (chunksOf c flat)
  where
    flat = concat xss

    chunksOf :: Int -> [a] -> [[a]]
    chunksOf _ [] = []
    chunksOf n xs
      | n <= 0 = error "chunksOf: non-positive chunk size"
      | otherwise = let (h, t) = splitAt n xs in h : chunksOf n t

row :: Int -> Matrix a -> Vector a
row x (Matrix xss) = V.fromList (xss !! (x - 1))

rows :: Matrix a -> Int
rows (Matrix xss) = length xss

shape :: Matrix a -> (Int, Int)
shape matrix = (rows matrix, cols matrix)

transpose :: Matrix a -> Matrix a
transpose (Matrix xss) = Matrix (L.transpose xss)

stringToInts :: Read a => String -> [[a]]
stringToInts =
  map (map read .words) . lines

The initial stub and the example solution are also error-ing here. The dependencies and imports look ok at a glance.

One thing of note is that this exercise (I think all of them in the repo) lists the resolver as lts-20.18, but the test-runner is now using lts-22.44. That might explain the difference between running the tests locally and online.

On the server, we have about 20s to compile and run the tests. I asked an LLM why matrix takes longer.

There’s a package called matrix in Stackage lts-22.44. When the exercise’s local
package is also named matrix, stack shadows/drops that snapshot package, which
changes the computed snapshot-install hash. …, so the matrix build gets a
brand-new empty snapshot dir … and recompiles ~55 dependency packages
(QuickCheck, aeson, vector, hspec…) from scratch.

Three exercises collide with Stackage lts-22.44 package names and all suffer the same ~60s recompile:

  • matrix (collides with the matrix math library)
  • binary-search (collides with the binary-search package)
  • say (collides with the say package)
3 Likes
» for i in matrix anagram say darts; do echo $i; time bash ../test_examples $i; done
matrix
matrix: testing...
matrix: done

real    1m39.591s
matrix: pass

---

anagram
anagram: testing...
anagram: done

real    0m5.679s
anagram: pass

---

say
say: testing...
say: done

real    1m37.034s
say: pass

---

darts
darts: testing...
darts: done

real    0m5.323s
darts: pass

Checks out. Those exercises might just need package renaming.

Module names in the track could append or prepend some text that is unlikely to generate a conflict in the future. For example exercismMatrix.

Can someone please summarise why my submission did not pass?

There is an issue with the test runner causing it to time out on three specific exercises.

Thank you Isaac. Not much I can do about that then.