Search code examples
prologpredicatewin-prolog

Prolog error: Predicate Protected


I'm new to prolog and I'm trying to write a predicate that returns "Yes" if two train stations are on the same line.

line(1,[a,b,c,d,e]);
line(2,[b,g,f,e,i,h]);
line(3,[l,m,g,n,f,o,p,q,i,j]);


same_line(X,Y):-
    line(_,L),
    member(X,L),
    member(Y,L).

Example: ?- same_line(n,j). Yes

However, I get this error in WIN-PROLOG when compiling: ! Error 67 : Predicate Protected

What am I doing wrong?


Solution

  • Just answering, so that the question goes off the list of unanswered questions: Yes the (;)/2 is a problem. Further if you really only want a yes or no, you could also try memberchk/2 instead of member/2. The code would read:

    line(1,[a,b,c,d,e]).
    line(2,[b,g,f,e,i,h]).
    line(3,[l,m,g,n,f,o,p,q,i,j]).
    
    same_linechk(X,Y):-
        line(_,L),
        memberchk(X,L),
        memberchk(Y,L).
    

    And works as expected:

    ?- same_linechk(n,j).
    true.
    

    The predicate memberchk/2 is part of SWI-Prolog and does not backtrack after the first time it has found a matching member. It is essentially a menber/2 with a builtin cut and thus in the average twice as fast.

    Bye