WA3-solutions

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

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

Handout 9 Solutions to Written Assignment 3 1. (10 pts) Consider the following class deﬁnitions. 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

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 01/12/2010 for the course CS 143 at Stanford.

### 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
Ask a homework question - tutors are online