Let's say I want to write a Sudoku solution with some representative abstraction using typeclasses. So I would like to make typeclass for row and matrix:
{-
Obviously, I would add more, but these functions are enough for me to have problems. Now let's say that I want to implement this using nested lists. Attempt:
instance Row r => Sudoku [r] where row ns = s !! (n - 1)
lands in hot water:
Couldn't match expected type `r1' against inferred type `r' `r1' is a rigid type variable bound by the type signature for `row' at 96b.hs:7:14 `r' is a rigid type variable bound by the instance declaration at 96b.hs:12:13 In the expression: s !! (n - 1) In the definition of `row': row ns = s !! (n - 1) In the instance declaration for `Sudoku [r]'
Second hit with:
instance Row [Int] where r ! n = r !! (n - 1) instance Sudoku [[Int]] where row ns = s !! (n - 1)
tariffs are not better:
Couldn't match expected type `r' against inferred type `[Int]' `r' is a rigid type variable bound by the type signature for `row' at 96b.hs:8:14 In the expression: s !! (n - 1) In the definition of `row': row ns = s !! (n - 1) In the instance declaration for `Sudoku [[Int]]'
Something seems to be missing. What is the correct way to simulate a simple scenario like this?
source share