Brian Pattison
Brian Pattison

Reputation: 21

How to solve an undefined procedure error in Prolog?

I wrote this chunk of code on a provided computer:

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).

I am trying to run just a small test case for my homework assignment which is as follows:

:- 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).

However, whenever I run the tests I receive the following error:

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

I have tried many times to fix this and I am unable.

Any suggestions?

Upvotes: 1

Views: 1487

Answers (1)

Paulo Moura
Paulo Moura

Reputation: 18663

The plunit tool expects the code that you want to test to be encapsulated in a module. There's also a missing predicate in your code, scale/3. Try instead:

:- module(foo, []).

% your code here

:- begin_tests(imperial_part1).

% your tests here

:- end_tests(imperial_part1).

Then:

?- [foo], run_tests.

You will get (due to the missing predicate):

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

Add the missing predicate and your tests should run.

Upvotes: 3

Related Questions