0% found this document useful (0 votes)
72 views27 pages

Unit 3 Representing Knowledge: PROLOG Programming

This document discusses PROLOG syntax including terms, atoms, variables, compound terms, facts, rules, programs, and queries. It defines the basic components of PROLOG like terms, which can be atoms, numbers, variables, or compound terms. It explains atoms, variables, and compound terms in more detail. It also describes facts as predicates followed by a period, and rules as having a head and body separated by ":-". Finally, it discusses PROLOG programs as a sequence of clauses and queries submitted to the interpreter.

Uploaded by

Hyper Abi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views27 pages

Unit 3 Representing Knowledge: PROLOG Programming

This document discusses PROLOG syntax including terms, atoms, variables, compound terms, facts, rules, programs, and queries. It defines the basic components of PROLOG like terms, which can be atoms, numbers, variables, or compound terms. It explains atoms, variables, and compound terms in more detail. It also describes facts as predicates followed by a period, and rules as having a head and body separated by ":-". Finally, it discusses PROLOG programs as a sequence of clauses and queries submitted to the interpreter.

Uploaded by

Hyper Abi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Unit 3

Representing Knowledge

PROLOG Programming

Department of Computer Science and Engineering


Rajalakshmi Engineering College
PROLOG Syntax

Terms
• The central data structure in PROLOG is that of a term.
• There are terms of four kinds:
• atoms,
• numbers,
• variables, and
• compound terms.
• Atoms and numbers are sometimes grouped together and called
atomic terms.
Atomic Terms
• Atomic terms are usually strings made up of lower- and uppercase
letters, digits, and the underscore, starting with a lowercase letter.
• The following are all valid PROLOG atoms:
• elephant,
• b,
• abcXYZ,
• x_123,
• another_print_for_me_please
Variables
• Variables are strings of letters, digits, and the underscore, starting
with a capital letter or an underscore.
• Examples:
• X,
• Elephant,
• _4711,
• X_1_2,
• MyVariable,
• _
Compound terms
• Compound terms are made up of a PROLOG atom and a number of
arguments (PROLOG terms, i.e., atoms, numbers, variables, or other
compound terms) enclosed in parentheses and separated by commas.
• The following are some examples for compound terms:
• is_bigger(horse, X), f(g(X, _), 7),
• The sets of compound terms and atoms together form the set of
PROLOG predicates.
• A term that doesn’t contain any variables is called a ground term.
Facts
A fact is a predicate followed by a dot.
Examples:
• bigger_animal(whale).
• life_is_beautiful.
• The intuitive meaning of a fact is that we define a certain instance of
a relation as being true.
Rules
• A rule consists of a head (a predicate) and a body (a sequence of
predicates separated by commas).
• Head and body are separated by the sign :- and, like every PROLOG
expression, a rule has to be terminated by a dot.
• Examples:
• is_smaller(X, Y) :- is_bigger(Y, X).
• aunt(Aunt, Child) :- sister(Aunt, Parent),parent(Parent, Child).
• The intuitive meaning of a rule is that the goal expressed by its head
is true, if we (or rather the PROLOG system) can show that all of the
expressions (subgoals) in the rule’s body are true.
Programs and Queries
• Programs- A PROLOG program is a sequence of clauses.
• Queries -After compilation a PROLOG program is run by submitting queries
to the interpreter. A query has the same structure as the body of a rule, i.e.
it is a sequence of predicates separated by commas and terminated by a
dot. They can be entered at the PROLOG prompt, which in most
implementations looks something like this: ?-.
• When writing about queries we often include the ?-. Examples:
• ?- is_bigger(elephant, donkey).
• ?- small(X), green(X), slimy(X).
• Intuitively, when submitting a query like the last example, we ask PROLOG
whether all its predicates are provably true, or in other words whether
there is an X such that small(X), green(X), and slimy(X) are all true.
Install SWI-PROLOG

Steps to Compile :
1. Open Notepad .
2. Write your Facts and Rules and save the file with .pl extension.
3. To open the desired file
File → Edit → Go to the location → select file →Open
4. To check the syntax
In file editor
Compile → make
If no error it will compile
5. To fire query
In PROLOG editor
File → consult → select the file name
Write the query ,do not forget to put full stop( .)
If there is more than one answer to get use ( ; )
SWISH – online PROLOG compiler
• SWISH was originally written by Torbjörn Lager as a homage to SWI-
Prolog.
• SWISH is a great tool for teaching Prolog.
• SWISH is embedded to run examples and solve exercises from within
our browser.
• Peter Flach prepared his book Simply Logical for SWISH.
• The SWISH source is available from Github.
• It is under heavy development and often requires SWI-Prolog 7
installed from the latest GIT.
Question Answering: wh-questions
Exercises
KB 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.
Query 1: ?-woman(mia).
Query 2: ?-playsAirGuitar(mia).
Query 3: ?-party.
Query 4: ?-concert.
Exercises
KB 2
happy(yolanda).
listens2music(mia).
listens2music(yolanda) :- happy(yolanda).
playsAirGuitar(mia) :- listens2music(mia).
playsAirGuitar(Yolanda) :- listens2music(yolanda).
Query 1: ?-playsAirGuitar(mia).
Query 2: ?-playsAirGuitar(yolanda).
Exercises
KB 3
likes(dan,sally).
likes(sally,dan).
likes(john,brittney).
married(X,Y) :- likes(X,Y) , likes(Y,X).
friends(X,Y) :- likes(X,Y) ; likes(Y,X).
Query 1: ?-likes(dan,X).
Query 2: ?-married(dan,sally).
Query 3: ?-married(john,brittney).
Thank You

You might also like