What are type quantifiers?

后端 未结 3 1681
温柔的废话
温柔的废话 2021-02-04 01:52

Many statically typed languages have parametric polymorphism. For example in C# one can define:

T Foo(T x){ return x; }

In a call site

3条回答
  •  天命终不由人
    2021-02-04 02:35

    if forall is lambda ..., then what is exists

    Why, tuple of course!

    In Martin-Löf type theory you have Π types, corresponding to functions/universal quantification and Σ-types, corresponding to tuples/existential quantification.

    Their types are very similar to what you have proposed (I am using Agda notation here):

    Π : (A : Set) -> (A -> Set) -> Set
    Σ : (A : Set) -> (A -> Set) -> Set
    

    Indeed, Π is an infinite product and Σ is infinite sum. Note that they are not "intersection" and "union" though, as you proposed because you can't do that without additionally defining where the types intersect. (which values of one type correspond to which values of the other type)

    From these two type constructors you can have all of normal, polymorphic and dependent functions, normal and dependent tuples, as well as existentially and universally-quantified statements:

    -- Normal function, corresponding to "Integer -> Integer" in Haskell
    factorial : Π ℕ (λ _ → ℕ)
    
    -- Polymorphic function corresponding to "forall a . a -> a"
    id : Π Set (λ A -> Π A (λ _ → A))
    
    -- A universally-quantified logical statement: all natural numbers n are equal to themselves
    refl : Π ℕ (λ n → n ≡ n)
    
    
    -- (Integer, Integer)
    twoNats : Σ ℕ (λ _ → ℕ)
    
    -- exists a. Show a => a
    someShowable : Σ Set (λ A → Σ A (λ _ → Showable A))
    
    -- There are prime numbers
    aPrime : Σ ℕ IsPrime
    

    However, this does not address parametricity at all and AFAIK parametricity and Martin-Löf type theory are independent.

    For parametricity, people usually refer to the Philip Wadler's work.

提交回复
热议问题