Download SICStus Prolog User`s Manual
Transcript
Chapter 35: SICStus Objects
409
The following is an object sort that sorts lists of different types. sort has a parameter that
defines the type of the elements of the list. Notice that Type is visible to all methods in the
body of sort, and is used in the method partition/4. In the query, we use sort(rat) to
sort a list of terms denoting rational numbers. We must therefore define a rat object and
its < method also:
rat :: {
(P/Q < R/S) :- :(P*S < Q*R)
}.
sort(Type) :: {
:- :use_module(library(lists), [append/3]) &
qsort([], []) &
qsort([P|L], S) :partition(L, P, Small, Large),
qsort(Small, S0),
qsort(Large, S1),
:append(S0, [P|S1], S) &
partition([], _P, [], []) &
partition([X|L1], P, Small, Large) :(
Type :: (X < P) ->
Small = [X|Small1], Large = Large1
;
Small = Small1, Large = [X|Large1]
),
partition(L1, P, Small1, Large1)
}.
| ?- sort(rat) :: qsort([23/3, 34/11, 45/17], L).
L = [45/17,34/11,23/3]
Parameterized objects are interesting in their own right in Prolog even if one is not interested
in the object-oriented paradigm. They provide global context variables in a Prolog program
without having to add such variables as additional context arguments to each clause that
potentially uses the context.
35.3 Self, Message Sending, and Message Delegation
In SICStus Objects, each method is executed in the context of an object. This object may
not be the static object where the method is declared. The current contextual object is
used to determine dynamically which attributes are accessed, and which methods are called.
This leads to a mechanism known as dynamic binding. This object can be retrieved using
the universal method self(S), where S will be bound to the current contextual object.
Related documents
SICStus Prolog User`s Manual
SICStus Prolog User`s Manual
SICStus Prolog User's Manual
SICStus Prolog User`s Manual
Planning using Situation Calculus, Prolog and a - CEUR
Ciao Manual - The Ciao System
SICStus Prolog Release Notes
Sec - SICStus Prolog 3
PR Pilot 575 User Manual
Design and Applied Technology (Secondary 4 – 6)
AX50 Color Screen Digital Mobile Phone User Manual