Categories
Programming

Language of the Month: Prolog

New month, new programming language to learn, the 3rd one in this series. So the repertoire so far contains:

Prolog

It is again a very different choice, logic programming. Been playing with it for the last two weeks or so, and it really makes me think differently about programming and programs. Logic and complex thinking was always a favorite past-time of me (e.g. solving puzzles and such), but only now I realized that I do have a lot to practice in this area.

Prolog is also one of the older languages (feels like a “classmate” of Lisp and Fortran) so it was the first language in the series where I could actually go to a library, take out some books to learn it, and that book wasn’t hopelessly out of date (try to do the same thing with Python or Ruby…). Since these books were also written by academics and not necessarily computer scientists, their whole approach is different, in a way more curious, though probably less practical.

In the end, I think what I would like to gain is a tool that I can use to attack problems that are intractable or at least very difficult in other languages.

Start

One of the first thing I found hard to figure out was how to actually run a program. So in a nutshell: even if most of the interaction is within an interpreter-like environment, all the basics have to be prepared in a file and loaded.

E.g. I prepare a file like this (based on Ender’s Game), call it ender.pl:

%% Genealogy of Ender's Game.
% Facts
male(ender).
male(peter).
male('john paul').
female(valentine).
female(theresa).

parent('john paul', ender).
parent('john paul', peter).
parent('john paul', valentine).
parent(theresa, ender).
parent(theresa, peter).
parent(theresa, valentine).

% Predicates
father(X, Y) :-
	male(X),
	parent(X, Y).
mother(X, Y) :-
	female(X),
	parent(X, Y).

sibling(X, Y) :-
	father(F, X), mother(M, X),
	father(F, Y), mother(M, Y),
	X \= Y.
brother(X, Y) :-
	male(X),
	sibling(X, Y).
sister(X, Y) :-
	female(X),
	sibling(X, Y).

Then starting Prolog I can load the file with the [‘ender.pl’]. form, and ask questions like who is Ender’s sister? Who are Theresa’s kids? Who are Peter’s siblings?

?- ['ender.pl'].
% ender.pl compiled 0.00 sec, 144 bytes
true.

?- sister(Sis, ender).
Sis = valentine ;
false.

?- mother(theresa, Kid).
Kid = ender ;
Kid = peter ;
Kid = valentine.

?- sibling(peter, X).
X = ender ;
X = valentine ;
false.

Well, this is laughably simple, and I’m beyond this already, but it’s good for a first illustration.

By the way, it looks like 90% of elementary Prolog examples use family trees (Nordic or Greek gods, literary, real families…)

Now let’s get in there and learn some more stuff…

Links

Tutorials & Info

Books

Compilers

Code

5 replies on “Language of the Month: Prolog”

For a more programmer-oriented book on Prolog, I highly recommend Richard A. O’Keefe’s “The Craft of Prolog”. He doesn’t focus on the academic approach, but beautifully shows how to write clear, efficient, elegant code in Prolog.

I share your apparent discomfort with a strictly interactive implementation. Back in the late ’80s I wrote a more conventional compile, link, & run Prolog compiler, which I’ve been using and maintaining ever since. It produces a C source file as output, which is compiled & linked with the Prolog runtime library. The result is an ordinary program, invoked like any other. It also contains an interface definition language with a stub compiler that automatically generates stubs for calling C functions from Prolog, or allowing C code to call a Prolog predicate as though it were a normal C function.

It was born on Unix, for a time was developed & used under Windows, but I’ve gone home to Linux & have been supporting it on that exclusively for the last 10 yrs. GPL licensed, by the way.

By the way, I don’t use it for family trees, but for pretty much everything. (I’ve even written an experimental shell in Prolog.

Thanks a lot for the suggestion, finally found the book. Feels like they don’t write books like this anymore, reading the intro and beginning it just has a great style. :)

Can you give me a link to some more info about the compiler? Looks very interesting and also a good thing to compare, in this Language of the Month series since so many of these languages use the compile-lang-A-to-lang-B way to improve speed or usability.

The family tree was a bad excuse for an example to show a little bit of the syntax, something simple that still shows how things look. Yeah, not surprised that you are using it for everything, wasn’t even one of the first Prolog compilers written in Prolog? I think I read that in the Art of Prolog. Probably the biggest difficulty when I’m learning a new language for learning’s sake, to figure out what does it excel at, what it can be used for. That does not come from tutorials but experience and needs more than 1 month. Prolog so far is pretty mind-blowing for me, I’m sure I will keep it up later as well.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.