@@ -2572,6 +2572,7 @@ Guile log also sports an iso-prolog interface as a logic programming interface b

* modules:: How to handle name spacing

* scheme:: Hooking in scheme expressions

* functors:: map funcitons to functions

* sets:: set operations

* closures:: Using closures in prolog

* continuations:: Advanced management af what to continue with

* extended matching:: Matching extensions deviating from normal prolog

...

...

@@ -2937,6 +2938,299 @@ The othe uses the meta functor @code{functorize}, the signature is

Examples of functors and goal functors to use are @code{recursive}, @code{memo}, @code{rec}, @code{tabling} etc found in @code{(logic guile-log memoize)}, and also @code{adaptable_vars} that tells that all variables created in the function should use a default attribute constructor if defined, this defined in @code{(logic guile-log)}.

@node sets

@section sets and maps in prolog

We are going to explore how set's are handled in guile-log prolog. Lets start off simple.

To define a set just do:

@verbatim

X is {1,2,3}.

X ={1,2,3}

@end verbatim

We have added common set operators e.g.

@verbatim

% union e.g. all combined elements (cup in latex).

?- X is {1,2} ∪ {2,3}.

X ={1,2,3}.

% intersection, e.g. all elements located in both sets (cap in latex)

?- X is {1,2} ∩ {2,3}.

X ={2}.

% set addition e.g. all elements of the sets not located in both (oplus in latex)

?- X is {1,2} ⊕ {2,3}.

X ={1,3}.

% sed difference e.g. removing all elements in the second set from the first (setminus in latex).

?- X is {1,2} ∖ {2,3}.

X is {1}.

@end verbatim

A new set is not nessesary constructed at each operation, usually we take one of the arguments datastructure and modify that. Ouch you may say, well it's not that terrible. We are working with functional hashmaps that mimics a @code{cons} list e.g. @code{vhashes} E.g. we are building a new datastructure with the old one semantically untouched. Removing an element a is also not that terrible, just add a map @code{a->deleted} and only when there is a lot of fluff we reconstruct the whole map. This means that we can efficiently support a functional interface in stead of the standard mutable interface you see in languages like java and C#. Later we will se that new map bindings will override already stored maps. This is simple to implement using these functional hashmaps.

An element as a datastructure will be interpretted value wise, e.g. two lists with the same content will be equal. Any found prolog varibles will be interpreted as an adress identity. This means that if we have an object @code{X} put as a set member and it is later bound to 1, then the variable edentity will still remain and the key does not change. So the set element will be flatted into a scheme datastructure. In the future we will introduce a gard symbol so that you can do e.g. @code{X is @{[1,guard(X)]@}}, then with this @code{X} will be copied over verbatim. In all we recomend that varibles are not used as set elemets unless you really now what you are doing.

The first special addition we will make is that a det is a union of a finite set and the complement of a finite set e.g. @code{Aᶜ}(unicode superscript c) we can then define the set operators as,

@verbatim

(A∪Bᶜ) is (B∖A)ᶜ

(A∩Bᶜ) is (A∖B)

(Aᶜ∪Bᶜ) is (A∩B)ᶜ

(Aᶜ∩Bᶜ) is (A∪B)ᶜ

@end verbatim

@code{⊕,∖} follows from these relations. The conclusion is that any such set can indeed be represented as a generilized set where we define a generalized set as either @code{Set = A} or @code{Set = Aᶜ}. This is well defined. When one wants a a true set from this representatation one takes a world @code{Ω} and intersect it with theSet and for the complement you get @code{(Ω∖A)}.

The next addition to our set datastructure is to assume that the elements in the set can be a key value pair where the key is the usual set element, e.g. to enable the set to be a map. The basic set operations defined on this will always bind the key to the value located for this in the leftmost set where it is defined. We will see how to preserve this operation even for complemented sets, but's lets see the simple things,

@verbatim

% notise how the keys follow the set logic with the addition that leftmost

% value take precedence

X is {1-a,2-a}∪{2-b,3-b}.

X ={1-a,2-a,3-b}

X is {1-a,2-a}∩{2-b,3-b}.

X ={2-a}

@end verbatim

Now, there is two roads to go from here. Either we are satisfied or we can demand that the information of the complement for a set be recorded in case complements is used e.g. Basically we want this to be true,

@verbatim

% This shows that we want the elements in the complement to be saved later

% for use if used before the other operator.

X is {1-a}ᶜ, Y is Xᶜ ∩ {1-b}

X ={1-a}ᶜ.

Y ={1-a}.

% {1-a} is in the complement of X and the system remembers that in

% the calculation of Y.

X is {1-a,2-a}∩{2-b}, Y is Xᶜ ∩ {1-c}

X ={2-a}.

Y ={1-a}.

@end verbatim

If we first forget about complement, we could just focus on set's and simply for each map @code{A} also collect the information that have been skipt in the complement you would then simply have another set @code{B}, in the complement of @code{A} that contains the extra key value information that will later be materlized in taking a complement. We simply realize that we can use the notation @code{A = A∖B} and this is our notation which is both true acording to set theory and contains all extra information that is needed for retrieving correct values associated to the keys. The union and intersection becomes:

@verbatim

(A1∖B1)∩(A2∖B2)=(A1∩A2)∖(B1∪(A1⊕A2)∪B2).

(A1∖B1)∪(A2∖B2)=(A1∪A2)∖((B1∖A2)∪(B2∖A1)).

@end verbatim

Note that the order is important here, this means that basically every set element that has ever been touched uppon is stored in either @code{A} or @code{B} which is kind of expensive. Therefore this is an issue that is critical to control and it is possible to inhibit this feature. Therefore it is good to separate a generlized set/map in one set part and one map part and therfore we end up with the following representation:

@verbatim

% A is a pure map, B is a pure map and C is a pure set,

% The set representation is then A∖B∪C, with A∩B=∅,

When we finally add complemented sets we end up with the representation:

@verbatim

% A is a pure map, B is a pure map and C is a pure set,

% For repr A∖B∪C: A∩B=∅.

% For repr A∖B∪Cᶜ: A∩B=∅.

(A∖B ∪ Cᶜ)ᶜ =(B∪Aᶜ)∩C =(B∩C)∖(A∪(B∖C)) ∪ (C∖A)

(A∖B ∪ C)ᶜ =(B∪Aᶜ)∩Cᶜ =(B∖C)∖(A∪(B∩C))∪(A∪C)ᶜ

@end verbatim

And we see that the operation of complement is closed, e.g. taking the complement the representation still holds.

The intersection becomes:

@verbatim

(A1∖B1 ∪ C1) ∩ (A2∖B2 ∪ C2)=(

((A1∩A2)∪(A1∩C2)∪(A2∩C1))

∖ ((B1∪(A1⊕A2)∪B2)∖(A1∩C2)∖(A2∩C1)))

) ∪ (C1∩C2)

(A1∖B1 ∪ C1) ∩ (A2∖B2 ∪ C2ᶜ)=(

((A1∩A2)∪(A1∖C2)∪(A2∩C1))

∖ ((B1∪(A1⊕A2)∪B2)∖(A1∖C2)∖(A2∩C1)))

) ∪ (C1∖C2)

(A1∖B1 ∪ C1ᶜ) ∩ (A2∖B2 ∪ C2)=(

((A1∩A2)∪(A1∩C2)∪(A2∖C1))

∖ ((B1∪(A1⊕A2)∪B2)∖(A1∩C2)∖(A2∖C1)))

) ∪ (C2∖C1)

(A1∖B1 ∪ C1ᶜ) ∩ (A2∖B2 ∪ C2ᶜ)=(

((A1∩A2)∪(A1∖C2)∪(A2∖C1))

∖ ((B1∪(A1⊕A2)∪B2)∖(A1∖C2)∖(A2∖C1)))

) ∪ (C1∪C2)ᶜ

@end verbatim

E.g. The representation is closed under intersection.

Finally we need to check it for union as well e.g.

@verbatim

(A1∖B1 ∪ C1) ∪ (A2∖B2 ∪ C2)=(

(A1∪A2)

∖ ((B1∖A2)∪(B2∖A1))

) ∪ (C1 ∪ C2)

(A1∖B1 ∪ C1) ∪ (A2∖B2 ∪ C2ᶜ)=(

(A1∪A2)

∖ ((B1∖A2)∪(B2∖A1))

) ∪ (C2∖C1)ᶜ

(A1∖B1 ∪ C1ᶜ) ∪ (A2∖B2 ∪ C2)=(

(A1∪A2)

∖ ((B1∖A2)∪(B2∖A1))

) ∪ (C1∖C2)ᶜ

(A1∖B1 ∪ C1ᶜ) ∪ (A2∖B2 ∪ C2ᶜ)=(

(A1∪A2)

∖ ((B1∖A2)∪(B2∖A1))

) ∪ (C1∩C2)ᶜ

@end verbatim

And this is also closed. Because all other operations can be expressed from these operations the system is closed under all set operations.

Testing for an element etc is done through @code{∈}("in" in latex) , the semantics is easiest to show with examples:

@verbatim

?- X is {1,2-a,3-a}, K ∈ X.

K =2.

K =3.

K =1.

?- X is {1,2-a,3-a}, 1 ∈ X.

yes

?- X is {1,2-a,3-a}, 4 ∈ X.

no

?- X is {1,2-a,3-a}, 1-V ∈ X.

V = #f.

?- X is {1,2-a,3-a}, 2-V ∈ X.

V = a.

?- X is {1,2-a,3-a}, K-V ∈ X.

K =2,

V = a.

K =3,

V = a.

K =1,

V = #f.

@end verbatim

Now ordering of the entering objects is preserved for pure sets and pure map sets. And this is in the spec. Actually the underlying datastructure allows to tag a set element or a key value pair as unordered e.g. some optimisations can be done if we relax the ordering condition, taking advantage of this will come in the future. Let's in stead look at prolog code in action using the set framework. First @code{list_to_set}(defined in the module):

@verbatim

list_to_set(L,S) :-

reverse(L,LL),

list_to_set(LL,∅,S).

list_to_set([],S,S).

list_to_set([X|L],S,SS) :-

(

(\+var(X), X = K-V)->

S2 is {K-V} ∪ S;

S2 is {X} ∪ S

),

list_to_set(L,S2,SS).

?- list_to_set([1,2],S).

S ={1,2}.

@end verbatim

Simple as a pancake. Now here is a useful procedure, `mapset`:

@verbatim

mapset(S,Map,SS) :-

findall(V, (_-V ∈ (Map ∩ S), V \= #f),L),

list_to_set(L,SS).

?- X is {2,1},Map is {1-2,2-1,3-0}, mapset(X,Map,S).

S ={2,1}.

@end verbatim

Note that we take the intersection with @code{S} last. This will mean that @code{Map} decides the order of the resulting set, but in case @code{S} containes a key value pair @code{Map} take precidence and this is more important.

We define an @code{equal} function for sets, and this means that @code{=,==} etc do works as expected up to a certain level. The value part is not seen by @code{equal?} e.g.:

@verbatim

?- X is {1}, Y is {1-1}, X==Y.

yes

@end verbatim

This might be unsatisfying and it is unsure what to do, probably we will define a fluid that specify wha kind of equal we would like to have for set's (there is many thinkable variants).

Also worth noting is that testing to find that two sets are different are cheap, it is when two sets are equal, that generally we need to check @code{n} times. We do that by using a nifty hash and explore the properties of the xorfuntion. Namely:

@verbatim

xor(a1,a2,a3,...)= ? % does not depend of the order

xor(a1,a1)=0

xor(0 ,a )= a

@end verbatim

This means that maintaining the hash for deletion and addition is a simple one op. So a ~64bit hash is always maintaind for the set and they most often different for different sets. This means that set of sets is much more effective than what you first would think.

Finally we have the subset operators: @code{⊆,⊂}(subseteq, subset in latex) The second one test for subset or equal and the second one for a pure subset e.g:

<tr><tdalign="left"valign="top">•<ahref="functors.html#functors"accesskey="6">functors</a>:</td><td> </td><tdalign="left"valign="top">map funcitons to functions

</td></tr>

<tr><tdalign="left"valign="top">•<ahref="closures.html#closures"accesskey="7">closures</a>:</td><td> </td><tdalign="left"valign="top">Using closures in prolog

<tr><tdalign="left"valign="top">•<ahref="continuations.html#continuations"accesskey="8">continuations</a>:</td><td> </td><tdalign="left"valign="top">Advanced management af what to continue with

<tr><tdalign="left"valign="top">•<ahref="closures.html#closures"accesskey="8">closures</a>:</td><td> </td><tdalign="left"valign="top">Using closures in prolog

</td></tr>

<tr><tdalign="left"valign="top">•<ahref="extended-matching.html#extended-matching"accesskey="9">extended matching</a>:</td><td> </td><tdalign="left"valign="top">Matching extensions deviating from normal prolog

<tr><tdalign="left"valign="top">•<ahref="continuations.html#continuations"accesskey="9">continuations</a>:</td><td> </td><tdalign="left"valign="top">Advanced management af what to continue with

</td></tr>

<tr><tdalign="left"valign="top">•<ahref="extended-matching.html#extended-matching">extended matching</a>:</td><td> </td><tdalign="left"valign="top">Matching extensions deviating from normal prolog

</td></tr>

<tr><tdalign="left"valign="top">•<ahref="prolog_002ddynamic_002dfunctions.html#prolog_002ddynamic_002dfunctions">prolog-dynamic-functions</a>:</td><td> </td><tdalign="left"valign="top">A discussion of guile log’s version of this