CS 641 Lecture -*- Outline -*- * Cardelli's Semantics of Multiple Inheritance Classic paper about semantics of OOP Basic ideas: objects are records with functions (operations) subtyping on records and functions models of subtypes as subsets Note the use of denotational semantics for stating things precisely. ** What is an object? a function over a finite set of message names a record (which is a special kind of finite function) finite map from labels to values note: no update operation on these records in Simula, x.m(args) only type checks if x has an operation field m most langauges don't have static type checking for message sends ------------------ type any = () type object = (age:int) type vehicle = (age:int, speed:int) type machine = (age:int, fuel:string) type car = (age:int, speed:int, fuel:string) ------------------- *** inheritance convenient notation for deriving implementations type vehicle = object and (speed:int) *** functional fields sending a message m to x with args written x.m(args) *** self reference --------------------- type point = (x:real, y:real) type active_point = point and (d: point -> real) val make-active_point(px:real, py:real): active_point = rec self: active_point . (x = px, y = py, d = \p:point . sqrt((p.x - self.x)**2 + (p.y - self.y)**2)) --------------------- ** Subtyping rules --------------------- basics: bool <= bool int <= int etc. records: t1 <= t1', ... tn <= tn' ___________________________________________________ (a:t1, ..., a:tn, ..., a:tm) <= (a:t1', ..., a:tn') variants: t1 <= t1', ... tn <= tn' ___________________________________________________ [a:t1, ..., a:tn] <= [a:t1', ..., a:tn', ..., a:tm'] functions: s <= s', t <= t' ______________________ (s -> t) <= (s' -> t') subsumption: H |- e:t, t <= t' _________________ H |- e:t' --------------------- Illustrate with examples from the types above. and variants: type precious_metal = [gold:unit, silver:unit] type metal = [gold:unit, silver:unit, steel:unit] draw subtyping graphs of the types. ** Soundness of subtyping rules Why these rules? *** Objectives of type system (page 60) prevent type errors: use of non-boolean in test of if application of non-function fetch of record field that doesn't exist (and fetch from non-record) etc. *** Wrong values all run-time type errors modeled by value "wrong" in the semantics. wrong in W, but W is not part of the denotations of normal types. see section 9, and the theorem there. *** Semantic Soundness theorem (page 63) denotation of a well-typed expression cannot be wrong.