WA3-solutions

WA3-solutions - CS 143 Compilers Handout 9 Solutions to...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Handout 9 Solutions to Written Assignment 3 1. (10 pts) Consider the following class definitions. class A { i: Int o: Object b: B <- new B x: SELF_TYPE f(): SELF_TYPE {x} } class B inherits A { g(b: Bool): Object { (* EXPRESSION *) } } Assume that the type checker implements the rules described in the lectures and in the Cool Reference Manual. For each of the following expressions, occurring in place of (* EXPRESSION *) in the body of the method g , show the static type inferred by the type checker for the expression. If the expression causes a type error, give a brief explanation of why the appropriate type checking rule for the expression cannot be applied. (a) (2 pt) x Solution: SELF TYPE B (b) (2 pt) self = x Solution: bool (c) (2 pt) self = i Solution: Error; Int objects can only be compared with other Int objects (d) (2 pt) let x: B <- x in x Solution: B (e) (2 pt) case o of o: Int => b; o: Bool => o; o: Object => true; esac Solution: bool 2. (10 pts) A type derivation shows the inductive proof of a typing judgement as a tree. For example, the type derivation for O [Int /x ] x + 1 : Int is given as follows: O[Int/x](x) = Int O [Int /x ] x : Int O [Int
Background image of page 1

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

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 3

WA3-solutions - CS 143 Compilers Handout 9 Solutions to...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online