



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




On the actual midterm, you will have plenty of space to put your answers. Some of these questions may be reused for the exam.
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 (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
( ( 0 ( 1 ( 1 ] 1 ] 0 b. 0 ( 1 0 ( 1 ] Solution:
0 ( 1 0 ( ] c. ( 0 ( 1 0 1] 0 ] Solution: No parse tree