Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Perl and Prolog

by Zaxo (Archbishop)
on Feb 04, 2002 at 10:11 UTC ( #143198=perlmeditation: print w/replies, xml ) Need Help??

I should subtitle this "What I did on my PerlMonks Vacation". We all had to find another profitable use of time during the server move hiatus. This article is about what I did, and how Perl helped.

It is nearly a FAQ for someone in CB to ask, "What other computer language should I learn?" Plainly there is no single answer, but the one I usually give is "One that's completely different." Forth, Lisp, and Prolog are examples of what I mean: languages which can burn new pathways into the brain. While was moving, I decided it was time to take my own advice and learn some Prolog.

I got a gnu implementation called gprolog. I also found an introductory book and an elementary tutorial online. Many Prolog resource sites are peppered with dead links, and the ISO working group for Prolog seems to have withered. Probably no HR clerk will line you up for untold wealth seeing Prolog on your resume. Its dollar value is in the brain pathways I mentioned.

I've barely advanced to baby talk in the language, but I'll try to convey some of its flavor. Prolog is called "rule based" or "logic programming". A program consists of a sequence of assertions, called "predicates". Here is a classic example (adapted from the tutorial):

% this is a comment, % is like # in Perl % All men are mortal mortal(X) :- man(X). % Socrates is a man. man(socrates).
If this immortal wisdom is loaded in an interactive session ('| ?-' is gprolog's baroque prompt):
$ gprolog GNU Prolog 1.2.8 By Daniel Diaz Copyright (C) 1999-2001 Daniel Diaz | ?- consult(''). compiling /home/Zaxo/ for byte code... /home/Zaxo/ compiled, 6 lines read - 382 bytes written, 61 + ms yes | ?- mortal(socrates). yes | ?- mortal(V). V = socrates yes | ?- mortal(aristotle). no | ?- halt. $
Knowing enough from the first two assertions, Prolog was able to confirm the mortality of Socrates with a 'yes'. The previous 'yes' confirmed that was truly consulted (unfortunately, the other conventional extension for Prolog files is '.pl').

How does this work? The online book I cited explains, in a chapter called The Box Model of Execution. Each predicate is described as a black box with two inputs, Call and Redo, and two outputs, Fail and Exit. If a predicate fails, execution returns via redo to the previous predicate.

This seemed vague and unsatisfactory to me until I hit a trigger word -- backtracking. Light dawned: Prolog acts like Perl's regex engine! In fact it acts like a hyperextended dynamic regex engine, whose 'patterns' can be modified, combined, chopped, sliced and diced.

Perl gave me enough of a Prolog-type mental pathway to give me a flying start. Now I know for sure that studying Prolog will improve my Perl. That is a hell of a good bargain.

I'm gathering more impressions and ideas as I go. If there's interest, I'll enjoy airing them here from time to time.

Update: Minor cleanups in text. Added queries to show a more active kind of response from Prolog and confirmation that it never heard of Aristotle.


  1. merlyn, thanks for the pointer. wrote its own makefile.PL but that one wasn't very successful. I'll install by hand, than see about modernizing the build system. I'd been thinking in terms of a not-yet-existant Inline::Prolog, which would make use of the gprolog compilers (more about them below).
  2. Hanamaki, it appears that Goddard's Language-Prolog-Interpreter package has a namespace conflict with Shirazi's. I'll certainly look at both.
  3. FoxtrotUniform, funny you should mention Perl6 :) One of my other observations is on the close resemblence of Perl6's proposed architecture to that of gprolog.
++ all around to you guys.

After Compline,

Replies are listed 'Best First'.
Re: Perl and Prolog
by merlyn (Sage) on Feb 04, 2002 at 14:25 UTC
      True for the original Prolog-alpha by Jack Shirazi, but Lee Goddard's Language-Prolog-Interpreter has a Makefile.PL and is (maybe) maintained. merlyns link points two both distributions.

        Hanamaki - actually, Lee only did a little work on it a few years ago, and is waiting for someone to feel the same way. In other words, if anyone fancies working on it, please ask and I'll transfer ownership. I think the original author is credited in the POD. Nemeste Lee Goddard
      you can be unifying and verifying under Perl!
      I have often wanted to write an article called AI::Prolog::DontBother It is much easier to map human reasoning to Perl than to Prolog. Actually CLIPS is a pretty good rule/expert system, and Mercury is a great step forward past Prolog, integrating functional and logic programming.

      And a good book on the subject is David H.D. Warren's "Computing with Logic: Logic Programming in Prolog" which moves through propositional, predicate and functional logic (the last of which is what prolog uses).

      An implementation of the first (propositional) logic is available in AI::Proplog.

      But like I said, I dont think Prolog is really any more powerful than Perl and even though object and functional and procedural mechanisms are available in Perl, logical ones are not here and not apparently needed. ref Prolog-Perl Golf Association

        Um... No. For people who can only think procedurally, Prolog is very, very hard. For many things, it's very unnatural. But when you have a lot of complicated, interrelated data, Prolog is amazing. You will never ever want to go back to using databases again.
Re: Perl and Prolog
by FoxtrotUniform (Prior) on Feb 04, 2002 at 20:01 UTC

    I'm TAing a LISP/Prolog course at the moment, and because I didn't learn Prolog particularly well when I took the course as a student, I volunteered to write the sample solutions for the Prolog assignments. In the year or so between taking the course and TAing it, I learned Perl, and found out about regexes.

    I now have an excellent mental model of Prolog's backtracking system. (Yeah, I suppose this is a glorified "Me, too!" post.) I expect this to make (re)learning Prolog much easier, and I expect (re)learning Prolog to drastically improve my regex skills.

    And it looks like Perl 6 will have much better support for logical programming, which makes the exercise even more worthwhile.

Re: Perl and Prolog
by Hanamaki (Chaplain) on Feb 04, 2002 at 20:05 UTC
    I have tried quit a few programming languages, but Prolog is the only language I feel as much fun as with Perl. Both languages are perfect to just write down your ideas into code, aka good prototyping languages and good programming languages. If you don't understand Zaxo's or my excitement, give Prolog a try.

    ...happily coding in Perl and SWI-Prolog.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://143198]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2017-02-28 11:15 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (399 votes). Check out past polls.