[Haskell-cafe] a really juvenile question .. hehehehe ;^)

Jason Dagit dagit at codersbase.com
Mon Oct 6 03:10:41 EDT 2008


2008/10/5 Galchin, Vasili <vigalchin at gmail.com>

> ok ... by using "newtype", we are constricting/constraining to a subset of
> CInt .. e.g. something like a "subtype" of CInt?? (where by "subtype", I
> mean like the notion of subtype in languages like Ada). For our audience,
> can you perhaps distinguish (in a typeful way) between the Haskell notion of
> "type", "newtype" and "data"? Or maybe let's distinguish between these
> notions not only in a typeful manner, but also in a historical motivation?
> .. ...  motivations are always IMO very, very enlightening!
>

If you like historical perspective check out this:
http://research.microsoft.com/~simonpj/papers/history-of-haskell/index.htm

type is similar to typedef in C.  That is, it's more or less just for
renaming.  I say "more or less" because I'm not sure exactly how the
"renaming" works in the presence of rank-2 and higher types.

data is essentially for declaring new data structures.  As a result of this
it also defines a new type.

newtype is for reusing existing types or data structures but with a new
distinct type.

data and newtype vary in one more subtle way, and that's how/when they
evaluate to bottom.  Most of the time they behave identically, but in the
right cases they act sightly differently.  newtype is usually regarded as
more efficient than data.  This is because the compiler can choose to
optimize away the newtype so that it only exists at type check time.  I
think this is also possible with data in some, but not all, uses.

Jason
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20081006/3ec7357b/attachment.htm


More information about the Haskell-Cafe mailing list