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/aliases/aliases.mlgt
pmoura 921e576877 Logtalk 2.21.0 files.
git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@1138 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
2004-09-14 23:11:12 +00:00

71 lines
1.2 KiB
Plaintext

/*
This example illustrates the use of the predicate directive alias/3 for
defining alternative names for inherited predicates.
*/
% first, we define a simple parametric object for representing rectangles:
:- object(rectangle(_Width, _Height)).
:- public(width/1).
:- public(height/1).
:- public(area/1).
width(Width) :-
parameter(1, Width).
height(Height) :-
parameter(2, Height).
area(Area) :-
::width(Width),
::height(Height),
Area is Width*Height.
:- end_object.
% next, we define a square object which adds an alias, side/1, for the
% inherited predicate width/1:
:- object(square(Side),
extends(rectangle(Side, Side))).
:- alias(rectangle(_, _), width/1, side/1).
:- end_object.
% a similar example can be defined using ellipses and circles:
:- object(ellipse(_RX, _RY)).
:- public(rx/1).
:- public(ry/1).
:- public(area/1).
rx(Rx) :-
parameter(1, Rx).
ry(Ry) :-
parameter(2, Ry).
area(Area) :-
::rx(Rx),
::ry(Ry),
Area is Rx*Ry*3.1415927.
:- end_object.
% in this case, we define an alias named r/1 for the inherited
% predicate rx/1:
:- object(circle(Radius),
extends(ellipse(Radius, Radius))).
:- alias(ellipse(_, _), rx/1, r/1).
:- end_object.