Zulip Chat Archive

Stream: Type theory

Topic: dependent types and parametric polymorphism in lean


Bulhwi Cha (Oct 10 2024 at 11:44):

Are dependent functions and dependent products in Lean parametrically polymorphic?

πš πš˜πš“πšŒπš’πšŽπšŒπš‘ πš—πšŠπš πš›πš˜πšŒπš”πš’ (Oct 10 2024 at 16:27):

Parametricity is not provable in Lean, and it's not true in all models of Lean + mathlib axioms; in particular, it's inconsistent with the axiom of choice. Check out also previous discussions here or here.

Bulhwi Cha (Oct 11 2024 at 01:43):

From https://docs.lean-lang.org/theorem_proving_in_lean4/dependent_type_theory.html:

Suppose you wish to write a function cons which inserts a new element at the head of a list. What type should cons have? Such a function is polymorphic: you expect the cons function for Nat, Bool, or an arbitrary type Ξ± to behave the same way.

What does it mean that a function or datatype is parametrically polymorphic? What does the word "polymorphic" mean in the above context? I don't think it means universe polymorphism.

Bulhwi Cha (Oct 11 2024 at 01:50):

I read a Wikipedia article about parametric polymorphism and thought dependent functions and products in Lean are parametrically polymorphic. It seems I misunderstood the notion of parametric polymorphism. What is it, then?

Bulhwi Cha (Oct 11 2024 at 01:58):

Wojciech Nawrocki said:

Parametricity is not provable in Lean, and it's not true in all models of Lean + mathlib axioms; in particular, it's inconsistent with the axiom of choice. Check out also previous discussions here or here.

I'm not trying to prove the parametricity theorem; I haven't heard of it until now. I just want to check my understanding of parametric polymorphism.

πš πš˜πš“πšŒπš’πšŽπšŒπš‘ πš—πšŠπš πš›πš˜πšŒπš”πš’ (Oct 11 2024 at 02:34):

Ah, sorry! I misread this as a question about parametricity, but you are just asking about polymorphism. Yes, dependent functions are polymorphic.

What does it mean that a function or datatype is parametrically polymorphic?

It just means that we have one implementation of cons which works for lists containing elements of any type. That is, consing a Boolean onto a List Bool uses the same function as consing a number onto a List Nat, etc. The way this is formalized in dependent type theory is that the first argument of List.cons is a type, and then List.cons Bool (or List.cons Nat or whatever) is a "monomorphic" function of type Bool -> List Bool -> List Bool (or analogous).

Bulhwi Cha (Oct 11 2024 at 02:54):

Thanks. One more question: are dependent products (pairs) in Lean polymorphic? It doesn't seem so.

Marcus Rossel (Oct 11 2024 at 06:50):

Note that "polymorphic" isn't a precise term in this context. Quoting from Types and Programming Languages:

The term polymorphism refers to a range of language mechanisms that allow a single part of a program to be used with different types in different contexts (Β§23.2 discusses several varieties of polymorphism in more detail).

Β§23.2

By that description, I'd argue that dependent products are polymorphic.


Last updated: May 02 2025 at 03:31 UTC