The mathematics foundation for first/second/third class values in programming languages

mathprogramming-languages

Added

Just found two related questions

https://math.stackexchange.com/q/1759680/1281

https://stackoverflow.com/a/2582804/156458


In programming languages, from Michael Scott's Programming Language Pragmatics

In general, a value in a programming language is said to have
first-class status if it can be passed as a parameter, returned from a subroutine, or assigned into a variable. Simple types such as
integers and characters are first-class values in most programming
languages. By contrast, a “second-class” value can be passed as a
parameter, but not returned from a subroutine or assigned into a
variable, and a “third-class” value cannot even be passed as a
parameter.

Labels are third-class values in most programming languages, but
second-class values in Algol. Subroutines display the most variation.
They are first-class values in all functional programming languages and
most scripting languages. They are also first-class values in C# and,
with some restrictions, in several other imperative languages,
including Fortran, Modula-2 and -3, Ada 95, C, and C++. 11 They are
second-class values in most other imperative languages, and
third-class values in Ada 83.

  1. What is the mathematics foundation for first/second/third class
    values in programming languages?

    The terminology reminds me of first/second order logic, but are they
    related?

  2. It seems to me that the difference between them is which specific
    case a value can be used

    • passed as a parameter,
    • returned from a subroutine, or
    • assigned into a variable.

    Why are the specific cases important, while not other cases not
    mentioned?

Thanks.

Best Answer

There isn't any, and it's pretty arbitrary.

The only useful distinction is between first class, and all others. Every case that's in the "other" bracket has its own distinct set of rules in each case and lumping them all together just isn't very helpful. "First class" means "You don't have to look up the rules", essentially, and "other" is "You have to learn the rules".

For instance in C++ individual functions are first class values, as long as they're stateless. Overload sets are not but lambdas are. In C# functions are generally first-class values but there's some awkward cases that arise when dealing with type inference that prevent them from being in all cases.