Reintegration Integration Issues

I suggested the following clpfd-based code for the recent Prolog List Separation question :

list_evens_odds([],[],[]).
list_evens_odds([X|Xs],[X|Es],Os) :-
   X mod 2 #= 0,
   list_evens_odds(Xs,Es,Os).
list_evens_odds([X|Xs],Es,[X|Os]) :-
   X mod 2 #= 1,
   list_evens_odds(Xs,Es,Os).

It is concise and clean, but can leave many unnecessary points of choice. Consider:

?- list_evens_odds([1,2,3,4,5,6,7],Es,Os).

The above query reserves a useless selection point for each odd item in [1,2,3,4,5,6,7].

Alternative implementation

Using the reification method demonstrated by @false in Prolog Aggregation for AUBUC can reduce the number of unnecessary selection points. Implementation may change to:

list_evens_odds([],[],[]).
list_evens_odds([X|Xs],Es,Os) :-
   if_(#<=>(X mod 2 #= 0), (Es=[X|Es0],Os=   Os0),
                           (Es=   Es0, Os=[X|Os0])),
   list_evens_odds(Xs,Es0,Os0).

For direct interaction with clpfd-reification, the implementation if_/3can be adapted as follows:

if_( C_1, Then_0, Else_0) :-
   call(C_1,Truth01),
   indomain(Truth01),
   ( Truth01 == 1 -> Then_0 ; Truth01 == 0, Else_0 ).

Of course, (=)/3it will also be necessary to adapt to this agreement.

Bottom row

: 0 1 false true ?

? ! !

+4
3

SWI-Prolog zcompare/3:

:- use_module(library(clpfd)).

list_evens_odds([], [], []).
list_evens_odds([X|Xs], Es, Os) :-
      Mod #= X mod 2,
      zcompare(Ord, 0, Mod),
      ord_(Ord, X, Es0, Es, Os0, Os),
      list_evens_odds(Xs, Es0, Os0).

ord_(=, X, Es0, [X|Es0], Os, Os).
ord_(<, X, Es, Es, Os0, [X|Os0]).

:

?- list_evens_odds([1,2,3,4,5,6,7], Es, Os).
Es = [2, 4, 6],
Os = [1, 3, 5, 7].
+1

" " if_/3, , .

@false @lurker @mat . !

"", , ; :

  • if_/3, , LOC.
  • , , : clpfd , . OTOH .
  • . , , " "...
+1

( reifiable clp (fd) ), ,

:- use_module(library(clpfd)).

list_evens_odds([],[],[]).
list_evens_odds([X|Xs],Es,Os) :-
   B #<==> (X mod 2 #= 0),
   freeze(B, (B=1 -> Es=[X|Es0],Os=Os0 ; Es=Es0,Os=[X|Os0])),
   list_evens_odds(Xs,Es0,Os0).

0/1 true/false, . , 0/1 , , , . ..

0

All Articles