Navigating Errors in N-Prolog: Theory Meets Practice

Kenichi Sasagawa
2 min readSep 28, 2024

--

Implementing Error Checking in N-Prolog

I am working towards a stable release of N-Prolog, ver3.3. In this version, I aim to enhance compatibility with ARITY/PROLOG, particularly concerning record-related predicates, while implementing thorough error checks.

For example, the following code checks for specified errors in the length/2 predicate:

%length
:- n_error(length(1,1),10). % Check for incorrect input
:- n_error(length([],a),19). % Check for type errors
:- n_error(length([],0,2),67). % Check for incorrect arguments
:- n_error(length([a|b],N),7). % Check for non-standard lists
:- n_error(length([],-1),49). % Check for invalid values

In this code, the second argument contains the error code corresponding to the specific error.

Philosophy Behind Error Handling

While conducting this test, I pondered how errors should be handled. In versions prior to 3.25, the system returned “no” for length(1,1) and for non-standard lists as well. length([1|2],X) returned "no". Reflecting on this, I believe it was influenced by GNU-Prolog. In ver3.3, I changed to treat these cases as errors, similar to SWI-Prolog.

I think GNU-Prolog stays true to the theory of predicate logic. length(1,1) is not possible; it theoretically does not exist, so it returns a negative "no". On the other hand, SWI-Prolog seems to emphasize practical programming. If something like length(1,1) occurs, it is likely due to human error. If "no" is returned here, there’s a risk of the programmer not realizing the mistake, or if they do notice, they may struggle with debugging.

I am considering revisiting mathematics through N-Prolog. I did not want to struggle with debugging when dealing with complex programs. I decided to prioritize practicality over theory and ensure that uncomputable situations definitely generate errors. This perspective might differ from ISO-Prolog. However, N-Prolog is modeled after ARITY/PROLOG, which predated ISO-Prolog. I believe this approach is valid. While I don’t recall clearly, I have a vague memory that in ARITY/PROLOG, length(1,1) returned "no" instead of an error. I’m not entirely sure about this, though.

sasagawa888/nprolog: interpreter and compiler to be compatible with ARITY/PROLOG(MS-DOS) (github.com)

--

--