lecture17.pdf - Lecture 17: Types1 Administrivia •...

This preview shows page 1 - 7 out of 33 pages.

Lecture 17: Types1AdministriviaReminder: Test #1inclasson Wednesday,11March.1From material byG.Neculaand P.HilfingerLastmodified: Fri Mar 13 11:13:042015CS164: Lecture#17 1
Type Checking PhaseDeterminesthe type of each expressionin the program, (each nodeintheAST that correspondsto anexpression)Findstypeerrors.Examples?Thetype rulesof a language define each expression’s type and thetypesrequiredof allexpressionsand subexpressions.Lastmodified: Fri Mar 13 11:13:042015CS164: Lecture #17 2
Types and Type SystemsAtypeisasetofvaluestogetherwithasetofoperationsonthosevalues.E.g., fields and methods of a Java class are meant to correspond tovaluesandoperations.A language’stype systemspecifies which operations are valid forwhichtypes.Goaloftypecheckingistoensurethatoperationsareusedwiththecorrecttypes,enforcingintendedinterpretationof values.Notion of “correctness” often depends on what programmer has inmind, ratherthanwhatthe representationwould allow.Most operationsarelegalonly forvalues of sometypesDoesn’t make senseto adda functionpointerandan integerinCIt doesmake senseto add twointegersBut bothhavethe sameassemblylanguageimplementation:movl y, %eax; addl x, %eaxLastmodified: Fri Mar 13 11:13:042015CS164: Lecture #17 3
Uses of TypesDetecterrors:Memoryerrors,suchasattemptingtouseanintegerasapointer.Violationsofabstractionboundaries,suchasusingaprivatefieldfrom outsidea class.Helpcompilation:When Python seesx+y, its type systems tells it almost nothingabout typesofxandy, so codemust be general.In C, C++, Java, code sequences forx+yare smaller and faster,becauserepresentationsareknown.Lastmodified: Fri Mar 13 11:13:042015CS164: Lecture #17 4
Review: Dynamic vs. Static TypesAdynamictypeattachestoanobjectreferenceorothervalue. It’sa run-timenotion, applicableto any language.Thestatic typeof an expression or variable is a constraint on thepossibledynamictypesof its value, enforcedat compiletime.Language isstatically typedif it enforces a “significant” set ofstatictypeconstraints.Amatterofdegree: assemblylanguagemightenforceconstraintthat “all registers contain 32-bit words,” but since this allowsjust about anyoperation,not consideredstatictyping.C sortof hasstatictyping, but rathereasyto evadeinpractice.Java’s enforcementisprettystrict.In early type systems,dynamictype(E) =statictype(E)for all ex-pressionsE,sothatinallexecutions,Eevaluatestoexactlytypeofvalue deducedby thecompiler.Getsmore complexinadvanced typesystems.Lastmodified: Fri Mar 13 11:13:042015CS164: Lecture #17 5
SubtypingDefinearelationXprecedesequalYonclassesto say that:An object (value) of typeXcould be used when one of typeYisacceptableorequivalentlyXconformstoYIn JavathismeansthatXextendsY.

Upload your study docs or become a

Course Hero member to access this document

Upload your study docs or become a

Course Hero member to access this document

End of preview. Want to read all 33 pages?

Upload your study docs or become a

Course Hero member to access this document

Term
Spring
Professor
RajuPandey
Tags
Type system, type systems, Type Checking Phase

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture