Difference between revisions of "Unboxed type"

From HaskellWiki
Jump to navigation Jump to search
(→‎See Also: fix link)
Line 5: Line 5:
 
[[Category:Language]]
 
[[Category:Language]]
   
  +
Since Haskell values may contain unevaluated thunks in addition to specific
==From the old Wiki==
 
  +
values, in general values must be represented by a pointer to a
  +
heap-allocated object. This is fairly slow so compilers attempt to replace
  +
these boxed values with unboxed raw values when possible. Unboxed values are
  +
a feature of some compilers that allows directly manipulating these low
  +
level values. Since they behave differently than normal haskell types,
  +
generally the type system is extended to type these unboxed values. how it
  +
is done is compiler specific.
   
  +
[[Compiler Specific]]
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.
 
   
  +
[[GHC]]
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 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> or
  +
<hask>($)</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.
+
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>
 
<haskell>
Line 20: Line 33:
 
showUnboxedInt n = (show $ I# n) ++ "#"
 
showUnboxedInt n = (show $ I# n) ++ "#"
 
</haskell>
 
</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.
+
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.
  +
  +
[[JHC]]
  +
  +
Jhc unboxed values behave similarly to ghc but with some differences, jhc
  +
doesn't allow the # in identifiers so by convention uses a trailing
  +
underscore to indicate an unboxed type. However it does use the trailing
  +
hash for unboxed literals like ghc.
  +
  +
In addition jhc allows a limited polymorphism on unboxed values, they may be
  +
used polymorphically but if an exact type is not determined at the end of
  +
typechecking, they are defaulted to specific unboxed types. So 1# can be a
  +
Bits8_, Int_ or Bool_. The rules for when polymorphic unboxed types may be
  +
used without annotation are the same as for when rank n types can be used.
   
 
=== Unboxed Tuples and Arrays ===
 
=== Unboxed Tuples and Arrays ===
   
  +
Unboxed tuples use the syntax (# a,b,c #) and may not be assigned to values, they must be immediately scrutinized or used.
...
 
   
 
=== When to use Unboxed Types ===
 
=== When to use Unboxed Types ===

Revision as of 22:54, 23 May 2014

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.

Since Haskell values may contain unevaluated thunks in addition to specific values, in general values must be represented by a pointer to a heap-allocated object. This is fairly slow so compilers attempt to replace these boxed values with unboxed raw values when possible. Unboxed values are a feature of some compilers that allows directly manipulating these low level values. Since they behave differently than normal haskell types, generally the type system is extended to type these unboxed values. how it is done is compiler specific.

Compiler Specific

GHC 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 or ($) 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.

JHC

Jhc unboxed values behave similarly to ghc but with some differences, jhc doesn't allow the # in identifiers so by convention uses a trailing underscore to indicate an unboxed type. However it does use the trailing hash for unboxed literals like ghc.

In addition jhc allows a limited polymorphism on unboxed values, they may be used polymorphically but if an exact type is not determined at the end of typechecking, they are defaulted to specific unboxed types. So 1# can be a Bits8_, Int_ or Bool_. The rules for when polymorphic unboxed types may be used without annotation are the same as for when rank n types can be used.

Unboxed Tuples and Arrays

Unboxed tuples use the syntax (# a,b,c #) and may not be assigned to values, they must be immediately scrutinized or used.

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 :)