Debugging and Testing: PROLOG
SWI-Prolog’s debugger is a command-line debugger which also provides access to the code. Graphical Debugger can be provided using this feature.
Testing:
Testing the software during the development stage is probably the most important part of building the software. Testing is done to ensure that the code is used correctly. Testing can be used to validate the claims we make while implementing Prolog. By writing a test, the claim can be made explicit.
Example: length of a list
Let’s consider a simple program with a Prolog error:
Here we are describing the list and its corresponding length. At first, there seems the program work for many conditions as we assumed.
For Example:
The Query ?- l_len([ ], 0). Executes correctly as intended.
We write the program to get solutions.
?- l_len(Ls, _).
St = [ ] ;
St = [_GA24] ;
St = [_GA52, _GW36 ] ;
?- l_len(Ls, _).
St = [ ] ;
St = [_GA24] ;
St = [_GA52, _GW36 ] ;
Actually, there are some mistakes in the program above.
Testing nontermination
The first example shows the required termination properties of the program. Let us consider a case of l_length / 2 from the above example, we know that for general queries the program must not be terminated. Its because we assume the relationship is valid for a list of all lengths and there is no another way to get the report for this set with a finite number of answers.
The Prolog Query Q to study termination:
?- Q, false.
?- Q, false.
If and only if this query is not executed, then Q terminates universally. For the concrete case, we use:
?- l_len(Ls, L), false.
?- l_len(Ls, L), false.
Here, the programs run as expected and no terminations are seen. However, the properties of testing terminations are not enough to find all the mistakes. So we can try for other testing processes like concrete test cases.
Concrete test cases
Generally, the testing for pure programs in Prolog is very easy in this way: Start with simple try queries on the top level. In addition to these, the backtracking mechanism in Prolog helps in expressing more and more sets of concrete test cases at a time.
For example, as stated above we know that for lists of all lengths the relation should yield answers. In particular, it should succeed for lists in the form [ ], [ a ], [ a,a ], …..
Let us check for counterexample if exists:
No counterexample reported. So, in this case, also the program performs as assumed. We can extend this and can be more generally for the above test cases for example to:
For the most generalization query also, No counterexample is reported.
Testing against a reference implementation
In addition to the above testing cases, we can test against a reference implementation. In our concrete case, this testing process is easily possible, because most of the programs in prolog ship with a predicate called length/2 this is the relation we are describing above. This testing process reveals an error in our program immediately.
Generally, the reference implementation is not seen in most of the cases.
Testing declarative properties
By using further properties we can test the relationship and checks for if the relationship between them is implemented correctly.
For example
If the length of a list Ls is K, then the length of [_lLs ] is K+1. The counterexample of this:
Here, the length of [_] should be 1 but is showing as 2(incorrect). The length of [ ] or the length of [ _ ] is not shown correctly. In particular, the below query incorrectly fails:
DEBUGGING
→ Debugging is useful in identifying the errors/mistakes in the code. Declarative debugging is one of the mechanisms to identify those mistakes made by the coder.
→ Tracing: Following are the commands to trace programs in the Prolog environment:
→ Drawbacks of tracing: Major drawback of tracers is that they involve mistakes too. Apart from the errors in the user’s code, tracer also sometimes has errors. The alternate solution to such a procedural approach is a declarative approach, they provide us with the advantage of finding mistakes quickly.
Mistakes in Prolog code:
→ These mistakes are classified into 2 main types:
-
too general — gives unnecessary solutions.
-
too specific — unreported solutions.
Example: The following sample code contains a mistake:
a) Too general case:
b) Too specific case:
Finding Mistakes:
To locate the mistakes in the prolog code, we trace the code step by step and see if the given input satisfies the conditions in the programs. The debugger finds the mistake in the code and highlights and flags the conditions when there is a mistake.
The UNIT testing framework in PROLOG
Testing is coded in Prolog, and it is included in this way stating with begin_tests(name) and ends with end_tests(name). This can be framed inside a normal module, or they can be in a different test case file that calls the files to be tested. The starting point is defined by a set of rules — test (+name) OR test (+name, +options). Here “options” justifies additional features of the test case.
Consider the example below:
The second argument (‘options’) carries different additional options.
Have a look at some of them
Blocked(+reason:atom)
Here the test will be disabled. If a test can’t be run for some reason, tests are marked as blocked.
fixme(+Reason:atom)
It is identical to the above à blocked(reason). If a failure occurs ‘-‘ is printed rather than character
condition(:Goal)
It is basically a pre-condition before running the test. When the condition fails à test will be skipped. As an alternative solution to the ‘setup’ option, the condition can be used. So, the only distinction is that a condition’s failure skips the test and when using the ‘setup’ option is considered an error.
cleanup(:Goal)
The Goal is often called just after the test — the body has been completed, irrespective of whether an exception fails succeeds or throws. This option should be used by tests requiring side effects to be changed back after completion of the test.
true (AnswerTerm Cmp Value)
The body has to be succeeded deterministically. Using the Cmp comparison operator, AnswerTerm is contrasted to value.
fail → It must fail.
Writing the test body
The code for testing in PROLOG is an ordinary prolog code which should be able to execute without any options and must succeed deterministically. Options such as fail, true,etc.… can be used for different results.
Testing deterministic predicates
These are the predicates that are succeeded only once and there are no other choices. These usually have one or more inputs. The test program inputs proper values for argument and verifies the output.
Testing semi-deterministic predicates
There is the type of predicates that succeed or fail exactly one and has no other choices. Testing them is similar to testing deterministic predicates.
Testing error conditions
The throws(error) option is used for testing error conditions.
Using separate test files
The tests can be written in normal prolog source code and they can also be placed in another file. Test files use a separate extension called .plt (which refers to prolog tests).
Running the test suite
For running the test suite we must initially load the program and then run to using the command run_tests(+unit). If you want to run some specified tests we can give the name of the test as a parameter to the function run_tests. This is used for tracing a specific test as demonstrated in the video.
Tests and production systems
As we don’t require the test suite to be available with the exported application, one method to avoid this is — place all the tests in separate files and don’t load this during the running of the final application. We can also use the test option as shown below.
Controlling the test suite
Some of the test suite options are
Silent(+boot) → when this option is enabled there is no output statements that are failed, only the output is displayed for the errors.
There are other options like sto, load and run.
Test_report(+what) → When the test is executed and need to print the report, this can be used.
Coverage analysis
This shows the coverage report of our code. This is run using the command show_coverage(:Goal).
Portability of the test-suite
The tests which are written for the application needs to have less integration for the application so that they can be portable so that they can test the application in different environments. Some of the options as mentions before such as load — is used to avoid the automatic loading of the test suites.


