Dependent Types for Mere Mortalslkuper/talks/dependent-types/...for Mere Mortals Lindsey Kuper...

Post on 19-Jul-2020

3 views 0 download

Transcript of Dependent Types for Mere Mortalslkuper/talks/dependent-types/...for Mere Mortals Lindsey Kuper...

Dependent Types for Mere Mortals

Lindsey Kuper

Thursday, February 25, 2010

Motivation

Thursday, February 25, 2010

Motivation

• Types let us specify some aspects of what can and cannot be done with data.

Thursday, February 25, 2010

Motivation

• Types let us specify some aspects of what can and cannot be done with data.

append : Vector n n Vector m Vector n+m n+m

!!dequeue : Queue n+1 data!

Thursday, February 25, 2010

Motivation

• Types let us specify some aspects of what can and cannot be done with data.

(n, m : Nat)

append : Vector n n Vector m Vector n+m n+m

!!dequeue : Queue n+1 data!append : Vector n n Vector m Vector n+m

n+m!!

dequeue : Queue n+1 data!

Thursday, February 25, 2010

Motivation

• Types let us specify some aspects of what can and cannot be done with data.

• Dependent types allow us to be as precise as we like with those specifications.

(n, m : Nat)

append : Vector n n Vector m Vector n+m n+m

!!dequeue : Queue n+1 data!append : Vector n n Vector m Vector n+m

n+m!!

dequeue : Queue n+1 data!

Thursday, February 25, 2010

Motivation

Thursday, February 25, 2010

Motivation

• Aren’t we just “moving around the work”?

Thursday, February 25, 2010

Motivation

• Aren’t we just “moving around the work”?

• If we can express a proof as a program, then checking that the proof is correct amounts to typechecking the program.

“While the price for formally certified software may be high, it is good to know that we can pay it in installments.”

McKinna, “Why Dependent Types Matter”

Thursday, February 25, 2010

Consider a type family Vector.

Canonical example

Vector :: Nat *!

Thursday, February 25, 2010

Consider a type family Vector.

cons : n:Nat. data n Vector n Vector n+1+m!!!

Canonical example

Vector :: Nat *!

empty : Vector 0

init : n:Nat. data n Vector n Vector n+1+m!!

Thursday, February 25, 2010

Dependent types defined

• Dependent types, in general, are functions that return types.

• We’ll use it to mean functions that take terms and return types.

• Our dependently-typed language: , extended with -types and CoC.

!LF!

Thursday, February 25, 2010

types!

Thursday, February 25, 2010

• A generalization of arrow types. We could treat as the type , but only if does not appear free in .

types

! ! ! !!x : !. ! !

x ! !

!

Thursday, February 25, 2010

• A generalization of arrow types. We could treat as the type , but only if does not appear free in .

• Not entirely unlike System F’s types, but the bound variable is a term, not a type.

types

! ! ! !!x : !. ! !

x ! !

!

!

Thursday, February 25, 2010

• A generalization of arrow types. We could treat as the type , but only if does not appear free in .

• Not entirely unlike System F’s types, but the bound variable is a term, not a type.

• The name comes from “Product”: an artifact of the way the quantifier is interpreted in classical logic.

types

! ! ! !!x : !. ! !

x ! !

!

!

!

Thursday, February 25, 2010

We can now write functions that cannot be applied to the wrong arguments.

Here, first 0 returns a function that must be applied to something of type Vector 1, at least.

first : n:Nat. Vector(n+1) n data !!

What good is this?

Thursday, February 25, 2010

Definition of !LF (don’t panic. it’s just STLC plus )!

(from ATTAPL p. 51)Thursday, February 25, 2010

Definition of !LF (don’t panic. it’s just STLC plus )!

(from ATTAPL p. 51)Thursday, February 25, 2010

types!

Thursday, February 25, 2010

• A generalization of pair types. We could treat as the type , but only if does not appear free in .

types

x ! !

!

!x : !. ! ! ! ! ! !

Thursday, February 25, 2010

• A generalization of pair types. We could treat as the type , but only if does not appear free in .

• The name comes from “Sum”: an artifact of the way the quantifier is interpreted in classical logic.

types

x ! !

!

!

!x : !. ! ! ! ! ! !

Thursday, February 25, 2010

• A generalization of pair types. We could treat as the type , but only if does not appear free in .

• The name comes from “Sum”: an artifact of the way the quantifier is interpreted in classical logic.

• We can encode modules with types (see: Cayenne).

types

x ! !

!

!

!x : !. ! ! ! ! ! !

!

Thursday, February 25, 2010

Definition of , plus types!LF

(from ATTAPL p. 62)

!

Thursday, February 25, 2010

Definition of , plus types!LF

(from ATTAPL p. 62)

!

Thursday, February 25, 2010

Working with types!

Thursday, February 25, 2010

• Pairs of a Nat and a Vector of that length have type

Working with types!

!n:Nat. Vector n

Thursday, February 25, 2010

• Pairs of a Nat and a Vector of that length have type

• We can “package” types with axioms restricting their elements. The type of binary, associative operations on :

Working with types!

!m : ! ! ! ! !.!x : !. !y : !. !z : !.

Id(m(x, m(y, z)))(m(m(x, y), z))

!

!n:Nat. Vector n

Thursday, February 25, 2010

The bad news

• If nonterminating terms (or terms with side effects!) appear in types, dependent type checking is undecidable.

• In practice, though, there are reasonable workarounds.

Thursday, February 25, 2010

Dependent ML

• Restricted form of dependent types: only index sorts may depend on terms.

• Decidable typechecking (cf. Cayenne).

Thursday, February 25, 2010

The Calculus of Constructions

• Here we have dependent types and impredicativity, giving us a terrifyingly expressive type system.

• Extended with inductive datatypes to form CIC, the basis of Coq.

Thursday, February 25, 2010

And all of this is part of...

Barendregt’s -cube!

Thursday, February 25, 2010

Bibliography

• David Aspinall and Martin Hofmann, “Dependent Types” (Chapter 2 of ATTAPL)

• Lennart Augustsson, “Cayenne: A Language With Dependent Types” (ICFP 1998)

• James McKinna, “Why Dependent Types Matter” (POPL 2006)

Thursday, February 25, 2010

(exit)

Thursday, February 25, 2010

Equivalence rules for !LF

(from ATTAPL p. 52)Thursday, February 25, 2010