INFS3450 RMU C&IS

Logic Programming Set Operations in Prolog

The SWI Prolog documentation (see http://www.swi-prolog.org/ for details) identifies the following SWI Prolog built in predicates relating to set manipulation:

  • is_set (set in; in Prolog sets are lists without duplicates; the fact that Prolog orders lists should be disregarded in considering these structures sets).

Example 1 (succeeds, note list/set may be specified without order):

1 ?- is_set([2,4,5,3,1]).

 

Yes

Example 2 (fails):

10 ?- is_set([1,7,8,2,4,1]).

 

No

  • list_to_set (list in, set out; eliminates duplicates)
    Example 1:

2 ?- list_to_set([upitt, rmu, cmu, upitt], X).

 

X = [upitt, rmu, cmu]

 

Yes

  • intersection (set 1 in, set 2 in, intersection-result out) Example 1:

3 ?- intersection([1,3,7,8,9,11],[2,4,8,12,7],X).

 

X = [7, 8]

 

Yes

  • subtract (set 1 in, set 2 in, set-difference result out) Example 1:

4 ?- subtract([1,2,3,4,5],[2,4,6],X).

 

X = [1, 3, 5]

 

Yes

  • union (set 1 in, set 2 in, union-result out) Example 1:

5 ?- union([1,2,3,4,5],[3,5,6,7,8],X).

 

X = [1, 2, 4, 3, 5, 6, 7, 8]

 

Yes

  • subset (proposed subset in, proposed superset in)
    Example 1 (succeeds):

6 ?- subset([1,2,7],[1,2,3,4,5,6,7]).

 

Yes

Example 2 (fails):

7 ?- subset([1,2,7],[1,2,3,4,8]).

 

No

SWI Prolog licensed under GNU Public License; Documentation, Jan Wielemaker, HCS group, University of Amsterdam
Contact for this page:

Valerie J. Harvey, RT(R), PhD, C&IS, RMU