150 likes | 260 Vues
Explore the foundational concepts in object types within the Objective Caml programming system from the 1990s, drawing from research, design principles, and manual references. Learn about creating object types, methods, subtype ordering, type inference, and more advanced features to enhance programming efficiency and flexibility. Discover the advantages of object types over ADTs for increased polymorphism and flexibility in code structuring.
E N D
Objects in Objective Caml Gert Smolka Programming Systems Lab, UdS, Saarbrücken August 1999
Motivation • Starting or reference point for objects in Amadeus • Represents a decade of research in FP + OO • Mitchell Wand, LICS 1987 • Didier Rémy, POPL 1989 • Competing designs: MOBY and ML2000, based on implicit subtyping
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
Object Type for Stacks type 'a stack = < put : 'a -> unit; get : 'a; empty : bool; clone : 'a stack >
Object Types • List method names with their types • Methods are obtained from pre-methods fn self => expression upon method selection o#m as in sigma calculus of Abadi & Cardelli • Can be recursive • No polymorphic methods
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
Subtype Ordering width depth Nonprimitive type constructors are invariant
Explicit Subtyping with Coercions (exp :> typ) • No coercions to subtypes (would require types at run-time, non-obvious for functions) • Signature matching does not incorporate subtyping
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
Type Inference • Method selection: No problem due to open object types • Coercions: Heuristic, can be avoided with (exp : t :> t') • Attractive alternative for Amadeus: • Write exp for coercions • Infer lower and upper type and verify subsumption • Clever use of type abbreviations
Binary Methods Expose Internals sig type t type point = < move : real * real -> unit distance : point -> real rep : t > val new : real * real -> point end
Fixed by Partial Type Revelation sig type point :> < move : real * real -> unit distance : point -> real > val new : real * real -> point end
Lack of Polymorphic Methods type 'a stack = < put : 'a -> unit; get : 'a; empty : bool; map : ['b] ('a -> 'b) -> 'b stack >
Advantages of Object types over ADTs • Less to write: e#me'rather than S.m(e,e') • Object types can be declared independent of their implementations,implementations can be added as needed • More polymorphism: • Coercions (explicit subtype polymorphism) • Open object types (row polymorphism)