Objects for Amadeus

Download Report

Transcript Objects for Amadeus

Objects for Amadeus
Gert Smolka
Programming Systems Lab, UdS, Saarbrücken
August 1999
1
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.
2
External View
Use of Objects
Object Types
Internal View
Creation of Objects
3
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
4
Object Type for Stacks
type intStack =
< put
: int -> unit;
get
: int;
empty : bool >
5
Parameterisation
type 'a stack =
< put
: 'a -> unit;
get
: 'a;
empty : bool >
6
Recursion
type 'a stack =
< put
: 'a -> unit;
get
: 'a;
empty : bool;
clone : 'a stack >
7
Polymorphic Methods
type 'a
put
get
empty
map
>
stack = <
: 'a -> unit;
: 'a;
: bool;
: ['b] ('a -> 'b) -> 'b stack
8
Object Types have Structural Equality
type 'a
put
get
empty
clone
>
stack = <
: 'a -> unit;
: 'a;
: bool;
: 'a stack
type 'a
put
get
empty
clone
>
queue = <
: 'a -> unit;
: 'a;
: bool;
: 'a queue
• Definitions of object types are non-generative
• Type constructors stack and queue are equal
• Cannot separate stacks from queues
9
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
10
Subtype Ordering
M1 ... Mn Mn1  M1 ... Mn 
t  t'
m : t M1 ... Mn   m : t' M1 ... Mn 
t'1  t1
t 2  t'2
t1  t 2  t'1  t'2
width
depth
t1  t'1
t 2  t'2
t1  t 2  t'1  t'2
Nonprimitive type constructors are invariant
11
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
12
Object-oriented Modelling versus ADTs
• concise:
e#m e' rather than S.m e e'
• higher-order
• interfaces are types rather than signatures
13
External View
Use of Objects
Object Types
Internal View
Creation of Objects
14
Object Creation
object(self)
method m = exp
...
end
• Objects have generative equality
• Method declarations define pre-methods
fn self => exp
that are applied to object upon method selection
• self provides recursion
15
Class Declarations
class claid =
fn pat =>
object(self)
inheritance declarations
field declarations
method declarations
initializers
end
new claid
16
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
17
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)
18
Super
inherit classid exp as super
• super#m can be used in method declarations and
initializers
19
Cloning
class c = object
val mutable x = 0
method inc = x<-1
method clone = {< >}
end
20
Self Types in Class Interfaces
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
val y
method movex
method movey
end
:
:
:
:
int;
int;
int -> 'a;
int -> 'a
21
Differences to Objective Caml
•
•
•
•
•
No partial type abstraction
Open object types
Fields as instance variables
Functional objects
Abstract classes and methods
22
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
23
Binary Methods Expose Internals
sig
type t
type point =
< move
:
distance :
rep
:
val new : real
end
real * real -> unit;
point -> real;
t >
* real -> point
• Alternative: partial type abstraction
24