4.3 Knowledge Representation - Logic Programming
4.3 Knowledge Representation - Logic Programming
3 Knowledge
Representation:Logic
Programming
Upasana Talukdar
CSE
Difference between Forward chaining
and Backward chaining
Forward chaining Backward chaining
It uses a down-up (bottom-up) approach It uses a up-down (top-bottom) approach
The approach data driven. The approach is goal driven. The endpoint (goal) is
subdivided into sub-goals to prove the truth of facts.
More flexible, as no limitation on the number of facts Less flexible, as number of facts to be proved is pre-
to be proved. defined.
Multiple conclusions can be drawn. Not possible to infer multiple conclusions as the goal is
pre-defined.
Can be time consuming Less time consuming, only pre-defined facts has to be
proved
Inferring the goal is not always efficient. It is easier to infer the conclusions as the goal is
already known.
Example: DENDRAL expert system (used to predict Example: MYCIN expert system ( predicts the suitable
the molecular sub-structure of the substances) treatments for patients based on patients’information)
Logic Programming
• Logic Programming is a programming language paradigm in which logical
assertions are viewed as programs.
• PROLOG is the most popular logical programming system.
• PROLOG: PROgraming in LOGic
• PROLOG is defined as a series of logical assertions, each of which is a Horn
clause, with the following conditions: [Horn Clause:Ahorn clause will have at
most one positive literal.]
• If the Horn clause contains no negative literals, retain it as it is.
• Otherwise, rewrite the Horn clause using implication, combining all the negative literals
as antecedent of the implication, and the positive literal as the consequent.
• Example: P(x): Q(x, y) ≡ ∀𝑥: ∃𝑦: 𝑄(𝑥, 𝑦) → 𝑃(𝑥)
PROLOG
• Consequences of Horn Clause in PROLOG:
• Due to use of only Horn clauses, it is possible to design simple and
efficient interpreter.
• The logic of the Horn Clause is decidable.
• PROLOG employs backward reasoning to prove the goal (final inference)
given the assertions/ facts in the program.
• The program is read top-to-bottom, and left-to-right, employing DFS with
backtracking.
• PROLOG interpreter has a fixed control strategy. The facts in the program
follows a certain path to answer any query.
Basic Syntax of PROLOG
• The following is a simple Prolog program:
man(socrates).
• mortal(X) :- man(X).
The first line can be read, "Socrates is a man.'' It is a base clause, which represents a simple fact.
The second line can be read, "X is mortal if X is a man;'' in other words, "All men are mortal.''
This is a clause, or rule, for determining when its input X is "mortal.'' (The symbol ":-'‘ is
pronounced "if''.)
We can test the program by asking the question:
| ?- mortal(socrates), implies "Is Socrates mortal?'' (The "| ?-'' is the computer's prompt for
a question.) Prolog will respond "yes''.
Another question we may ask is:
| ?- mortal(X).
• That is, "Who (X) is mortal?'' Prolog will respond "X = socrates''.
Predicate defines a logical relationship or rule using a name and arity.
Clause contributes to the definition of a predicate and can be a base clause (fact) or a non-base clause (rule or condition).
Queries / Goals