简体   繁体   中英

clauses for the same predicate should be grouped

Here I want to make a prolog implementation that will generate the list of permutations of n,n+1,...2*n-1, having the absolute difference between 2 consecutive values <=2. Ex: for n=4 the list on which I make the permutations would be [4,5,6,7] , a valid permutation would be [5,4,6,7] and not a valid one would be [7,4,6,5] because 7-4 is 3 . The problem is that I get the following error clauses for the same predicate should be grouped . I don't understand what I did wrong..Please help me

domains
    el=integer
    list=el*
    lista=list*
predicates
    perm(list,list)
    permutations(integer,list)
    delete(integer,list,list)
    generate(integer,lista)
    create(integer,list)
    create_list(integer,integer,list)
    permutations_aux(integer,list)
    diff(list,integer).
clauses
    create(N,L):-
        M = 2*N,
        create_list(N,M,L).

    create_list(N,M,[N|R]):-
        N<M,
        N1=N+1,
        create_list(N1,M,R).

    create_list(M,M,[]).

    perm([],[]).
    perm(Y,[A|X]):-
        delete(A,Y,Y1),
        perm(Y1,X).

    delete(A,[A|X],X).
    delete(A,[B|X],[B|Y]):-
        delete(A,X,Y).

    permutations(N,X):-
        create(N,X1),
        perm(X1,X).
        diff(X,2).

    permutations_aux(N,X):-
        permutations(N,X).

    diff([],_).
    diff([_],_).
    diff([X,Y|T],M):-
        abs(Y-X) <=M,!,
        diff([Y|T],M).

    generate(N,R):-
        findall(X,permutations_aux(N,X),R).


goal
generate(4,P),
write(P).

The error or warning:

Clauses for the same predicate should be grouped

Means that you have interleaved definition of different clauses. In other words, clauses for a predicate with the same name should all be defined together in a group. Such as:

foo(X) :-
    some stuff
foo(Y) :-
    some stuff

bar(X) :-
    some stuff
bar(Y) :-
    some stuff

You will get the warning/error if you do:

foo(X) :-
    some stuff

bar(X) :-
    some stuff

foo(Y) :-
    some stuff

bar(Y) :-
    some stuff

Your error is that you have a period after perm(X1,X). in your permutations clause. It should be a comma:

permutations(N,X):-
    create(N,X1),
    perm(X1,X).      <--- Error here
    diff(X,2).

permutations_aux(N,X):-
    permutations(N,X).

diff([],_).

So it really thinks you have:

permutations(N,X):-
    create(N,X1),
    perm(X1,X).

diff(X,2).

permutations_aux(N,X):-
    permutations(N,X).

diff([],_).

Which is an interleave of definitions for diff .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM