This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/Logtalk/examples/viewpoints/SCRIPT

110 lines
1.9 KiB
Plaintext
Raw Normal View History

=================================================================
Logtalk - Object oriented extension to Prolog
Release 2.14.3
Copyright (c) 1998-2002 Paulo Moura. All Rights Reserved.
=================================================================
% we can start by asking joe its age:
| ?- joePerson::age(Age).
Age = 30
yes
% the same question could be made via any of its viewpoints:
| ?- joeSportsman::age(Age).
Age = 30
yes
% now let's tell joe to grow old:
| ?- joePerson::growOld.
yes
% we can verify the effect of the above message from any of the viewpoints:
| ?- joeFilmEnthusiast::age(Age).
Age = 31
yes
% because the growOld/0 and the age/1 predicates are implemented using
% property sharing, we can send the growOld message to any viewpoint:
| ?- joeEmployee::growOld.
yes
% we can check this by asking joe its age:
| ?- joePerson::age(Age).
Age = 32
yes
% as you can see, although the modification message have been sent to a
% descendant, its the predicate age/1 in the parent that got updated
% to illustrate value sharing we use a couple of predicates, counter/1 and
% incCounter/0, defined in joePerson:
| ?- joePerson::counter(Counter).
Counter = 0
yes
% initially, counter/1 is only defined for joePerson, so every descendant
% or viewpoint will share its value/definition:
| ?- joeSportsman::counter(Counter).
Counter = 0
yes
% but if we decide to increment the counter by sending the incCounter/0
% message to a descendant:
| ?- joeFilmEnthusiast::incCounter.
yes
| ?- joeFilmEnthusiast::counter(Counter).
Counter = 1
yes
% then the descendant will now have a local definition for counter/1,
% independent of the definition in its parent, joePerson:
| ?- joePerson::counter(Counter).
Counter = 0
yes
% the other descendants/viewpoints will continue to share the definition
% in joePerson:
| ?- joeSportsman::counter(Counter).
Counter = 0
yes