Search code examples
prologzebra-puzzleclpb

Bertrand Russell Puzzle


Solve the following Caliban problem, translating each clue 'loyally' into Prolog, i.e. as loyally as possible.

As a simple exercise in abstraction suppose that four meaningless symbols a, b, c, and d correspond in one order or another to the equally meaningless symbols w, x, y, and z, and suppose further that

If a is not x, then c is not y.
If b is either y or z, then a is x.
If c is not w, then b is z.
If d is y, then b is not x.
If d is not x, then b is x.

In what order do the two sets of symbols correspond?

I tried the following code:

vban(L) :-
       L=[[a,C1],[b,C2],[c,C3],[d,C4]],

       ( member(C1,[w,y,z]) -> member(C3,[w,x,z])),
       ( member(C2,[y,z]) -> member(C1,[x])),
       ( member(C3,[x,y,z]) -> member(C2,[z])),
       ( member(C4,[y]) -> member(C2,[w,y,z])),
       ( member(C4,[w,y,z]) -> member(C2,[x])).

But it shows fail.Any help would be appreciated.


Solution

  • Using CLP(B) in SICStus Prolog or SWI:

    :- use_module(library(clpb)).
    :- use_module(library(lists)).
    :- use_module(library(clpfd)).
    
    corresponding(Matrix) :-
            Matrix = [[ _,AX, _, _],
                      [ _,BX,BY,BZ],
                      [CW, _,CY, _],
                      [ _,DX,DY, _]],
            maplist(card1, Matrix),
            transpose(Matrix, TMatrix),
            maplist(card1, TMatrix),
            sat(~AX =< ~CY),
            sat(BY + BZ =< AX),
            sat(~CW =< BZ),
            sat(DY =< ~BX),
            sat(~DX =< BX).
    
    card1(Vs) :- sat(card([1], Vs)).
    

    Example query:

    ?- corresponding(Vs), 
       pairs_keys_values(Pairs, [t,a,b,c,d], [[w,x,y,z]|Vs]),
       maplist(writeln, Pairs).
    

    Yielding (1 denotes corresponding elements):

    t-[w,x,y,z]
    a-[0,0,1,0]
    b-[0,1,0,0]
    c-[1,0,0,0]
    d-[0,0,0,1]
    

    and bindings for Vs and Pairs.