240 likes | 388 Views
Objects for Amadeus. Gert Smolka Programming Systems Lab, UdS, Saarbrücken August 1999. 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.
E N D
Objects for Amadeus Gert Smolka Programming Systems Lab, UdS, Saarbrücken August 1999
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.
External View Use of Objects Object Types Internal View Creation of Objects
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
Object Type for Stacks type intStack = < put : int -> unit; get : int; empty : bool >
Parameterisation type'a stack = < put : 'a -> unit; get : 'a; empty : bool >
Recursion type 'a stack = < put : 'a -> unit; get : 'a; empty : bool; clone : 'a stack >
Polymorphic Methods type 'a stack = < put : 'a -> unit; get : 'a; empty : bool; map : ['b] ('a -> 'b) -> 'b stack >
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 > Object Types have Structural Equality • Definitions of object types are non-generative • Type constructors stack and queue are equal • Cannot separate stacks from queues
Partial Type Abstraction sig type'a stack :> < put : 'a -> unit; get : 'a; empty : bool > val stack : unit -> 'a stack end • Generative • Can reveal abstract supertypes
Subtype Ordering width depth Nonprimitive type constructors are invariant
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
Object-oriented Modelling versus ADTs • concise: e#me' rather than S.mee' • higher-order • interfaces are types rather than signatures
External View Use of Objects Object Types Internal View Creation of Objects
Object Creation object(self) methodm = exp ... end • Objects have generative equality • Method declarations define pre-methods fnself=>exp that are applied to object upon method selection • self provides recursion
Class Declarations class claid = fn pat => object(self) inheritance declarations field declarations method declarations initializers end new claid
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
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)
Super inheritclassidexpassuper • super#m can be used in method declarations and initializers
Cloning class c = object val mutable x = 0 method inc = x<-1 method clone = {< >} end
class point1 = object val x = 0 method movex d = {< x = x+d >} end class('a) val x : int; method movex : int -> 'a end class point2 = object inherit point1 val y = 0 method movey d = {< y = y+d >} end class('a) val x : int; val y : int; method movex : int -> 'a; method movey : int -> 'a end Self Types in Class Interfaces
Differences to Objective Caml • No partial type abstraction • Open object types • Fields as instance variables • Functional objects • Abstract classes and methods
Open Object Types <M1 ... Mn;..> • Can express most general type of method selection #m <m:'a;..> -> 'a • Can express most general type of cloning function (<..> as 'a) -> 'a • Can express general type of min function (<m:int;..> as 'a) * 'a -> 'a • Subtype ordering defined as one would expect
Binary Methods Expose Internals sig type t type point = < move : real * real -> unit; distance : point -> real; rep : t > val new : real * real -> point end • Alternative: partial type abstraction