首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何解决Prolog中未定义的过程错误?

如何解决Prolog中未定义的过程错误?
EN

Stack Overflow用户
提问于 2020-02-11 10:29:18
回答 1查看 650关注 0票数 1

我在一台提供的计算机上编写了这段代码:

代码语言:javascript
复制
factor_in_inches(Unit, Scale) :- scale_of(Unit, Scale, inch).


scale_factor(Unit1, Unit2, Factor) :- scale_of(Unit2, Factor, Unit1).

%PartI
scales(BigUnit, Scale, LittleUnit) :-
    scale(BigUnit, Scale, LittleUnit);
    scale(LittleUnit, Scale1, BigUnit),
    Scale is 1/Scale1.


%check if input follow base or reverse predicate
scale_of(BigUnit, Scale, LittleUnit) :- 
    scales(BigUnit, Scale, LittleUnit);
    scales(BigUnit, Scale1, LittleUnit1), 
    scale_of(LittleUnit1, Scale2, LittleUnit),
    Scale is Scale1 * Scale2.

%Checks if Unit1 * Quantity1 = Unit2 * Quantity2
convert(Unit1, Quantity1, Unit2, Quantity2) :-
    factor_in_inches(Unit1, Scale1),
    factor_in_inches(Unit2, Scale2),
    Scale1 is round(Scale2 * Quantity2 / Quantity1).

我正在尝试为我的家庭作业运行一个小的测试用例,如下所示:

代码语言:javascript
复制
:- begin_tests(imperial_part1).

  test(factor_in_inches) :- factor_in_inches(inch,  1.0).
  test(factor_in_inches) :- factor_in_inches(foot, 12.0).
  test(factor_in_inches) :- factor_in_inches(rod, 198.0).

:- end_tests(imperial_part1).

但是,每当我运行测试时,我都会收到以下错误:

代码语言:javascript
复制
ERROR: /Users/brianpattison/Desktop/Semester_2/Progamming_Languages/Prolog/SWI-Prolog.app/Contents/MacOS/imperial_hw.pl:67:
    test factor_in_inches: received error: plunit_imperial_part1:'unit body'/2: Undefined procedure: plunit_imperial_part1:factor_in_inches/2
  However, there are definitions for:
        factor_in_inches/2
ERROR: /Users/brianpattison/Desktop/Semester_2/Progamming_Languages/Prolog/SWI-Prolog.app/Contents/MacOS/imperial_hw.pl:68:
    test factor_in_inches: received error: plunit_imperial_part1:'unit body'/2: Undefined procedure: plunit_imperial_part1:factor_in_inches/2
  However, there are definitions for:
        factor_in_inches/2
ERROR: /Users/brianpattison/Desktop/Semester_2/Progamming_Languages/Prolog/SWI-Prolog.app/Contents/MacOS/imperial_hw.pl:69:
    test factor_in_inches: received error: plunit_imperial_part1:'unit body'/2: Undefined procedure: plunit_imperial_part1:factor_in_inches/2
  However, there are definitions for:
        factor_in_inches/2

我已经尝试了很多次来解决这个问题,但我无法解决。

有什么建议吗?

EN

回答 1

Stack Overflow用户

发布于 2020-02-11 18:29:37

plunit工具期望您想要测试的代码封装在一个模块中。您的代码中还缺少一个谓词,scale/3。相反,尝试:

代码语言:javascript
复制
:- module(foo, []).

% your code here

:- begin_tests(imperial_part1).

% your tests here

:- end_tests(imperial_part1).

然后:

代码语言:javascript
复制
?- [foo], run_tests.

您将得到(由于缺少谓词):

代码语言:javascript
复制
ERROR: .../foo.pl:30:
    test factor_in_inches: received error: foo:scales/3: Unknown procedure: foo:scale/3
  However, there are definitions for:
        foo:scales/3
ERROR: .../foo.pl:31:
    test factor_in_inches: received error: foo:scales/3: Unknown procedure: foo:scale/3
  However, there are definitions for:
        foo:scales/3
ERROR: .../foo.pl:32:
    test factor_in_inches: received error: foo:scales/3: Unknown procedure: foo:scale/3
  However, there are definitions for:
        foo:scales/3

添加缺少的谓词,您的测试就应该运行。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60161169

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档