Daily Archives: July 30, 2013

Prolog and Logic Programming

hasFlavor(F) :- instock(F).

This is the first Prolog program I’ve written. Doesn’t look too impressive, does it? These two lines of code tell me that if a flavor is in stock, then we have a flavor. Then it states that the “chocolate” flavor is in stock, which therefore means that hasFlavor(chocolate) is true. If I were to query the program, saying

?- hasFlavor(chocolate)

it would print out Yes.

I wrote another prolog program using ASP (Answer Set Programming) that represents a family.

sibling(X,Y) :- parent(Z,X), parent(Z,Y).
mother(X,Y) :- parent(X,Y), female(X).
spouse(X,Y) :- parent(X,Z), parent(Y,Z).
female(kimberly). female(katherine). female(joanne). male(david).
parent(joanne, kimberly). parent(joanne, katherine).
parent(david, kimberly). parent(david, katherine).

This program prints out: 

parent(david,katherine) parent(david,kimberly) parent(joanne,katherine) parent(joanne,kimberly) male(david) female(joanne) female(katherine) female(kimberly) spouse(david,david) spouse(joanne,david) spouse(david,joanne) spouse(joanne,joanne) mother(joanne,katherine) mother(joanne,kimberly) sibling(katherine,katherine) sibling(kimberly,katherine) sibling(katherine,kimberly) sibling(kimberly,kimberly) 

Unlike other types of programming, in logic programming everything happens at once. There is no way to tell a program to perform steps in order, like you would in Java. How this works is that when it reaches statements like hasFlavor(F) :- instock(F), it searches for all terms that can substituted in for the argument (i.e. instock(chocolate)) and puts them in. In this way, the code is not read in a linear fashion, but rather “all at once”.

Free Software?

There is a controversial issue in the programming world that has been around for decades: whether it would be better for software to be free and open to everybody, or whether the current system of licensing and selling software is more beneficial. GNU (Gnu’s Not Unix) supports the former, whilst Bill Gates’ Letter to Hobbyists expresses the latter opinion.

While free and open software sounds tantalizing, there are serious drawbacks to the concept. Writing, debugging, and distributing software requires work – and the truth is, we live in a largely capitalist world where nobody does anything for free. Salaries and pay raises give people incentive to do the best work they can; without it, products that they churn out will be inferior at best. 

On the other hand, the power of the people is not to be underestimated. If software were made free to the public, and if enough competent people felt motivated to improve it, the results would not be unimpressive. However, these are big if’s. In the end it really comes down to your view on human nature; basically, if you handed a silver bracelet to the mob, would you expect to get it back spit-shined and clean, or would you expect it to be tarnished by the grubby hands of the masses?