⊥⊥→A for every type A. In fact, there exists a unique such function. It is therefore, fairly reasonable for this function to be provided as part of the standard library. Often it is called something like absurd
. (In systems with subtyping, this might be handled simply by having ⊥ be a subtype of every type. Then the implicit conversion is absurd
. Another related approach is to define ⊥ as ∀α.α which can simply be instantiated to any type.)
⊥E+AEthrow:E→⊥. In the A case, I'll use f:A→B. Overall, I want a value of type B so I need to do something to turn a ⊥ into a B. That's what absurd
would let me do.
That said, there's not a whole lot of reason to define your own functions of ⊥→A. By definition, they would necessarily be instances of absurd
. Still, you might do it if absurd
isn't provided by the standard library, or you wanted a type specialized version to assist type checking/inference. You can, however, easily produce functions that will end up instantiated to a type like ⊥→A.
Even though there isn't much a reason to write such a function, it should generally still be allowed. One reason is that it simplifies code generation tools/macros.
Derek Elkins left SE
fonte
(x ? 3 : throw new Exception())
gets replaced for analysis purposes with something more like(x ? 3 : absurd(throw new Exception()))
?absurd
would implicitly be being inserted. Of course, you could put theabsurd
inside the definition ofthrow
which is effectively what definingTo add to what has been said about the function
absurd: ⊥ -> a
I have a concrete example of where this function is actually useful.Consider the Haskell data-type
Free f a
which represents a general tree structure withf
-shaped nodes and leaves containinga
s:data Free f a = Op (f (Free f a)) | Var a
These trees can be folded with the following function:
Briefly, this operation places
alg
at the nodes andgen
at the leaves.Now to the point: all recursive datastructures can be represented using a Fixed-point datatype. In Haskell this is
Fix f
and it can defined astype Fix f = Free f ⊥
(i.e. Trees withf
-shaped nodes and no leaves outside of the functorf
). Traditionally this structure has a fold as well, calledcata
:Which gives a quite neat use of absurd: since the tree cannot have any leaves (since ⊥ has no inhabitants other than
undefined
), it is never possible to use thegen
for this fold andabsurd
illustrates that!fonte
The bottom type is a subtype of every other type, which can be extremely useful in practice. For example, the type of
NULL
in a theoretical type-safe version of C must be a subtype of every other pointer type, otherwise you couldn't e.g. returnNULL
where achar*
was expected; similarly, the type ofundefined
in theoretical type-safe JavaScript must be a subtype of every other type in the language.As a function return type, it's also very useful to have certain functions that never return. In a strongly-typed language with exceptions, for instance, what type should⊥ —that is, a function returning
exit()
orthrow()
return? They never return control flow to their caller. And since the bottom type is a subtype of every other type, it's perfectly valid for a function returningInt
to instead returnInt
can also choose not to return at all. (Maybe it callsexit()
, or maybe it goes into an infinite loop.) This is good to have, because whether a function ever returns or not is famously undecidable.Finally, it's very useful for writing constraints. Suppose you want to constrain all parameters on "both sides", providing a type that must be a supertype of the parameter, and another type that must be a subtype. Since bottom is a subtype of every type, you can express "any subtype of S" as⊥≺T≺S . Or, you can express "any type at all" as ⊥≺T≺⊤ .
fonte
NULL
is a unit type isn't it, distinct from ⊥ which is the empty type?void*
, you'd need a specific type for it that could be used for any pointer type.<:
e.g. SystemThere is one use I can think of, and it's something that has been considered as an improvement to the Swift programming language.
Swift has a
maybe
Monad, spelledOptional<T>
orT?
. There are many ways to interact with it.You can use conditional unwrapping like
You can use
map
,flatMap
to transform the values!
, of type(T?) -> T
) to forcefully unwrap the contents, otherwise triggering a crashThe nil-coalescing operator (
??
, of type(T?, T) -> T
) to take its value or otherwise use a default value:Unfortunately, there was no concise way of saying "unwrap or throw an error" or "unwrap or crash with a custom error message". Something like
doesn't compile, because
fatalError
has type() -> Never
(()
isVoid
, Swift' unit type,Never
is Swift's bottom type). Calling it producesNever
, which isn't compatible with theT
expected as a right operand of??
.In an attempt to remedy this, Swift Evolution propsoal
SE-0217
- The “Unwrap or Die” operator was put forth. It was ultimately rejected, but it raised interest in makingNever
be a subtype of all types.If
Never
was made to be a subtype of all types, then the previous example will be compilable:because the call site of
??
has type(T?, Never) -> T
, which would be compatible with the(T?, T) -> T
signature of??
.fonte
Swift has a type "Never" which seems to be quite like the bottom type: A function declared to return Never can never return, a function with a parameter of type Never can never be called.
This is useful in connection with protocols, where there may be a restriction due to the type system of the language that a class must have a certain function, but with no requirement that this function should ever be called, and no requirement what the argument types would be.
For details you should have a look at the newer posts on the swift-evolution mailing list.
fonte