Unboxed type

From HaskellWiki
Revision as of 23:38, 23 August 2007 by Weihu (talk | contribs) (Fixed a link)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Unboxed types are types that represent raw values. Unboxed types have kind #.

Note that unboxed types of different storage behaviours (four bytes, eight bytes etc.) are all lumped together under kind #. As a result, type variables must have kinds which are #-free.

From the old Wiki

One might imagine numbers naively represented in Haskell "as pointer to a heap-allocated object which is either an unevaluated closure or is a "box" containing the number's actual value, which has now overwritten the closure" [3. below]. GHC (and other implementations?) allow direct access to the value inside the box, or "Unboxed" values.

In GHC, by convention(?), unboxed values have a hash mark as a suffix to their name. For instance, the unboxed reprsentation of 42 is 42#. There are some restrictions to their use. In particular, you can't pass them to polymorphic functions (like show for instance).

In this example, I# is a constructor that takes an unboxed integer and returns the Int that we know and love.

module Main where
import GHC.Exts

showUnboxedInt :: Int# -> String
showUnboxedInt n = (show $ I# n) ++ "#"

Here we wrap the unboxed Int n with the I# constructor and show the regular-old Int, whith a hash mark on the end.

Unboxed Tuples and Arrays

...

When to use Unboxed Types

...

See Also

  1. See the discussion on primitives in the GHC's User's Guide.
  2. See the GHC.Exts module.
  3. See SPJ's paper Unboxed values as first class citizens.

This page is a work in progress by IsaacJones. More input welcome :)