Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
57 views

Complex Knowledgebase in Prolog

This document provides an introduction to Prolog, including: - Prolog is a declarative programming language used in artificial intelligence. It specifies rules and facts to derive solutions to problems rather than specifying algorithms. - A Prolog program consists of clauses that are either facts or rules. Users can submit queries to the Prolog interpreter, which will provide answers based on the facts and rules. - The document discusses first-order logic, examples of converting English statements to FOL, Prolog syntax including atoms, variables, terms, and symbols, and the structure of a Prolog knowledge base. It provides instructions on installing SWI-Prolog and an example of writing predicates and rules to check connectivity between cities.

Uploaded by

Muhammad Sufyan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Complex Knowledgebase in Prolog

This document provides an introduction to Prolog, including: - Prolog is a declarative programming language used in artificial intelligence. It specifies rules and facts to derive solutions to problems rather than specifying algorithms. - A Prolog program consists of clauses that are either facts or rules. Users can submit queries to the Prolog interpreter, which will provide answers based on the facts and rules. - The document discusses first-order logic, examples of converting English statements to FOL, Prolog syntax including atoms, variables, terms, and symbols, and the structure of a Prolog knowledge base. It provides instructions on installing SWI-Prolog and an example of writing predicates and rules to check connectivity between cities.

Uploaded by

Muhammad Sufyan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Complex Knowledgebase in

Prolog

Lab09

Instructor : Saqib Daud


Introduction to Prolog

• Prolog stands for Programming in logic. It is used in artificial


intelligence programming.
• Prolog is a declarative programming language.
• For example: While implementing the solution for a given problem,
instead of specifying the ways to achieve a certain goal in a specific
situation, user needs to specify about the situation (rules and facts) and
the goal (query).
• After these stages, Prolog interpreter derives the solution.
• Prolog is useful in AI, NLP, and databases but useless in other areas
such as graphics or numerical algorithms.
Introduction to Prolog

• Lisp and Prolog are the most frequently used symbolic programming
languages for artificial intelligence.

• They are widely regarded as excellent languages for "exploratory" and


"prototype programming".
Concept Map

A Prolog program consists of a number of clauses. Each clause is either


a fact or a rule.

After a Prolog program is loaded (or consulted) in a Prolog interpreter,


users can submit goals or queries, and the Prolog interpreter will give
results (answers) according to the facts and rules.
First-order logic
• First-order logic is symbolized reasoning in which each sentence, or
statement, is broken down into a subject and a predicate.
• The predicate modifies or defines the properties of the subject.
• In first-order logic, a predicate can only refer to a single subject.
• First-order logic is also known as first-order predicate calculus or first-
order functional calculus.
• Some of the most important operators used in FOL are ¬(NOT), ∧
(AND), ∨ (OR), ⇒ (IMPLIES), ⇔ (EQUIV).
Examples
English Statement FOL
Gorilla is black Gorilla(x) Black(x)

All Students are smart ∀ x ( Student(x) Smart(x) )

∃ x Student(x).
There exists a student.

There exists a smart student. ∃ x ( Student(x) ∧ Smart(x) )

Every student loves some student. ∀ x ( Student(x) ∃ y ( Student(y) ∧


Loves(x,y) ))

Every student loves some other student. y) ∧ Loves(x,y) ))


∀ x ( Student(x) ∃ y ( Student(y) ∧¬ (x =
• In first-order logic, a sentence can be structured using the universal quantifier
(symbolized ∀ ) or the existential quantifier (Ǝ ).
• Consider a subject that is a variable represented by x.
• Let A be a predicate "is an apple,"
• F be a predicate "is a fruit,“
• S be a predicate "is sour"',
• and M be a predicate "is mushy.“
Then we can say
• x : A(x)  F(x)
• Ǝ x : F(x) A(x)
• Ǝ x : A(x) S(x)
• Ǝ x : A(x) M(x)
Terminologies
• Fact
• A fact is something that seems to be true.
• For example:
• It's raining. In Prolog, facts are used to form the statements. Facts
consist of a specific item or relation between two or more items.
Rules
• A rule can be viewed as an extension of a fact with added conditions
that also have to be satisfied for it to be true.
• It consists of two parts. The first part is similar to a fact (a predicate
with arguments).
• The second part consists of other clauses (facts or rules which are
separated by commas) which must all be true for the rule itself to be
true. For example: Jaya likes food if it is delicious.
How to convert Fact and Rules to
Prolog?
• It is very simple to convert English sentence into Prolog facts. Some
• examples are explained in the following table.

English Statement Prolog


Dog is barking. barking(dog)
Jaya likes food if it is delicious. likes(jaya, Food):-delicious(Food)
• In the above table, the statement 'Dog is barking' is a fact, while the
statement 'Jaya likes food if it is delicious' is called rule.
• In this statement, variable like 'Food' has a first letter in capital,
because its value came from previous fact.
• The symbol ':-' is used to denote that “Jaya likes delicious food”.
Prolog Syntax
• Atoms
An atom is either:
1. A string of characters made up of upper-case letters, lower-case letters, digits, and the
underscore character, that begins with a lower-case letter. Here are some examples:
butch ,big_kahuna_burger , listens2Music and playsAirGuitar.
2. An arbitrary sequence of characters enclosed in single quotes. For example ’ Vincent ’, ’ The
Gimp ’, ’ Five_Dollar_Shake ’, ’ &^%&#@$ &* ’, and ’ ’. The sequence of characters between the
single quotes is called the atom name. Note that we are allowed to use spaces in such atoms; in
fact, a common reason for using single quotes is so we can do precisely that.

3. A string of special characters. Here are some examples: @= and ====> and ;
and :- are all atoms. As we have seen, some of these atoms, such as ; and :- have a
pre-defined meaning.
Prolog Syntax
• Numbers
Real numbers aren’t particularly important in typical Prolog
applications. So although most Prolog implementations do support
floating point numbers or floats (that is, representations of real
numbers such as 3.16573087 or π ) we say little about them in this
book. But integers (that is: …,-2, -1, 0, 1, 2, 3…) are useful for such tasks
as counting the elements of a list. Their Prolog syntax is the obvious
one: 23, 1001, 0, -365, and so on.
Prolog Syntax
• Variables
A variable is a string of upper-case letters, lower-case letters, digits and
underscore characters that starts either with an upper-case letter or
with an underscore. For example, X , Y ,Variable , _tag , X_526 , List ,
List24 , _head , Tail , _input and O utput are all Prolog variables.
Prolog Syntax
• Complex terms
Constants, numbers, and variables are the building blocks: now we need to
know how to fit them together to make complex terms. Complex terms are
build out of a functor followed by a sequence of arguments. The arguments are
put in ordinary parentheses, separated by commas, and placed after the
functor. Note that the functor has to be directly followed by the parenthesis;
you can’t have a space between the functor and the parenthesis enclosing the
arguments. The functor must be an atom. That is, variables cannot be used as
functors. On the other hand, arguments can be any kind of term. For example,
playsAirGuitar(jody) is a complex term: its functor isplaysAirGuitar and its
argument is jody . Other examples are loves(vincent,mia) and, to give an
example containing a variable, jealous(marsellus,W) .
Prolog Syntax
• But the definition allows for more complex terms than this. In fact, it allows
us to keep nesting complex terms inside complex terms indefinitely (that is,
it is allows recursive structure). For example
hide(X,father(father(father(butch)))) is a perfectly acceptable complex term.
Its functor is hide , and it has two arguments: the variable X , and the
complex term father(father(father(butch))) . This complex term has father as
its functor, and another complex term, namely father(father(butch)) , as its
sole argument. And the argument of this complex term, namely
father(butch) , is also complex. But then the nesting bottoms out, for the
argument here is the constant butch . The number of arguments that a
complex term has is called its arity. For example, woman(mia) is a complex
term of arity 1, and loves(vincent,mia) is a complex term of arity 2.
Prolog Syntax
• Symbols
Knowledge Base
• There are only three basic constructs in Prolog: facts, rules, and
queries. A collection of facts and rules is called a knowledge base (or a
database) and Prolog programming is all about writing knowledge
bases. That is, Prolog programs simply are knowledge bases,
collections of facts and rules which describe some collection of
relationships that we find interesting. So how do we use a Prolog
program? By posing queries. That is, by asking questions about the
information stored in the knowledge base.
Lab Activity 1
• Convert the following English sentences to FOL(First Order Logic)
bought(Frank, dvd)
Frank bought a dvd.
Pass(x, c)
student x passes course c.
 the score obtained by student x in course c Score(x, c)
 Some students took French in spring 2001.. ∃x Student(x) ∧
Takes(x, F,
Spring2001).
• Which of the following sequences of characters are atoms, which are variables, and
which are neither?
1. vINCENT
2. Footprint
3. variable23
4. Variable2000
5. Zinger_burger
6. 'Zinger burger'
7. Zinger burger
8. 'Farhan'
9. _Farhan
Installation of Procedure & Tools
• Tools
• SWI-Prolog
• Compiler.
Download SWI prolog
• Download the SWI Prolog from the link give below.
• https://www.swi-prolog.org/download/stable
For more visit
• https://www.cpp.edu/~jrfisher/www/prolog_tutorial/pt_framer.html
Task
• Write the following predicates and rules and consult in SWI Prolog
and execute goals to check the results.
next_to(peshaware, islamabad).
next_to(peshaware,quetta).
next_to(islamabad, peshaware).
next_to(islamabd,quetta).
next_to(islamabd,lahore).
travel(A,C):- next_to(A,B), next_to(B,C).

You might also like