Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Something my teacher repeatedly say during the lectures was "in logic programming we express relations". Remember this, ponder it, and you'll save yourself some trouble, especially if you already are comfortable with functional languages. Relations are named by predicates and predicates are not functions.

If that is as clear as the sky above the clouds then you can stop reading now. All I'm going to try to do in the rest of this post is to give examples of how these relations work and how they allow you to express programs differently than in other languages that look similar, like Haskell.

Moving on...

A predicate is true if the relation can be true. If there's any uninstantiated variables in the predicate Prolog gives them values that fulfill the relation and keep them for the rest of the scope. This works a bit like capturing groups and backreferences in regexes (like /(.*?)\1/).

An interesting example of this is how the assignment operator = works in Prolog. It's not really an assignment operator. It's a very simple predicate named = and X = Y can be written as =(X, Y). We could've called it eq and written it as

eq(X, X).
which basically says that the first argument must match the second argument. As you see, it's symmetrical, so X = 2 is the same as 2 = X.

I don't expect anyone without prior knowledge of Prolog to understand the following example, but I think it's nice anyway, and those familiar with functional programming may recognize the example.

If we want to manually define the natural numbers, 0 ... Inf, we can do that by writing an inc predicate used as inc(X, Xplus1) and have a base (atom) just called zero. If we define inc as

inc(X, succ(X)). % you don't have to understand this.
the number 2 would be represented by succ(succ(zero)). But here's where the fun comes in. Since predicates express relations we can use it to decrease a value too: inc(Xminus1, X). Here I've said nothing about whether X needs to be instantiated or not. If fact, neither in inc(X1, X2) needs to be instantiated. It just says that after that predicate, X1 and X2 will be two successive numbers:
foo :- inc(X, Y), % Implicit RELATION. X = zero, % MATCH X against the atom zero, impliclity mat +ch Y. something(X, Y). % same as something(zero, succ(zero)). bar :- inc(X, Y), % Same implicit relation. Y = succ(zero), % Implicitly match X against zero. something(X, Y). % same as something(zero, succ(zero)).
It's all about relations.

ihb

See perltoc if you don't know which perldoc to read!
Read argumentation in its context!


In reply to Re: Bringing Logic Programming to Perl by ihb
in thread Bringing Logic Programming to Perl by Ovid

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 avoiding work at the Monastery: (8)
    As of 2014-09-16 00:53 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite cookbook is:










      Results (155 votes), past polls