3 ms·
> It can just compile a single version of the lambda that is capable of handling all possible input types - no polymorphism required, parametric or otherwise.
by colanderman 12y ago
> It can just compile a single version of the lambda that is capable of handling all possible input types - no polymorphism required, parametric or otherwise.
That's the very definition of parametric polymorphism. From http://en.wikipedia.org/wiki/Polymorphism_(computer_science) http://en.wikipedia.org/wiki/Polymorphism_(computer_science): If the code is written without mention of any specific type and thus can be used transparently with any number of new types, it is called parametric polymorphism.
> Whereas in a language like ML with static typing the compiler would need to compile different polymorphic versions of the function for all the different combinations of types for f and x that are being used in the program.
$ echo 'let f g x = g x' > test.ml
$ ocamlopt -c test.ml
$ objdump -d test.o
0000000000000000 <camlTest__f_1030>:
0: 48 89 c6 mov %rax,%rsi
3: 48 89 d8 mov %rbx,%rax
6: 48 8b 3e mov (%rsi),%rdi
9: 48 89 f3 mov %rsi,%rbx
c: ff e7 jmpq *%rdi
e: 66 90 xchg %ax,%ax
Not so much.
(Yes, type specialization is a strategy used by certain whole-program optimizers, such as the MLton compiler, but this is in no way tied to the language or the type system or lack thereof. Types exist whether you name and control them or not, just like tigers and asteroids do.)
- bunderbunder 12y agoPerhaps click the words "parametric polymorphism" in that quote you keep using, and read a bit deeper. Or alternatively, scroll down and read the more detailed exposition on the subject in that same page you link. As is so often the case with technical jargon, a one-sentence definition does not necessarily capture the whole of the concept.
- colanderman 12y agoYes, phrases therein such as "Parametric polymorphism allows a function or a data type to be written generically, so that it can handle values identically without depending on their type." and "A function that can evaluate to or be applied to values of different types is known as a polymorphic function." further reinforce my point that a language can support parametric polymorphism without being statically typed. I don't see why this is not self-evident to you. Any program written in Scheme + static types is a valid program in plain Scheme. The addition of a typing system to the language does not necessarily imbue it with new capabilities, nor (as I showed above) does it necessarily alter the output of the compiler. (Note: I distinguish "typing system" from "type constructors". Obviously adding type constructors adds capability to the language. But typing systems and type constructors exist independently of each other: Prolog has no formal type system, but has type constructors; SQL has a formal type system but no type constructors.)