我想给出一个特定的write/1
当谓词失败时:
因此,聊天仅在获得输入"bye"时启动此循环。 但是,如果由于格式错误的输入而导致进程失败,我想写成"错误",但仍保留在聊天循环中。
chat:-
repeat,
readinput(Input),
process(Input),
(Input = [bye| _] ),!.
因此,如果该过程失败,则只会重复,
?- chat.
|: oooo
|: poppsps
|: looool
|: pjpkpkpl
|: bye
> bye!
true.
我这样做了:
chat:-
repeat,
readinput(Input),
(process(Input);write('WRONG') ),
(Input = [bye| _] ),!.
如果进程失败,它确实可以处理,但它只是保持错误,
?- chat.
|: ooo
WRONG
WRONG
WRONG
WRONG
WRONG
WRONG
WRONG
WRONG
如果我确实给了一个削减,它就会退出聊天:
chat:-
repeat,
readinput(Input),
(process(Input);write('WRONG'), ! ),
(Input = [bye| _] ),!.
?- chat.
|: pop
WRONG
false.
?-
进程内部具有此分析谓词,该谓词在格式错误的输入上失败。 所以我尝试找到解析何时有一个没有语义表示值的变量并将 1 传递给 Zeta,并在编写后在此处失败:
process(Input):-
parse(Input,SemanticRepresentation, Zeta),
( Zeta == 1 ->
writeln('Wrong n Failing Now'), fail;
sat([],SemanticRepresentation,ModelResponse)
), ..... do more stuff
其中parse/3
定义为:
parse(Sentence,Parse, Zeta):-
srparse([],Sentence,Parse),
(nonvar(Parse) ->
write('not free variable'), nl, write(Parse),
Zeta = 1;
write('Free Variable'), nl, write(Parse),
Zeta = 0).
这确实在某种程度上有效,但反过来,有效句子失败。
?- chat.
|: asfafaf
|: afsgsg
|: sgregergerge
|: rgergergerge
|: asfsfsf
|: a blue box contains some ham
not free variable
s(exists(_G2515,and(and(box(_G2515),blue(_G2515)),exists(_G2602,and(ham(_G2602),contain(_G2515,_G2602))))),[])wrong
not free variable
s(exists(_G2515,and(and(box(_G2515),blue(_G2515)),exists(_G2602,and(ham(_G2602),contain(_G2515,_G2602))))),[])wrong
|:
我是新来的, 所以问题是什么是错误的专用方式。如何优雅地处理谓词失败?
我是否使用 nonvar 处理,我是否将其作为主循环中的 if else 断路器处理?
我使用尝试/捕获吗?有人可以帮助我在Prolog中处理错误吗?
nonvar((
您的第二次尝试已接近:
repeat,
readinput(Input),
(process(Input); write('WRONG') ),
(Input = [bye| _] ),!.
我假设如果输入不可接受process
就会失败,否则就会成功。只要确保你没有练习重复循环:
repeat,
readinput(Input),
( process(Input)
; write('WRONG'), nl,
fail
),
Input = [bye| _],
!.