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

Prolog调用weather谓词时报temp(A)过程不存在错误求助

问题原因

报错procedure 'temp(A)' does not exist的核心原因有两个:

  • Prolog要求所有可以在运行时动态增删事实的谓词,必须提前用dynamic/1指令声明。你代码里没有提前声明temp/1、humidity/1、sky/1是动态谓词,Prolog在解析weather规则时找不到这三个谓词的预定义实现,就会抛出不存在的错误,哪怕你后续运行时会通过assert插入事实也无法跳过这个检查。
  • 你代码里retractall语句写的双下划线__不是标准匿名变量写法,部分Prolog解释器会识别为普通变量报错,应该统一改成单下划线_。
修正后完整代码
/* 提前声明动态谓词,格式为 谓词名/参数数量 */
:- dynamic temp/1, humidity/1, sky/1.

/* 天气规则库 */
weather(good):- 
    temp(high), 
    humidity(dry), 
    sky(sunny).
weather(bad):- 
    humidity(wet);
    temp(low);
    sky(cloudy).

/* 交互接口 */
go:- 
    write('Is the temperature high or low? '), 
    read(Temp), nl, 
    write('Is the sky sunny or cloudy? '), 
    read(Sky), nl, 
    write('Is the humidity dry or wet? '), 
    read(Humidity), nl, 
    assert(temp(Temp)),
    assert(sky(Sky)),
    assert(humidity(Humidity)),
    weather(Weather),
    write('The weather is '), write(Weather), nl,
    retractall(temp(_)),
    retractall(sky(_)),
    retractall(humidity(_)).
运行说明

修正后在Prolog解释器中加载代码,输入go.即可正常触发交互流程,得到天气判断结果。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 02:24:03