prologruledcg

How to represent "somewhere to the left" in prolog?


In the Prolog, how to represent the situation "somewhere to the left". For example, there is a List "List" and two terms "X" and "Y", how to represent the rule: X is somewhere to the left of Y in the List.


Solution

  • This can be reduced to the problem of subsequence matching.

    subsequence([], _).
    subsequence([X|Sub], [X|Seq]) :-
        subsequence(Sub, Seq).
    subsequence(Sub, [_|Seq]) :-
        subsequence(Sub, Seq).
    

    Then your "left-of" query would be subsequence([X, Y], List), !.