Homework 5: Simply-typed λ
- Due: 03/22 at 11:55pm
- Points: 15 (each question 5 points)
Please prepare your solutions as a PDF file (scanned handwriting is okay, but no .doc or .docx, please). Submit your PDF file in Sakai.
1. Practice with simply-typed λ
Consider the following function of type Nat→Nat
in PCF:
double ≡ fix (λf:___. λx:Nat. if iszero x then 0 else succ (succ (f (pred x))))
What should the type of
f
be (fill in the___
)?Write the typing derivation for
double : Nat→Nat
. To avoid repeating yourself, feel free to define new names liket1 ≡ pred x
orΓ1 ≡ x:Nat
.
2. letpair
In Python, we can write things like (a, b) = p
, which is equivalent to a = p[0]; b = p[1]
if p
is a pair. Let’s add a similar construct to our extended λ-calculus:
t ::= ...
letpair {x1, x2} = t3 in t4
We could define this as syntactic sugar in terms of .1
and .2
:
(letpair {x1, x2} = t3 in t4) ≡ (let x1 = t3.1 in let x2 = t3.2 in t4)
For example,
letpair {x, y} = {0, true} in if y then succ x else x ⟶ succ 0
But suppose that we want to get rid of .1
and .2
and make letpair
the only way to un-pair pairs.
Write
.1
and.2
as syntactic sugar in terms ofletpair
(fill in the___
):t.1 ≡ ___ t.2 ≡ ___
Write evaluation and typing rules for
letpair
.
3. Redefining NB
For this question, we’ll use the simply-typed λ-calculus with sum types (<·>
, inl
, inr
, case
), the unit type (Unit
, unit
), and recursive types (μ
). Don’t use fix
or built-in Bool
or Nat
.
We don’t need Booleans or numbers to be built in; we can define them ourselves.
Write a definition for
Bool
, and then use it to definetru : Bool fls : Bool test : Bool → Nat → Nat → Nat
Recall that
test
is likeif
but always evaluates all its arguments; here, the “then” and “else” parts are (arbitrarily) restricted to be of typeNat
. Your definitions should satisfy:test tru x y = x test fls x y = y
Write a definition for
Nat
, and then use it to definezro : Nat scc : Nat → Nat iszro : Nat → Bool prd : Nat → Nat
Please use isorecursive types, so do useSince I neglected to talk aboutfold
andunfold
where needed.fold
andunfold
, you don’t need to use them if you don’t want to. Your definitions should satisfy:iszro zro = tru iszro (scc x) = fls prd zro = zro prd (scc x) = x