[Haskell-cafe] Re: ANN: First Monad Tutorial of the Season

Don Stewart dons at galois.com
Mon Aug 25 14:49:18 EDT 2008


ryani.spam:
> On Mon, Aug 25, 2008 at 5:33 AM, Hans van Thiel <hthiel.char at zonnet.nl> wrote:
> > The books I use for reference, the Craft and SOE, don't seem to mention
> > this. I have to confess, I don't really understand the difference
> > between newtype and data. Again, an explanation would be appreciated.
> 
> A newtype has no run-time representation; it is an entity at
> type-checking time, nothing more.  data, on the other hand, has a
> runtime representation.
> 
> > data D = D Int
> > newtype N = N Int
> 
> > d1 = D 1
> > n1 = N 1
> > i1 = 1 :: Int
> 
> In memory at runtime, these look different; n1 looks exactly the same
> as i1.  We can represent them both in this way:
> 
> lit = 1 :: Int#  (unlifted, unboxed int)
> i1 = { tag = I#, content -> lit }
> n1 = { tag = I#, content -> lit }
> 
> But d1 is different; it looks like this:
> 
> d1 = { tag = D, content -> i1 }
> 
> There's an extra level of indirection between the "data" version and
> the contents.  What does this mean?  Well, in particular, you get
> different behavior in a couple of cases when dealing with "error" (or
> infinite loops):

Though the D tag will be represented as a bit set on the bottom of the
thunk pointer in GHC (useful to remember). 'data' tags are cheap.


More information about the Haskell-Cafe mailing list