5

what is the source code of setof in prolog?

false
  • 10,264
  • 13
  • 101
  • 209
  • Are you looking for a particular implementation? I used SWI-Prolog for my answer, but I can give you another if you'd like. – Rafe Kettler Apr 16 '11 at 03:32

2 Answers2

7
?- listing(setof).
:- meta_predicate setof(?,0,-).

setof(A, B, F) :-
    free_variable_set(A, B, D, C),
    (   C==v
    ->  findall(A, D, E),
        E\==[],
        sort(E, F)
    ;   findall(C-A, D, E),
        (   ground(E)
        ->  sort(E, G),
        pick(G, C, F)
        ;   bind_bagof_keys(E, _),
        sort(E, G),
        pick(G, C, H),
        sort(H, F)
        )
    ).

true.
0

In case you are looking for the Sicstus built-in predicate implementation, it can be found here: http://www.sics.se/sicstus/docs/4.2.1/html/sicstus/mpg_002dref_002dsetof.html as:

setof(+Template, +Generator, -Set)

Unlike findall/3 and bagof/3, setof does not return duplicates and does give sorted order.

I.

panza
  • 1,341
  • 7
  • 38
  • 68