Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6007609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:42:19+00:00 2026-05-23T01:42:19+00:00

Let’s say I want to write a Sudoku solver with some representational abstraction using

  • 0

Let’s say I want to write a Sudoku solver with some representational abstraction using typeclasses. So I’d like to make a typeclass for the row and the matrix:

{-# LANGUAGE FlexibleInstances #-}

class Row r where
  (!) :: r -> Int -> Int

class Sudoku a where
  row :: (Row r) => Int -> a -> r

Obviously, I would add more, but just these functions are enough to get me in trouble. Now let’s say I want to implement this with nested lists. Trying:

instance Row r => Sudoku [r] where
  row n s = s !! (n - 1)

lands me 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 n s = s !! (n - 1)
In the instance declaration for `Sudoku [r]'

A second stab with:

instance Row [Int] where
  r ! n = r !! (n - 1)

instance Sudoku [[Int]] where
  row n s = s !! (n - 1)

fares no 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 n s = s !! (n - 1)
In the instance declaration for `Sudoku [[Int]]'

I appear to be missing something. What’s the proper way of modelling a simple scenario like this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:42:19+00:00Added an answer on May 23, 2026 at 1:42 am

    Your Sudoku class does not indicate any relationship between a and r. It is currently saying that if you have a sudoku, you can get any type of row from it. Your instances only show how to get one specific type of row from a sudoku, so that doesn’t meet the requirement that any row type should work.

    There are two common ways to solve this. One way is to use type families to relate the row type to the sudoku type:

    {-# LANGUAGE TypeFamilies, FlexibleInstances #-}
    
    class Sudoku a where
        type RowType a :: *
        row :: Int -> a -> RowType a
    
    instance Row r => Sudoku [r] where
        type RowType [r] = r
        row n s = s !! (n - 1)
    

    You can also obtain the same result by using functional dependencies. We then add the row type as an additional parameter to the Sudoku class, and indicate the relationship that the sudoku determines the row type by using a functional dependency | a -> r:

    {-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies,
                 FlexibleInstances #-}
    
    class Row r where
      (!) :: r -> Int -> Int
    
    instance Row [Int] where
      r ! n = r !! (n - 1)
    
    class Sudoku a r | a -> r where
        row :: (Row r) => Int -> a -> r
    
    instance Row r => Sudoku [r] r where
        row n s = s !! (n - 1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a drive such as C:\ , and I want to
Let's say you have some products which have 2 fields, a description and a
Let's say i have an android device that has some extra buttons on it,
Let's say that I am using a 3rd party utility tool and I can't
Let's say you create a wizard in an HTML form. One button goes back,
Let's say I'm building a data access layer for an application. Typically I have
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say I'm writing a PHP (>= 5.0) class that's meant to be a
Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.