Sample Midterm2 Sol-fa07

# Sample Midterm2 Sol-fa07 - Solutions for Sample Questions...

This preview shows pages 1–3. Sign up to view the full content.

Solutions for Sample Questions for Midterm 2 (CS 421 Fall 2007) On the actual midterm, you will have plenty of space to put your answers. Some of these questions may be reused for the exam. 1. Using the rules provided in class, derive a valid type judgment for let rec fact = fun n -> if n = 0 then 1 else let r = fact (n - 1) in n * r in fact;; (The rules will be provided for you on the exam, if this kind of question is asked.) Solution: (I didn’t just write the tree because it wouldn’t fit.) By the let_rec rule we have (1) {fact : int -> int} |- (fun n -> if n = 0 then 1 else let r = fact (n – 1) in n * r) : int -> int (2) {fact : int -> int} |- fact : int -> int {} |- let rec fact = fun n -> if n = 0 then 1 else let r = fact (n – 1) in n * r in fact : int -> int (2) is valid by the variable rule: (2) {fact : int -> int} |- fact : int -> int By the fun rule we have (3) {n : int, fact : int -> int} |- (if n = 0 then 1 else let r = fact (n – 1) in n * r) : int (1) {fact : int -> int} |- (fun n -> if n = 0 then 1 else let r = fact (n – 1) in n * r) : int -> int By the if_then_else rule we have (4) {n : int, fact : int -> int} |- (n = 0) : bool (5) {n : int, fact : int -> int} |- 1:int (6) {n : int, fact : int -> int} |- (let r = fact (n – 1) in n * r) : int (3) {n : int, fact : int -> int} |- (if n = 0 then 1 else let r = fact (n – 1) in n * r) : int (5) is valid by the rule for constants. By the rule for binary relations we have (7) {n : int, fact : int -> int} |- n : int (8) ) {n : int, fact : int -> int} |- 0 : int (4) {n : int, fact : int -> int} |- (n = 0) : bool (7) is valid by the rule for variables. (8) is valid by the rule for constants. By the rule for let, we have (9) {n:int, fact:int -> int} |- fact (n – 1) : int (10) {r:int, n:int, fact:int -> int} |- (n * r) : int (6) {n : int, fact : int -> int} |- (let r = fact (n – 1) in n * r) : int By the rule for applications we have (11) {n:int, fact:int -> int} |- fact : int -> int (12){n:int, fact:int -> int} |- (n – 1) : int (9) {n:int, fact:int -> int} |- fact (n – 1) : int (11) is valid by the variable rule. By the rule for binary operations, we have

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
(13){n:int, fact:int -> int} |- n : int (14){n:int, fact:int -> int} |- 1 : int (12){n:int, fact:int -> int} |- (n – 1) : int (13) is valid by the variable rule. (14) is valid by the constant rule. Thus (12) is valid. Thus (9) is valid. We have (10) left. By the rule for binary operations we have: (15) {r:int, n:int, fact:int -> int} |- n: int (16) {r:int, n:int, fact:int -> int} |- r : int (10) {r:int, n:int, fact:int -> int} |- (n * r) : int (15) and (16) are both valid by the variable rule. Hence (10) is valid. Hence (6) is valid. Hence (3) is valid, and hence (1) is valid
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 8

Sample Midterm2 Sol-fa07 - Solutions for Sample Questions...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online