【问题标题】:Solve in prolog resulting in false?在序言中解决导致错误?
【发布时间】:2013-04-06 09:26:02
【问题描述】:

我尝试过使用类似于此链接的代码:

Solving a textual logic puzzle in Prolog - Find birthday and month

我要解决的问题是这个(电话对话)。 http://www.cis.upenn.edu/~matuszek/cis554-2012/Assignments/prolog-01-logic-puzzle.html

我的代码:

dated(Date):-
member(Date,[1928,1929,1932,1935]).
exchanged(Exchange):-
member(Exchange,[al,be,pe,sl]).

solve(X):-
X=[[gertie,Exchange1,Date1],
   [herbert,Exchange2,Date2],
   [miriam,Exchange3,Date3],
   [wallace,Exchange4,Date4]],

exchanged(Exchange1), exchanged(Exchange2), exchanged(Exchange3), exchanged(Exchange4),
Exchange1 \== Exchange2, Exchange1 \== Exchange3, Exchange1 \== Exchange4,
Exchange2 \== Exchange1, Exchange2 \== Exchange3, Exchange2 \== Exchange4,
Exchange3 \== Exchange1, Exchange3 \== Exchange2, Exchange3 \== Exchange4,
Exchange4 \== Exchange1, Exchange4 \== Exchange2, Exchange4 \== Exchange3,

dated(Date1), dated(Date2), dated(Date3), dated(Date4),
Date1 \== Date2, Date1 \== Date3, Date1 \== Date4,
Date2 \== Date1, Date2 \== Date3, Date2 \== Date4,
Date3 \== Date1, Date3 \== Date2, Date3 \== Date4,
Date4 \== Date1, Date4 \== Date2, Date4 \== Date3,

%Herbet's first exchange wasn't for BE
Exchange2 \== be,

%The Person whose first exchange was SL wasn't Getie or Herbert
Exchange1 \== sl,
Exchange2 \== sl,

%The person whose first exchange was BE didn't get the phone in 1935
member([_,be, \+1935], X),

%The person who got the first phone in 1932 didn't have an exchange for AL or BE
member([_, \+al, 1932], X),
member([_, \+be, 1932],X),

%The person who got the first phone in 1928 had an exchange for PE
member([_,pe,1929], X),

%Wallace first exchange was AL
Exchange4 == al.

我的问题是这样的:

?- solve(X).
 false.

【问题讨论】:

    标签: prolog


    【解决方案1】:

    所以您的问题是,您的 solve 谓词找不到任何解决方案。这意味着,对于解决方案树中的所有可能路径,找到解决方案的先决条件之一都失败了。

    您是否真的尝试搜索它是哪一个?当然不是,否则你会注意到这个:

    member([_,be,\+1935],X)
    

    总是失败。为什么? \+/1 是什么? “如果 Goal 无法证明,\+ :Goal 为真”。换句话说,您不能使用\+ 进行匹配。相反,你可以写:

    \+ member([_,be,1935),X).
    

    所以有了所有的更正:

    ?- solve(X).
    X = [[gertie, be, 1928], [herbert, pe, 1929], [miriam, sl, 1932], [wallace, al, 1935]] ;
    false.
    

    假设程序的其余部分是正确的。

    使用 stackoverflow 作为调试代码的替代方法真的很糟糕。

    【讨论】:

    • 请原谅我不知道如何使用 SWI prolog 进行精确调试。我对声明式编程完全陌生!感谢您的回复 - 是的,我也尝试过搜索
    • @AlexCutajar 我试图展示我发现你的程序有什么问题的方式,这样你就可以自己做。实际上,无论使用哪种语言,您都必须学会在程序中发现问题。
    【解决方案2】:

    代替

    exchanged(Exchange1), exchanged(Exchange2), exchanged(Exchange3), exchanged(Exchange4),
    Exchange1 \== Exchange2, Exchange1 \== Exchange3, Exchange1 \== Exchange4,
    Exchange2 \== Exchange1, Exchange2 \== Exchange3, Exchange2 \== Exchange4,
    Exchange3 \== Exchange1, Exchange3 \== Exchange2, Exchange3 \== Exchange4,
    Exchange4 \== Exchange1, Exchange4 \== Exchange2, Exchange4 \== Exchange3,
    
    dated(Date1), dated(Date2), dated(Date3), dated(Date4),
    Date1 \== Date2, Date1 \== Date3, Date1 \== Date4,
    Date2 \== Date1, Date2 \== Date3, Date2 \== Date4,
    Date3 \== Date1, Date3 \== Date2, Date3 \== Date4,
    Date4 \== Date1, Date4 \== Date2, Date4 \== Date3,
    

    你可以写

    permutation([al,be,pe,sl], [Exchange1, Exchange2, Exchange3, Exchange4]),
    permutation([1928,1929,1932,1935], [Date1, Date2, Date3, Date4]),
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-17
      • 2016-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多