Friday, May 16, 2014

Prolog



If you are looking for a completely different programming experience you must simply try Prolog. Using this language you can simply ask your computer simple questions and it will answer it for you. I did a course this semester in which I came across Prolog programming language for the first time. I was pretty impressed with this language as it was completely different from all the languages that I had seen so far. Prolog unlike traditional programming languages is a logical and declarative language. In fact, it was one of the first programming languages based on logic. It was introduced in 1970 by Robert A. Kowalski at Edinburgh University. This language is used widely for artificial intelligence. 

How Prolog works is that it makes deductions based on facts and rules, both of which are specified by the user. A fact describes the object and its relationship with the object. For example, if we consider Silver is shiny as a fact then it is expressed as shiny(Silver) in Prolog. Here 'Silver' is the object. Another example is likes (Swapna, chocolate). This defines a fact that Swapna likes chocolate.

A rule describes a relation between different facts. For example friends(X,Y) :- likes(X,Y),likes(Y,X). Here ':-' is 'if' in Prolog and ',' means 'and'. So this rule means that X and Y are friends if X likes Y and Y likes X. We need to make sure the rules are valid. Every rule must end with a period. The left hand side of the rule has to be a single literal and cannot be negated. In the above example, if the left hand side is not(friends(X,Y)) then it is not a valid rule.

In Prolog the user creates a database with rules and facts. Having done that, the user can ask questions about the various objects and their relationships. Below is a snippet of how the prolog interface looks like.



Let us consider a database example. For instance we can create a database like this:
likes(alice, diamonds).
likes(alice, john).
likes(john, alice).
buys(john, diamonds) :- likes(john, alice).

After the database is created one can ask questions like this:
?- likes (X, diamonds). Would ask the question who likes diamonds
X = alice;                   The answer displayed would be Alice
?- likes (X, john).     Here the question asked is who likes john
X = alice;                Again, the answer displayed would be Alice

A Prolog program database can be thought of a collection of predicate logic statements. The way evaluation takes place is that once you query, prolog performs substitutions based on the logic statements to find the right answer. The searching algorithms are in built in the Prolog compiler.
This predicative logic lends itself naturally to AI programming and thus makes it a very powerful language to use in the fields of artificial intelligence, weather prediction, defense applications to name a few.

References:

1 comment:

  1. This was a great blog describing what exactly Prolog is. I did not know what Prolog was before I read this and it was easy to read through and explained it perfectly to a person, like me, who didn't have any prior knowledge about this topic. The examples are concise and clear, and the screenshots are an added bonus to help explain the topic more. I would recommend this blog post to anybody interested in learning more about Prolog.

    ReplyDelete