https://wiki.haskell.org/api.php?action=feedcontributions&user=Cptwunderlich&feedformat=atomHaskellWiki - User contributions [en]2021-06-16T18:17:06ZUser contributionsMediaWiki 1.27.4https://wiki.haskell.org/index.php?title=Unboxed_type&diff=64061Unboxed type2021-03-18T22:31:43Z<p>Cptwunderlich: Updated dead link</p>
<hr />
<div>'''Unboxed types''' are [[type]]s that represent raw values. Unboxed types have [[kind]] <TT>#</TT>.<br />
<br />
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.<br />
<br />
[[Category:Language]]<br />
<br />
Since Haskell values may contain unevaluated thunks in addition to specific<br />
values, in general values must be represented by a pointer to a<br />
heap-allocated object. This is fairly slow so compilers attempt to replace<br />
these boxed values with unboxed raw values when possible. Unboxed values are<br />
a feature of some compilers that allows directly manipulating these low<br />
level values. Since they behave differently than normal haskell types,<br />
generally the type system is extended to type these unboxed values. how it<br />
is done is compiler specific.<br />
<br />
[[Compiler Specific]]<br />
<br />
[[GHC]]<br />
In GHC, by convention(?), unboxed values have a hash mark as a suffix to<br />
their name. For instance, the unboxed reprsentation of <code>42</code> is<br />
<code>42#</code>. There are some restrictions to their use. In particular,<br />
you can't pass them to polymorphic functions (like <hask>show</hask> or<br />
<hask>($)</hask> for instance).<br />
<br />
In this example, <hask>I#</hask> is a constructor that takes an unboxed<br />
integer and returns the <hask>Int</hask> that we know and love.<br />
<br />
<haskell><br />
module Main where<br />
import GHC.Exts<br />
<br />
showUnboxedInt :: Int# -> String<br />
showUnboxedInt n = (show $ I# n) ++ "#"<br />
</haskell><br />
Here we wrap the unboxed Int <hask>n</hask> with the <hask>I#</hask><br />
constructor and show the regular-old <hask>Int</hask>, with a hash mark on<br />
the end.<br />
<br />
[[JHC]]<br />
<br />
Jhc unboxed values behave similarly to ghc but with some differences, jhc<br />
doesn't allow the # in identifiers so by convention uses a trailing<br />
underscore to indicate an unboxed type. However it does use the trailing<br />
hash for unboxed literals like ghc. <br />
<br />
In addition jhc allows a limited polymorphism on unboxed values, they may be<br />
used polymorphically but if an exact type is not determined at the end of<br />
typechecking, they are defaulted to specific unboxed types. So 1# can be a<br />
Bits8_, Int_ or Bool_. The rules for when polymorphic unboxed types may be<br />
used without annotation are the same as for when rank n types can be used.<br />
<br />
=== Unboxed Tuples and Arrays ===<br />
<br />
Unboxed tuples use the syntax (# a,b,c #) and may not be assigned to values, they must be immediately scrutinized or used.<br />
<br />
=== When to use Unboxed Types ===<br />
<br />
...<br />
<br />
=== See Also ===<br />
<br />
#See the [https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/exts/primitives.html discussion on unboxed types and primitive operations] in the [http://www.haskell.org/ghc/docs/latest/html/users_guide/ GHC's User's Guide].<br />
#See the [http://hackage.haskell.org/package/base/docs/GHC-Exts.html GHC.Exts] module.<br />
#See Simon L. Peyton Jones and John Launchbury's paper [https://www.microsoft.com/en-us/research/wp-content/uploads/1991/01/unboxed-values.pdf "Unboxed values as first class citizens"].<br />
<br />
----<br />
This page is a work in progress by IsaacJones. More input welcome :)</div>Cptwunderlich