# Unboxed type

### From HaskellWiki

(Difference between revisions)

(→See Also: fix link) |
|||

(5 intermediate revisions by 3 users not shown) | |||

Line 1: | Line 1: | ||

− | '''Unboxed types''' represent raw values. Unboxed types have [[kind]] <TT>#</TT>. |
+ | '''Unboxed types''' are [[type]]s that represent raw values. Unboxed types have [[kind]] <TT>#</TT>. |

− | Note that unboxed types of different storage behaviours are all lumped together under kind <tt>#</tt>. As a result, [[type variable]]s must have kinds which are <tt>#</tt>-free. |
+ | Note that unboxed types of different storage behaviours (four bytes, eight bytes etc.) are all lumped together under kind <tt>#</tt>. As a result, [[type variable]]s must have kinds which are <tt>#</tt>-free. |

[[Category:Language]] |
[[Category:Language]] |
||

+ | |||

+ | ==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 <code>42</code> is <code>42#</code>. There are some restrictions to their use. In particular, you can't pass them to polymorphic functions (like <hask>show</hask> for instance). |
||

+ | |||

+ | In this example, <hask>I#</hask> is a constructor that takes an unboxed integer and returns the <hask>Int</hask> that we know and love. |
||

+ | |||

+ | <haskell> |
||

+ | module Main where |
||

+ | import GHC.Exts |
||

+ | |||

+ | showUnboxedInt :: Int# -> String |
||

+ | showUnboxedInt n = (show $ I# n) ++ "#" |
||

+ | </haskell> |
||

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

+ | |||

+ | === Unboxed Tuples and Arrays === |
||

+ | |||

+ | ... |
||

+ | |||

+ | === When to use Unboxed Types === |
||

+ | |||

+ | ... |
||

+ | |||

+ | === See Also === |
||

+ | |||

+ | #See the [http://www.haskell.org/ghc/docs/latest/html/users_guide/primitives.html discussion on primitives] in the [http://www.haskell.org/ghc/docs/latest/html/users_guide/ GHC's User's Guide]. |
||

+ | #See the [http://www.haskell.org/ghc/docs/latest/html/libraries/base/GHC-Exts.html GHC.Exts] module. |
||

+ | #See SPJ's paper [http://research.microsoft.com/Users/simonpj/Papers/unboxed-values.ps.Z Unboxed values as first class citizens]. |
||

+ | |||

+ | ---- |
||

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

## Revision as of 20:59, 21 March 2011

**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.

## Contents |

## 1 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

I#

Int

module Main where import GHC.Exts showUnboxedInt :: Int# -> String showUnboxedInt n = (show $ I# n) ++ "#"

n

I#

Int

### 1.1 Unboxed Tuples and Arrays

...

### 1.2 When to use Unboxed Types

...

### 1.3 See Also

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

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