2002-05-28 12:35:10 +01:00
|
|
|
=================================================================
|
|
|
|
Logtalk - Object oriented extension to Prolog
|
2005-08-08 13:13:45 +01:00
|
|
|
Release 2.25.1
|
2002-05-28 12:35:10 +01:00
|
|
|
|
2005-01-13 12:22:42 +00:00
|
|
|
Copyright (c) 1998-2005 Paulo Moura. All Rights Reserved.
|
2002-05-28 12:35:10 +01:00
|
|
|
=================================================================
|
|
|
|
|
|
|
|
|
2004-06-13 19:04:28 +01:00
|
|
|
% start by loading the example:
|
|
|
|
|
2004-11-29 20:36:31 +00:00
|
|
|
| ?- logtalk_load(dynpred(loader)).
|
2004-06-13 19:04:28 +01:00
|
|
|
...
|
|
|
|
|
|
|
|
|
|
|
|
% sending to descendant the message p/1, returns the definition in root:
|
2002-05-28 12:35:10 +01:00
|
|
|
|
|
|
|
| ?- descendant::p(Value).
|
|
|
|
|
|
|
|
Value = root
|
|
|
|
yes
|
|
|
|
|
|
|
|
|
2004-06-13 19:04:28 +01:00
|
|
|
% asserting a local definition for p/1 in descendant overrides the inherited
|
2002-05-28 12:35:10 +01:00
|
|
|
% definition:
|
|
|
|
|
|
|
|
| ?- descendant::(assertz(p(descendant)), p(Value)).
|
|
|
|
|
|
|
|
Value = descendant
|
|
|
|
yes
|
|
|
|
|
|
|
|
|
2005-04-23 00:57:45 +01:00
|
|
|
% if we retract the local definition, again the definition inherited from root
|
2002-05-28 12:35:10 +01:00
|
|
|
% will be used:
|
|
|
|
|
|
|
|
| ?- descendant::(retractall(p(_)), p(Value)).
|
|
|
|
|
|
|
|
Value = root
|
|
|
|
yes
|
|
|
|
|
|
|
|
|
|
|
|
% class does not understand the message p1/1 (the predicate is declared only
|
|
|
|
% for the class descendant instances):
|
|
|
|
|
|
|
|
| ?- class::p1(X).
|
|
|
|
|
|
|
|
error(existence_error(predicate_declaration, p1(_)), class::p1(_), user)
|
|
|
|
|
|
|
|
|
|
|
|
% the same message is valid for the class instances:
|
|
|
|
|
|
|
|
| ?- instance::p1(X).
|
|
|
|
|
|
|
|
X = class
|
|
|
|
yes
|
|
|
|
|
|
|
|
|
2004-06-13 19:04:28 +01:00
|
|
|
% if we assert a clause for a new predicate, p2/1, in the class
|
2002-05-28 12:35:10 +01:00
|
|
|
% (a side-effect being a dynamic declaration of the predicate):
|
|
|
|
|
|
|
|
| ?- class::assertz(p2(class)).
|
|
|
|
|
|
|
|
yes
|
|
|
|
|
|
|
|
|
|
|
|
% the new predicate, like p1/1, is not available for the class:
|
|
|
|
|
|
|
|
| ?- class::p2(Value).
|
|
|
|
|
|
|
|
error(existence_error(predicate_declaration, p2(_)), class::p2(_), user)
|
|
|
|
|
|
|
|
|
|
|
|
% but is available for the class instances, the same way as p1/1:
|
|
|
|
|
|
|
|
| ?- instance::p2(X).
|
|
|
|
|
|
|
|
X = class
|
|
|
|
yes
|
|
|
|
|
|
|
|
|
2004-06-13 19:04:28 +01:00
|
|
|
% using a prototype, assert three new predicates (the method object_assert/0
|
2002-05-28 12:35:10 +01:00
|
|
|
% asserts the predicate public_predicate/0 from outside the prototype; the
|
|
|
|
% method self_assert/0 asserts the predicate protected_predicate/0 in self;
|
|
|
|
% the method this_assert/0 asserts the predicate private_predicate/0 in this):
|
|
|
|
|
|
|
|
| ?- prototype::(object_assert, self_assert, this_assert).
|
|
|
|
|
|
|
|
yes
|
|
|
|
|
|
|
|
|
|
|
|
% and check the resulting scope of each predicate:
|
|
|
|
|
|
|
|
| ?- prototype::dynamic_predicates.
|
|
|
|
|
|
|
|
public_predicate/0 - public
|
|
|
|
protected_predicate/0 - protected
|
|
|
|
private_predicate/0 - private
|
|
|
|
yes
|