[Haskell-cafe] Abstraction in data types

Luke Palmer lrpalmer at gmail.com
Thu Mar 18 14:25:56 EDT 2010


On Thu, Mar 18, 2010 at 12:17 PM, John Meacham <john at repetae.net> wrote:
> On Wed, Mar 17, 2010 at 09:20:49PM -0700, Darrin Chandler wrote:
>> data Point    = Cartesian (Cartesian_coord, Cartesian_coord)
>>               | Spherical (Latitude, Longitude)
>
> Just a quick unrelated note, though you are probably aware of this,
> doing
>
>> data Foo = Foo (X,Y)
> means something subtly different than
>> data Foo = Foo X Y
> and can be less efficient.

On the other hand, the latter is equivalent to:

newtype Foo = Foo (X,Y)

> A quick way to see they are different is to count the bottoms,
>
> in the first case (where _ is bottom and X is some value)
> you have the cases
>> Foo _
>> Foo (_,_)
>> Foo (X,_)
>> Foo (_,X)
>> Foo (X,X)
> and in the other case you have
>> Foo _ _
>> Foo X _
>> Foo _ X
>> Foo X X
>
> so one has 5 distinct values, and the other has 4, hence they are not
> isomorphic. All things being equal, this means the second case will be
> more efficient as there is one less case it needs to distinguish (every
> potential bottom implys an 'eval' somewhere). Depending on your code,
> all things may not be equal and there are rare times when the tupled
> version is more efficient however.
>
>        John
>
>
> --
> John Meacham - ⑆repetae.net⑆john⑈ - http://notanumber.net/
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>


More information about the Haskell-Cafe mailing list