Talk:Polymorphism (computer science)

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia
(Redirected from Talk:Type polymorphism)

Do Haskell Type Classes count as bounded quantification?[edit]

The article describes Haskell type class constraints like (Eq a) => [a]->[a] as a form of bounded quantification... but in the intended dictionary-passing interpretation this is nothing more than an abbreviation for (forall a, (a->a)->[a]->[a]) where the first argument is the "eq" function (generally: the set of functions which instances of Eq must implement) on the type "a". I don't know if this is quite the same as something like F-sub. —Preceding unsigned comment added by Megacz (talkcontribs) 20:24, 6 February 2008 (UTC)[reply]

F-sub should be considered actually as a different matter if we refer to forms of bounded quantification as intended by common practice. I have a ten year experience in commercial software development and this is just clear when fererring to haskell types. —Preceding unsigned comment added by 212.182.132.93 (talk) 16 January 2009

I think that is true. Bounded quantification is a tricky topic though. Pierce, 2002, ch. 31 discusses , (F-omega-sub) which is more formally called Higher-Order bounded quantification; this is not covered in here or anywhere else on this wiki as of today. F-omega-sub is probably closer to Haskell since you can have types, kinds and parametric polymorphism combined with subtyping. The caveat is that the Haskell subtyping isn't "real" in the sense that a type class does not define a type... Pcap ping 10:16, 19 August 2009 (UTC)[reply]

conflicting definitions of ad-hoc polymorphism[edit]

In the second paragraph:

If the range of actual types that can be used is finite and the combinations must be specified individually prior to use, it is called Ad-hoc polymorphism.

In section 3, "Ad-hoc polymorphism":

Strachey chose the term ad-hoc polymorphism to refer to polymorphic functions which can be applied to arguments of different types, but which behave differently depending on the type of the argument to which they are applied

How is a reader supposed to learn what ad-hoc polymorphism means when the writers can't even do it? Herorev (talk) —Preceding comment was added at 19:06, 17 April 2008 (UTC)[reply]

I think the meanings of these terms have drifted over time, and the are used differently between the typed-FP and the OO communities. In the typed FP world we'd say that a polymorphic function is one that universally quantifies over some class of types, and doesn't know anything about the specific type given to any instantiation. A function that actually inspects the type of its argument and does different things depending on the type is called "generic". The article generic programming is in pretty bad shape right now, but it has some useful links that explain this. 67.122.210.149 (talk) 12:10, 20 November 2008 (UTC)[reply]
It's not conflicting, both do not exclude each other, and there is no 'if and only if'. Rajakhr (talk) 08:42, 17 May 2010 (UTC)[reply]
in the first paragragh .mcclarah ia the owner of the system which undergone the implementation process long time ago. — Preceding unsigned comment added by 197.221.253.21 (talk) 16:19, 20 March 2017 (UTC)[reply]

Way too complex article!![edit]

I am a java programmer with 8 years of experience. When I end up sitting pretty much baffled by what I read here, there is something fundamentally flawed with the article. Many of these computer science articles are way to tied into the theoretical underpinnings, which is very sad for people actually trying to use Wikipedia as a tool to learn and/or understand things, and to do a "quick look-up" of some aspect. Such theory is obviously important for a complete discussion of the aspect, but should OBVIOUSLY come as a distinct chapter later. The intro should be in a "layman terms" language, importantly exemplified with an example written in some NORMAL generic pseudo-language (not any lisp-like silliness - what stupid idea is that?!), as in "This exemplifies the aspect under discussion". I must say that it is obvious that the hardcode theory geeks are WAY too much in control for the comp.sci. articles, making them read like scientific articles seemingly written for journals - and as I've understand the theory of WP, this is not its intention. Stolsvik (talk) 14:45, 18 December 2008 (UTC)[reply]

I agree. I can code in a whole bunch of languages, including Java and Haskell, and this article is hard for me to understand.—greenrd (talk) 17:06, 18 December 2008 (UTC)[reply]
Having 100 years of programming experience in Java does not unfortunately give you the ability to understand an article that uses quite a vast array of notions from type theory, and mathematical logic in general. This article is indeed written in a way that caters to graduate students in CS, and likely to those specializing in programming languages. I added a big "about" template at the top to redirect readers like you to the gentler introductory article Polymorphism in object-oriented programming. Pcap ping 09:24, 18 August 2009 (UTC)[reply]

I have just read three books each with a mutaully exclusive meaning for ad hoc polymorohism. I can say the same for many terms in the literature. —Preceding unsigned comment added by 192.207.123.2 (talk) 20:37, 6 October 2010 (UTC)[reply]

I have read this article many times over the last few years. I must admit that the first few readings (a few years ago), this article was pretty confusing. Gradually, and over time, I begin to understand more and more of the article (specifically the different type of polymorphism), to the point where as of May of 2011, I believe I can understand most if not all it. I believe the key is to be patient, and come back later if you have trouble understand it. Don't be discourage if you cannot understand everything at first. — Preceding unsigned comment added by 65.197.233.126 (talk) 01:02, 23 June 2011 (UTC)[reply]

This is a communication style conflict, which is not resolvable because all people believe their style is correct. A more top-down approach, moving from the simple to the more complex, would benefit all communication styles, not just the analytical. More balance means more understanding, which is the goal, and would make the article accessible to more people. Dprust (talk) 18:27, 27 May 2015 (UTC)[reply]

Genetic polymorphism[edit]

The article needs to mention or redirect to genetic polymorphism (which is what I was looking for) which is a medical realm . I know little about it - other than it is not cited here as existing. Spanglej (talk) 00:05, 28 February 2009 (UTC)[reply]

This article is "Polymorphism_(computer_science)", making it clear that it's a CS article, while "Polymorphism" is a disambiguation page, which is where you ought to look. No changes are needed. — Preceding unsigned comment added by Skulldyvan (talkcontribs) 11:06, 11 February 2014 (UTC)[reply]

I changed the OOP bit at the start...[edit]

I'm pretty sure it was wrong, inheritance and subtyping are different things. Inheritance allows reusing code, whereas subtyping allows one type to be substituted for another. Surely polymorphism is achieved through the latter not the former, as the article originally suggested. The textbook Programming Language Concepts by Mitchell mentions this distinction. —Preceding unsigned comment added by 193.60.95.68 (talk) 13:32, 22 May 2009 (UTC)[reply]

This talk page needs (auto-)archiving[edit]

Really old threads and newer threads are randomly intermingled. Pcap ping 09:26, 18 August 2009 (UTC)[reply]

I have manually created an archive page for the older entries. — Tobias Bergemann (talk) 07:27, 19 August 2009 (UTC)[reply]
Thanks. Pcap ping 10:27, 19 August 2009 (UTC)[reply]

External Links - Commercial Sites...?[edit]

The following external links should be considered for removal:

Broken example[edit]

The example assumes that the language differentiates between integers and floating point numbers. Not all do. Not only can a language treat integers as floating point numbers that happen not to have any significant digits after the decimal point, but some languages handle all numbers using a "big number" format, in which the digits are stored as characters. —Preceding unsigned comment added by 208.80.104.2 (talk) 17:41, 11 August 2010 (UTC)[reply]

Horrible mish-mash[edit]

Sorry, but this is a really horrible article! It is full of half truths, for example "most OO languages allow constraints on type parameters" is wrong: neither Java nor C++ allow constraints, and that's 99% of all (statically typed) OO language usage in the real world.

Then there's the incorrect assertion polymorphism applies to data types. No, it doesn't, even though that's common lay language. If you take the example of a list of some "arbitrary type", for example, it is not a polymorphic data type at all, it is in fact a data functor that maps each type T to a type list of T.

What makes this a functor is that it is structure preserving: for every function f: U -> V, there is a function list(f): list(U) -> list(V) written in Ocaml as List.map f, such that list(f) . list(g) = list (f . g), which is commonly known as the deforestation isomorphism: in expanded form, ML notation: List.map f (List.map g lst) = List.map (fun x -> f (g (x))) lst.

Now, if we consider a polymorphic function such as list reversal, what is it? The signature tells the story: List.rev: T -> (list(T) -> list(T)), this entity maps each type T to a function on lists of T. Note carefully the result is a single arrow (for any given T, in most FPLs this is also a value). This operation preserves structure because for any function f: U -> V, map f (rev lst) = rev (map f list), in other words it is independent of T.

This is what a polymorphic function is. A categorical explanation in lay-ish terms is the only proper way to describe it. Even mentioning "type variables" is a seriously bad idea: term calculi are often used in calculations but they're woeful way to describe anything. For example, when we talk about "instantiating a type variable with a type" and allow or not that the type may itself be "polymorphic" we're just getting confused. A type variable is not a type. It's a "hole" in a type term, which makes for a messy way to write a data functor. Instantiation (substitution) of a type variable is then nothing more than composition of functors. The so-called predicative typing is nothing more than a requirement the composition be with a null-ary functor such as 1 |-> int so we get 1 |-> list(int), rather than the impredicative kind such as the bifunctor T,U |-> T * U which leaves the specialisation T,U |-> list (T * U). In other words, the distinction just isn't important: they're both just compositions.

I hope it is clear the abstract (categorical) view is much easier to understand than the archaic term based type theories. All modern type theory uses the language of categories.

So called "object oriented" polymorphism via "subtyping" isn't polymorphism at all. This is another myth. Subtyping is subtyping. It isn't polymorphism. A function with signature f: A -> B will clearly accept a value of any subset of A. There's no polymorphism here.

There is also too much focus on higher rank polymorphism. Although interesting, it is primarily a problem with Hindley-Milner type inference: it isn't fundamental to the notion of polymorphism, inference is just a syntactic convenience.

What is important is something else: functorial polymorphism, sometimes called polyadic programming. A function with this kind of polymorphism is properly higher order (not just higher rank): it operates on data structures independently of the structure. The canonical example would be a generic fold or a type specific version thereof such as a summation of integers, which operates just as well on a list, array, tree, or any other container.

No production languages provide statically type checked polyadic programming, although there do exist some research languages that do in some limited form (such as Jay's FISh over arrays, or Bondi, which is fully general). Most languages emulate it with some help, such as Haskell (cheating with type classes), or Charity (fully automatic but doesn't support recursion). Even C++ has some polyadic programming (via polymorphic methods together with overloading hackery: the STL is based on obtaining polyadicity by using iterators).

Polyadicity is the only true kind of polymorphism, because polymorphic means "over many forms" which means "independent of structure": the point is that parametric data polymorphism is very weak. Polyadicity is the holy grail. The existing article focuses on too much junk that has little to do with what polymorphism is really about: being able to write routines with a very high level of reusability, and therefore vastly improving the chance a program will be correct by reducing the amount of code that requires examination.

Yttrill (talk) 16:13, 14 January 2011 (UTC)[reply]

I agree that's the most coherent definition currently used, but to some extent we also need to document terms as they're used in different communities, not *solely* in specific parts of academic PLs research, especially since terminology has shifted over the years. I agree that what the OO community means by "polymorphism" isn't what most current-day PLs researchers define as polymorphism, but nonetheless it's common terminology there, so should be mentioned somehow, even if the actual discussion is in a different article. It's not as if it's only laypeople who don't know better making the "error", either; there are some pretty well-known academics who call OO subtyping "polymorphism", and it's present under that name in many textbooks. --Delirium (talk) 15:55, 10 March 2011 (UTC)[reply]

I've redirected Polymorphism in object-oriented programming to this article as there didn't seem to be anything in that article that isn't already discussed either here or in Subtyping. —Ruud 16:48, 22 September 2013 (UTC)[reply]

Useful reference[edit]

This might be a useful reference (I have only skimmed):

Ruud 16:13, 23 September 2013 (UTC)[reply]

Needs full rewrite[edit]

Still a horrible mish-mash after 5 years! I've been programming in Java since 1996, and I've used polymorphism via subclassing and overloading. Everyone says that for a language to be "object-oriented", it must support polymorphism, but I didn't see much about OOP in this article.

I think it began its life as a grad-school level article for math geeks. Can we write something practical for budding software engineers? --Uncle Ed (talk) 11:39, 22 October 2013 (UTC)[reply]

I think this article is in a much better shape than it was 5 years ago, thanks to the editing of User:Crosbiesmith. What you might know as polymorphism is a very specific form of polymorphism also called subtyping. I think the article adequately describes this distinction, although the subsection on subtyping could still be improved significantly. —Ruud 12:52, 7 November 2013 (UTC)[reply]

I think this article should present a clear distinction of polymorphism under Object Oriented, Functional Languages, and Type Theory. They are fundamentally different, even when OO languages implement concepts from functional languages. It would be much easier to split into 3 or more wikipedia entries, each maintained for specialists of the correct areas. — Preceding unsigned comment added by 179.219.172.183 (talk) 23:03, 18 November 2013 (UTC)[reply]

We already have more in-depth articles on ad-hoc polymorphism, parametric polymorphism and subtyping. It doesn't make sense to split this into specific articles for OO and FP as, unlike in 1996, most contemporary OO and FP languages support all three forms of polymorphism to a lesser or greater degree. —Ruud 16:47, 6 December 2013 (UTC)[reply]
There used to be a separate article, Polymorphism in object-oriented programming, but imo it wasn't in great shape, and it was merged into this article a few months ago. An older version of the article was written from a more traditional OO perspective (analogous to the way polymorphism is introduced in many introductory Java or C++ textbooks). Whether a separate article makes sense I'm ambivalent about. I do think the current article comes across as somewhat "foreign" to people who come from the OOP world rather than the PLs world. Not hugely so, but somewhat. --Delirium (talk) 17:58, 6 December 2013 (UTC)[reply]

Pure and reverse polymorphism[edit]

The redirects for Pure polymorphism and Reverse polymorphism go to the disambiguation page Polymorphism. Should they actually go to this article's section Types of polymorphism? If so, could someone put a short reference to these concepts here in order that the redirect makes sense. Thanks.--NHSavage (talk) 18:15, 18 December 2013 (UTC)[reply]

The {{Polymorphism}} template adds nothing to the article. The important links are right there in the lead. The links to various types of dynamic dispatch are excessive. This *is* the article on polymorphism. If the concepts in the nav-box aren't in the lead of this article, they aren't worth having in the nav-box. Either the links are redundant, or the whole nav-box is redundant. There is no requirement for us to add a nav-box for consistency with other articles on polymorphism. There is a requirement for us to remove unhelpful clutter from the lead. - Crosbie 18:41, 19 February 2014 (UTC)[reply]

I agree eliminating clutter is good. However, the Polymorphism Sidebar template wouldn't be clutter. If indeed the lead of the article did contain all the links found in the side-bar, there would be concern. Except it wouldn't be concern for duplicate links, it would be the risk of having a confusing introductory paragraph, failing to give a concise overview of the topic. Fortunately, that's not what we have. I encourage you to become familiar with the purpose and current numerous examples of Sidebars throughout Wikipedia (see Category:Sidebar templates). Being a Sidebar, it has no intent of providing information to the reader (as would an Infobox) other than to say, these are related topics to the current article (since, in fact and fortunately, again all of them are not linked in the lead paragraph). You mentioned it doesn't add any information to the article, and this is good because it's not an infobox. As with other articles with Sidebars, the Polymorphism Sidebar template is particularly relevant to the top of this article..digamma (talk) 19:53, 19 February 2014 (UTC)[reply]
I agree with Crosbie, that template is horrible. The links in there either duplicate links already in the lead, or are too specific and belong in one of the sections further down. —Ruud 13:08, 11 July 2016 (UTC)[reply]

Ad hoc polymorphism result[edit]

In the section on ad hoc polymorphism, the result of the writeln calls is never explicitly stated. The section of the article appears to be attempting to illustrate how the two Add functions are different, but doesn't actually demonstrate this. Shouldn't it be explicitly stated that the first will write out '3', and the second will write 'Hello, World!'? Or am I missing something?

Without stating the result explicitly, it doesn't appear demonstrate anything unless you had prior knowledge of what ad hoc polymorphism was, defeating the purpose of that part of this section. The main article calls this out, I imagine this section probably should too.

Slurgi (talk) 19:47, 26 February 2015 (UTC)[reply]

Fixed. —Ruud 22:34, 26 February 2015 (UTC)[reply]

Monomorphism redirects here but isn't mentioned in the page[edit]

I came looking for an explanation of "monomorphization" and couldn't even find an (explicit) explanation of "monomorphism". The sort of answer I was looking for appears here: http://stackoverflow.com/a/14198060. Perhaps some kind wikipedian with a strong CS background could incorporate that into this or a linked page? — Preceding unsigned comment added by 135.23.107.176 (talk) 15:20, 8 December 2015 (UTC)[reply]

That article on SO looks to me to be something different, I've not heard the term monomorphism used but in this case it would be the opposite of some sort of polymorphism, e.g. a class that doesn't implement any of its parent's interface differently from the parent class. 82.71.38.251 (talk) 17:23, 5 May 2016 (UTC)[reply]

Cleaning up the Ad-Hoc subsection[edit]

The Ad-Hoc section is vague and inaccurate in many places, over-specific in others.

  • There's no need to be repeating history details which are already covered in the History section and in the main page for Ad-Hoc Polymorphism
  • "functions that can be applied to arguments of different types, but that behave differently depending on the type of the argument to which they are applied" this is equally true of subtyping and not informative.
  • Function overloading and operator overloading are treated interchangeably, when they are significantly different. In fact, defining function overloading as a type of ad-hoc polymorphism - or polymorphism at all - is contentious.
  • The single line about dynamic types should be saved for the main page on this topic.
  • The single line about coercive polymorphism shouldn't even be in this subsection.

So I'm doing a little pruning Itsbruce (talk) 15:39, 16 August 2018 (UTC)[reply]

Reference 5 is a dead link[edit]

Syrak (talk) 17:31, 14 March 2019 (UTC)[reply]