prolog sets problem, stack overflow

Posted by garm0nboz1a on Stack Overflow See other posts from Stack Overflow or by garm0nboz1a
Published on 2011-02-14T15:42:15Z Indexed on 2011/03/18 0:09 UTC
Read the original article Hit count: 159

Filed under:
|
|

Hi. I'm gonna show some code and ask, what could be optimized and where am I sucked?

sublist([], []).
sublist([H | Tail1], [H | Tail2]) :-
    sublist(Tail1, Tail2).
sublist(H, [_ | Tail]) :-
    sublist(H, Tail).

less(X, X, _).
less(X, Z, RelationList) :-
    member([X,Z], RelationList).
less(X, Z, RelationList) :-
    member([X,Y], RelationList),
    less(Y, Z, RelationList),
    \+less(Z, X, RelationList).

lessList(X, LessList, RelationList) :-
    findall(Y, less(X, Y, RelationList), List),
    list_to_set(List, L),
    sort(L, LessList), !.

list_mltpl(List1, List2, List) :-
    findall(X, (
        member(X, List1),
        member(X, List2)),
    List).

chain([_], _).
chain([H,T | Tail], RelationList) :-
    less(H, T, RelationList),
    chain([T|Tail], RelationList),
    !.

have_inf(X1, X2, RelationList) :-
    lessList(X1, X1_cone, RelationList),
    lessList(X2, X2_cone, RelationList),
    list_mltpl(X1_cone, X2_cone, Cone),
    chain(Cone, RelationList),
    !.

relations(List, E) :-
    findall([X1,X2],
        (member(X1, E),
        member(X2, E),
        X1 =\= X2),
    Relations),
    sublist(List, Relations).

semilattice(List, E) :-
    forall(
        (member(X1, E),
        member(X2, E),
        X1 < X2),
    have_inf(X1, X2, List)
    ).

main(E) :-
    relations(X, E),
    semilattice(X, E).

I'm trying to model all possible graph sets of N elements. Predicate relations(List, E) connects list of possible graphs(List) and input set E. Then I'm describing semilattice predicate to check relations' List for some properties.

So, what I have.

1) semilattice/2 is working fast and clear

?- semilattice([[1,3],[2,4],[3,5],[4,5]],[1,2,3,4,5]).
true.

?- semilattice([[1,3],[1,4],[2,3],[2,4],[3,5],[4,5]],[1,2,3,4,5]).
false.

2) relations/2 is working not well

?- findall(X, relations(X,[1,2,3,4]), List), length(List, Len), writeln(Len),fail.
4096
false.

?- findall(X, relations(X,[1,2,3,4,5]), List), length(List, Len), writeln(Len),fail.
ERROR: Out of global stack
^  Exception: (11) setup_call_catcher_cleanup('$bags':'$new_findall_bag'(17852886), '$bags':fa_loop(_G263, user:relations(_G263, [1, 2, 3, 4|...]), 17852886, _G268, []), _G835, '$bags':'$destroy_findall_bag'(17852886)) ? abort
% Execution Aborted

3) Mix of them to finding all possible semilattice does not work at all.

?- main([1,2]).
ERROR: Out of local stack
^  Exception: (15) setup_call_catcher_cleanup('$bags':'$new_findall_bag'(17852886), '$bags':fa_loop(_G41, user:less(1, _G41, [[1, 2], [2, 1]]), 17852886, _G52, []), _G4767764, '$bags':'$destroy_findall_bag'(17852886)) ? 

© Stack Overflow or respective owner

Related posts about prolog

Related posts about algebra