0% found this document useful (0 votes)
9 views

4.3 Knowledge Representation - Logic Programming

Uploaded by

Kashish Ahuja
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

4.3 Knowledge Representation - Logic Programming

Uploaded by

Kashish Ahuja
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

4.

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).

Basic Syntax of PROLOG


• Aprogram, or database, in Prolog consists of one or more predicates; each predicate consists
of one or more clauses.
• Aclause is a base clause if it is unconditionally true, that is, it has no "if part.''
<program> ::= <predicate> | <program><predicate>
<predicate> ::= <clause> | <predicate><clause>
<clause> ::= <base clause> | <nonbase clause>
• Two clauses belong to the same predicate if they have the same functor (name) and the same
arity (number of arguments). Thus, mother(jane) and mother(jane, jim) are different
predicates.
Base Clause: <functor>(<arguments>).
<base clause> ::= <structure> . Non-base Clause: <functor>(<arguments>) :- <structures>.
Structures: <structure> or <structure>, <structures>
Predicate: <functor>/<arity> (e.g., mother/1, parent/2)
<nonbase clause> ::= <structure> :- <structures> .
<structures> ::= <structure> | <structure> , <structures>
Basic Syntax of PROLOG
• Astructure is a function followed by zero or more arguments; the arguments are enclosed
in parentheses and separated by commas. There must be no space between the function
and the opening parenthesis! If there are no arguments, the parentheses are omitted.
<structure> ::= <name> | <name> ( <arguments> )
<arguments> ::= <argument> | <argument> , <arguments>
• Arguments may be any legal Prolog values or variables.
• Avariable is written as a sequence of letters and digits, beginning with a capital letter.
The underscore (_) is considered to be a capital letter.
• An atom is any sequence of letters and digits, beginning with a lowercase letter.
Alternatively, an atom is any sequence of characters, enclosed by single quotes ('); an
internal single quote must be doubled. Examples: cat, r124c41, max_value, maxValue,
'max value', 'Don't go'.
Basic Syntax of PROLOG
• Prolog allows certain infix operators: ',' (comma), ';' (semicolon), ':-'
(turnstile), +, -, *,
• /, =, ==, and many others. These are the same as the operator written
as the functor of a structure; for example, 2+2 is the same as '+'(2,2).
• Comments begin with the characters /* and end with */. Comments
are not restricted
• to a si
• Example
food(burger). // burger is a food
food(sandwich). // sandwich is a food
food(pizza). // pizza is a food
lunch(sandwich). // sandwich is a lunch
dinner(pizza). // pizza is a dinner
Example: PROLOG program
Rules
meal(X) :- food(X). // Every food is a meal OR
Anything is a meal if it is a food

Queries / Goals

?- food(pizza). // Is pizza a food? Answer: True.


?- meal(X), lunch(X). // Which food is meal and lunch? Here in this query we have provided two sub-goals
Answer: X = sandwich where "," comma means 'and'. Prolog always tries to
satisfy sub-goals in left-to-right manner, so first try to
get left most goal i.e. meal(X). Meal(X) rule says - X is
a meal if X is a food. Here X is
a variable and it can bound with any related value.
So, in food(X) - X can be burger, sandwich or pizza. But
second goal says that X should also be lunch. Now we
look for X value in lunch(X) and i.e. sandwich.
?- dinner(sandwich). // Is sandwich a dinner? In this case prolog will find the
Answer: False 'dinner' predicate and will match the argument
inside the bracket. Will return “False” since it can’t find
the match.

You might also like