BUU asked:
... I was wondering if I might impose upon your good nature and inquire as to exactly why you feel people should learn Logic Programming in general (and prolog in specific?)? Is it just a general notion that the more paradigms we're exposed to, the more options you have when devising solutions?
First: the reason I'm using Prolog for this is because I don't know Mercury, Gödel, or any other language that supports logic programming. However, since Prolog is far and away the most popular, I wasn't too worried about this. In other words, I think logic programming is important; the fact that I'm using Prolog is almost accidental. It also doesn't hurt that Prolog is very easy to parse.
Now, on to the benefits!
Do you ever use regular expressions? They're a simple form of logic programming. Of course, there's nothing in regexes that I really can't do with substr and a lot of imperative programming, but that would be silly if I have regular expressions at my disposal.
Larry Wall also has some interesting things to say about logic programming (emphasis mine):
Transactions are not just something that database programmers have to worry about. I think training in transactional thinking tends to fall through the paradigmatic cracks because transactions can't be classified as functions or events or objects. If anything, a transaction is most closely related to the logic programming paradigm, which is undertaught. A transaction can be viewed as a sort of hypothesis that can either be proven or disproven. A partially proven hypothesis is of little use.
Logic programming is also used quite extensively in expert systems, natural language processing and artificial intelligence because it makes so many of the ideas contained in them very easy to express.
Further, and this is a big plus, well-written prolog Programs can be as close to "programming from the spec" as you can get. For example, here's a little snippet derived from the famous "Clocksin/Mellish" programming text. Can you guess what we're defining?
average_taxpayer(Person) :- not(foreigner(Person)), not( spouse(Person, Spouse), gross_income(Spouse, Income1), Income > 3000 ), gross_income(Person, Income2), ...
That should not be terribly hard to figure out. If you have a program that needs to determine if someone is an "average" taxpayer, that's it. You would have a database with facts containing people's gross incomes or who is married to whom, but Prolog is often that straightforward.
The key in the snippet above is that the various bits express relationships between things. If your program is mostly concerned with how a bunch of things are related to one another, logic programming is an excellent choice, but like regular expressions or closures, the utility is often not very apparent until one actually has a chance to use it. In that respect, it's like a foreign country: you can read about it all you want, but until you live there, it's tough to appreciate its culture.
Update: In the type of programming that most Perl programmers do, we have our data and we keep munging that data until it eventually looks like our goal. In logic programming, we tell the computer what our goal looks like and we let the computer figure out how to get there. Is that straightforward enough? :)
Cheers,
Ovid
New address of my CGI Course.
In reply to Re^4: (why to use logic programming)
by Ovid
in thread Easy Text Adventures in Perl
by Ovid
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |