Prolog is a programming language which works around relations not functions.
Prolog is homoiconic due the structure of its clauses and database that provide it the ability to represent itself.
Note: A language is homoiconic when the language is self-defined. This means that if we run on the interpreter a copy of itself, it should work. Another behaviour of an homoiconic language is to lets the programmer to create, change, and run the code on the fly.
Prolog is not fully reflective, but close, Prolog seems like reflective language because  a program that can resolve a problem by itself by just using relationships, facts, rules, structures, and a database. A fully reflective language should be able to create relationships, rules, structures, and database by itself as needed to find a solution.

In Prolog, facts are clauses with an empty body, eg:

is_mother_of(Maria, Matias). /* Maria is_mother_of Matias */

Rules are clauses with a head and body (head():- body), eg:

/* A Person is_parent_of the Child if (Smile the Person is_mother_of the Child */
is_parent_of(Person, Child) :- is_mother_of(Person, Child).

Predicate are clauses using the same head with different bodies, eg:

/* A Person is_parent_of the Child if (Smile the Person is_mother_of the Child */
is_parent_of(Person, Child) :- is_mother_of(Person, Child).
/* A Person is_parent_of the Child if (Smile the Person is_father_of the Child */
is_parent_of(Person, Child) :- is_father_of(Person, Child).

Note: Every code written before ‘?-‘ are facts and rules. Code written following ‘?-‘ are queries, and all code after ‘?-‘ are the results that prolog returns. eg:

is_mother_of(Marina, Marcos).   /* This is a fact */
is_parent_of(Person, Child) :- is_mother_of(Person, Child). /* This is a rule */
?- is_parent_of(Marina, Marcos). /* This is a query */
True /* This is a result. note that this comment will not show up */
?- is_parent_of(Marina, Lucia). /* This is a query */
False

Lets talk about unification.

/* A unified with B if and only if A and B are the same */
?- =(A, B).
A = B.

/* This is the same as =(A, B) */
?- A = B.
A = B.

Note that A = 1 + 2 will not give you 3 but A = 1 + 2 which means you are unifying A with 1 + 2.
If you to obtain a mathemarical result you should use the following code, eg:

?- A is 1 + 2.
A = 3.

The follows are some examples of unification:

/* a is a constant (also known as an atom) */
/* Since constants can unify with themselves then this is true */
?- a = a.
True

/* a and b are constants, but b is not the same as a, when trying to unify a with b, this will give you false */
?- a = b.
False

/* In case you are unifying facts with constants */
?- foo(a, b) = foo(a, b).
True.

/* This case fails since in both facts, at least one of the constants are nor the same */
?- foo(a, b) = foo(a, c).
False.

?- foo(a, b) = foo(b, a).
False.

Unification can also being use to unify a constant with a variable (Variables start with capital letter), eg:

?- A = a.
A = a.

/* In this case for example we can unify A with c and B with d */
?- foo(A, B) = foo(c, d).
A = c,
B = d.

Note that in Prolog, a variable cannot be modified after

You may have notice the comma after A = c. In Prolog, ‘,’ have the same meaning as AND, while a new line or ‘;’ means OR.

Example:

/* Lets say that prolog provide us with an answer */
?- foo(a, X) = foo(a, b).
X = b; /* <- if we add this semicolon ';' instead of a newline to ask another answer (OR) */
False  /* We get a false because there is no other case */

/* Person is_parent_of Child if Person is_mother_of Child OR Person is_father_of Child */
is_parent_of(Person, Child) :- is_mother_of(Person, Child).
is_parent_of(Person, Child) :- is_father_of(Person, Child).

/* Person is_mother_of Child if Person is_parent_of Child AND Person is_female */
is_mother_of(Person, Child) :- is_parent_of(Person, Child), is_female(Person).

/* Another example of AND and unification */
/* A is unified with B AND A is unified with constant z AND B is unified with Y */
?- A = B, A = z, B = Y.
A = z, /* A was unified with z */
B = z, /* Since A = B and A = z then B = z */
Y = z. /* Since B = A = z then Y = z */

Explicit versus Implicit:

Explicit:

relationship(Z, Z).
is_related(X, Y):-
    relationship(X, b),
    relationship(a, Y).
?- check(First, Second).
First = b,
Second = a.

Implicit:

relationship(Z, Z).
?- is_related(relationship(First, b), relationship(a, Second)).
First = a,
Second = b.

More examples of simple Facts and Rules:

Example 1: Lets do a modification of the game of Yahtzee (http://en.wikipedia.org/wiki/Yahtzee).
Yahtzee is played with 5 dices, and depending of the combination you can obtain: Chance, Three-Of-A-Kind, Four-Of-A-Kind, Full House, Small Straight, Large Straight, and Yahtzee.
We are going to do only the Yahtzee combination for this example and the user will input values from 1 to 6.

/* Establish the facts and rules */

/* Yahtzee: All five dices show the same face. */
yahtzee(A, A, A, A, A).
?- yahtzee(5,5,5,5,5).
True

?- yahtzee(5,4,5,4,5).
False

Example 2: Lets do some adding. This is a way that you can do adding by using successor(0)
as a numeral.

/* When 0 is the same as 0, then Y = 0 */
add(0, Y, Y).
/* */
add(successor(X), Y, successor(Z)) :- add(X, Y, Z).
/* Example of behavior when used in the wrong way */
?- add(A, B, C).
A = 0,
B = C;
A = successor(0),
C = successor(B);
A = successor(successor(0)),
C = successor(successor(B));
... /* This keeps on going if you use OR ';' */
/* Example of behavior when used in the wrong way */
?- add(A, 0, C).
A = 0,
C = 0;
A = successor(0),
C = successor(0);
A = successor(successor(0)),
C = successor(successor(0));
... /* This keeps on going if you use OR ';' */
/* Example of behavior when used in the wrong way */
?- add(0, B, C).
A = 0,
B = 0.
/* Example of behavior when used in the wrong way */
?- add(0, 1, C).
false.
/* Do 1 + 1 = 2: successor(0) + successor(0) = successor(successor(0)). */
?- add(successor(0), successor(0), X).
X = successor(successor(0)).
/* Do 2 + 1 = 3. */
/* successor(successor(0)) + successor(0) = successor(successor(successor(0))). */
?- add(successor(successor(0)), successor(0), X).
X = successor(successor(successor(0))).

Notice that a rule can behave different depending who you define your inputs and outputs.

Example:

/* Here we find that we are doing -1 + 1 = 0 */
?- add(successor(0), X, successor(0)).
X = 0.
/* Here we have 2 - 1 = 1 */
?- add(successor(0), X, successor(successor(0))).
X = successor(0).
/* While this give you X = 0, it would fail in a different case */
?- add(X, successor(0), successor(0)).
X = 0
/* Here this fails */
?- add(X, successor(successor(0)), successor(0)).
false.

In the next submittion we are going to see recursion in Prolog.

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

Share

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