Python class ( Prolog ) ???

Joseph A. Knapka jknapka at
Sat Sep 7 01:53:55 EDT 2002

Terry Hancock wrote:
> Well, I haven't seen any posts go by on the python-logic
> SIG, so I thought I'd carry over this topic from the
> python list as a conversation starter:
> While we're on the subject -- I've taken a couple of
> tries at Prolog in the past, but didn't really get
> anywhere with it.  I understand the concept of
> expressing logic in code, but got rather bogged down
> when it came to any practical examples.

When done properly, Prolog is beautiful, easy to understand,
easy to maintain. It is not well-applicable to a wide
range of problems, as is Python; largely because
procedural algorithms, which are the meat-and-potatoes
of mainstream IT, are rather painful to express in
Prolog. But if you happen to have a problem whose
structure is easily expressed in first-order predicate
logic, you can often simply write down that problem
description in your editor and run it as Prolog code.
Many problems to which we typically apply procedural
algorithms are easily expressed in FOPL; we don't
do it simply because efficient procedural algorithms
have been around for far longer than practical
declarative-logic languages.

> Python, with it's object/procedural focus seems like
> it would complement Prolog well (or vice-versa). Can
> anyone fluent in both compare what sort of applications
> you might apply a prolog-python solution to?  Where
> would you draw the line between the two? What would
> using prolog help me with? What's a good problem to
> solve as a learning project?

I would apply Prolog to a problem that is naturally
expressed as a search of a solution space for
solutions that satisfy some set of constraints. Of
course, any problem -can- be expressed in such
fashion; the question is, is it a -natural-
description. For trivial examples of the kind
of problem I mean, see any book of logic
puzzles of the "The man who wears blue lives in
the house closest to the river; the man with the
parrot lives next to the man who wears green"
variety. In this sort of problem, it's not obvious
how I would efficiently find an arrangement of
houses and owners that satisfies all the given
constraints, short of exhaustive generate-and-test;
but I can easily write Prolog rules expressing the
constraints themselves, and let the interpreter
perform the search for me. If I express the
constraints properly, the result will be as
fast as anything I could code up by hand in
Python or perhaps even C.

Prolog is (IMO) not particularly suitable for
applications that involve lots of user interaction
(though this may be because the Prolog UI toolsets
I've used have generally been thin wrappers around
underlying procedural toolkits - declarative UI
design could be quite powerful, as in XUL, etc).
The UI is probably best expressed using something
like Python. It's also not a good choice for
a problem that is mainly a long grindy numeric
computation - there are better languages to
express such things. When well-known, fast procedural
algorithms are applicable, apply them. Prolog
is good when you know a lot about the syntax of the
solution space, so to speak, but not a great deal
about its semantics -- "I ain't never seen one
before, but I'll know it when I see it!"

Incidentally, there's a Prolog interpreter buried
within the WinNT kernel; it handles configuration
of network devices at boot time (among possibly
other things).

> Any more book or website
> recommendations for a learner?
> Conversely is there anyone who thinks Prolog is
> a waste of time for a Python programmer, or that
> it would be better to use something else in such
> a role?

I think that any programmer would benefit from learning
Prolog well (and Lisp, and Haskell, and assembler, and C).
It will probably be quite a drastic change if all you
know is OO and structured programming, and when you
"get it" you will know things about programming that
you didn't before. (Disclaimer: Prolog was the second
language I learned really well, after BASIC :-) so my
experience with it is not typical. When I learned C
later on, *that* made my brain hurt.)


-- Joe
  "I'd rather chew my leg off than maintain Java code, which
   sucks, 'cause I have a lot of Java code to maintain and
   the leg surgery is starting to get expensive." - Me

More information about the Python-list mailing list