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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:30:05+00:00 2026-06-03T09:30:05+00:00

Lets say we have the following code: class C t where g :: t

  • 0

Lets say we have the following code:

class C t where
  g :: t

instance C Int where
  g = 42

Simple. We can also define functions on Int, like so:

f1 :: Int -> Int
f1 x = x * x

I’ve been working with type families, in particular because Data.Has uses them, and I want to insert them into an IxSet.

But here I’m going to present a simplified example. Lets say we want to define a new type X, that is similar to an Int. We could do this:

type family X
type instance X = Int

We can then define functions on X like so:

f2 :: X -> X
f2 x = x * x + 1

No problems so far. Now lets try to define an instance C X, like we did for C Int:

instance C X where
  g = 43

Uh oh, now we have the following error:

Illegal type synonym family application in instance: X
In the instance declaration for 'C X'

Now lets try something a bit different:

newtype NewX = NewX X

instance C NewX where
  g = 43

Now we’ve got another error, namely:

No instance for (Num NewX)
arising from the literal '43'

It seems like the newtype keyword eliminates any information about what classes the previous class belonged too. However, it also seems I can’t avoid newtype, as I can’t use type families in instance definitions.

Is there a better way to do this without having to rewrite instance definitions with additional explicit instances mentions which otherwise would be inferred?


Background information:

The reason why I need this to work is the following:

import Data.Has
import Data.IxSet

data Col1 = Col1; type instance TypeOf Col1 = Text
data Col2 = Col2; type instance TypeOf Col2 = Text

type Row = FieldOf Col1 :&: FieldOf Col2;

instance Indexable Row where
  empty = ixSet [ixFun $ (\x -> [ Col1 ^. x ]) ] -- Maybe add some more indexes later

This fails with:

Illegal type synonym family application in instance: Row
In the instance declaration for 'Indexable Row'

Making Row a newtype causes the following error:

No instance for (Contains (Labelled Col1 Text) Row)
arising from a use of `^.’
Possible fix:
add an instance declaration for
(Contains (Labelled Col1 Text) Row)

The only way I can work around this is by adding a long deriving clause as follows:

newtype Row = Row (FieldOf Col1 :&: FieldOf Col2)
  deriving 
  (
    Contains (Labelled Col1 Text), -- Add this for every column
    Contains (Labelled Col2 Text)  -- ...
  )

Even something which allows me to “typedef” Contains (Labelled x (TypeOf x)) to say HasCol x would be helpful.

  • 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-06-03T09:30:06+00:00Added an answer on June 3, 2026 at 9:30 am

    A newtype does just that–it defines a new type, while a type defines a synonym. If you dont like a bunch of deriving clauses, one can always use the isomorphism with the underlying type

    instance C NewX where
       g = NewX  43
    

    the reason type synonyms dont play nice with Instance declarations, is that functions (including type functions) only work in one direction. You can only pattern match on constructors, so newtype allows you to introduce a new type constructor at zero runtime cost. In your problem, why not

    newtype Row = Row {runRow :: FieldOf Col1 :&: FieldOf Col2}
    
    instance Indexable Row where
      empty = ixSet [ixFun $ (\x -> [ Col1 ^. (runRow x) ]) ]
    

    I should note that in general GeneralizedNewtypeDeriving is unsound. Doesn’t mean you should avoid using it, but does imply what you want is probably impossible.


    Edit (Question asker):

    Better yet, no need to even change the data type Row

    newtype Row = Row ( FieldOf Col1 :&: FieldOf Col2 )
    
    instance Indexable Row where
      empty = ixSet [ixFun $ (\(Row x) -> [ Col1 ^. x ]) ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say that I have the following code: public class Shelter<A extends Animal, B
Lets say I have the following code: public class Base { // Some stuff
Lets say I have the following code: public class Collection implements CollectionInterface{ ElementInterface[] elementArray
Let's say I have the following code: public class Foo { private int x;
Lets say I have the following code: abstract class Animal case class Dog(name:String) extends
Let's say, for instance, I have the following extremely simple window: <Window x:Class=CalendarGenerator.Window1 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
Lets say we have following sample code in C#: class BaseClass { public virtual
Lets say i have the following code. class A { function one() { return
Let's say we have the following code in a class: //class code TextBox t
Lets say I have the following code: import collections d = collections.OrderedDict() d['foo'] =

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.