El Dj
El Dj

Reputation: 385

Prolog: subsets facts not working

I've never written in Prolog before. I have to provide facts so that when it runs it displays:

    ?- subset([a,b],[a,c,d,b]).
    true.
    ?-include([],[a,b]).
    true.

So far, I've written this:

subset([],_Y). 
subset([X|T],Y):- member(X,Y),subset(T,Y).

But include doesn't work when I write include([],[a,b]). . It shows this:

ERROR: toplevel: Undefined procedure: include/2 (DWIM could not correct goal)

Any help would be appreciated. Thanks

Upvotes: 3

Views: 275

Answers (1)

tas
tas

Reputation: 8140

You get the error because you didn't define the predicate include/2. Your given example looks like include/2 should be describing the same relation as subset/2. So you can either rename your definition from subset/2 to include/2 and then run the query or you can use subset/2 to define include/2:

include(X,Y) :-
   subset(X,Y).

Note that in order to use member/2 you have to use library(lists). However, in some Prolog systems (e.g. SWI) this library includes a predicate subset/2 thus leading to a warning when you consult your source file:

Warning: ...
Local definition of user:subset/2 overrides weak import from lists

If you want to implement your own version of subset/2 anyway and not get this warning, you can rename your predicate or not use library(lists) and implement your version of member/2, for example:

subset([],_Y). 
subset([X|T],Y) :-
   element_in(X,Y),
   subset(T,Y).

element_in(X,[X|_]).
element_in(X,[Y|Ys]) :-
   dif(X,Y),
   element_in(X,Ys).

Upvotes: 3

Related Questions