------------------------------------------------------------------------------- Solution for hw8. Exercises 1, 5, 6, 9 P68-69 in OOPS Note: "<" is used for subset relation. (That is, A < B means A is a subset of B.) ------------------------------------------------------------------------------- 1. Describe the semantics of the following novel BOPL expressions. Given for each expression the appropriate type constraings and extend the type checking algorithm TC to handle them. a) repeat EXP1 until EXP2 end semantics: setp 1. evaluate EXP1 setp 2. evaluate EXP2 setp 3. if EXP2 is TURE, break. Otherwise, go to setp 1. type ConstraInts: [EXP1] = {Bool} [repeat EXP1 until EXP2 end] = {} TC(G, L, repeat EXP1 until EXP2 end) = let te = TC(G,L,EXP2) in if te = {Bool} then {} else type-error end b) for ID := EXP1 to EXP2 do EXP3 end semantics:(most common one) step 1. evaluate EXP1 and initialize ID step 2. if ID is not greater than EXP2, evaluate EXP3. Ohterwise, break. step 3. increase ID by 1 and go to step 2. type constraInts: [EXP1] = [EXP2] = {Int} [ID] > {Int} [for ID := EXP1 to EXP2 do EXP3 end] = {} TC(G, L, for ID:=EXP1 to EXP2 do EXP3 end) = let te1 = TC(G, L, EXP1) in let te2 = TC(G, L, EXP2) in if te1 = {Int} & te2 = {Int} & L(ID) > {Int} then {} else type-error end 5. minimal solution [j] = {Int} [i] = {Int} [n] = {Int} [i-j] = {Int} [0] = {Int} [n=0] = {Bool} [if n=0 then 1 else j:=1; n*self.fac(self.dec(n)) end] = {Int} [1] = {Int} [j:=1] = {Int} [j:=1; n*self.fac(self.dec(n))] = {Int} [n*self.fac(self.dec(n))] = {Int} [self.fac(self.dec(n))] = {Int} [self-X] = {X} [self-X.dec(n)] = {Int} [X new] = {X} [7] = {Int} [(X new).fac(7)] = {Int} 6. Show that there's no solution. contradiction: {Bool} > {Int, Bool} Insert dynamic check class Y var a:{Int, Bool} method m(b:Int) returns Bool if b = 0 then (a instance-of Bool) else self.m(b-1) end end end (Y new).m(87) 9. Derive type constraints 18) "EXP class new" [EXP] < {all classes of type ^C and @C} [EXP class new] = [EXP] 19) "EXP instance-of @ID" [EXP instance-of @ID] = @ID 22) if [EXP] is @D, then the condition in 22) can be verified by checking C = D. Subtyping rules < Void Int Bool ^C @C ------------------------------------------------ Void true true true true true Int false true false false false Bool false false true false false ^D false false false D