I don't understand why I can't remove the write()

I’m using: SWI-Prolog version 8.0.3

I want the code to do the same as the following code:

customer(tom, smith, 20.55).
customer(sally, smith, 120.55).

get_cust_bal(FName, LName) :-
        customer(FName, LName, Bal),
        write(FName), tab(1),
        format("~w owes us $~2f", [LName, Bal]).

But what I’m getting is:

20 ?- [db].
ERROR: d:/prog_langs/other/prolog/db.pl:36:2: Syntax error: Operator expected
true.

My code looks like this:

customer(tom, smith, 20.55).
customer(sally, smith, 120.55).

get_cust_bal(FName, LName) :-
        customer(FName, LName, Bal),
        format("~w ~w owes us $~2f", [FName, LName, Bal]).

This is a very basic question, but I don’t understand why I can’t just rermove the write() and add another item in the list I pass into the format() function.

Works for me as written.

customer(tom, smith, 20.55).
customer(sally, smith, 120.55).


get_cust_bal_1(FName, LName) :-
        customer(FName, LName, Bal),
        write(FName), tab(1),
        format("~w owes us $~2f", [LName, Bal]).

get_cust_bal(FName, LName) :-
        customer(FName, LName, Bal),
        format("~w ~w owes us $~2f", [FName, LName, Bal]).
?- get_cust_bal_1(F,L).
tom smith owes us $20.55
F = tom,
L = smith ;
sally smith owes us $120.55
F = sally,
L = smith.

?- get_cust_bal(F,L).
tom smith owes us $20.55
F = tom,
L = smith ;
sally smith owes us $120.55
F = sally,
L = smith.

I did rename the first predicate to make it different from the second and did press space bar to see additional answers but it does work without errors.

This was done using SWI-Prolog (threaded, 64 bits, version 8.3.0-172-g5e86d749d) on Window 10.
This is a daily build which is why the version number my not look correct but it is correct.


Did you consult the correct source code file?

20 ?- [db].
ERROR: d:/prog_langs/other/prolog/db.pl:36:2: Syntax error: Operator expected
true.
1 Like

I know that the code you posted works. That wasn’t the question. The question was why I can’t remove the write() without causing an error. My actual code is the final snippet. The code that works is the first snippet. I want to know why removing the write() in my code breaks it.

As I noted, it works fine when I run it. I can not reproduce what you did to break it.

1 Like

To break it, I removed the write. I changed:

get_cust_bal(FName, LName) :-
        customer(FName, LName, Bal),
        write(FName), tab(1),
        format("~w owes us $~2f", [LName, Bal]).

to

get_cust_bal(FName, LName) :-
        customer(FName, LName, Bal),
        format("~w ~w owes us $~2f", [FName, LName, Bal]).

looks the same as what I tested

which ran successfully :grinning:

1 Like

That’s very strange, because I’m running that code and getting an error. I’m getting the following:

?- get_cust_bal(F, L).
ERROR: Undefined procedure: get_cust_bal/2 (DWIM could not correct goal)
4 ?-

Try using listing/1, e.g.

?- listing(get_cust_bal).
get_cust_bal(FName, LName) :-
    customer(FName, LName, Bal),
    format("~w ~w owes us $~2f", [FName, LName, Bal]).

true.

do you see the same source code?

1 Like

It tells me that get_cust_bal doesn’t exist

Can you share the complete file? Please remove all the irrelevant stuff, but make sure the bug remains. At least the location than fits and may give a hint. My suspicion is there is something unreadable in the file like a zero-space character or whatever.

1 Like
customer(tom, smith, 20.55).
customer(sally, smith, 120.55).

get_cust_bal(FName, LName) :-
        customer(FName, LName, Bal),
        # write(FName), tab(1),
        format("~w ~w owes us $~2f", [FName, LName, Bal]).
1 Like

That is now reproducing the error on my machine.

?- consult("C:/Users/Eric/Documents/Projects/Prolog/swi-discourse_043.pl").
ERROR: c:/users/eric/documents/projects/prolog/swi-discourse_043.pl:25:9: Syntax error: Operator expected
true.

EDIT

Notice the line now shows a #

        # write(FName), tab(1),
1 Like

Glad we’re on the same page now.

Is that not commenting it out?

No

You need to use a percent sign :grinning:

% write(FName), tab(1),

EDIT

See: Structured comments

1 Like

I did look at that, but the only thing that appears to even remotely comment it out (shows everything in the same colour as it would be if it were a comment) is the # symbol.

EDIT: Multiline JavaDoc comments seem to work, but single-line (%) comments don’t.

Are you saying your editor is complaining about the % as a comment? If so and Prolog is running the code correctly then it is your editor that is the problem. If it is Prolog complaining when the code is run the we should see why. In the many years of using SWI-Prolog I have never seen it complain about a % comment. I have seen them used in the wrong place and cause syntax errors.

Personally I use Visual Studio Code.

1 Like

I’m also using VS Code, and it isn’t complaining about the percent sign, but it doesn’t colour that line all in the same colour as it does in other languages. Do you also have this? Is there a setting I need to change?

1 Like

Here are two links where I mention VSC (1,2).

image

The line with the % is highlighted as a Prolog comment in green.
Also notice the file type in the lower right is identified as Prolog.

Currently the Prolog extension is disabled and VSC-Prolog is enabled.

image

1 Like