________________________________________________________________________

This file is part of Logtalk <https://logtalk.org/>  
Copyright 1998-2020 Paulo Moura <pmoura@logtalk.org>

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(proxies(loader)).
...


% print the area and the perimeter for all circle proxies:

| ?- forall(circle(Id, R, C), circle(Id, R, C)::print).

id: #1, area: 4.75291, perimeter: 7.72831, color: blue
id: #2, area: 43.2412, perimeter: 23.3106, color: yellow
id: #3, area: 0.477836, perimeter: 2.45044, color: green
id: #4, area: 103.508, perimeter: 36.0655, color: black
id: #5, area: 217.468, perimeter: 52.2761, color: cyan
true.


% Logtalk provides a convenient notation for accessing proxies
% represented as Prolog facts when sending a message:

| ?- {circle(_, _, _)}::print, fail; true.

id: #1, area: 4.75291, perimeter: 7.72831, color: blue
id: #2, area: 43.2412, perimeter: 23.3106, color: yellow
id: #3, area: 0.477836, perimeter: 2.45044, color: green
id: #4, area: 103.508, perimeter: 36.0655, color: black
id: #5, area: 217.468, perimeter: 52.2761, color: cyan
true.


% print the area and the perimeter for the circle #2:

| ?- {circle('#2', R, C)}::print.

id: #2, area: 43.2412, perimeter: 23.3106, color: yellow
R = 3.71,
C = yellow.


% construct a list with the areas of all circles:

| ?- findall(Area, {circle(_, _, _)}::area(Area), Areas).

Areas = [4.75291, 43.2412, 0.477836, 103.508, 217.468].


% some example queries for backend Prolog compilers implementing the 
% time/1 timing predicate (e.g. SWI-Prolog or YAP; the adapter files
% for these two systems ensure that a ::/2 goal in the argument of the
% time/1 predicate is compiled prior to calling it so that we benchmark
% the code instead of the compiler):

?- set_logtalk_flag(optimize, on).
true.

?- logtalk_load(proxies(loader)).
...
true.

?- time(true).  % autoload the predicate in the case of SWI-Prolog
% 2 inferences, 0.000 CPU in 0.000 seconds (67% CPU, 250000 Lips)
true.

?- time(circle(one, 7, red)::id(Id)).
% 1 inferences, 0.000 CPU in 0.000 seconds (56% CPU, 76923 Lips)
Id = one.

?- time(circle(one, 7, red)::radius(Radius)).
% 1 inferences, 0.000 CPU in 0.000 seconds (61% CPU, 83333 Lips)
Radius = 7.

?- time(circle(one, 7, red)::color(Color)).
% 1 inferences, 0.000 CPU in 0.000 seconds (56% CPU, 71429 Lips)
Color = red.

?- Id0 = one, time(circle(Id0, 7, red)::id(Id)).
% 1 inferences, 0.000 CPU in 0.000 seconds (56% CPU, 76923 Lips)
Id0 = Id, Id = one.

?- Radius0 = 7, time(circle(one, Radius0, red)::radius(Radius)).
% 1 inferences, 0.000 CPU in 0.000 seconds (61% CPU, 83333 Lips)
Radius0 = Radius, Radius = 7.

?- Color0 = red, time(circle(one, 7, Color0)::color(Color)).
% 1 inferences, 0.000 CPU in 0.000 seconds (56% CPU, 71429 Lips)
Color0 = Color, Color = red.

?- time({circle('#2', Radius, Color)}::id(Id)).
% 2 inferences, 0.000 CPU in 0.000 seconds (71% CPU, 90909 Lips)
Radius = 3.71,
Color = yellow,
Id = '#2'.
