13 Assignment 13: Types
Preliminaries
Type | = | num | ||
| | bool | |||
| | (Type * Type) | |||
| | (Type -> Type) | |||
| | (Type + Type) | |||
TEnv | = | () | ||
| | Symbol | |||
| | ((Symbol Type) . TEnv) | |||
Judgment | = | (TEnv /- Expr : Type) | ||
Proof | = | (Judgment because . ListOfProofs) | ||
ListOfProofs | = | () | ||
| | (Proof . ListOfProofs) |
((+ (num -> (num -> num))) (- (num -> (num -> num))) (zero? (num -> bool )))
Assignment
Types classify programs
Feel free to define functions that automate repetitive aspects of the following exercises.
- Define q1 to be a type derivation for (zero? ((- 3) 2)) in the initial type environment. In other words, complete the following proof:
(define q1 '((Gamma0 /- (zero? ((- 3) 2)) : ...) because ...)) To visualize and check your work, use the draw-proof and unproven-judgments functions in 04-16_proof.rkt in the lecture notes.> (unproven-judgments q1) '() Define q2 to be a type derivation for (λ x (zero? ((- 3) x))) in the initial type environment.
Define q3 to be a type derivation for (λ x (λ y (pair ((+ x) y) ((- x) y)))) in the initial type environment.
Define q4 to be a type derivation for (λ p ((+ (fst p)) (snd p))) in the initial type environment.
Define q5 to be a type derivation for (λ p (pair (snd p) (fst p))) in the initial type environment.
Define q6 to be a type derivation for (λ p (if (fst p) (fst p) (snd p))) in the initial type environment.
Define q7 to be a type derivation for (λ b (λ p (if b (pair #t (snd p)) (pair (fst p) 3)))) in the initial type environment.
Define q8 to be a type derivation for (λ f (λ x (f (f x)))) in the initial type environment.
Define q9 to be a type derivation for (λ f (λ g (λ x (f (g x))))) in the initial type environment.
Define q10 to be a type derivation for (λ x (case x y (right y) z (left z))) in the initial type environment.
Types reject programs
In comments, explain briefly in English to someone who knows how our programs run but is unfamiliar with our type system:
- Why doesn’t the program evaluated below have any type? It seems to work fine.
> (value-of '(((λ b (if b zero? (+ 1))) #t) 3) init-env) - Why doesn’t the program evaluated below have any type? It seems to work fine.
> (value-of '(((λ b (λ x (if b (pair 3 x) x))) #t) 4) init-env) - Why doesn’t the program evaluated below have any type? It seems to work fine.
> (value-of '((λ x (x x)) (λ x (x x))) init-env)
Extending the type system
Expr | = | (let Symbol Expr Expr) |
- Define and test a function inference-ok-let? that takes a Proof and determines whether its root is a valid inference for the type of a let expression. The two premises for the inference should be type judgments for the two subexpressions. Check just the root inference, not how the two premises are themselves proven.
> (inference-ok-let? '((Gamma0 /- (let + ((+ 3) 2) (zero? +)) : bool) because ((Gamma0 /- ((+ 3) 2) : num) because I said so) ((((+ num) . Gamma0) /- (zero? +) : bool) because it is trivial))) #t
Brainteasers: Programs inhabit types
> (value-of '((rec f x (f x)) 3) init-env)
Define bb to be a list of as many programs as you can think of, each of which has the type (bool -> bool) in the initial type environment. The programs must each behave differently.
Define bbnn to be a list of as many programs as you can think of, each of which has both the type (bool -> bool) and the type (num -> num) in the initial type environment. The programs must each behave differently.
Define bbbnnn to be a list of as many programs as you can think of, each of which has both the type ((bool * bool) -> bool) and the type ((num * num) -> num) in the initial type environment. The programs must each behave differently.
Just Dessert
Expr | = | (null) | ||
| | (cons Expr Expr) | |||
| | (lcase Expr Expr Symbol Symbol Expr) | |||
Type | = | (Type list) |
(rec map f (λ lst (lcase lst (null) x xs (cons (f x) ((map f) xs)))))
Define and test a function inference-ok-lists? that takes a Proof and determines whether its root is a valid inference for the type of a null, cons, or lcase expression. Check just the root inference, not how the premises are themselves proven.