Question

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.

Was it helpful?

Solution

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 ]) ]

OTHER TIPS

The following file compiles here:

{-# LANGUAGE GeneralizedNewtypeDeriving, TypeFamilies #-}

class C a where g :: a
type family X
type instance X = Int
newtype NewX = NewX X deriving Num
instance C NewX where g = 43
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top