# Prolog

Topics: Prolog, Object-oriented programming, Quantification Pages: 6 (617 words) Published: April 2, 2013
LOGIC PROGRAMMING

What are the various programming paradigms

I’ve used so far?

2 CSED Computational Intelligence

February 26, 2013

  

Imperative
Procedural Declarative Functional LISt Processing C, Pascal

Logical
Object Oriented

PROgramming in LOGic
C++

3 CSED Computational Intelligence

February 26, 2013

PROLOG :: COMPONENTS

 

FACTS
RULES QUESTIONS/QUERIES

4 CSED Computational Intelligence

February 26, 2013

PROLOG :: COMPONENTS :: FACTS
attacks (tom, jerry)
relationship object_1 object_2

; round(earth)
relationship object

The names of relationship and objects start with lowercase letters. The relationship should be written first and the objects should be separated by commas.

5 CSED Computational Intelligence

February 26, 2013

PROLOG :: COMPONENTS :: FACTS
example_01 : Directed Graph
edge (a, b). edge (a, e). edge (b, d).

edge (b, c).
edge (c, a). edge (e, b).
6 CSED Computational Intelligence

February 26, 2013

PROLOG :: COMPONENTS :: RULES

Rule is a general way to say when a predicate is true.
With rules we can state that a predicate is true,

provided that other predicates are true. example: a :- b, c, d. {a is true if b, c, d are true} a :- b, c, d. b. c. d :- e e. example_01 : Directed Graph :: Rule for “Path of length two” tedge(Node1, Node2) :- edge (Node1, X), edge (X, Node2).

7 CSED Computational Intelligence

February 26, 2013

PROLOG :: COMPONENTS :: QUESTIONS

Look for FACTS from database
?- attacks (tom, jerry). ?- edge (a, b).

True or False

8 CSED Computational Intelligence

February 26, 2013

SWI PROLOG :: INSTALL & COMPILE

2) Create a new file with an extension .pl :
3) Write a sample Prolog program and save the file as test01.pl 4) To compile the program type the following in the prompt: ?- [test01]. (don’t forget to use the period symbol at the end)

9 CSED Computational Intelligence

February 26, 2013

SWI PROLOG :: EXERCISE #01
At the SWI Prolog Prompt try the following:
?- edge(a, b). press enter

?- edge(a, d).
?- edge(a, b), edge(a, e). ?- edge(a, b), edge(a, d).

press enter
press enter press enter

?- edge(a, X), edge(X, d).
?- edge(a, X). ?- edge(X, Y).

press enter
press enter then press ; press enter then press ;

10 CSED Computational Intelligence

February 26, 2013

SWI PROLOG :: EXERCISE #02
Write a Prolog program which describes a directed graph with the following structure. Also include in the program a predicate (tedge) which defines the “path of length 3” relation.

11 CSED Computational Intelligence

February 26, 2013

Facts & Queries are entered in the same way

Prompt? Facts are entered into the interpreter using the

built-in predicate assert ?- assert(round(earth))
12 CSED Computational Intelligence

February 26, 2013

DEFINE :: VARIABLE
 Names of objects that has to be found by Prolog

The name of a variable begins with a capital letter, and may

contain other letters, digits, or underscores
edge(a, X). attacks(X, jerry). edge(a, b). edge(a, e). Instantiation Instantiation

In Prolog a variable is assigned a value in runtime unlike other programming languages (compile-time). 13 CSED Computational Intelligence

February 26, 2013

What is
pure Instantiation?

14 CSED Computational Intelligence

February 26, 2013

DEFINE :: QUANTIFIER

   

X (man(X)  mortal(X))
mortal(X) :- man(X).

Predicate Logic Representation
Prolog Representation

Quantifiers are not written in Prolog, but they are implied In a rule or a fact, all variables are universally quantified In a query, all variables are existentially quantified

15 CSED Computational Intelligence

February 26, 2013

PROLOG :: I/O

I/O is procedural in nature, and does not...