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

23
Logtalk/library/NOTES Normal file
View File

@@ -0,0 +1,23 @@
=================================================================
Logtalk - Object oriented extension to Prolog
Release 2.8.4
Copyright (c) 1998-2001 Paulo Moura. All Rights Reserved.
=================================================================
This folder contains objects, categories, and protocols that implement
predicates for dealing with common Prolog types and structures.
To load all a group of objects, protocols and categories in this
library consult the corresponding *.loader utility file.
Some files represent work in progress and are not loaded by default by
any loader utility file.
Specific notes about each group of objects, categories, and protocols
can be found in the corresponding *.notes files.
Some of the code in this library is based on public domain Prolog code,
in particular, adopted from the Edinburgh Prolog library.
All source files are formatted using four-spaces tabs.

32
Logtalk/library/SCRIPT Normal file
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.
=================================================================
| ?- meta::apply(current_object, [meta]).
yes
| ?- meta::succeeds(integer, [1, 2, 3]).
yes
| ?- meta::map(object_property, [meta, user], Out).
Out = [static,built_in] ? ;
Out = [static,static] ? ;
no
| ?- meta::filter(integer, [1, a, X, b(_), 4, 7.8, 'AAA'], Integers).
Integers = [1,4]
yes

View File

@@ -0,0 +1,39 @@
:- object(after_event_registry,
implements(event_registryp)).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'After events registry predicates.']).
monitors(Monitors) :-
findall(Monitor, current_event(after, _, _, _, Monitor), List),
{sort(List, Monitors)}.
monitored(Objects) :-
findall(Object, current_event(after, Object, _, _, _), List),
{sort(List, Objects)}.
monitor(Object, Message, Sender, Monitor) :-
current_event(after, Object, Message, Sender, Monitor).
set_monitor(Object, Message, Sender, Monitor) :-
define_events(after, Object, Message, Sender, Monitor).
del_monitors(Object, Message, Sender, Monitor) :-
abolish_events(after, Object, Message, Sender, Monitor).
del_monitors :-
abolish_events(after, _, _, _, _).
:- end_object.

17
Logtalk/library/atom.lgt Normal file
View File

@@ -0,0 +1,17 @@
:- object(atom,
extends(atomic)).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'Atom data type predicates.']).
valid(Atom) :-
atom(Atom).
:- end_object.

View File

@@ -0,0 +1,17 @@
:- object(atomic,
extends(term)).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'Atomic data type predicates.']).
valid(Atomic) :-
atomic(Atomic).
:- end_object.

View File

@@ -0,0 +1,105 @@
:- category(attributes).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'Dynamic attributes dictionary.']).
:- public(attribute/2).
:- mode(attribute(?nonvar, ?nonvar), zero_or_more).
:- info(attribute/2,
[comment is 'Returns, by backtracking, all pairs of atribute-values.',
argnames is ['Attribute', 'Value']]).
:- public(attributes/1).
:- mode(attributes(-list), one).
:- info(attributes/1,
[comment is 'List of all pairs of atribute-values.',
argnames is ['Attributes']]).
:- private(attribute_/2).
:- dynamic(attribute_/2).
:- mode(attribute_(?nonvar, ?nonvar), zero_or_more).
:- info(attribute_/2,
[comment is 'Stores attributes values.',
argnames is ['Attribute', 'Value']]).
:- public(del_attribute/2).
:- mode(del_attribute(?nonvar, ?nonvar), zero_or_more).
:- info(del_attribute/2,
[comment is 'Deletes a matching attribute-value pair.',
argnames is ['Attribute', 'Value']]).
:- public(del_attributes/2).
:- mode(del_attributes(@term, @term), one).
:- info(del_attributes/2,
[comment is 'Deletes all matching attribute-value pairs.',
argnames is ['Attribute', 'Value']]).
:- public(set_attribute/2).
:- mode(set_attribute(+nonvar, +nonvar), one).
:- info(set_attribute/2,
[comment is 'Sets an attribute value.',
argnames is ['Attribute', 'Value']]).
:- public(set_attributes/1).
:- mode(set_attributes(+list), one).
:- info(set_attributes/1,
[comment is 'Sets a list of attribute-value pairs.',
argnames is ['Attributes']]).
attribute(Attribute, Value) :-
::attribute_(Attribute, Value).
attributes(Attributes) :-
findall(Attribute, ::attribute_(Attribute, _), Attributes).
del_attribute(Attribute, Value) :-
::retract(attribute_(Attribute, Value)).
del_attributes(Attribute, Value) :-
::retractall(attribute_(Attribute, Value)).
set_attribute(Attribute, Value) :-
::retractall(attribute_(Attribute, _)),
::assertz(attribute_(Attribute, Value)).
set_attributes([]).
set_attributes([Attribute-Value| Attributes]) :-
::retractall(attribute_(Attribute, _)),
::assertz(attribute_(Attribute, Value)),
set_attributes(Attributes).
:- end_category.

View File

@@ -0,0 +1,39 @@
:- object(before_event_registry,
implements(event_registryp)).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'Before events registry predicates.']).
monitors(Monitors) :-
findall(Monitor, current_event(before, _, _, _, Monitor), List),
{sort(List, Monitors)}.
monitored(Objects) :-
findall(Object, current_event(before, Object, _, _, _), List),
{sort(List, Objects)}.
monitor(Object, Message, Sender, Monitor) :-
current_event(before, Object, Message, Sender, Monitor).
set_monitor(Object, Message, Sender, Monitor) :-
define_events(before, Object, Message, Sender, Monitor).
del_monitors(Object, Message, Sender, Monitor) :-
abolish_events(before, Object, Message, Sender, Monitor).
del_monitors :-
abolish_events(before, _, _, _, _).
:- end_object.

155
Logtalk/library/bintree.lgt Normal file
View File

@@ -0,0 +1,155 @@
:- object(bintree,
implements(dictionaryp),
extends(compound)).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'Dictionary protocol implemented using binary trees.']).
:- private(map/4).
:- metapredicate(map(*, *, *, ::)).
:- mode(map(+atom, +tree, -tree, -callable), zero_or_one).
as_list(Tree, List) :-
as_list(Tree, [], List).
as_list(t, List, List).
as_list(t(Key, Value, Left, Right), Acc, List) :-
as_list(Right, Acc, Acc2),
as_list(Left, [Key-Value| Acc2], List).
empty(Tree) :-
Tree == t.
insert(Key, Value, t, t(Key, Value, t, t)) :-
nonvar(Key).
insert(Key, Value, t(Key1, Value1, Left1, Right1), t(Key1, Value2, Left2, Right2)) :-
compare(Order, Key, Key1),
insert(Order, Key, Value, Key1, Value1, Left1, Right1, Value2, Left2, Right2).
insert(=, _, Value, _, _, Left, Right, Value, Left, Right).
insert(<, Key, Value, _, Value1, Left1, Right, Value1, Left2, Right) :-
insert(Key, Value, Left1, Left2).
insert(>, Key, Value, _, Value1, Left, Right1, Value1, Left, Right2) :-
insert(Key, Value, Right1, Right2).
insert_all([], Tree, Tree).
insert_all([Key-Value| Rest], Old, New) :-
insert(Key, Value, Old, Aux),
insert_all(Rest, Aux, New).
lookup(Key, Value, Tree) :-
var(Key) ->
lookup_var(Key, Value, Tree)
;
lookup_nonvar(Key, Value, Tree).
lookup_nonvar(Key, Value, t(Key1, Value1, Left1, Right1)) :-
compare(Order, Key, Key1),
lookup_nonvar(Order, Key, Value, Value1, Left1, Right1).
lookup_nonvar(=, _, Value, Value, _, _).
lookup_nonvar(<, Key, Value, _, Left, _) :-
lookup_nonvar(Key, Value, Left).
lookup_nonvar(<, Key, Value, _, _, Right) :-
lookup_nonvar(Key, Value, Right).
lookup_var(Key, Value, t(_, _, Left, _)) :-
lookup_var(Key, Value, Left).
lookup_var(Key, Value, t(Key, Value,_,_)).
lookup_var(Key, Value, t(_, _, _, Right)) :-
lookup_var(Key, Value, Right).
keys(Tree, Keys) :-
keys(Tree, [], Keys).
keys(t, Keys, Keys).
keys(t(Key, _, Left, Right), Acc, Keys) :-
keys(Right, Acc, Acc2),
keys(Left, [Key| Acc2], Keys).
delete(t, _, _, t).
delete(t(Key1, Value1, Left1, Right1), Key, Value, Out) :-
compare(Order, Key, Key1),
delete(Order, Key1, Value1, Left1, Right1, Key, Value, Out).
delete(=, Key1, Value1, Left1, Right1, Key1, Value1, Out) :-
join(Left1, Right1, Out).
delete(<, Key1, Value1, Left1, Right1, Key, Value, t(Key1, Value1, Left2, Right1)) :-
delete(Left1, Key, Value, Left2).
delete(>, Key1, Value1, Left1, Right1, Key, Value, t(Key1, Value1, Left1, Right2)) :-
delete(Right1, Key, Value, Right2).
join(t, Right, Right) :-
!.
join(Left, t, Left) :-
!.
join(t(Key, Value, Left, Right), Tree, t(Key, Value, Left, Right2)) :-
join(Right, Tree, Right2).
map(Pred, Old, New) :-
map(Pred, Old, New, _).
map(Pred, t(Key1, Value1, Left1, Right1), t(Key2, Value2, Left2, Right2), Goal) :-
Goal =.. [Pred, Key1-Value1, Key2-Value2],
once(Goal),
map(Pred, Left1, Left2, _),
map(Pred, Right1, Right2, _).
map(_, t, t, _).
new(t).
size(Dictionary, Size) :-
size(Dictionary, 0, Size).
size(t, Size, Size).
size(t(_, _, Left, Right), Acc, Size) :-
size(Left, Acc, Acc2),
Acc3 is Acc2 + 1,
size(Right, Acc3, Size).
:- end_object.

View File

@@ -0,0 +1,17 @@
:- object(callable,
extends(term)).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'Callable term type predicates.']).
valid(Callable) :-
once((atom(Callable); compound(Callable))).
:- end_object.

View File

@@ -0,0 +1,124 @@
:- object(character,
implements(characterp),
extends(atom)).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'Character predicates.']).
is_alpha('_').
is_alpha(Char) :-
is_letter(Char).
is_letter(Char) :-
is_lower_case(Char).
is_letter(Char) :-
is_upper_case(Char).
is_alphanumeric(Char) :-
is_alpha(Char).
is_alphanumeric(Char) :-
is_dec_digit(Char).
is_bin_digit(0).
is_bin_digit(1).
is_octal_digit(Digit) :-
Digit @>= 0,
Digit @=< 7.
is_dec_digit(Digit) :-
Digit @>= 0,
Digit @=< 9.
is_hex_digit(Digit) :-
Digit @>= 0,
Digit @=< 9.
is_hex_digit(Digit) :-
Digit @>= 'A',
Digit @=< 'F'.
is_hex_digit(Digit) :-
Digit @>= a,
Digit @=< f.
is_lower_case(Char) :-
Char @>= a,
Char @=< z.
is_upper_case(Char) :-
Char @>= 'A',
Char @=< 'Z'.
is_vowel(a).
is_vowel(e).
is_vowel(i).
is_vowel(o).
is_vowel(u).
is_vowel('A').
is_vowel('E').
is_vowel('I').
is_vowel('O').
is_vowel('U').
is_layout(' ').
lower_upper(a, 'A').
lower_upper(b, 'B').
lower_upper(c, 'C').
lower_upper(d, 'D').
lower_upper(e, 'E').
lower_upper(f, 'F').
lower_upper(g, 'G').
lower_upper(h, 'H').
lower_upper(i, 'I').
lower_upper(j, 'J').
lower_upper(k, 'K').
lower_upper(l, 'L').
lower_upper(m, 'M').
lower_upper(n, 'N').
lower_upper(o, 'O').
lower_upper(p, 'P').
lower_upper(q, 'Q').
lower_upper(r, 'R').
lower_upper(s, 'S').
lower_upper(t, 'T').
lower_upper(u, 'U').
lower_upper(v, 'V').
lower_upper(w, 'W').
lower_upper(x, 'X').
lower_upper(y, 'Y').
lower_upper(z, 'Z').
lower_upper(Char, Char) :-
\+ (Char @>= a, Char @=< z),
\+ (Char @>= 'A', Char @=< 'Z').
valid(Character) :-
atom(Character),
atom_length(Character, 1).
:- end_object.

View File

@@ -0,0 +1,122 @@
:- protocol(characterp).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'Character protocol.']).
:- public(is_alphanumeric/1).
:- mode(is_alphanumeric(+char), zero_or_one).
:- info(is_alphanumeric/1, [
comment is 'True if the argument is an alphanumeric character.',
argnames is ['Term']]).
:- public(is_alpha/1).
:- mode(is_alpha(+char), zero_or_one).
:- info(is_alpha/1, [
comment is 'True if the argument is a letter or an underscore.',
argnames is ['Term']]).
:- public(is_letter/1).
:- mode(is_letter(+char), zero_or_one).
:- info(is_letter/1, [
comment is 'True if the argument is a letter.',
argnames is ['Term']]).
:- public(is_bin_digit/1).
:- mode(is_bin_digit(+char), zero_or_one).
:- info(is_bin_digit/1, [
comment is 'True if the argument is a binary digit.',
argnames is ['Term']]).
:- public(is_octal_digit/1).
:- mode(is_octal_digit(+char), zero_or_one).
:- info(is_octal_digit/1, [
comment is 'True if the argument is an octal digit.',
argnames is ['Term']]).
:- public(is_dec_digit/1).
:- mode(is_dec_digit(+char), zero_or_one).
:- info(is_dec_digit/1, [
comment is 'True if the argument is a decimal digit.',
argnames is ['Term']]).
:- public(is_hex_digit/1).
:- mode(is_hex_digit(+char), zero_or_one).
:- info(is_hex_digit/1, [
comment is 'True if the argument is an hexadecimal digit.',
argnames is ['Term']]).
:- public(is_lower_case/1).
:- mode(is_lower_case(+char), zero_or_one).
:- info(is_lower_case/1, [
comment is 'True if the argument is a lower case letter.',
argnames is ['Term']]).
:- public(is_upper_case/1).
:- mode(is_upper_case(+char), zero_or_one).
:- info(is_upper_case/1, [
comment is 'True if the argument is a upper case letter.',
argnames is ['Term']]).
:- public(is_vowel/1).
:- mode(is_vowel(+char), zero_or_one).
:- info(is_vowel/1, [
comment is 'True if the argument is a vowel.',
argnames is ['Term']]).
:- public(is_layout/1).
:- mode(is_layout(+char), zero_or_one).
:- info(is_layout/1, [
comment is 'True if the argument is a layout character.',
argnames is ['Term']]).
:- public(lower_upper/2).
:- mode(lower_upper(?char, ?char), zero_or_more).
:- mode(lower_upper(+char, ?char), zero_or_one).
:- mode(lower_upper(?char, +char), zero_or_one).
:- info(lower_upper/2, [
comment is 'Converts between lower and upper case letters.',
argnames is ['Term1', 'Term2']]).
:- end_protocol.

View File

@@ -0,0 +1,196 @@
:- category(class_hierarchy,
implements(class_hierarchyp)).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'Class hierarchy predicates.']).
class(Class) :-
self(Self),
instantiates_class(Self, Class).
classes(Classes) :-
self(Self),
findall(Class, instantiates_class(Self, Class), Classes).
ancestor(Ancestor) :-
self(Self),
ancestor(Self, Ancestor).
ancestor(Self, Ancestor) :-
instantiates_class(Self, Ancestor).
ancestor(Self, Ancestor) :-
instantiates_class(Self, Class),
superclass(Class, Ancestor).
ancestors(Ancestors) :-
self(Self),
findall(Ancestor, ancestor(Self, Ancestor), Ancestors).
instance(Instance) :-
self(Self),
instantiates_class(Instance, Self).
instances(Instances) :-
self(Self),
findall(Instance, instantiates_class(Instance, Self), Instances).
subclass(Subclass) :-
self(Self),
specializes_class(Subclass, Self).
subclasses(Subclasses) :-
self(Self),
findall(Subclass, specializes_class(Subclass, Self), Subclasses).
superclass(Superclass) :-
self(Self),
superclass(Self, Superclass).
superclass(Self, Superclass) :-
specializes_class(Self, Superclass).
superclass(Self, Superclass) :-
specializes_class(Self, Class),
superclass(Class, Superclass).
superclasses(Superclasses) :-
self(Self),
findall(Superclass, specializes_class(Self, Superclass), Superclasses).
leaf(Leaf) :-
self(Self),
leaf(Self, Leaf).
leaf(Self, Leaf) :-
instantiates_class(Leaf, Self),
\+ instantiates_class(_, Leaf),
\+ specializes_class(_, Leaf).
leaf(Self, Leaf) :-
specializes_class(Leaf, Self),
\+ instantiates_class(_, Leaf),
\+ specializes_class(_, Leaf).
leaf(Self, Leaf) :-
specializes_class(Subclass, Self),
leaf(Subclass, Leaf).
leaves(Leaves) :-
self(Self),
findall(Leaf, leaf(Self, Leaf), Leaves).
leaf_instance(Leaf) :-
self(Self),
leaf_instance(Self, Leaf).
leaf_instance(Self, Leaf) :-
instantiates_class(Leaf, Self),
\+ instantiates_class(_, Leaf).
leaf_instance(Self, Leaf) :-
specializes_class(Subclass, Self),
leaf_instance(Subclass, Leaf).
leaf_instances(Leaves) :-
self(Self),
findall(Leaf, leaf_instance(Self, Leaf), Leaves).
leaf_class(Leaf) :-
self(Self),
leaf_class(Self, Leaf).
leaf_class(Self, Leaf) :-
specializes_class(Leaf, Self),
\+ specializes_class(_, Leaf).
leaf_class(Self, Leaf) :-
specializes_class(Subclass, Self),
leaf_class(Subclass, Leaf).
leaf_classes(Leaves) :-
self(Self),
findall(Leaf, leaf_class(Self, Leaf), Leaves).
descendant(Descendant) :-
self(Self),
descendant(Self, Descendant).
descendant(Self, Descendant) :-
instantiates_class(Descendant, Self).
descendant(Self, Descendant) :-
specializes_class(Descendant, Self),
\+ instantiates_class(Descendant, Self).
descendant(Self, Descendant) :-
specializes_class(Subclass, Self),
descendant(Subclass, Descendant).
descendants(Descendants) :-
self(Self),
findall(Descendant, descendant(Self, Descendant), Descendants).
descendant_class(Descendant) :-
self(Self),
descendant_class(Self, Descendant).
descendant_class(Self, Descendant) :-
specializes_class(Descendant, Self).
descendant_class(Self, Descendant) :-
specializes_class(Subclass, Self),
descendant_class(Subclass, Descendant).
descendant_classes(Descendants) :-
self(Self),
findall(Descendant, descendant_class(Self, Descendant), Descendants).
descendant_instance(Descendant) :-
self(Self),
descendant_instance(Self, Descendant).
descendant_instance(Self, Descendant) :-
instantiates_class(Descendant, Self).
descendant_instance(Self, Descendant) :-
specializes_class(Subclass, Self),
descendant_instance(Subclass, Descendant).
descendant_instances(Descendants) :-
self(Self),
findall(Descendant, descendant_instance(Self, Descendant), Descendants).
:- end_category.

View File

@@ -0,0 +1,157 @@
:- protocol(class_hierarchyp,
extends(hierarchyp)).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'Class hierarchy protocol.']).
:- public(class/1).
:- mode(class(?object), zero_or_more).
:- info(class/1, [
comment is 'Returns, by backtracking, all object classes.',
argnames is ['Class']]).
:- public(classes/1).
:- mode(classes(-list), one).
:- info(classes/1, [
comment is 'List of all object classes.',
argnames is ['Classes']]).
:- public(instance/1).
:- mode(instance(?object), zero_or_more).
:- info(instance/1, [
comment is 'Returns, by backtracking, all class instances.',
argnames is ['Instance']]).
:- public(instances/1).
:- mode(instances(-list), one).
:- info(instances/1, [
comment is 'List of all class instances.',
argnames is ['Instances']]).
:- public(subclass/1).
:- mode(subclass(?object), zero_or_more).
:- info(subclass/1, [
comment is 'Returns, by backtracking, all class subclasses.',
argnames is ['Subclass']]).
:- public(subclasses/1).
:- mode(subclasses(-list), one).
:- info(subclasses/1, [
comment is 'List of all class subclasses.',
argnames is ['Subclasses']]).
:- public(superclass/1).
:- mode(superclass(?object), zero_or_more).
:- info(superclass/1, [
comment is 'Returns, by backtracking, all class superclasses.',
argnames is ['Superclass']]).
:- public(superclasses/1).
:- mode(superclasses(-list), one).
:- info(superclasses/1, [
comment is 'List of all class superclasses.',
argnames is ['Superclasses']]).
:- public(leaf_instance/1).
:- mode(leaf_instance(?object), zero_or_more).
:- info(leaf_instance/1, [
comment is 'Returns, by backtracking, all class leaf instances.',
argnames is ['Leaf']]).
:- public(leaf_instances/1).
:- mode(leaf_instances(-list), one).
:- info(leaf_instances/1, [
comment is 'List of all class leaf instances.',
argnames is ['Leaves']]).
:- public(leaf_class/1).
:- mode(leaf_class(?object), zero_or_more).
:- info(leaf_class/1, [
comment is 'Returns, by backtracking, all class leaf subclasses.',
argnames is ['Leaf']]).
:- public(leaf_classes/1).
:- mode(leaf_classes(-list), one).
:- info(leaf_classes/1, [
comment is 'List of all class leaf leaf subclasses.',
argnames is ['Leaves']]).
:- public(descendant_instance/1).
:- mode(descendant_instance(?object), zero_or_more).
:- info(descendant_instance/1, [
comment is 'Returns, by backtracking, all class descendant instances.',
argnames is ['Descendant']]).
:- public(descendant_instances/1).
:- mode(descendant_instances(-list), one).
:- info(descendant_instances/1, [
comment is 'List of all class descendant instances.',
argnames is ['Descendants']]).
:- public(descendant_class/1).
:- mode(descendant_class(?object), zero_or_more).
:- info(descendant_class/1, [
comment is 'Returns, by backtracking, all class descendant subclasses.',
argnames is ['Descendant']]).
:- public(descendant_classes/1).
:- mode(descendant_classes(-list), one).
:- info(descendant_classes/1, [
comment is 'List of all class descendant subclasses.',
argnames is ['Descendants']]).
:- end_protocol.

View File

@@ -0,0 +1,66 @@
:- protocol(comparingp).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'Comparing protocol using overloading of standard operators.']).
:- public((<)/2).
:- mode(<(+term, +term), zero_or_one).
:- info((<)/2, [
comment is 'True if Term1 is less than Term2.',
argnames is ['Term1', 'Term2']]).
:- public((=<)/2).
:- mode(=<(+term, +term), zero_or_one).
:- info((=<)/2, [
comment is 'True if Term1 is less or equal than Term2.',
argnames is ['Term1', 'Term2']]).
:- public((>)/2).
:- mode(>(+term, +term), zero_or_one).
:- info((>)/2, [
comment is 'True if Term1 is greater than Term2.',
argnames is ['Term1', 'Term2']]).
:- public((>=)/2).
:- mode(>=(+term, +term), zero_or_one).
:- info((>=)/2, [
comment is 'True if Term1 is equal or grater than Term2.',
argnames is ['Term1', 'Term2']]).
:- public((=:=)/2).
:- mode(=:=(+term, +term), zero_or_one).
:- info((=:=)/2, [
comment is 'True if Term1 is equal to Term2.',
argnames is ['Term1', 'Term2']]).
:- public((=\=)/2).
:- mode(=\=(+term, +term), zero_or_one).
:- info((=\=)/2, [
comment is 'True if Term1 is not equal to Term2.',
argnames is ['Term1', 'Term2']]).
:- end_protocol.

View File

@@ -0,0 +1,17 @@
:- object(compound,
extends(term)).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'Compound data type.']).
valid(Compound) :-
compound(Compound).
:- end_object.

71
Logtalk/library/date.lgt Normal file
View File

@@ -0,0 +1,71 @@
:- object(date,
implements(datep)).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'Date predicates.']).
today(Year, Month, Day) :-
{lgt_current_date(Year, Month, Day)}.
leap_year(Year) :-
(0 is mod(Year, 4),
\+ 0 is mod(Year, 100)
;
0 is mod(Year, 400)),
!.
name_of_day(1, 'Sunday', 'Sun').
name_of_day(2, 'Monday', 'Mon').
name_of_day(3, 'Tuesday', 'Tue').
name_of_day(4, 'Wednesday', 'Wed').
name_of_day(5, 'Thursday', 'Thu').
name_of_day(6, 'Friday', 'Fri').
name_of_day(7, 'Saturday', 'Sat').
name_of_month( 1, 'January', 'Jan').
name_of_month( 2, 'February', 'Feb').
name_of_month( 3, 'March', 'Mar').
name_of_month( 4, 'April', 'Apr').
name_of_month( 5, 'May', 'May').
name_of_month( 6, 'June', 'Jun').
name_of_month( 7, 'July', 'Jul').
name_of_month( 8, 'August', 'Aug').
name_of_month( 9, 'September', 'Sep').
name_of_month(10, 'October', 'Oct').
name_of_month(11, 'November', 'Nov').
name_of_month(12, 'December', 'Dec').
days_in_month( 1, _, 31).
days_in_month( 2, Year, Days) :-
leap_year(Year) -> Days = 29; Days = 28.
days_in_month( 3, _, 31).
days_in_month( 4, _, 30).
days_in_month( 5, _, 31).
days_in_month( 6, _, 30).
days_in_month( 7, _, 31).
days_in_month( 8, _, 31).
days_in_month( 9, _, 30).
days_in_month(10, _, 31).
days_in_month(11, _, 30).
days_in_month(12, _, 31).
valid(Year, Month, Day) :-
integer(Year),
integer(Month), Month >= 1, Month =< 12,
integer(Day),
days_in_month(Month, Year, Days),
Day >= 1, Day =< Days.
:- end_object.

66
Logtalk/library/datep.lgt Normal file
View File

@@ -0,0 +1,66 @@
:- protocol(datep).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'Date protocol.']).
:- public(today/3).
:- mode(today(-integer, -integer, -integer), one).
:- info(now/3, [
comment is 'Returns current date.',
argnames is ['Year', 'Month', 'Day']]).
:- public(leap_year/1).
:- mode(leap_year(+integer), zero_or_one).
:- info(empty/1,
[comment is 'True if the argument is a leap year.',
argnames is ['Year']]).
:- public(name_of_day/3).
:- mode(name_of_day(?integer, ?atom, ?atom), zero_or_more).
:- info(valid/3, [
comment is 'Name and short name of day.',
argnames is ['Index', 'Name', 'Short']]).
:- public(name_of_month/3).
:- mode(name_of_month(?integer, ?atom, ?atom), zero_or_more).
:- info(valid/3, [
comment is 'Name and short name of month.',
argnames is ['Index', 'Name', 'Short']]).
:- public(days_in_month/3).
:- mode(days_in_month(?integer, +integer, ?integer), zero_or_more).
:- info(valid/3, [
comment is 'Number of days in a month.',
argnames is ['Month', 'Year', 'Days']]).
:- public(valid/3).
:- mode(valid(@integer, @integer, @integer), zero_or_one).
:- info(valid/3, [
comment is 'True if the arguments represent a valid date.',
argnames is ['Year', 'Month', 'Day']]).
:- end_protocol.

View File

@@ -0,0 +1,5 @@
:- initialization(
logtalk_load([
datep, date,
timep, time])).

View File

@@ -0,0 +1,18 @@
=================================================================
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 group consult the dates.loader utility
file.
The object date implements some useful calandar date predicates.
The object time implements some useful time predicates.
Please note that some of the functionality of these objects depends
on the used Prolog support for accessing operating system time and
date values.

View File

@@ -0,0 +1,181 @@
:- object(debugger,
implements(debuggerp, event_handlersp),
imports(monitor)).
:- info([
version is 1.0,
date is 2000/7/24,
authors is 'Paulo Moura',
comment is 'Debugging facilities similar to those found in most Prolog compilers.']).
:- initialization(::init).
:- protected(port_output/4).
:- mode(port_output(+atom, +object, @callable, +object), one).
:- info(port_output/4, [
comment is 'Outputs current port information.',
argnames is ['Port', 'Object', 'Message', 'Sender']]).
:- protected(execute_option/1).
:- mode(execute_option(+atom), one).
:- info(execute_option/1, [
comment is 'Executes a user option at a debugger port.',
argnames is ['Option']]).
:- protected(query_user/1).
:- mode(query_user(-atom), one).
:- info(query_user/1, [
comment is 'Query a user about an option at a debugger port.',
argnames is ['Option']]).
:- private(stream_/2).
:- dynamic(stream_/2).
:- mode(stream_(?atom, ?stream), zero_or_more).
:- info(stream/2, [
comment is 'Stores the current debugger input and ouput streams.',
argnames is ['Kind', 'Stream']]).
stream(Name, Stream) :-
::stream_(Name, Stream).
set_stream(Name, Stream) :-
::retractall(stream_(Name, _)),
::assertz(stream_(Name, Stream)).
trace :-
self(Self),
abolish_events(before, _, _, _, Self),
abolish_events(after, _, _, _, Self),
define_events(before, _, _, _, Self),
define_events(after, _, _, _, Self).
notrace :-
self(Self),
abolish_events(before, _, _, _, Self),
abolish_events(after, _, _, _, Self).
debugging :-
::monitor_activated.
debug :-
::activate_monitor.
nodebug :-
::suspend_monitor.
port_output(Port, Object, Message, Sender) :-
::stream(output, Output),
write(Output, Port),
write(Output, ': '),
writeq(Output, Object),
write(Output, ' <- '),
writeq(Output, Message),
write(Output, ' from '),
writeq(Output, Sender),
nl(Output).
query_user(Option) :-
::stream(output, Output),
::stream(input, Input),
repeat,
write(Output, ' >> '),
read(Input, Option),
nl(Output),
(valid_option(Option) ->
true
;
::execute_option(h), fail),
!.
execute_option(c).
execute_option(f) :-
!, fail.
execute_option(n) :-
::nodebug.
execute_option(b) :-
::stream(output,Output),
::stream(input, Input),
repeat,
write(Output, ' :- '),
read(Input, Goal),
writeq(Output, Goal),
nl(Output),
(once(Goal) ->
write(Output, ' answer: '),
writeq(Output, Goal), nl(Output)
;
write(Output, ' no'), nl(Output)),
Goal = true,
!.
execute_option(a) :-
abort.
execute_option(h) :-
::stream(output, Output),
write(Output, ' Available options are:'), nl(Output),
write(Output, ' c - creep (go on)'), nl(Output),
write(Output, ' f - fail (force failure or backtracking)'), nl(Output),
write(Output, ' n - nodebug (turn off debug)'), nl(Output),
write(Output, ' b - break (submit queries to the interpreter, type true to terminate)'), nl(Output),
write(Output, ' a - abort (return to top level interpreter)'), nl(Output),
write(Output, ' h - help (prints this list of options)'), nl(Output),
nl(Output).
valid_option(c).
valid_option(f).
valid_option(n).
valid_option(b).
valid_option(a).
before(Object, Message, Sender) :-
::port_output(call, Object, Message, Sender),
::query_user(Option),
::execute_option(Option).
after(Object, Message, Sender) :-
::port_output(exit, Object, Message, Sender),
::query_user(Option),
::execute_option(Option).
init :-
::reset_monitor,
current_input(Input),
::set_stream(input, Input),
current_output(Output),
::set_stream(output, Output).
:- end_object.

View File

@@ -0,0 +1,78 @@
:- protocol(debuggerp).
:- info([
version is 1.0,
date is 2000/7/24,
authors is 'Paulo Moura',
comment is 'Debugging protocol similar to those found in most Prolog compilers.']).
:- public(trace/0).
:- mode(trace, one).
:- info(trace/0, [
comment is 'Starts tracing all message sending events.']).
:- public(notrace/0).
:- mode(notrace, one).
:- info(notrace/0, [
comment is 'Stops tracing.']).
:- public(debug/0).
:- mode(debug, one).
:- info(debug/0, [
comment is 'Activates spy points and starts debugging.']).
:- public(nodebug/0).
:- mode(nodebug, one).
:- info(nodebug/0, [
comment is 'Suspends spy points and stops debugging.']).
:- public(debugging/0).
:- mode(debugging, zero_or_one).
:- info(debugging/0, [
comment is 'True if the debugger is active.']).
:- public(init/0).
:- mode(init, one).
:- info(init/0, [
comment is 'Initializes debugger, turns debugging off and resets all spy points and streams.']).
:- public(stream/2).
:- mode(stream(?atom, ?stream), zero_or_more).
:- info(stream/2, [
comment is 'Current debugger input or ouput streams.',
argnames is ['Kind', 'Stream']]).
:- public(set_stream/2).
:- mode(set_stream(+atom, +stream), one).
:- info(set_stream/2, [
comment is 'Sets the debugger input or output stream.',
argnames is ['Kind', 'Stream']]).
:- end_protocol.

View File

@@ -0,0 +1,5 @@
:- initialization(
logtalk_load([
debuggerp,
debugger])).

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.
=================================================================
To load all objects in this group consult the debugging.loader utility
file.
You will also need to load FIRST the events.loader file.
The object debugger enables you to:
- spy all or specific messages to an object
- trace an execution
- specify the streams used for debugger input/output
These capabilities rely on the use of the event-based programming built in
Logtalk. That means that you can only debug public messages sent using the
::/2 operator.
You can have any number of debuggers active simultaneously, possibly
assigning different input/output streams to each one.
Input/output is one area where Prologs compilers can differ, sometimes
because of differences in the underlying operating system. Therefore,
it is advisable that you look at the code of class debugger before
trying to use it. The i/o operations are done by the methods output/3,
query_user/1 and execute_option/1.

View File

@@ -0,0 +1,103 @@
:- protocol(dictionaryp).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'Dictionary protocol.']).
:- public(as_dictionary/2).
:- mode(as_dictionary(@list, -dictionary), one).
:- info(as_dictionary/2, [
comment is 'Converts a list of key-value pairs to a dictionary.',
argnames is ['List', 'Dictionary']]).
:- public(as_list/2).
:- mode(as_list(@dictionary, -list), one).
:- info(as_list/2, [
comment is 'Converts a dictionary to a list of key-value pairs.',
argnames is ['Dictionary', 'List']]).
:- public(delete/4).
:- mode(delete(+dictionary, @ground, ?term, -dictionary), zero_or_one).
:- info(delete/4, [
comment is 'Deletes a matching Key-Value pair from a dictionary, returning the updated dictionary.',
argnames is ['Dictionary_in', 'Key', 'Value', 'Dictionary_out']]).
:- public(empty/1).
:- mode(empty(@dictionary), zero_or_one).
:- info(empty/1, [
comment is 'True if the dictionary is empty.',
argnames is ['Dictionary']]).
:- public(insert/4).
:- mode(insert(+ground, @term, +dictionary, -dictionary), one).
:- info(insert/4, [
comment is 'Inserts a Key-Value pair into a dictionary, returning the updated dictionary.',
argnames is ['Key', 'Value', 'Dictionary_in', 'Dictionary_out']]).
:- public(insert_all/3).
:- mode(insert_all(@list, +dictionary, -dictionary), one).
:- info(insert_all/3, [
comment is 'Inserts a list of Key-Value pairs into a dictionary, returning the updated dictionary.',
argnames is ['List', 'Dictionary_in', 'Dictionary_out']]).
:- public(lookup/3).
:- mode(lookup(+ground, ?term, @dictionary), zero_or_one).
:- mode(lookup(-ground, ?term, @dictionary), zero_or_more).
:- info(lookup/3, [
comment is 'Get a matching Key-Value pair from a dictionary.',
argnames is ['Key', 'Value', 'Dictionary']]).
:- public(keys/2).
:- mode(keys(@dictionary, -list), one).
:- info(keys/2, [
comment is 'Returns a list with all dictionary keys.',
argnames is ['Dictionary', 'List']]).
:- public(map/3).
:- mode(map(+functor, +dictionary, -dictionary), zero_or_one).
:- info(map/3, [
comment is 'Maps a binary predicate over each dictionary key-value pair returning a new pair.',
argnames is ['Functor', 'In', 'Out']]).
:- public(size/2).
:- mode(size(@dictionary, ?integer), zero_or_one).
:- info(size/2, [
comment is 'Number of dictionary entries.',
argnames is ['Dictionary', 'Size']]).
:- end_protocol.

View File

@@ -0,0 +1,350 @@
:- object(difflist,
implements(listp),
extends(compound)).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'Difference list predicates.']).
:- public(as_list/2).
:- mode(as_list(+list, -list), one).
:- info(as_list/2,
[comment is 'Converts a difference list to a normal list.',
argnames is ['Diffist', 'List']]).
append(List1-Back1, Back1-Back2, List1-Back2) :-
nonvar(List1),
nonvar(Back1),
!.
append(Prefix, Suffix, List) :-
length(List, Length),
prefix(Prefix, List),
length(Prefix, PLength),
SLength is Length - PLength,
length(Suffix, SLength),
suffix(Suffix, List).
as_list(List-Back, Out) :-
List == Back ->
Out = []
;
Out = [Head| Tail],
List = [Head| Rest],
as_list(Rest-Back, Tail).
delete(List-Back, Element, Remaining) :-
List == Back ->
unify_with_occurs_check(Remaining, Back-Back)
;
List \== Back,
List = [Head| Tail],
(Head == Element ->
delete(Tail-Back, Element, Remaining)
;
Remaining = [Head| Tail2],
delete(Tail-Back, Element, Tail2-Back)).
delete_matches(List-Back, Element, Remaining) :-
List == Back ->
unify_with_occurs_check(Remaining, Back-Back)
;
List \== Back,
List = [Head| Tail],
(\+ \+ Head = Element ->
delete_matches(Tail-Back, Element, Remaining)
;
Remaining = [Head| Tail2],
delete_matches(Tail-Back, Element, Tail2-Back)).
empty(List-Back) :-
List == Back.
flatten(List-Back, Flatted-Back) :-
flatten(List-Back, Back-Back, Flatted-Back).
flatten(Var, Tail-Back, [Var| Tail]-Back) :-
var(Var),
!.
flatten(List-Back, Flatted, Flatted) :-
List == Back,
!.
flatten(List-Back, Acc, Flatted) :-
!,
List \== Back,
List = [Head| Tail],
flatten(Tail-Back, Acc, Acc2),
flatten(Head, Acc2, Flatted).
flatten(Head, Tail-Back, [Head| Tail]-Back).
keysort(Difflist, Sorted) :-
as_list(Difflist, List),
{keysort(List, List2)},
list::as_difflist(List2, Sorted).
last(List-Back, Last) :-
List \== Back,
List = [Head| Tail],
last(Tail-Back, Head, Last).
last(List, Last, Last) :-
unify_with_occurs_check(List, Back-Back).
last(List-Back, _, Last) :-
List \== Back,
List = [Head| Tail],
last(Tail-Back, Head, Last).
length(List, Length) :-
integer(Length) ->
Length >= 0,
make_list(Length, List)
;
length(List, 0, Length).
length(List, Length, Length) :-
unify_with_occurs_check(List, Back-Back).
length(List-Back, Acc, Length) :-
List \== Back,
List = [_| Tail],
Acc2 is Acc + 1,
length(Tail-Back, Acc2, Length).
make_list(0, List):-
!,
unify_with_occurs_check(List, Back-Back).
make_list(N, List-Back):-
List \== Back,
List = [_| Tail],
M is N-1,
make_list(M, Tail-Back).
max(List-Back, Max) :-
List \== Back,
List = [Head| Tail],
max(Tail-Back, Head, Max).
max(List-Back, Max, Max) :-
List == Back, !.
max(List-Back, Aux, Max) :-
List \== Back,
List = [Head| Tail],
(Aux @< Head ->
max(Tail-Back, Head, Max)
;
max(Tail-Back, Aux, Max)).
member(Element, List-Back) :-
List \== Back,
List = [Element|_].
member(Element, List-Back) :-
List \== Back,
List = [_| Tail],
member(Element, Tail-Back).
memberchk(Element, List) :-
once(member(Element, List)).
nth(Position, List, Element) :-
nth(Element, List, 1, Position).
nth(Element, List-Back, Position, Position) :-
List \== Back,
List = [Element| _].
nth(Element, List-Back, Count, Position) :-
List \== Back,
List = [_| Tail],
Count2 is Count + 1,
nth(Element, Tail-Back, Count2, Position).
min(List-Back, Min) :-
List \== Back,
List = [Head| Tail],
min(Tail-Back, Head, Min).
min(List-Back, Min, Min) :-
List == Back, !.
min(List-Back, Aux, Min) :-
List \== Back,
List = [Head| Tail],
(Head @< Aux ->
min(Tail-Back, Head, Min)
;
min(Tail-Back, Aux, Min)).
new(List) :-
unify_with_occurs_check(List, Back-Back).
permutation(List, Permutation) :-
same_length(List, Permutation),
permutation2(List, Permutation).
permutation2(List1-Back1, List2-Back2) :-
List1 == Back1,
List2 == Back2.
permutation2(List1-Back1, List2-Back2) :-
List2 \== Back2,
List2 = [Head2| Tail2],
select(Head2, List1-Back1, Tail1-Back1),
permutation2(Tail1-Back1, Tail2-Back2).
prefix(List, _) :-
unify_with_occurs_check(List, Back-Back).
prefix(List-Back, List2-Back2) :-
List \== Back,
List = [Head| Tail],
List2 \== Back2,
List2 = [Head| Tail2],
prefix(Tail-Back, Tail2-Back2).
reverse(List-Back, Reversed-Back) :-
same_length(List-Back, Reversed-Back),
reverse(List-Back, Back-Back, Reversed-Back).
reverse(List-Back, Reversed, Reversed) :-
List == Back.
reverse(List-Back, Acc-Back, Reversed) :-
List \== Back,
List = [Head| Tail],
reverse(Tail-Back, [Head| Acc]-Back, Reversed).
same_length(List1, List2) :-
unify_with_occurs_check(List1, Back1-Back1),
unify_with_occurs_check(List2, Back2-Back2).
same_length(List1-Back1, List2-Back2) :-
List1 \== Back1,
List1 = [_| Tail1],
List2 \== Back2,
List2 = [_| Tail2],
same_length(Tail1-Back1, Tail2-Back2).
select(Head, List-Back, Tail-Back) :-
List \== Back,
List = [Head| Tail].
select(Head, List-Back, List2-Back) :-
List \== Back,
List = [Other| Tail],
List2 \== Back,
List2 = [Other| Tail2],
select(Head, Tail-Back, Tail2-Back).
sort(Difflist, Sorted) :-
as_list(Difflist, List),
{sort(List, List2)},
list::as_difflist(List2, Sorted).
sublist(Sublist, List) :-
unify_with_occurs_check(Sublist, List).
sublist(Sublist-Back, List-Back):-
List \== Back,
List = [Head| Tail],
sublist(Tail-Back, Head, Sublist-Back).
sublist(List, _, Sublist) :-
unify_with_occurs_check(List, Sublist).
sublist(List-Back, _, Sublist-Back):-
List \== Back,
List = [Head| Tail],
sublist(Tail-Back, Head, Sublist-Back).
sublist(List-Back, Element, [Element| Sublist]-Back):-
List \== Back,
List = [Head| Tail],
sublist(Tail-Back, Head, Sublist-Back).
subtract(List-Back, _, Result) :-
unify_with_occurs_check(Result, Back-Back),
List == Back, !.
subtract(List-Back, Ys, List2-Back) :-
List \== Back,
List = [Head| Tail],
(member(Head, Ys) ->
subtract(Tail-Back, Ys, List2-Back)
;
List2 = [Head| Tail2],
subtract(Tail-Back, Ys, Tail2-Back)).
suffix(Suffix, List) :-
unify_with_occurs_check(Suffix, List).
suffix(Suffix-Back, List-Back) :-
List \== Back,
List = [_| Tail],
suffix(Suffix-Back, Tail-Back).
valid(List) :-
nonvar(List),
valid2(List).
valid2(List-Back) :-
List == Back,
!.
valid2(List-Back) :-
nonvar(List),
List = [_| Tail],
valid2(Tail-Back).
:- end_object.

View File

@@ -0,0 +1,30 @@
:- protocol(event_handlersp).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'Event handlers protocol.']).
:- public(before/3).
:- mode(before(?object, ?nonvar, ?object), zero_or_one).
:- info(before/3, [
comment is 'Before event handler.',
argnames is ['Object', 'Message', 'Sender']]).
:- public(after/3).
:- mode(after(?object, ?nonvar, ?object), zero_or_one).
:- info(after/3, [
comment is 'After event handler.',
argnames is ['Object', 'Message', 'Sender']]).
:- end_protocol.

View File

@@ -0,0 +1,51 @@
:- object(event_registry,
implements(event_registryp)).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'Before and after events registry predicates.']).
monitors(Monitors) :-
findall(
Monitor,
(current_event(before, _, _, _, Monitor),
current_event(after, _, _, _, Monitor)),
List),
{sort(List, Monitors)}.
monitored(Objects) :-
findall(
Object,
(current_event(before, Object, _, _, _),
current_event(after, Object, _, _, _)),
List),
{sort(List, Objects)}.
monitor(Object, Message, Sender, Monitor) :-
current_event(before, Object, Message, Sender, Monitor),
current_event(after, Object, Message, Sender, Monitor).
set_monitor(Object, Message, Sender, Monitor) :-
define_events(before, Object, Message, Sender, Monitor),
define_events(after, Object, Message, Sender, Monitor).
del_monitors(Object, Message, Sender, Monitor) :-
abolish_events(before, Object, Message, Sender, Monitor),
abolish_events(after, Object, Message, Sender, Monitor).
del_monitors :-
abolish_events(before, _, _, _, _),
abolish_events(after, _, _, _, _).
:- end_object.

View File

@@ -0,0 +1,65 @@
:- protocol(event_registryp).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'Event registry protocol.']).
:- public(monitors/1).
:- mode(monitors(-list), one).
:- info(monitors/1, [
comment is 'Returns a list of all current monitors.',
argnames is ['Monitors']]).
:- public(monitored/1).
:- mode(monitored(-list), one).
:- info(monitored/1, [
comment is 'Returns a list of all currently monitored objects.',
argnames is ['Objects']]).
:- public(monitor/4).
:- mode(monitor(?object, ?nonvar, ?object, ?object), zero_or_more).
:- info(monitor/4, [
comment is 'True if the arguments describe a currently defined monitored event.',
argnames is ['Object', 'Message', 'Sender', 'Monitor']]).
:- public(set_monitor/4).
:- mode(set_monitor(?object, ?nonvar, ?object, +object), zero_or_one).
:- info(set_monitor/4, [
comment is 'Sets a monitor for the set of matching events.',
argnames is ['Object', 'Message', 'Sender', 'Monitor']]).
:- public(del_monitors/4).
:- mode(del_monitors(?object, ?nonvar, ?object, ?object), one).
:- info(del_monitors/4, [
comment is 'Deletes all matching monitored events.',
argnames is ['Object', 'Message', 'Sender', 'Monitor']]).
:- public(del_monitors/0).
:- mode(del_monitors, one).
:- info(del_monitors/0, [
comment is 'Deletes all monitored events.']).
:- end_protocol.

View File

@@ -0,0 +1,7 @@
:- initialization(
logtalk_load([
event_handlersp,
event_registryp, event_registry,
before_event_registry, after_event_registry,
monitorp, monitor])).

View File

@@ -0,0 +1,22 @@
=================================================================
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 events.loader utility
file.
The objects event_registry, before_event_registry, and after_event_registry
implement convenient predicates for registering before and after events.
The protocol event_handlersp declares the two basic event handler predicates
(before/3 and after/3). You will need to refer this protocol in your objects
if you need to use the super control structure (^^/1) with these predicates.
The monitor object implements more sophisticated event handling predicates.
Some of the objects assume that the used Prolog compiler defines the
usual sort/2 built-in predicate.

17
Logtalk/library/float.lgt Normal file
View File

@@ -0,0 +1,17 @@
:- object(float,
extends(number)).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'Floating point numbers data type predicates.']).
valid(Float) :-
float(Float).
:- end_object.

View File

@@ -0,0 +1,6 @@
:- initialization(
logtalk_load([
hierarchyp,
proto_hierarchyp, proto_hierarchy,
class_hierarchyp, class_hierarchy])).

View File

@@ -0,0 +1,13 @@
=================================================================
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 group consult the hierarchies.loader
utility file.
These objects implement reflection predicates over class/instance
and prototype hierarchies.

View File

@@ -0,0 +1,66 @@
:- protocol(hierarchyp).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'Common hierarchy protocol for prototype and class hierarchies.']).
:- public(ancestor/1).
:- mode(ancestor(?object), zero_or_more).
:- info(ancestor/1, [
comment is 'Returns, by backtracking, all object ancestors.',
argnames is ['Ancestor']]).
:- public(ancestors/1).
:- mode(ancestors(-list), one).
:- info(ancestors/1, [
comment is 'List of all object ancestors.',
argnames is ['Ancestors']]).
:- public(leaf/1).
:- mode(leaf(?object), zero_or_more).
:- info(leaf/1, [
comment is 'Returns, by backtracking, all object leaves.',
argnames is ['Leaf']]).
:- public(leaves/1).
:- mode(leaves(-list), one).
:- info(leaves/1, [
comment is 'List of all object leaves.',
argnames is ['Leaves']]).
:- public(descendant/1).
:- mode(descendant(?object), zero_or_more).
:- info(descendant/1, [
comment is 'Returns, by backtracking, all object descendants.',
argnames is ['Descendant']]).
:- public(descendants/1).
:- mode(descendants(-list), one).
:- info(descendants/1, [
comment is 'List of all object descendants.',
argnames is ['Descendants']]).
:- end_protocol.

View File

@@ -0,0 +1,46 @@
:- object(integer,
extends(number)).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'Integer data type predicates.']).
:- public(between/3).
:- mode(between(+integer, +integer, ?integer), zero_or_more).
:- info(between/3, [
comment is 'Returns integers in the interval defined by the two first arguments.',
argnames is ['Lower', 'Upper', 'Integer']]).
between(Lower, Upper, Integer) :-
integer(Lower),
integer(Upper),
(var(Integer) ->
Lower =< Upper,
generate(Lower, Upper, Integer)
;
integer(Integer),
Lower =< Integer,
Integer =< Upper).
generate(Lower, _, Lower).
generate(Lower, Upper, Integer) :-
Lower < Upper,
Next is Lower + 1,
generate(Next, Upper, Integer).
valid(Integer) :-
integer(Integer).
:- end_object.

253
Logtalk/library/list.lgt Normal file
View File

@@ -0,0 +1,253 @@
:- object(list,
implements(listp),
extends(compound)).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'List predicates.']).
:- public(as_difflist/2).
:- mode(as_difflist(+list, -list), one).
:- info(as_difflist/2,
[comment is 'Converts a list to a difference list.',
argnames is ['List', 'Diffist']]).
append([], List, List).
append([Head| Tail], List, [Head| Tail2]) :-
append(Tail, List, Tail2).
as_difflist([], Back-Back).
as_difflist([Head| Tail], [Head| Tail2]-Back) :-
as_difflist(Tail, Tail2-Back).
delete([], _, []).
delete([Head| Tail], Element, Remaining) :-
Head == Element ->
delete(Tail, Element, Remaining)
;
Remaining = [Head| Tail2],
delete(Tail, Element, Tail2).
delete_matches([], _, []).
delete_matches([Head| Tail], Element, Remaining) :-
\+ \+ Head = Element ->
delete_matches(Tail, Element, Remaining)
;
Remaining = [Head| Tail2],
delete_matches(Tail, Element, Tail2).
empty(List) :-
List == [].
flatten(List, Flatted) :-
flatten(List, [], Flatted).
flatten(Var, Tail, [Var| Tail]) :-
var(Var),
!.
flatten([], Flatted, Flatted) :-
!.
flatten([Head| Tail], List, Flatted) :-
!,
flatten(Tail, List, Aux),
flatten(Head, Aux, Flatted).
flatten(Head, Tail, [Head| Tail]).
keysort(List, Sorted) :-
{keysort(List, Sorted)}.
last([Head| Tail], Last) :-
last(Tail, Head, Last).
last([], Last, Last).
last([Head| Tail], _, Last) :-
last(Tail, Head, Last).
length(List, Length) :-
integer(Length) ->
Length >= 0,
make_list(Length, List)
;
length(List, 0, Length).
make_list(0, []):-
!.
make_list(N, [_| Tail]):-
M is N-1,
make_list(M, Tail).
length([], Length, Length).
length([_| Tail], Acc, Length) :-
Acc2 is Acc + 1,
length(Tail, Acc2, Length).
max([N| Ns], Max) :-
max(Ns, N, Max).
max([], Max, Max).
max([N| Ns], Aux, Max) :-
N @> Aux ->
max(Ns, N, Max)
;
max(Ns, Aux, Max).
member(Element, [Element| _]).
member(Element, [_| List]) :-
member(Element, List).
memberchk(Element, [Element| _]) :-
!.
memberchk(Element, [_| List]) :-
memberchk(Element, List).
min([N| Ns], Min) :-
min(Ns, N, Min).
min([], Min, Min).
min([N| Ns], Aux, Min) :-
N @< Aux ->
min(Ns, N, Min)
;
min(Ns, Aux, Min).
new([]).
nth(Nth, List, Element) :-
nth(Element, List, 1, Nth).
nth(Head, [Head| _], Position, Position).
nth(Head, [_| Tail], Count, Position) :-
Count2 is Count + 1,
nth(Head, Tail, Count2, Position).
permutation(List, Permutation) :-
same_length(List, Permutation),
permutation2(List, Permutation).
permutation2([], []).
permutation2(List, [Head| Tail]) :-
select(Head, List, Remaining),
permutation2(Remaining, Tail).
prefix([], _).
prefix([Element| Tail], [Element| Tail2]) :-
prefix(Tail, Tail2).
reverse(List, Reversed) :-
same_length(List, Reversed),
reverse(List, [], Reversed).
reverse([], Reversed, Reversed).
reverse([Head| Tail], List, Reversed) :-
reverse(Tail, [Head| List], Reversed).
same_length([], []).
same_length([_| Tail1], [_| Tail2]) :-
same_length(Tail1, Tail2).
select(Head, [Head| Tail], Tail).
select(Head, [Head2| Tail], [Head2| Tail2]) :-
select(Head, Tail, Tail2).
sort(List, Sorted) :-
{sort(List, Sorted)}.
sublist(List, List).
sublist(Sublist, [Head| Tail]):-
sublist(Tail, Head, Sublist).
sublist(Sublist, _, Sublist).
sublist([Head| Tail], _, Sublist):-
sublist(Tail, Head, Sublist).
sublist([Head| Tail], Element, [Element| Sublist]):-
sublist(Tail, Head, Sublist).
subtract([], _, []).
subtract([Head| Tail], List, Rest) :-
::memberchk(Head, List) ->
subtract(Tail, List, Rest)
;
Rest = [Head| Tail2],
subtract(Tail, List, Tail2).
suffix(List, List).
suffix(List, [_| Tail]) :-
suffix(List, Tail).
valid(List) :-
nonvar(List),
\+ \+ valid2(List).
valid2([]).
valid2([_| List]) :-
valid2(List).
:- end_object.

215
Logtalk/library/listp.lgt Normal file
View File

@@ -0,0 +1,215 @@
:- protocol(listp).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'List protocol.']).
:- public(append/3).
:- mode(append(?list, ?list, ?list), zero_or_more).
:- info(append/3, [
comment is 'Appends two lists.',
argnames is ['List1', 'List2', 'List']]).
:- public(delete/3).
:- mode(delete(+list, @term, ?list), one).
:- info(delete/3,
[comment is 'Deletes from a list all ocurrences of an element returning the list of remaining elements.',
argnames is ['List', 'Element', 'Remaining']]).
:- public(delete_matches/3).
:- mode(delete_matches(+list, @term, ?list), one).
:- info(delete_matches/3,
[comment is 'Deletes all matching elements from a list, returning the list of remaining elements.',
argnames is ['List', 'Element', 'Remaining']]).
:- public(empty/1).
:- mode(empty(@list), zero_or_one).
:- info(empty/1,
[comment is 'True if the argument is an empty list.',
argnames is ['List']]).
:- public(flatten/2).
:- mode(flatten(+list, -list), one).
:- info(flatten/2,
[comment is 'Flattens a list of lists into a list.',
argnames is ['List', 'Flatted']]).
:- public(keysort/2).
:- mode(keysort(+list, -list), one).
:- info(keysort/2,
[comment is 'Sorts a list of key-value pairs in ascending order.',
argnames is ['List', 'Sorted']]).
:- public(last/2).
:- mode(last(?list, ?term), zero_or_more).
:- info(last/2,
[comment is 'List last element (if it exists).',
argnames is ['List', 'Last']]).
:- public(length/2).
:- mode(length(?list, ?integer), zero_or_more).
:- info(length/2,
[comment is 'List length.',
argnames is ['List', 'Length']]).
:- public(max/2).
:- mode(max(+list, -term), zero_or_one).
:- info(max/2,
[comment is 'Determines the list maximum value using standard order. Fails if the list is empty.',
argnames is ['List', 'Maximum']]).
:- public(member/2).
:- mode(member(?term, ?list), zero_or_more).
:- info(member/2,
[comment is 'Element is a list member.',
argnames is ['Element', 'List']]).
:- public(memberchk/2).
:- mode(memberchk(?term, ?list), zero_or_one).
:- info(memberchk/2,
[comment is 'Checks if a term is a member of a list.',
argnames is ['Element', 'List']]).
:- public(min/2).
:- mode(min(+list, -term), zero_or_one).
:- info(min/2,
[comment is 'Determines the minimum value in a list using standard order. Fails if the list is empty.',
argnames is ['List', 'Minimum']]).
:- public(nth/3).
:- mode(nth(?integer, +list, ?term), zero_or_more).
:- info(nth/3, [
comment is 'Nth element of a list.',
argnames is ['Nth', 'List', 'Element']]).
:- public(permutation/2).
:- mode(permutation(?list, ?list), zero_or_more).
:- info(permutation/2,
[comment is 'The two lists are a permutation of the same list.',
argnames is ['List', 'Permutation']]).
:- public(prefix/2).
:- mode(prefix(?list, +list), zero_or_more).
:- info(prefix/2,
[comment is 'Prefix is a prefix of List.',
argnames is ['Prefix', 'List']]).
:- public(reverse/2).
:- mode(reverse(+list, ?list), zero_or_one).
:- mode(reverse(?list, +list), zero_or_one).
:- mode(reverse(-list, -list), one_or_more).
:- info(reverse/2,
[comment is 'Reverses a list.',
argnames is ['List', 'Reversed']]).
:- public(same_length/2).
:- mode(same_length(+list, ?list), zero_or_one).
:- mode(same_length(?list, +list), zero_or_one).
:- mode(same_length(-list, -list), one_or_more).
:- info(same_length/2,
[comment is 'The two lists have the same length.',
argnames is ['List1', 'List2']]).
:- public(select/3).
:- mode(select(?term, +list, ?list), zero_or_more).
:- mode(select(?term, ?list, +list), zero_or_more).
:- info(select/3,
[comment is 'Selects an element from a list, returning the list of remaining elements.',
argnames is ['Element', 'List', 'Remaining']]).
:- public(sort/2).
:- mode(sort(+list, -list), one).
:- info(sort/2,
[comment is 'Sorts a list in ascending order.',
argnames is ['List', 'Sorted']]).
:- public(sublist/2).
:- mode(sublist(?list, +list), zero_or_more).
:- info(sublist/2,
[comment is 'The first list is a sublist of the second.',
argnames is ['Sublist', 'List']]).
:- public(subtract/3).
:- mode(subtract(+list, +list, -list), one).
:- info(subtract/3,
[comment is 'Removes all elements in the second list from the first list, returning the list of remaining elements.',
argnames is ['List', 'Elements', 'Remaining']]).
:- public(suffix/2).
:- mode(suffix(?list, +list), zero_or_more).
:- info(suffix/2,
[comment is 'Suffix is a suffix of List.',
argnames is ['Suffix', 'List']]).
:- end_protocol.

75
Logtalk/library/loop.lgt Normal file
View File

@@ -0,0 +1,75 @@
:- object(loop,
implements(loopp)).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'Loop control structures predicates.']).
:- metapredicate(dowhile(::, ::)).
:- metapredicate(forto(*, *, ::)).
:- metapredicate(forto(*, *, *, ::)).
:- metapredicate(fordownto(*, *, ::)).
:- metapredicate(fordownto(*, *, *, ::)).
:- metapredicate(whiledo(::, ::)).
dowhile(Action, Condition) :-
\+ \+ call(Action),
whiledo(Condition, Action).
whiledo(Condition, Action) :-
call(Condition) ->
\+ \+ call(Action),
whiledo(Condition, Action)
;
true.
forto(First, Last, Call) :-
First =< Last ->
\+ \+ call(Call),
Next is First + 1,
forto(Next, Last, Call)
;
true.
forto(Count, First, Last, Call) :-
First =< Last ->
\+ \+ (Count = First, call(Call)),
Next is First + 1,
forto(Count, Next, Last, Call)
;
true.
fordownto(First, Last, Call) :-
First >= Last ->
\+ \+ call(Call),
Next is First - 1,
fordownto(Next, Last, Call)
;
true.
fordownto(Count, First, Last, Call) :-
First >= Last ->
\+ \+ (Count = First, call(Call)),
Next is First - 1,
fordownto(Count, Next, Last, Call)
;
true.
:- end_object.

72
Logtalk/library/loopp.lgt Normal file
View File

@@ -0,0 +1,72 @@
:- protocol(loopp).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'Loop control structures protocol.']).
:- public(dowhile/2).
:- metapredicate(dowhile(::, ::)).
:- mode(dowhile(+callable, @callable), zero_or_one).
:- info(dowhile/2, [
comment is 'Do Action while Condition is true.',
argnames is ['Action', 'Condition']]).
:- public(forto/3).
:- metapredicate(forto(*, *, ::)).
:- mode(forto(+integer, +integer, @callable), zero_or_one).
:- info(forto/3, [
comment is 'Counting from First to Last do Call.',
argnames is ['First', 'Last', 'Call']]).
:- public(forto/4).
:- metapredicate(forto(*, *, *, ::)).
:- mode(forto(-integer, +integer, +integer, @callable), zero_or_one).
:- info(forto/4, [
comment is 'Do Call counting from First to Last and instantiating Count to each sucessive value.',
argnames is ['Count', 'First', 'Last', 'Call']]).
:- public(fordownto/3).
:- metapredicate(fordownto(*, *, ::)).
:- mode(fordownto(+integer, +integer, @callable), zero_or_one).
:- info(fordownto/3, [
comment is 'Counting from First to Last do Call.',
argnames is ['First', 'Last', 'Call']]).
:- public(fordownto/4).
:- metapredicate(fordownto(*, *, *, ::)).
:- mode(fordownto(-integer, +integer, +integer, @callable), zero_or_one).
:- info(fordownto/4, [
comment is 'Do Call counting from First to Last and instantiating Count to each sucessive value.',
argnames is ['Count', 'First', 'Last', 'Call']]).
:- public(whiledo/2).
:- metapredicate(whiledo(::, ::)).
:- mode(whiledo(+callable, @callable), zero_or_one).
:- info(whiledo/2, [
comment is 'While Condition is true do Action.',
argnames is ['Condition', 'Action']]).
:- end_protocol.

86
Logtalk/library/meta.lgt Normal file
View File

@@ -0,0 +1,86 @@
:- object(meta,
implements(metap)).
:- info([
version is 1,
date is 2000/7/24,
authors is 'Paulo Moura',
comment is 'Useful meta-predicates.']).
:- private(apply/3).
:- metapredicate(apply(*, *, ::)).
:- mode(apply(+callable, +list, -callable), zero_or_more).
:- info(apply/3, [
comment is 'Applies a predicate to list of arguments.',
argnames is ['Predicate', 'Arguments', 'Goal']]).
apply(Pred, Args) :-
apply(Pred, Args, _).
apply(Pred, Args, Goal) :-
(atom(Pred) ->
Goal =.. [Pred| Args]
;
Pred =.. Old,
append(Old, Args, New),
Goal =.. New),
call(Goal).
append([], List, List).
append([Head| Tail], List, [Head| Tail2]) :-
append(Tail, List, Tail2).
callable(Term) :-
nonvar(Term),
functor(Term, Functor, _),
atom(Functor).
filter(Pred, In, Out) :-
filter2(In, Pred, Out).
filter2([], _, []).
filter2([Arg| Args], Pred, List) :-
(apply(Pred, [Arg], _) ->
List = [Arg| Args2]
;
List = Args2),
filter2(Args, Pred, Args2).
map(Pred, In, Out) :-
map2(In, Pred, Out).
map2([], _, []).
map2([Old| Olds], Pred, [New| News]) :-
apply(Pred, [Old, New], _),
map2(Olds, Pred, News).
succeeds(Pred, List) :-
succeeds2(List, Pred).
succeeds2([], _).
succeeds2([Head| Tail], Pred) :-
apply(Pred, [Head], _),
succeeds2(Tail, Pred).
:- end_object.

57
Logtalk/library/metap.lgt Normal file
View File

@@ -0,0 +1,57 @@
:- protocol(metap).
:- info([
version is 2,
date is 2000/7/24,
authors is 'Paulo Moura',
comment is 'Useful meta-predicates protocol.']).
:- public(apply/2).
:- mode(apply(+callable, +list), zero_or_more).
:- info(apply/2, [
comment is 'Applies a predicate to list of arguments.',
argnames is ['Predicate', 'List']]).
:- public(callable/1).
:- mode(callable(@term), zero_or_one).
:- info(callable/1, [
comment is 'True if the argument can be called as a goal.',
argnames is ['Term']]).
:- public(filter/3).
:- mode(filter(+callable, +list, -list), one).
:- info(filter/3, [
comment is 'Returns a list of all list elements that satisfy a predicate using apply/2.',
argnames is ['Predicate', 'In', 'Out']]).
:- public(map/3).
:- mode(map(+callable, ?list, ?list), zero_or_more).
:- info(map/3, [
comment is 'Maps a predicate over a list of elements using apply/2.',
argnames is ['Predicate', 'In', 'Out']]).
:- public(succeeds/2).
:- mode(succeeds(+callable, +list), zero_or_more).
:- info(succeeds/2, [
comment is 'True if the predicate succeeds for each list element using apply/2.',
argnames is ['Predicate', 'List']]).
:- end_protocol.

View File

@@ -0,0 +1,5 @@
:- initialization(
logtalk_load([
metap, meta,
loopp, loop])).

View File

@@ -0,0 +1,17 @@
=================================================================
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 group consult the metapredicates.loader
utility file.
The object loop implements several kinds of loops typical of imperative
languages.
The object meta implements common Prolog metapredicates like apply/2
and map/3.

View File

@@ -0,0 +1,64 @@
:- category(monitor,
implements(monitorp)).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'Monitor predicates.']).
:- private(spy_point_/4).
:- dynamic(spy_point_/4).
:- mode(spy_point_(?event, ?object, ?callable, ?object), zero_or_more).
:- info(spy_point_/4, [
comment is 'Stores current spy points.',
argnames is ['Event', 'Object', 'Message', 'Sender']]).
monitor_activated :-
self(Self),
once(current_event(_, _, _, _, Self)).
activate_monitor :-
self(Self),
abolish_events(before, _, _, _, Self),
abolish_events(after, _, _, _, Self),
forall(
::spy_point_(Event, Object, Message, Sender),
define_events(Event, Object, Message, Sender, Self)).
suspend_monitor :-
self(Self),
abolish_events(before, _, _, _, Self),
abolish_events(after, _, _, _, Self).
reset_monitor :-
self(Self),
abolish_events(before, _, _, _, Self),
abolish_events(after, _, _, _, Self).
::retractall(spy_point_(_, _, _, _)).
spy_point(Event, Object, Message, Sender) :-
::spy_point_(Event, Object, Message, Sender).
set_spy_point(Event, Object, Message, Sender) :-
::retractall(spy_point_(Event, Object, Message, Sender)),
once((var(Event); Event = before; Event = after)),
::assertz(spy_point_(Event, Object, Message, Sender)).
del_spy_points(Event, Object, Message, Sender) :-
::retractall(spy_point_(Event, Object, Message, Sender)).
:- end_category.

View File

@@ -0,0 +1,71 @@
:- protocol(monitorp).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'Monitor protocol.']).
:- public(monitor_activated/0).
:- mode(monitor_activated, zero_or_one).
:- info(monitor_activated/0, [
comment is 'True if monitor is currently active.']).
:- public(activate_monitor/0).
:- mode(activate_monitor, one).
:- info(activate_monitor/0, [
comment is 'Activates all spy points and start monitoring.']).
:- public(suspend_monitor/0).
:- mode(suspend_monitor, one).
:- info(suspend_monitor/0, [
comment is 'Suspends monitoring, deactivating all spy points.']).
:- public(reset_monitor/0).
:- mode(reset_monitor, one).
:- info(reset_monitor/0, [
comment is 'Resets monitor, deactivating and deleting all spy points.']).
:- public(spy_point/4).
:- mode(spy_point(?event, ?object, ?callable, ?object), zero_or_more).
:- info(spy_point/4, [
comment is 'Current spy point.',
argnames is ['Event', 'Object', 'Message', 'Sender']]).
:- public(set_spy_point/4).
:- mode(set_spy_point(?event, ?object, ?callable, ?object), one).
:- info(set_spy_point/4, [
comment is 'Sets a spy point.',
argnames is ['Event', 'Object', 'Message', 'Sender']]).
:- public(del_spy_points/4).
:- mode(del_spy_points(@event, @object, @callable, @object), one).
:- info(del_spy_points/4, [
comment is 'Deletes all matching spy points.',
argnames is ['Event', 'Object', 'Message', 'Sender']]).
:- end_protocol.

View File

@@ -0,0 +1,24 @@
:- object(natural,
extends(integer)).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'Natural numbers data type predicates.']).
between(Lower, Upper, Integer) :-
integer(Lower),
Lower > 0,
^^between(Lower, Upper, Integer).
valid(Natural) :-
integer(Natural),
Natural > 0.
:- end_object.

View File

@@ -0,0 +1,17 @@
:- object(number,
extends(atomic)).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'Number data type predicates.']).
valid(Number) :-
number(Number).
:- end_object.

View File

@@ -0,0 +1,76 @@
:- object(numberlist,
implements(numberlistp),
extends(list)).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'List of numbers predicates.']).
average([], 0).
average([N| Ns], Average) :-
average([N| Ns], 0, 0, Average).
average([], Length, Sum, Average) :-
Average is Sum / Length.
average([N| Ns], Lacc, Sacc, Average) :-
Lacc2 is Lacc + 1,
Sacc2 is Sacc + N,
average(Ns, Lacc2, Sacc2, Average).
min([N| Ns], Min) :-
min(Ns, N, Min).
min([], Min, Min).
min([N| Ns], Aux, Min) :-
N < Aux ->
min(Ns, N, Min)
;
min(Ns, Aux, Min).
max([N| Ns], Max) :-
max(Ns, N, Max).
max([], Max, Max).
max([N| Ns], Aux, Max) :-
N > Aux ->
max(Ns, N, Max)
;
max(Ns, Aux, Max).
sum(List, Sum) :-
sum(List, 0, Sum).
sum([], Sum, Sum).
sum([N| Ns], Acc, Sum) :-
Acc2 is Acc + N,
sum(Ns, Acc2, Sum).
valid(List) :-
nonvar(List),
\+ \+ valid2(List).
valid2([]).
valid2([Head| Tail]) :-
number(Head),
valid2(Tail).
:- end_object.

View File

@@ -0,0 +1,30 @@
:- protocol(numberlistp).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'List of numbers protocol.']).
:- public(sum/2).
:- mode(sum(+list, ?number), zero_or_one).
:- info(sum/2,
[comment is 'Calculates the sum of all list values.',
argnames is ['List', 'Sum']]).
:- public(average/2).
:- mode(average(+list, ?number), zero_or_one).
:- info(average/2,
[comment is 'Calculates the average of a list of values.',
argnames is ['List', 'Average']]).
:- end_protocol.

View File

@@ -0,0 +1,82 @@
:- category(proto_hierarchy,
implements(proto_hierarchyp)).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'Prototype hierarchy predicates.']).
parent(Parent) :-
self(Self),
extends_object(Self, Parent).
parents(Parents) :-
self(Self),
findall(Parent, extends_object(Self, Parent), Parents).
ancestor(Ancestor) :-
self(Self),
ancestor(Self, Ancestor).
ancestor(Self, Ancestor) :-
extends_object(Self, Ancestor).
ancestor(Self, Ancestor) :-
extends_object(Self, Parent),
ancestor(Parent, Ancestor).
ancestors(Ancestors) :-
self(Self),
findall(Ancestor, ancestor(Self, Ancestor), Ancestors).
leaf(Leaf) :-
self(Self),
leaf(Self, Leaf).
leaf(Self, Leaf) :-
extends_object(Leaf, Self),
\+ extends_object(_, Leaf).
leaf(Self, Leaf) :-
extends_object(Object, Self),
leaf(Object, Leaf).
leaves(Leaves) :-
self(Self),
findall(Leaf, leaf(Self, Leaf), Leaves).
descendant(Descendant) :-
self(Self),
descendant(Self, Descendant).
descendant(Self, Descendant) :-
extends_object(Descendant, Self).
descendant(Self, Descendant) :-
extends_object(Descendant, Self),
\+ extends_object(Descendant, Self).
descendant(Self, Descendant) :-
extends_object(Subclass, Self),
descendant(Subclass, Descendant).
descendants(Descendants) :-
self(Self),
findall(Descendant, descendant(Self, Descendant), Descendants).
:- end_category.

View File

@@ -0,0 +1,31 @@
:- protocol(proto_hierarchyp,
extends(hierarchyp)).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'Prototype hierarchy protocol.']).
:- public(parent/1).
:- mode(parent(?object), zero_or_more).
:- info(parent/1, [
comment is 'Returns, by backtracking, all object parents.',
argnames is ['Parent']]).
:- public(parents/1).
:- mode(parents(-list), one).
:- info(parents/1, [
comment is 'List of all object parents.',
argnames is ['Parents']]).
:- end_protocol.

88
Logtalk/library/queue.lgt Normal file
View File

@@ -0,0 +1,88 @@
:- object(queue,
implements(queuep),
extends(compound)).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'Queue predicates implemented using difference lists.']).
as_list(Queue-Back, List) :-
Queue == Back ->
List = []
;
List = [Head| Tail],
Queue = [Head| Rest],
as_list(Rest-Back, Tail).
empty(Front-Back) :-
Front == Back.
head(Front-Back, Head) :-
Front \== Back,
Front = [Head| _].
join(Element, Front-[Element| Back], Front-Back).
join_all([], Queue, Queue).
join_all([Head| Tail], Queue1, Queue3) :-
join(Head, Queue1, Queue2),
join_all(Tail, Queue2, Queue3).
jump(Element, Front-Back, [Element| Front]-Back).
jump_all([], Queue, Queue).
jump_all([Head| Tail], Queue1, Queue3) :-
jump(Head, Queue1, Queue2),
jump_all(Tail, Queue2, Queue3).
length(Front-Back, Length) :-
length(Front, Back, 0, N),
Length = N.
length(Front, Back, N, N) :-
Front == Back, !.
length([_|Front], Back, K, N) :-
L is K+1,
length(Front, Back, L, N).
new(Back-Back).
serve(OldFront-Back, Head, NewFront-Back) :-
OldFront \== Back,
OldFront = [Head| NewFront].
valid(Queue) :-
nonvar(Queue),
valid2(Queue).
valid2(Queue-Back) :-
Queue == Back,
!.
valid2(Queue-Back) :-
nonvar(Queue),
Queue = [_| Tail],
valid2(Tail-Back).
:- end_object.

View File

@@ -0,0 +1,93 @@
:- protocol(queuep).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'Queue protocol.']).
:- public(empty/1).
:- mode(empty(@queue), zero_or_one).
:- info(empty/1, [
comment is 'True if the queue is empty.',
argnames is ['Queue']]).
:- public(head/2).
:- mode(head(+queue, ?term), zero_or_one).
:- info(head/2, [
comment is 'Unifies Head with the first element of the queue.',
argnames is ['Queue', 'Head']]).
:- public(join/3).
:- mode(join(@term, +queue, -queue), zero_or_one).
:- info(join/3, [
comment is 'Adds the new element at the end of the queue.',
argnames is ['Element', 'Queue_in', 'Queue_out']]).
:- public(join_all/3).
:- mode(join_all(+list, +queue, -queue), zero_or_one).
:- info(join_all/3, [
comment is 'Adds the new elements at the end of the queue. The elements are added in the same order that they appear in the list.',
argnames is ['List', 'Queue_in', 'Queue_out']]).
:- public(jump/3).
:- mode(jump(@term, +queue, -queue), zero_or_one).
:- info(jump/3, [
comment is 'Adds the new element at the front of the queue.',
argnames is ['Element', 'Queue_in', 'Queue_out']]).
:- public(jump_all/3).
:- mode(jump_all(+list, +queue, -queue), zero_or_one).
:- info(jump_all/3, [
comment is 'Adds the new elements at the front of the queue. The elements are added in the same order that they appear in the list.',
argnames is ['Element', 'Queue_in', 'Queue_out']]).
:- public(length/2).
:- mode(length(+queue, ?integer), zero_or_one).
:- info(length/2,
[comment is 'Queue length.',
argnames is ['Queue', 'Length']]).
:- public(serve/3).
:- mode(serve(+queue, ?term, -queue), zero_or_one).
:- info(serve/3, [
comment is 'Removes the first element of the queue for service.',
argnames is ['Queue_in', 'Head', 'Queue_out']]).
:- public(as_list/2).
:- mode(as_list(+queue, -list), one).
:- info(as_list/2,
[comment is 'Converts a queue to a list.',
argnames is ['Queue', 'List']]).
:- end_protocol.

170
Logtalk/library/random.lgt Normal file
View File

@@ -0,0 +1,170 @@
:- object(random,
implements(randomp)).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'Random number generator predicates.']).
:- initialization(reset_seed).
:- private(seed_/3).
:- dynamic(seed_/3).
:- mode(seed_(+integer, +integer, +integer), one).
:- info(seed_/3, [
comment is 'Stores the current random generator seed values.',
argnames is ['S0', 'S1', 'S2']]).
random(Random) :-
retract(seed_(A0, A1, A2)),
random(A0, A1, A2, B0, B1, B2, Random),
asserta(seed_(B0, B1, B2)).
random(A0, A1, A2, B0, B1, B2, Random) :-
B0 is (A0*171) mod 30269,
B1 is (A1*172) mod 30307,
B2 is (A2*170) mod 30323,
Float is A0/30269 + A1/30307 + A2/30323,
Random is Float - truncate(Float).
random(Lower, Upper, Random) :-
integer(Lower),
integer(Upper),
Upper >= Lower,
!,
random(Float),
Random is truncate((Float*(Upper-Lower)+Lower)).
random(Lower, Upper, Random) :-
float(Lower),
float(Upper),
Upper >= Lower,
random(Float),
Random is Float*(Upper-Lower)+Lower.
randseq(Length, Lower, Upper, Sequence) :-
integer(Length),
Length >= 0,
integer(Lower),
integer(Upper),
Upper >= Lower,
!,
retract(seed_(A0, A1, A2)),
randseq(Length, Lower, Upper, (A0, A1, A2), (B0, B1, B2), [], List),
asserta(seed_(B0, B1, B2)),
map_truncate(List, Sequence).
randseq(Length, Lower, Upper, Sequence) :-
integer(Length),
Length >= 0,
float(Lower),
float(Upper),
Upper >= Lower,
retract(seed_(A0, A1, A2)),
randseq(Length, Lower, Upper, (A0, A1, A2), (B0, B1, B2), [], Sequence),
asserta(seed_(B0, B1, B2)).
randseq(0, _, _, Seed, Seed, List, List) :-
!.
randseq(N, Lower, Upper, (A0, A1, A2), (C0, C1, C2), Acc, List) :-
N2 is N - 1,
random(A0, A1, A2, B0, B1, B2, R),
Random is R*(Upper-Lower)+Lower,
randseq(N2, Lower, Upper, (B0, B1, B2), (C0, C1, C2), [Random| Acc], List).
map_truncate([], []).
map_truncate([Float| Floats], [Integer| Integers]) :-
Integer is truncate(Float),
map_truncate(Floats, Integers).
randset(Length, Lower, Upper, Set) :-
integer(Length),
Length >= 0,
integer(Lower),
integer(Upper),
Upper >= Lower,
Length =< Upper - Lower,
!,
retract(seed_(A0, A1, A2)),
randset(Length, Lower, Upper, (A0, A1, A2), (B0, B1, B2), [], Set),
asserta(seed_(B0, B1, B2)).
randset(Length, Lower, Upper, Set) :-
integer(Length),
Length >= 0,
float(Lower),
float(Upper),
Upper >= Lower,
retract(seed_(A0, A1, A2)),
randset(Length, Lower, Upper, (A0, A1, A2), (B0, B1, B2), [], Set),
asserta(seed_(B0, B1, B2)).
randset(0, _, _, Seed, Seed, List, List) :-
!.
randset(N, Lower, Upper, (A0, A1, A2), (C0, C1, C2), Acc, List) :-
N2 is N - 1,
random(A0, A1, A2, B0, B1, B2, Float),
Float2 is Float*(Upper-Lower)+Lower,
(integer(Lower) ->
Random is truncate(Float2)
;
Random is Float2),
(not_member(Acc, Random) ->
add_ordered(Acc, Random, Acc2),
randset(N2, Lower, Upper, (B0, B1, B2), (C0, C1, C2), Acc2, List)
;
randset(N, Lower, Upper, (B0, B1, B2), (C0, C1, C2), Acc, List)).
not_member([], _).
not_member([H| T], R) :-
H =\= R,
not_member(T, R).
add_ordered([], R, [R]).
add_ordered([H| T], R, L) :-
H > R ->
L = [R, H| T]
;
L = [H| T2],
add_ordered(T, R, T2).
reset_seed :-
retractall(seed_(_, _, _)),
asserta(seed_(3172, 9814, 20125)).
set_seed(Seed) :-
integer(Seed),
Seed > 0,
retractall(seed_(_, _, _)),
S0 is Seed mod 30269,
S1 is Seed mod 30307,
S2 is Seed mod 30323,
asserta(seed_(S0, S1, S2)).
:- end_object.

View File

@@ -0,0 +1,5 @@
:- initialization(
logtalk_load([
randomp,
random])).

View File

@@ -0,0 +1,12 @@
=================================================================
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 random.loader utility
file.
The object random implements a basic random number generator.

View File

@@ -0,0 +1,68 @@
:- protocol(randomp).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'Random number generator protocol.']).
:- public(random/1).
:- mode(random(-float), one).
:- info(random/1, [
comment is 'Returns a new random float value in the interval [0.0, 1.0[.',
argnames is ['Random']]).
:- public(random/3).
:- mode(random(+integer, +integer, -integer), zero_or_one).
:- mode(random(+float, +float, -float), zero_or_one).
:- info(random/3, [
comment is 'Returns a new random value in the interval [Lower, Upper[.',
argnames is ['Lower', 'Upper', 'Random']]).
:- public(randseq/4).
:- mode(randseq(+integer, +integer, +integer, -list), zero_or_one).
:- mode(randseq(+integer, +float, +float, -list), zero_or_one).
:- info(randseq/4, [
comment is 'Returns a list of Length random values in the interval [Lower, Upper[.',
argnames is ['Length', 'Lower', 'Upper', 'List']]).
:- public(randset/4).
:- mode(randset(+integer, +integer, +integer, -list), zero_or_one).
:- mode(randset(+integer, +float, +float, -list), zero_or_one).
:- info(randset/4, [
comment is 'Returns an ordered set of Length random values in the interval [Lower, Upper[.',
argnames is ['Length', 'Lower', 'Upper', 'Set']]).
:- public(reset_seed/0).
:- mode(reset_seed, one).
:- info(reset_seed/0, [
comment is 'Resets the random seed to its default value.']).
:- public(set_seed/1).
:- mode(set_seed(+integer), zero_or_one).
:- info(set_seed/1, [
comment is 'Sets the random seed to the given value.',
argnames is ['Seed']]).
:- end_protocol.

263
Logtalk/library/set.lgt Normal file
View File

@@ -0,0 +1,263 @@
:- object(set,
implements(setp),
extends(compound)).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'Set predicates implemented using ordered lists. Uses ==/2 for element comparison and standard term ordering.']).
delete([], _, []).
delete([Head| Tail], Element, Remaining) :-
compare(Order, Head, Element),
delete(Order, Head, Tail, Element, Remaining).
delete(=, _, Tail, _, Tail).
delete(<, Head, Tail, Element, [Head| Tail2]) :-
delete(Tail, Element, Tail2).
delete(>, _, Tail, _, Tail).
disjoint([], _) :- !.
disjoint(_, []) :- !.
disjoint([Head1| Tail1], [Head2| Tail2]) :-
compare(Order, Head1, Head2),
disjoint(Order, Head1, Tail1, Head2, Tail2).
disjoint(<, _, Tail1, Head2, Tail2) :-
disjoint(Tail1, [Head2| Tail2]).
disjoint(>, Head1, Tail1, _, Tail2) :-
disjoint([Head1| Tail1], Tail2).
equal(Set1, Set2) :-
Set1 == Set2.
empty(Set) :-
Set == [].
insert([], Element, [Element]).
insert([Head| Tail], Element, Set) :-
compare(Order, Head, Element),
insert(Order, Head, Tail, Element, Set).
insert(<, Head, Tail, Element, [Head| Set]) :-
insert(Tail, Element, Set).
insert(=, Head, Tail, _, [Head| Tail]).
insert(>, Head, Tail, Element, [Element, Head| Tail]).
insert_all([], Set, Set).
insert_all([Head| Tail], Set1, Set3) :-
insert(Set1, Head, Set2),
insert_all(Tail, Set2, Set3).
intersect([Head1| Tail1], [Head2| Tail2]) :-
compare(Order, Head1, Head2),
intersect(Order, Head1, Tail1, Head2, Tail2).
intersect(=, _, _, _, _).
intersect(<, _, Tail1, Head2, Tail2) :-
intersect(Tail1, [Head2| Tail2]).
intersect(>, Head1, Tail1, _, Tail2) :-
intersect([Head1| Tail1], Tail2).
intersection(_, [], []) :- !.
intersection([], _, []) :- !.
intersection([Head1| Tail1], [Head2| Tail2], Intersection) :-
compare(Order, Head1, Head2),
intersection(Order, Head1, Tail1, Head2, Tail2, Intersection).
intersection(=, Head, Tail1, _, Tail2, [Head| Intersection]) :-
intersection(Tail1, Tail2, Intersection).
intersection(<, _, Tail1, Head2, Tail2, Intersection) :-
intersection(Tail1, [Head2| Tail2], Intersection).
intersection(>, Head1, Tail1, _, Tail2, Intersection) :-
intersection([Head1|Tail1], Tail2, Intersection).
length(Set, Length) :-
length(Set, 0, Length).
length([], Length, Length).
length([_| Set], Acc, Length) :-
Acc2 is Acc + 1,
length(Set, Acc2, Length).
member(Element, Set) :-
var(Element) ->
member_var(Element, Set)
;
member_nonvar(Element, Set).
member_var(Element, [Element| _]).
member_var(Element, [_| Set]) :-
member_var(Element, Set).
member_nonvar(Element, [Head| Tail]):-
compare(Order, Element, Head),
member_nonvar(Order, Element, Tail).
member_nonvar(=, _, _).
member_nonvar(>, Element, [Head| Tail]) :-
compare(Order, Element, Head),
member_nonvar(Order, Element, Tail).
new([]).
powerset(Set, PowerSet):-
reverse(Set, RSet),
powerset_1(RSet, [[]], PowerSet).
powerset_1([], PowerSet, PowerSet).
powerset_1([X| Xs], Yss0, Yss):-
powerset_2(Yss0, X, Yss1),
powerset_1(Xs, Yss1, Yss).
powerset_2([], _, []).
powerset_2([Zs| Zss], X, [Zs, [X| Zs]| Yss]):-
powerset_2(Zss, X, Yss).
reverse(List, Reversed) :-
reverse(List, [], Reversed).
reverse([], Reversed, Reversed).
reverse([Head| Tail], List, Reversed) :-
reverse(Tail, [Head| List], Reversed).
select(Head, [Head| Tail], Tail).
select(Head, [Head2| Tail], [Head2| Tail2]) :-
select(Head, Tail, Tail2).
subset([], _) :- !.
subset([Head1| Tail1], [Head2| Tail2]) :-
compare(Order, Head1, Head2),
subset(Order, Head1, Tail1, Head2, Tail2).
subset(=, _, Tail1, _, Tail2) :-
subset(Tail1, Tail2).
subset(>, Head1, Tail1, _, Tail2) :-
subset([Head1| Tail1], Tail2).
subtract(Set, [], Set) :- !.
subtract([], _, []) :- !.
subtract([Head1| Tail1], [Head2| Tail2], Difference) :-
compare(Order, Head1, Head2),
subtract(Order, Head1, Tail1, Head2, Tail2, Difference).
subtract(=, _, Tail1, _, Tail2, Difference) :-
subtract(Tail1, Tail2, Difference).
subtract(<, Head1, Tail1, Head2, Tail2, [Head1| Difference]) :-
subtract(Tail1, [Head2| Tail2], Difference).
subtract(>, Head1, Tail1, _, Tail2, Difference) :-
subtract([Head1| Tail1], Tail2, Difference).
symdiff(Set, [], Set) :- !.
symdiff([], Set, Set) :- !.
symdiff([Head1| Tail1], [Head2| Tail2], Difference) :-
compare(Order, Head1, Head2),
symdiff(Order, Head1, Tail1, Head2, Tail2, Difference).
symdiff(=, _, Tail1, _, Tail2, Difference) :-
symdiff(Tail1, Tail2, Difference).
symdiff(<, Head1, Tail1, Head2, Tail2, [Head1| Difference]) :-
symdiff(Tail1, [Head2| Tail2], Difference).
symdiff(>, Head1, Tail1, Head2, Tail2, [Head2| Difference]) :-
symdiff([Head1| Tail1], Tail2, Difference).
union(Set, [], Set) :- !.
union([], Set, Set) :- !.
union([Head1| Tail1], [Head2| Tail2], Union) :-
compare(Order, Head1, Head2),
union(Order, Head1, Tail1, Head2, Tail2, Union).
union(=, Head, Tail1, _, Tail2, [Head| Union]) :-
union(Tail1, Tail2, Union).
union(<, Head1, Tail1, Head2, Tail2, [Head1| Union]) :-
union(Tail1, [Head2| Tail2], Union).
union(>, Head1, Tail1, Head2, Tail2, [Head2| Union]) :-
union([Head1| Tail1], Tail2, Union).
valid(Set) :-
nonvar(Set),
valid2(Set).
valid2([]) :-
!.
valid2([_]) :-
!.
valid2([Element1, Element2| Set]) :-
Element1 @< Element2,
valid2([Element2| Set]).
:- end_object.

158
Logtalk/library/setp.lgt Normal file
View File

@@ -0,0 +1,158 @@
:- protocol(setp).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'Set protocol.']).
:- public(delete/3).
:- mode(delete(+set, @term, ?set), one).
:- info(delete/3,
[comment is 'Deletes an element from a set returning the set of remaining elements.',
argnames is ['Set', 'Element', 'Remaining']]).
:- public(disjoint/2).
:- mode(disjoint(+set, +set), zero_or_one).
:- info(disjoint/2, [
comment is 'True if the two sets have no element in common.',
argnames is ['Set1', 'Set2']]).
:- public(equal/2).
:- mode(equal(+set, +set), zero_or_one).
:- info(equal/2, [
comment is 'True if the two sets are equal.',
argnames is ['Set1', 'Set2']]).
:- public(empty/1).
:- mode(empty(+set), zero_or_one).
:- info(empty/1, [
comment is 'True if the set is empty.',
argnames is ['Set']]).
:- public(insert/3).
:- mode(insert(+set, +term, ?set), one).
:- info(insert/3, [
comment is 'Inserts an element in a set, returning the resulting set.',
argnames is ['In', 'Element', 'Out']]).
:- public(insert_all/3).
:- mode(insert_all(+list, +set, ?set), one).
:- info(insert_all/3, [
comment is 'Inserts a list of elemnts in a set, returning the resulting set.',
argnames is ['List', 'In', 'Out']]).
:- public(intersect/2).
:- mode(intersect(+set, +set), zero_or_one).
:- info(intersect/2, [
comment is 'True if the two sets have at least one element in common.',
argnames is ['Set1', 'Set2']]).
:- public(intersection/3).
:- mode(intersection(+set, +set, ?set), zero_or_one).
:- info(intersection/3, [
comment is 'Returns the intersection of Set1 and Set2.',
argnames is ['Set1', 'Set2', 'Intersection']]).
:- public(length/2).
:- mode(length(+set, ?integer), zero_or_one).
:- info(length/2,
[comment is 'Number of set elements.',
argnames is ['Set', 'Length']]).
:- public(member/2).
:- mode(member(+term, +set), zero_or_one).
:- mode(member(-term, +set), zero_or_more).
:- info(member/2,
[comment is 'Element is a member of set Set.',
argnames is ['Element', 'Set']]).
:- public(powerset/2).
:- mode(powerset(+set, -list), one).
:- info(powerset/2,
[comment is 'Returns the power set of a set, represented as a list of sets.',
argnames is ['Set', 'Powerset']]).
:- public(select/3).
:- mode(select(?term, +set, ?set), zero_or_more).
:- info(select/3,
[comment is 'Selects an element from a set, returning the set of remaining elements.',
argnames is ['Element', 'Set', 'Remaining']]).
:- public(subset/2).
:- mode(subset(+set, +set), zero_or_one).
:- mode(subset(?set, +set), zero_or_more).
:- info(subset/2, [
comment is 'True if Subset is a subset of Set.',
argnames is ['Subset', 'Set']]).
:- public(subtract/3).
:- mode(subtract(+set, +set, ?set), zero_or_one).
:- info(subtract/3, [
comment is 'True when Difference contains all and only the elements of Set1 which are not also in Set2.',
argnames is ['Set1', 'Set2', 'Difference']]).
:- public(symdiff/3).
:- mode(symdiff(+set, +set, ?set), zero_or_one).
:- info(symdiff/3, [
comment is 'True if Difference is the symmetric difference of Set1 and Set2.',
argnames is ['Set1', 'Set2', 'Difference']]).
:- public(union/3).
:- mode(union(+set, +set, ?set), zero_or_one).
:- info(union/3, [
comment is 'True if Union is the union of Set1 and Set2.',
argnames is ['Set1', 'Set2', 'Union']]).
:- end_protocol.

View File

@@ -0,0 +1,84 @@
:- protocol(systemp).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'Operating system protocol.']).
:- public(make_directory/1).
:- mode(make_directory(+atom), zero_or_one).
:- info(make_directory/1, [
comment is 'Make a new directory.',
argnames is ['Directory']]).
:- public(delete_directory/1).
:- mode(delete_directory(+atom), zero_or_one).
:- info(delete_directory/1, [
comment is 'Delete a directory.',
argnames is ['Directory']]).
:- public(change_directory/1).
:- mode(change_directory(+atom), zero_or_one).
:- info(change_directory/1, [
comment is 'Change working directory.',
argnames is ['Directory']]).
:- public(working_directory/1).
:- mode(working_directory(?atom), zero_or_one).
:- info(working_directory/1, [
comment is 'Current working directory.',
argnames is ['Directory']]).
:- public(directory_files/2).
:- mode(directory_files(+atom, -list), zero_or_one).
:- info(directory_files/2, [
comment is 'List of all directory files.',
argnames is ['Directory', 'Files']]).
:- public(file_exists/1).
:- mode(file_exists(+atom), zero_or_one).
:- info(file_exists/1, [
comment is 'True if the specified file exists.',
argnames is ['File']]).
:- public(delete_file/1).
:- mode(delete_file(?atom), zero_or_one).
:- info(delete_file/1, [
comment is 'Deletes a file.',
argnames is ['File']]).
:- public(rename_file/1).
:- mode(rename_file(?atom), zero_or_one).
:- info(rename_file/1, [
comment is 'Renames a file.',
argnames is ['File']]).
:- end_protocol.

131
Logtalk/library/term.lgt Normal file
View File

@@ -0,0 +1,131 @@
:- object(term,
implements(termp)).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'Prolog term predicates.']).
ground(Term) :-
nonvar(Term),
functor(Term, _, Arity),
ground(Arity, Term).
ground(0, _) :-
!.
ground(N, Term) :-
N > 0,
arg(N, Term, Arg),
ground(Arg),
N2 is N - 1,
ground(N2, Term).
occurs(Var, Term) :-
var(Term) ->
Var == Term
;
functor(Term, _, Arity),
occurs(Arity, Var, Term).
occurs(N, Var, Term) :-
compound(Term),
arg(N, Term, Arg),
occurs(Var, Arg),
!.
occurs(N, Var, Term) :-
N > 1,
N2 is N - 1,
occurs(N2, Var, Term).
subsumes(General, Specific) :-
vars(Specific, Vars),
subsumes(General, Specific, Vars).
subsumes(General, Specific, Vars) :-
var(General),
!,
(var_member_chk(General, Vars) ->
General == Specific
;
General = Specific).
subsumes(General, Specific, Vars) :-
nonvar(Specific),
functor(General, Functor, Arity),
functor(Specific, Functor, Arity),
subsumes(Arity, General, Specific, Vars).
subsumes(0, _, _, _) :- !.
subsumes(N, General, Specific, Vars) :-
arg(N, General, GenArg),
arg(N, Specific, SpeArg),
subsumes(GenArg, SpeArg, Vars),
M is N-1, !,
subsumes(M, General, Specific, Vars).
var_member_chk(Var, [Head| Tail]) :-
Var == Head ->
true
;
var_member_chk(Var, Tail).
subterm(Term, Term).
subterm(Sub, Term) :-
nonvar(Term),
functor(Term, _, N),
subterm(N, Sub, Term).
subterm(N, Sub, Term) :-
compound(Term),
arg(N, Term, Arg),
subterm(Sub, Arg).
subterm(N, Sub, Term) :-
N > 1,
M is N-1,
subterm(M, Sub, Term).
valid(_).
vars(Term, Vars) :-
vars(Term, [], Vars).
vars(Term, Acc, Vars) :-
var(Term) ->
(var_member_chk(Term, Acc) ->
Vars = Acc
;
Vars = [Term| Acc])
;
Term =.. [_| Args],
var_list(Args, Acc, Vars).
var_list([], Vars, Vars).
var_list([Term| Terms], Acc, Vars) :-
vars(Term, Acc, Acc2),
var_list(Terms, Acc2, Vars).
:- end_object.

75
Logtalk/library/termp.lgt Normal file
View File

@@ -0,0 +1,75 @@
:- protocol(termp).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'Prolog terms protocol.']).
:- public(ground/1).
:- mode(ground(@term), zero_or_one).
:- info(ground/1, [
comment is 'True if the argument is ground.',
argnames is ['Term']]).
:- public(new/1).
:- mode(new(-nonvar), zero_or_one).
:- info(new/1, [
comment is 'Creates a new term instance (if meaningful).',
argnames is ['Term']]).
:- public(occurs/2).
:- mode(occurs(@var, @term), zero_or_one).
:- info(occurs/2, [
comment is 'True if the variable occurs in the term.',
argnames is ['Variable', 'Term']]).
:- public(subsumes/2).
:- mode(subsumes(@term, @term), zero_or_one).
:- info(subsumes/2, [
comment is 'The first term subsumes the second term.',
argnames is ['General', 'Specific']]).
:- public(subterm/2).
:- mode(subterm(?term, +term), zero_or_more).
:- info(subterm/2, [
comment is 'The first term is a subterm of the second term.',
argnames is ['Subterm', 'Term']]).
:- public(valid/1).
:- mode(valid(@nonvar), zero_or_one).
:- info(valid/1, [
comment is 'Term is valid.',
argnames is ['Term']]).
:- public(vars/2).
:- mode(vars(@term, -list), one).
:- info(vars/2, [
comment is 'Returns a list of all term variables.',
argnames is ['Term', 'List']]).
:- end_protocol.

27
Logtalk/library/time.lgt Normal file
View File

@@ -0,0 +1,27 @@
:- object(time,
implements(timep)).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'Time predicates.']).
now(Hours, Mins, Secs) :-
{lgt_current_time(Hours, Mins, Secs)}.
cpu_time(Seconds) :-
{lgt_cpu_time(Seconds)}.
valid(Hours, Mins, Secs) :-
integer(Hours), Hours >= 0,
integer(Mins), Mins >= 0, Mins =< 59,
integer(Secs), Secs >= 0, Secs =< 59.
:- end_object.

39
Logtalk/library/timep.lgt Normal file
View File

@@ -0,0 +1,39 @@
:- protocol(timep).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'Time protocol.']).
:- public(now/3).
:- mode(now(-integer, -integer, -integer), one).
:- info(now/3, [
comment is 'Returns current time.',
argnames is ['Hours', 'Mins', 'Secs']]).
:- public(cpu_time/1).
:- mode(cpu_time(-number), one).
:- info(cpu_time/1,
[comment is 'Returns the current cpu time.',
argnames is ['Time']]).
:- public(valid/3).
:- mode(valid(+integer, +integer, +integer), zero_or_one).
:- info(valid/3, [
comment is 'True if the arguments represent a valid time value.',
argnames is ['Hours', 'Mins', 'Secs']]).
:- end_protocol.

View File

@@ -0,0 +1,16 @@
:- initialization(
logtalk_load([
termp, term,
atomic,
atom, callable,
characterp, character,
number, float, integer, natural,
compound,
listp, list, difflist,
numberlistp, numberlist,
varlist,
queuep, queue,
dictionaryp, bintree,
setp, set,
comparingp])).

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 group consult the types.loader utility
file.
These objects implement predicates over common Prolog terms and
structures like lists, diffrence lists, binary trees, dictionaries,
and queues.

View File

@@ -0,0 +1,46 @@
:- object(varlist,
extends(list)).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 2000/7/24,
comment is 'List of variables predicates.']).
member(Element, [Head| _]) :-
Element == Head.
member(Element, [_| Tail]) :-
member(Element, Tail).
memberchk(Element, [Head| Tail]) :-
Element == Head ->
true
;
memberchk(Element, Tail).
prefix([], _).
prefix([Head1| Tail1], [Head2| Tail2]) :-
Head1 == Head2,
prefix(Tail1, Tail2).
valid(List) :-
nonvar(List),
\+ \+ valid2(List).
valid2([]).
valid2([Head| Tail]) :-
var(Head),
valid2(Tail).
:- end_object.