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

comment on

( [id://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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-24 04:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found