You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Clingo语法错误求助:意外出现>与!=运算符问题

修复Clingo中的语法错误

错误原因与修复方案

  • 第一处错误(selects规则行):
    Clingo中not操作符只能作用于单个文字,或者用括号包裹的一组合取条件。原代码里not直接跟了多个原子,导致语法解析失败。
    错误代码片段:

    not pref(A,C), clockwise(A,C), path(A,C,Z), pref(A,B), clockwise(A,B), path(A,B,W), W > Y, Z < Y,
    

    修正后:

    not (pref(A,C), clockwise(A,C), path(A,C,Z), pref(A,B), clockwise(A,B), path(A,B,W), W > Y, Z < Y),
    
  • 第二处及关联错误:
    原代码存在多余的右括号(比如not selects(A,B,Z), Z > Y),末尾多了一个)),同时部分not后的复合条件未加括号,需要统一修正:

    • 将not selects(A,B,Z), Z > Y),改为not (selects(A,B,Z), Z > Y),
    • 将not selects(_,A,Z), Z > Y),改为not (selects(_,A,Z), Z > Y),
    • prefers规则中的not (pref(A,C), clockwise(A,C), selects(C,A,_), C != B),本身括号语法正确,无需修改。

修正后的完整代码

as(1..3).

clockwise(1,2).
clockwise(2,3).
clockwise(3,1).

path(1,2,20).
path(2,3,30).
path(3,1,40).
path(1,3,310).
path(3,2,230).

pref(1,2).
pref(2,3).
pref(3,1).

selects(1,2,20).
selects(2,3,30).
selects(3,1,40).

gadget :-
    selects(2,3,230),
    not selects(3,1,310),
    not selects(1,2,20),
    selects(3,1,30),
    selects(1,2,40).

selects(A,X,Y) :-
    pref(A,B),
    clockwise(A,B),
    path(A,B,Y),
    not (pref(A,C), clockwise(A,C), path(A,C,Z), pref(A,B), clockwise(A,B), path(A,B,W), W > Y, Z < Y),
    not (selects(A,B,Z), Z > Y),
    not (gadget, selects(A,_,Z), Z > Y),
    not (selects(_,A,Z), Z > Y),
    not (gadget, selects(_,A,Z), Z > Y),
    not (selects(A,X,Z), Z < Y).

prefers(A,B) :-
    selects(B,A,_),
    not (pref(A,C), clockwise(A,C), selects(C,A,_), C != B),
    not (gadget, selects(C,A,_), selects(B,C,_)),
    not (pref(A,B), selects(_,A,_), not selects(B,A,_)),
    not (gadget, selects(_,A,_), selects(B,C,_), selects(C,D,_), selects(D,E,_), selects(E,F,_), selects(F,G,_), selects(G,H,_), selects(H,I,_), selects(I,B,_)).

:- selects(A,X,Y), selects(B,X,Z), A != B, Y > Z, not gadget.

关键修正点

  1. 所有not后的复合条件必须用括号包裹,确保not作用于整个合取逻辑。
  2. 清理代码中多余的括号,避免解析器报错。
  3. 算术比较(W > Y、Z < Y等)语法本身正确,只要变量能被正确实例化即可运行。

内容的提问来源于stack exchange,提问作者user12272065

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 22:47:46