Prolog 伯特兰罗素难题

Prolog 伯特兰罗素难题,prolog,zebra-puzzle,clpb,Prolog,Zebra Puzzle,Clpb,解决以下Caliban问题,翻译每条线索 “忠实地”进入序言,即尽可能忠实地 作为一个简单的抽象练习,假设四个 符号a、b、c和d以一种或另一种顺序对应于 同样没有意义的符号w、x、y和z,进一步假设 如果a不是x,那么c不是y。 如果b是y或z,那么a是x。 如果c不是w,那么b是z。 如果d是y,那么b不是x。 如果d不是x,那么b就是x。 这两组符号的对应顺序是什么 我尝试了以下代码: vban(L) :- L=[[a,C1],[b,C2],[c,C3],[d,C4]],

解决以下Caliban问题,翻译每条线索 “忠实地”进入序言,即尽可能忠实地

作为一个简单的抽象练习,假设四个 符号a、b、c和d以一种或另一种顺序对应于 同样没有意义的符号w、x、y和z,进一步假设

如果a不是x,那么c不是y。
如果b是y或z,那么a是x。
如果c不是w,那么b是z。
如果d是y,那么b不是x。
如果d不是x,那么b就是x。

这两组符号的对应顺序是什么

我尝试了以下代码:

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])).

但它显示失败。任何帮助都将不胜感激。

使用约束编程库将问题语句直接翻译为ECLiPSe Prolog:

:- lib(ic).
:- lib(ic_symbolic).
:- local domain(symbol(w,x,y,z)).
russel(A, B, C, D) :-
    [A, B, C, D] &:: symbol,
    (A &\= x) => (C &\= y),
    (B &= y or B &= z) => (A &= x),
    (C &\= w) => (B &= z),
    (D &= y) => (B &\= x),
    (D &\= x) => (B &= x),
    ic_symbolic:alldifferent([A, B, C, D]),
    ic_symbolic:indomain(A),
    ic_symbolic:indomain(B),
    ic_symbolic:indomain(C),
    ic_symbolic:indomain(D).
解决方案:

[eclipse]: russel(A,B,C,D).
A = y
B = x
C = w
D = z
Yes
在SICStus Prolog或SWI中使用CLP(B):

:- 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)).
屈服(
1
表示相应的元素):


以及
Vs
对的绑定

我喜欢Mat的解决方案,但为了解决这个问题,我们可以用“and”和“or”来编写逻辑表达式

a、 b,c和d可以用[0,0]、[0,1]、[1,0]和[1,1]来表示

如果(M1=N1和M2=N2),则两个数字M和N相等

如果(M1\=N1)或(M2\=N2)(或不(等于))两个数是不同的

隐含u=>v被翻译成not(u)或v

因此,我们得到:

:- use_module(library(clpb)).
:- use_module(library(lambda)).

or(A,B,A+B).
and(A,B,A*B).

% two numbers are equal
equal(A, B, Eq) :-
    foldl(\X^Y^Z^T^and(Z, (X =:= Y), T), A, B, 1, Eq).

% two numbers are different
different(A, B, Diff) :-
    equal(A,B,Eq),
    Diff = ~Eq.
    % foldl(\X^Y^Z^T^or(Z, (X =\= Y), T), A, B, 0, Diff).


puzzle :-
    A = [0,0],
    B = [0,1],
    C = [1,0],
    D = [1,1],

    W = [_,_],
    X = [_,_],
    Y = [_,_],
    Z = [_,_],

    % If a is not x, then c is not y.
    % (a is x) or (c is not y)
    equal(A, X, Eq1),
    different(C, Y, Di1),
    or(Eq1, Di1, P1),

    % If b is either y or z, then a is x.
    % (b is not y) and (b is not z) or (a is x)
    different(B, Y, Di2),
    different(B, Z, Di3),
    equal(A, X, Eq2),
    and(Di2, Di3, P2),
    or(Eq2, P2, P3),

    % If c is not w, then b is z.
    % (c is w) or (b is z)
    equal(C, W, Eq3),
    equal(B, Z, Eq4),
    or(Eq3, Eq4, P4),

    % If d is y, then b is not x.
    % (d is not y) or (b is not x)
    different(D, Y, Di4),
    different(B, X, Di5),
    or(Di4, Di5, P5),

    % If d is not x, then b is x.
    %(d is x) or (b is x)
    equal(D, X, Eq5),
    equal(B, X, Eq6),
    or(Eq5, Eq6, P6),

    % we must express that W,X,Y,Z are differents
    % W is different from X, Y, Z
    foldl(W +\R^S^T^(different(W, R, U),
             and(S, U, T)),
             [X,Y,Z], 1, Dif1),

    % X is different from Y, Z
    foldl(X +\R^S^T^(different(X, R, U),
             and(S, U, T)),
             [Y,Z], 1, Dif2),

    % Y is different from Z
    different(Y, Z, Dif3),

    % now we join all these expressions with an and
    Expr = *([P1,P3,P4,P5,P6, Dif1,Dif2, Dif3]),

    % we ask Prolog to count the number of solutions
    sat_count(Expr, N),
    writeln(N : ' solution(s)'),

    % we ask Prolog to satisfy the expr
    sat(Expr),

    maplist(writeln, [A, B, C, D]), nl,
    maplist(writeln, [W, X, Y, Z]).
我们得到:

?- puzzle.
1: solution(s)
[0,0]
[0,1]
[1,0]
[1,1]

[1,0]
[0,1]
[0,0]
[1,1]
true.

首先,
(If->Then)
不是逻辑含义,它是false,如果
If
是false。No ex falso Quodlibe阅读上的信息以了解这是一种完全相同的谜题。我正在使用爱丁堡Prolog。您能告诉我如何在这方面执行相同的任务吗?提前谢谢。@abs我的答案使用ECLiPSe CLP Prolog()的特定库。它不能很容易地转换到其他Prolog系统。
:- use_module(library(clpb)).
:- use_module(library(lambda)).

or(A,B,A+B).
and(A,B,A*B).

% two numbers are equal
equal(A, B, Eq) :-
    foldl(\X^Y^Z^T^and(Z, (X =:= Y), T), A, B, 1, Eq).

% two numbers are different
different(A, B, Diff) :-
    equal(A,B,Eq),
    Diff = ~Eq.
    % foldl(\X^Y^Z^T^or(Z, (X =\= Y), T), A, B, 0, Diff).


puzzle :-
    A = [0,0],
    B = [0,1],
    C = [1,0],
    D = [1,1],

    W = [_,_],
    X = [_,_],
    Y = [_,_],
    Z = [_,_],

    % If a is not x, then c is not y.
    % (a is x) or (c is not y)
    equal(A, X, Eq1),
    different(C, Y, Di1),
    or(Eq1, Di1, P1),

    % If b is either y or z, then a is x.
    % (b is not y) and (b is not z) or (a is x)
    different(B, Y, Di2),
    different(B, Z, Di3),
    equal(A, X, Eq2),
    and(Di2, Di3, P2),
    or(Eq2, P2, P3),

    % If c is not w, then b is z.
    % (c is w) or (b is z)
    equal(C, W, Eq3),
    equal(B, Z, Eq4),
    or(Eq3, Eq4, P4),

    % If d is y, then b is not x.
    % (d is not y) or (b is not x)
    different(D, Y, Di4),
    different(B, X, Di5),
    or(Di4, Di5, P5),

    % If d is not x, then b is x.
    %(d is x) or (b is x)
    equal(D, X, Eq5),
    equal(B, X, Eq6),
    or(Eq5, Eq6, P6),

    % we must express that W,X,Y,Z are differents
    % W is different from X, Y, Z
    foldl(W +\R^S^T^(different(W, R, U),
             and(S, U, T)),
             [X,Y,Z], 1, Dif1),

    % X is different from Y, Z
    foldl(X +\R^S^T^(different(X, R, U),
             and(S, U, T)),
             [Y,Z], 1, Dif2),

    % Y is different from Z
    different(Y, Z, Dif3),

    % now we join all these expressions with an and
    Expr = *([P1,P3,P4,P5,P6, Dif1,Dif2, Dif3]),

    % we ask Prolog to count the number of solutions
    sat_count(Expr, N),
    writeln(N : ' solution(s)'),

    % we ask Prolog to satisfy the expr
    sat(Expr),

    maplist(writeln, [A, B, C, D]), nl,
    maplist(writeln, [W, X, Y, Z]).
?- puzzle.
1: solution(s)
[0,0]
[0,1]
[1,0]
[1,1]

[1,0]
[0,1]
[0,0]
[1,1]
true.