Saturday, November 3, 2007

Introduction to Prolog (Part 2)

Examples of Facts

  • female(alison).
      • “alison is a female”
  • has_feathers(sparrow).
      • “sparrow has feathers”
  • father(“John”,”Sam”).
      • ”John is the father to Sam”
  • mother(”Jeanette”,”Mary”).
      • “Jeanette is the mother to Mary”
  • bird(type(sparrow), name(toto))).
      • “toto is a bird that is of sparrow class”

Examples of Rules

  • You should read the prolog operator “:-” as “if ”, “;” as an “or”, while “,” as meaning “and”.
  • Rules consist of a head and a body. For example the rule “a(X) :- b(X), c(X).” has head “a(X)” and body “b(X), c(X)”.
  • All arguments beginning with a capital letter (such as X and Y) are variables. Variables don't have to have values). Any constant should NOT begin with a capital letter else it will be treated as a variable.
  • sister(X,Y) :- father(Z,X),father(Z,Y).
  • “X and Y are sisters if Z is father to X and father to Y”

Example of a Prolog Program

PREDICATES

son(STRING, STRING)

CLAUSES

son("John", "Dan").

son("Allan","Dan").

Goal son("John", "Dan").

Example 2

PREDICATES

son(STRING,STRING)

sister(STRING,STRING)

brother(STRING,STRING)

married(STRING,STRING)

sister_in_law(STRING,STRING)

CLAUSES

son("John", "Dan").

sister("Mary","Suzan").

brother("Harold", "Larry").

married("John", "Mary").

married("Larry", "Sue").

married("Harold", "Emma").

CLAUSES

sister_in_law(A, B):-married(A, C), sister(C, B).

sister_in_law(A, B):-brother(A, C), married(C, B).

GOAL

sister_in_law("John", Z).

Example 3

domains

brand, color = symbol

age, price = integer

milage = real

predicates

car(brand, milage, age, color, price)

clauses

car(chrysler, 130000, 3, red, 12000).

car(ford, 90000, 4, gray, 25000).

car(datsun, 8000, 1, red, 30000).

goal

car(renault, 13, 3.5, red, 12000).

/* car(ford, 9000, gray, 4, 25000).

car(Make, Odometer, Years_on_road, Body, 25000).

car(Make, Odometer, Years_on_road, Body, Cost) and Cost < style=""> */

Introduction to Prolog (Part 1)

History of Prolog

  • Prolog was developed at the University of Marseilles, France by Alain Colmerauer in the early 1970s as a convenient tool for PROgramming in LOGic.
  • In 1983 Japan chose Prolog as the main language for 5th generation computers.
  • Turbo Prolog was the first implementation of Prolog for IBM PC that came out in 1986.
  • Visual Prolog is developed by PDC in Denmark.

What Can Prolog be Used For?

  • Expert Systems.
  • Artificial Intelligent Systems.
  • Translate languages, either natural human languages or from one programming language to another.
  • Construct Natural Language interfaces to existing software.
  • Control and monitoring of industrial processes.
  • Theorem proving software in which deductive reasoning capabilities are used.
  • Development of Relational Databases.
  • Produce prototypes for virtually any application program.

In what Areas Prolog is Distinctively Better?

  • Prolog is a Descriptive Language.
  • Prolog Uses Facts and Rules.
  • Prolog can make Deductions.
  • Pattern Matching ability.
  • Execution of Prolog Programs is Controlled automatically.
  • Prolog is very User-friendly and has a short and simple syntax.
  • Efficiency of Application Programs is almost as good as for C++ programs.

Glossary of Terms

  • Atom: A relation possibly involving objects or variables.
  • Domain: Specifies the types of values the objects may take in relation.
  • Clause: A fact or rule for a particular predicate, followed by a period.
  • Fact: . Facts declare things that are always true. It is a relation between objects. In the fact;
      • likes(john, mary)
  • likes is the name of the relation and john and mary are objects.
  • Rule: Rules declare things that are true depending on some conditions. It is a relationship between a fact and a list of sub-goals which must be satisfied for that fact to be true.
  • Predicate: Every Prolog fact or rule belongs to some predicate, which specifies the name of the relation involved and the types of objects involved in the relation.
  • Backtracking: The mechanism built into Prolog whereby, when evaluation of a given sub-goal is complete, Prolog returns to the previous sub-goal and tries to satisfy it in a different way.