13. Coq 3Z$7$=$&
: CPDT
M2 $O8&5f$K Coq $r;H$($F$J ... ! (;$?$
H !)
in $*CBgc 7/24
14. Coq 3Z$7h Coq
ICFP2009 $N B. C. Pierce $N>7BT9V1i
“Lambda, the Ultimate TA: Using a Proof Assistant
to Teach Programming Language Foundations”
from
Theory of PL for PL geeks
to
Software Foundation to the masses
http://www.cis.upenn.edu/~bcpierce/sf
in $*CBgc 9/24
17. factorial 4X?t (CPS)
let rec fac n =
if n <= 0 then 1
else n * fac ( n - 1 )
let rec fac_cps n k =
if n <= 0 then k 1
else
fac_cps ( n - 1 ) ( fun x - > k ( n * x ) )
let main n = fac_cps n ( fun x - > x )
in $*CBgc 12/24
18. factorial 4X?t (Hs4X?t2= )
type cont = C0 | C1 of int * cont
let rec apply k x = match k with
| C0 - > x
| C1 (n , k ) - > apply k ( n * x )
let rec fac_defunc n k =
if n <= 0 then apply k 1
else fac_defunc ( n - 1 ) ( C1 (n , k ) )
let main n = fac_defunc n C0
in $*CBgc 13/24
22. CPS JQ49$N@5Ev- (factorial)
Theorem
∀n ∈ N, f acds n = f accps n id
in $*CBgc 16/24
23. CPS JQ49$N@5Ev- (factorial)
Theorem
∀n ∈ N, f acds n = f accps n id
Lemma
∀n ∈ N, k ∈ N → N,
k (f acds n) = f accps n k
>ZL@ : n $K4X$9$k5"G<K!
in $*CBgc 16/24
24. CPS JQ49$N@5Ev- (factorial)
Theorem
∀n ∈ N, f acds n = f accps n id
Lemma
∀n ∈ N, k ∈ N → N,
k (f acds n) = f accps n k
>ZL@ : n $K4X$9$k5"G<K!
>e$NDjM}$O k = id $K$9$l$P<($,$k
in $*CBgc 16/24
26. (factorial)
Theorem
∀n ∈ N, f accps n id = f acdef unc n C0
Lemma
∀n ∈ N, k ∈ N → N, c ∈ cont,
(∀a ∈ N, k a = apply c a) → f accps n k = f acdef unc n c
>ZL@ : n $K4X$9$k5"G<K!
in $*CBgc 17/24
27. (factorial)
Theorem
∀n ∈ N, f accps n id = f acdef unc n C0
Lemma
∀n ∈ N, k ∈ N → N, c ∈ cont,
(∀a ∈ N, k a = apply c a) → f accps n k = f acdef unc n c
>ZL@ : n $K4X$9$k5"G<K!
>e$NDjM}$O k = id!$ c = C0 $K
$9$l$P<($;$k
in $*CBgc 17/24
28. CPS JQ49 & Hs4X?t2=$N@5Ev- (fibonacci)
let rec fib n =
if n <= 1 then 1
else fib ( n - 1 ) + fib ( n - 2 )
: $[“ factorial $HF1$83J9%
: well founded induction $G4hD%$k
in $*CBgc 18/24
29. CPS JQ49 & Hs4X?t2=$N@5Ev- (fibonacci)
let rec fib n =
if n <= 1 then 1
else fib ( n - 1 ) + fib ( n - 2 )
: $[“ factorial $HF1$83J9%
: well founded induction $G4hD%$k
?? apply 4X?t$HAj8_:F5”$N7A$K$Jj
Coq $G>e<j$/Dj5A=PMh$J$ ... (>ZL@$bH~$7$/$J )
in $*CBgc 18/24
30. fibonacci (CPS, OCaml)
let rec fib_cps n k =
if n < 2 then k 1
else
fib_cps ( n - 1 ) ( fun x - >
fib_cps ( n - 2 ) ( fun y - > k ( x + y ) ) )
in $*CBgc 19/24
31. fibonacci (defunctionalized, OCaml)
type cont =
| C0
| C1 of int * cont_t
| C2 of int * cont_t
let rec apply k x = match k with
| C0 - > x
| C1 (n , k ) - >
fib_defunc ( n - 2 ) ( C2 (x , k ) )
| C2 ( n1 , k ) - > apply k ( n1 + x )
and fib_defunc n k =
if n < 2 then apply k 1
else fib_defunc ( n - 1 ) ( C1 (n , k ) )
in $*CBgc 20/24
32. fibonacci (Coq)
Fixpoint app ( k : cont_fib ) ( x : nat ) ( count : nat )
{ struct count } : option nat : =
match count with
| O = > None
| S count ’ = >
match k with
| C0_fib = > Some x
| C1_fib n k = >
fib_defunc ( minus n 1 ) ( C2_fib x k ) count ’
| C2_fib n1 k = > app k ( plus n1 x ) count ’
end
end
with fib_defunc ( n : nat ) ( k : cont_fib ) ( count : nat )
{ struct count } : option nat : =
match count with
| O = > None
| S count ’ = >
match n with
| O | S O = > app k 1 count ’
| S m = > fib_defunc m ( C1_fib m k ) count ’
end
end .
in $*CBgc 21/24