# prolog

Document Sample

```					Introduction to PROLOG

1
PROLOG
   PROgramming LOGic designed in 1972
   Philippe Roussel and Alain Colmerauer of the
University of Aix-Marseille and Robert Kowalski at
the University of Edinburgh
   Niklaus Wirth
   Algorithms + data structure = Programs
   Kowalski
   Algorithms = Logic + Control

2
PROLOG
   Prolog is referred to as a declarative language because all
program statements are definitional.
   A Prolog program consists of
   Facts and Rules
   Two levels of meaning of Prolog programs:
   Declarative meaning
   concerned only with the relations defined by the program.
   Procedural meaning
   determines what will be the output of the program; and
how this output is obtained.
   Programmers should concentrate mainly on the declarative
meaning and, whenever possible, avoid being distracted by
the executional details.

3
PROLOG
   To find a solution to a problem Prolog interprets
the knowledge about the problem domain.

   The sequence of actions that is applied to find
solution to a problem is not determined by the
description of the problem domain (program) or by
the query, but is governed by the control strategy
embedded within Prolog.

4
Propositional Calculus
   The propositional calculus is based on propositions.

   A proposition can be thought of as a logical statement that
may or may not be true. It is made up of objects and their
relationships to each other.

   The simplest proposition, which is called atomic proposition,
consists of single compound term.

   A compound term is composed of two parts: a functor, which
is the function symbol that means the relation, and an
ordered list of parameters
   For example: man(adam) is a compound term where man is
functor or the relation and adam is a parameter.
5
Propositional Calculus
   These propositions have no intrinsic semantics.
   They mean whatever we want them to mean.
   For example, like(joe, monkey) may mean that joe likes
monkey or it may mean that joe behaves like monkey.
   Symbolic logic can be used for the three basic needs of
formal logic.
   To express propositions
   To express relationship between propositions, and
   To describe how new propositions can be inferred from
other propositions that are assume to be true.
   A particular form of symbolic logic used for logic
programming is called predicate calculus.

6
Predicate Calculus
   The names, symbols, and meanings of the
predicate calculus logical connectors are as
follows:
   Negation         a      Not a
   Conjunction      ab     a and b
   Disjunction      ab     a or b
   Equivalence      ab     a is equivalent to b
   Implication      ab     a implies b
   ab     b implies a

7
Predicate Calculus
   Variables can appear in propositions but only
when introduced by special symbols called
quantifiers.
   Predicate calculus includes two quantifiers:
   Universal       X.P For all X, P is true (X is a variable
and P is a Proposition)
   Existential     X.P There exists a value of X such that
P is true
   Examples:
   X.(woman(X)  human(X))
   X.(mother(aasia, X)  male(X))

8
Example
   If p stands for "all fish are smelly" and p is
true then we would like to be able to prove
that "my fish wanda is smelly".

   X.(fish(X)  smelly(X))  fish(wanda)
smelly(wanda)

9
Clausal Form
   Clausal form is a simple form of stating
propositions in predicate calculus.
   The right side of a clausal form proposition is
called antecedent. The left side is called the
consequence because it is the consequence of
the truth of the antecedent.
    Example:
   likes(adam, orange)  likes(adam, citrus)  citrus(orange)

10
Resolution
   The process of inferring a proposition from the
original two propositions is called resolution.
   Example:
   older(asma, jaffery)  mother(asma, jaffery)
   wiser(asma, jaffery)  older(asma, jaffery)
   from this propositions, the following proposition
can be constructed:
   wiser(asma, jaffery)  mother(asma, jaffery)

11
Horn Clauses
   When propositions are used for resolution, only a
restricted kind of clausal form can be used, which
further simplifies the resolution process. The
special kinds of propositions, called Horn clauses,
can be only in two forms:
   A single atomic proposition on the left side (for example)
   Likes (adam, orange)  likes (adam, citrus) 
citrus(orange)
   OR An empty left side (for example)
   Father (ismail, ibrahim)

12
Facts & PROLOG
   Propositions can be stated in two modes:
   one in which the proposition is defined to be true (facts), and
   one in which the truth of the proposition is something that is
to be determined (queries).
   You could express domain knowledge like:
   The Chip Trade (inc.) makes RAM chips.
   In Prolog as:
   The 'make' relationship is called a predicate name in Prolog.
   The objects 'chip_trade' and 'ram_chips' are called arguments.
   The whole of the predicate name, and arguments is called a
predicate.

13
Predicates
   A predicate name has to start with a lower-case
character. A number written as a sequence of
digits (e.g. 23), can be an argument but cannot be
a predicate name.
   Spaces are not allowed in predicate names or
arguments.
   Arguments are separated by commas and are
contained within round brackets.
   There is a full stop/period at the end of the
predicate.

14
Data Objects in Prolog
Data Object

Simple Object         Structure

Constant            Variable

Atom              number

15
   Constant
   A constant is either an atom or a number
   An atom is either a string of letters, digits, and underscore
that begins with a lowercase letter or a string of any
printable ASCII characters delimited by apostrophes.
   Variable
   A variable is any string of letters, digits, and underscores
that begins with an uppercase letter.
   Variables are not bound to types by declarations.

16
How do we work?
   If “the capital of france is paris” then we can
represent this in predicate calculus form as:
   france has_capital paris
   We have a binary relationship (two things are
related).
   The relationship (or predicate) has been given the
name “has_capital” - hence we say that the
predicate name is “has_capital”.
   And in Prolog form by such as:
   has_capital (france, paris).

17
Exercise
   Write Prolog facts for each of the following
sentences:
   nadeem works at Punjab University
   cost of a PIII is 9000
   ahmed is bright
   haroon enjoys riding
   yasmine likes ice-cream
   ail is tall
   ali hits timmy with the cricket bat
   walid travels to mecca by plane
   farid lives at 16 sawan road in Islamabad

18
QUERY - Putting a question
   Prompt
   ?-
   :
   ?
   A query consists of one or more goals
   Each individual goal has to be a predicate

Before you put a question you need to start-up
PROLOG and load/consult the file containing your
program.

19
Query
   A query asks whether there is a relationship
between objects or not?
   Response to your query
   Yes
 If the relationship is true

 Indicates a Success

   No
 If the relationship has not been declared true by a fact

 Indicates a Failure

20
Program and Query
makes(chip_trade, ram_chips).        A program consists of one
makes(wafer_house, cpus).             or more procedures
makes(wafer_house, ram_chips).       A procedure is a collection
makes(nippon_silicon, cpus).          of all facts with the same
makes(taipei_trade, ram_chips).       predicate name
   A query
foreign(nippon_silicon).
domestic(wafer_house).
Contains one goal.
domestic(chip_trade).                A pattern matching
domestic(wafer_house).                process
domestic(hnd).

21
Query
domestic(wafer_house).
   Operationally a query initiates a process of
pattern matching between the predicate in the
query, and the facts in the working memory.
   The argument chip_trade in the fact fails to match
the argument wafer_house of the goal.
   Whenever a goal fails to match a fact, the next
fact below is tried.

22
Query
makes(wafer_house, cpus).
makes(wafer_house, ram_chips).
makes(nippon_silicon, cpus).

   Matching predicates with multiple
arguments.
makes(wafer_house, ram_chips).

23
Query - Exercise
ship_arrival(viking_line, los_angeles, aug9).
ship_arrival(viking_line, seattle, aug11).
ship_arrival(viking_line, osaka, jan5).
ship_arrival(viking_line, san_frncisco, aug9).

Q: Is viking line arriving at san francisco on Aug
9th?

24
Variables
   Instead of a specific object’s name you can
use a variable to ask for which object(s) the
predicate is true.
   A variable in PROLOG can represent any
object, but it can represent only one object
at a time.
   Remember the variables naming rule

25
QUERY with variables
   Which products does wafer_house make?
makes(wafer_house, Product).
   The makes goal will only succeed if all the arguments successfully
match the corresponding arguments in a fact.
   Matches first argument first and then second argument (a variable).
   Matching a variable always succeeds and bind the variable to what
it’s being matched with.
   When a query that contains variables, succeeds - PROLOG reports
the bindings that the variables acquired while proving the query.
   Product = cpus

26
QUERY - Exercise
   Which suppliers make   makes(chip_trade, ram_chips).
makes(wafer_house, cpus).
hard disk?             makes(wafer_house, ram_chips).
makes(nippon_silicon, cpus).
   What will happen?
foreign(nippon_silicon).
Product).
domestic(wafer_house).
domestic(hnd).

27
QUERY - Variables
   If one of the arguments is of no interest,
then you should use anonymous variable
because it will not result in a binding report
when the query succeeds.
   The anonymous variable is written as a
single underscore (_).
   makes(Supplier, _).

28
Exercise
   rasheed reads tolstoy, sultana reads national
newsweek, arsalan reads times and quran,
   Translate above facts and the following questions
in to PROLOG.
   What does sultana read?
   Who reads quran?

29
Query and Conjunctions
   A query consists of one or more goals
   A conjunction can be used to express that
a query is true if goal a and goal b are both
true.
   PROLOG uses the comma to separate the
goals in a conjunction.

domestic(Supplier), makes(Supplier, ram_chips).

30
Rules
   Express new or more complex relationships that
are based on simple relationships.
   A company is a domestic manufacturer of a
product if the company is domestic and the
company makes that product.
   domestic_manf(Company, Product) :-
domestic(Company),
makes(Company,Product).

31
Rules
   A predicate is defined by clauses, which may be
facts or rules.
   A rule is like a stored query. Its syntax is
   a predicate definition (just like a fact)
   :-
   the neck symbol, sometimes read as “if”
   body
   one or more goals (a query)

32
RULES
   All mothers are women
   woman(Person) :- mother(Person).
   All Scot people are British
   british(Person) :- scot(Person).
   Declaratively the head of a rule is true if the
conditions in the body are true.
   Operationally a goal that matches the head of the
rule succeeds WHEN the goal(s) in the body
succeed(s).

33
Exercise
   All animal eat custard.
   eat_custard(X) :- animal(X).
   Everyone loves Arne’s film.
   loves_arne_film(X) :- watch_arne_film(X).
   Saira likes Ahmed’s possessions.
   saira_likes(Thing) :- ahmed_poss(Thing).
   If someone needs a bike then they may borrow mine’s.
   borrow_mine(P) :- needs_bike(P).

34
Rules and Disjunction
   Someone is happy if they are healthy, wealthy or
wise.
   Note how we have had to rewrite the original
informal statement into something like:
   Someone is happy if they are healthy OR
Someone is happy if they are wealthy OR
Someone is happy if they are wise
   In PROLOG
   happy(Person):- healthy(Person).
   happy(Person):- wealthy(Person).
   happy(Person):- wise(Person).

35
Exercise
   You are British if you are Welsh, Scottish, or English.
   british(X) :- scottish(X).
british(X) :- welsh(X).
british(X) :- english(X).
   You are eligible for scholarship if you score A grade or your
father is a pensioner.
   scholarship(X) :- grade_A(X).
scholarship(X) :- father_pen(X).
   Those who play cricket, hockey, or football are players.
   player(X) :- plays_hockey(X).
player(X) :- plays_ cricket(X).
player(X) :- plays_ football(X).

36
Disjunction & Conjunction
   We combine both disjunctions and conjunctions
together.
   Consider: A woman is happy if she is healthy,
wealthy or wise
   happy(Person):- healthy(Person),woman(Person).
   happy(Person):-wealthy(Person),woman(Person).
   happy(Person):-wise(Person),woman(Person).

37
Search Strategy
   Now to look at a goal which requires
Prolog to solve two sub goals. Here is our
set of facts and one rule.
   woman(jane).
man(fred).
wealthy(fred).
happy(Person) :- wealthy(Person),
woman(Person).

38
Search Strategy
   We shall ask whether      woman(jane).
“jane is happy”.           man(fred).
   We get this terminal       wealthy(fred).
interaction:               happy(Person) :-
wealthy(Person),
?-   happy(jane).             woman(Person).
no
?-

39
Search Strategy
   Prolog has to match:
happy(jane) against happy(Person)
   We call this matching process unification.
   What happens here is that the logical variable
Person gets bound to the atom jane.
   You could paraphrase “bound” as “is temporarily identified
with”.

40
Search Strategy
   In this case the match produces a substitution,
Person=jane, and two sub goals replace the
current goal.
   The substitution of Person by jane is known as a
unifier and often written Person/jane
   The process of replacing a single goal by one or
more sub goals -- with whatever substitutions are
applicable-- is part of the resolution process.

41
Search Strategy
   To solve our problem, Prolog must set up
two sub goals. But we must make sure that,
since Person is a logical variable, that
everywhere in the rule that Person occurs
we will replace Person by jane.
   We now have something equivalent to:
   happy(jane):- wealthy(jane),woman(jane).

42
The Control Strategy

   PROLOG program does not contains HOW to
solve the problem. Nor it is present in the
query.
   It is governed by the control strategy
embedded within PROLOG
   Forward control strategy
   depth-first left to right
   Reverse control strategy
   chronological backtracking

43
The Control Strategy
a :- b, c, d.
a
b :- e, f.
c :- g, h.
c :- i, j.          b               c       d
d.
e :- k.
f :- l.         e       f       g       h
g.
i.
j.              k       l
k.
l.
44
The Control Strategy
big(bear).       %clause 1
big(elephant).   %clause 2
small(cat).      %clause 3

brown(bear).    %clause 4
black(cat).     %clause 5
gray(elephant). %clause 6
? – dark(X), big(X)
dark(Z):-        %clause 7
black(Z).
dark(Z):-        %clause 8
brown(Z).

45
RECURSION
   A relationship that is defined in terms of
itself.
   That is, the body of the rule contains the
same predicate name as the head.
   For example:
pred_a(…) :- pred_b(…), pred_a(…).

46
RECURSION
Fuji Foundation

Fuji Fertilizers   Fuji Soft         Fuji Textile

Fuji cotton mill       Fuji silks
owns(fuji_foundation, fuji_fertilizers).
owns(fuji_foundation, fuji_soft).                             Fuji Thai silk
owns(fuji_foundation, fuji_textile).
owns(fuji_textile, fuji_cotton).
owns(fuji_textile, fuji_silks).
owns(fuji_silks, fuji_thai).

47
RECURSION
   Two relationships:
   Direct
   Indirect
   The first argument of the owns predicate is the
holding company, and the second argument is the
subsidiary
   The owns predicate alone is limited to answering
quires about direct relationship.

48
RECUSION
   subsidiary(Holding, Unit) :-
owns(Holding, Unit).
   This represents: a unit is a subsidiary of a holding
if the holding owns that unit.
   The indirect relationship:
subsidiary(Holding, Unit) :-
owns(Holding, Inter_Unit),
subsidiary(Inter_Unit, Unit).

49
RECURSION
   Whenever you apply recursion, make sure that
the recursion will end under all circumstances.
   The termination of the recursion is achieved by
ensuring that the procedure not only contains
recursive rules, but also sufficient boundary
conditions.
   subsidiary(Holding, Unit) :-
owns(Holding, Unit).

50
RECURSION - Exercise
   Check your ancestors
   How do we define our ancestors?
   How do we represent this relationship in
PROLOG?

51
parent(pam,bob).
parent(tom,bob).
parent(tom,liz).
parent(bob,ann).
parent(bob,pat).
parent(pat,jim).

ancestor(X,Y):-
parent(X,Y).

ancestor(X,Z):-
parent(X,Y),
ancestor(Y,Z).
52

```
DOCUMENT INFO
Shared By:
Categories:
Stats:
 views: 43 posted: 8/23/2012 language: English pages: 52
Description: This Doc ppt is about Artificial Intelligense
How are you planning on using Docstoc?