First-class citizen

Last updated

In a given programming language design, a first-class citizen [lower-alpha 1] is an entity which supports all the operations generally available to other entities. These operations typically include being passed as an argument, returned from a function, and assigned to a variable. [1]

Contents

History

The concept of first- and second-class objects was introduced by Christopher Strachey in the 1960s. [2] [3] He did not actually define the term strictly, but contrasted real numbers and procedures in ALGOL:

First and second class objects. In ALGOL, a real number may appear in an expression or be assigned to a variable, and either of them may appear as an actual parameter in a procedure call. A procedure, on the other hand, may only appear in another procedure call either as the operator (the most common case) or as one of the actual parameters. There are no other expressions involving procedures or whose results are procedures. Thus in a sense procedures in ALGOL are second class citizens—they always have to appear in person and can never be represented by a variable or expression (except in the case of a formal parameter)... [4]

Robin Popplestone gave the following definition: All items have certain fundamental rights.

  1. All items can be the actual parameters of functions
  2. All items can be returned as results of functions
  3. All items can be the subject of assignment statements
  4. All items can be tested for equality. [5]

During the 1990s, Raphael Finkel [6] proposed definitions of second and third class values, but these definitions have not been widely adopted. [7] [ better source needed ]

Examples

The simplest scalar data types, such as integer and floating-point numbers, are nearly always first-class.

In many older languages, arrays and strings are not first-class: they cannot be assigned as objects or passed as parameters to a subroutine. For example, neither Fortran IV nor C supports array assignment, and when they are passed as parameters, only the position of their first element is actually passed—their size is lost. C appears to support assignment of array pointers, but in fact these are simply pointers to the array's first element, and again do not carry the array's size.[ citation needed ]

In most languages, data types are not first-class objects, though in some object-oriented languages, classes are first-class objects and are instances of metaclasses. Languages in the functional programming family often also feature first-class types, in the form of, for example, generalized algebraic data types, or other metalanguage amenities enabling programs to implement extensions to their own implementation language.

Few languages support continuations and GOTO-labels as objects at all, let alone as first-class objects.

ConceptDescriptionLanguages
first-class function closures and anonymous functions Smalltalk, Dart, Scheme, ML, Haskell, F#, Kotlin, Scala, Swift, Perl, PHP, Python, Raku, JavaScript, Delphi, Rust
first-class control continuations Scheme, ML, F#
first-class type dependent types Coq, Idris, Agda
first-class data type Generic Haskell, C++11
first-class polymorphism impredicative polymorphism
first-class message dynamic messages (method calls) Smalltalk, [8] Objective-C, [8] Common Lisp
first-class class metaclass and metaobject Smalltalk, Objective-C, Ruby, Python, Delphi, Common Lisp
first-class proofs proof object [9] Coq, Agda

Functions

Many programming languages support passing and returning function values, which can be applied to arguments. Whether this suffices to call function values first-class is disputed.

Some authors require it be possible to create new functions at runtime to call them 'first-class'.[ citation needed ] Under this definition, functions in C are not first-class objects; instead, they are sometimes called second-class objects, because they can still be manipulated in most of the above fashions (via function pointers).

In Smalltalk, functions (methods) are first-class objects, just like Smalltalk classes. Since Smalltalk operators (+, -, etc.) are methods, they are also first-class objects.

Reflection

Some languages, such as Java and PHP, have an explicit reflection subsystem which allow access to internal implementation structures even though they are not accessible or manipulable in the same way as ordinary objects.

In other languages, such as those in the Lisp family, reflection is a central feature of the language, rather than a special subsystem. Typically this takes the form of some set of the following features:

These allow varying forms of first-class access to the language implementation, and are, in general, manipulable in the same way as, and fully indistinguishable from, ordinary language objects. Because of this, their usage generally comes with some (cultural) stipulations and advice, as untested modification of the core programming system by users can easily undermine performance optimisations made by language implementers.

See also

Notes

  1. Also known as first-class type, first-class object, first-class entity, or first-class value.

Related Research Articles

In computer science, an abstract data type (ADT) is a mathematical model for data types, defined by its behavior (semantics) from the point of view of a user of the data, specifically in terms of possible values, possible operations on data of this type, and the behavior of these operations. This mathematical model contrasts with data structures, which are concrete representations of data, and are the point of view of an implementer, not a user. For example, a stack has push/pop operations that follow a Last-In-First-Out rule, and can be concretely implemented using either a list or an array. Another example is a set which stores values, without any particular order, and no repeated values. Values themselves are not retrieved from sets, rather one tests a value for membership to obtain a Boolean "in" or "not in".

C is a general-purpose computer programming language. It was created in the 1970s by Dennis Ritchie, and remains very widely used and influential. By design, C's features cleanly reflect the capabilities of the targeted CPUs. It has found lasting use in operating systems, device drivers, and protocol stacks, but its use in application software has been decreasing. C is commonly used on computer architectures that range from the largest supercomputers to the smallest microcontrollers and embedded systems.

In object-oriented programming, a class is an extensible program-code-template for creating objects, providing initial values for state and implementations of behavior.

<span class="mw-page-title-main">Lisp (programming language)</span> Programming language family

Lisp is a family of programming languages with a long history and a distinctive, fully parenthesized prefix notation. Originally specified in the late 1950s, it is the second-oldest high-level programming language still in common use, after Fortran. Lisp has changed since its early days, and many dialects have existed over its history. Today, the best-known general-purpose Lisp dialects are Common Lisp, Scheme, Racket, and Clojure.

Pascal is an imperative and procedural programming language, designed by Niklaus Wirth as a small, efficient language intended to encourage good programming practices using structured programming and data structuring. It is named after French mathematician, philosopher and physicist Blaise Pascal.

<span class="mw-page-title-main">Smalltalk</span> Object-oriented programming language released first in 1972

Smalltalk is a purely object oriented programming language (OOP) that was originally created in the 1970s for educational use, specifically for constructionist learning, but later found use in business. It was created at Xerox PARC by Learning Research Group (LRG) scientists, including Alan Kay, Dan Ingalls, Adele Goldberg, Ted Kaehler, Diana Merry, and Scott Wallace.

In programming languages, a closure, also lexical closure or function closure, is a technique for implementing lexically scoped name binding in a language with first-class functions. Operationally, a closure is a record storing a function together with an environment. The environment is a mapping associating each free variable of the function with the value or reference to which the name was bound when the closure was created. Unlike a plain function, a closure allows the function to access those captured variables through the closure's copies of their values or references, even when the function is invoked outside their scope.

In computer science, imperative programming is a programming paradigm of software that uses statements that change a program's state. In much the same way that the imperative mood in natural languages expresses commands, an imperative program consists of commands for the computer to perform. Imperative programming focuses on describing how a program operates step by step, rather than on high-level descriptions of its expected results.

Metaprogramming is a programming technique in which computer programs have the ability to treat other programs as their data. It means that a program can be designed to read, generate, analyse or transform other programs, and even modify itself while running. In some cases, this allows programmers to minimize the number of lines of code to express a solution, in turn reducing development time. It also allows programs a greater flexibility to efficiently handle new situations without recompilation.

<span class="mw-page-title-main">C syntax</span> Set of rules defining correctly structured programs

The syntax of the C programming language is the set of rules governing writing of software in C. It is designed to allow for programs that are extremely terse, have a close relationship with the resulting object code, and yet provide relatively high-level data abstraction. C was the first widely successful high-level language for portable operating-system development.

<span class="mw-page-title-main">Pointer (computer programming)</span> Object which stores memory addresses in a computer program

In computer science, a pointer is an object in many programming languages that stores a memory address. This can be that of another value located in computer memory, or in some cases, that of memory-mapped computer hardware. A pointer references a location in memory, and obtaining the value stored at that location is known as dereferencing the pointer. As an analogy, a page number in a book's index could be considered a pointer to the corresponding page; dereferencing such a pointer would be done by flipping to the page with the given page number and reading the text found on that page. The actual format and content of a pointer variable is dependent on the underlying computer architecture.

In object-oriented programming, a metaclass is a class whose instances are also classes. Just as an ordinary class defines the behavior of certain objects, a metaclass defines the behavior of certain classes and their instances. Not all object-oriented programming languages support metaclasses. Among those that do, the extent to which metaclasses can override any given aspect of class behavior varies. Metaclasses can be implemented by having classes be first-class citizens, in which case a metaclass is simply an object that constructs classes. Each language has its own metaobject protocol, a set of rules that govern how objects, classes, and metaclasses interact. Metaclasses can be used, for example, to generate code automatically, or in developing frameworks.

POP-2 is a programming language developed around 1970 from the earlier language POP-1 by Robin Popplestone and Rod Burstall at the University of Edinburgh. It drew roots from many sources: the languages Lisp and ALGOL 60, and theoretical ideas from Peter J. Landin. It used an incremental compiler, which gave it some of the flexibility of an interpreted language, including allowing new function definitions at run time and modification of function definitions while a program runs, without the overhead of an interpreted language.

A struct in the C programming language is a composite data type declaration that defines a physically grouped list of variables under one name in a block of memory, allowing the different variables to be accessed via a single pointer or by the struct declared name which returns the same address. The struct data type can contain other data types so is used for mixed-data-type records such as a hard-drive directory entry, or other mixed-type records.

In computer science, a programming language is said to have first-class functions if it treats functions as first-class citizens. This means the language supports passing functions as arguments to other functions, returning them as the values from other functions, and assigning them to variables or storing them in data structures. Some programming language theorists require support for anonymous functions as well. In languages with first-class functions, the names of functions do not have any special status; they are treated like ordinary variables with a function type. The term was coined by Christopher Strachey in the context of "functions as first-class citizens" in the mid-1960s.

The computer programming languages C and Pascal have similar times of origin, influences, and purposes. Both were used to design their own compilers early in their lifetimes. The original Pascal definition appeared in 1969 and a first compiler in 1970. The first version of C appeared in 1972.

In a programming language, an evaluation strategy is a set of rules for evaluating expressions. The term is often used to refer to the more specific notion of a parameter-passing strategy that defines the kind of value that is passed to the function for each parameter and whether to evaluate the parameters of a function call, and if so in what order. The notion of reduction strategy is distinct, although some authors conflate the two terms and the definition of each term is not widely agreed upon.

S-algol is a computer programming language derivative of ALGOL 60 developed at the University of St Andrews in 1979 by Ron Morrison and Tony Davie. The language is a modification of ALGOL to contain orthogonal data types that Morrison created for his PhD thesis. Morrison would go on to become professor at the university and head of the department of computer science. The S-algol language was used for teaching at the university at an undergraduate level until 1999. It was also the language taught for several years in the 1980s at a local school in St. Andrews, Madras College. The computer science text Recursive Descent Compiling describes a recursive descent compiler for S-algol, implemented in S-algol.

This comparison of programming languages compares how object-oriented programming languages such as C++, Java, Smalltalk, Object Pascal, Perl, Python, and others manipulate data structures.

<span class="mw-page-title-main">Object-oriented programming</span> Programming paradigm based on the concept of objects

Object-oriented programming (OOP) is a programming paradigm based on the concept of objects, which can contain data and code: data in the form of fields, and code in the form of procedures. In OOP, computer programs are designed by making them out of objects that interact with one another.

References

  1. Scott, Michael (2006). Programming Language Pragmatics . San Francisco, CA: Morgan Kaufmann Publishers. p.  140. ISBN   9780126339512.
  2. Rod Burstall, "Christopher Strachey—Understanding Programming Languages", Higher-Order and Symbolic Computation13:52 (2000)
  3. Harold Abelson and Gerald Jay Sussman, Structure and Interpretation of Computer Programs, 2nd edition, section 1.3.4 footnote 64 Archived 2015-03-09 at the Wayback Machine
  4. Christopher Strachey, "Fundamental Concepts in Programming Languages" in Higher-Order and Symbolic Computation13:11 (2000); though published in 2000, these are notes from lectures Strachey delivered in August, 1967
  5. R. J. Popplestone: The Design Philosophy of POP-2. in: D. Michie: Machine Intelligence 3, Edinburgh at the University Press, 1968
  6. Finkel, R. Advanced Programming language Design, p 73
  7. Norman Ramsey. "About first-,second- and third-class value". stackoverflow.com. Retrieved 14 September 2013.
  8. 1 2 Paritosh Shroff, Scott F. Smith. Type Inference for First-Class Messages with Match-Functions
  9. Bove, Ana; Dybjer, Peter (2009). "Dependent Types at Work" (PDF). Language Engineering and Rigorous Software Development. Lecture Notes in Computer Science. 5520: 57–99. doi:10.1007/978-3-642-03153-3_2. ISBN   978-3-642-03152-6. Archived (PDF) from the original on April 2, 2014. Retrieved 8 June 2015. (also archived)