Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

comment on

( #3333=superdoc: 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,

In reply to Perl and Prolog by Zaxo

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (2)
    As of 2021-02-25 03:11 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found