CSC Informal Prolog Observations

Help

SWI-Prolog's Page is a good place to start for documentation.

It's not crazy to subscribe to the SWI-Prolog mailing list and post your questions hoping for help. I've been helped out on some REALLY ignorant questions and my ego is (almost) still intact.

typing ?- help(). at prolog (don't forget that period!) gets help for operations, reference manual sections, predicates, etc.

Clocksin and Mellish 5th Ed.

I don't know where this tradition of single-character identifiers came from, but it certainly makes the 5th edition of the Prolog book unnecessarily opaque, since the authors reuse the same variable name with different semantics a lot, especially between the code example and the prose describing it.

There are a few misprints in the 5th Ed. Ones I've found (please share others you find with me).

The soi-disant index would be a joke if it were't criminal. The authors and editor should have their asses thoroughly kicked.

Interaction and Typing

Line Editing: This is supposed to be an interactive sort of language but where is the history and the line editing? *I* sure can't find them: maybe one of youse can help. I can't edit in the middle of a line I'm typing, I can't go re-execute a previous line unless I cut and paste it...pfui! Seriously, let me know if you find something like a command-line setting that would fix this lack.

In the meantime, a stopgap is to put test code into a file you can easily edit. Then you have to consult it but retyping the consult might be easier than editing the 'input' of the test. For instance

testit(V) :-
phrase(expr(PTree), [a , * , '(' , 3 , + , 4 , ')', - , 3 , '/' , 2],[]),
cbval(PTree, V).

is one way to test the parser and evaluator from the Prolog assignment; to modify the input just edit the list of atoms.

Consulting: refer to files with no quotes or .pl suffix, or with single quotes. I use
?- [file].
to read in clauses from the file file.pl .

Don't forget the period at the end of clauses!!

Don't forget ONLY variables can start with a capital, and they MUST!!

Syntax checking. It seems there is often NO syntax checking. Prolog doesn't catch all sorts of easy to make errors. Like misspelling: if you type Myop for MyOp, or (God forbid) myop for MyOp, things just fail. You just see
No
for failing match.

Other typos: these are all wrong, and I don't think you get a lot of help nailing down the problems....

pl([H|T, foo, bar).
pl([H|T]), foo, bar).
pl([H|T), foo, bar).

When things are caught, the diagnosis isn't too user-friendly (Quickie! What's wrong with line 11?)

11 ?- phrase (expr(V), [1, *, 1],[]). ERROR: Syntax error: Operator expected ERROR: phrase (expr(V), [1, *, 1],[] ERROR: ** here ** ERROR: ) .

The problem is the Prolog parser got off when it saw the space after 'phrase' and blew up somewhere in your arglist looking for god knows what. Notice that the '** here **' indicator is NOT under the problem but is interpolated into the line where the parser blew up, so the location is right after '....],[]'.

Obviously then, another Prologism is that you always have to cram the functors right up against the (...) of the arguments. So foo(bar) is OK but foo (bar) is probably an error as above.

Semantics

Variables don't get assigned in the sense you're used to: In a preview of how LISP works, only with a slightly different formal basis, variables are only 'assigned' through unification, and their values don't stick around, as in: 15 ?- Z = 5. Z = 5 Yes 16 ?- Z. % ... 1,000,000 ............ 10,000,000 years later % % >> 42 << (last release gives the question) 16 ?- Cute? You Douglas Adams fans may be amused.

You have to put the variables you want to assign into clauses as arguments to be bound by unification. Since there's nothing like
Ans = op(Arg1, Arg2) .
Instead, you do op(Arg1, Arg2, Ans). for instance.

Some simple library routines don't seem to be in place for me, e.g. random/1. It may be that this is part of the clib library, which looks quite useful (supports sockets, for instance -- pre-442 students take note!).

Standard Prolog isn't great with I/O. I'm sure there are libraries or packages you can find, but I've found it easier pre-tokenize input, so for input I'd use something like [a, +, 5] rather than ``a+5'', say.

Tracing is, for me, often not all that helpful. In particular, when I use ``;'' to re-satisfy for more answers, it seems re-dos, nothing happens. At best, I usually just wind up with a No, but at least then I know about how far the program got.

Warning: Predicting backtracking behavior is very mysterious. Very risky to rely on details of what is going to get retried, I find.

A common warning is for ``singleton variables''. They appear only once in a clause, so can be replaced by _, or _Varname. Since they can't have their value changed, Prolog thinks you might have mistyped something. Actually, for me, quite often it's right!


Back to the course home page

Last Change: 9/05//07