Complex Knowledgebase in Prolog
Complex Knowledgebase in Prolog
Prolog
Lab09
• Lisp and Prolog are the most frequently used symbolic programming
languages for artificial intelligence.
∃ x Student(x).
There exists a student.
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).