Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

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 perlmonks.org 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('socrates.pro'). compiling /home/Zaxo/socrates.pro for byte code... /home/Zaxo/socrates.pro 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 socrates.pro 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.

Replies:

  1. merlyn, thanks for the pointer. CPAN.pm 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,
Zaxo


In reply to Perl and Prolog by Zaxo

Title:
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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (7)
    As of 2014-09-20 18:19 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (160 votes), past polls