Posts Tagged ‘prolog’

The “user” virtual file / workspace in Prolog.

Thursday, February 11th, 2010

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.

SWI-Prolog from MacPorts on OS X 10.5

Wednesday, December 23rd, 2009

Tried installing the binary of SWI-Prolog (swi-prolog-5.8.2-leopard-intel.mpkg) on my Mac OS X 10.5 box.  For whatever reason, it was bombing out with some nasty errors (Libary not loaded … image not found).  Read about others’ troubles and decided to try MacPorts.  Couldn’t get SWI-Prolog to install using a standard:

$ sudo port -v install swi-prolog

Ended up getting lots of BS errors on the lines of “ERROR: Could not load/find library -lXpm.”

* ERROR: Could not load/find library -lXpm.         *
* Please install the XPM library and rerun configure.       *
* The primary source for libXpm is at:              *
*                               *
*     *
*                               *
* NOTE: If your system has libXpm, check the following:     *
*                               *
*   - Do you have the development version of the library?   *
*   - Is the library you have of the right architecture?    *
*   - Could it be that other parts of X11 are missing?  *
*     (typically the case if -lXext is also missing)    *
*                               *
*   Examine packages/xpce/src/config.log for details    *
*   Linux users: check README.debian for dependencies   *
*   (note that the exact package names vary between     *
*   distributions).                     *

Long story short: For various reasons, the MacPorts installed software wasn’t finding my Apple Developer X11 libraries, and it also wasn’t appropriately requiring the software as dependencies for the swi-prolog package.  Had to do the following:

$ sudo port -v install xpm
$ sudo port -v install xorg-libXt
$ sudo port -v install Xft2

to get the prereqs installed (warning: those libs, in turn, have some mongo big dependencies, like Perl itself, so be ready for some serious compiling).  After that, installing swi-prolog worked just fine.