Categories
Programming

Language of the Month: Prolog, part 2

If something, then this is a belated post. Prolog was the language of July and now it’s September. Anyway, before I completely fail, let’s just wrap it up and go on the next language with this one month hiatus in August.

I really enjoyed the language and one month is indeed barely enough to start doing something useful. So I have to come back to it again and maybe keep reading about it in the meantime. It’s actually quite interesting to try to figure out Prolog code on paper, without actually running something. I think one of the books I was reading had plenty of exercises like this: without running the code, can you guess what is it doing? Needless to say, there were plenty of tricky bits/

From the comments

During two months, some additional resources did show up. A dear commenter on the previous post recommended me the following book: Richard O’Keefe: The Craft of Prolog. I got about a third, maybe halfway through it and it’s interesting, written long time ago in a style that since gone out of fashion, unfortunately. It’s a programming book that is fun to read and one can see that the author is very knowledgeable. Some aspects of the book didn’t age very well, though. The author keeps comparing Prolog to other languages – many of them are not very widely available either. Also, some of the language features are specific to his version of Prolog, that wasn’t the same one I was using. Would recommend, though!

This last part, the different implementations of the same language, can be a real problem. Of the three compilers available for me, all of them had specific strengths and weaknesses. Guess they are converging, but not quite yet. So far I was mostly using SWI Prolog, but this might change in the future.

The said commenter also recommended a cross-compiler, doing Prolog-to-C magic, for portability and other goodness. Can grab it from here: http://ftp.shaw.ca/irvinsh/prolog.tar.gz I haven’t had time to try it yet, but once I have, I’ll do a comparison.

From the web

From a friend’s recommendation I was checking out a site with free textbooks. They are all advertisement-sponsored, which is an intriguing idea (for another post). The IT section had not one but two books on Prolog: Prolog Techniques and Applications of Prolog.

Two prolog book covers
Free textbooks on Prolog from Bookboon

This latter one has a Hungarian author so I’m even more intrigued, we used to have great computer scientists (John von Neumann / Neumann János, anyone?), so I hope we keep up that tradition. (Oh, yeah, and had great physicists as well, maybe I can do more on that front later).

I was only skimmed them a little bit, but looks like these will be good addition for my “programming for fun and efficiency” library.

Will update the original LotM:Prolog page with these links as well. Now onto September’s Language, fortunately I have idea what I want to learn for the next ~3.5 weeks. October will be something Artificial Intelligence related since I signed up for the AI-class.

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