Document 10805475

advertisement
Short Quiz 9 1 ) What is the difference between Type Checking and Type Inference ? Ans: Type checking: given an expression e, and a type environment E for the free variables in e, check if e is well formed and return its type. Type inference: given an expression e, compute the types of all variables in e. Use those types to determine the type of e and its sub-­‐expression. 2) Please evaluate expression (129 + 3) * (18/2) using Operational Semantics. (129+3): 132 132*9 : 1188 (18/2) : 9 3) What is the final rule of following assumptions ? ! x1 : lambda z. x1'' + x1''
t ! x1'[x2 / z] : t Ans: take a look at parse tree first + We need one more rule in Environment to finish it: x ''1 : x '1 So the final rules include above rules and one more below the horizontal bar : x1
: t 4) Assume you have following rules, please evaluate (lambda z. “abc” “def”+6 ): ! x1 : lambda z. x1'' + x1'' ! x1'[x2' / z] : t ! x2 : x2' !
(x1 x2 ) : t
Ans : as described in question 3, we have (lambda z. “abc” “def”+6 ) + (lambda z. “abc” “def”+6 ) 5) Please write down Operational Semantics rules for one expression, say (z) ? Ans: z: z’ (z’) : t z: t 6) Assume We want to prove Soundness of type system for integer: int eger i
E ! i :i
int eger i
! ! i : Int
Assume S1 , S2 are integers, please prove S1 ! S2 Proof: Recall the definition of Soundness : Soundness of type system: If type system says an expression e has type τ, then e evaluates to a value which is actually of type τ. E ! S1 : i1
! ! S1 : Int
E ! S2 : i2 ! ! S2 : Int E
! S1 ! S2 : i1 ! i2 ! ! S1 " S2 : Int 7) What is the difference between Big-­‐Step and Small-­‐Step Semantics ? Ans: There are two points: (a) In big-­‐step semantics, any rule may invoke any number of other rules in the hypothesis. This means any derivation is a tree. In small-­‐step semantics, each rule only performs one step of computation. This means any derivation is a line (b) The other big difference is that we can quantify the cost of a computation with the number of steps in a small-­‐step derivation 8)In Haskell, what is result of expression [1,2,3] ++ [4] ? Ans: [1,2,3,4] 9) In Haskell, is ”2+2” equal to ”4”? Ans: no. It is still evaluated as “2+2”, as a string 10) If myfile contains ”Hello!”, is readFile ”myfile” equal to ”Hello!”? Ans: It depends on what is in “myfile”. If there are more contents than “Hello!”, the return value of readFile”myfile” will be more than “Hello!”. 
Download