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 7645281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:54:05+00:00 2026-05-31T09:54:05+00:00

I have this code: type family Id obj :: * type instance Id Box

  • 0

I have this code:

type family Id obj :: *
type instance Id Box = Int

And I want to make it so I can always get an Int from the Id type family. I recognize that a conversion will be required.

I thought maybe creating a class would work:

class IdToInt a where
  idToInt :: Id a -> Int

instance IdToInt Box where
  idToInt s = s

And that actually compiles. But when I try to use it:

testFunc :: Id a -> Int
testFunc x = idToInt x

I get error:

src/Snowfall/Spatial.hs:29:22:
Couldn't match type `Id a0' with `Id a'
NB: `Id' is a type function, and may not be injective
In the first argument of `idToInt', namely `x'
In the expression: idToInt x
In an equation for `testFunc': testFunc x = idToInt x

So, how can I create a conversion for a type family Id to get an Int?

Based on the answer by ehird, I tried the following but it doesn’t work either:

class IdStuff a where
  type Id a :: *
  idToInt :: Id a -> Int

instance IdStuff Box where
  type Id Box = Int
  idToInt s = s

testFunc :: (IdStuff a) => Id a -> Int
testFunc x = idToInt x

It gives error:

src/Snowfall/Spatial.hs:45:22:
Could not deduce (Id a0 ~ Id a)
from the context (IdStuff a)
  bound by the type signature for
             testFunc :: IdStuff a => Id a -> Int
  at src/Snowfall/Spatial.hs:45:1-22
NB: `Id' is a type function, and may not be injective
In the first argument of `idToInt', namely `x'
In the expression: idToInt x
In an equation for `testFunc': testFunc x = idToInt x
  • 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-31T09:54:06+00:00Added an answer on May 31, 2026 at 9:54 am

    As others have pointed out, the problem is that the compiler can’t figure out which a to use. Data families are one solution, but an alternative that’s sometimes easier to work with is to use a type witness.

    Change your class to

    class IdToInt a where
      idToInt :: a -> Id a -> Int
    
    instance IdToInt Box where
      idToInt _ s = s
    
    -- if you use this a lot, it's sometimes useful to create type witnesses to use
    box = undefined :: Box
    
    -- you can use it like
    idToInt box someId
    
    -- or
    idToInt someBox (getId someBox)
    

    The question you need to answer is, for any given Id, is there only one type a it should appear with? That is, is there a one to one correspondence between as and Id as? If so, data families are the correct approach. If not, you may prefer a witness.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code that I want to look like this: List<Type> Os; ... foreach
I have this code here, which is intended to allow any type of arguments:
I wrote this code I have these errors Cannot implicitly convert type x.Program.TreeNode' to
I have seen code like this (actually seeing another person type it up): catch
The following code fails in 'Evaluate' with: This expression was expected to have type
I have this code in jQuery, that I want to reimplement with the prototype
I have this code #include <iostream> using namespace std; int main(int argc,char **argv) {
How can I have this other box display at the bottom of the first
I have this jquery for my menu buttons, what I want is to get
I have a data frame of the form: Family Code Length Type 1 A

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.