【问题标题】:Prolog Can't get to the predicateProlog 无法到达谓词
【发布时间】:2021-11-24 19:30:06
【问题描述】:

prolog 应该是找到五个 statements 语句的顺序。一切正常,但是当我调用查询 solution([A, B, C, D, E]) 时,我得到一个像这样的沙盒错误:

错误:

Sandbox restriction!
Could not derive which predicate may be called from
      call(C)
      all(schoolgirl,[A,B,C,D,E])
      solution([A,B,C,D,E])

完整的 Prolog 程序:

all(_,[]).
all(Pred, [X|Xs]):-
    P =..[Pred,X],
    call(P),
    all(Pred,Xs).
distinct([]).
distinct([X|Xs]):- 
    not(member(X, Xs)), distinct(Xs).
x0r(A, B):- 
    A, not(B).
x0r(A, B):-
    not(A), B.

schoolgirl(betty).
schoolgirl(ethel).
schoolgirl(joan).
schoolgirl(kitty).
schoolgirl(mary).

betty(Snd,Trd):- 
    x0r(Snd=kitty, Trd=betty).
ethel(Fst, Snd):- 
    x0r(Fst=ethel, Snd=joan).
joan(Trd, Fith):- 
    x0r(Trd=joan, Fith=ethel).
kitty(Snd, Forth):- 
    x0r(Snd=kitty, Forth=mary).
mary(Forth, Fst):- 
    x0r(Forth=mary, Fst-betty).

solution([Fst, Snd, Trd, Forth, Fith]):-
    all(schoolgirl, [Fst,Snd, Trd, Forth, Fith]),
    distinct([Fst, Snd, Trd, Forth, Fith]),
    betty(Snd, Trd),
    ethel(Fst, Snd),
    joan(Trd, Fith),
    kitty(Snd, Forth),
    mary(Forth, Fst).

电话是

solution([A, B, C, D, E])

【问题讨论】:

  • 沙盒,我拿表明你正在使用 SWISH。我不是 SWISH 用户,所以无法帮助您。尝试安装 SWI-Prolog 并从命令行执行。我怀疑是call/1 导致了这个问题,但这纯粹是猜测。再一次没有告诉鸭子。

标签: prolog swi-prolog swi-prolog-for-sharing


【解决方案1】:

正如评论者所说,这似乎是使用 SWI-Prolog 的基于浏览器的 SWISH 系统 (https://swish.swi-prolog.org/) 时的特殊限制。它不希望您将 call/1 与它不太了解的术语一起使用。

幸运的是,您可以提供更多信息:您在以下上下文中使用call/1

P =..[Pred,X],
call(P),

也就是说,调用谓词Pred 时只有一个参数X。有一个更直接的语法:

call(Pred, X)

这足以使错误消失,并使 SWISH 愿意运行您的程序。 (事实上​​,这种语法更通用一点,因为它需要将一个 additional 参数添加到 Pred 中已经存在的参数中,因此 call(f(a), b) 将调用目标 f(a, b)。)

您的查询现在将因一个有点模糊的错误而终止:

procedure `A-B' does not exist
Reachable from:
      call(_1690-betty)
      not(A-betty)
      x0r(A=mary,B-betty)
      mary(A,B)
      solution(A)

mary/2 谓词的定义中有错字。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多