Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

haskell

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)
like image 392
Vektorweg Avatar asked Jan 15 '14 23:01

Vektorweg


1 Answers

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)}
like image 172
Daniel Gratzer Avatar answered Sep 19 '22 21:09

Daniel Gratzer