Professional Documents
Culture Documents
Pluto TOOLS01
Pluto TOOLS01
Pluto TOOLS01
Systems (TOOLS USA 2001) Santa Barbara, USA, July 29-August 3, 2001. IEEE-CS Press, pp. ??-??.
Meng
hi Liu
S
hool of Computer S
ien
e, Carleton University, Ottawa, Canada
meng
his
s.
arleton.
a
Abstra t
The obje
t-oriented philosophy
reates a powerful synergy throughout the development life
y
le by
ombining abstra
tion, en
apsulation, and modularity. However, existing obje
t-oriented program-
ming languages are low-level pro
edural ones that are hard to program and debug. Logi
programming
allows
omputing problems to be expressed in a high-level de
larative way, without giving instru
-
tions for how the problem is to be solved. However, logi
programming la
ks powerful methods to
model appli
ations. In the past de
ades, the integration of obje
t-oriented programming and logi
programming has attra
ted a lot of interests with a number of languages proposed. But these lan-
guages fail to address some key obje
t-oriented features properly. In this paper, we introdu
e a novel
obje
t-oriented logi
programming language that supports nearly all obje
t-oriented features in a logi
programming framework. We also des
ribe its operational semanti
s.
1 Introdu
tion
Obje
t-oriented programming and logi
programming are two independently developed areas in
Computer S
ien
e. They oer better programming te
hnologies than traditional pro
edural-based
approa
h. Logi
programming began in the early seventies as a dire
t outgrowth of earlier work
in automated theorem proving and arti
ial intelligen
e [10℄. Logi
programming is based on rst-
order logi
, formalized in terms of proof theory and model theory. Proof theory provides formal
spe
i
ations for
orre
t reasoning with premises, while model theory analyses how general assertions
may be interpreted with respe
t to a
olle
tion of spe
i
fa
ts. First-order logi
was not used in
programming a
tual appli
ations until the introdu
tion of Prolog, a language for PROgramming
in LOGi
. Prolog uses a restri
ted form of more general theorem proving te
hniques to provide
eÆ
ien
y and programmability. It allows
omputing problems to be expressed in a de
larative way,
without giving instru
tions for how the problem is to be solved.
Begun in late sixties, obje
t-oriented programming is now the most popular programming paradigm.
Obje
t orientation is a strategy for organizing systems as
olle
tions of intera
ting obje
ts that
om-
bine data and behavior. The obje
t-oriented philosophy
reates a powerful synergy throughout the
development life
y
le by
ombining abstra
tion, en
apsulation, and modularity. Abstra
tion lets us
fo
us on essential aspe
ts of an appli
ation while ignoring details. En
apsulation separates external
spe
i
ation from internal implementation. Modularity promotes
oheren
e, understandability, and
symmetry by organizing a system into groups of
losely related obje
ts.
In the past few years, the integration of obje
t-oriented and logi
programming has attra
ted
great interests. The obje
tive is to gain the best of both approa
hes. Various methods of in
or-
porating obje
t-oriented programming into Prolog have been dis
ussed [5, 13, 14℄. Also, a number
of novel languages have been proposed su
h as OOLP [3℄, C-Logi
[2℄, L&O [11℄, Prolog++ [12℄,
COMPLEX [4℄, LIFE [1℄ and some of them have been implemented. However, none of them support
all important obje
t-oriented features.
In this paper, we des
ribe Pluto, an obje
t-oriented logi
programming language that supports
nearly all obje
t-oriented features in a logi
programming framework, su
h as obje
t identity,
om-
plex obje
ts,
lass denitions, typing,
lause-based methods, en
apsulation of data and methods,
information hiding, overloading, late binding, polymorphism,
lass hierar
hy, and non-monotoni
multiple stru
tural and behavioral inheritan
e with overriding, blo
king and
on
i
t handling.
Pluto takes many features from ROL2 [8℄ and Java. Unlike ROL2 whi
h is intended to be mainly
a database query language, Pluto is intended to be a general programming language and in
ludes
many programming related features not in ROL2, su
h as Input/Output, Constru
tors, a novel way
to obje
t
reations, Prolog way of assignments, dot notations and thus more like Java and Prolog.
It has two major goals: to support in a
lause-based framework whatever obje
t-oriented languages
an support but at a higher
on
eptual and de
larative level so that programming
an be
ome easier
as many low-level pro
edural
onstru
ts are no longer needed; and to support in an obje
t-oriented
fashion whatever logi
programming languages
an support.
The paper is organized as follows. Se
tion 2 introdu
es types, values and
lasses in Pluto. Se
tion
3 dis
usses non-monotoni
multiple inheritan
e in Pluto. Se
tion 4 shows how to use Pluto and how
to program in Pluto. Se
tion 5
on
ludes the paper.
Input/output In Pluto, two primitive input/output
ommands are provided: write and read like
in Prolog. The following
lass denition
ontains the input/output methods
lass person [ birthyear ) integer; gender )
har('M','F');
showPerson() f showPerson() :{ birthyear ! B, gender ! G,
write("The birth year is " + B), write("The gender is " + G) g℄
where + is a string
on
atenation operation as in Java.
Note that in the above showPerson method, we have to use variables B and G to bind birthyear
and gender. From programming point of view, it is
umbersome however. For this reason, Pluto
allows the user to use attribute names to stand for attribute values. For example, the rule in the
above showPerson method
an also be written in Pluto as follows:
showPerson() :{ write("The birth year is " + birthyear), write("The gender is " + gender)
Instan
e methods
an only be invoked through instan
es. For example, if bob referen
es an
instan
e of person, then the following query invokes the showPerson method:
?- bob.showPerson()
In Pluto, more spe
i
methods for input/output are provided in the
lass system su
h as readInt,
, , , , , ,
readReal readChar readString writeInt writeReal writeChar writeString.
Dedu
tion Pluto is a logi
programming language. It is mainly used to derive information that
is not expli
itly available. Consider the following
lass denition.
) integer; parents) fpersong; stati
thisyear ) integer default 2000;
lass person [ birthyear
age()) integerf age() ! A :{ A = thisyear - birthyearg
an
estors()) fpersong f an
estors()! hXi:{ parents ! hXi;
an
estors()! hXi:{ parents ! hPi, P.an
estors() ! hXig ℄
The rst method derives the age of a person from the birthyear. The se
ond method derives
an
estors from the attribute parents re
ursively. The
onstru
t hX1 ; :::; Xn i is taken from ROL [6℄
and RLOG [7℄ for set manipulations. It fun
tions in two dierent ways: it denotes a subset of the
set when used in the body of the rule and it is used to group a set when used in the head.
In Pluto, method type denition and implementation
an be tightly
oupled. For example, the
above
lass denition
an also be written like the one in Java:
lass person [ birthyear) integer; parents) fpersong; stati
thisyear ) integer default 2000;
age() ) integer A f A = thisyear - birthyearg
an
estors() ) fpersong hXif parents ! hXi; parents ! hXi, an
estors() ! hXig ℄
Like Prolog, rules in Pluto are exe
uted non-deterministi
ally. fail and ! (
ut)
an be used to
ontrol the exe
ution of the rules.
Updates Attribute values of an obje
t may
hange over the time. Like Prolog whi
h resorts to
and retra
t for updates, Pluto uses
assert insert and delete for updates. The following methods
demonstrate updates in Pluto:
lass person [ spouse ) person;
marry(person) f marry(S) :{ not spouse ! , insert spouse ! S, insert S.spouse ! Self g
divor
e() f divor
e() :{ delete spouse ! S, delete S.spouse ! g℄
As the above example shows, always using delete and insert operations is low level and
umber-
some. In Pluto, we
an use the is operation for assignments instead. For example, the above two
methods
an also be written as follows:
marry(person S) f not spouse ! , spouse is S, S.spouse is Self g
divor
e() f spouse.spouse is null, spouse is null g℄
More formally, A is V in Pluto has the following meanings:
(1) if A is not an instan
e variable, then it means
ompute V and store the result in A; otherwise,
(2) if V is null, then it means delete A ! , otherwise,
(3) if V does not
ontain A, then it means insert A ! V , otherwise,
(4) if V
ontains A and assume X does not o
ur in V , then it means delete A ! X , insert A !
V 0 where V 0 is obtained from V by repla
ing A with X .
Constru
tor When an instan
e is
reated, it is usually desirable to perform some initialization
a
tion to setup its attribute values. In Pluto, we
an use
onstru
tors for this purpose. A
onstru
tor
is a method that has the same name as the
lass. Consider the following example:
) integer; gender)
har(f'M','F'g); stati
ount) integer default 0;
lass person [ birthyear
person(integer B)) f
ount is
ount + 1, birthyear is B g
person(integer B,
har G)) f
ount is
ount + 1, birthyear is B, gender is G g ℄
This
lass has two
onstru
tors that take dierent input values.
Unlike methods other than
onstru
tors,
onstru
tors are exe
uted whenever an obje
t of the
lass is
reated. Consider the following query:
?{ liz = new person(1950).
It
reates a new person obje
t and binds the obje
t to the name liz , and assigns the value 1950
into the
orresponding attribute and in
reases the
ount of the obje
ts in the
lass by invoking the
onstru
tor. Note here no value is provided for the attribute gender. As a result, the system assigns
the default value F to the attribute. In order to
hange the value of gender later, we have to use
both delete and insert as shown in the update methods part.
If su
h a
onstru
tor is not dened in the
lass denition, then we have to use the following
queries and updates to a
hieve the same result:
?{ liz = new person(). ?{ liz.
ount is liz.
ount + 1 .
Note that Pluto does type and
onsisten
y
he
king automati
ally. Consider the following query
and update:
?{ pam = new person(1952, "Female"). ?{ liz.birthyear is 1952.
The system will tell that "Female" is not
hara
ter for the rst one and indi
ate liz already has a
value for attribute birthyear for the se
ond.
Obje
t Creation In Pluto, obje
ts
an be
reated in two dierent ways. One is using the obje
t
reation methods. The other is using queries as shown above or using Pluto programs. We dis
uss
the latter in detail in Se
tion 4.
Consider the following
lass denition:
) integer(1..31); month) integer(1..12); year) integer(1950..2050);
lass date [ day
f day is D, month is M, year is Y g
date(integer D, integer M, integer Y)
opy() ) date N f day ! D, month ! M, year ! Y, N = new date(D, M, Y) g ℄
In this example, date is a
onstru
tor and
opy is an obje
t
reation method.
2.2.2 Class Methods
Like attributes, methods in Pluto
an also be
lass ones. A
lass method has the following form
in Pluto:
stati
op (1 ; :::; n ) ) f rule denitions g
where op(1 ; ::; n ) ) is the method type whi
h spe
ies the name op of the method, the argument
types 1 ; :::; n that the method takes, and the result type . In parti
ular op(1 ; ::; n ) is
alled the
signature of the method and has to be unique within the
lass. We
an omit if it is void.
The following is an example:
lass person [ name ) string; stati
ount ) integer default 0;
stati
showCount() f showCount() :{
ount ! C, write(C)g ℄
Class methods
an only use
lass attributes but not instan
e attributes within the
lass. Instan
e
methods, however,
an use both
lass attributes and instan
e attributes in any
lass.
Like
lass attributes,
lass methods
an be invoked through the
lass. For example, we
an issue
the following query:
?{ person.showCount().
Class methods
an also be invoked through the instan
es. We
an have the following query whi
h
generates the same result as the previous one.
?{ bob.showCount().
where the rst method is used to obtain the
hildren of a person while the se
ond is used to obtain
the
ommon
hildren of two persons, one denoted by Self and the other by P. The name
hildren is
overloaded in these two methods.
Like attributes, the use of methods other than
onstru
tors
an also be
ontrolled with the
modiers publi
and private. The default is publi
while the use of private disallows the dire
t use
and is mainly for intermediate methods. The
onstru
tors are always publi
.
As a sub
lass of person, student inherits all instan
e attribute denitions, default values and methods
of person. However, it has its default values for birthyear and gender whi
h override the inherited
ones, its own attributes takes and methods drop and takes.
The next example shows how to blo
k attribute and method inheritan
e from a super
lass.
lass
elibate isa person spouse[ ) none; marry(person) ) none; divor
e() ) none ℄
The built-in
lass none is used here to blo
k the inheritan
e of the
orresponding attribute and
methods from
lass person to
lass
elibate so that instan
es of
elibate do not have a spouse and
annot use methods marry and divor
e.
As Pluto supports method overloading, it is ne
essary to spe
ify the method signature for blo
k-
ing. Also, attribute denitions and their default values if any are bound together in terms of
inheritan
e; that is, if a sub
lass inherits an attribute denition from a super
lass, then it inherits
the default value if any for the
orresponding attribute from the same
lass if it does not override
it. If the attribute denition is not inherited be
ause of overriding, blo
king or
on
i
t handling
me
hanisms to be dis
ussed later, then the
orresponding default value is also blo
ked. Similarly,
when an attribute or method is not inherited, methods based on it may be
ome ill-typed or even
result in run-time errors if they are inherited.
The Pluto system uses the dependen
y relation to determine what
an be inherited automati
ally.
Here wstudent is a sub
lass of employee and student. It inherits attributes and methods from both
employee and student and a
on
i
t on the attribute room happens.
Unlike Java whi
h only allows single inheritan
e and C++ whi
h prohibits
on
i
ting properties
to be inherited, Pluto provides two dierent ways for
on
i
t resolution: (1) user-spe
ied priority
and (2) inheritan
e path sele
tion, with user-spe
ied priority as the default me
hanism:
User-Spe
ied Priority In Pluto, there is a built-in left-to-right ordering of super
lasses in a
lass denition. If two or more super
lasses have the attribute denitions and default values with
the same name or methods with the same signature either dire
tly dened or inherited, then the
one found in the
lass appearing rst in the list is inherited.
Continuing with the previous example, wstudent automati
ally inherits attribute room from em-
ployee rather than from student. It does not inherit the default value for room from student.
However, if two or more super
lasses have methods with the same name but dierent signatures,
then all of them are inherited by the sub
lass. In this
ase, the method is just overloaded.
Inheritan
e Path Sele
tion If a sub
lass needs to inherit one attribute de
laration, default value,
or method from one super
lass and another from a dierent super
lass, then user-spe
ied priority
annot meet these demands. To solve this problem, Pluto allows the user to spe
ify the inheritan
e
path to override user-spe
ied priority using the from keyword.
Continue with the previous example of wstudent. Suppose that employee and student also have a
method in
ome with same signature but dierent implementations. If we want wstudent to inherit
the attribute room and method in
ome from student instead of employee, then we
an use the
following
lass denition:
lass wstudent isa employee, student [ room, in
ome() from student ℄
Note that with this
lass denition, wstudent inherits not only the attribute de
laration but also
the default value of room from student. If the method in
ome is overloaded in wstudent, only the
0-ary method is inherited with the above denition.
4 Programming in Pluto
Programming in Pluto is like programming in other obje
t-oriented programming languages:
writing various
lasses that are needed for the appli
ation and writing programs that use the
lasses.
After dening the
lasses we need to use, we need to
ompile ea
h
lass into its internal form for
optimal exe
ution like in Java. The
ompilation also handles inheritan
e issues so that the
lass in
its internal form
ontains all the attributes, default values and methods, either dire
tly dened or
inherited.
The following are two
omplete
lass denitions:
lass point [ x, y ) real;
point(real X, Y) f x is X, y is Yg
translate(point V) f x is x + V.x, y is y + V.yg
distan
e(point V) ) real D f X is x - V.x, D = sqrt(XX + YY)g ℄
lass re
tangle [ v1, v2, v3, v4 ) point;
re
tangle(int X1, Y1, Y2, Y2, X3, Y3, X4, Y4) f v1 ! new point(X1,Y1),
v2 ! new point(X2,Y2), v3 ! new point(X3,Y3), v4 ! new point(X4,Y4)g
length() ) real Lf L is v1.distan
e(v2)g
width() ) real Wf W is v1.distan
e(v4)g
area() ) real Af A is length()width()g
translate(point V) f v1.translate(V), v2.translate(V), v3.translate(V), v4.translate(V)g ℄
When we have all the
lasses we need, we
an then use the Pluto system intera
tively through
the Pluto
ommand interpreter like Prolog. The rst thing we have to do is to load the
lasses
we need to use into the main memory with the built-in method load in the built-in
lass system.
On
e the
lasses are loaded into the memory, we
an then use various
ommands in Pluto to deal
with obje
ts. There are four kinds of
ommands in Pluto: (1) the obje
t
reation
ommand new
to
reate obje
ts of some
lasses in the main memory, (2) the obje
t destroy
ommand destroy to
delete obje
ts from the main memory and release the spa
e o
upied by the obje
ts, (3) the obje
t
manipulation
ommands insert and delete to manipulate attribute values, (4) the query
ommands to
query attribute values and to invoke various methods. In fa
t, we have already seen some
ommands
in method denitions in previous se
tions.
Consider the following example:
?{ system.load(point). ?{ system.load(re
tangle).
?{ p1 = new point(0,0). ?{ p2 = new point(1,1).
?{ r1 = new re
tangle(0,0,0,1,1,0,1,1). ?{ write(r1.area()).
?{ r1.translate(v2). ?{ v1.destroy().
The rst two are query
ommands that load two
lasses into the main memory. The next three are
obje
t
reation
ommands that
reate two obje
ts in the point
lass and one obje
t in the re
tangle
lass and bind them to names p1, p2 and r1. The rest other than the last are all query
ommands.
The rst one is used to display the area of the re
tangle r1. The se
ond performs translation of the
re
tangle with respe
t to point p2 The last one is an obje
t destroy
ommand whi
h removes the
obje
t p1 from the memory.
Note that in Pluto, some obje
ts may have a name su
h as p1, p2 and r1 above and obje
ts may
not have a name su
h that the point obje
ts
reated by the
onstru
tor of re
tangle. Named obje
ts
remain in the system until they are expli
itly destroyed. Unnamed obje
ts remain in the system
only if they are referen
ed.
In Pluto, we
an also put various
ommands into a named program. A program in Pluto has a
following form: program N ame [
ommands ℄ For example, we
an put the above
ommands in a
program. whi
h
an be dire
tly exe
uted by the Pluto
ommand interpreter in a non-intera
tive
mode.
5 Con
lusion
In this paper, we have des
ribed Pluto, a de
larative programming language that supports nearly
all obje
t-oriented features in a logi
programming framework and its operational semanti
s. We
have shown that the two programming paradigms t together ni
ely so that the best of the two
an
be a
hieved. Furthermore, we have provided the link between the two programming paradigms so
that the user
an understand obje
t-oriented programming from logi
programming point of view
and logi
programming from obje
t-oriented point of view.
Like Prolog, programming in Pluto is mostly de
larative. The power of Pluto
an be easily
extended by developing various
lasses like in Java, su
h as graphi
s, applets, and persistent data.
The Pluto system is
urrently being implemented. We plan to make the system publi
ally avail-
able over the Internet after further testing and debugging. Our obje
tive is to develop a high-level
de
larative programming language to ease the painful programming. To this end, we will add other
features and provide various built-in
lasses to make Pluto a real general programming language.
The semanti
s of Pluto has been partially investigated in [9℄. More general semanti
s for Pluto is
urrently under resear
hing.
Referen
es
[1℄ H. Ait-Ka
i and A. Podelski. Towards a Meaning of Life. Journal of Logi
Programming,
16(3):195{234, 1993.
[2℄ W. Chen and D.S. Warren. C-Logi
for Complex Obje
ts. In Pro
eedings of the ACM Sympo-
sium on Prin
iples of Database Systems, pages 369{378, Philadelphia, Pennsylvania, 1989.
[3℄ M. Dalal and D. Gangopadhyay. OOLP: A Translation Approa
h to Obje
t-Oriented Logi
Programming. In W. Kim, J.M. Ni
olas, and S. Nishio, editors, Pro
eedings of the International
Conferen
e on Dedu
tive and Obje
t-Oriented Databases, pages 593{606, Kyoto, Japan, 1989.
North-Holland.
[4℄ S. Gre
o, N. Leone, and P. Rullo. COMPLEX: An Obje
t-Oriented Logi
Programming System.
IEEE Transa
tions on Knowledge and Data Engineering, 4(4):344{359, 1992.
[5℄ K. Kahn, E.D. Tribble, M.S. Miller, and D. G. Bobrow. Obje
ts in Con
urrent Logi
Program-
ming Languages. In OOPSLA '86 Pro
eedings, pages 247{252. ACM New York, 1986.
[6℄ M. Liu. ROL: A Dedu
tive Obje
t Base Language. Information Systems, 21(5):431 { 457, 1996.
[7℄ M. Liu. Relationlog: A Typed Extension to Datalog with Sets and Tuples. Journal of Logi
Programming, 36(3):271{299, 1998.
[8℄ M. Liu. Overview of the ROL2 Dedu
tive Obje
t-Oriented Database System. In Pro
eedings
of the 30th International Conferen
e on Te
hnology of Obje
t-Oriented Languages & Systems
(TOOLS USA '99), pages 63{72, Santa Barbara, CA, USA, August 1-5 1999. IEEE-CS Press.
[9℄ M. Liu, G. Dobbie, and T. W. Ling. A Logi
al Foundation for Dedu
tive Obje
t-Oriented
Databases. In Pro
eedings of the 7th International Conferen
e on Database Systems for Ad-
van
ed Appli
ations (DASFAA 2001), pages 116{125, Hong Kong, China, April 18-20 2001.
IEEE Computer So
iety Press.
[10℄ J. W. Lloyd. Foundations of Logi
Programming. Springer-Verlag, 2 edition, 1987.
[11℄ F. G. M
Cabe. Logi
and Obje
ts. Prenti
e Hall., 1992.
[12℄ C. Moss. Prolog++. Addison-Wesley, 1994.
[13℄ P. S
ha
hte and G. Saab. EÆ
ient obje
t-oriented programing in prolog. In Christoph Beierle
and Lutz Plumer, editors, Logi
al Programming: Formal Methods and Pra
ti
al Appli
ations,
Studies in Computer S
ien
e and Arti
ial Intelligen
e. Elsevier S
ien
e, 1995.
[14℄ E. Shapiro and A. Takeu
hi. Obje
t oriented programming in
on
urrent Prolog. New Gener-
ation Computing, 1:25{48, 1983.