0% found this document useful (0 votes)
108 views91 pages

Lecture-1 (Introduction To Prolog)

This document provides an introduction to Prolog. It discusses exercises and resources for learning Prolog including the Learn Prolog Now book and SWI Prolog interpreter. It describes the basic constructs in Prolog including facts, rules, and queries. It also discusses Prolog's history and differences from procedural programming languages.

Uploaded by

merajul islam
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)
108 views91 pages

Lecture-1 (Introduction To Prolog)

This document provides an introduction to Prolog. It discusses exercises and resources for learning Prolog including the Learn Prolog Now book and SWI Prolog interpreter. It describes the basic constructs in Prolog including facts, rules, and queries. It also discusses Prolog's history and differences from procedural programming languages.

Uploaded by

merajul islam
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/ 91

1

Prolog
(Programming In Logic)
2
Artificial Intelligence
Introduction to Prolog

 Exercises Teaching Material


 Learn Prolog Now!
https://sites.google.com/site/prologsite/home
 SWI Prolog interpreter http://www.swi-prolog.org/

 Book: Learn Prolog Now! by Patrick Blackburn, Johan


Bos and Kristina Striegnitz
 Code Editors: Github Atom with language-prolog
plugin / Notepad++ with Prolog (SWI) plugin. Or you
can simply use notepad/gedit to edit your code.

Jisan Mahmud 4/29/2016


3
SWI Prolog

 Sociaal-Wetenschappelijke Informatica ("Social Science


Informatics")
 SWI-Prolog is a free interpreter of the programming language
Prolog
 Works with
 Linux[Ubuntu, Debian, Fedora, openSUSE etc]
 Windows, or
 Mac OS
 There are many more Prolog interpreters

Jisan Mahmud 4/29/2016


4
Lecture 1

 Theory
 Introduction to Prolog
 Facts, Rules and Queries
 Prolog Syntax

Jisan Mahmud 4/29/2016


5
Aim of this lecture

 Give some simple examples of Prolog programs


 Discuss the three basic constructs in Prolog:
 Facts
 Rules
 Queries
 Introduce other concepts, such as
 the role of logic
 unification with the help of variables
 Begin the systematic study of Prolog by defining
terms, atoms, and variables

Jisan Mahmud 4/29/2016


6
Prolog

 "Programming with Logic"


 Declarative
 Very different from other (procedural) programming
languages
 Good for knowledge-rich tasks

Jisan Mahmud 4/29/2016


7
History of Prolog

first Prolog interpreter by


Colmerauer and Roussel

1972 1977 1980 1980s/1990s 2005


Jisan Mahmud 4/29/2016
8
History of Prolog

implementation of DEC10
compiler by Warren

1972 1977 1980 1980s/1990s 2005


Jisan Mahmud 4/29/2016
9
History of Prolog

Definite Clause Grammars


implementation by Pereira and
Warren

1972 1977 1980 1980s/1990s 2005


Jisan Mahmud 4/29/2016
10
History of Prolog

Prolog grows in popularity especially in


Europe and Japan

1972 1977 1980 1980s/1990s 2005


Jisan Mahmud 4/29/2016
11
History of Prolog

Prolog used to program natural


language interface in
International Space Station by
NASA

1972 1977 1980 1980s/1990s 2005


Jisan Mahmud 4/29/2016
12
Basic idea of Prolog

 Describe the situation of interest


 Ask a question
 Prolog logically deduces new facts about the
situation we described
 Prolog gives us its deductions back as answers

Jisan Mahmud 4/29/2016


13
Consequences

 Think declaratively, not procedurally


 Challenging
 Requires a different mindset
 High-level language
 Not as efficient as, say, C
 Good for rapid prototyping
 Useful in many AI applications

Jisan Mahmud 4/29/2016


Difference between a 14
procedural language and
prolog
Let’s assume that we know these rules-
1. If event A happens, then event B happens as well. Hence, happens(A) → happens(B)
2. If event B happens, then event C happens as well. Hence, happens(B) → happens(C)

Also assume that we know for a fact, that-


1. Event A happens. Hence, happens(A) = TRUE.

Can we deduce from these rules and the fact if event C happens?
1. happens(A) → happens(B),and happens(A) = TRUE. So, happens(B) = TRUE.
2. happens(B) → happens(C),and happens(B) = TRUE. So, happens(C) = TRUE.

Thus, we deduce that event C happens!


Jisan Mahmud 4/29/2016
Example of a logical deduction in 15
a procedural programming
language(such as C)
#include <stdio.h>

int main()
{
int A_happens, B_happens, C_happens;
int A_happens = 1;

if (A_happens == 1)
B_happens = 1;
else
B_happens = 0;

if (B_happens == 0)
C_happens = 1;
else
C_happens = 0;

printf("%d", C_happens);

return 0;
}

Jisan Mahmud 4/29/2016


16
Example of a logical
deduction in a Prolog

% We DECLARE the rules

happens(b) :- happens(a).
happens(c) :- happens(b).

% And we DECLARE the fact(s)

happens(a).

That’s all we need to do! If we query the Prolog ‘Knowledge base’


regarding happens(c), it will state that happens(c) = TRUE

Jisan Mahmud 4/29/2016


17
Chapter 1: Logic Programs

 A logic program is a set of axioms, or rules, defining


relations between objects.
 There are 3 basic constructs which define logic
programming.
 Get well familiar with this terminologies

1. Facts
2. Queries
3. Rules

Jisan Mahmud 4/29/2016


18
Facts

 The simplest kind of statement is called a fact.


 Relation holds between objects  Facts
 Example: father(abraham, isaac).
 This fact says that Abraham is the father of Isaac, or
that the relation father holds between the individuals
named abraham and isaac.

 Predicate: Another name for a relation is a predicate.


 Atoms: Names of individuals are known as atoms.
Example: abraham and isaac.
 Program: A finite set of facts and rules constitute a
program.

Jisan Mahmud 4/29/2016


19
Queries

 Queries are a means of retrieving information from a


logic program.
 Example: father(abraham, isaac)?
 Given the facts of previous slide, the answer to this
query is true/yes.

Jisan Mahmud 4/29/2016


20
Fact vs. Queries

 .
Fact terminates with a period ( ).

 Queries terminates with a question mark ( ?).


 We call the entity without the period or question
mark a goal. A fact P. states that the goal P is true. A
query P? asks whether the goal P is true.

Jisan Mahmud 4/29/2016


21
Atoms

 A sequence of characters of upper-case letters, lower-case


letters, digits, or underscore, starting with a lowercase letter.
• Examples: abraham, big_kahuna_burger, hamSandwich etc.

 An arbitrary sequence of characters enclosed in single quotes


• Examples: 'Vincent', 'Suarez’s bite', '@$%'
• Note: abraham and ‘abraham’ are the same thing in Prolog
 A sequence of special characters
• Examples: : , ; . :-

Jisan Mahmud 4/29/2016


22
Numbers

 0-9
 Integers: 12, -34, 22342
 Floats: 34573.3234

Jisan Mahmud 4/29/2016


23
Variables

 A sequence of characters of upper-case letters,


lower-case letters, digits, or underscore, starting with
either an uppercase letter or an underscore.

 Examples:

X, Y, Variable, Vincent, _tag etc.

Jisan Mahmud 4/29/2016


24
The Logical Variable,
Substitutions, and Instances

 Suppose we want to know of whom abraham is the


father.
 father(abraham, lot)?
 father(abraham, milcah)?
 ,...,
 father (abraham, isaac)? until an answer yes is
given.
 Better way to use variable logically 
 father (abraham, X)?
 X={Isaac}.

Jisan Mahmud 4/29/2016


25
Terms

 Variables. Example: X, Y, etc.


 Constants Example: abraham, isaac, etc.

Compound Terms: (functor)


o A functor is characterized by its name, which is an
atom, and its arity, or number of arguments.
o Example: f(t1, t2, ..., tn)
o Atom  f
o Arity  n
o ti are arguments.

Jisan Mahmud 4/29/2016


26
Ground Vs. Nonground

 queries, goals or terms without variable is ground.


 Example: foo(a, b) is ground.

 queries, goals or terms with variable is a non-ground.


 Example: bar(X) is non-ground.

Jisan Mahmud 4/29/2016


27
Substitution Vs. Instance

 father(abraham, isaac).
 father(abraham, X). Here substitution is {X=isaac}.

 father(abraham, isaac) is an instance of father(abraham,


X).
 Similarly, mother(sarah,isaac) is instance of mother(X,Y)
under the substitution {X=sarah, Y=isaac}.

Jisan Mahmud 4/29/2016


28
Prolog and Logic

 Clearly Prolog has something to do with logic


 Operators
 Implication :-
 Conjunction ,
 Disjunction ;
 Use of modus ponens
 Negation

Jisan Mahmud 4/29/2016


29
Rules

 Rules are statements of the form:


A :- B1,B2,...,Bn. where n >= 0.
 Note that a fact is just a special case of a rule when
n = 0. Facts are also called unit clauses.
 Example: A rule expressing the son relationship is
son(X,Y) :- father(Y,X), male(X).
 Similarly one can define a rule for the daughter
relationship:
daughter(X,Y) :- father(Y,X), female(X).

Jisan Mahmud 4/29/2016


30
Modus Ponens

 To incorporate rules into our framework of logical


deduction, we need the law of modus ponens.
Modus ponens states that from P, and Q :- P, we
can deduce Q.

Jisan Mahmud 4/29/2016


31
Knowledge Base

 A collection of facts and rules is called a knowledge


base (KB) (or a database) and Prolog programming
is all about writing knowledge bases.

Jisan Mahmud 4/29/2016


32
Knowledge Base 1

woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.

Jisan Mahmud 4/29/2016


33
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.

?- woman(mia).
true
?- playsAirGuitar(jody).

Jisan Mahmud 4/29/2016


34
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.

?- woman(mia).
true
?- playsAirGuitar(jody).
true
?-

Jisan Mahmud 4/29/2016


35
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.

?- woman(mia).
true
?- playsAirGuitar(jody).
true
?- playsAirGuitar(mia).
false
Jisan Mahmud 4/29/2016
36
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.

?- tattoed(jody).

Jisan Mahmud 4/29/2016


37
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.

?- tattoed(jody).
ERROR: predicate tattoed/1 not defined.
?-

Jisan Mahmud 4/29/2016


38
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.

?- party.

Jisan Mahmud 4/29/2016


39
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.

?- party.
yes
?-

Jisan Mahmud 4/29/2016


40
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.

?- rockConcert.

Jisan Mahmud 4/29/2016


41
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.

?- rockConcert.
no
?-

Jisan Mahmud 4/29/2016


42
Knowledge Base 2

happy(yolanda).
listens2music(mia).
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):-
listens2music(yolanda).

Jisan Mahmud 4/29/2016


43
Knowledge Base 2
fact
happy(yolanda).
listens2music(mia).
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).

Jisan Mahmud 4/29/2016


44
Knowledge Base 2
fact
happy(yolanda).
fact
listens2music(mia).
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):-
listens2music(yolanda).

Jisan Mahmud 4/29/2016


45
Knowledge Base 2
fact
happy(yolanda).
fact
listens2music(mia). rule
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).

Jisan Mahmud 4/29/2016


46
Knowledge Base 2
fact
happy(yolanda).
fact
listens2music(mia). rule
listens2music(yolanda):- happy(yolanda). rule
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).

Jisan Mahmud 4/29/2016


47
Knowledge Base 2
fact
happy(yolanda).
fact
listens2music(mia). rule
listens2music(yolanda):- happy(yolanda). rule
playsAirGuitar(mia):- listens2music(mia). rule
playsAirGuitar(yolanda):- listens2music(yolanda).

Jisan Mahmud 4/29/2016


48
Knowledge Base 2
happy(yolanda).
listens2music(mia).
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).

head body

Jisan Mahmud 4/29/2016


49
Knowledge Base 2
happy(yolanda).
listens2music(mia).
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).

?-

Jisan Mahmud 4/29/2016


50
Knowledge Base 2
happy(yolanda).
listens2music(mia).
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).

?- playsAirGuitar(mia).
true
?-

Jisan Mahmud 4/29/2016


51
Knowledge Base 2

happy(yolanda).
listens2music(mia).
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).

?- playsAirGuitar(mia).
true.
?- playsAirGuitar(yolanda).
true.
?-

Jisan Mahmud 4/29/2016


52
Knowledge Base 2

happy(yolanda).
listens2music(mia).
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).

?- playsAirGuitar(mia).
true.
?- playsAirGuitar(yolanda).
true.
?- playsAirGuitar(amanda).
false.
?-
Jisan Mahmud 4/29/2016
53
Clauses
happy(yolanda).
listens2music(mia).
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).

There are five clauses in this knowledge base:


two facts, and three rules.

The end of a clause is marked with a full stop.

Jisan Mahmud 4/29/2016


54
Predicates
happy(yolanda).
listens2music(mia).
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).

There are three predicates


in this knowledge base:
happy, listens2music, and playsAirGuitar

Jisan Mahmud 4/29/2016


55
Knowledge Base 3

happy(vincent).
listens2music(butch).
playsAirGuitar(vincent):- listens2music(vincent),
happy(vincent).
playsAirGuitar(butch):- happy(butch).
playsAirGuitar(butch):- listens2music(butch).

Jisan Mahmud 4/29/2016


56
Expressing Conjunction

happy(vincent).
listens2music(butch).
playsAirGuitar(vincent):- listens2music(vincent),
happy(vincent).
playsAirGuitar(butch):- happy(butch).
playsAirGuitar(butch):- listens2music(butch).

The comma “," expresses conjunction in Prolog

Jisan Mahmud 4/29/2016


57
Knowledge Base 3
happy(vincent).
listens2music(butch).
playsAirGuitar(vincent):- listens2music(vincent), happy(vincent).
playsAirGuitar(butch):- happy(butch).
playsAirGuitar(butch):- listens2music(butch).

?- playsAirGuitar(vincent).
false
?-

Jisan Mahmud 4/29/2016


58
Knowledge Base 3
happy(vincent).
listens2music(butch).
playsAirGuitar(vincent):- listens2music(vincent),
happy(vincent).
playsAirGuitar(butch):- happy(butch).
playsAirGuitar(butch):- listens2music(butch).

?- playsAirGuitar(butch).
true
?-

Jisan Mahmud 4/29/2016


59
Expressing Disjunction
happy(vincent).
listens2music(butch).
playsAirGuitar(vincent):- listens2music(vincent), happy(vincent).
playsAirGuitar(butch):- happy(butch).
playsAirGuitar(butch):- listens2music(butch).

happy(vincent).
listens2music(butch).
playsAirGuitar(vincent):- listens2music(vincent), happy(vincent).
playsAirGuitar(butch):- happy(butch); listens2music(butch).

Jisan Mahmud 4/29/2016


60
Knowledge Base 4

woman(mia).
woman(jody).
woman(yolanda).

loves(vincent, mia).
% Careful, this doesn’t necessarily mean that loves(mia, vincent) is a fact
loves(marsellus, mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).

Jisan Mahmud 4/29/2016


61
Prolog Variables
woman(mia).
woman(jody).
woman(yolanda).

loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).

?- woman(X).

Jisan Mahmud 4/29/2016


62
Variable Instantiation
woman(mia).
woman(jody).
woman(yolanda).

loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).

?- woman(X).
X=mia

Jisan Mahmud 4/29/2016


63
Asking Alternatives
woman(mia).
woman(jody).
woman(yolanda).

loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).

?- woman(X).
X=mia;

Jisan Mahmud 4/29/2016


64
Asking Alternatives
woman(mia).
woman(jody).
woman(yolanda).

loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).

?- woman(X).
X=mia;
X=jody

Jisan Mahmud 4/29/2016


65
Asking Alternatives
woman(mia).
woman(jody).
woman(yolanda).

loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).

?- woman(X).
X=mia;
X=jody;
X=yolanda

Jisan Mahmud 4/29/2016


66
Asking Alternatives
woman(mia).
woman(jody).
woman(yolanda).

loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).

?- woman(X).
X=mia;
X=jody;
X=yolanda;

Jisan Mahmud 4/29/2016


67
Knowledge Base 4
woman(mia).
woman(jody).
woman(yolanda).

loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).

?- loves(marsellus,X), woman(X).

Jisan Mahmud 4/29/2016


68
Knowledge Base 4
woman(mia).
woman(jody).
woman(yolanda).

loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).

?- loves(marsellus,X), woman(X).
X=mia
yes
?-

Jisan Mahmud 4/29/2016


69
Knowledge Base 4
woman(mia).
woman(jody).
woman(yolanda).

loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).

?- loves(pumpkin,X), woman(X).

Jisan Mahmud 4/29/2016


70
Knowledge Base 4
woman(mia).
woman(jody).
woman(yolanda).

loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).

?- loves(pumpkin,X), woman(X).
false
?-

Jisan Mahmud 4/29/2016


71
Knowledge Base 5

loves(vincent,mia).
loves(marsellus,mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).

jealous(X,Y):- loves(X,Z), loves(Y,Z).

Jisan Mahmud 4/29/2016


72
Knowledge Base 5
loves(vincent,mia).
loves(marsellus,mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).

jealous(X,Y):- loves(X,Z), loves(Y,Z).

?- jealous(marsellus,W).

Jisan Mahmud 4/29/2016


73
Knowledge Base 5
loves(vincent,mia).
loves(marsellus,mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).

jealous(X,Y):- loves(X,Z), loves(Y,Z).

?- jealous(marsellus,W).
W=vincent
?-

Jisan Mahmud 4/29/2016


What will be the output of the query

?- jealous(vincent, vincent).
What will be the output of the query

?- jealous(vincent, vincent).
true.
?

But, in real life, does this really make sense? We should have written the rule
in a way, so that the result of these types of queries(where we query the
‘jealous’ relation with the same names) would return false. We should add
another term to the rule using conjunction, which checks that the first and
second arguments are not equal.
76
Corrected Knowledge Base 5

loves(vincent,mia).
loves(marsellus,mia).
loves(jack, mia).
loves(jack, kate).
jealous(X,Y):- loves(X,Z), loves(Y,Z), \+(X == Y).
% Alternatively,
% jealous(X, Y) :- loves(X, Z), loves(Y, Z), X \== Y.

Jisan Mahmud 4/29/2016


77
Prolog Syntax

Terms

Simple Terms
Complex Terms

Constants Variables

Atoms Numbers

Jisan Mahmud 4/29/2016


78
Arity

 The number of arguments a complex term has is


called its arity

 Examples:

woman(mia) is a term with arity 1


loves(vincent,mia) has arity 2
father(father(butch)) arity 1

Jisan Mahmud 4/29/2016


79
Arity is important

 In Prolog you can define two predicates with the


same functor but with different arity
 Prolog would treat this as two different predicates
 In Prolog documentation arity of a predicate is
usually indicated with the suffix "/" followed by a
number to indicate the arity

Jisan Mahmud 4/29/2016


80
Example of Arity

happy(yolanda).
listens2music(mia).
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).
 This knowledge base defines
 happy/1
 listens2music/1
 playsAirGuitar/1

Jisan Mahmud 4/29/2016


81
Operators and Functions

 Normal Prolog operators are prefix:


 @>(Item1, Item2).
 @=<(Item1, Item2).
 ==(Item1, Item2).
 \==(Item1, Item2).
 Some symbols can be used infix: arithmetic and
comparison

Jisan Mahmud 4/29/2016


82
Comparison Operators
Operator Meaning
\+ Not (negation by failure)
=, \=, =.. Unification, not unifiable, list unification
==, \== Term identical, term not identical
@<, @=<, @>, @>= Term less than, term less than or equal to, etc.
is Unify left-hand side with result of evaluating right
hand side.
=:=, =\= Arithmetic equal, arithmetic not equal
<, =<, >, >= Arithmetic less than, etc.
:
+, -, /\, \/ Add, subtract, bitwise AND, bitwise OR
*, /, //, Mult, div, integer division
rem, mod Different versions of mod (see manual)
>>, << Shift right, shift left
**, ^. \ Exponentiation, bitwise XOR, bitwise NOT
Jisan Mahmud 4/29/2016
83
Comparison functions

Jisan Mahmud 4/29/2016


84
Arithmetic

 Example.

bonus(Number) :- Number is 2 + 3.
?- bonus(3).
No
?- bonus(5).
Yes
?- bonus(X).
X = 5

Jisan Mahmud 4/29/2016


85
Arithmetic

 Example.
= and is have
| ?- X is 5 + 2. different meanings
X = 7
yes
= means term assignment
| ?- X = 5 + 2.
is means arithmetic assignment
X = 5+2
yes
| ?- X is 5.3 + 7.
X = 12.300000000000001
yes
Integers are coerced
| ?-
to float

Jisan Mahmud 4/29/2016


86
Arithmetic
 Example: geography.pl
/*
north latitudes and west longitudes are positive.
sound latitudes and east longitudes are negative. Try:
*/
north_of(madrid, tokyo).
location(tokyo, 35, -139).
location(rome, 41, -12).
location(london, 51, 0).
west_of(X, tokyo).
location(canberra, -31, -149). west_of(london, X).
location(madrid, 48, 3).
north_of(X, Y) :-
location(X, Lat1, _),
location(Y, Lat2, _),
Lat1 > Lat2.
west_of(X, Y) :-
location(X, _, Long1),
location(Y, _, Long2),
Long1 > Long2.
Jisan Mahmud 4/29/2016
87
Tracing

Must have a period!


 The prolog command is trace

|?-trace.

yes

{trace}

|?-north_of(madrid, tokyo).

1 1 Call: north_of(madrid,tokyo) ?

1 1 means top level call


2 2 Call: location(madrid,_81,_41) ?

2 2 Exit: location(madrid,48,3) ?

3 3 Call: location(tokyo,_108,_68) ? 3 2 means 3rd level call in


3 2 Exit: location(tokyo,35,-139) ? response to the current 2nd
4 2 Call: 48>35 ?
level call.
4 2 Exit: 48>35 ?

1 1 Exit: north_of(madrid,tokyo) ?

Yes

{trace}

| ?-

For more: visit: http://www.cse.unsw.edu.au/~billw/dictionaries/prolog/tracing.html

Jisan Mahmud 4/29/2016


88
Tracing

The command to turn off is notrace

|?-notrace.
The debugger is switched off
Yes
|?-

Jisan Mahmud 4/29/2016


89
Making Decisions
 There are no if or case statements in prolog.
 How do we make decisions?
 Example: determine how hot it is:
// determine how hot it is
void howHot(string &HowHot, int Temp){
if (Temp >= 100)
HowHot = “very”;
else if (Temp >= 90)
HowHot = “pretty”;
else if (Temp >= 70)
HowHot = “perfect”;
else if (Temp < 70)
HowHot = “cold”;
}
Jisan Mahmud 4/29/2016
90
Making Decisions

 Making decisions (howHot.pl)


Can ask
% determine how hot it is
howHot(X, 80).
Hot(HowHot, Temp) :-
Temp >= 100,
But not
HowHot = ‘very’;
howHot(very, X).
Temp < 100,
Temp >= 90,
HowHot = ‘pretty’; Cannot use is must use =
Temp < 90, is only works on arithmetic
Temp >= 70, expressions
HowHot = ‘perfect’;
Temp < 70,
Must have this test because prolog
HowHot = ‘cold’.
will view each or clause
independently. If we left out Temp
Jisan Mahmud < 100 then a temp of 110 would 4/29/2016

return both “very” and “pretty”


91

Thanks

?
Jisan Mahmud 4/29/2016

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy