Bottom type: Difference between revisions
rv mistaken addition, the 'void' type in C/C++ is not a bottom type |
|||
Line 22: | Line 22: | ||
==In programming languages== |
==In programming languages== |
||
Most commonly used languages don't have a way to explicitly denote the empty type. There are a few notable exceptions. |
|||
The keyword 'void' in [[C]] corresponds loosely to the bottom type, in that it is used to represent the return type of a function that does not return any value. |
|||
[[Haskell (programming language)|Haskell]] does not support empty data types. However, in [[Glasgow Haskell Compiler|GHC]], there is a flag <tt>-XEmptyDataDecls</tt> to allow the definition <tt>data Empty</tt> (with no constructors). The type <tt>Empty</tt> is not quite empty, as it contains non-terminating programs and the <tt>undefined</tt> constant. The <tt>undefined</tt> constant is often used when you want something to have the empty type, because <tt>undefined</tt> matches any type (so is kind of a "subtype" of all types), and attempting to evaluate <tt>undefined</tt> will cause the program to abort, therefore it never returns an answer. |
[[Haskell (programming language)|Haskell]] does not support empty data types. However, in [[Glasgow Haskell Compiler|GHC]], there is a flag <tt>-XEmptyDataDecls</tt> to allow the definition <tt>data Empty</tt> (with no constructors). The type <tt>Empty</tt> is not quite empty, as it contains non-terminating programs and the <tt>undefined</tt> constant. The <tt>undefined</tt> constant is often used when you want something to have the empty type, because <tt>undefined</tt> matches any type (so is kind of a "subtype" of all types), and attempting to evaluate <tt>undefined</tt> will cause the program to abort, therefore it never returns an answer. |
Revision as of 19:47, 28 September 2010
In type theory, a theory within mathematical logic, the bottom type is the type that has no values. It is also called the zero or empty type, and is sometimes denoted with falsum (⊥).
A function whose return type is bottom cannot return any value. In the Curry–Howard correspondence, the bottom type corresponds to falsity.
Computer science applications
The bottom type is a subtype of all types.[1] It is used to represent the return type of a function that does not return a value: for instance, one which loops forever, signals an exception, or exits.
Because the bottom type is used to indicate the lack of a normal return, it typically has no values. It contrasts with the top type, which spans all possible values in a system, and a unit type, which has exactly one value. The bottom type is sometimes confused with the so-called "void type", which is actually a unit type, albeit one with no defined operations.
The bottom type is frequently used for the following purposes:
- To signal that a function or computation diverges; in other words, does not return a result to the caller. (This does not necessarily mean that the program fails to terminate; a subroutine may terminate without returning to its caller, or exit via some other means such as a continuation.)
- When coupled with the Curry-Howard interpretation of bottom as "falsity", this yields a computational interpretation of non-constructive logic in terms of control flow operators[2].
- As an indication of error; this usage primarily occurs in theoretical languages where distinguishing between errors is unimportant. Production programming languages typically use other methods, such as option types (including tagged pointers) or exception handling.
In Bounded Quantification with Bottom[1], Pierce says that "Bot" has many uses:
- In a language with exceptions, a natural type for the raise construct is raise ∈ exception -> Bot, and similarly for other control structures. Intuitively, Bot here is the type of computations that do not return an answer.
- Bot is useful in typing the "leaf nodes" of polymorphic data structures. For example, List(Bot) is a good type for nil.
- List[Bot] is a natural type for the "null pointer" value (a pointer which does not point to any object) of languages like Java: in Java,
null
is a value of every reference type, which is just the same as saying "null ∈ Bot." - A type system including both Top and Bot seems to be a natural target for type inference, allowing the constraints on an omitted type parameter to be captured by a pair of bounds: we write S<:X<:T to mean "the value of X must lie somewhere between S and T." In such a scheme, a completely unconstrained parameter is bounded below by Bot and above by Top.
In programming languages
Most commonly used languages don't have a way to explicitly denote the empty type. There are a few notable exceptions.
Haskell does not support empty data types. However, in GHC, there is a flag -XEmptyDataDecls to allow the definition data Empty (with no constructors). The type Empty is not quite empty, as it contains non-terminating programs and the undefined constant. The undefined constant is often used when you want something to have the empty type, because undefined matches any type (so is kind of a "subtype" of all types), and attempting to evaluate undefined will cause the program to abort, therefore it never returns an answer.
In Common Lisp the symbol NIL, amongst its other uses, is also the name of a type that has no values. It is the complement of T which is the top type. The type named NIL is sometimes confused with the type named NULL, which has one value, namely the symbol NIL itself.
In Scala, the bottom type is denoted as Nothing. Besides its use for functions that just throw exceptions or otherwise don't return normally, it's also used for covariant parameterized types. For example, Scala's List is a covariant type constructor, so List[Nothing] is a subtype of List[A] for all types A. So Scala's Nil, the object for marking the end of a list of any type, belongs to the type List[Nothing].
See also
- Haskell's Denotational semantics (Discusses the role of Bottom in the denotational semantics of programming languages)
- Top type
- NaN
- Fail-stop
References
- ^ a b Pierce, Benjamin C. (1997). Bounded Quantification with Bottom
- ^ Griffin, Timothy G. (1990). "The Formulae-as-Types Notion of Control". Conf. Record 17th Annual ACM Symp. on Principles of Programming Languages, POPL '90, San Francisco, CA, USA, 17-19 Jan 1990. pp. 47–57.