Previous: , Up: prolog-libraries   [Index]


16.13.9 Prolog Extensions Of Findall Bagof Etc

There are quite a number of acumumulations and related constructs defined in guile-log’s prolog, we will discuss them here,

prolog findall(Template,Goal,Result), this will simply copy the template values for all solutions of goal and put it in result. prolog bagof(Template,Goal,Result), this will simply copy the template values for all solutions of goal and put it in result. any templates prefixed like Goal = A^B^Goal2 means that variables A,B will be free to change for each solution of Goal2 e.g. it will find the first acumulation of solutions with all other variables inside Goal2 fixed to the first solution, then at backtracking the next one will be shown etc, here is an example

prolog@(prolog-user)> bagof([X,Y],X^(between(1,3,X),between(1,3,Y)),L).

   Y = 1,
   L = [[1, 1], [2, 1], [3, 1]].
more (y/n/a/s) > y

   Y = 2,
   L = [[1, 2], [2, 2], [3, 2]].
more (y/n/a/s) > y

   Y = 3,
   L = [[1, 3], [2, 3], [3, 3]].
more (y/n/a/s) > y

no

prolog setof(Template,Goal,Result), is the same as bagof, but the fixed variables will not be duplicated if they are. Example

%This will generate two solution with the same X,Y values
prolog@(prolog-user)> setof([X,Y],X^(between(1,3,X),between(1,3,Y),(true;true)),L).

   Y = 1,
   L = [[1, 1], [2, 1], [3, 1]].
more (y/n/a/s) > y

   Y = 2,
   L = [[1, 2], [2, 2], [3, 2]].
more (y/n/a/s) > y

   Y = 3,
   L = [[1, 3], [2, 3], [3, 3]].
more (y/n/a/s) > y

no

The next section will document plain acumulations with numerics. The important adition is that template is evaluated which means that things like sumof(X*X,between(1,10,X),SumOfSquares) does the right thing. The acumulators are: prolog sumof(Expr,Goal,Result), the sum of all expressions, like bagof. prolog prodof(Expr,Goal,Result), the product of all expressions, like bagof. prolog countof(Goal,Result), the count of all solutions, like bagof. prolog maxof(Expr,Goal,Result), the maximum of all expressions, like bagof. prolog minof(Expr,Goal,Result), the minimum of all expressions, like bagof. prolog lastof(Template,Goal,Result), the last expression, like bagof. prolog foldof(Template,Goal,Result).

prolog sumall(Expr,Goal,Result), the sum of all expressions, like findall. prolog prodall(Expr,Goal,Result), the product of all expressions, like findall. prolog countall(Goal,Result), the count of all solutions, like findall. prolog maxall(Expr,Goal,Result), the maximum of all expressions, like findall. prolog minall(Expr,Goal,Result), the minimum of all expressions, like findall. prolog lastall(Template,Goal,Result), the last expression, like findall. prolog foldall(Template,Goal,Result).

The next section describes ideoms like forall that for each acumulated value the predicate suceeds and at backtracking the next value is tried. e.g.

prolog@(prolog-user)> sumstp(X*X,between(1,3,X),L).

   X = 1,
   L = 1.
more (y/n/a/s) > y

   X = 2,
   L = 5.
more (y/n/a/s) > y

   X = 3,
   L = 14.
more (y/n/a/s) > y

no

prolog sumstp(Expr,Goal,Result), the sum of all expressions. prolog prodstp(Expr,Goal,Result), the product of all expressions. prolog countstp(Goal,Result), the count of all solutions. prolog maxstp(Expr,Goal,Result), the maximum of all expressions. prolog minstp(Expr,Goal,Result), the minimum of all expressions. prolog foldstp(Template,Goal,Result).

There is class named as above with the suffix n. This add an extra element first which if an integer n then it will acumulat at most n times and if n == strict(m), then in order for it to sucess exactly m times and if less. So we get the interfaces example:

prolog@(prolog-user)> sumalln(3,X*X,between(1,3,X),L).

   L = 14.
prolog@(prolog-user)> sumalln(2,X*X,between(1,3,X),L).

   L = 5.
prolog@(prolog-user)> sumalln(10,X*X,between(1,3,X),L).

   L = 14.
prolog@(prolog-user)> sumalln(strict(3),X*X,between(1,3,X),L).

   L = 14.
prolog@(prolog-user)> sumalln(strict(2),X*X,between(1,3,X),L).

   L = 5.
prolog@(prolog-user)> sumalln(strict(4),X*X,between(1,3,X),L).

no

prolog sumalln(N,Expr,Goal,Result), the sum of all expressions. prolog prodalln(N,Expr,Goal,Result), the product of all expressions. prolog countalln(N,Goal,Result), the count of all solutions. prolog maxalln(N,Expr,Goal,Result), the maximum of all expressions. prolog minalln(N,Expr,Goal,Result), the minimum of all expressions. prolog lastalln(N,Expr,Goal,Result), the last of all expressions. prolog foldalln(N,Template,Goal,Result).

The same as previous chunk of interfaces but the stepwise version is of cause

prolog sumstpn(N,Expr,Goal,Result), the sum of all expressions. prolog prodstpn(N,Expr,Goal,Result), the product of all expressions. prolog counstpn(N,Goal,Result), the count of all solutions. prolog maxstpn(N,Expr,Goal,Result), the maximum of all expressions. prolog minstpn(N,Expr,Goal,Result), the minimum of all expressions. prolog foldstpn(N,Template,Goal,Result).

The sumof can be adjusted so that i the same solution appears twise then the solution is combined, e.g.

prolog@(prolog-user)> sumofs(X,member(X,[1,2,2,3]),L).

   X = 1,
   L = 1.
more (y/n/a/s) > y

   X = 2,
   L = 4.
more (y/n/a/s) > y

   X = 3,
   L = 3.
more (y/n/a/s) > y

no

prolog sumofs(Expr,Goal,Result), the sum of all expressions. prolog proofs(N,Expr,Goal,Result), the product of all expressions. prolog countofs(N,Goal,Result), the count of all solutions. prolog maxofs(N,Expr,Goal,Result), the maximum of all expressions. prolog minofs(N,Expr,Goal,Result), the minimum of all expressions. prolog foldofn(N,Template,Goal,Result).

The stepwise sumof are: prolog sumofn(Expr,Goal,Result), the sum of all expressions. prolog proofn(N,Expr,Goal,Result), the product of all expressions. prolog countofn(N,Goal,Result), the count of all solutions. prolog maxofn(N,Expr,Goal,Result), the maximum of all expressions. prolog minofn(N,Expr,Goal,Result), the minimum of all expressions. prolog foldofn(N,Template,Goal,Result).

And the combination of the s and n simply is

prolog sumofsn(Expr,Goal,Result), the sum of all expressions. prolog proofsn(N,Expr,Goal,Result), the product of all expressions. prolog countofsn(N,Goal,Result), the count of all solutions. prolog maxofsn(N,Expr,Goal,Result), the maximum of all expressions. prolog minofsn(N,Expr,Goal,Result), the minimum of all expressions. prolog foldofsn(N,Template,Goal,Result).

Af all examples above we also av an argument max and argument min version as well just replace sum with amax or amin bmax or bmin in the name of the predicate e.g. The return value is in the form MAX|ARGMAX with index starting at one and zero for the empty list. Example:

prolog@(prolog-user)> aminall(X,member(X,[2,4,4,1,1,3]),L).

   L = [1|4].
prolog@(prolog-user)> bminall(X,member(X,[2,4,4,1,1,3]),L).

   L = [1|5].
prolog@(prolog-user)> amaxall(X,member(X,[2,4,4,1,1,3]),L).

   L = [4|2].
prolog@(prolog-user)> bmaxall(X,member(X,[2,4,4,1,1,3]),L).

   L = [4|3].


Previous: , Up: prolog-libraries   [Index]