Milktrader

Iterating Until Convergence

Sunday, July 31, 2011

Logical Nonsense

In 1979, rock band Supertramp released their Logical Song. It's a plaintive melody that compares two sets of values with an obvious bias against acting "quote" -- logical. But what's wrong with logical? Isn't the world made that way, and doesn't our ability to perceive it correctly rest on our understanding of its constraints? Our long-haired, falsetto-voiced friends from the 1980s may have been unto something though. Sometimes logic will lead you to draw silly conclusions.

A good way to test this idea is to run a simple program in Prolog, the logic programming language. This obscure little programming language is particularly useful in sorting out logic problems from scheduling applications to Bayesian networks. It does voodoo on facts and rules to answer questions. Let's try it out with some simple facts. I'm using the Yap implementation by the way. The following code snippet looks like a function, but read it differently. Read the following snippet as saying "euro is the currency of greece". Ignore the lower case for now. It must be. Look into it later if it's important to you.

          currency(euro, greece).

This is a fact in prolog. It uses it to build its perception of the world that you're describing. Let's put some more in our program.

    currency(euro, germany). 
    currency(euro, france).
    currency(euro, spain).
    currency(euro, portugal).
    currency(euro, italy).

There are more we can add, but that'll do for now. Next, we'll create a rule to expand the knowledge of the program.

        friends(X, Y) :-
        currency(Z, X), 
        currency(Z, Y).

We've introduced some variables and they're upper-case. Again, it must be so. We've also introduced a strange symbol with :- that looks to separate two parts of an argument. Indeed, that's exactly what it does. Sometimes its called the neck because it joins the head to the tail. Prolog considers the tail first and if it is true, applies the head to its knowledge as being true. Read the tail first. If Z is the currency of X and Z is the currency of Y (they share the same currency), then ... read the head ...  X and Y are friends.

Save the above script in a file called fiasco by doing something simple from command line such as this:

      $ vi fiasco

:wq it and in the same directory start up a Prolog session and type in the following after the ?- prompt:

    ?- [fiasco].

This should get the program ready for query. Let's ask a question, which is basically the tail of a predicate.

    ?- friends(greece, germany).

    yes

We ask if Greece and Germany are friends and based on the knowledge that we've input it tells us that yes it's true. They are friends. This is logical and though you may argue it's nonsense, it really isn't because we've made it so. But now let's push this idea a little bit more.

    ?- friends(greece, greece).

    yes

Well, there you have it. Greece is friends with itself! Now that is truly ridiculous. But it is logical because it meets the requirements we've put in place. It turns out that logic sometimes needs a little filtering to make sense, something humans are quite good at. And since we're so good at it, let's let Prolog in on our secret with the following snippet, added to the friends rule:

    dif(X,Y).

You'll need to change the period after the previous tail sequence to a comma because comma is a conjunction operator while the period concludes the tail.

To recompile the program after you've edited it, type in the following:

    ?- [-fiasco].
 
You can't be friends with yourself because friendship is a relationship between two, duh Prolog. You don't get by with a little help from yourself, you get by with a little help from your friends.  It has been written in song, and it is true.
  

0 comments:

Post a Comment