prologprolog-coroutining

Remove leading s(s(0)) in list


(This is a followup to that question).

How to write lead1(Xs,Ys) which is true, iff Ys is a suffix of Xs with all leading s(s(0)) terms removed. Thus instead of removing leading 0s this question is now about removing leading s(s(0))s.

Compared to the original question, the difficulty lies here in handling cases of s(X) and s(s(X)) properly.


Solution

  • Here's a version with if_/3 and =/3:

    list_suffix([],[]).
    list_suffix([X|Xs],S) :-
       if_(X=s(s(0)), list_suffix(Xs,S), S=[X|Xs]).
    

    Queries with a ground first argument succeed deterministically:

    ?- list_suffix([s(0)],S).
    S = [s(0)].
    
    ?- list_suffix([s(0),s(s(0))],S).
    S = [s(0), s(s(0))].
    
    ?- list_suffix([s(s(0)),s(0),s(s(0))],S).
    S = [s(0), s(s(0))].
    
    ?- list_suffix([s(s(0)), s(s(0)),s(0),s(s(0))],S).
    S = [s(0), s(s(0))].
    

    If the list consists of a term different from s/1, say f(_) the second list is identical to the first:

    ?- list_suffix([f(_)],S).
    S = [f(_G201)].
    
    ?- list_suffix([f(_)],[]).
    false.
    

    Partially instantiated lists work as well:

    ?- list_suffix([X, s(s(0)),s(0),s(s(0))],S).
    X = s(s(0)),
    S = [s(0), s(s(0))] ;
    S = [X, s(s(0)), s(0), s(s(0))],
    dif(X, s(s(0))).
    

    The most general query works as well but is listing the answer in an unfair manner:

    ?- list_suffix(X,Y).
    X = Y, Y = [] ;
    X = [s(s(0))],
    Y = [] ;
    X = [s(s(0)), s(s(0))],
    Y = [] ;
    X = [s(s(0)), s(s(0)), s(s(0))],
    Y = [] ;
    .
    .
    .
    

    However, this can be remedied by prefixing a goal length/2:

    ?- length(X,_), list_suffix(X,Y).
    X = Y, Y = [] ;
    X = [s(s(0))],
    Y = [] ;
    X = Y, Y = [_G155],
    dif(_G155, s(s(0))) ;
    X = [s(s(0)), s(s(0))],
    Y = [] ;
    X = [s(s(0)), _G79],
    Y = [_G79],
    dif(_G79, s(s(0))) ;
    X = Y, Y = [_G155, _G158],
    dif(_G155, s(s(0))) ;
    X = [s(s(0)), s(s(0)), s(s(0))],
    Y = [] ;
    .
    .
    .