[Haskell-cafe] data vs newtype (was: OOP'er with (hopefully) trivial questions)

Derek Elkins derek.a.elkins at gmail.com
Mon Dec 17 13:11:14 EST 2007


On Mon, 2007-12-17 at 13:51 +0000, Bayley, Alistair wrote:
> > From: haskell-cafe-bounces at haskell.org 
> > [mailto:haskell-cafe-bounces at haskell.org] On Behalf Of Nicholls, Mark
> > 
> > To recap...
> > 
> > "type" introduces a synonym for another type, no new type is
> > created....it's for readabilities sake.
> > 
> > "Newtype" introduces an isomorphic copy of an existing type...but
> > doesn't copy it's type class membership...the types are
> > disjoint/distinct but isomorphic (thus only 1 constructor param).
> > 
> > "data" introduces a new type, and defines a composition of existing
> > types to create a new one based on "->" and "(".
> > 
> > "class" introduces a constraint that any types declaring themselves to
> > be a member of this class...that functions must exist to satisfy the
> > constraint.
> 
> 
> As an aside, I was wondering exactly what the differences are between
> newtype and data i.e. between
> 
> > newtype A a = A a
> 
> and
> 
> > data A a = A a
> 
> According to:
>   http://www.haskell.org/onlinereport/decls.html#sect4.2.3
> newtype is, umm, stricter than data i.e. newtype A undefined =
> undefined, but data A undefined = A undefined. Other than that, newtype
> just seems to be an optimization hint. Is that a more-or-less correct
> interpretation?

More less than more.  There is a context that can distinguish a newtype
from a data type.  This is explained on this wiki page that addresses
exactly this question. http://www.haskell.org/haskellwiki/Newtype



More information about the Haskell-Cafe mailing list