Generic Programming With Dependent Types: III - University of
Dependent Types for Mere Mortalslkuper/talks/dependent-types/...for Mere Mortals Lindsey Kuper...
Transcript of Dependent Types for Mere Mortalslkuper/talks/dependent-types/...for Mere Mortals Lindsey Kuper...
![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](https://reader033.fdocuments.net/reader033/viewer/2022050204/5f5800d319a6ed69a1729a17/html5/thumbnails/1.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022050204/5f5800d319a6ed69a1729a17/html5/thumbnails/2.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022050204/5f5800d319a6ed69a1729a17/html5/thumbnails/3.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022050204/5f5800d319a6ed69a1729a17/html5/thumbnails/4.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022050204/5f5800d319a6ed69a1729a17/html5/thumbnails/5.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022050204/5f5800d319a6ed69a1729a17/html5/thumbnails/6.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022050204/5f5800d319a6ed69a1729a17/html5/thumbnails/7.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022050204/5f5800d319a6ed69a1729a17/html5/thumbnails/8.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022050204/5f5800d319a6ed69a1729a17/html5/thumbnails/9.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022050204/5f5800d319a6ed69a1729a17/html5/thumbnails/10.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022050204/5f5800d319a6ed69a1729a17/html5/thumbnails/11.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022050204/5f5800d319a6ed69a1729a17/html5/thumbnails/12.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022050204/5f5800d319a6ed69a1729a17/html5/thumbnails/13.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022050204/5f5800d319a6ed69a1729a17/html5/thumbnails/14.jpg)
• 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](https://reader033.fdocuments.net/reader033/viewer/2022050204/5f5800d319a6ed69a1729a17/html5/thumbnails/15.jpg)
• 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](https://reader033.fdocuments.net/reader033/viewer/2022050204/5f5800d319a6ed69a1729a17/html5/thumbnails/16.jpg)
• 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](https://reader033.fdocuments.net/reader033/viewer/2022050204/5f5800d319a6ed69a1729a17/html5/thumbnails/17.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022050204/5f5800d319a6ed69a1729a17/html5/thumbnails/18.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022050204/5f5800d319a6ed69a1729a17/html5/thumbnails/19.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022050204/5f5800d319a6ed69a1729a17/html5/thumbnails/20.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022050204/5f5800d319a6ed69a1729a17/html5/thumbnails/21.jpg)
• 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](https://reader033.fdocuments.net/reader033/viewer/2022050204/5f5800d319a6ed69a1729a17/html5/thumbnails/22.jpg)
• 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](https://reader033.fdocuments.net/reader033/viewer/2022050204/5f5800d319a6ed69a1729a17/html5/thumbnails/23.jpg)
• 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](https://reader033.fdocuments.net/reader033/viewer/2022050204/5f5800d319a6ed69a1729a17/html5/thumbnails/24.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022050204/5f5800d319a6ed69a1729a17/html5/thumbnails/25.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022050204/5f5800d319a6ed69a1729a17/html5/thumbnails/26.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022050204/5f5800d319a6ed69a1729a17/html5/thumbnails/27.jpg)
• 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](https://reader033.fdocuments.net/reader033/viewer/2022050204/5f5800d319a6ed69a1729a17/html5/thumbnails/28.jpg)
• 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](https://reader033.fdocuments.net/reader033/viewer/2022050204/5f5800d319a6ed69a1729a17/html5/thumbnails/29.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022050204/5f5800d319a6ed69a1729a17/html5/thumbnails/30.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022050204/5f5800d319a6ed69a1729a17/html5/thumbnails/31.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022050204/5f5800d319a6ed69a1729a17/html5/thumbnails/32.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022050204/5f5800d319a6ed69a1729a17/html5/thumbnails/33.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022050204/5f5800d319a6ed69a1729a17/html5/thumbnails/34.jpg)
(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](https://reader033.fdocuments.net/reader033/viewer/2022050204/5f5800d319a6ed69a1729a17/html5/thumbnails/35.jpg)
Equivalence rules for !LF
(from ATTAPL p. 52)Thursday, February 25, 2010