Bottom Type in F#


It is possible to express the bottom type in F#.

The bottom type is the type that has no values. It is useful as the return type of a function when every execution either does not terminate or throws an exception. Some languages include the bottom type. The suggestion for F# to add the bottom type was declined. Interestingly, the suggestion for C# to add the bottom type lives on.

Nevertheless, it is possible to define the bottom type in F#. Here is how I would do it.

1linktype Bottom = private Bottom of Bottom


My original thought was to omit of Bottom and then have this line be alone in its module (so that a public function does not provide access to this private case constructor). Then I read the suggestion to include of Bottom in this post on Reddit from three years ago. By including of Bottom, this discriminated union is a recursive type but lacks a base case. As such, it is clear from this one line that it is impossible to obtain an instance of Bottom.

In addition to having no values, there is another property of the bottom type. In subtyping systems, it is a subtype of all types. That is the motivation for the name "bottom". It sits at the bottom of the type hierarchy. My bottom type does not have this property. I think the suggestion for F# to add the bottom type was specifically requesting this feature.

The tags feature of Coding Blog Plugin is still being developed. Eventually the tags will link somewhere.

#Type_Theory #FSharp