Questions tagged [parametric-polymorphism]

In computer science, polymorphism is a programming language feature that allows values of different data types to be handled using a uniform interface. The concept of parametric polymorphism applies to both data types and functions.

Parametric polymorphism is a kind of where a function or data type is parameterized over a type. By doing so, the function or data structure will be able to handle any type without knowing anything about it, while consumers will still be able to track which type is handled, thereby maintaining full type-safety.

Examples

OCaml

type 'a tree =
  | Node of 'a * 'a tree * 'a tree
  | Empty

(** val map : ('a -> 'b) -> 'a tree -> 'b tree *)
let rec map f =
  function | Empty -> Empty
           | Node (value, left, right) ->
             Node (f value, map f left, map f right)

(* val int_tree : int tree *)
let int_tree = Node (1, Node (2, Empty, Empty), Empty)

(* val string_tree : string tree *)
let string_tree = int_tree |> map string_of_int

C#

using System;

class Tree<T> {
    public T value;
    public Tree<T> left;
    public Tree<T> right;

    public Tree(T value) {
        this.value = value;
    }

    public Tree<U> Map<U>(Func<T,U> f) {
        var tree = new Tree<U>(f(this.value));
        tree.left = left?.Map(f);
        tree.right = right?.Map(f);
        return tree;
    }
}

public class Program {
    public static void Main(string[] args) {
        var intTree = new Tree<int>(1);
        intTree.left = new Tree<int>(2);

        Tree<string> stringTree = intTree.Map(n => n.ToString());
    }
}

Papers

Types, abstraction, and parametric polymorphism by John C. Reynolds

See also

211 questions
111
votes
7 answers

What is polymorphism in Javascript?

I have read some possible article I could found on the internet on polymorphism. But I think I could not quite grasp the meaning of it and its importance. Most of the articles don't say why it is important and how I can achieve polymorphic behavior…
78
votes
6 answers

Why does Haskell's `head` crash on an empty list (or why *doesn't* it return an empty list)? (Language philosophy)

Note to other potential contributors: Please don't hesitate to use abstract or mathematical notations to make your point. If I find your answer unclear, I will ask for elucidation, but otherwise feel free to express yourself in a comfortable…
Jack Henahan
  • 1,356
  • 1
  • 12
  • 17
45
votes
4 answers

What's the difference between parametric polymorphism and higher-kinded types?

I am pretty sure they are not the same. However, I am bogged down by the common notion that "Rust does not support" higher-kinded types (HKT), but instead offers parametric polymorphism. I tried to get my head around that and understand the…
43
votes
2 answers

When and why to use AsRef instead of &T

AsRef documentation writes Used to do a cheap reference-to-reference conversion. I understand reference-to-reference part what does it mean by cheap? I hope it has nothing to do with complexity theoretic (big Oh,. etc) "cheapness". Example: struct…
storm
  • 795
  • 1
  • 5
  • 12
27
votes
2 answers

Use case for rank-3 (or higher) polymorphism?

I've seen a few use cases for rank-2 polymorphism (the most prominent example being the ST monad), but none for a higher rank than that. Does anyone know of such a use case?
23
votes
3 answers

What are type quantifiers?

Many statically typed languages have parametric polymorphism. For example in C# one can define: T Foo(T x){ return x; } In a call site you can do: int y = Foo(3); These types are also sometimes written like this: Foo :: forall T. T -> T I…
Jules
  • 6,318
  • 2
  • 29
  • 40
20
votes
3 answers

Advanced polymorphism in Java

I have classes in advanced programming at my university and I have a little trouble understanding how this code works. public final class GenericClass { private void overloadedMethod(Collection o) { …
18
votes
4 answers

How do I get around Go not having parametric polymorphism?

I'm a Go newcomer, but I have read that Go regulars do not miss parametric polymorphism. Every time I try to learn a new language I use the L99 list of problems to get some practice. Even if I try to write something as trivial as the first problem…
trh178
  • 11,228
  • 5
  • 28
  • 37
17
votes
1 answer

Why does GHC infer a monomorphic type here, even with MonomorphismRestriction disabled?

This was prompted by Resolving the type of `f = f (<*>) pure`, which discusses a more complicated example, but this one works too. The following definition compiles without problem: w :: Integral a => a w = fromInteger w ...Of course it doesn't…
leftaroundabout
  • 117,950
  • 5
  • 174
  • 319
16
votes
5 answers

Why is forall a. a not considered a subtype of Int while I can use an expression of type forall a. a anywhere one of type Int is expected?

Consider the following pair of function definitions, which pass the type checker: a :: forall a. a a = undefined b :: Int b = a I.e. an expression of type forall a. a can be used where one of type Int is expected. This seems to me a lot like…
user1804599
16
votes
0 answers

Is a function that curries the first argument of a list of functions typeable in Typed Racket?

I can write a simple function in untyped Racket called curry-all that takes a list of functions, all of which accept the same kind of value for their first argument, and produces a list of functions with their first arguments curried. (define…
Alexis King
  • 43,109
  • 15
  • 131
  • 205
15
votes
2 answers

What is the 'msg' in 'HTML msg' in Elm actually?

I'm teaching myself elm and see (of course) many references to Html msg-- I understand that this is a 'parameterised type', that is, that (as I understand it), the constructor of the type Html takes a parameter -- much as List Char does. OK. But…
Cerulean
  • 5,543
  • 9
  • 59
  • 111
14
votes
2 answers

Type signature in a where clause

I've written a function similar to Data.Enumerator.List.map that makes an Iteratee compatible with an Enumerator that feeds a different Stream type. import Data.Enumerator test :: Monad m => (ao -> ai) -> Iteratee ai m b -> Iteratee ao m b test f…
qubital
  • 1,064
  • 9
  • 14
14
votes
2 answers

In Haskell, are "higher-kinded types" *really* types? Or do they merely denote collections of *concrete* types and nothing more?

Paramametrically polymorphic functions Consider the following function: f :: a -> Int f x = (1 :: Int) We might say that the type of f is a -> Int, and that f therefore is of a "polymorphic" type. Which of the following is the most accurate way to…
George
  • 6,927
  • 4
  • 34
  • 67
13
votes
3 answers

Which is a polymorphic type: a type or a set of types?

Programming in Haskell by Hutton says: A type that contains one or more type variables is called polymorphic. Which is a polymorphic type: a type or a set of types? Is a polymorphic type with a concrete type substituting its type variable a…
Tim
  • 1
  • 141
  • 372
  • 590
1
2 3
14 15