PROLOG What is PROLOG Prolog is a Declarative

  • Slides: 22
Download presentation
PROLOG

PROLOG

What is PROLOG? • Prolog is a Declarative or logical Language. • Prolog takes

What is PROLOG? • Prolog is a Declarative or logical Language. • Prolog takes only facts and rules to arrive at goals. • The programmer doesn’t tell it how to solve. • For solving logic and decision problems, Prolog is ideal. • Typical applications: AI, Database apps, proving theorems, symbolic evaluation (i. e. differentiation).

How does Prolog work ? • Prolog is based on ‘Horn Clauses’ • Horn

How does Prolog work ? • Prolog is based on ‘Horn Clauses’ • Horn Clauses are a subset of ‘Predicate Logic’ • Predicate logic is a way of simply defining how reasoning gets done in logic terms. • Predicate Logic is a syntax for easily reading and writing Logical ideas.

Predicate Logic • To transform an English sentence to Predicate Logic, we remove unnecessary

Predicate Logic • To transform an English sentence to Predicate Logic, we remove unnecessary terms. • This leaves only the relationship and the entities involved, known as arguments. • Ex: A pie is good = good(pie) • The relation is ‘good’, the relation’s argument is ‘pie’. • In Prolog, we call the relation’s name (e. g. “good”) the ‘Functor’. A relation may include many arguments after the functor.

Components of Prolog • Facts and rules are the two important components in prolog.

Components of Prolog • Facts and rules are the two important components in prolog.

Facts • Facts have simple syntax and it always begin with lowercase letter. •

Facts • Facts have simple syntax and it always begin with lowercase letter. • Eg : • smaller(ant, grasshopper). • smaller(ant, ladybird). • smaller(ladybird, grasshopper). • These facts are also known as clauses.

 • After compilation of the facts if we query the prolog system :

• After compilation of the facts if we query the prolog system : • ? - smaller(ant, ladybird). • Yes. • ? - smaller(grasshopper, ladybird). • No.

Rules • Any conditional statement about the world are stated rules in prolog. •

Rules • Any conditional statement about the world are stated rules in prolog. • For representing rules : - symbol is used. • Syntax of rule : • Head : - body pattern • Head is true if body is true.

 • • fly(x) : - bird(x) bird(dove). ? -fly(A) A = dove

• • fly(x) : - bird(x) bird(dove). ? -fly(A) A = dove

Prolog syntax • 1. Atoms : are sting of lowercase and uppercase letter, digits,

Prolog syntax • 1. Atoms : are sting of lowercase and uppercase letter, digits, underscore. • 2. Numbers : integers, floats • 3. Variables : are string of lowercase and uppercase letter, digits, underscore. • 4. Compound terms : consists of prolog atoms ‘functor’. Is_bigger(tiger, X)

Built-in Predicates • • ? - X=tiger, write(X), n 1. Tiger. X=Tiger. Yes.

Built-in Predicates • • ? - X=tiger, write(X), n 1. Tiger. X=Tiger. Yes.

Matching • • • ? - is_bigger(X, cat) = is_bigger(tiger, cat). X=tiger Yes ?

Matching • • • ? - is_bigger(X, cat) = is_bigger(tiger, cat). X=tiger Yes ? - p(_, 2, 2) = p(1, Y, _) Y=2 Yes

List Manipulation • • List uses collection of terms. List is denoted by square

List Manipulation • • List uses collection of terms. List is denoted by square brackets []. [] means an empty list. The first element of the list is called head and remaining is called tail.

 • • ? - [1, 2, 3, 4, 5] = [Head | Tail].

• • ? - [1, 2, 3, 4, 5] = [Head | Tail]. Head = 1 Tail = [2, 3, 4, 5] Yes ? - [q, l, j, j, l, b] = [__, X | __] X=l Yes

 • ? - concat_lists([1, 2, 3], [a, b, c, d], X) • X

• ? - concat_lists([1, 2, 3], [a, b, c, d], X) • X = [1, 2, 3, a, b, c, d] • Yes

Built-in predicates in list • • ? – length(List, 3) List = [A 1,

Built-in predicates in list • • ? – length(List, 3) List = [A 1, A 2, A 3] Yes ? – member(cat, [horse, donkey, cat, monkey]) Yes ? - analyse_list([]) This is an empty list. Yes

 • • ? - analyse_list([dog, cat, horse, cow]) This is the head of

• • ? - analyse_list([dog, cat, horse, cow]) This is the head of your list : dog This is the tail of your list : [cat, horse, cow] Yes ? - remove_duplicates([a, b, a, c, d, d], List). List = [b, a, c, d] Yes

 • • • ? - reverse_list([lion, elephant, monkey], List) List = [monkey, elephant,

• • • ? - reverse_list([lion, elephant, monkey], List) List = [monkey, elephant, lion] Yes ? - replace([1, 2, 3, 4, 3, 5], 3, x, List). List = [1, 2, x, 4, x, 5] Yes

Arithmetic operations in prolog • • • ? - X is 3 + 5

Arithmetic operations in prolog • • • ? - X is 3 + 5 , X = 8 X=8 Yes ? - 8 is 3 + 5 Yes

Functions in prolog • • Max Min Sqrt abs

Functions in prolog • • Max Min Sqrt abs

relations • • >= <= == (not equal) =: = (equal)

relations • • >= <= == (not equal) =: = (equal)

structure • • • Stores in form of record. bookdetails(title, AI, publisher, spd). Query

structure • • • Stores in form of record. bookdetails(title, AI, publisher, spd). Query : ? - bookdetails(title, _, publisher, _) AI spd Yes