this text file

advertisement
PRObjLOG = PROLOG + OBJECTS implemented in Lolli
by G.Delzanno, DISO-University og Genova, August 12, 1997
PRObjLOG is an extension of Prolog with Object-Oriented features.
The language is designed accordingly to the object-calculi proposed
by Abadi & Cardelli, Fisher & Honsell & Mitchell, etc/.
In particular the idea is to enrich the universe of plain terms of Prolog
with structured data, i.e, objects, which contain method definitions
given in clausal form.
Message passing becomes here a built-in of the language.
In PRObjLOG it is possible to declare classes, create instances, and
modify them as described below.
Accirdingly to the above mentioned calculi, here objects do not
have identifiers, they are passed as values through different
invocations, e.g., using an abstract syntax PRObjLOG provides
built-in goal expression as O.m -> P, P.n -> Q, Q.k ->R
where m,n,k are method names and O,P,Q,R object expressions
(O must be ground).
The logical foundations of PRObjLOG lie in Higher-Order
Intuitionistic Linear Logic: Lolli results to be a perfect platform
to implement a prototype for it, thanks to the higher-order facility
provided here (e.g., free variables in G-position).
USAGE:
In order to use the language it is necessary to load the modules
aux.ll and obj.ll.
The latter contains the definitions of the object-primitives like
"new", "send", "override" etc.
For instance, in order to see the examples in "first.ll" working,
use the following:
?- aux --o obj --o first --o top.
and then insert one of the sample queries below as
DESCRIPTION OF THE OBJECT-ORIENTED FACILITIES:
PRObjLOG allows to define classes with the predicate:
* class ClassName ListofAttributes ListofMethodsNames.
* class ClassName ListofAttributes ListofMethodsNames .
where ListofAttributes has the form
nil or ((AttrName,Value)::ListofAttributes)
and
ListofMethodsNames has the form
nil or (MethName::ListofMethodsNames)
MethName is the name of a user-defined method (as explained below)
or one of the built-in methods 'init', 'write', 'swrite' (the latter
only writes the attributes of an object).
Inheritance can be achieved by writing complex clauses for classes
definition.
Example:
class myc St Mts:St=((a,0)::(b,0)::nil),
Mts=(mymeth::mypred::init::write::nil).
class mycc ((d,0)::) Mts:class myc St Mts.
% mycc inherits a,b,mypred,mymeth,init,write from myc.
Methods can be defined by facts having one of the following formats:
* meth MethodName Self Parameters Result (
BODY
).
* pred MethodName Self Parameters (
BODY
).
where
- BODY is a conjunction of goals (i.e., G1,...,Gn),
- Self a free variable that can be used inside the method to
self-referencing the defining object,
Ex (contained in "first.ll")
meth mymeth
get Self a
get Self b
R is X + Y
).
pred
get
get
X >
X >
).
Self X R (
Y,
Z,
+ Z
mypred Self X (
Self a Y,
Self b Z,
Y,
Z
The built-in objects operations (i.e. built-in predicates) are:
* new Class Obj: to create an object of class Class,
OBj is the very object and not a reference to it.
Ex:
?-new myc P.
?-new mycc P,call P write X.
* get
ObjExpG AttrName
Value:
to retrieve the value of an attribute
note: ObjExpG can be an Obj expression or (g ObjExpG Meth).
Where get (g ObjExpG m) n R stands for the composition
of get invocations: get ObjExprS m V, get V n R.
(i.e., O.get m.get n -> R = O.get m -> V, V.get n -> R).
Ex: ?-new myc P,get P a X.
* update
Obj AttrName
NewValue:
to update the value of an attribute
Ex: ?-new myc P,update P a 5.
* send ObjExpS MethodName Parameters Result:
to invoke a meth-method
note: ObjExpS can be an Obj expression or (s ObjExpS Meth Pars).
Where send (s ObjExpS m Ps) n Qs R stands for the composition
of methods invocations: send ObjExpS m Ps V, send V n Qs R.
(i.e., O.m.n -> R = O.m -> V, V.n -> R).
Ex: ?-new myc P,send P mymeth 4 R.
* call Obj MethodName Parameters:
to invoke a pred-method
Ex: ?-new myc P,call P mypred 4.
* enrich
Obj AttrName
Value NewObj:
to add a new attribute with Value
Ex: ?-new myc P,enrich P c 6 Q, get Q c X.
* override Obj MethName Def
NewObj:
to override a method
Ex: ?-new myc P,
override P mymeth ((p Self nil) <= write_sans("Buh!"),nl,nl) Q,
call Q mymeth nil.
* extend
note:
Obj MethName Def
NewObj:
to add a method
Def must have the form ((m Self Pars) <= BODY) wher Self and BODY
as before. In order to avoid name-clash of variables new
names must be considered for universally quantified vars.
EXAMPLES IN ./ :
- first.ll
: the sample queries above.
* over.ll
: an example of methods overriding
Usage:
aux --o obj --o over --o top.
Description:
The possibility of dynamically modifying the set of methods of a \probjobjects can
be useful in order to deal with exceptions, without the need of inserting
a new
class and thus to re-design the data scheme.
For instance, consider the definitions in "over.ll".
The class "employee" inherits the attributes and methods of the class
"person"
(the methods are the built-in ones specified in obj.ll) adding the method
"check" which checks whether the salary of an employee is less then the
salary
of his/her superior (see predicates test/1, test1/1 in "over.ll").
Ex:
?- test.
?- test1.
Now, if we assume that a given "employee" enjoys extra earnings and we
want to
specify this by adding a further attribute "extras" to its set of
methods, than
we must re-arrange the definition of method "check" in order not to
burden the
scheme by adding a further method see predicate "test/2" in "over.ll"
Here a query like "modify 1000" creates a new employee E, and assuming
a new definition of "check", enriches the set of attributes of "E" with
the pair
(extras,1000) and overrides the definition of "check" with the new one D.
Ex: ?- test 1000.
?- test 3000.
* geo,class,triangle.ll:
A complex example stolen from computational geometry
Usage:
aux --o
obj --o gaux --o gclass --o gmain --o top.
Description:
In computer graphics problem concerning surfaces representation and
analisys it comes
naturally to resort to logic programming techniques to handle queries on
a large
search-space.
Here we will consider a particular representation of surfaces based
on triangulated models. The idea is to consider various level of
precision in the
description of a terrain by defining a hierarchy of triangles (called
HTTM:
hierarchical triangulation terrain model), i.e., triangles can be
subdivided in more
precise ones.
Our problem is a simplification of the more general of solving
interference queries:
given a HTTM T, a geometrical object O, and a fixed precision epsilon,
it is to find the minimal triangle Tr interfering with O and such that
its precision is less or equal to epsilon.
In our case we will consider "points" as geometrical objects with the
interference
relation given as follows: a point belongs to a triangle of the
hierarchy.
Let us formulate the data-scheme in our \probj prototype.
We will reconsider the class "point" definition of module "gclass" in
addition
to the class "triangle" of mod. "gclass", whose attributes are
its vertices "p, q, r", considered in anticlockwise order, its precision
"eps",
and the list of subtriangles "lx".
The most important method of this class is "in" which takes Self and
InP (a point object) as input arguments and checks whether InP belongs to
Self.
The other ones are used to pretty-print its objects.
The main program is based on a visit of the HTTM in order to search for
the minimal
triangle this simple program is shown in module "gmain", where "start" is
the
goal that must be invoked to start the search process.
?- demo.
?- start.
Download