Introduction to Prolog setof
Prolog setof is defined as, the prolog has in-built predicate ‘setof’ which is used to collect the objects together that gets from successful computation, it is a combination of the term(template), goal and list or set where the term is a variable which contained in the list, the goal is a callable term that means it calling the undefined procedure and list is a set of variables, it also binds or attaches the set to the ordered list of all instances of term so that goal is satisfied, the condition in setof may fail if the goal does not have a solution.
The syntax of setof is given below:
“setof(term, goal, list)”
The ‘setof(term, goal, list)’ succeeds, when the list is non-empty and the term of instances is in the ordered form then the goal gets executed.
- term: This is the prolog term that has all forms of data, it is similar to the variable.
- goal: This is a callable term.
- list: It is a collection of terms that contains the instance of the variable, it is non-empty and must be in sorted order.
How setof works in Prolog?
- The ‘setof’ is a built-in predicate in prolog, it gives a sorted form of variables without duplicates because it does not allow the duplicate values. The syntax of setof is ‘setof(term, goal, list)’, we can call it setof/3. The setof/3 has three conditions which are helpful to find the values and organize them in ordered form so that the goal gets succeeds. The setof/3 is also called as ‘bagof/3’ which has the arguments, the first argument term is used to collect items, second is the condition, and the third argument is the list to collect into it, if we consider the first argument employee name as a variable then by putting conditions we will get the list of all employee names in the third argument.
- The setof/3 works very much like findall/3, findall/3 is also an in-built predicate in prolog like setof/3, the setof/3 produces the set of all results, any duplicates can be removed in this case and it will give result in sorted form. If we have a variable which is used in the goal argument and that variable did not appear in the first argument of the set then the separate result will get setof/3 for each possible instantiation of that variable.
Let us take an example to illustrate the working of an in-built predicate setof.
This is a set of facts having the name and their ages in which both name and ages are not sorted, names are also not sorted alphabetically, some facts are repeated so let us apply in-built predicate setof.
setof(Child, age(Child, Age), Results).
In the above line, the results will form a list with the first argument Child and Child is the first argument in the respective facts, then for the similar ages we shall form a list in the output with the Child names, so the list will be.
Age = 6,
Results = [anni, tomy] ?;
Age = 8,
Results = [piter] ? ;
Age = 9,
Results = [pit] ?;
Here we can see that the output comes in a sorted form and also the resultant names in the lists are also in sorted form and another thing is that the repeated data will also come in sorted it means it not getting repeated in output, so this is the purpose of the setof.
We can use a nested call to setof/3 to collect together the individual results.
- setof(Age/Student, setof(Stud, age(Stud, age), Student ), AllResult).
AllResult = [6/[anni, tomy], 8/[piter], 9/[pit]] yes
If we do not care about a variable that does not appear in the first argument, we can use the following form.
- setof(Child, Age^age(Child, Age), Results).
Results = [anni ,pit ,piter ,tomy] no,
This can be read as to find the set of all children, such that the Child has an age, whatever it may be, and put the results in Results.
In this way the setof works on sets, we can give inputs as per the condition then setof takes those conditions and worked on it and it provides the output.
Examples of Prolog setof
Given below are the examples mentioned:
main :- write('The setof predicate').
good(M) :- bad(M).
setof(N, good(N), Set).
In the above program, we have written code for the use of ‘setof’ predicate and we have written one fact ‘good(sharry).’ and from the fact and rule we shown that ‘sharry’ is good and sharry will appears only once and also in sorted order.
main :- write('Hello prolog').
1) setof(X, Y^age(X,Y), Res).
3) setof(X/Y, setof(Child,age(Child,X), Y), AllRes).
1. In the above program when we give input ‘setof(X, Y^age(X,Y), Res).’ Then the output will be the name list and that is alphabetically sorted as shown in the screenshot
2. When we give input ‘setof(Y, X^age(X,Y),Res).’ Then the output will their ages in sorted order and that does not provide the repeated values, such as.
3. And now if we give nested input such as ‘setof(X/Y, setof(Child,age(Child,X), Y), AllRes).’, then it gives an output in the ordered form of numbers and also follows the alphabetical order, as we see there are no duplicates allowed. As we can see in out the names anju, annu and tommy have same ages so due to that, it provides under one list.
In the above article, we conclude that in prolog programming language we work with sets that do not have the repeated elements, as duplicates are not allowed in sets, we also conclude that ‘setof’ is an important tool that we can use to calculate sets by having the goals and arguments.
This is a guide to Prolog setof. Here we discuss the introduction, how setof work in Prolog? and examples for better understanding. You may also have a look at the following articles to learn more –