Answers to Self-Study Questions

Test Yourself #1

C |- cdr : ∀α. [α] → [α]
C |- cdr : [α] → [α]C |- L : [α]
C |- null: ∀ α.[α] → boolC |- length : [α] → intC |- cdr(L) : [α]
C |- null : [α] → boolC |- L : [α]C |- succ : int → intC |- length(cdr(L)) : int
C |- null(L) : boolC |- 0 : intC |- succ(length(cdr(L))) : int
C |- if null(L) then 0 else succ(length(cdr(L))) : int
B |- λL. if null(L) then 0 else succ(length(cdr(L))) : [α] → int
B |- λL. if null(L) then 0 else succ(length(cdr(L))) : ∀ α. [α] → int
A |- fix length. λL. if null(L) then 0 else succ(length(cdr(L))) : ∀ α. [α] → int

Type Environment (we use the B and C below as shorthand above):

A =
0:int
null:∀α. [α] → bool
cdr:∀α. [α] → [α]
succ:int → int

B = A. length : ∀α. [α] → int

C = B. L : [α]


Test Yourself #2

Test Yourself #3

Starting type environment:
A = 3 : int

  W(A, (λx.x)(3))

Use rule b for Algorithm W:
= let (R, r)=W(A, λx.x)in
let (S, s)=W(RA, 3)in
let U=Unify(I, Sr, s → b1)in
(USR, U(b1))

Use rule d for Algorithm W:
= let (R, r)=(let (R, r) = W(A.x:b2, x)in (R, R(b2 → r))in
let (S, s)=W(RA, 3)in
let U=Unify(I, Sr, s → b1)in
(USR, U(b1))

Use rule a for Algorithm W:
= let (R, r)=(let (R, r) = (I, b2) in (R, R(b2 → r))in
let (S, s)=W(RA, 3)in
let U=Unify(I, Sr, s → b1)in
(USR, U(b1))

Do the "let" on the first line (Recall that I is the identity substitution, so we don't need to write it when it is applied to something else):
= let (R, r)=(I, b2 → b2)in
let (S, s)=W(RA, 3)in
let U=Unify(I, Sr, s → b1)in
(USR, U(b1))

Do the "let" on the first line (Recall that I is the identity substitution, so we don't need to write it when it is applied to something else):
= let (S, s)=W(A, 3)in
let U=Unify(I, S(b2 → b2), s → b1)in
(USR, U(b1))

Use rule a for Algorithm W:
= let (S, s)=(I, int)in
let U=Unify(I, S(b2 → b2), s → b1)in
(USI, U(b1))

Do the "let" on the first line (Recall that I is the identity substitution, so we don't need to write it when it is applied to something else):
= let U=Unify(I, b2 → b2, int → b1)in
(U, U(b1))

Expand Unify:
= let U=Unify( Unify(I, b2, int), b2, b1 )in
(U, U(b1))

Expand Unify:
= let U=Unify( b2:int, b2, b1 )in
(U, U(b1))

Expand Unify:
= let U=Unify( b2:int, int, b1 )in
(U, U(b1))

Expand Unify:
= let U=(b1 : int)(b2 : int)in
(U, U(b1))

Do the "let":
= ((b1:int)(b2:int), (b1:int)(b2:int)b1)

Apply the type substitution:
= ((b1:int)(b2:int), int)

And so, the inferred type of the expression (λx.x)(3) is int.