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:
vsc
2001-06-06 19:40:57 +00:00
parent 38247e38fc
commit cc4531cd1e
344 changed files with 27125 additions and 0 deletions

View File

@@ -0,0 +1,64 @@
=================================================================
Logtalk - Object oriented extension to Prolog
Release 2.8.4
Copyright (c) 1998-2001 Paulo Moura. All Rights Reserved.
=================================================================
To load all objects in this example consult the birds.loader utility
file.
You will also need to also load the library/hierarchies.loader file.
Note that the *.loader files are Prolog files.
This folder contains an example of a bird identification expert system
adopted with permission from the book "Adventure in Prolog" by Amzi! inc.
The book is available on-line in HTML format at the URL:
http://www.amzi.com
Please refer to the book for more information on the original example.
The bird identification hierarchy is organized as a prototype hierarchy
as follows:
<order>
<family>
<bird>
order
falconiforms
falcon
peregrine_falcon
sparrow_hawk
vulture
california_condor
turkey_vulture
passerformes
flycatcher
ash_throated_flycatcher
great_crested_flycatcher
swallow
barn_swallow
cliff_swallow
purple_martin
tubenose
fulmar
albatross
black_footed_albatross
laysan_albatross
waterfowl
duck
female_mallard
male_mallard
pintail
goose
canada_goose
snow_goose
swan
trumpeter_swan
whistling_swan

View File

@@ -0,0 +1,72 @@
=================================================================
Logtalk - Object oriented extension to Prolog
Release 2.8.4
Copyright (c) 1998-2001 Paulo Moura. All Rights Reserved.
=================================================================
% ask the expert system for help in identifying a bird:
| ?- expert::identify.
Bird identification expert system
bill:sharp_hooked? (yes or no): yes.
eats:birds? (yes or no): yes.
feet:curved_talons? (yes or no): yes.
head:large? (yes or no): yes.
What is the value for tail?
1 : narrow_at_tip
2 : forked
3 : long_rusty
4 : square
5 : other
Enter the number of choice> 1.
wings:long_pointed? (yes or no): yes.
Possible identification : peregrine_falcon
No (more) candidates found.
(16379 ms) yes
% identify another bird:
| ?- expert::identify.
Bird identification expert system
bill:sharp_hooked? (yes or no): no.
bill:flat? (yes or no): no.
bill:short? (yes or no): no.
bill:hooked? (yes or no): yes.
What is the value for flight?
1 : ponderous
2 : powerful
3 : agile
4 : flap_glide
5 : other
Enter the number of choice> 2.
color:dark? (yes or no): yes.
live:at_sea? (yes or no): yes.
nostrils:external_tubular? (yes or no): yes.
What is the value for size?
1 : large
2 : plump
3 : medium
4 : small
Enter the number of choice> 1.
wings:long_narrow? (yes or no): yes.
Possible identification : black_footed_albatross
No (more) candidates found.
(34624 ms) yes
| ?-

View File

@@ -0,0 +1,14 @@
:- object(albatross,
imports(descriptors),
extends(tubenose)).
family(albatross).
size(large).
wings(long_narrow).
:- end_object.

View File

@@ -0,0 +1,10 @@
:- object(ash_throated_flycatcher,
imports(descriptors),
extends(flycatcher)).
throat(white).
:- end_object.

View File

@@ -0,0 +1,10 @@
:- object(barn_swallow,
imports(descriptors),
extends(swallow)).
tail(forked).
:- end_object.

View File

@@ -0,0 +1,41 @@
:- initialization(
logtalk_load([
descriptors,
expert,
order,
falconiforms, % orders
passerformes,
tubenose,
waterfowl,
albatross, % families
duck,
falcon,
flycatcher,
goose,
swallow,
swan,
vulture,
ash_throated_flycatcher, % birds
barn_swallow,
black_footed_albatross,
california_condor,
canada_goose,
cliff_swallow,
female_mallard,
fulmar,
great_crested_flycatcher,
laysan_albatross,
male_mallard,
peregrine_falcon,
pintail,
purple_martin,
snow_goose,
sparrow_hawk,
trumpeter_swan,
turkey_vulture,
whistling_swan])).

View File

@@ -0,0 +1,10 @@
:- object(black_footed_albatross,
imports(descriptors),
extends(albatross)).
color(dark).
:- end_object.

View File

@@ -0,0 +1,10 @@
:- object(california_condor,
imports(descriptors),
extends(vulture)).
flight_profile(flat).
:- end_object.

View File

@@ -0,0 +1,12 @@
:- object(canada_goose,
imports(descriptors),
extends(goose)).
head(black).
cheek(white).
:- end_object.

View File

@@ -0,0 +1,10 @@
:- object(cliff_swallow,
imports(descriptors),
extends(swallow)).
tail(square).
:- end_object.

View File

@@ -0,0 +1,56 @@
:- category(descriptors).
:- info([
authors is 'Paulo Moura',
version is 1.0,
date is 2000/2/18,
comment is 'Bird descriptors predicates.',
source is 'Example adopted from an Amzi! Inc Prolog book.']).
:- public([
bill/1,
cheek/1,
color/1,
eats/1,
family/1,
feed/1,
feet/1,
flight/1,
flight_profile/1,
head/1,
live/1,
neck/1,
nostrils/1,
order/1,
size/1,
tail/1,
throat/1,
voice/1,
wings/1]).
:- public(descriptor/1).
descriptor(bill/1).
descriptor(cheek/1).
descriptor(color/1).
descriptor(eats/1).
descriptor(feed/1).
descriptor(feet/1).
descriptor(flight/1).
descriptor(flight_profile/1).
descriptor(head/1).
descriptor(live/1).
descriptor(neck/1).
descriptor(nostrils/1).
descriptor(size/1).
descriptor(tail/1).
descriptor(throat/1).
descriptor(voice/1).
descriptor(wings/1).
:- end_category.

View File

@@ -0,0 +1,14 @@
:- object(duck,
imports(descriptors),
extends(waterfowl)).
family(duck).
feed(on_water_surface).
flight(agile).
:- end_object.

View File

@@ -0,0 +1,166 @@
:- object(expert,
imports(protected::descriptors)).
:- info([
authors is 'Paulo Moura',
version is 1.0,
date is 2000/2/18,
comment is 'Expert system for bird identification.',
source is 'Example adopted from an Amzi! Inc Prolog book.']).
:- public(identify/0).
:- mode(identify, one).
:- info(identify/0,
[comment is 'Starts a bird identification session.']).
:- private(known_/3).
:- dynamic(known_/3).
:- mode(known_(?nonvar, ?nonvar, ?nonvar), zero_or_more).
:- info(known_/3, [
comment is 'Table of already known facts.',
argnames is ['Answer', 'Attribute', 'Value']]).
identify :-
::retractall(known_(_, _, _)),
write('Bird identification expert system'), nl, nl,
forall(
(order::leaf(Bird), check(Bird)),
(nl, write('Possible identification : '), write(Bird), nl)),
nl, write('No (more) candidates found.').
check(Bird) :-
forall(
(::descriptor(Functor/Arity),
functor(Predicate, Functor, Arity),
Bird::Predicate),
call(Predicate)).
bill(X):-
ask(bill, X).
cheek(X):-
ask(cheek, X).
color(X):-
ask(color, X).
eats(X):-
ask(eats, X).
feed(X):-
ask(feed,X).
feet(X):-
ask(feet, X).
flight(X):-
menuask(flight, X, [ponderous, powerful, agile, flap_glide, other]).
flight_profile(X):-
menuask(flight_profile, X, [flat, v_shaped, other]).
head(X):-
ask(head,X).
live(X) :-
ask(live, X).
neck(X):-
ask(neck, X).
nostrils(X):-
ask(nostrils, X).
size(X):-
menuask(size, X, [large, plump, medium, small]).
tail(X):-
menuask(tail, X, [narrow_at_tip, forked, long_rusty, square, other]).
throat(X):-
ask(throat, X).
voice(X):-
ask(voice,X).
wings(X):-
ask(wings, X).
ask(Attribute,Value):-
::known_(yes, Attribute, Value),
!.
ask(Attribute,Value):-
::known_(_, Attribute, Value),
!, fail.
ask(Attribute,_):-
::known_(yes, Attribute, _),
!, fail.
ask(Attribute, Value):-
write(Attribute:Value),
write('? (yes or no): '),
read(Answer),
::asserta(known_(Answer, Attribute, Value)),
Answer = yes.
menuask(Attribute,Value, _):-
::known_(yes, Attribute, Value),
!.
menuask(Attribute, _, _):-
::known_(yes, Attribute, _),
!, fail.
menuask(Attribute, AskValue, Menu):-
nl, write('What is the value for '), write(Attribute), write('?'), nl,
display_menu(Menu),
write('Enter the number of choice> '),
read(Num),nl,
pick_menu(Num, AnswerValue, Menu),
::asserta(known_(yes,Attribute,AnswerValue)),
AskValue = AnswerValue.
display_menu(Menu):-
display_menu(Menu, 1).
display_menu([], _).
display_menu([Item| Rest], N):-
write(N), write(' : '), write(Item), nl,
NN is N + 1,
display_menu(Rest, NN).
pick_menu(N, Val, Menu):-
integer(N),
pic_menu(1, N, Val, Menu), !.
pick_menu(Val, Val, _).
pic_menu(_, _, none_of_the_above, []).
pic_menu(N, N, Item, [Item| _]).
pic_menu(Ctr, N, Val, [_| Rest]):-
NextCtr is Ctr + 1,
pic_menu(NextCtr, N, Val, Rest).
:- end_object.

View File

@@ -0,0 +1,16 @@
:- object(falcon,
imports(descriptors),
extends(falconiforms)).
family(falcon).
wings(long_pointed).
head(large).
tail(narrow_at_tip).
:- end_object.

View File

@@ -0,0 +1,16 @@
:- object(falconiforms,
imports(descriptors),
extends(order)).
order(falconiforms).
eats(meat).
feet(curved_talons).
bill(sharp_hooked).
:- end_object.

View File

@@ -0,0 +1,12 @@
:- object(female_mallard,
imports(descriptors),
extends(duck)).
voice(quack).
color(mottled_brown).
:- end_object.

View File

@@ -0,0 +1,14 @@
:- object(flycatcher,
imports(descriptors),
extends(passerformes)).
family(flycatcher).
bill(flat).
eats(flying_insects).
:- end_object.

View File

@@ -0,0 +1,12 @@
:- object(fulmar,
imports(descriptors),
extends(tubenose)).
size(medium).
flight(flap_glide).
:- end_object.

View File

@@ -0,0 +1,14 @@
:- object(goose,
imports(descriptors),
extends(waterfowl)).
family(goose).
size(plump).
flight(powerful).
:- end_object.

View File

@@ -0,0 +1,10 @@
:- object(great_crested_flycatcher,
imports(descriptors),
extends(flycatcher)).
tail(long_rusty).
:- end_object.

View File

@@ -0,0 +1,11 @@
:- object(laysan_albatross,
imports(descriptors),
extends(albatross)).
color(white).
:- end_object.

View File

@@ -0,0 +1,12 @@
:- object(male_mallard,
imports(descriptors),
extends(duck)).
voice(quack).
head(green).
:- end_object.

View File

@@ -0,0 +1,6 @@
:- object(order,
imports(descriptors, proto_hierarchy)).
:- end_object.

View File

@@ -0,0 +1,12 @@
:- object(passerformes,
imports(descriptors),
extends(order)).
order(passerformes).
feet(one_long_backward_toe).
:- end_object.

View File

@@ -0,0 +1,10 @@
:- object(peregrine_falcon,
imports(descriptors),
extends(falcon)).
eats(birds).
:- end_object.

View File

@@ -0,0 +1,10 @@
:- object(pintail,
imports(descriptors),
extends(duck)).
voice(short_whistle).
:- end_object.

View File

@@ -0,0 +1,10 @@
:- object(purple_martin,
imports(descriptors),
extends(swallow)).
color(dark).
:- end_object.

View File

@@ -0,0 +1,10 @@
:- object(snow_goose,
imports(descriptors),
extends(goose)).
color(white).
:- end_object.

View File

@@ -0,0 +1,10 @@
:- object(sparrow_hawk,
imports(descriptors),
extends(falcon)).
eats(insects).
:- end_object.

View File

@@ -0,0 +1,16 @@
:- object(swallow,
imports(descriptors),
extends(passerformes)).
family(swallow).
wings(long_pointed).
tail(forked).
bill(short).
:- end_object.

View File

@@ -0,0 +1,16 @@
:- object(swan,
imports(descriptors),
extends(waterfowl)).
family(swan).
neck(long).
color(white).
flight(ponderous).
:- end_object.

View File

@@ -0,0 +1,10 @@
:- object(trumpeter_swan,
imports(descriptors),
extends(swan)).
voice(loud_trumpeting).
:- end_object.

View File

@@ -0,0 +1,16 @@
:- object(tubenose,
imports(descriptors),
extends(order)).
order(tubenose).
nostrils(external_tubular).
live(at_sea).
bill(hooked).
:- end_object.

View File

@@ -0,0 +1,10 @@
:- object(turkey_vulture,
imports(descriptors),
extends(vulture)).
flight_profile(v_shaped).
:- end_object.

View File

@@ -0,0 +1,14 @@
:- object(vulture,
imports(descriptors),
extends(falconiforms)).
family(vulture).
feed(scavange).
wings(broad).
:- end_object.

View File

@@ -0,0 +1,14 @@
:- object(waterfowl,
imports(descriptors),
extends(order)).
order(waterfowl).
feet(webbed).
bill(flat).
:- end_object.

View File

@@ -0,0 +1,10 @@
:- object(whistling_swan,
imports(descriptors),
extends(swan)).
voice(muffled_musical_whistle).
:- end_object.

View File

@@ -0,0 +1,26 @@
=================================================================
Logtalk - Object oriented extension to Prolog
Release 2.8.4
Copyright (c) 1998-2001 Paulo Moura. All Rights Reserved.
=================================================================
To load all objects in this example consult the bricks.loader utility
file (note that the *.loader files are Prolog files).
You will need to load the objects in the roots and relations
examples (consulting the corresponding roots.loader and relations.loader
files).
You will also need to consult the following files in the library directory:
events.loader, types.loader, metapredicates.loader, and hierarchies.loader.
This folder contains an example of representation and handling of
relations using events. We have instances of class brick and a binary
brick_stack relation between the bricks. Every time we move a brick, we
want the bricks on top of it to move along. If we break the stack by
moving a middle brick, we want to automatically destroy the
corresponding relation tuple.
It's instructive to use the debugger to better understand this example.
Set spy points in all brick instances and then activate the debugger.

View File

@@ -0,0 +1,225 @@
=================================================================
Logtalk - Object oriented extension to Prolog
Release 2.8.4
Copyright (c) 1998-2001 Paulo Moura. All Rights Reserved.
=================================================================
% create four bricks, all standing on the "ground" (use your imagination... ;-)
| ?- brick::new(a, [position-(8, 1)]).
yes
| ?- brick::new(b, [position-(6, 1)]).
yes
| ?- brick::new(c, [position-(4, 1)]).
yes
| ?- brick::new(d, [position-(2, 1)]).
yes
% set up ascii stack monitor so we can watch the bricks moving
| ?- after_event_registry::set_monitor(_, move(_,_), _, stack_monitor).
yes
% make the stack
| ?- brick_stack::add_tuple([c,d]).
|.c......
|.d...b.a
---------
yes
| ?- brick_stack::add_tuple([b,c]).
|.b......
|.c......
|.d.....a
---------
yes
| ?- brick_stack::add_tuple([a,b]).
|.a
|.b
|.c
|.d
---
yes
% check results
| ?- brick_stack::tuple(Tuple), write(Tuple), nl, fail.
[c,d]
[b,c]
[a,b]
no
| ?- before_event_registry::monitors(Mb), after_event_registry::monitors(Ma).
Ma = [brick_stack, stack_monitor]
Mb = [brick_stack]
yes
% move all stack to new position by moving bottom brick; check results
| ?- d::move(9, 1).
|.a.......
|.b.......
|.c.......
|........d
----------
|.a.......
|.b.......
|........c
|........d
----------
|.a.......
|........b
|........c
|........d
----------
|........a
|........b
|........c
|........d
----------
yes
| ?- a::position(Xa, Ya), b::position(Xb, Yb), c::position(Xc, Yc), d::position(Xd, Yd).
Xa = 9,
Xb = 9,
Xc = 9,
Xd = 9,
Ya = 4,
Yb = 3,
Yc = 2,
Yd = 1
yes
| ?- brick_stack::tuple(Tuple), write(Tuple), nl, fail.
[c,d]
[b,c]
[a,b]
no
% break stack in half by moving b to the "ground"; check results
| ?- b::move(3, 1).
|........a
|.........
|........c
|..b.....d
----------
|..a.....c
|..b.....d
----------
yes
| ?- a::position(Xa, Ya), b::position(Xb, Yb), c::position(Xc, Yc), d::position(Xd, Yd).
Xa = 3,
Xb = 3,
Xc = 9,
Xd = 9,
Ya = 2,
Yb = 1,
Yc = 2,
Yd = 1
yes
| ?- brick_stack::tuple(Tuple), write(Tuple), nl, fail.
[c,d]
[a,b]
no
% create new brick_stack tuple ; check results
| ?- brick_stack::add_tuple([d, a]).
|..d......
|..a.....c
|..b......
----------
|..c
|..d
|..a
|..b
----
yes
| ?- a::position(Xa, Ya), b::position(Xb, Yb), c::position(Xc, Yc), d::position(Xd, Yd).
Xa = 3,
Xb = 3,
Xc = 3,
Xd = 3,
Ya = 2,
Yb = 1,
Yc = 4,
Yd = 3
yes
| ?- brick_stack::tuple(Tuple), write(Tuple), nl, fail.
[c,d]
[a,b]
[d,a]
no
% move all stack to new position by moving bottom brick; check results
| ?- b::move(5, 1).
|..c..
|..d..
|..a..
|....b
------
|..c..
|..d..
|....a
|....b
------
|..c..
|....d
|....a
|....b
------
|....c
|....d
|....a
|....b
------
yes
| ?- a::position(Xa, Ya), b::position(Xb, Yb), c::position(Xc, Yc), d::position(Xd, Yd).
Xa = 5,
Xb = 5,
Xc = 5,
Xd = 5,
Ya = 2,
Yb = 1,
Yc = 4,
Yd = 3
yes
| ?- brick_stack::tuple(Tuple), write(Tuple), nl, fail.
[c,d]
[a,b]
[d,a]
no
% clean up instances, tuples and monitors
| ?- brick_stack::remove_all_tuples.
yes
| ?- after_event_registry::del_monitors(_, _, _, stack_monitor).
yes
| ?- brick::delete_all.
yes

View File

@@ -0,0 +1,76 @@
:- object(brick,
instantiates(class),
specializes(object)).
:- info([
version is 1.1,
date is 2000/10/31,
authors is 'Paulo Moura',
comment is 'Two-dimensional brick (or should I say square?) class.']).
:- public(position/2).
:- mode(position(?integer, ?integer), zero_or_one).
:- info(position/2, [
comment is 'Brick current position.',
argnames is ['X', 'Y']]).
:- private(position_/2).
:- dynamic(position_/2).
:- mode(position_(?integer, ?integer), zero_or_one).
:- info(position_/2, [
comment is 'Stores brick current position.',
argnames is ['X', 'Y']]).
:- public(move/2).
:- mode(move(+integer, +integer), one).
:- info(move/2, [
comment is 'Moves a brick to a new position.',
argnames is ['X', 'Y']]).
position(X, Y) :-
::position_(X, Y).
move(X, Y) :-
::retractall(position_(_, _)),
::assertz(position_(X, Y)).
default_init_option(position-(0, 0)).
default_init_option(Default) :-
^^default_init_option(Default).
process_init_option(position-(X, Y)) :-
::assertz(position_(X, Y)).
process_init_option(Option) :-
^^process_init_option(Option).
valid_init_option(position-(X, Y)) :-
!,
integer(X),
integer(Y).
valid_init_option(Option) :-
^^valid_init_option(Option).
instance_base_name(b).
:- end_object.

View File

@@ -0,0 +1,59 @@
:- object(brick_stack,
instantiates(constrained_relation)).
:- info([
version is 1.0,
date is 1998/3/23,
authors is 'Paulo Moura',
comment is 'Stack of bricks as a constrained binary relation.']).
descriptor_([top, bottom]).
domain_(top, brick).
domain_(bottom, brick).
key_([top, bottom]).
cardinality_(top, 0, 1).
cardinality_(bottom, 0, 1).
delete_option_(top, cascade).
delete_option_(bottom, restrict).
add_tuple([A, B]) :-
B::position(Xb, Yb),
Ya2 is Yb + 1,
{A::move(Xb, Ya2)},
^^add_tuple([A, B]).
activ_points_(top, before, []).
activ_points_(top, after, [move(_, _)]).
activ_points_(bottom, before, []).
activ_points_(bottom, after, [move(_, _)]).
propagate(after, move(X, Y), Top, top, [Top, Bottom]) :-
!,
Y2 is Y - 1,
(Bottom::position(X, Y2) ->
true
;
::remove_tuple([Top, Bottom])).
propagate(after, move(X, Y), Bottom, bottom, [Top, Bottom]) :-
!,
Y2 is Y + 1,
{Top::move(X, Y2)}.
:- end_object.

View File

@@ -0,0 +1,6 @@
:- initialization(
logtalk_load([
brick,
brick_stack,
stack_monitor])).

View File

@@ -0,0 +1,38 @@
:- object(stack_monitor,
implements(event_handlersp)).
:- info([
version is 1.0,
date is 1998/3/23,
authors is 'Paulo Moura',
comment is 'Monitor for brick movements printing an ascii representation of each brick position.']).
:- uses(loop).
:- uses(list).
after(_, move(_, _), _) :-
findall(
(Brick, X, Y),
(instantiates_class(Brick, brick), Brick::position(X, Y)),
Bricks),
setof(X, Brick^Y^ (list::member((Brick,X,Y), Bricks)), Xs),
list::last(Xs, Xmax),
setof(Y, Brick^X^ (list::member((Brick,X,Y), Bricks)), Ys),
list::last(Ys, Ymax),
loop::fordownto(Y, Ymax, 1,
(write('|'),
loop::forto(X, 1, Xmax,
(list::member((Brick, X, Y), Bricks) ->
write(Brick)
;
write('.'))),
nl)),
write('-'),
loop::forto(X, 1, Xmax, write('-')), nl.
:- end_object.

View File

@@ -0,0 +1,15 @@
=================================================================
Logtalk - Object oriented extension to Prolog
Release 2.8.4
Copyright (c) 1998-2001 Paulo Moura. All Rights Reserved.
=================================================================
To load all objects in this example consult the classvars.loader utility
file (note that the *.loader files are Prolog files).
This folder contains an example that shows how to implement class variables
as defined in Smalltalk. The name shared instance variables is however much
more accurate. In systems like Logtalk that enable the use of explicit
metaclasses, true class variables are just the class (as an object) own
instance variables!

View File

@@ -0,0 +1,32 @@
=================================================================
Logtalk - Object oriented extension to Prolog
Release 2.8.4
Copyright (c) 1998-2001 Paulo Moura. All Rights Reserved.
=================================================================
% get the value of the class variable for each instance:
| ?- instance1::cv(Value1), instance2::cv(Value2), instance3::cv(Value3).
Value1 = 0
Value2 = 0
Value3 = 0
yes
% change the value of the class variable via instance1:
| ?- instance1::set_cv(1).
yes
% get the value of the class variable for the other instances:
| ?- instance2::cv(Value2), instance3::cv(Value3).
Value2 = 1
Value3 = 1
yes

View File

@@ -0,0 +1,7 @@
:- initialization(
logtalk_load([
root,
instance1,
instance2,
instance3])).

View File

@@ -0,0 +1,6 @@
:- object(instance1,
instantiates(root)).
:- end_object.

View File

@@ -0,0 +1,6 @@
:- object(instance2,
instantiates(root)).
:- end_object.

View File

@@ -0,0 +1,6 @@
:- object(instance3,
instantiates(root)).
:- end_object.

View File

@@ -0,0 +1,29 @@
:- object(root,
instantiates(root)).
:- private(cv_/1).
:- dynamic(cv_/1).
:- mode(cv_(?integer), zero_or_one).
:- public(cv/1).
:- mode(cv(?integer), zero_or_one).
:- public(set_cv/1).
:- mode(set_cv(+integer), one).
cv_(0). % cv value is stored locally, in this class
cv(Value) :-
cv_(Value). % retrive cv value, shared for all instances
set_cv(Value) :-
retractall(cv_(_)), % retract old cv value from this class
asserta(cv_(Value)). % assert the new value in this class
:- end_object.

View File

@@ -0,0 +1,16 @@
=================================================================
Logtalk - Object oriented extension to Prolog
Release 2.8.4
Copyright (c) 1998-2001 Paulo Moura. All Rights Reserved.
=================================================================
To load all objects in this example consult the inheritance.loader utility
file (note that the *.loader files are Prolog files).
This folder contains examples of public, protected and private inheritance,
for both prototypes and classes/instances. The category "predicates" defines
a set of three predicates, one public, one protected and one private. This
category is imported by the root objects: "parent" for the prototypes and
"object" for the classes/instances. Each root object have a set of three
descendants, each one using one of the inheritance types.

View File

@@ -0,0 +1,98 @@
=================================================================
Logtalk - Object oriented extension to Prolog
Release 2.8.4
Copyright (c) 1998-2001 Paulo Moura. All Rights Reserved.
=================================================================
% parent interface
| ?- parent::interface.
public/0 - public
protected/0 - protected
private/0 - private
interface/0 - public
yes
% prototype1 extends public::parent
| ?- prototype1::interface.
interface/0 - public
public/0 - public
protected/0 - protected
yes
% prototype2 extends protected::parent
| ?- prototype2::interface.
interface/0 - public
public/0 - protected
protected/0 - protected
yes
% prototype3 extends private::parent
| ?- prototype3::interface.
interface/0 - public
public/0 - private
protected/0 - private
yes
% object (root of the inheritance graph) interface
| ?- root::interface.
public/0 - public
protected/0 - protected
private/0 - private
interface/0 - public
yes
% instance1 instantiates subclass1 that specializes public::root
| ?- instance1::interface.
interface/0 - public
public/0 - public
protected/0 - protected
yes
% instance2 instantiates subclass2 that specializes protected::root
| ?- instance2::interface.
interface/0 - public
public/0 - protected
protected/0 - protected
yes
% instance3 instantiates subclass3 that specializes private::root
| ?- instance3::interface.
interface/0 - public
public/0 - private
protected/0 - private
yes

View File

@@ -0,0 +1,18 @@
:- initialization(
logtalk_load([
predicates,
interface,
parent,
prototype1,
prototype2,
prototype3,
root,
subclass1,
subclass2,
subclass3,
instance1,
instance2,
instance3])).

View File

@@ -0,0 +1,6 @@
:- object(instance1,
instantiates(subclass1)).
:- end_object.

View File

@@ -0,0 +1,6 @@
:- object(instance2,
instantiates(subclass2)).
:- end_object.

View File

@@ -0,0 +1,6 @@
:- object(instance3,
instantiates(subclass3)).
:- end_object.

View File

@@ -0,0 +1,22 @@
:- category(interface).
:- public(interface/0).
:- mode(interface, one).
interface :-
forall(
(::current_predicate(Functor/Arity),
functor(Pred, Functor, Arity)),
(::predicate_property(Pred, Prop), scope(Prop),
writeq(Functor/Arity), write(' - '), writeq(Prop), nl)).
scope(public).
scope(protected).
scope(private).
:- end_category.

View File

@@ -0,0 +1,6 @@
:- object(parent,
imports(predicates, interface)).
:- end_object.

View File

@@ -0,0 +1,25 @@
:- category(predicates).
:- public(public/0).
:- mode(public, one).
:- protected(protected/0).
:- mode(protected, one).
:- private(private/0).
:- mode(private, one).
public :-
write('Public predicate declared and defined in category predicates.'), nl.
protected :-
write('Protected predicate declared and defined in category predicates.'), nl.
private :-
write('Private predicate declared and defined in category predicates.'), nl.
:- end_category.

View File

@@ -0,0 +1,7 @@
:- object(prototype1,
imports(interface),
extends(public::parent)).
:- end_object.

View File

@@ -0,0 +1,7 @@
:- object(prototype2,
imports(interface),
extends(protected::parent)).
:- end_object.

View File

@@ -0,0 +1,7 @@
:- object(prototype3,
imports(interface),
extends(private::parent)).
:- end_object.

View File

@@ -0,0 +1,7 @@
:- object(root,
imports(predicates, interface),
instantiates(root)).
:- end_object.

View File

@@ -0,0 +1,7 @@
:- object(subclass1,
imports(interface),
specializes(public::root)).
:- end_object.

View File

@@ -0,0 +1,7 @@
:- object(subclass2,
imports(interface),
specializes(protected::root)).
:- end_object.

View File

@@ -0,0 +1,7 @@
:- object(subclass3,
imports(interface),
specializes(private::root)).
:- end_object.

View File

@@ -0,0 +1,14 @@
=================================================================
Logtalk - Object oriented extension to Prolog
Release 2.8.4
Copyright (c) 1998-2001 Paulo Moura. All Rights Reserved.
=================================================================
To load all objects in this example consult the instmethods.loader utility
file (note that the *.loader files are Prolog files).
This folder contains an example of instance defined methods. When using
classes and instances, methods must be declared in a class but the method
definitions may be stored in the instances, either overriding or specializing
the class definitions.

View File

@@ -0,0 +1,36 @@
=================================================================
Logtalk - Object oriented extension to Prolog
Release 2.8.4
Copyright (c) 1998-2001 Paulo Moura. All Rights Reserved.
=================================================================
% instance1 definition for method "method" is found in its class:
| ?- instance1::method.
This is the default definition for the method, stored in class root.
yes
% instance2 overrides definition of method "method"
| ?- instance2::method.
This is an overriding definition stored in the instance2 instance itself.
yes
% instance3 specializes definition of method "method"
| ?- instance3::method.
This is a specializing definition stored in the instance3 instance itself.
It makes a super call to execute the default definition:
This is the default definition for the method, stored in class root.
yes

View File

@@ -0,0 +1,6 @@
:- object(instance1,
instantiates(root)).
:- end_object.

View File

@@ -0,0 +1,13 @@
:- object(instance2,
instantiates(root)).
method :-
this(This),
write('This is an overriding definition stored in the '),
writeq(This),
write(' instance itself.'), nl.
:- end_object.

View File

@@ -0,0 +1,15 @@
:- object(instance3,
instantiates(root)).
method :-
this(This),
write('This is a specializing definition stored in the '),
writeq(This),
write(' instance itself.'), nl,
write('It makes a super call to execute the default definition:'), nl, nl,
^^method.
:- end_object.

View File

@@ -0,0 +1,7 @@
:- initialization(
logtalk_load([
root,
instance1,
instance2,
instance3])).

View File

@@ -0,0 +1,15 @@
:- object(root,
instantiates(root)).
:- public(method/0).
method :-
this(This),
write('This is the default definition for the method, stored in class '),
writeq(This), write('.'), nl.
:- end_object.

15
Logtalk/examples/lo/NOTES Normal file
View File

@@ -0,0 +1,15 @@
=================================================================
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 Logic&Objects system
by Francis G. McCabe. Detailed descriptions and the original source code
can be found on the book "Logic and Objects" by the author published by
Prentice Hall.
Note that because Logtalk lacks the functional notation of the
Logic&Objects system, my adaptation of the book examples may have
introduced some bugs not present in the original code.

View File

@@ -0,0 +1,14 @@
=================================================================
Logtalk - Object oriented extension to Prolog
Release 2.8.4
Copyright (c) 1998-2001 Paulo Moura. All Rights Reserved.
=================================================================
This example is an adaptation of a LPA Prolog++ example.
To load all objects in this example consult the lpa.loader utility
file.
You will also need to consult the library/hierarchies.loader file.
Note that the *.loader files are Prolog files.

View File

@@ -0,0 +1,21 @@
=================================================================
Logtalk - Object oriented extension to Prolog
Release 2.8.4
Copyright (c) 1998-2001 Paulo Moura. All Rights Reserved.
=================================================================
| ?- fault::findall.
Please answer all questions with yes or no.
The starter turns but the engine doesnt fire? no.
The engine has difficulty starting? yes.
The engine cuts out shortly after starting? yes.
Location : distributor
Possible Fault: Worn distributor brushes
No (more) explanations found.
yes.

View File

@@ -0,0 +1,6 @@
:- object(cylinders,
extends(engine)).
:- end_object.

View File

@@ -0,0 +1,25 @@
:- object(distributor,
extends(sparking)).
fault(f1001, 'Condensation in the distributor cap').
fault(f1002, 'Faulty distributor arm').
fault(f1003, 'Worn distributor brushes').
symptom(s1001, 'The starter turns but the engine doesnt fire').
symptom(s1002, 'The engine has difficulty starting').
symptom(s1003, 'The engine cuts out shortly after starting').
symptom(s1004, 'The engine cuts out at speed').
effect(f1001, s1001).
effect(f1002, s1001).
effect(f1002, s1004).
effect(f1003, s1002).
effect(f1003, s1003).
contrary(s1002, s1001).
contrary(s1003, s1001).
:- end_object.

View File

@@ -0,0 +1,6 @@
:- object(electrical,
extends(fault)).
:- end_object.

View File

@@ -0,0 +1,6 @@
:- object(engine,
extends(mechanical)).
:- end_object.

View File

@@ -0,0 +1,72 @@
:- object(fault,
imports(proto_hierarchy)).
:- info([
authors is 'Paulo Moura',
version is 1.0,
date is 2000/4/22,
comment is 'Expert system for automobile fault diagnosis.',
source is 'Example adopted from the LPA Prolog++ documentation.']).
:- public(findall/0).
:- mode(findall, one).
:- private(told_by_user_/2).
:- dynamic(told_by_user_/2).
:- mode(told_by_user_(?nonvar, ?nonvar), zero_or_more).
:- public(find/1).
:- mode(find(?nonvar), zero_or_more).
:- private(exhibited/1).
:- mode(exhibited(+nonvar), zero_or_one).
:- public(contrary/2).
:- mode(contrary(?nonvar, ?nonvar), zero_or_more).
:- public(fault/2).
:- mode(fault(?nonvar, ?nonvar), zero_or_more).
:- public(effect/2).
:- mode(effect(?nonvar, ?nonvar), zero_or_more).
:- public(symptom/2).
:- mode(symptom(?nonvar, ?nonvar), zero_or_more).
findall :-
retractall(told_by_user_(_, _)),
write('Please answer all questions with yes or no.'), nl, nl,
forall(
(::descendant(Where), Where::find(Description)),
(nl, write('Location : '), write(Where), nl,
write('Possible Fault: '), write(Description), nl)),
nl, write('No (more) explanations found.').
find(Description) :-
::fault(Fault, Description),
forall(::effect(Fault, Symptom), exhibited(Symptom)).
exhibited(Symptom) :-
told_by_user_(Symptom, Reply),
!,
Reply = yes.
exhibited(Symptom) :-
::symptom(Symptom, Description),
write(Description), write('? '),
read(Reply),
asserta(told_by_user_(Symptom, Reply)),
Reply = yes,
forall(
(::contrary(Symptom, Contrary);
::contrary(Contrary, Symptom)),
asserta(told_by_user_(Contrary, no))).
:- end_object.

View File

@@ -0,0 +1,6 @@
:- object(fuel_system,
extends(fault)).
:- end_object.

View File

@@ -0,0 +1,6 @@
:- object(lights,
extends(electrical)).
:- end_object.

View File

@@ -0,0 +1,15 @@
:- initialization(
logtalk_load([
cylinders,
distributor,
electrical,
engine,
fault,
fuel_system,
lights,
mechanical,
plugs,
sparking,
starter_motor,
starting])).

View File

@@ -0,0 +1,6 @@
:- object(mechanical,
extends(fault)).
:- end_object.

View File

@@ -0,0 +1,6 @@
:- object(plugs,
extends(sparking)).
:- end_object.

View File

@@ -0,0 +1,6 @@
:- object(sparking,
extends(starting)).
:- end_object.

View File

@@ -0,0 +1,6 @@
:- object(starter_motor,
extends(starting)).
:- end_object.

View File

@@ -0,0 +1,6 @@
:- object(starting,
extends(electrical)).
:- end_object.

View File

@@ -0,0 +1,16 @@
=================================================================
Logtalk - Object oriented extension to Prolog
Release 2.8.4
Copyright (c) 1998-2001 Paulo Moura. All Rights Reserved.
=================================================================
To load all objects in this library consult the metapredicates.loader utility
file.
You will also need to load the library/types.loader file (note that the
*.loader files are Prolog files).
This example shows the use of metapredicates in Logtalk. Metapredicates are
predicates whose head contains arguments that will be called as goals in the
body of the predicate definition.

View File

@@ -0,0 +1,48 @@
=================================================================
Logtalk - Object oriented extension to Prolog
Release 2.8.4
Copyright (c) 1998-2001 Paulo Moura. All Rights Reserved.
=================================================================
% note that "user" is a pseudo-object representing the Prolog database
% this implies that the integer comparisons are done using the standard
% Prolog built-in predicates
| ?- sort(user)::sort([3, 1, 4, 2, 9], Sorted).
call: partition([1,4,2,9],3,_358,_359)
exit: partition([1,4,2,9],3,[1,2],[4,9])
call: sort([1,2],_740)
call: partition([2],1,_967,_968)
exit: partition([2],1,[],[2])
call: sort([],_1300)
exit: sort([],[])
call: sort([2],_1539)
call: partition([],2,_1765,_1766)
exit: partition([],2,[],[])
call: sort([],_2093)
exit: sort([],[])
call: sort([],_2332)
exit: sort([],[])
exit: sort([2],[2])
exit: sort([1,2],[1,2])
call: sort([4,9],_2831)
call: partition([9],4,_3058,_3059)
exit: partition([9],4,[],[9])
call: sort([],_3391)
exit: sort([],[])
call: sort([9],_3630)
call: partition([],9,_3856,_3857)
exit: partition([],9,[],[])
call: sort([],_4184)
exit: sort([],[])
call: sort([],_4423)
exit: sort([],[])
exit: sort([9],[9])
exit: sort([4,9],[4,9])
Sorted = [1,2,3,4,9] ?
yes

View File

@@ -0,0 +1,5 @@
:- initialization(
logtalk_load([
sort1,
tracer])).

View File

@@ -0,0 +1,61 @@
% sort code adopted from an example in the SICStus Prolog User Manual
% metapredicate example taken from Prolog Part 2, Modules - Committee Draft
:- object(sort(_Type)).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'List sorting parameterized by the type of the list elements.']).
:- uses(list).
:- uses(tracer).
:- 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 'Partition a list in two lists containing the elements smaller and larger than a pivot.',
argnames is ['List', 'Pivot', 'Small', 'Large']]).
sort([], []).
sort([Head| Tail], Sorted) :-
tracer::(
trace(partition(Tail, Head, Small, Large)),
trace(sort(Small, Sorted1)),
trace(sort(Large, Sorted2))),
list::append(Sorted1, [Head| Sorted2], Sorted).
partition([], _, [], []).
partition([Head| Tail], Pivot, Small, Large) :-
parameter(1, Type),
( Type::(Head < Pivot) ->
Small = [Head| Small1], Large = Large1
; Small = Small1, Large = [Head| Large1]
),
partition(Tail, Pivot, Small1, Large1).
:- end_object.

View File

@@ -0,0 +1,37 @@
% example adopted from:
% Programming Language Prolog Part 2, Modules
% Committee Draft - January 14, 1998 X3J17/97/5
:- object(tracer).
:- info([
version is 2,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'Tracer for a goal call and exit ports.']).
:- public(trace/1).
:- metapredicate(trace(::)).
:- mode(trace(+callable), zero_or_more).
:- info(trace/1, [
comment is '.',
argnames is ['Goal']]).
trace(Goal) :-
write('call: '), writeq(Goal), nl,
call(Goal),
write('exit: '), writeq(Goal), nl.
trace(Goal) :-
write('fail: '), writeq(Goal), nl,
fail.
:- end_object.

15
Logtalk/examples/mi/NOTES Normal file
View File

@@ -0,0 +1,15 @@
=================================================================
Logtalk - Object oriented extension to Prolog
Release 2.8.4
Copyright (c) 1998-2001 Paulo Moura. All Rights Reserved.
=================================================================
To load all objects in this example consult the mi.loader utility
file (note that the *.loader files are Prolog files).
There are two examples in this folder. The first one is an adoption of a
multi-inheritance C++ example found on the D. M. Capper book "Introducing
C++ for Scientists, Engineers and Mathematicians" published by
Springer-Verlag. It uses dynamic predicates for storing state. The second
example is a variant of the first using parametric objects.

View File

@@ -0,0 +1,89 @@
=================================================================
Logtalk - Object oriented extension to Prolog
Release 2.8.4
Copyright (c) 1998-2001 Paulo Moura. All Rights Reserved.
=================================================================
% set a point in the space-time:
| ?- space_time::rotate(1, 2, 3).
yes
| ?- space_time::translate(4).
yes
% verify it:
| ?- space_time::xyzt(X, Y, Z, T).
T = 4,
X = 1,
Y = 2,
Z = 3 ?
yes
% enumerate space_time public predicates:
| ?- space_time::(current_predicate(Functor/Arity), functor(Pred, Functor, Arity), predicate_property(Pred, declared_in(Object))).
Pred = xyzt(_A,_B,_C,_D),
Arity = 4,
Object = space_time,
Functor = xyzt ? ;
Pred = xyz(_A,_B,_C),
Arity = 3,
Object = space,
Functor = xyz ? ;
Pred = rotate(_A,_B,_C),
Arity = 3,
Object = space,
Functor = rotate ? ;
Pred = t(_A),
Arity = 1,
Object = time,
Functor = t ? ;
Pred = translate(_A),
Arity = 1,
Object = time,
Functor = translate ? ;
no
% get the origin distance from a point in the space-time(_, _, _, _):
| ?- space_time(2,3,4,7)::distance(D).
D = 5.385164807134504 ?
yes
| ?- space_time(2,3,4,7)::time(T).
T = 7 ?
yes
% enumerate space_time(_, _, _, _) public predicates:
| ?- space_time(2,3,4,7)::(current_predicate(Functor/Arity), functor(Pred, Functor, Arity), predicate_property(Pred, declared_in(Object))).
Pred = distance(_A),
Arity = 1,
Object = space(_B,_C,_D),
Functor = distance ? ;
Pred = time(_A),
Arity = 1,
Object = time(_B),
Functor = time ? ;
no

View File

@@ -0,0 +1,10 @@
:- initialization(
logtalk_load([
space,
space_time,
time,
space3,
space_time4,
time1])).

View File

@@ -0,0 +1,28 @@
:- object(space).
:- public(xyz/3).
:- mode(xyz(?integer, ?integer, ?integer), zero_or_one).
:- private(xyz_/3).
:- mode(xyz_(?integer, ?integer, ?integer), zero_or_one).
:- dynamic(xyz_/3).
:- public(rotate/3).
:- mode(rotate(+integer, +integer, +integer), zero_or_one).
xyz(X, Y, Z) :-
::xyz_(X, Y, Z).
rotate(X, Y, Z) :-
integer(X),
integer(Y),
integer(Z),
::retractall(xyz_(_, _, _)),
::assertz(xyz_(X, Y, Z)).
:- end_object.

View File

@@ -0,0 +1,16 @@
:- object(space(_X,_Y,_Z)).
:- public(distance/1).
:- mode(xyz(?nunber), one).
distance(Distance) :-
parameter(1, X),
parameter(2, Y),
parameter(3, Z),
Distance is sqrt(X*X+Y*Y+Z*Z).
:- end_object.

View File

@@ -0,0 +1,15 @@
:- object(space_time,
extends(space, time)).
:- public(xyzt/4).
:- mode(xyzt(?integer, ?integer, ?integer, ?integer), zero_or_one).
xyzt(X, Y, Z, T) :-
::xyz(X, Y, Z),
::t(T).
:- end_object.

Some files were not shown because too many files have changed in this diff Show More