Did you know ... Search Documentation:
Pack logtalk -- logtalk-3.77.0/examples/predicate_lookups/SCRIPT.txt

This file is part of Logtalk https://logtalk.org/ SPDX-FileCopyrightText: 1998-2023 Paulo Moura <pmoura@logtalk.org> SPDX-License-Identifier: Apache-2.0

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

% start by loading the example:

| ?- logtalk_load(predicate_lookups(loader)). ...

% when sending a message to a prototype, the lookup for the predicate % declaration starts at the prototype itself; therefore, the following % message is valid:

| ?- bike::frame(Material).

Material = aluminum yes

% when a prototype doesn't contain a declaration for the predicate in % the message, the lookup continues in the prototype parent(s):

| ?- bike::where(Where).

Where = land yes

% when sending a message to a prototype, the lookup for the predicate % definition starts at the prototype itself:

| ?- mountain_bike::frame(Material).

Material = carbon yes

% when a prototype doesn't contain a definition for the predicate in % the message, the lookup continues in the prototype parent(s):

| ?- mountain_bike::crewed.

yes

% we can use the built-in reflection predicates to query about predicate % declarations and definitions; for example:

| ?- mountain_bike::current_predicate(Predicate).

Predicate = crewed/0 ; Predicate = frame/1 ; Predicate = where/1 ; no

| ?- mountain_bike::predicate_property(frame(_), Property).

Property = logtalk ; Property = scope(public) ; Property = (public) ; Property = static ; Property = declared_in(bike) ; Property = declared_in(bike, 37) ; Property = defined_in(mountain_bike) ; Property = defined_in(mountain_bike, 49) ; Property = redefined_from(bike) ; Property = redefined_from(bike, 39) ; Property = number_of_clauses(1) ; Property = number_of_rules(0) yes

% when sending a message to an instance, the lookup for the predicate % declaration starts at the instance class; therefore, the following % message is valid:

| ?- paraglider::structure(Structure).

Structure = soft yes

% note that the lookup for the predicate definition starts in the % instance itself, resulting in the structure(soft) answer;

% when the instance class doesn't contain a declaration for the predicate % in the message, the lookup continues in the class superclasses; therefore, % the following message is also valid:

| ?- sailplane::purpose(Purpose).

Purpose = fun yes

% when the instance doesn't contain a definition for the predicate in the % message, the lookup continues in the class and, if not found there, in % the class superclasses:

| ?- sailplane::structure(Structure).

Structure = rigid yes

% we can use the built-in reflection predicates to query about predicate % declarations and definitions; for example:

| ?- sailplane::current_predicate(Predicate).

Predicate = purpose/1 ? ; Predicate = structure/1 ? ; no

| ?- sailplane::predicate_property(purpose(_), Property).

Property = logtalk ? ; Property = scope(public) ? ; Property = public ? ; Property = static ? ; Property = declared_in(artificial) ? ; Property = declared_in(artificial,40) ? ; Property = defined_in(sailplane) ? ; Property = defined_in(sailplane,74) ? ; Property = redefined_from(aircraft) ? ; Property = redefined_from(aircraft,53) ? ; Property = number_of_clauses(1) ? ; Property = number_of_rules(0) yes