Talk:First-class citizen/Archive 1
This is an archive of past discussions about First-class citizen. Do not edit the contents of this page. If you wish to start a new discussion or revive an old one, please do so on the current talk page. |
Archive 1 |
Overheard
> While you can't create new functions and unbound methods in C++ at > runtime, > you can take their address and pass that around as a first class > object. True, though that usually devolves into a semantic issue about whether functions are first-class if you can only manipulate pointers to them. I usually say C has first-class functions if you squint. C++ doesn't have first-class bound member function pointers, as you point out, and another obvious example in C++ is the lack of first-class classes -- a feature in Python that is extremely powerful, since it almost makes the "factory" pattern automatic in Python (the class object itself is just a callable type that creates instances).
[link]
Not being a programmer, I am not sure what to make of this in regards to the statement in the article relating to C not having first class functions... --Lenehey 22:59, May 4, 2005 (UTC)
I think this just means -- as far as C goes -- that while the functions themselves aren't first-class, you can usefully refer to them with objects that are first-class, which means that you get most of the "first-class-ness" except the creatability. C++ still doesn't allow the creation of new functions, but with templating and/or operator overloading, it's possible to create new objects that both syntactically and semantically act like closures of existing functions at runtime and thus get a bit closer to "real" first-class functions. I'll rewrite the statement to say something about this. --Tardis 21:26, 20 July 2005 (UTC)
- Just goes to show that "First class object" is a loaded term. There's nothing about C functions that isn't "First Class". They're not pretty to work with, but that's an entirely seperate issue. A pointer to a function is an identity seperate from its name. That it doesnt need to have extra arbitrary information on top of it to be useful doesn't make it any less useful. Perhaps to be "first class", an object needs to be alterable using the same type of language syntax used to initially create it? I've been trying to understand what Lispers are going on about. --vstarre 21:54, 31 January 2007 (UTC)
Also, C++ does not support closures, which I find an important tool. Not sure if that is a requirement to first-class functions or not. --Spoon! 02:36, 1 September 2006 (UTC)
Is it is, or is it aint?
The article basically defines a first-class object as an entity which can be used in programs without restriction (when compared to other kinds of objects in the same language), in the context of a particular programming language.
And then gives this a a list of possible usages for a first-class object:
- being expressible as an anonymous literal value
- being storable in variables
- being storable in data structures
- having an intrinsic identity (independent of any given name)
- being comparable for equality with other entities
- being passable as a parameter to a procedure/function
- being returnable as the result of a procedure/function
- being constructable at runtime
Now isn't this too loose? I've got some problems or at least questions about at least two of these;
- 1 - It's not clear to me that being able to construct an object with a literal is a pre-requisite. I'm taking literal here in the common sense that there is a syntax which the compiler understands to construct an instance/data value at compile time. This really excludes way too many objects, I think.
- 5 - I'm not sure what this means. Maybe if we mean identity comparison, but to my mind equality usually has to do with some kind of 'state' equivalence, and not all first-class objects are so comparable.
Is there a reference to the source of this list? or did it just come about here?
Since it's often a topic of dispute whether or not a programming language supports certain constructs as first-class, I'm troubled that this definition seems to make things so language dependent. Since, C doesn't allow functions to be constructed at run-time, BUT the definition is language dependent, we might as well say that functions in C are first class. But are they? --Rick 14:59, 23 August 2006 (UTC)
- The definition has to allow for the wide range of things that can and can't be done with values considered unarguably first-class in a language - for example, a language might not have any literal forms and might not support equality on 'most' values (Haskell, for example, only allows equality on types belonging to the Eq class, which is just a typeclass like any other). In comparison, being at least as constructable at run-time as numbers (computing 1+1 constructs 2, for example) is a hard and fast requirement in any turing complete language, as it's impossible to have a language that's turing complete but won't encode constructing new natural numbers in some shape or form.86.10.90.136 (talk) 15:19, 8 December 2007 (UTC)
- C doesn't allow structures to be defined at runtime, but it does allow you to make new ones to your heart's content. The same should hold true for functions -- and indeed, in C one may multiply function pointers without limit. Intuitively, however, this misses the point. There's something Haskell, OCaml and even Python all offer here, that C doesn't -- closures. In C, you can not rebind the free variables in a function, make a pointer to it and pass it around. -jsnx (talk) 05:06, 9 December 2007 (UTC)
- C does allow structure values to be defined and passed around at runtime since at least ANSI 89. They are first class. This has nothing to do with the definition of structure types. Also, function pointers are first-class, but they aren't functions. Indeed, C does not have first-class functions precisely because it does not have closures - they are the standard implementation technique for first-class functions. 77.11.133.252 (talk) 09:13, 9 December 2007 (UTC)
- To illustrate the point about closures, I would have a hard time considering a language to have first-class functions if it does not have the ability to write versions of the K combinator. (It doesn't have to have polymorphism, but it should be possible to write a version of the combinator for each combination of input types). That is to say, you should be able to write a unary function K which returns unary functions, such that if you store the return value of K(x) in a variable, and then later apply that variable to an argument, you will always get back the original x. C's function pointers don't give you the natural ability to do this. -Chinju (talk) 01:39, 16 December 2007 (UTC)
I would really like to see more citations in this article. It seems to me that the text here is really just hypothesis and conjecture rather than hard facts. --Boyton 13:47 24 September 2007 (UTC) —Preceding unsigned comment added by Boyton (talk • contribs)
This article is widely inaccurate and confusing (opinion)
The article contradicts itself - it says that being constructible at runtime is a requirement, but of course that is impossible in most compiled languages. Sames goes for being readable, storable, etc - actually, most of the list. Further it implies that closures are also a requirement - such a claim is not justified.
> I'm going to make this point into a section because I think it has merit. Oligomous (talk) 23:57, 3 March 2010 (UTC)
The point seems to be that Smalltalk, Lisp, JavaScript, etc, all have first class functions, while the rest (C, Java, etc) don't. I don't agree with this (to me it is clear that in C functions are first class) but even if I did, the problem with this article is that it doesn't at least specify a clear criterion which includes all languages which supposedly have first class functions. If that is not possible, then at least a list of the languages which are commonly accepted to have support for first-class functions should be given. Even that is better than the current situation.
I think the article should be deleted or significantly deleted because it is spreading disinformation and confusion.
--24.4.151.152 (talk) 19:55, 24 June 2008 (UTC)
I have to disagree. This is one of the best written articles I have seen. I think you are disagreeing because your CS teacher used slightly different definitions for this term.
Why C functions aren't first class
First-class-ness (is that a word?) has to do with comparing what you can do with one kind of object that you can't do with another. Let's take ints in C as our exemplar of first-class objects in C. Yeah, you can pass ints to functions and return ints from functions. But more importantly, you can write functions that combine ints to create new ints. You can add them, subtract them, multiply them, etc. Indeed, all those functions come "out of the box" in C via various operators, but you can write those yourself or create new ones to your heart's content.
Just as ints can be combined to create new ints, there exist mathematical functions to combine functions into new functions. Arguably the most important way to combine functions is via composition. Mathematicians write the compose operator with a little round circle - I'll use lower case o. http://en.wikipedia.org/wiki/Function_composition. The definition is this: given functions f and g of the appropriate types, and a value x then (f o g)(x) = (f(g(x))).
Now, C's type system doesn't allow you to just say "pointers to functions of the appropriate type" but we can limit ourselves to functions from int to int. The type system issue is orthogonal - Haskell, ML, F#, Clean, and Scala all let you write a statically type checked compose function. Anyway, given the int limitation what we want is
typedef int(*funcPtr)(int)
funcPtr compose (funcPtr f, funcPtr g) { /* your code goes here */ }
Such that
compose(f,g)(x) == f(g(x))
In other words, compose should take two function pointers, f and g, and return a pointer to a function that is the composition (f o g).
And there's the problem. C lets you get a pointer to an existing function only. What we want is a pointer to a new function that combines the two functions given as arguments. But ANSI C doesn't have a way to create a new function.
Compose comes out of the box in all the functional languages I mentioned above (all of which are compiled or at least have compiled implementations - static compilation is irrelevant to the discussion). It, also, of course comes in dynamically typed languages like Scheme and Common Lisp, both of which are compiled in some implementations.
But if compose wasn't in, say Haskell, here's how you'd write it
compose f g = \x -> f (g x)
Opening up GHCi shows that compose does what we expect it to do
Prelude> let f x = 1 + x
Prelude> f 5
6
Prelude> let g x = 2 * x
Prelude> g 5
10
Prelude> let fog = compose f g
Prelude> fog 5
11
Prelude> let gof = compose g f
Prelude> gof 5
12 —Preceding unsigned comment added by 70.187.129.176 (talk) 17:07, 21 October 2008 (UTC)
In references
Programming language pragmatics by Michael Lee Scott
Defines (p.140 in the 2006 edition):
- 1st class -- can be passed as parameter, returned from a subroutine, assinged to a variable
- 2nd class -- can be passed as parameter, but not the other two
- 3rd class -- can't even pass as parameter
Examples given:
- labels -- 3rd class almost everywhere, but 2nd class in ALGOL
- subroutines --
He also says in a footnote that according to other authors, first class functions also require the ability to create new functions at runtime, which is possible in all f.p. languages, C#, Perl, Python, but not in most imperative programming languages. So, it's not that clear cut. I'll look for more refs. Pcap ping 11:36, 21 August 2009 (UTC)
- The 2009 edition differs slightly: instead of "create new functions", which is rather vague, he explicitly refers to anonymous function definition, aka lambda expressions. (p. 155) Pcap ping 05:49, 24 August 2009 (UTC)
Types and programming languages by Benjamin C. Pierce
Uses first-class, and first-class citizen with respect to existential types (no clear definition there), first-class polymorphism (here 1st class actually has a completely different meaning, as given in that article), and first-class type operators in system F-omega, where they're first class because they can be passed to functions. Does not ever define what first-class means. Essentially, the meaning of the adjective depends on context. Pcap ping 11:46, 21 August 2009 (UTC)
Side note: he never uses the term anonymous function either. Pcap ping 11:50, 21 August 2009 (UTC)
Concepts in programming languages by John C. Mitchell
A programming language has first-class functions if fuctions can be (p. 182):
- declared within any scope,
- passed as arguments to other functions, and
- returned as results of functions
He also says that in a statically scoped language first-class functions are generally represented by a closure. Pcap ping 12:03, 21 August 2009 (UTC)
Important issue
- The "ability to create new functions" as used in this wiki article is very misleading, because it's not about generating new code, but just supporting closures in statically-scoped language. Pcap ping 05:52, 24 August 2009 (UTC)
- Some dynamic languages can create new functions just as they can create other data types at runtime. If first class-ness alludes to having equal flexibility as other types in a language then it could mean more than a closure. Any references? --Paddy (talk) 04:33, 3 December 2009 (UTC)
- But isn't that an issue of that particular language then? What with the other case: if a language doesn't support generating code dynamically? (most compiled languages which doesn't build-in a whole compiler into the executable or the runtime environment, I suppose) Does it mean that first-class functions and types are impossible in compiled languages? --Sasq777 (talk) 04:05, 11 May 2010 (UTC)
- Some dynamic languages can create new functions just as they can create other data types at runtime. If first class-ness alludes to having equal flexibility as other types in a language then it could mean more than a closure. Any references? --Paddy (talk) 04:33, 3 December 2009 (UTC)
Rewrite notice
Unless someone can source the current content, I'm going to rewrite the article based on the sources given above in the following days. Pcap ping 11:09, 27 August 2009 (UTC)
RE: Rewrite notice
Go for it. I was about to do it myself based on the same reference, but then I looked at the talk page.
ConsilienceNow (talk) 12:17, 17 September 2009 (UTC)
Confusing sentence
"Few languages support continuations and GOTO-labels as first-class objects, though arguably they don't support them as objects at all."
Here, is "they" = the few languages that do, or others, or ..? (In Common Lisp, of course, labels are first-class, so some languages do support them as first-class objects, but it's not at all clear from that sentence.) —Preceding unsigned comment added by 216.163.72.2 (talk) 23:12, 12 February 2010 (UTC)
Runtime Construction Requirement
What constitutes runtime construction? Does a variable in C get constructed at runtime? The value is loaded from the data segment on disk into memory but it's not "constructed" per se so much as the bits are copied. Also I don't believe function in any language can be "constructed" unless you were to randomly generate their definitions or something. You could argue that function composition creates a new function but I still have my reservations whether this requirement is doing anything other than add confusion. Oligomous (talk) 00:03, 4 March 2010 (UTC)
Object versus Value
Currently First-class value redirects to First-class object. Should those be swapped around and value made the primary descriptor of this concept? Object is easily confused with its object-oriented sense. But, I'm not sure if first-classness might be a requirement of a value, making the qualification of "first-class value" seem silly. Can you have values that aren't first-class? Value (computer science) talks about a value being data but doesn't specify what a value constitutes semantically. I can conceive of its possibility of values that aren't first-class but I don't know if it really happens. Other words, such as entity (similar problem to object?) or thing (too broad?), could be used I guess.
We could also broaden the article to talk about value class in general and mention second and third-class as well as first-class values . If we were going to do that calling it "object class" would seem incredibly misleading.
Oligomous (talk) 23:46, 3 March 2010 (UTC)
Another citation?
http://msdn.microsoft.com/en-us/library/dd233158(VS.100).aspx -- might be useful. Oligomous (talk) 00:14, 4 March 2010 (UTC)
Area expert will help with editing if volunteer will help with citations
I am an expert in this area; part of my living is writing about programming languages. I think I can improve the article substantially, but I cannot afford the time to do all the searches needed for citations to meet wikipedia standards. If someone will volunteer to look up such search terms as "first-class value" "first-class function" "first-class module" "first-class type" "first-class polymorphism" "first-class object" in Google Scholar, I will tell you what conferences and journals are most reputable. Michael Scott is a good man but on this topic he's a little out of his area. Anyway, if somebody will work with me on citations I'm willing to do a new article.
Norman Ramsey (talk) 05:11, 7 April 2010 (UTC)
Subjective
"First class object" is a bit of a subjective term: a first class object is the kind of thing that a language is *about*. C is all about pointers and primitive types, Lisp is about lists, XQuery is about DOM fragments, APL is about vectors and so on. First class objects are the state that a language spends its time manipulating. BUt it's subjective - a right brain thing. If the first-class objects of a language are X, then you have to at least grokk X, or you will not understand what code is trying to accomplish. —Preceding unsigned comment added by Paul Murray (talk • contribs) 01:37, 8 September 2010 (UTC)
Back to basics
This article is trying to hard to be specific about what a first-class object is in terms of particular operations (assignment, etc.) that can be performed on it. This is a bit missing the point. In a language which (say) has communicating processes, an object isn't first class if it can't be passed as a parameter in an inter-process communication. The basic concept is that all operations which are generally available for its basic datatypes (typically integers, etc.) are also available for other datatypes (arrays, functions, datatypes, processes, etc.). The concept started with Strachey's definition of a first-class function which can be passed as an argument, returned as a value, and so on. I will try to write some language along these lines.... --Macrakis (talk) 19:20, 7 September 2013 (UTC)
Also, WP:NPOV tells us to report all reputable points of view on a topic. The current version of the article claims that creating new functions is required to call them first-class; but that is not the unanimous opinion in our WP:RS. --Macrakis (talk) 19:49, 7 September 2013 (UTC)