dependent types for mere mortalslkuper/talks/dependent-types/...for mere mortals lindsey kuper...

35
Dependent Types for Mere Mortals Lindsey Kuper Thursday, February 25, 2010

Upload: others

Post on 19-Jul-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Dependent Types for Mere Mortalslkuper/talks/dependent-types/...for Mere Mortals Lindsey Kuper Thursday, February 25, 2010 Motivation Thursday, February 25, 2010 Motivation • Types

Dependent Types for Mere Mortals

Lindsey Kuper

Thursday, February 25, 2010

Page 2: Dependent Types for Mere Mortalslkuper/talks/dependent-types/...for Mere Mortals Lindsey Kuper Thursday, February 25, 2010 Motivation Thursday, February 25, 2010 Motivation • Types

Motivation

Thursday, February 25, 2010

Page 3: Dependent Types for Mere Mortalslkuper/talks/dependent-types/...for Mere Mortals Lindsey Kuper Thursday, February 25, 2010 Motivation Thursday, February 25, 2010 Motivation • Types

Motivation

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

Thursday, February 25, 2010

Page 4: Dependent Types for Mere Mortalslkuper/talks/dependent-types/...for Mere Mortals Lindsey Kuper Thursday, February 25, 2010 Motivation Thursday, February 25, 2010 Motivation • Types

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

Page 5: Dependent Types for Mere Mortalslkuper/talks/dependent-types/...for Mere Mortals Lindsey Kuper Thursday, February 25, 2010 Motivation Thursday, February 25, 2010 Motivation • Types

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

Page 6: Dependent Types for Mere Mortalslkuper/talks/dependent-types/...for Mere Mortals Lindsey Kuper Thursday, February 25, 2010 Motivation Thursday, February 25, 2010 Motivation • Types

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

Page 7: Dependent Types for Mere Mortalslkuper/talks/dependent-types/...for Mere Mortals Lindsey Kuper Thursday, February 25, 2010 Motivation Thursday, February 25, 2010 Motivation • Types

Motivation

Thursday, February 25, 2010

Page 8: Dependent Types for Mere Mortalslkuper/talks/dependent-types/...for Mere Mortals Lindsey Kuper Thursday, February 25, 2010 Motivation Thursday, February 25, 2010 Motivation • Types

Motivation

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

Thursday, February 25, 2010

Page 9: Dependent Types for Mere Mortalslkuper/talks/dependent-types/...for Mere Mortals Lindsey Kuper Thursday, February 25, 2010 Motivation Thursday, February 25, 2010 Motivation • Types

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

Page 10: Dependent Types for Mere Mortalslkuper/talks/dependent-types/...for Mere Mortals Lindsey Kuper Thursday, February 25, 2010 Motivation Thursday, February 25, 2010 Motivation • Types

Consider a type family Vector.

Canonical example

Vector :: Nat *!

Thursday, February 25, 2010

Page 11: Dependent Types for Mere Mortalslkuper/talks/dependent-types/...for Mere Mortals Lindsey Kuper Thursday, February 25, 2010 Motivation Thursday, February 25, 2010 Motivation • Types

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

Page 12: Dependent Types for Mere Mortalslkuper/talks/dependent-types/...for Mere Mortals Lindsey Kuper Thursday, February 25, 2010 Motivation Thursday, February 25, 2010 Motivation • Types

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

Page 13: Dependent Types for Mere Mortalslkuper/talks/dependent-types/...for Mere Mortals Lindsey Kuper Thursday, February 25, 2010 Motivation Thursday, February 25, 2010 Motivation • Types

types!

Thursday, February 25, 2010

Page 14: Dependent Types for Mere Mortalslkuper/talks/dependent-types/...for Mere Mortals Lindsey Kuper Thursday, February 25, 2010 Motivation Thursday, February 25, 2010 Motivation • Types

• 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

Page 15: Dependent Types for Mere Mortalslkuper/talks/dependent-types/...for Mere Mortals Lindsey Kuper Thursday, February 25, 2010 Motivation Thursday, February 25, 2010 Motivation • Types

• 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

Page 16: Dependent Types for Mere Mortalslkuper/talks/dependent-types/...for Mere Mortals Lindsey Kuper Thursday, February 25, 2010 Motivation Thursday, February 25, 2010 Motivation • Types

• 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

Page 17: Dependent Types for Mere Mortalslkuper/talks/dependent-types/...for Mere Mortals Lindsey Kuper Thursday, February 25, 2010 Motivation Thursday, February 25, 2010 Motivation • Types

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

Page 18: Dependent Types for Mere Mortalslkuper/talks/dependent-types/...for Mere Mortals Lindsey Kuper Thursday, February 25, 2010 Motivation Thursday, February 25, 2010 Motivation • Types

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

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

Page 19: Dependent Types for Mere Mortalslkuper/talks/dependent-types/...for Mere Mortals Lindsey Kuper Thursday, February 25, 2010 Motivation Thursday, February 25, 2010 Motivation • Types

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

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

Page 20: Dependent Types for Mere Mortalslkuper/talks/dependent-types/...for Mere Mortals Lindsey Kuper Thursday, February 25, 2010 Motivation Thursday, February 25, 2010 Motivation • Types

types!

Thursday, February 25, 2010

Page 21: Dependent Types for Mere Mortalslkuper/talks/dependent-types/...for Mere Mortals Lindsey Kuper Thursday, February 25, 2010 Motivation Thursday, February 25, 2010 Motivation • Types

• 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

Page 22: Dependent Types for Mere Mortalslkuper/talks/dependent-types/...for Mere Mortals Lindsey Kuper Thursday, February 25, 2010 Motivation Thursday, February 25, 2010 Motivation • Types

• 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

Page 23: Dependent Types for Mere Mortalslkuper/talks/dependent-types/...for Mere Mortals Lindsey Kuper Thursday, February 25, 2010 Motivation Thursday, February 25, 2010 Motivation • Types

• 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

Page 24: Dependent Types for Mere Mortalslkuper/talks/dependent-types/...for Mere Mortals Lindsey Kuper Thursday, February 25, 2010 Motivation Thursday, February 25, 2010 Motivation • Types

Definition of , plus types!LF

(from ATTAPL p. 62)

!

Thursday, February 25, 2010

Page 25: Dependent Types for Mere Mortalslkuper/talks/dependent-types/...for Mere Mortals Lindsey Kuper Thursday, February 25, 2010 Motivation Thursday, February 25, 2010 Motivation • Types

Definition of , plus types!LF

(from ATTAPL p. 62)

!

Thursday, February 25, 2010

Page 26: Dependent Types for Mere Mortalslkuper/talks/dependent-types/...for Mere Mortals Lindsey Kuper Thursday, February 25, 2010 Motivation Thursday, February 25, 2010 Motivation • Types

Working with types!

Thursday, February 25, 2010

Page 27: Dependent Types for Mere Mortalslkuper/talks/dependent-types/...for Mere Mortals Lindsey Kuper Thursday, February 25, 2010 Motivation Thursday, February 25, 2010 Motivation • Types

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

Working with types!

!n:Nat. Vector n

Thursday, February 25, 2010

Page 28: Dependent Types for Mere Mortalslkuper/talks/dependent-types/...for Mere Mortals Lindsey Kuper Thursday, February 25, 2010 Motivation Thursday, February 25, 2010 Motivation • Types

• 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

Page 29: Dependent Types for Mere Mortalslkuper/talks/dependent-types/...for Mere Mortals Lindsey Kuper Thursday, February 25, 2010 Motivation Thursday, February 25, 2010 Motivation • Types

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

Page 30: Dependent Types for Mere Mortalslkuper/talks/dependent-types/...for Mere Mortals Lindsey Kuper Thursday, February 25, 2010 Motivation Thursday, February 25, 2010 Motivation • Types

Dependent ML

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

• Decidable typechecking (cf. Cayenne).

Thursday, February 25, 2010

Page 31: Dependent Types for Mere Mortalslkuper/talks/dependent-types/...for Mere Mortals Lindsey Kuper Thursday, February 25, 2010 Motivation Thursday, February 25, 2010 Motivation • Types

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

Page 32: Dependent Types for Mere Mortalslkuper/talks/dependent-types/...for Mere Mortals Lindsey Kuper Thursday, February 25, 2010 Motivation Thursday, February 25, 2010 Motivation • Types

And all of this is part of...

Barendregt’s -cube!

Thursday, February 25, 2010

Page 33: Dependent Types for Mere Mortalslkuper/talks/dependent-types/...for Mere Mortals Lindsey Kuper Thursday, February 25, 2010 Motivation Thursday, February 25, 2010 Motivation • Types

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

Page 34: Dependent Types for Mere Mortalslkuper/talks/dependent-types/...for Mere Mortals Lindsey Kuper Thursday, February 25, 2010 Motivation Thursday, February 25, 2010 Motivation • Types

(exit)

Thursday, February 25, 2010

Page 35: Dependent Types for Mere Mortalslkuper/talks/dependent-types/...for Mere Mortals Lindsey Kuper Thursday, February 25, 2010 Motivation Thursday, February 25, 2010 Motivation • Types

Equivalence rules for !LF

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