Unboxed types, like Int#
, and strict functions, like f (!x) = ...
, are something different, but I see conceptual similarity - they disallow thunks/laziness in some way. If Haskell was a strict language like Ocaml, every function would be strict and every type unboxed. What is the relationship between unboxed types and enforcing strictness?
相关问题
- Understanding do notation for simple Reader monad:
- Making Custom Instances of PersistBackend
- Haskell: What is the differrence between `Num [a]
- applying a list to an entered function to check fo
- Haskell split a list into two by a pivot value
相关文章
- Is it possible to write pattern-matched functions
- Haskell underscore vs. explicit variable
- Top-level expression evaluation at compile time
- Stuck in the State Monad
- foldr vs foldr1 usage in Haskell
- List of checkboxes with digestive-functors
- How does this list comprehension over the inits of
- Replacing => in place of -> in function type signa
Unboxed types are necessarily strict, but not all strict values are necessarily unboxed.
has two strict fields
has two strict fields, but the first one is unboxed.
Ultimately, the reason unboxed types are (necessarily) strict is there is no place to store the thunk as they are just flat, dumb data at that point.
Unboxed vs Boxed Data
To support parametric polymorphism and laziness, by default Haskell data types are represented uniformly as a pointer to a closure on the heap, with a structure like this:
These are "boxed" values. An unboxed object is represented by the value itself directly, without any indirection or closure.
Int
is boxed, butInt#
is unboxed.Lazy values require a boxed representation. Strict values do not: they can represented either as fully evaluated closures on the heap, or as primitive unboxed structures. Note that pointer tagging is an optimization that we can use on boxed objects, to encode the constructor in the pointer to the closure.
The Relationship to Strictness
Normally, unboxed values are generated in an ad hoc fashion by functional language compilers. In Haskell, however, unboxed values are special. They:
#
;Because they are unlifted they are necessarily strict. The representation of laziness is not possible.
So particular unboxed types, like
Int#
,Double#
, really are represented just as double or int on the machine (in C notation).Strictness Analysis
Separately, GHC does strictness analysis of regular Haskell types. If a value's use is found to be strict – i.e. it can never be 'undefined' – the optimizer might replace all uses of the regular type (e.g.
Int
) with an unboxed one (Int#
), since it knows that the use ofInt
is always strict, and thus replacement with the more efficient (and always strict) typeInt#
is safe.We can of course have strict types without unboxed types, for example, an element-strict polymorphic list:
is strict in its elements, but does not represent them as unboxed values.
This also points out the mistake you made about strict languages, like OCaml. They still need to support polymorphism, so either they provide a uniform representation, or they specialize data types and functions to every type. GHC by default uses uniform representation, as does OCaml, though GHC can also specialize types and functions now (like C++ templates).
Arguments of any types can be made "strict", but the only unboxed types that have corresponding boxed types are
Char#
,Int#
,Word#
,Double#
andFloat#
.If you know low-level languages like C, it's easier to explain. Unboxed types are like
int
,double
, etc., and the boxed types are likeint*
,double*
, etc. When you've got anint
, you already know the whole value as it's represented in the bit pattern, therefore, it is not lazy. It must be strict too, as all values ofint
are valid and not ⊥.However, given an
int*
you may choose to dereference the pointer later to get the actual value (thus lazy), and it is possible to have invalid pointers (it contains ⊥, i.e. non-strict).