Include Paulo Moura's Logtalk OO LP system
git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@53 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
This commit is contained in:
14
Logtalk/examples/sicstus/NOTES
Normal file
14
Logtalk/examples/sicstus/NOTES
Normal file
@@ -0,0 +1,14 @@
|
||||
=================================================================
|
||||
Logtalk - Object oriented extension to Prolog
|
||||
Release 2.8.4
|
||||
|
||||
Copyright (c) 1998-2001 Paulo Moura. All Rights Reserved.
|
||||
=================================================================
|
||||
|
||||
The examples in this folder are adopted from the SICStus Prolog manual.
|
||||
|
||||
To load all objects in this example consult the sicstus.loader utility
|
||||
file (note that the *.loader files are Prolog files).
|
||||
|
||||
You will also need to load the follwing files in library directory:
|
||||
hierarchies.loader and types.loader.
|
98
Logtalk/examples/sicstus/SCRIPT
Normal file
98
Logtalk/examples/sicstus/SCRIPT
Normal file
@@ -0,0 +1,98 @@
|
||||
=================================================================
|
||||
Logtalk - Object oriented extension to Prolog
|
||||
Release 2.8.4
|
||||
|
||||
Copyright (c) 1998-2001 Paulo Moura. All Rights Reserved.
|
||||
=================================================================
|
||||
|
||||
|
||||
| ?- sort(rational)::sort([1/8, 2/7, 6/5, 2/9, 1/3], Sorted).
|
||||
|
||||
Sorted = [1/8, 2/9, 2/7, 1/3, 6/5]
|
||||
yes.
|
||||
|
||||
|
||||
| ?- sort(colours)::sort([orange, indigo, red, yellow, violet, blue, green], Sorted).
|
||||
|
||||
Sorted = [red, orange, yellow, green, blue, indigo, violet]
|
||||
yes
|
||||
|
||||
|
||||
| ?- sort(user)::sort([3, 1, 4, 2, 9], Sorted).
|
||||
|
||||
Sorted = [1, 2, 3, 4, 9]
|
||||
yes
|
||||
|
||||
|
||||
| ?- red_circle(3)::color(Color).
|
||||
|
||||
Color = red
|
||||
yes.
|
||||
|
||||
|
||||
| ?- red_circle(3)::area(Area).
|
||||
|
||||
Area = 28.274334
|
||||
yes.
|
||||
|
||||
|
||||
| ?- red_circle(3)::ancestors(As).
|
||||
|
||||
As = [circle(3, red), ellipse(3, 3, red)]
|
||||
yes.
|
||||
|
||||
|
||||
|
||||
| ?- red_circle(3)::context.
|
||||
|
||||
red_circle1
|
||||
self: red_circle(3)
|
||||
this: red_circle(3)
|
||||
sender: user
|
||||
|
||||
circle2
|
||||
self: red_circle(3)
|
||||
this: circle(3,red)
|
||||
sender: user
|
||||
|
||||
ellipse3
|
||||
self: red_circle(3)
|
||||
this: ellipse(3,3,red)
|
||||
sender: user
|
||||
|
||||
yes
|
||||
|
||||
|
||||
| ?- square(2)::(side(Side), width(Width), height(Height), area(Area)).
|
||||
|
||||
Side = 2
|
||||
Width = 2
|
||||
Height = 2
|
||||
Area = 4
|
||||
yes.
|
||||
|
||||
|
||||
| ?- square(2)::current_predicate(Pred).
|
||||
|
||||
Pred = side/1 ;
|
||||
|
||||
Pred = width/1 ;
|
||||
|
||||
Pred = height/1 ;
|
||||
|
||||
Pred = area/1 ;
|
||||
|
||||
no
|
||||
|
||||
|
||||
| ?- square(_)::predicate_property(side(_), Prop).
|
||||
|
||||
Prop = public ;
|
||||
|
||||
Prop = static ;
|
||||
|
||||
Prop = declared_in(square(_133)) ;
|
||||
|
||||
Prop = defined_in(square(_164)) ;
|
||||
|
||||
no
|
23
Logtalk/examples/sicstus/circle11.lgt
Normal file
23
Logtalk/examples/sicstus/circle11.lgt
Normal file
@@ -0,0 +1,23 @@
|
||||
|
||||
:- object(circle1(Color),
|
||||
extends(circle(1, Color))).
|
||||
|
||||
|
||||
:- info([
|
||||
authors is 'Paulo Moura',
|
||||
version is 1.0,
|
||||
date is 2000/4/22,
|
||||
comment is 'Parametric object for representing geometric circles with radius = 1.',
|
||||
parnames is ['Color'],
|
||||
source is 'Example adopted from the SICStus Objects documentation.']).
|
||||
|
||||
|
||||
context :-
|
||||
write(circle11), nl,
|
||||
self(Self), write('self: '), writeq(Self), nl,
|
||||
this(This), write('this: '), writeq(This), nl,
|
||||
sender(Sender), write('sender: '), writeq(Sender), nl,
|
||||
^^context.
|
||||
|
||||
|
||||
:- end_object.
|
48
Logtalk/examples/sicstus/circle2.lgt
Normal file
48
Logtalk/examples/sicstus/circle2.lgt
Normal file
@@ -0,0 +1,48 @@
|
||||
|
||||
:- object(circle(Radius, Color),
|
||||
extends(ellipse(Radius, Radius, Color))).
|
||||
|
||||
|
||||
:- info([
|
||||
authors is 'Paulo Moura',
|
||||
version is 1.0,
|
||||
date is 2000/4/22,
|
||||
comment is 'Parametric object for representing geometric circles.',
|
||||
parnames is ['Radius', 'Color'],
|
||||
source is 'Example adopted from the SICStus Objects documentation.']).
|
||||
|
||||
|
||||
:- public(r/1).
|
||||
|
||||
:- mode(r(?number), zero_or_one).
|
||||
|
||||
:- info(r/1, [
|
||||
comment is 'Circle radius.',
|
||||
argnames is ['Radius']]).
|
||||
|
||||
|
||||
r(Radius) :-
|
||||
parameter(1, Radius).
|
||||
|
||||
|
||||
color(Color) :-
|
||||
parameter(2, Color).
|
||||
|
||||
|
||||
rx(Radius) :-
|
||||
::r(Radius).
|
||||
|
||||
|
||||
ry(Radius) :-
|
||||
::r(Radius).
|
||||
|
||||
|
||||
context :-
|
||||
write(circle2), nl,
|
||||
self(Self), write('self: '), writeq(Self), nl,
|
||||
this(This), write('this: '), writeq(This), nl,
|
||||
sender(Sender), write('sender: '), writeq(Sender), nl, nl,
|
||||
^^context.
|
||||
|
||||
|
||||
:- end_object.
|
54
Logtalk/examples/sicstus/colours.lgt
Normal file
54
Logtalk/examples/sicstus/colours.lgt
Normal file
@@ -0,0 +1,54 @@
|
||||
|
||||
:- object(colours,
|
||||
implements(comparingp)).
|
||||
|
||||
|
||||
:- info([
|
||||
authors is 'Paulo Moura',
|
||||
version is 1.0,
|
||||
date is 2000/4/22,
|
||||
comment is 'Implements comparison between visible colors.']).
|
||||
|
||||
|
||||
Colour1 < Colour2 :-
|
||||
order(Colour1, N1),
|
||||
order(Colour2, N2),
|
||||
{N1 < N2}.
|
||||
|
||||
|
||||
Colour1 =< Colour2 :-
|
||||
order(Colour1, N1),
|
||||
order(Colour2, N2),
|
||||
{N1 =< N2}.
|
||||
|
||||
|
||||
Colour1 > Colour2 :-
|
||||
order(Colour1, N1),
|
||||
order(Colour2, N2),
|
||||
{N1 > N2}.
|
||||
|
||||
|
||||
Colour1 >= Colour2 :-
|
||||
order(Colour1, N1),
|
||||
order(Colour2, N2),
|
||||
{N1 >= N2}.
|
||||
|
||||
|
||||
Colour1 =:= Colour2 :-
|
||||
{Colour1 == Colour2}.
|
||||
|
||||
|
||||
Colour1 =\= Colour2 :-
|
||||
{Colour1 \== Colour2}.
|
||||
|
||||
|
||||
order(red, 1).
|
||||
order(orange, 2).
|
||||
order(yellow, 3).
|
||||
order(green, 4).
|
||||
order(blue, 5).
|
||||
order(indigo, 6).
|
||||
order(violet, 7).
|
||||
|
||||
|
||||
:- end_object.
|
88
Logtalk/examples/sicstus/ellipse3.lgt
Normal file
88
Logtalk/examples/sicstus/ellipse3.lgt
Normal file
@@ -0,0 +1,88 @@
|
||||
|
||||
:- object(ellipse(_RX, _RY, _Color),
|
||||
imports(proto_hierarchy)).
|
||||
|
||||
|
||||
:- info([
|
||||
authors is 'Paulo Moura',
|
||||
version is 1.0,
|
||||
date is 2000/4/22,
|
||||
comment is 'Parametric object for representing geometric ellipses.',
|
||||
parnames is ['RX', 'RY', 'Color'],
|
||||
source is 'Example adopted from the SICStus Objects documentation.']).
|
||||
|
||||
|
||||
:- uses(math_constants).
|
||||
|
||||
|
||||
:- public(color/1).
|
||||
|
||||
:- mode(color(?atom), zero_or_one).
|
||||
|
||||
:- info(color/1, [
|
||||
comment is 'Ellipse color.',
|
||||
argnames is ['Color']]).
|
||||
|
||||
|
||||
:- public(rx/1).
|
||||
|
||||
:- mode(rx(?number), zero_or_one).
|
||||
|
||||
:- info(rx/1, [
|
||||
comment is 'Ellipse x axis.',
|
||||
argnames is ['Rx']]).
|
||||
|
||||
|
||||
:- public(ry/1).
|
||||
|
||||
:- mode(ry(?number), zero_or_one).
|
||||
|
||||
:- info(ry/1, [
|
||||
comment is 'Ellipse y axis.',
|
||||
argnames is ['Ry']]).
|
||||
|
||||
|
||||
:- public(area/1).
|
||||
|
||||
:- mode(area(-number), one).
|
||||
|
||||
:- info(area/1, [
|
||||
comment is 'Ellipse area.',
|
||||
argnames is ['Area']]).
|
||||
|
||||
|
||||
:- public(context/0).
|
||||
|
||||
:- mode(context, one).
|
||||
|
||||
:- info(context/0, [
|
||||
comment is 'Shows execution context (self, this and sender values).']).
|
||||
|
||||
|
||||
color(Color) :-
|
||||
parameter(3, Color).
|
||||
|
||||
|
||||
rx(Rx) :-
|
||||
parameter(1, Rx).
|
||||
|
||||
|
||||
ry(Ry) :-
|
||||
parameter(2, Ry).
|
||||
|
||||
|
||||
area(Area) :-
|
||||
::rx(Rx),
|
||||
::ry(Ry),
|
||||
math_constants::pi(Pi),
|
||||
Area is Rx*Ry*Pi.
|
||||
|
||||
|
||||
context :-
|
||||
write(ellipse3), nl,
|
||||
self(Self), write('self: '), writeq(Self), nl,
|
||||
this(This), write('this: '), writeq(This), nl,
|
||||
sender(Sender), write('sender: '), writeq(Sender), nl, nl.
|
||||
|
||||
|
||||
:- end_object.
|
26
Logtalk/examples/sicstus/math_constants.lgt
Normal file
26
Logtalk/examples/sicstus/math_constants.lgt
Normal file
@@ -0,0 +1,26 @@
|
||||
|
||||
:- object(math_constants).
|
||||
|
||||
|
||||
:- info([
|
||||
authors is 'Paulo Moura',
|
||||
version is 1.0,
|
||||
date is 2000/4/22,
|
||||
comment is 'Implements predicates for retriving common mathematical constants.']).
|
||||
|
||||
|
||||
:- public(pi/1).
|
||||
:- mode(pi(-float), one).
|
||||
|
||||
:- public(e/1).
|
||||
:- mode(e(-float), one).
|
||||
|
||||
|
||||
pi(Pi) :-
|
||||
Pi is 4.0*atan(1.0).
|
||||
|
||||
e(E) :-
|
||||
E is exp(1.0).
|
||||
|
||||
|
||||
:- end_object.
|
37
Logtalk/examples/sicstus/rational.lgt
Normal file
37
Logtalk/examples/sicstus/rational.lgt
Normal file
@@ -0,0 +1,37 @@
|
||||
|
||||
:- object(rational,
|
||||
implements(comparingp)).
|
||||
|
||||
|
||||
:- info([
|
||||
authors is 'Paulo Moura',
|
||||
version is 1.0,
|
||||
date is 2000/4/22,
|
||||
comment is 'Implements comparison between rational numbers represented as Num/Den.']).
|
||||
|
||||
|
||||
N1/D1 < N2/D2 :-
|
||||
{N1*D2 < N2*D1}.
|
||||
|
||||
|
||||
N1/D1 =< N2/D2 :-
|
||||
{N1*D2 =< N2*D1}.
|
||||
|
||||
|
||||
N1/D1 > N2/D2 :-
|
||||
{N1*D2 > N2*D1}.
|
||||
|
||||
|
||||
N1/D1 >= N2/D2 :-
|
||||
{N1*D2 >= N2*D1}.
|
||||
|
||||
|
||||
N1/D1 =:= N2/D2 :-
|
||||
{N1*D2 =:= N2*D1}.
|
||||
|
||||
|
||||
N1/D1 =\= N2/D2 :-
|
||||
{N1*D2 =\= N2*D1}.
|
||||
|
||||
|
||||
:- end_object.
|
54
Logtalk/examples/sicstus/rectangle2.lgt
Normal file
54
Logtalk/examples/sicstus/rectangle2.lgt
Normal file
@@ -0,0 +1,54 @@
|
||||
|
||||
:- object(rectangle(_Width, _Height)).
|
||||
|
||||
|
||||
:- info([
|
||||
authors is 'Paulo Moura',
|
||||
version is 1.0,
|
||||
date is 2000/4/22,
|
||||
comment is 'Parametric object for representing geometric rectangles.',
|
||||
parnames is ['Width', 'Height']]).
|
||||
|
||||
|
||||
:- public(width/1).
|
||||
|
||||
:- mode(width(?number), zero_or_one).
|
||||
|
||||
:- info(width/1, [
|
||||
comment is 'Rectangle width.',
|
||||
argnames is ['Width']]).
|
||||
|
||||
|
||||
:- public(height/1).
|
||||
|
||||
:- mode(height(?number), zero_or_one).
|
||||
|
||||
:- info(height/1, [
|
||||
comment is 'Rectangle height.',
|
||||
argnames is ['Height']]).
|
||||
|
||||
|
||||
:- public(area/1).
|
||||
|
||||
:- mode(area(-number), one).
|
||||
|
||||
:- info(area/1, [
|
||||
comment is 'Rectangle area.',
|
||||
argnames is ['Area']]).
|
||||
|
||||
|
||||
width(Width) :-
|
||||
parameter(1, Width).
|
||||
|
||||
|
||||
height(Height) :-
|
||||
parameter(2, Height).
|
||||
|
||||
|
||||
area(Area) :-
|
||||
::width(Width),
|
||||
::height(Height),
|
||||
Area is Width*Height.
|
||||
|
||||
|
||||
:- end_object.
|
23
Logtalk/examples/sicstus/red_circle1.lgt
Normal file
23
Logtalk/examples/sicstus/red_circle1.lgt
Normal file
@@ -0,0 +1,23 @@
|
||||
|
||||
:- object(red_circle(Radius),
|
||||
extends(circle(Radius, red))).
|
||||
|
||||
|
||||
:- info([
|
||||
authors is 'Paulo Moura',
|
||||
version is 1.0,
|
||||
date is 2000/4/22,
|
||||
comment is 'Parametric object for representing geometric red circles.',
|
||||
parnames is ['Radius'],
|
||||
source is 'Example adopted from the SICStus Objects documentation.']).
|
||||
|
||||
|
||||
context :-
|
||||
write(red_circle1), nl,
|
||||
self(Self), write('self: '), writeq(Self), nl,
|
||||
this(This), write('this: '), writeq(This), nl,
|
||||
sender(Sender), write('sender: '), writeq(Sender), nl, nl,
|
||||
^^context.
|
||||
|
||||
|
||||
:- end_object.
|
13
Logtalk/examples/sicstus/sicstus.loader
Normal file
13
Logtalk/examples/sicstus/sicstus.loader
Normal file
@@ -0,0 +1,13 @@
|
||||
|
||||
:- initialization(
|
||||
logtalk_load([
|
||||
circle11,
|
||||
circle2,
|
||||
colours,
|
||||
math_constants,
|
||||
ellipse3,
|
||||
rational,
|
||||
rectangle2,
|
||||
red_circle1,
|
||||
sort1,
|
||||
square1])).
|
57
Logtalk/examples/sicstus/sort1.lgt
Normal file
57
Logtalk/examples/sicstus/sort1.lgt
Normal file
@@ -0,0 +1,57 @@
|
||||
|
||||
:- object(sort(_Type)).
|
||||
|
||||
|
||||
:- info([
|
||||
authors is 'Paulo Moura',
|
||||
version is 1.0,
|
||||
date is 2000/4/22,
|
||||
comment is 'List sorting parameterized by the type of the list elements.',
|
||||
parnames is ['Type'],
|
||||
source is 'Example adopted from the SICStus Objects documentation.']).
|
||||
|
||||
|
||||
:- uses(list).
|
||||
|
||||
:- calls(comparingp).
|
||||
|
||||
|
||||
:- public(sort/2).
|
||||
|
||||
:- mode(sort(+list, -list), one).
|
||||
|
||||
:- info(sort/2, [
|
||||
comment is 'Sorts a list in ascending order.',
|
||||
argnames is ['List', 'Sorted']]).
|
||||
|
||||
|
||||
:- private(partition/4).
|
||||
|
||||
:- mode(partition(+list, +nonvar, -list, -list), one).
|
||||
|
||||
:- info(partition/4, [
|
||||
comment is 'List partition in two sub-lists using a pivot.',
|
||||
argnames is ['List', 'Pivot', 'Lowers', 'Biggers']]).
|
||||
|
||||
|
||||
sort([], []).
|
||||
|
||||
sort([P| L], S) :-
|
||||
partition(L, P, Small, Large),
|
||||
sort(Small, S0),
|
||||
sort(Large, S1),
|
||||
list::append(S0, [P| S1], S).
|
||||
|
||||
|
||||
partition([], _, [], []).
|
||||
|
||||
partition([X| L1], P, Small, Large) :-
|
||||
parameter(1, Type),
|
||||
( Type::(X < P) ->
|
||||
Small = [X| Small1], Large = Large1
|
||||
; Small = Small1, Large = [X| Large1]
|
||||
),
|
||||
partition(L1, P, Small1, Large1).
|
||||
|
||||
|
||||
:- end_object.
|
35
Logtalk/examples/sicstus/square1.lgt
Normal file
35
Logtalk/examples/sicstus/square1.lgt
Normal file
@@ -0,0 +1,35 @@
|
||||
|
||||
:- object(square(Side),
|
||||
extends(rectangle(Side, Side))).
|
||||
|
||||
|
||||
:- info([
|
||||
authors is 'Paulo Moura',
|
||||
version is 1.0,
|
||||
date is 2000/4/22,
|
||||
comment is 'Parametric object for representing geometric squares.',
|
||||
parnames is ['Side']]).
|
||||
|
||||
|
||||
:- public(side/1).
|
||||
|
||||
:- mode(side(?number), zero_or_one).
|
||||
|
||||
:- info(side/1, [
|
||||
comment is 'Square side.',
|
||||
argnames is ['Side']]).
|
||||
|
||||
|
||||
side(Side) :-
|
||||
parameter(1, Side).
|
||||
|
||||
|
||||
width(Width) :-
|
||||
parameter(1, Width).
|
||||
|
||||
|
||||
height(Height) :-
|
||||
parameter(1, Height).
|
||||
|
||||
|
||||
:- end_object.
|
Reference in New Issue
Block a user