vek
vek

Reputation: 1543

Why do polymorphic type synonyms not work like polymorphic data declarations in instances?

Vec2 is working as data declaration, but i try to scrap my boilerplate with tuples in this scenario:

{-# LANGUAGE FlexibleInstances #-}

type Vec2 a = (a,a)

class Vector v where 
  foo :: v Integer

instance Vector Vec2 where 
  foo = (1,2)

Upvotes: 5

Views: 174

Answers (1)

daniel gratzer
daniel gratzer

Reputation: 53871

Type synonyms can't be partially applied. Since they are essentially type level functions, deciding equality between partially applied type synonyms is akin to deciding extensional equivalence.

You're well within your power to do something like

{-# LANGUAGE FlexibleInstances #-}
instance Foo String where
  ...

Since String is fully applied. There is a work around however, since types are curried, in some cases you can write things like

 type Arr = ((->) Int)

And create instances for this since the type synonym is "fully applied".

In this case clever eta conversion isn't possible so you're going to have to use a newtype,

newtype SimplePair a = SimplePair {unSimplePair :: (a, a)}

Upvotes: 5

Related Questions