Take the 2-minute tour ×

I found answers explaining difference between newtype and data in Haskell. But if I have the following type synonym:

type Point = (Int,Int)

Would that be more efficient rather to use:

data Point = Pt (Int,Int) ?
share|improve this question
see stackoverflow.com/questions/5889696/…, in particular the representation part. –  Jeff Foster Jun 8 '11 at 11:00
I checked that question before putting this one. The other question is about different between newtype and data NOT between type and data –  vis Jun 8 '11 at 11:08
Wrapping the data constructor Pt with a pair (,) in data Point = Pt (Int,Int) is inefficient. You are better to use data Point = Pt Int Int. In some cases you might opt for strict Ints as well. –  stephen tetley Jun 8 '11 at 11:26
@stephen: and how does this compare to the type synonym? –  vis Jun 8 '11 at 11:51
add comment

2 Answers

up vote 7 down vote accepted

Using type will be more efficient, as it incurs one less indirection than the data version.

Note that both are more inefficient than:

data Point = Point {-# UNPACK #-}!Int {-# UNPACK #-}!Int

as you can see from this earlier question on data representations.

share|improve this answer
wonder if this is more efficient, why isn't it default in the language –  vis Jun 8 '11 at 15:57
Changes the semantics, and makes some programs unimplementable. Defaults /= Most Efficient. Instead, Most General. –  Don Stewart Jun 8 '11 at 16:05
With some exceptions (note length verses genericLength) –  Thomas M. DuBuisson Jun 8 '11 at 17:43
Am i right to say data Point = Point {-# UNPACK #-}!(Int,Int) that is single constructor is better than data Point = Point {-# UNPACK #-}!Int {-# UNPACK #-}!Int? –  vis Jun 10 '11 at 12:10
No, that's a worse representation, as the tuple is still a pair of pointers to Ints. See the data representation link above. –  Don Stewart Jun 10 '11 at 13:07
add comment


The Pt construction adds one word of overhead (in GHC) and the field (i.e. the pair) is stored as a pointer to a pair, adding one additional word, for a total of two words overhead (and an extra indirection to get to the values).

I recommend that you either use the type synonym or, better yet, define

data Point = Pt {-# UNPACK #-} !Int {-# UNPACK #-} !Int

This requires 4 words less than

type Point = (Int, Int)

and uses one less level of indirections (pointers).

share|improve this answer
add comment

Your Answer


