Generic Programming With Dependent Types: I
Generic Programming With Dependent Types: I
Stephanie Weirich
University of Pennsylvania
March 2426, 2010 SSGIP
Generic programming is a
killer-app
for dependently-typed
Generic programming is a
killer-app
for dependently-typed
Goals:
Goals:
1
Goals:
1
Goals:
1
Non-goals:
Goals:
1
Non-goals:
1
Goals:
1
Non-goals:
1
Goals:
1
Non-goals:
1
Goals:
1
Non-goals:
1
Goals:
1
Non-goals:
1
Goals:
1
Non-goals:
1
Goals:
1
Non-goals:
1
Goals:
1
Non-goals:
1
http://wiki.portal.chalmers.se/agda/
Agda Wiki
type-generic programming. In
pages 1526, January 2010
3
http://www.seas.upenn.edu/~sweirich/ssgip/
What is Agda?
What is Agda?
data Bool :
true
false
:
:
Set
Bool
Bool
where
data Bool :
true
false
:
:
Set
where
Bool
Bool
Bool Bool
= true
= false
Bool
true
data Bool :
true
false
:
:
Set
where
Bool
Bool
Bool Bool
= true
= false
Bool
true
: { A } Bool A A A
if true then e1 else e2 = e1
if false then e1 else e2 = e2
if_then_else
Inductive datatypes
Datatypes can be inductive
data N :
zero
suc
Set
where
: N
: NN
Inductive datatypes
Datatypes can be inductive
data N :
zero
suc
Set
where
: N
: NN
: { A } N A List A
replicate zero
x = []
replicate (suc n) x = (x :: replicate n x)
replicate
Inductive datatypes
Datatypes can be inductive
data N :
zero
suc
Set
where
: N
: NN
: { A } N A List A
replicate zero
x = []
replicate (suc n) x = (x :: replicate n x)
replicate
: { A } (x :
length (replicate n x) n
replicate-spec
A)
(n : N)
data Vec (A :
[]
_::_
data Vec (A :
[]
_::_
: { A } (n : N) A Vec A n
x = []
repeat (suc n) x = x :: repeat n x
repeat
repeat zero
List "zap"
__
List "zap"
__
Vec "zap"
: { A B n } Vec (A B) n Vec A n Vec B n
~ []
= []
(a :: As) ~ (b :: Bs) = (a b :: As ~ Bs)
_~_
[]
Main thesis:
Dependently typed languages are not just for eliminating bugs, they
enable Generic Programming
Main thesis:
Dependently typed languages are not just for eliminating bugs, they
enable Generic Programming
But what is generic programming?
Main thesis:
Dependently typed languages are not just for eliminating bugs, they
enable Generic Programming
But what is generic programming? Lots of dierent denitions, but
they all boil down to lifting data structures and algorithms from
concrete instances to general forms.
Generalizing programs
Specic cases
zerox
zerox f x
onex
onex f x
twox
twox f x
Generalizing programs
Specic cases
zerox
zerox f x
onex
onex f x
twox
twox f x
Generic function
: N (Bool Bool) Bool Bool
f x = x
nx (suc n) f x = nx n f (f x)
nx
nx zero
Parametric polymorphism
Specic cases
app-nat
app-nat f x
app-bool
app-bool f x
: (N N) N N
= fx
: (Bool Bool) Bool Bool
= fx
Parametric polymorphism
Specic cases
app-nat
app-nat f x
app-bool
app-bool f x
: (N N) N N
= fx
: (Bool Bool) Bool Bool
= fx
Generic function
app
: { A } (A A) A A
= fx
app f x
Ad hoc polymorphism
: N N Bool
zero
=
eq-nat (suc n) (suc m) =
eq-nat
=
eq-nat
eq-nat zero
eq-bool
eq-bool false
eq-bool
eq-nat n m
false
Bool Bool
false
= true
true
= true
= false
Bool
eq-bool true
true
Ad hoc polymorphism
: N N Bool
zero
=
eq-nat (suc n) (suc m) =
eq-nat
=
eq-nat
eq-nat zero
eq-bool
true
eq-nat n m
false
Bool Bool
false
= true
true
= true
= false
Bool
eq-bool false
eq-bool true
eq-bool
: (b : Bool) J b K J b K Bool
eq-nat-bool true = eq-nat
eq-nat-bool false = eq-bool
eq-nat-bool
data Type :
nat
bool
pair
:
:
:
Set
where
Type
Type
Type
Type Type
data Type :
nat
bool
pair
:
:
:
Set
where
Type
Type
Type
Type Type
data Type :
nat
bool
pair
:
:
:
Set
where
Type
Type
Type
Type Type
: (t :
eq nat
eq bool
eq
(pair t1
J t K J t K Bool
y
= eq-nat x y
x
y
= eq-bool x y
t2 ) (x1 ,x2 ) (y1 ,y2 ) = eq t1 x1 y1 eq t2
Type)
x
x2 y2
Expressiveness
Patterns in both types and denitions
: {A B} B A B
zeroApp f x = f
zeroApp
oneApp
: { A B } (A B) A B
= fx
oneApp f x
twoApp
: { A B } (A A B) A B
= fxx
twoApp f x
Expressiveness
Patterns in both types and denitions
: {A B} B A B
zeroApp f x = f
zeroApp
oneApp
: { A B } (A B) A B
= fx
oneApp f x
twoApp
: { A B } (A A B) A B
= fxx
twoApp f x
NAPP zero
: { A B } (n : N) NAPP n A B A B
f x = f
nApp (suc n) f x = nApp n (f x) x
nApp
nApp zero
Strong elimination
if b then
N else Bool
Strong elimination
if b then
N else Bool
f true
Strong elimination
if b then
N else Bool
f true
Overall
Uniform extension of the notion of programmability from run-time
to compile-time.
: {A} A
= anything
: {A} A
= anything
Predicative polymorphism
Strictly-positive datatypes
Restrictions hinder
compile-time programmability.
Predicative polymorphism
Strictly-positive datatypes
Restrictions hinder
compile-time programmability.
-type-in-type
Predicative polymorphism
Strictly-positive datatypes
Restrictions hinder
compile-time programmability.
-type-in-type
functions -no-termination-check
Predicative polymorphism
Structural recursive
Strictly-positive datatypes
Restrictions hinder
compile-time programmability.
-type-in-type
Structural recursive functions -no-termination-check
Strictly-positive datatypes -no-positivity-check
Predicative polymorphism
Restrictions hinder
compile-time programmability.
Set
f x = (x x)
g : (A : Set) A A
g A x = x
f
Set
Set
f x = (x x)
g : (A : Set) A A
g A x = x
f
Set
h x
( A A)
= if x then (N,0) else (Bool,true)
Bool
Set
f x = (x x)
g : (A : Set) A A
g A x = x
f
Set
h x
( A A)
= if x then (N,0) else (Bool,true)
Bool
List Set
Type in Type
Both types and regular data may be inferred by the type checker
(as implicit arguments) and symbolically evaluated at compile time.
Type in Type
Both types and regular data may be inferred by the type checker
(as implicit arguments) and symbolically evaluated at compile time.
Type in Type
Both types and regular data may be inferred by the type checker
(as implicit arguments) and symbolically evaluated at compile time.
Type in Type
Both types and regular data may be inferred by the type checker
(as implicit arguments) and symbolically evaluated at compile time.
Type in Type
Both types and regular data may be inferred by the type checker
(as implicit arguments) and symbolically evaluated at compile time.
`e :
t then
`t :
Type in Type
Type in Type
-type-in-type
enables
` Set :
Set
Type in Type
-type-in-type
enables
` Set :
Set
: { A n } Vec A (suc n) A
head (x :: xs) = x
head
Type in Type
-type-in-type
enables
` Set :
Set
: { A n } Vec A (suc n) A
head (x :: xs) = x
head
x
x
: Set
= head (N :: Bool :: [])
: { A n } Vec A (suc n) A
head (x :: xs) = x
head
: { A n } Vec A (suc n) A
head (x :: xs) = x
head
Have
partial correctness:
Caveats:
Invalid proofs can also cause programs to diverge. (And can't
erase them either!)
Implications are not to be trusted.
Coming next...
Coming next...
Coming next...
Arity-generic programming