1 Objects for Amadeus Gert Smolka Programming Systems Lab, UdS, Saarbrücken August 1999
2 Sources User's Manual, OCaml Release 2.02 (March 1999) Objective ML: An effective object-oriented extension to ML, D. Rémy and Jérôme Vouillon. Theory and Practice of Object Systems,(4)1:27-50,1998. Principles and a Preliminary Design for ML2000, ML2000 Working Group, March 1999 The Design of a Class Mechanism for MOBY, Kathleen Fischer and John Reppy. POPL 1999.
3 External View Use of Objects Internal View Creation of Objects Object Types
4 Types as Interfaces int -> int Function types do not fix implementation Abstract types do Object types strengthen types-as-interfaces property –record expressivity –subtyping (can forget fields) –partial type abstraction –higher-order polymorphism
5 Object Type for Stacks type intStack = unit; get : int; empty : bool >
6 Parameterisation type 'a stack = unit; get : 'a; empty : bool >
7 Recursion type 'a stack = unit; get : 'a; empty : bool; clone : 'a stack >
8 Polymorphic Methods type 'a stack = < put : 'a -> unit; get : 'a; empty : bool; map : ['b] ('a -> 'b) -> 'b stack >
9 Object Types have Structural Equality type 'a stack = < put : 'a -> unit; get : 'a; empty : bool; clone : 'a stack > type 'a queue = < put : 'a -> unit; get : 'a; empty : bool; clone : 'a queue > Definitions of object types are non-generative Type constructors stack and queue are equal Cannot separate stacks from queues
10 Partial Type Abstraction sig type 'a stack :> unit; get : 'a; empty : bool > val stack : unit -> 'a stack end Generative Can reveal abstract supertypes
11 Subtype Ordering Nonprimitive type constructors are invariant width depth
12 Type Inference Derives omitted type annotations and type checks Type weakenings must be annotated: exp Constraints for method selections and type weakenings are delayed until enough information is available Conservative extension of ML-style type inference Clever use of type abbreviations
13 Object-oriented Modelling versus ADTs concise: e#m e' rather than S.m e e' higher-order interfaces are types rather than signatures
14 External View Use of Objects Internal View Creation of Objects Object Types
15 Object Creation Objects have generative equality Method declarations define pre-methods fn self => exp that are applied to object upon method selection self provides recursion object(self) method m = exp... end
16 Class Declarations class claid = fn pat => object(self) inheritance declarations field declarations method declarations initializers end new claid
17 Class Declarations Needed for inheritance Methods can be overriden (with subtype) self only visible in method declarations and initializers Classes have status similar to structures Class specifications for signatures Protected methods are straightforward Private methods are semantically non-obvious Class identifier is automatically overloaded as type identifier for the respective object type
18 Field Declarations Analogous to value declarations of core language Do not see self Do not contribute to object type (i.e, fields are protected) Are inherited Are not overriden Private fields Mutable fields (important for cloning)
19 Super inherit classid exp as super super#m can be used in method declarations and initializers
20 Cloning class c = object val mutable x = 0 method inc = x<-1 method clone = { } end
21 Self Types in Class Interfaces class('a) val x : int; method movex : int -> 'a end class point1 = object val x = 0 method movex d = { } end class point2 = object inherit point1 val y = 0 method movey d = { } end class('a) val x : int; val y : int; method movex : int -> 'a; method movey : int -> 'a end
22 Differences to Objective Caml No partial type abstraction Open object types Fields as instance variables Functional objects Abstract classes and methods
23 Open Object Types Can express most general type of method selection #m -> 'a Can express most general type of cloning function ( as 'a) -> 'a Can express general type of min function ( as 'a) * 'a -> 'a Subtype ordering defined as one would expect
24 Binary Methods Expose Internals sig type t type point = unit; distance : point -> real; rep : t > val new : real * real -> point end Alternative: partial type abstraction