Primitives, levity, and boxing
- Levity
- Levity polymorphism
- Boxing
A primitive datatype or operation is one that is available in Haskell but cannot be defined in Haskell; the primitives are the things that are “magical”.
These modules export primitives:
- Primarily,
GHC.Exts
in thebase
package. - There is also
GHC.Prim
in theghc-prim
package, which is where all of the primitives originally come from, but this isn’t intended to be an API that you use directly. GHC exposes a lot of internals, but that doesn’t mean anything is a stable API unless it says so!
Don’t worry about trying to use primitives for efficiency’s sake. Compiler optimizations will usually do that to your code automatically. GHC exposes a lot of internals, but that doesn’t mean everything that’s there is there because you need it! To write normal everyday Haskell, you do not need to read this article; you only need this if you’re contributing to GHC, if you want to understand some of the details about how optimization works, or if you’re just curious.
Roughly speaking, primitive, unboxed, and unlifted are usually synonymous. But they all mean slightly different things, and we’ll discuss the precise distinctions below.