Haskell type types, understanding error messages

Trying to use Data.Has, I write the code as follows:

data Name = Name; type instance TypeOf Name = Text
type NameRecord = FieldOf Name;

I found:

instance I NameRecord where
  ...

Throws a compilation error, namely:

Invalid instance synonym type family

While:

instance (NameRecord ~ a) => I a where
  ...

It compiles fine.

I believe that the error is related to this ticket in the GHC, marked as invalid.

The response to the ticket says:

I am not sure what you are offering. We cannot automatically convert

instance C (Fam Int) -- (1)

at

instance (Fam Int ~ famint) => C famint -- (2)

This works if there is only one instance, but as soon as there are two such cases, they always overlap.

, , . , . ( ).

- , , , (1) , (2) , ?

+5
1

, (1) , (2) ; (type ExampleOfATypeSynonym = ...) , , , , :

-- (1)
class Foo a
type Bla = ()
instance Foo Bla

... :

-- (2)
class Foo a
type Bla = ()
instance (a ~ Bla) => Foo a

, (1) , , , : , type B = A instance Foo B, , Foo A. , instance Foo A , , , .

, , , NameRecord. , FieldOf Name, ; , " " , FieldOf Name " " Name :> Text . , "" .

GHC.


, "... (2) , (1) ..."

, :

class Foo a where
  foo :: a

:

 instance Foo Int where
   foo = 0

 instance Foo Float where
   foo = 0

 main :: IO ()
 main = print (foo :: Float)

, . , :

{-# LANGUAGE FlexibleInstances, TypeFamilies #-}
class Foo a where
  foo :: a

instance (a ~ Int) => Foo a where
  foo = 0

instance (a ~ Float) => Foo a where
  foo = 0

main :: IO ()
main = print (foo :: Float)

; :

test.hs:5:10:
    Duplicate instance declarations:
      instance a ~ Int => Foo a -- Defined at test.hs:5:10-27
      instance a ~ Float => Foo a -- Defined at test.hs:8:10-29

, , . , Foo, . ?

GHC , ; .. =>. , "" =>, , . , :

instance Foo a where ...
instance Foo a where ...

, , .

+4

All Articles