Document 10835009

advertisement
CSE 305 Introduc0on to Programming Languages Lecture 14 – Type System 2 CSE @ SUNY-­‐Buffalo Zhi Yang Courtesy of John Mitchell No0ce Board •  First, homework6 has been posted and is due on July 25(Thursday), and please schedule your 0me accordingly. •  Second, on July 18, 2013(Thursday), you will be having the third long quiz. Declara0ons •  This lecture has majorly referred to on line material “Type Systems for Programming Languages”, by Benjamin C. Pierce. If you are interested in the official publica0on, please contact the author and Publisher MIT Press. This lecture is mainly focusing on teaching purpose and has nothing to do with any other commercial and academic ac0vi0es. Our objec0ve •  The first objec0ve of our class, is to comprehend a new programming language within very short 5me period, and because you have this ability to shorten your learning curve, you are going to manipulate the language with an insight learning. •  The second objec0ve is to even engineer your own language! Review what we ve learnt and see future eg: Egyp0an Number System; Complement Number eg: Abacus Number System eg: Gate system, Including different underline device 1st Genera0on language: Machine Code eg: MIPS 2nd Genera0on language: Assembly Code eg: Fortran Regular Expression What s next ? 3rd Genera0on Language: Macro func0on Macro func5on Basic Calcula0on System Lexer Compiler System Virtual Machine Parser Push Down Automata Type Checking Context-­‐Free Grammar Lambda Calculus Theory A family tree of languages
Cobol <Fortran> BASIC Algol 60 <LISP> PL/1 <Simula> <ML> Algol 68 C Pascal <Perl> <C++> Modula 3 Dylan Ada <Java> <C#> <Scheme> <Smalltalk> <Ruby> <Python> <Haskell> <Prolog> <JavaScript> References and Excep0ons (1) References
We will be introducing some concepts of Excep0ons in later This chapter
very
The basic
approach
seemsbfine,
there’s
a goodsystem deal of
lecture, so ishstill
ere wrough.
e simply talk a lijle it abut
bout type work to do on details.
for reference . References
Syntax
::=
(terms...)
variable
abstraction
application
constant
store location
reference creation
dereference
assignment
::=
(values...)
abstraction value
store location
::=
(types...)
type of functions
unit type
type of reference cells
::=
(contexts...)
empty context
term variable binding
76
References and Excep0ons (2) January 15, 2000
9. R EFERENCES
77
::=
empty store
location binding
::=
empty store typing
location typing
)
Evaluation (
(E-B ETA)
(E-A PP 1)
(E-A PP 2)
dom
(E-R EF)
(E-R EF 1)
(E-D EREF)
(E-D EREF 1)
(E-A SSIGN)
(E-A SSIGN 1)
(E-A SSIGN 2)
Typing (
)
(E-A SSIGN 1)
References and Excep0ons (3) (E-A SSIGN 2)
Typing (
)
(T-VAR)
(T-A BS)
January 15, 2000
9. R EFERENCES
78
(T-A PP)
(T-U NIT)
(T-L OC)
(T-R EF)
(T-D EREF)
(T-A SSIGN)
9.1 Definition: We say that a store
and a store typing , written
for every
dom .
is well-typed with respect to a typing context
, if dom
dom
and
(T-A SSIGN)
References and Excep0ons (4) 9.1 Definition: We say that a store
and a store typing , written
for every
dom .
is well-typed with respect to a typing context
, if dom
dom
and
!"#$%&'()*+($$
•  ,-($.#/0$%&'()*+($%1$%"#$)23//4$5/$0%$
!"#$%&'()*+($$
Proof: Just like the proof of Lemma 7.4.9.
•  !"#$%&'&()*+*(&,*$%"-%+##.(-*/+(-0+-&*
,-($.#/0$%&'()*+($%1$%"#$)23//4$5/$0%$
9.3 Lemma: If
and
, then
.
Proof: Easy.
,.1'.(*2&%3*4'"%1*5#&*$&%.")4$$367$&()3"/($
!"#$%&'()*+($
$
!"#$%&'&()*+*(&,*$%"-%+##.(-*/+(-0+-&*
9.4 Theorem [Preservation]: If
8%"$-3+($0-5/$3&52508$0%$/-%#0(6$8%"#$2(3#6569$
,.1'.(*2&%3*4'"%1*5#&*$&%.")4$$367$&()3"/($
!"#$%&'()*+($$
•  ,-($.#/0$%&'()*+($%1$%"#$)23//4$5/$0%$
8%"$-3+($0-5/$3&52508$0%$/-%#0(6$8%"#$2(3#6569$
)"#+(4$8%"$3#($9%569$0%$:365;"230($0-($
•  !"#$%&'&()*+*(&,*$%"-%+##.(-*/+(-0+-&*
,-($.#/0$%&'()*+($%1$%"#$)23//4$5/$0%$
then, for some
,
)"#+(4$8%"$3#($9%569$0%$:365;"230($0-($
2369"39($<50-$36$56/59-0$2(3#6569=$$$$
,.1'.(*2&%3*4'"%1*5#&*$&%.")4$$367$&()3"/($
!"#$%&'&()*+*(&,*$%"-%+##.(-*/+(-0+-&*
2369"39($<50-$36$56/59-0$2(3#6569=$$$$
.
8%"$-3+($0-5/$3&52508$0%$/-%#0(6$8%"#$2(3#6569$
Proof: Clearly, the preservation property for multi-step evaluation will follow (by
,.1'.(*2&%3*4'"%1*5#&*$&%.")4$$367$&()3"/($
a straightforward induction) from preservation for single-step evaluation. This,
•  )"#+(4$8%"$3#($9%569$0%$:365;"230($0-($
,-($/()%67$%&'()*+($5/$0%$(+(6$&(-.(&&%*3"0%*
. All
in turn, is established by an induction on a derivation of
•  8%"$-3+($0-5/$3&52508$0%$/-%#0(6$8%"#$2(3#6569$
,-($/()%67$%&'()*+($5/$0%$(+(6$&(-.(&&%*3"0%*
the cases of this induction are straightforward, using the lemmas above and the
",(*/+(-0+-&>$$
evident inversion property of the typing rules.
2369"39($<50-$36$56/59-0$2(3#6569=$$$$
)"#+(4$8%"$3#($9%569$0%$:365;"230($0-($
",(*/+(-0+-&>$$
9.5 Exercise: Is the evaluation relation defined in this chapter strongly normalizing on well-typed terms? (Solution on page 260.)
2369"39($<50-$36$56/59-0$2(3#6569=$$$$
9.2 Lemma [Substitution]: If
.
and
, then
To be written.
The point of introducing equivalence as a separate notion is that it gives a uniform
framework for all kinds of definitional equivalences on types. It introduces a bit of overhead
too, but I think it’s worth it in this case.
This chapter has just been added, and some of the later chapters have not been brought
up to date (the equivalence rules used to be folded into the subtyping relation).
Type Equivalence (1) Records with permutation of fields
Type equivalence (
)
(Q-R EFL)
(Q-S YMM)
(Q-T RANS)
(Q-A RROW )
is a permutation of
New typing rules (
(Q-R CD -P ERM )
)
(T-E Q)
81
Type Equivalence (2) January 15, 2000
11. T YPE E QUIVALENCE
82
Algorithmic rules for simply typed lambda-calculus
Algorithmic typing (
)
(TA-VAR)
(TA-A BS)
(TA-A PP)
Algorithmic rules for records with permutation
Algorithmic type equivalence (
)
(QA-A RROW )
is a permutation of
for each
Algorithmic typing (
(QA-R CD )
)
(TA-VAR)
(TA-A BS)
(TA-A PP)
for each
(TA-R CD)
(TA-P ROJ)
extend type equivalence relation
talk about exposing types before matching against them
Exercise: inner defns (not trivial: must eliminate remaining occurrences when
leaving the scope of the definition)
Type Defini0on (1) Type definitions
New syntactic forms
::= ...
(types...)
type variable
::= ...
(contexts...)
type definition binding
)
New type equivalence rules (
(Q-R EFL)
(Q-S YMM)
January 15, 2000
12. D EFINITIONS
(Q-T RANS)84
(Q-A RROW )
83
(Q-D EF)
New typing rules (
)
(T-E Q)
Algorithmic rules for type definitions
Algorithmic reduction (
)
(RA-D EF)
Type Defini0on (2) (T-E Q)
Algorithmic rules for type definitions
Algorithmic reduction (
)
(RA-D EF)
)
Algorithmic type equivalence (
(QA-B ASE)
(QA-R EDUCE L)
(QA-R EDUCE R)
(QA-A RROW )
)
Exposure (
otherwise
(XA-O THER)
(XA-R EDUCE)
Algorithmic typing (
)
(TA-VAR)
January 15, 2000
12. D EFINITIONS
85
(TA-A BS)
(TA-A PP)
12.2
Term Definitions
Subtyping This material is not too far from finished, but the proofs and presentation need to be reworked a little in light of the addition of Chapter 11.
In programming in the simply typed lambda-calculus, we may sometimes find
ourselves irritated by the type system’s insistence that types match exactly, for
example in the rule T-A PP for typing applications:
(T-A PP)
According to this rule, the term
is not typeable, even though it will obviously evaluate without producing any
run-time errors, since the function only requires that its argument has a field ; it
doesn’t care what other fields the argument may or may not have. Moreover, this
fact is evident from the type of the function—we don’t need to look at its body to
see that it doesn’t use any fields besides .
One way to formalize this observation is to extend the typing rules so that
is given a set of types including both
and
the term
. Then the application above is well typed because one of the possible
types of the argument matches the left-hand side of the type of the function.
To accomplish this in a general way, we introduce a principle of safe substitu, to mean that any term of
tion: We say that “ is a subtype of ,” written
type can safely be used in a context where a term of type is required. If this is
the case, then a term of type is also given the type , using the so-called rule of
subsumption:
(T-S UB)
The Subtype Rela0on 13.1 The Subtype Relation
Clearly, it is not safe to substitute functions for records, records for functions, etc.
The subtyping relation will only be defined for types with similar structure.
First, just to make sure that our subtyping relation is sensible, we make two
general stipulations: first, that it should be reflexive,
and second, that it should be transitive:
(S-T RANS)
These rules follow directly from the intuition of safe substitutivity. The remainder
of the rules defining the subtype relation concern the behavior of specific type
constructors.
For record types, we have already seen that we want to consider the type
to be a subtype of
if is obtained by
dropping fields from .
(S-R CD -W IDTH )
In fact, we can be a little more general than this and also allow the types of the
common fields to differ, so long as their types in are subtypes of their types in .
for each
(S-R CD -D EPTH )
Also, it makes sense to ignore the order of fields in a record, since the only thing
that we can do with records—namely, field projection—is insensitive to the order
of fields.
For functions, the rule is a little trickier:
(S-A RROW)
That is, we allow a function of one type to be used where another is expected as
long as none of the arguments that may be passed to the function by the context
) and none of the results that it returns will surprise the
will surprise it (
). Notice that the sense of the subtype relation is reversed on
context (
the left of the arrow; this rule is sometimes referred to as the “contravariant arrow
rule” for this reason.
Variance for subtyping between reference cell types?
Variance
We pause for a moment to introduce some useful terminology for discussing types
and subtyping.
is a type term containing some number of holes, written “ ” someSuppose
for the term that results from filling the holes in
place inside it; we write
with . For example, if
, then
.
is covariant in the position(s) marked by the holes if, whenever
We say that
, we have
. We say that
is contravariant if, whenever
, we have
. If
is neither covariant nor contravariant,
it is said to be invariant. For example, we say that the arrow type constructor
is
is contravariant in its first argument and covariant in its second, since
is covariant.
contravariant while
13.1.3 Exercise: Which of the following types are covariant in the position marked
by the hole(s)? Which are contravariant? Which are invariant?
1.
2.
3.
4.
5.
Summary
Subtyping extension (1) As usual, we present the subtyping extension of the pure simply typed lambda
calculus first, then the additional rules for records.
: Simply typed lambda-calculus with subtyping
Syntax
::=
(terms...)
variable
abstraction
application
::=
(values...)
abstraction value
::=
(types...)
type of functions
maximum type
::=
(contexts...)
empty context
term variable binding
)
Evaluation (
(E-B ETA)
(E-A PP 1)
(E-A PP 2)
Subtyping (
)
(S-R EFL)
(S-T RANS)
Summary
Subtyping extension (2) As usual, we present the subtyping extension of the pure simply typed lambda
calculus first, then the additional rules for records.
: Simply typed lambda-calculus with subtyping
Syntax
::=
(terms...)
variable
abstraction
application
::=
(values...)
abstraction value
::=
(types...)
type of functions
maximum type
::=
(contexts...)
empty context
term variable binding
)
Evaluation (
(E-B ETA)
(E-A PP 1)
(E-A PP 2)
Subtyping (
)
(S-R EFL)
(S-T RANS)
Subtyping extension (3) 13. S
January 15, 2000
UBTYPING
90
(S-T OP)
(S-A RROW)
Typing (
)
(T-VAR)
(T-A BS)
(T-A PP)
(T-S UB)
Subtyping rules for records
New type equivalence rules (
)
(Q-R EFL)
(Q-S YMM)
(Q-T RANS)
Subtyping extension (4) (T-S UB)
Subtyping rules for records
New type equivalence rules (
)
(Q-R EFL)
(Q-S YMM)
(Q-T RANS)
(Q-A RROW )
is a permutation of
New subtyping rules (
(Q-R CD -P ERM )
)
(S-E QV)
(S-R CD -W IDTH )
for each
(S-R CD -D EPTH )
In this section we consider some of the properties of the system
simply typed lambda-calculus
with
and subtyping.
Metatheory of records
Subtyping (1) , the
Algorithmic Subtyping
The three rules for record subtyping are often combined into one more compact
but less readable rule:
implies
(S-R CD)
13.2.1 Lemma: This rule can be derived from S-R CD -D EPTH , S-R ECORD -W IDTH ,
and S-R ECORD -P ERM (using reflexivity and transitivity) and vice versa.
%$
Proof: Exercise.
/+(-0+-&*
7$&()3"/($
We now observe that the rules of reflexivity and transitivity are inessential, in
$
"#$2(3#6569$
the following sense:
+(-0+-&*
$0-($
• 
,-($.#/0$%&'()*+($%1$%"#$)23//4$
• 
,-($.#/0$%&'()*+($%1$%"#$)23/
13.2.2 Lemma:
$$$&()3"/($
!"#$%&'&()*+*(&,*$%"-%+##
!"#$%&'&()*+*(&,*$%"-%+#
#$2(3#6569$
can be derived for every type without using S-R EFL.
1.
0-($
,.1'.(*2&%3*4'"%1*5#&*$&%.")4$
,.1'.(*2&%3*4'"%1*5#&*$&%."
can
be
derived
at
all,
then
it
can be derived without using S-T RANS.
2.
If
-.(&&%*3"0%*
• 
,-($.#/0$%&'()*+($%1$%"#$)23//4$5/$0%$
$
!"#$%&'()*+(
!"#$%&'()*
!"#$%&'()*+($$
8%"$-3+($0-5/$3&52508$0%$/-%#0(6
8%"$-3+($0-5/$3&52508$0%$/-%#0
!"#$%&'&()*+*(&,*$%"-%+##.(-*/+(-0+-&
Proof: Exercise.
January 15, 2000
13. S UBTYPING
Metatheory of Subtyping (2) 92
Algorithmic subtyping
Algorithmic subtyping (
)
(SA-T OP)
(SA-A RROW)
if
, then
(SA-R CD)
13.2.5 Proposition [Soundness and completeness of algorithmic subtyping]:
%$
iff
.
/+(-0+-&* Proof: Using the two previous lemmas.
7$&()3"/($ This, in turn, leads directly to an algorithm for checking the algorithmic subsubtype relation, by Proposition 13.2.5).
"#$2(3#6569$ type relation (and hence also the declarative
• 
,-($.#/0$%&'()*+($%1$%"#$)23//4$
=
if
,
subtype
$0-($
then true
and
else if
!"#$%&'&()*+*(&,*$%"-%+##
$$$
then subtype
subtype
else if
and
+(-0+-&*
then, for all,.1'.(*2&%3*4'"%1*5#&*$&%.")4$
,
and check
find such that
$&()3"/($
subtype
-.(&&%*3"0%*
8%"$-3+($0-5/$3&52508$0%$/-%#0(6
else
#$2(3#6569$
false.
)"#+(4$8%"$3#($9%569$0%$:365;"
0-($
Minimal Typing
$
!"#$%&'()*+(
else
false.
Minimal Typing Minimal Typing
By introducing the rule of subsumption into the typing relation, we have lost the
useful syntax-directedness property, which in the simply typed lambda-calculus
allows a typechecker to be implemented simply by “reading the rules from bottom
is a
to top.” Without this property, how can we now check whether
derivable statement?
We do it in two steps:
1. Find
another set of syntax rules that
syntax directed, defining a relation
January
15, 2000
13. Sare
UBTYPING
93
. This is called the algorithmic typing relation.
2. Show that, although they are defined differently, the ordinary typing relation
and the algorithmic typing relation
give rise to the same
set of typable terms. Interestingly, though, they will not assign exactly the
same types to those terms.
In order to find an algorithmic presentation of the typing relation, we need to
think carefully about the structure of typing derivations to see where the rule of
subsumption is really needed, and where we could just as well do without it.
For example, consider the following derivation:
..
.
..
.
think carefully about the structure of typing derivations to see where the rule of
subsumption is really needed, and where we could just as well do without it.
For example, consider the following derivation:
..
.
Example ..
.
T-S UB
T-A BS
Any such derivation can be rearranged like this:
..
.
S-R EFL
..
.
T-A BS
S-A BS
T-S UB
So we can get away with not allowing instances of T-S UB as the final rule in the
right-hand premise of an instance of T-A BS.
Similarly, any derivation of the form
..
.
..
.
..
.
S-A RROW
..
.
T-S UB
can be replaced by one of the form:
..
.
..
.
..
.
T-S UB
T-A PP
..
.
T-S UB
January 15, 2000
13. S
Example (cont.) 94
UBTYPING
That is, we can take any derivation where subsumption is used as the final rule in
the left-hand premise of an instance of T-A PP and replace it by a derivation with
one instance of subsumption appearing at the end of the right-hand premise and
another instance of subsumption appearing at the end of the whole derivation.
Finally, adjacent uses of subsumption can be coalesced, in the sense that any
derivation of the form
..
.
..
.
..
.
T-S UB
T-S UB
can be rewritten:
..
.
..
.
..
.
S-T RANS
T-S UB
13.2.6 Exercise [Recommended]: Show that a similar rearrangement can be performed on derivations in which T-S UB is used immediately before T-P ROJ .
A motivating intuition behind all of these transformations is that nothing is
harmed by moving most uses of subsumption as late as possible in a typing derivation, so that each subterm is given the smallest possible (or minimal) type at the
outset and this small type is maintained until a point is reached (in the application
rule) where a larger type is needed. This motivates the following definition:
formed on derivations in which T-S UB is used immediately before T-P ROJ .
A motivating intuition behind all of these transformations is that nothing is
harmed by moving most uses of subsumption as late as possible in a typing derivation, so that each subterm is given the smallest possible (or minimal) type at the
outset and this small type is maintained until a point is reached (in the application
rule) where a larger type is needed. This motivates the following definition:
Defini0on !"#$%&'()*+($$
13.2.7 Definition: The algorithmic typing relation is the least relation closed under the following rules:
Algorithmic typing
•  ,-($.#/0$%&'()*+($%1$%"#$)23//4$5/$0%$
Algorithmic typing (
)
(TA-V )
!"#$%&'&()*+*(&,*$%"-%+##.(-*/+(-0+-&*
,.1'.(*2&%3*4'"%1*5#&*$&%.")4$$367$&()3"/($
(TA-A )
8%"$-3+($0-5/$3&52508$0%$/-%#0(6$8%"#$2(3#6569$
January 15, 2000
13. S
(TA-A 95
)
)"#+(4$8%"$3#($9%569$0%$:365;"230($0-($
for each
2369"39($<50-$36$56/59-0$2(3#6569=$$$$ (TA-R )
AR
BS
UBTYPING
PP
CD
(TA-P ROJ)
•  ,-($/()%67$%&'()*+($5/$0%$(+(6$&(-.(&&%*3"0%*
13.2.8 Exercise [Recommended]: Show, by example, that minimal types of terms
",(*/+(-0+-&>$$
in this calculus can decrease during evaluation. Give a term such that
Completeness: Every typing statement that can be proved by an ordinary derivation can Theorem almost be proved
by an algorithmic derivation.
(In fact, the algorithmic derivation may yield a better type.)
13.2.9 Theorem
[Soundness of the algorithmic typing relation]: If
",(*/+(-0+-&>$$
.•  ,-($/()%67$%&'()*+($5/$0%$(+(6$&(-.(&&%*3"0%*
, then
2369"39($<50-$36$56/59-0$2(3#6569=$$$$
0%$ Proof: By straightforward
induction on algorithmic typing derivations. The de)"#+(4$8%"$3#($9%569$0%$:365;"230($0-($
-*/+(-0+-&*
tails are left8%"$-3+($0-5/$3&52508$0%$/-%#0(6$8%"#$2(3#6569$
as an exercise.
,.1'.(*2&%3*4'"%1*5#&*$&%.")4$$367$&()3"/($
67$&()3"/($
!"#$%&'&()*+*(&,*$%"-%+##.(-*/+(-0+-&*
The ordinary
typing relation can be used to assign many types to a term, while
•  ,-($.#/0$%&'()*+($%1$%"#$)23//4$5/$0%$
%"#$2(3#6569$
the algorithmic typing
relation assigns
at most one (this is easy to check). So a
!"#$%&'()*+($
$
0($0-($straightforward converse of Theorem 13.2.9 is clearly not going to hold. Instead,
can show that if a term has a type under the ordinary typing rules, then
=$$$$ itwehas
. In
a better type under the algorithmic rules, in the sense that
other words, the algorithmic rules assign each typeable term its smallest possible
(“minimal”) type.
(-.(&&%*3"0%*
13.2.10 Theorem [Minimal Typing]: If
, then
for some
.
Proof: Exercise (recommended).
13.2.11 Exercise: If we dropped the function subtyping rule S-A RROW but kept
0%$ the
rest of the subtyping and typing rules the same, would we lose the minimal
typing property? If not, prove it. If so, give an example of a term that would have
-*/+(-0+-&*
two incomparable types.
.2.13 Exercise [Recommended]: Show how to extend the proof of preservation
r the simply typed lambda-calculus (7.4.11) to the system with subtyping.
Implementa0on 3.3 Implementation
January 15, 2000
13. S UBTYPING
97
13.4
Meets and Joins (1) Meets and
Joins
A type
is called a join of a pair of types
for all types , if
and
and
if
then
A given typed lambda-calculus is said to have joins if, for every
some that is a join of and under .
Similarly, a type is called a meet of and if
for all types , if
and
and , there is
then
A pair of types and is said to be bounded below if there is some type such
and
. A typed lambda-calculus is said to have bounded
that
meets if, for every and such that and are bounded below, there is some
that is a meet of and .
13. S
Meets and Joins (2) January 15, 2000
UBTYPING
98
Meets and Joins (3) 14.1 Objects
Impera0ve Objects (1) January 15, 2000
14. I MPERATIVE O BJECTS
101
An object is a data structure encapsulating some internal state and offering access
to January
this state
to clients via a collection
of methods.
The internal state is
15, 2000
14. I MPERATIVE
O BJECTS
101typically
up into given
a number
ofsender
mutable
instance
variables
fields)
that are
, which
causes
it toshared
tobroken
some number
by the
of the
message;
and (or
among the
definitions
the methods
(and the
typically
inaccessible
to the
rest of the
message,
our cells
areit just
increment
number
it isof
(Except
, which
causes
to
to somethe
number
given
byholding.
the sender
of thefor
message; and
program).
An
object’s
methods
are
invoked
by
the
operation
of
message-sending.
s
that
we
saw
in
Chapter
9.)
an
object-oriented
variation
of
the
message, our cells are just
increment the number it is holding. (Except for the
Our
running
example
for
the
chapter
will
be
simple
“storage
cell”
A
simple way
of obtaining
using
the features
haveobjects.
stud- Each
that we saw
in Chapter
9.) we
anvery
object-oriented
variation
of the thissbehavior
A
simple
obtaining
thisthe
behavior
using
the
features
we have
stud-of, which
cellsoobject
hold
a of
single
number
and
respond
to three
ied
farvery
is will
to
use
away
reference
cell
for
object’s
internal
state messages:
and
a record
ied
so
far
is
to
use
a
reference
cell
for
the
object’s
internal
state
and
a
record
of
functions
methods.
, which sets its internal
state
causes itfor
tothe
return
the number it currently has;
functions for the methods.
100
state
sharedbybythe
themethods
methods and
and inaccessible
inaccessible to
TheThe
factfact
thatthat
thethe
state
is is
shared
tothe
therest
restofofthe
the
program arises directly from the lexical scope of the variable . “Sending a mesprogram arises directly from the lexical scope of the variable . “Sending a message” to the object means just extracting some field of the record and applying it
means justFor
extracting
sage”
to the
object argument.
to an
appropriate
example:some field of the record and applying it
to an appropriate argument. For example:
Note the use of the syntactic sugar for sequencing (cf. Section ??) in the last line.
We could equivalently (but less readably) have written
Impera0ve Objects (2) Note the use of the syntactic sugar for sequencing (cf. Section ??) in the last line.
We could equivalently (but less readably) have written
or even (using the
convention for “don’t care” variable names):
We may want to create and manipulate a lot of counters, so it will be convenient
to use an abbreviation for their rather long record type:
To force the typechecker to print the type of a new counter using the short form
January 15, 2000
14. I MPERATIVE O BJECTS
102
, we add an explicit coercion (recall from Section ?? that
means the
):
same as
(This need to explicitly annotate terms to control the way their types are printed
is a little annoying. It is the price we pay for “unbundling” all the features of our
Impera0ve Objects (3) (This need to explicitly annotate terms to control the way their types are printed
is a little annoying. It is the price we pay for “unbundling” all the features of our
programming language into the simplest and most orthogonal possible units, so
that we can study them in isolation. A practical high-level programming language
design would probably combine many of these primitive features into more convenient macro-constructs.)
A typical piece of “client code” that manipulates counter objects might look
something like this:
The function
takes a counter object and increments it three times by sending
messages in succession.
it three
14.2 Object Generators
In the development that follows, it will be useful to be able to manipulate all the
instance variables of an object as a single unit. To support this, let us change the
internal representation of our counters to be a record of reference cells and use the
name of this record to refer to instance variables from the method body.
14.2 ObjectObject Generators
Generators In the development that follows, it will be useful to be able to manipulate all the
instance variables of an object as a single unit. To support this, let us change the
internal representation of our counters to be a record of reference cells and use the
name of this record to refer to instance variables from the method body.
January 15, 2000
14. I MPERATIVE O BJECTS
This representation makes it is easy, for example, to write a counter generator that
creates a new counter every time it is called.
103
14.3 SubtypingSubtyping One of the reasons for the popularity of object-oriented programming styles is that
they permit objects of many shapes to be manipulated by the same client code.
objects defined above,
For example, suppose that, in addition to the
we also create some cell objects with one more method that allows them to be reset
to their initial state (say, ) at any time.
Note that our old
too:
function on counters can safely use our refined counters
14.4 Basic classes
Basic classes (1) The definitions of
and
are identical except for the
method. Of course, for such short definitions this makes little
body of the
difference, but if the method bodies were much longer we might quickly find ourselves wanting to write the common ones in just one place. The mechanism by
which this is achieved in most object-oriented languages is called the class.
in terms of
:
Here is one very simple way to define
But this trick is not very robust. For example, if the type
had just
and
methods (no
), then we couldn’t write
in this way.
A more general approach is to abstract the methods of a prototype counter
object over the instance variables. This is a simple example of a class.
January 15, 2000
14. I MPERATIVE O BJECTS
Basic classes (3) 14.5 Extending the Internal State
Suppose we want to define a class of
s whose reset method re
their state to its value at the last call to a new method backup...
14.5 Extending Extendingtthe
he IInternal
nternal State
State (1) Suppose we want to define a class of
s whose reset method resets
their state to its value at the last call to a new method backup...
Here is the class:
The interesting point is that we can actually define
in
. Note how subtyping is used in checking the defiterms of
Extending the Internal State (2) The interesting point is that we can actually define
in
. Note how subtyping is used in checking the defiterms of
. Also, note that we need to override the definition of the
nition of
method as well as adding
.
January 15, 2000
14. I MPERATIVE O BJECTS
106
The variable
was used above to “copy functionality” from the superclass
into the new subclass.
variable can also be used inside method definitions to extend the
The
superclass’s behavior with something new. Suppose, for instance, that we want a
class in which the
method increments not only
variant of our
Extending the Internal State (3) The variable
was used above to “copy functionality” from the superclass
into the new subclass.
variable can also be used inside method definitions to extend the
The
superclass’s behavior with something new. Suppose, for instance, that we want a
class in which the
method increments not only
variant of our
the cell’s value but also the backed-up value in the instance variable . (Goodness
knows why this behavior would be useful—it’s just an example!)
Note how use of
code here.
class’s
in the definition of
avoids repeating the super-
14.6 Classes with “Self”
Our final extension is allowing the methods of classes to refer to each other “re-
Classes with “Self” (1) 14.6 Classes
with “Self”
Our final extension is allowing the methods of classes to refer to each other “recursively.” For example, suppose that we want to implement the inc method of
simple counters in terms of the get and set methods. (Of course, all three methods
are so small in this example that all this code reuse is hardly worth the trouble!
For larger examples, though, it can make a substantial difference in code size and
maintainability.)
January 15, 2000
14. I MPERATIVE O BJECTS
107
Classes in mainstream object-oriented languages such as Smalltalk, C++, and
Java actually support a more general form of recursive call between methods,
sometimes known as open recursion. Instead of taking the fixed point within the
class, we wait until we use the class to actually create an object.
Classes with “Self” (2) Classes in mainstream object-oriented languages such as Smalltalk, C++, and
Java actually support a more general form of recursive call between methods,
sometimes known as open recursion. Instead of taking the fixed point within the
class, we wait until we use the class to actually create an object.
What’s interesting about leaving the recursion open is that, when we build a
, we can override the methods of
new class by subclassing
in such a way that recursive calls to these methods from within
will
Classes with “Self” (3) What’s interesting about leaving the recursion open is that, when we build a
, we can override the methods of
new class by subclassing
in such a way that recursive calls to these methods from within
will
actually invoke the new method bodies that we provide. For example, here’s a
method has been called.
subclass that keeps track of how many times the
January 15, 2000
14. I MPERATIVE O BJECTS
108
, the call to
from
Notice that, because of the open recursion through
results in the a field being incremented, even though the incrementwithin
is defined in the subclass and the call to
appears in the
ing behavior of
superclass.
15.1 Examples
Recursive Types Lists
Hungry Functions
As another simple illustration of the use of recursive types, here is a type of functions that can accept any number of numeric arguments:
An element of this type can be defined using the least fixed point operator on
values:
January 15, 2000
15. R ECURSIVE T YPES
110
Recursive Values from Recursive Types Recursive Values from Recursive Types
A more challenging example—and one that reveals some of the power of the extension we are making—uses recursive types to write down a well-typed version
of the least-fixed-point combinator:
Untyped Lambda-Calculus, Redux
Perhaps the best illustration of the power of recursive types is the observation
that it is possible to embed the whole untyped lambda-calculus (in a well-typed
way!) into a statically typed calculus with recursive types. Let be the following
universal type:
(Note that the form of ’s definition is reminiscent of the defining properties of
“universal domains” in denotational semantics.) Now define an “injection func-
Untyped Lambda-­‐Calculus, Redux Untyped Lambda-Calculus, Redux
Perhaps the best illustration of the power of recursive types is the observation
that it is possible to embed the whole untyped lambda-calculus (in a well-typed
way!) into a statically typed calculus with recursive types. Let be the following
universal type:
(Note that the form of ’s definition is reminiscent of the defining properties of
“universal domains” in denotational semantics.) Now define an “injection funcmapping functions from to into elements of as follows:
tion”
Now, suppose is a closed pure lambda-term involving just variables, abstractions, and applications. Then we can construct an element of representing ,
written , in a uniform way as follows:
For example, here is how the untyped fixed point combinator is expressed as an
element of :
numbers to elements of . (N.b.: this exercise has nothing to do with hungry functions!)
Recursive Objects Recursive Objects
15.2 Equi-recursive Types
note that we cannot obtain all infinite trees by writing finite expressions using ,
just the regular ones.
Prove this by writing a function that expands a type into its regular tree form,
and showing that this function is not surjective (maybe the latter is an exercise).
introduce the idea of contractive types
Equi-­‐recursive Types (1) 15.2 Equi-recursive Types
note that we cannot obtain all infinite trees by writing finite expressions using ,
just the regular ones.
Prove this by writing a function that expands a type into its regular tree form,
and showing that this function is not surjective (maybe the latter is an exercise).
introduce the idea of contractive types
use15,
the
equivalence relation (should
to capture the 113
January
2000
15. Rintroduce
ECURSIVEitTpreviously)
YPES
intuition that a recursive type is “the same as” its unfolding
but this doesn’t give us enough equivalences. For example
are equal as trees but inequivalent.
and
So we switch to a coinductive view of equivalence.
now equivalence coincides with equality of infinite tree expansions (prove
it!)
Now we need to decide this coinductive equivalence
– show simulation algorithm (and its ML realization)
– prove that it is sound, complete, and terminating
notice that we need to deal with more than just type equivalence: we also need
function that unrolls recursive types as needed during typto provide an
ing. What needs to be proved about this?
Algorithmic rules for equi-recursive types
Algorithmic type equivalence (
)
– show simulation algorithm (and its ML realization)
Equi-­‐recursive Types (2) – prove that it is sound, complete, and terminating
notice that we need to deal with more than just type equivalence: we also need
function that unrolls recursive types as needed during typto provide an
ing. What needs to be proved about this?
Algorithmic rules for equi-recursive types
Algorithmic type equivalence (
)
(QA-L OOP)
(QA-R EC L)
(QA-R EC R)
(QA-B ASE)
(QA-A RROW )
Exposure (
)
(XA-R EC)
is not a recursive type
Algorithmic typing (
January 15, 2000
(XA-O THER)
)
15. R ECURSIVE T YPES
(TA-V114
AR)
(TA-A BS)
(TA-A PP)
ML Implementation
Datatypes
(TA-A BS)
Equi-­‐recursive Types (3) (TA-A PP)
ML Implementation
Datatypes
Type substitution
Type equivalence
Equi-­‐recursive T15.ypes (4) R
T January 15, 2000
ECURSIVE
YPES
115
Typing
15.3 Iso-recursive Types
A different, and even simpler, way of dealing with recursive types is to “make the
isomorphism explicit”...
Simula 67 •  First object-­‐oriented language •  Designed for simula0on –  Later recognized as general-­‐purpose prog language •  Extension of Algol 60 •  Standardized as Simula (no 67 ) in 1977 •  Inspira0on to many later designers –  Smalltalk –  C++ –  ... Brief history • 
Norwegian Compu0ng Center –  Designers: Dahl, Myhrhaug, Nygaard –  Simula-­‐1 in 1966 (strictly a simula0on language) –  General language ideas •  Influenced by Hoare s ideas on data types •  Added classes and prefixing (subtyping) to Algol 60 –  Nygaard •  Opera0ons Research specialist and poli0cal ac0vist •  Wanted language to describe social and industrial systems •  Allow ordinary people to understand poli0cal (?) changes –  Dahl and Myhrhaug •  Maintained concern for general programming Comparison to Algol 60 •  Added features –  class concept –  reference variables (pointers to objects) –  pass-­‐by-­‐reference –  char, text, I/O –  corou0nes •  Removed –  Changed default par passing from pass-­‐by-­‐name –  some var ini0aliza0on requirements –  own (=C sta0c) variables –  string type (in favor of text type) Objects in Simula •  Class –  A procedure that returns a pointer to its ac0va0on record •  Object –  Ac0va0on record produced by call to a class •  Object access –  Access any local variable or procedures using dot nota0on: object. •  Memory management –  Objects are garbage collected •  user destructors considered undesirable Example: Circles and lines •  Problem –  Find the center and radius of the circle passing through three dis0nct points, p, q, and r •  Solu0on –  Draw intersec0ng circles Cp, Cq around p,q and circles Cq , Cr around q, r (Picture assumes Cq = Cq ) –  Draw lines through circle intersec0ons –  The intersec0on of the lines is the center of the desired circle. –  Error if the points are colinear. q p r Approach in Simula •  Methodology –  Represent points, lines, and circles as objects. –  Equip objects with necessary opera0ons. •  Opera0ons –  Point equality(anotherPoint) : boolean distance(anotherPoint) : real (needed to construct circles) –  Line parallelto(anotherLine) : boolean (to see if lines intersect) meets(anotherLine) : REF(Point) –  Circle intersects(anotherCircle) : REF(Line) Simula Point Class class Point(x,y); real x,y; formal p is pointer to Point begin boolean procedure equals(p); ref(Point) p; if p =/= none then equals := abs(x -­‐ p.x) + abs(y -­‐ p.y) < 0.00001 real procedure distance(p); ref(Point) p; if p == none then error else distance := sqrt(( x -­‐ p.x )**2 + (y -­‐ p.y) ** 2); end ***Point*** p :-­‐ new Point(1.0, 2.5); uninitialized ptr has
q :-­‐ new Point(2.0,3.5); value none
if p.distance(q) > 2 then ... pointer assignment
Representa0on of objects p
access link real x real y proc equals proc distance 1.0
2.5
code for
equals
code for
distance
Object is represented by ac0va0on record with access link to find global variables according to sta0c scoping Simula line class class Line(a,b,c); real a,b,c; begin boolean procedure parallelto(l); ref(Line) l; if l =/= none then parallelto := ... ref(Point) procedure meets(l); ref(Line) l; begin real t; if l =/= none and ~parallelto(l) then ... end; real d; d := sqrt(a**2 + b**2); if d = 0.0 then error else begin d := 1/d; a := a*d; b := b*d; c := c*d; end; end *** Line*** Local variables
line determined by
ax+by+c=0 Procedures
Initialization:
normalize a,b,c
Derived classes in Simula •  A class decl may be prefixed by a class name class A A class B A class C B class D •  An object of a prefixed class is the concatena0on of objects of each class in prefix –  d :-­‐ new D(…) d
A part
B part
D part
Subtyping •  The type of an object is its class •  The type associated with a subclass is treated as a subtype of the type assoc with superclass •  Example: class A(…); ... A class B(…); ... ref (A) a :-­‐ new A(…) ref (B) b :-­‐ new B(…) a := b /* legal since B is subclass of A */ ... b := a /* also legal, but run-­‐0me test */ Main object-­‐oriented features Classes Objects Inheritance ( class prefixing ) Subtyping Virtual methods –  A func0on can be redefined in subclass •  Inner –  Combines code of superclass with code of subclass •  Inspect/Qua –  run-­‐0me class/type tests • 
• 
• 
• 
• 
Features absent from Simula 67 •  Encapsula0on –  All data and func0ons accessible; no private, protected •  Self/Super mechanism of Smalltalk –  But has an expression this〈class〉 to refer to object itself, regarded as object of type 〈class〉. Not clear how powerful this is… •  Class variables –  But can have global variables •  Excep0ons –  Not an OO feature anyway ... Simula Summary •  Class –  procedure" that returns ptr to ac0va0on record –  ini0aliza0on code always run as procedure body •  Objects: closure created by a class •  Encapsula0on –  protected and private not recognized in 1967 –  added later and used as basis for C++ •  Subtyping: determined by class hierarchy •  Inheritance: provided by class prefixing Smalltalk •  Major language that popularized objects •  Developed at Xerox PARC –  Smalltalk-­‐76, Smalltalk-­‐80 were important versions •  Object metaphor extended and refined –  Used some ideas from Simula, but very different lang –  Everything is an object, even a class –  All opera0ons are messages to objects –  Very flexible and powerful language •  Similar to everything is a list in Lisp, but more so •  Example: object can detect that it has received a message it does not understand, can try to figure out how to respond. Mo0va0ng applica0on: Dynabook •  Concept developed by Alan Kay (now Disney?) •  Small portable computer –  Revolu0onary idea in early 1970 s •  At the 0me, a minicomputer was shared by 10 people, stored in a machine room. –  What would you compute on an airplane? •  Influence on Smalltalk –  Language intended to be programming language and opera0ng system interface –  Intended for non-­‐programmer –  Syntax presented by language-­‐specific editor Smalltalk language terminology Object Instance of some class Class Defines behavior of its objects Selector Name of a message Message Selector together with parameter values Method Code used by a class to respond to message Instance variable Data stored in object Subclass Class defined by giving incremental modifica0ons to some superclass • 
• 
• 
• 
• 
• 
• 
Example: Point class •  Class defini0on wrijen in tabular form class name
Point
super class
Object
class var
pi
instance var
x y
class messages and methods
〈…names and code for methods...〉
instance messages and methods
〈…names and code for methods...〉
7,&/494/&/3>*B1&=*%$,6.$6/)*7#9%/#/,&*$*6$4:$6/*01%%/0&14*$,3*$4/*38,$#70$%%8*&89/3>*C=/8*$4/*
Basics (1) $%)1*01,)73/4/3*4/-%/0&7</*:/0$.)/*&=/8*0$,*4/-%/0&*$,3*0=$,6/*1,*&=/#)/%</)*$&*4.,;&7#/*D-14*
/E$#9%/A*7,*"#$%%&$%'F*true become: false*7)*$*<$%73*)&$&/#/,&G*D?7'79/37$G>*
,-%+.(/)0/+
*
C1*.,3/4)&$,3*/7&=/4*%$,6.$6/A*H/(%%*,//3*&1*/I.$&/*&=/74*#1)&*:$)70*-/$&.4/)>*?=$&*
-1%%1H)*$4/*)7#9%/*/E$#9%/)*&1*6/&*.)*)&$4&/3>*5%/$)/*,1&/*&=$&*&=/*/E$#9%/)*:/%1H*$4/*,1&*
/E=$.)&7</A*7,*:1&=*%$,6.$6/)A*&=/4/*$4/*%7'/%8*)/</4$%*1&=/4*H$8)*&1*$9941$0=*&=/*&$)'*$&*=$,3>*
1(2)(3$%+4%0$(2(')*&/+(&5+6//)7&"%&'/+
"#$%%&$%'!
“this is a comment in Smalltalk”
| x y | “declare the variable x and y”
x := 1 “assign x a value of 1”
y := $q “assign y the character ‘q’”
()*+!
# this is a comment in Ruby
x = 1 # declare and assign x a value of 1
y = ‘q’ # declare and assign y a value of ‘q’
82)&')&7+'*+'-%+902%%&+
"#$%%&$%'!
Transcript show: ‘Hello World’.
()*+!
()*+!
# this is a comment in Ruby
Basics (2) x = 1 # declare and assign x a value of 1
y = ‘q’ # declare and assign y a value of ‘q’
82)&')&7+'*+'-%+902%%&+
"#$%%&$%'!
Transcript show: ‘Hello World’.
()*+!
p ‘Hello World’
:*&5)')*&($+9'('%"%&'/+
"#$%%&$%'!
"#$%%&$%'!
Transcript show: ‘Hello World’.
Basics (3) ()*+!
p ‘Hello World’
:*&5)')*&($+9'('%"%&'/+
!! "#$%%&$%'()*+,-%./,0/*1,*213/4,*54164$##7,6!
*
"#$%%&$%'!
x > 1
ifTrue: [Transcript show: ‘x is greater than 1’.]
ifFalse: [Transcript show: ‘x is less than or equal to 1’.]
"#$%!
if x > 1
p ‘x is greater than 1’
else
p ‘x is less than or equal to 1’
end
!""#$%&'
&'())*()+!
| x |
x := 10
x timesRepeat: [Transcript show: ‘this is an iteration’.]
1 to: x do: [Transcript show: ‘this is an iteration’.]
[x > 5] whileTrue: [x := x + 1. Transcript show: ‘this is an iteration’.]
p ‘x is greater than 1’
else
p ‘x is less than or equal to 1’
end
Basics (4) !""#$%&'
&'())*()+!
| x |
x := 10
x timesRepeat: [Transcript show: ‘this is an iteration’.]
1 to: x do: [Transcript show: ‘this is an iteration’.]
[x > 5] whileTrue: [x := x + 1. Transcript show: ‘this is an iteration’.]
"#$%!
x = 10
x.times{ p ‘this is an iteration’ }
1.upto(10){ p ‘this is an iteration’ }
while i > 5 do
i += 1
p ‘this is an iteration’
end
()*+',-.+/01'
I=$&*7):*&1*.&7%78/*$*>7,$4A*1</4$&14:*).0=*$)*JJ*1,*$,*1>?/0&*C%/&()*)$A:*K=$4*7,*"#$%%&$%':*"&47,6*7,*
Basics (5) L.>AD*4/M.74/)*1,%A*&=$&*&=/*K=$4*C14*"&47,6D*0%$))*7#<%/#/,&*$*#/&=13*,$#/3*;JJB*$,3*4/&.4,*
$*N11%/$,*H$%./*C9=70=*7)*$%)1*$,*1>?/0&ODE*
!""#$%&#'(&)*++,-./*'%&
*
N1&=* L.>A* $,3* "#$%%&$%'* 3/)047>/* $44$A)* 7,* $* )7#7%$4* -$)=71,E* +,* "#$%%&$%':* $44$A)* $4/*
-7P/3*%/,6&=*01%%/0&71,):*#/$,7,6*&=$&*1,0/*&=/A*$4/*3/0%$4/3*&=/74*)78/*#$A*,1&*0=$,6/Q*
| myArray x |
myArray := #(1 2 3 $a $b $c).
x := myArray at: 4
“arrays indices in Smalltalk begin at 1, the above statement yields $a”
I=/* $>1H/* 3/)047>/)* $,* $44$A* 1-* #7P/3* &A</:* 9=70=* 7,0%.3/)* &=/* +,&/6/4)* R:* S:* T* $,3* &=/*
K=$4$0&/4)*U$(:*U>(:*U0(*C7,*"#$%%&$%'*V!*W9=/4/*!*7)*$,A*0=$4$0&/4X*3/,1&/)*$*)7,6%/*0=$4$0&/4DE*+,*
L.>A:* &=/* )$#/* 7)* <1))7>%/:* >.&* &=/*<4164$##/4* #$A* 3A,$#70$%%A* )78/*&=/* $44$A* C%7'/* $* %7,'/3*
%7)&D*$)*=/*)//)*-7&Q*
myArray = [1, 2, 3, ‘a’, ‘b’, ‘c’]
Basics (6) !! "#$%%&$%'()*+,-%./,0/*1,*213/4,*54164$##7,6!
*
myArray << ‘d’
x = myArray[3] # yields ‘a’
"#$%%&$%'*8$)*)/9/4$%*:1%%/0&71,*0%$))/)*;8708*/<=1)/*$*,.#>/4*1-*#/&813)*&8$&*#$'/*&8/#*-$4*
#14/* /<=$,3$>%/* &8$,* 1437,$4?* $44$?)@* "1#/* >$)70* .)$6/* 1-* $* :1%%/0&71,* 7,* "#$%%&$%'* 7)* $)*
-1%%1;)A**
| myCollection x |
myCollection := OrderedCollection with: 1 with: 2 with: 3 with: $a with: $b
myCollection addLast: $c
Transcript show: myCollection at: 4. “$a”
+,*B.>?C*$,?*01%%/0&71,*1&8/4*&8$,*$,*7,&/6/4*>1.,3*$44$?*7)*01,)73/4/3*$*D$)8C*;8708*7)*)7#=%?*
$*01%%/0&71,*;7&8*'/?*EF*9$%./*=$74)@*G8/)/*$4/*9/4?*)7#=%?*3/-7,/3*$)A**
myCollection = { :please => “give”, :me => “me”, :an => “A+” }
p myCollection[:an] # prints A+
G8/*-$,0?*'/?)C*=4/-7</3*;7&8*$*01%1,*HAI*$4/*)?#>1%)*H&81.68*&8/?*31,(&*8$9/*&1*>/I@*G87)*>47,6)*
.)*&1*1.4*,/<&*&1=70J*
!"#$%&'(
!" #$%&'()*'+%*,$-*.)/$0+*1)
Basics (7) 2" #$%&'()34-&3'1)&')$')34-&3')5$15)-$60*)
+,*"#$%%&$%'8*$*)9#:1%*7)*4/;4/)/,&/3*$)<*
#thisIsMySymbol
+,*=.:98*&>/*)$#/<*
:thisIsMySymbol
?$'/*-14*/@$#;%/*$*>$)>*1-*$,7#$%*,17)/)8*4/-/4/,0/3*7,*&A1*0$)/)*:/%1A8*1,0/*A7&>*)&47,6*'/9)8*
$,3*$6$7,*A7&>*)9#:1%)<*
animalNoises = { “cat” => “meow”, “dog” => “woof”, “cow” => “moo” }
animalNoises = { :cat => “meow”, :dog => “woof”, :cow => “moo” }
B)* $,* 1:)/4C/4* 1-*&>/* 013/8* &>/74* 7,&/,&* 7)* 73/,&70$%8* :.&* &>/* $;;%70$&71,* 1-* )9#:1%)* 7)* $* #14/*
%1670$%*$;;41$0>*7-*&>/*01,)&4.0&*7)*$C$7%$:%/*&1*91.D*?>$&*7)8*7-*91.*31,(&*,//3*&1*&4/$&*91.4*'/9)*
$)*)&47,6)8*A>9*:1&>/4*317,6*7&E*B*)9#:1%*7)*$*.,7F./8*4/$3$:%/*73/,&7-7/4*-14*)1#/*1:G/0&*91.*
A$,&*&1*4/;4/)/,&H*&>/9*01,).#/*%/))*#/#149*I7&*7)*A14&>*,1&7,68*>1A/C/48*&>$&*&>/9*$4/*,1&*
6$4:$6/*01%%/0&/3J*$,3*$4/*7##.&$:%/D*K$%./)*0$,*:/*4/&47/C/3*-41#*&>7)*>$)>*&$:%/*$)*-1%%1A)<**
dogNoise = animalNoises[“dog”]
dogNoise = animalNoises[:dog]
?>/* ;17,&* 7)8* )9#:1%)* /@7)&* $)* $* #/$,)* &1* ,$#/* 1:G/0&)* 7,* =.:9* $,3* "#$%%&$%'8* &1* &4/$&* &>/#*
1&>/4A7)/*A1.%3*:/*$,*7,/--707/,&*.)/*1-*&>/*%$,6.$6/D*
!"#$$%#&'%()*+,'%-)."#/#*0,&$%
Basics (8) ?>/* ;17,&* 7)8* )9#:1%)* /@7)&* $)* $* #/$,)* &1* ,$#/* 1:G/0&)* 7,* =.:9* $,3* "#$%%&$%'8* &1* &4/$&* &>/#*
1&>/4A7)/*A1.%3*:/*$,*7,/--707/,&*.)/*1-*&>/*%$,6.$6/D*
!"#$$%#&'%()*+,'%-)."#/#*0,&$%
*
B,3* %$)&8* :.&* 0/4&$7,%9* ,1&* %/$)&8* A/* A7%%* 01C/4* >1A* 91.* #76>&* 3/-7,/* ,/A* 0%$))/)* $,3*
#/&>13)*7,*:1&>*%$,6.$6/)D*?>/*-1%%1A7,6*/@$#;%/*7)*-41#*L%47'*"0>.%&M()*"#$%%&$%'*?.&147$%<*
!! "#$%%&$%'()*+,-%./,0/*1,*213/4,*54164$##7,6!
*Employee subclass: #SalariedEmployee
instanceVariableNames:
!!
'position salary'
poolDictionaries: ' ' !
"#$%%&$%'()*+,-%./,0/*1,*213/4,*54164$##7,6!
classVariableNames: ' '
*
poolDictionaries: ' ' !
! SalariedEmployee publicMethods !
! SalariedEmployee publicMethods !
position: aString
position: aString
position
:= aString !
position := aString !
position
position
^position !
^position !
salary: n
salary:
n
salary
:= n !
salary
salary := n !
^salary ! !
salary
+,*&8/*$91:/*013/;*&8/*0%$))*"$%$47/3<#=%1>//*7)*3/-7,/3*$)*$*).90%$))*1-*<#=%1>//;*$,3*7&*8$)*
^salary ! !
&?1*7,)&$,0/*:$47$9%/)*@:$47$9%/)*&8$&*/A7)&*.,7B./%>*-14*/$08*7,)&$,0/*1-*&87)*0%$))CD*=1)7&71,*$,3*
)$%$4>E*F81)/*G7,)&$,0/*:$47$9%/)H*$4/*$0&.$%%>*=.9%70*#/&813);*7,*&87)*0$)/E*+-*>1.*$4/*-$#7%7$4*
+,*&8/*$91:/*013/;*&8/*0%$))*"$%$47/3<#=%1>//*7)*3/-7,/3*$)*$*).90%$))*1-*<#=%1>//;*$,3*7&*8$)*
?7&8*I$:$*)/&&/4J6/&&/4*#/&813)*14*KL*6/&J)/&*=41=/4&7/);*&87)*7)*4768&*$%1,6*&81)/*)$#/*%7,/)E*M*
&?1*7,)&$,0/*:$47$9%/)*@:$47$9%/)*&8$&*/A7)&*.,7B./%>*-14*/$08*7,)&$,0/*1-*&87)*0%$))CD*=1)7&71,*$,3*
.)/4*#$>*0$%%*&8/*=1)7&71,*#/&813*1-*&8/*"$%$47/3<#=%1>//*0%$))*&1*)/&*14*6/&*&8/*=/4)1,()*
Class messages and methods Three class methods newX:xvalue Y:yvalue | | ^ self new x: xvalue y: yvalue newOrigin | | ^ self new x: 0 y: 0 ini0alize | | pi <-­‐ 3.14159 Explanation
- selector is mix-fix newX:Y:
e.g, Point newX:3 Y:2
- symbol ^ marks return value
- new is method in all classes,
inherited from Object
- | | marks scope for local decl
- initialize method sets pi, called
automatically
- <- is syntax for assignment
Instance messages and methods Five instance methods x: xcoord y: ycoord | | x <-­‐ xcoord y <-­‐ ycoord moveDx: dx Dy: dy | | x <-­‐ dx + x y <-­‐ dy + y x | | ^x y | | ^y draw | | 〈...code to draw point...〉 Explanation
set x,y coordinates,
e.g, pt x:5 y:3
move point by given amount
return hidden inst var x
return hidden inst var y
draw point on screen
Run-­‐0me representa0on of point to superclass Object
Point class
Template
Point object
class
x
y
x
y
3
2
Method dictionary
Detail: class method shown in
dictionary, but lookup procedure
distinguishes class and instance
methods
newX:Y:
...
move
code
...
code
Inheritance •  Define colored points from points class name
ColorPoint
super class
Point
class var
instance var
color
new instance
variable
class messages and methods
newX:xv Y:yv C:cv 〈 … code … 〉
new method
instance messages and methods
color
| | ^color
draw
〈 … code … 〉
override Point
method
Run-­‐0me representa0on Point object
Point class
x
y
2
3
ColorPoint object
4
5
red
Template
ColorPoint class
Method dictionary
newX:Y:
draw
move
...
Template
x
y
color
Method dictionary
newX:Y:C:
color
draw
This is a schematic diagram meant to illustrate the main idea. Actual implementations may differ.
Encapsula0on in Smalltalk •  Methods are public •  Instance variables are hidden –  Not visible to other objects •  pt x is not allowed unless x is a method –  But may be manipulated by subclass methods •  This limits ability to establish invariants •  Example: –  Superclass maintains sorted list of messages with some selector, say insert –  Subclass may access this list directly, rearrange order Object type • 
• 
Each object has interface –  Set of instance methods declared in class –  Example: Point { x:y:, moveDx:Dy:, x, y, draw} ColorPoint { x:y:, moveDx:Dy:, x, y, color, draw} –  This is a form of type Names of methods, does not include type/protocol of arguments Object expression and type –  Send message to object p draw p x:3 y:4 q color q moveDx: 5 Dy: 2 –  Expression OK if message is in interface Subtyping •  Rela0on between interfaces –  Suppose expression makes sense p msg:pars -­‐-­‐ OK if msg is in interface of p –  Replace p by q if interface of q contains interface of p •  Subtyping –  If interface is superset, then a subtype –  Example: ColorPoint subtype of Point –  Some0mes called conformance Can extend to more detailed interfaces that include types of parameters Subtyping and Inheritance •  Subtyping is implicit –  Not a part of the programming language –  Important aspect of how systems are built •  Inheritance is explicit –  Used to implement systems –  No forced rela0onship to subtyping Collec0on Hierarchy Collection
isEmpty, size, includes: , …
at:
Indexed
Set
Updatable
at:Put:
Dictionary
Array
replaceFrom:to:with:
Sorted collection
associationAt:
Subtyping
Inheritance
add:
remove:
sortBlock:
…
Smalltalk Flexibility •  Measure of PL expressiveness: –  Can constructs of the language be defined in the language itself? –  Examples: •  Lisp cond: Lisp allows user-­‐defined special forms •  ML datatype: sufficient to define polymorphic lists, equivalent to built-­‐in list type •  ML overloading: limita0on, since not available to programmer •  C/C++: ??? •  Smalltalk is expressive in this sense –  Many constructs that would be primi0ves other are definable in Smalltalk –  Example: Booleans and Blocks Smalltalk booleans and blocks •  Boolean value is object with ifTrue:ifFalse: –  Class boolean with subclasses True and False –  True ifTrue:B1 ifFalse:B2 executes B1 –  False ifTrue:B1 ifFalse:B2 executes B2 •  Example expression i < j ifTrue: [i add 1] ifFalse: [j subtract 1] –  i < j is boolean expression, produces boolean object –  arg s are blocks, objects with execute methods •  Since booleans and blocks are very common –  Op0miza0on of boolean –  Special syntax for blocks Self and Super Integer Factorial | | self <= 1 SmallInt ifTrue: [^1] ifFalse: [^(self-­‐1) factorial * self LargeInt This method can be implemented in Integer, and works even if SmallInt and LargeInt are represented differently. C++ and Java type systems can t really cope with this. Ingalls test •  Dan Ingalls: principal designer Smalltalk system –  Grace Murray Hopper award for Smalltalk and Bitmap graphics work at Xerox PARC –  1987 ACM So‚ware Systems Award with Kay, Goldberg •  Proposed test for object oriented –  Can you define a new kind of integer, put your new integers into rectangles (which are already part of the window system), ask the system to blacken a rectangle, and have everything work? –  Smalltalk passes, C++ fails this test Smalltalk integer opera0ons •  Integer expression –  x plus: 1 0mes: 3 plus: (y plus: 1) print •  Proper0es –  All opera0ons are executed by sending messages –  If x is from some new kind of integer, expression makes sense as long as x has plus, 0mes, print methods. Actually, compiler does some op0miza0on. But will revert to this if x is not built-­‐in integer. Costs and benefits of true OO •  Why is property of Ingalls test useful? –  Everything is an object –  All objects are accessed only through interface –  Makes programs extensible •  What is implementa0on cost? –  Every integer opera0on involves method call •  Unless op0mizing compiler can recognize many cases –  Is this worth it? •  One applica0on where it seems useful ? •  One applica0on where it seems too costly? •  Are there other issues? Security? (wait for Java final classes…) Smalltalk Summary •  Class –  creates objects that share methods –  pointers to template, dic0onary, parent class •  Objects: created by a class, contains instance variables •  Encapsula0on –  methods public, instance variables hidden •  Subtyping: implicit, no sta0c type system •  Inheritance: subclasses, self, super Single inheritance in Smalltalk-­‐76, Smalltalk-­‐80 
Download