|(7 intermediate revisions by 4 users not shown)|
Revision as of 22:54, 23 May 2014
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.
In GHC, by convention(?), unboxed values have a hash mark as a suffix to
their name. For instance, the unboxed reprsentation of
42#. There are some restrictions to their use. In particular,
module Main where import GHC.Exts showUnboxedInt :: Int# -> String showUnboxedInt n = (show $ I# n) ++ "#"
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.
1 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.
2 When to use Unboxed Types
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 :)