(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:

  1. Install a text editor. It seems that Emacs is the right editor for this
    1. http://bruda.ca/emacs-prolog/
    2. 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]
  2. Install one of these prolog compiler:
    1. http://www.gnu.org/software/gprolog/
    2. http://www.swi-prolog.org/download/stable (recommended)

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:

have_inside(pens, ink).

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

a. Goal:

miss(maria, jose)

b. Unit Clause:

miss(maria, jose).

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:

square_root(1, 1).
square_root(4, 2).
square_root(9, 3).

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

can_pay_bills(tim) :-


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

number_is_odd(X) :-



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

cancel(classes) :-


cancel(classes) :-


iii. There is an special case in which two rules seems to be talking about the same variable but they are not:

number_is_divisible_by_two(X) :-


number_is_divisible_by_two(X) :-


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.

Related Links:

  1. http://www.learnprolognow.org/
  2. http://kti.ms.mff.cuni.cz/~bartak/prolog/
  3. http://cs.union.edu/~striegnk/learn-prolog-now/html/
  4. http://www.swi-prolog.org/
  5. http://www.cse.unsw.edu.au/~billw/prologdict.html
  6. http://homepages.inf.ed.ac.uk/pbrna/prologbook/index.html
  7. http://www.doc.gold.ac.uk/~mas02gw/prolog_tutorial/prologpages/

Work Citied

Roman Barták, http://kti.mff.cuni.cz/~bartak/prolog/intro.html

Paul Brna, http://homepages.inf.ed.ac.uk/pbrna/prologbook/node11.html

© 2010, Alejandro G. Carlstein Ramos Mejia. All rights reserved.


One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.


Click to Insert Smiley

SmileBig SmileGrinLaughFrownBig FrownCryNeutralWinkKissRazzChicCoolAngryReally AngryConfusedQuestionThinkingPainShockYesNoLOLSillyBeautyLashesCuteShyBlushKissedIn LoveDroolGiggleSnickerHeh!SmirkWiltWeepIDKStruggleSide FrownDazedHypnotizedSweatEek!Roll EyesSarcasmDisdainSmugMoney MouthFoot in MouthShut MouthQuietShameBeat UpMeanEvil GrinGrit TeethShoutPissed OffReally PissedMad RazzDrunken RazzSickYawnSleepyDanceClapJumpHandshakeHigh FiveHug LeftHug RightKiss BlowKissingByeGo AwayCall MeOn the PhoneSecretMeetingWavingStopTime OutTalk to the HandLoserLyingDOH!Fingers CrossedWaitingSuspenseTremblePrayWorshipStarvingEatVictoryCurseAlienAngelClownCowboyCyclopsDevilDoctorFemale FighterMale FighterMohawkMusicNerdPartyPirateSkywalkerSnowmanSoldierVampireZombie KillerGhostSkeletonBunnyCatCat 2ChickChickenChicken 2CowCow 2DogDog 2DuckGoatHippoKoalaLionMonkeyMonkey 2MousePandaPigPig 2SheepSheep 2ReindeerSnailTigerTurtleBeerDrinkLiquorCoffeeCakePizzaWatermelonBowlPlateCanFemaleMaleHeartBroken HeartRoseDead RosePeaceYin YangUS FlagMoonStarSunCloudyRainThunderUmbrellaRainbowMusic NoteAirplaneCarIslandAnnouncebrbMailCellPhoneCameraFilmTVClockLampSearchCoinsComputerConsolePresentSoccerCloverPumpkinBombHammerKnifeHandcuffsPillPoopCigarette