(This document is in process, it will be continually updated until completion. Please be patience).
Right now, I am learning how to program in PROLOG (PROgramming in LOGic) in my course of programming languages at Binghamton University. I must confess that I find it to be very different to what I thought it would looks like.
Prolog was mainly used for artificial intelligence and computational linguistics (http://en.wikipedia.org/wiki/Prolog). I would really like to know if it is used outside of the academic environment. If anyone have any info about this let me know.
The following article at http://www.kuro5hin.org/story/2004/2/25/124713/784 written by tkatchev says that Prolog can be used for hacking. Even do I agree with tkatchev that is plausible, I would love to see an actual example. Some encryption braking, etc. Also, I agree that the way it is presented could be more effective if applied to real life uses than IA.
At the bottom I will add a list of links related with this topic.
Prolog is a programming language that let the programmer write a declarative program instead of a procedural program. Just to make sure we are in the same page, a declarative program is when the programmer used “truth and logical deduction” (Barták). In declarative programming, the programmer must use a descriptive style that let know which are the relationship between the entities (Brna).
In a procedural program, the programmer indicate the machine what to do, step by step.
Prolog perform the execution of the program in an tree dept-first fashion or other way to call it: First Order Predicate Logic. Later I will go more in deep and clarify this.
Let start with installing the basics:
- Install a text editor. It seems that Emacs is the right editor for this
- If you have Ubuntu, you can install the emacs editor for prolog. Just go to the Ubuntu Software Center. Type Prolog in the search box and install “Emacs major mode for editing Prolog code” [prolog-el]
- Install one of these prolog compiler:
Prolog works using atoms, terms, variables, facts, rules, recursion, and queries between others. Little by little we will be introducing this concepts.
Lets analyse the following examples:
1. pens have inside ink
a. This should be written as:
b. Notice that pens and ink do not have the first letter in capital. Words that are start with capital letters are considerate logical variables.
Logical Variable: Pens
No logical variable: pens
c. A predicate cannot be a logical variable
Wrong: Have_inside(pens, ink).
Correct: have_inside(pens, ink).
d. pens and ink are called atoms (or constants).
e. This functor is a prefix called a relationship (or predicate).
f. This predicate have a arity (number of arguments) of 2.
i. A predicate with no arguments is a single proposition, a predicate with one argument is unary, with two arguments is binary, with three arguments is ternary, …
g. This is a clause therefore they must end with a period (.) and an space after or with a period(.) and a new line.
h. More examples:
i. pepe is bold:
ii. juan saw tim by the coffe shop:
saw(juan, tim, coffee_shop).
2. In the previous example, we named atoms (or constants) and logical variables
a. Examples of atoms:
countryA, –+–, ‘Hello World’ (Notice that we are using a single tilde (”) not a double tilde (“”))
3. Difference between a goal and a unit clause
b. Unit Clause:
What are the difference between both? The period at the end. A goal is called a unit while a clause is called a non-unit. More details about this later.
4. Multi Clause: We can have more than one clause with the same name for example:
In this case we are saying: the square root of 1 is 1 OR the square root of 4 is 2 OR the square root of 9 is 3.
Every time you use the period(.) and a newline, this is the equivalent of saying OR.
5. Rules: Rules are non-unit conditionals in Prolog. For example:
a. If tim have a job, then tim can pay his bills
i. :- is the equivalent of if
ii. This is a non-unit clause
b.Let say we want to use a rule with a variable:
i. if a number is divisible by 1 AND also the number is divisible by three then the number is odd
I. The comma (,) is the representation of the statement AND. If number_divisible_by_one is true AND number_divisible_by_three is true, then number_is_odd is true.
ii. classes are cancel if the day is a holiday OR the professor is sick
iii. There is an special case in which two rules seems to be talking about the same variable but they are not:
I. In this case it says that a number is divisible by one is the number is odd or a number is divisible by three if the number is odd. Believed or not, we are not talking about the same variable. Why? Because “local variables cannot be overwritten with a new value” (Paul Brna). Example:
A. if we say that X = 1 and later we try to change this variable (X = 2) this will not work for Prolog
I will try to continue this topic in another posting.
© 2010, Alejandro G. Carlstein Ramos Mejia. All rights reserved.