Curried Predicates in Prolog

Prolog is not a functional language - Not yet, at least. Sometimes, that’s a shame, especially when you want to use meta-predicates such as “forall” or “findall”, and use a predicate with partial arguments as the goal - As you would have done in functional programming languages.

Luckily, that specific scenario is actually possible - In SWI-Prolog, at least. The “call” meta-predicate, and its derivatives, may take a functor and extra arguments as parameters. Then, the extra arguments are concatenated to the arguments of the functor, and the resulting functor acts as a goal.

If this is all Prolog-Chinese for you, here’s an example:

Continue Reading »

No Comments »

Guy Wiener on December 4th 2009 in Programming

Prolog Hacking

“Prolog” and “Hacking” are not two terms that are usually found in close proximity. In a matter of fact, most hackers I know do not consider Prolog as a programming language. Prolog is just that thing you learn in a principles of programming languages course, that demonstrates “what are declarative languages”. Afterwards, you forget about it and go back to program in some imperative language, just as Kernighan & Ritchie intended you to do.

You couldn’t get it more wrong.

Continue Reading »

1 Comment »

Guy Wiener on September 12th 2009 in Programming

Logic Programming, Models and Bugs

A couple of days ago I finally wrapped up an assignment in the Logic Programming course. It took me some time, since I couldn’t find a nasty bug in it. Very embarrasing.

The assignment itself was a classic case of model-driven development, at least in its raw form: The model was half-a-dozen or so statements in first order logic. Based on this model I wrote about 100 lines of Prolog code. The Prolog program generated several hundreds of CNF clauses. A CNF solver took these clauses as a long list and gave a solution. The only problem was, of course, that the solution was wrong.

I looked for the bug inside my 100 lines of Prolog code, in vein. It wasn’t there. The Prolog program was just a simple CNF generator that worked well. It took some data and generated CNF clauses according to those logic statements that I wrote. That’s it.

The bug was, of course, in the model. The first-order logic formulas were wrong. I used a redundant logic variable that created un-needed constraints. I removed this variable and simplified the formulas, and then re-wrote the Prolog program. It took a couple of hours and, amazingly, worked perfectly from the first run.

The size of the bug was a single letter. My original formulas contained the variables “N”, “E”, “C”, “V” and “B”. “C” was redundant. Removing it and re-writing the statements solved the problem.

Moral: Models matter. Every program is based, explicitly or implicitly, on a set of ideas and assumptions. In this assignment I had to write them down, so I got a glimpse of their power. A single wrong assumption can be the difference between “working” and “not working”.

The Devil’s programer would say: “We, at hell.com, solved this problem. We don’t have a model for our programs, so the model cannot be wrong.” He’s lying, of course. All programs derive from a model. In most cases, it is a mental model. It doesn’t mean that it does not exist or that it cannot be wrong. It just means that it is much more difficult to debug it.

No Comments »

Guy Wiener on March 8th 2009 in Programming

On Speed and Simplicity

Albert Einstein once said “Things should be as simple as possible, but not simpler”. I learned that lesson the hard way once: It was during my first degree in the Technion, in a course on parallel computing. The assignment was to solve the travelling salesman problem for 12 nodes in minimal time, using 5 dedicated computers connected by a fast bus. My partner was Zvi Avidor, a Math-and-CS grad. We came up with a really good distributed algorithm: No master computer, automatic load balancing, no waiting for the slowest machine, etc. We deployed it on the test platform and wait. After half an hour, the system threw us out, to give other students their turn. The smell of troubles started to rise.

Continue Reading »

1 Comment »

Guy Wiener on January 7th 2009 in Programming