The “user” virtual file / workspace in Prolog.

When you’re first learning about Prolog, sometimes you’ll read books or tutorials that show you typing in “clauses,” and then immediately thereafter typing a “query.” If you use a visual or browser-based Prolog implementation, you’ll discover that there are two “modes” (my term), one for the input of the program / database, and one for querying. The query form is denoted by the prompt “?-” as in:

?- my_query_(Variable).

To me, it was somewhat confusing as to why and how I had to keep separate my program from my queries, since I was used to REPL-type interactive programming (e.g. irb for Ruby).

Later, in reading the SWI-Prolog manual, I saw reference to this odd snippet in the “Adding Arithmetic Functions” section:

?- [user].

:- other_stuff(x).

It appeared that [user] was changing the mode from query-mode to program-mode, and allowing me to define new predicates.

Well, close. It turns out the way to think about this (can’t seem to find it in the SWI-Prolog manual) is that the square-bracket notation is the “load file” shortcut, and Prolog comes with a virtual file known as “user.” When you query:

?- [user].

and then go on to get:

|:

as your new prompt, you’re loading the “user” file which is like opening a handle on STDIN. (Not precisely, but close enough.) You can then type your program entries, /but you must terminate them with EOF/ (ctrl-D).

This bit of prerequisite knowledge would have saved me a lot of puzzlement and trial-and-error. A shame that Prolog, despite a nearly 40-year history of continuous use and usefulness, has such a high propaedeutic load.

Tags: ,

Leave a Reply