Introduction to PROLOG Prolog A logic programming language

  • Slides: 15
Download presentation
Introduction to PROLOG

Introduction to PROLOG

Prolog • A logic programming language created in 1972 • PROgramming in LOGic •

Prolog • A logic programming language created in 1972 • PROgramming in LOGic • Restricted to Horn clauses – Head: - body • Inference – Backward chaining

Knowledge Base-facts • Knowledgebase can have facts: – woman(ruba). – playsguitar(jane). –. . .

Knowledge Base-facts • Knowledgebase can have facts: – woman(ruba). – playsguitar(jane). –. . .

Knowledge Base-facts • Knowledgebase can have facts: – woman(ruba). – playsguitar(jane). • Consulting the

Knowledge Base-facts • Knowledgebase can have facts: – woman(ruba). – playsguitar(jane). • Consulting the KB is done in the Interpreter window: – Prolog listens to your queries and answers: • ? - woman(ruba). //asking if mia is a woman • true

Consulting • Consulting the KB: – Prolog listens to your queries and answers: •

Consulting • Consulting the KB: – Prolog listens to your queries and answers: • ? - woman(ruba) • true • ? - woman(jane) • false – doesn’t follow from KB • ? - woman(salma) • false – doesn’t know anything about alisa

Knowledge. Base - rules male(yasin). female(ruba). male(yusuf). mortal(X) : - person(X) : - female(X).

Knowledge. Base - rules male(yasin). female(ruba). male(yusuf). mortal(X) : - person(X) : - female(X). person(X) : - male(X). head : = body means body => head e. g. person (X) => mortal (X)

Knowledge. Base - rules male(yasin). female(ruba). male(yusuf). mortal(X) : - person(X) : - female(X).

Knowledge. Base - rules male(yasin). female(ruba). male(yusuf). mortal(X) : - person(X) : - female(X). person(X) : - male(X). – If you save these in a file: mortal. pl (a prolog program), you can TELL these to the interpreter via (or when you click on file name): • ? - consult(mortal). • Yes – If you type “listing”, Prolog will list all the facts and rules you just “read in” (consulted). • ? - listing. • male(yasin) • . . .

Knowledge. Base - rules • • • male(yasin). female(ruba). male(yusuf). mortal(X) : - person(X)

Knowledge. Base - rules • • • male(yasin). female(ruba). male(yusuf). mortal(X) : - person(X) : - female(X). person(X) : - male(X). –. . . – Now we can test the program inside the Listener with prolog queries: • ? - mortal(ziggy). • false • ? - mortal(yasin). • true

Rules - Logical AND • dances(vincent) : - happy(vincent) , listens. To. Music(vincent). •

Rules - Logical AND • dances(vincent) : - happy(vincent) , listens. To. Music(vincent). • , is used to indicate Logical AND • Equivalent to: – happy(vincent) listens. To. Music(vincent) => dances(vincent) – “Vincent dances if he listens to music and he is happy”. • Other example: – father(X, Y) : - parent(X, Y) , male(X).

Rules - Logical OR • dances(john) : - happy(john). • dances(john) : - listens.

Rules - Logical OR • dances(john) : - happy(john). • dances(john) : - listens. To. Music(john). – Indicates LOGICAL OR – Equivalent to: • happy(john) listens. To. Music(john) => dances(john) • “John dances either if he listens to music, or if he is happy. ” • This can also be stated as: – dances(john) : - happy(john) ; listens. To. Music(john). – where ; indicates OR.

Consulting • • woman(ruba). woman(jody). man(yusuf). woman(heba). • ? - woman(X). • X =

Consulting • • woman(ruba). woman(jody). man(yusuf). woman(heba). • ? - woman(X). • X = mia • ? - ; • • • X = jody ? - ; X = heba ? - ; no (remember that ; means OR so this query means: “are there any more women? ”) ( No other match is possible)

Wildcard • In Prolog predicates, underscore (_) is the wildcard (matches anything): – Mother(M,

Wildcard • In Prolog predicates, underscore (_) is the wildcard (matches anything): – Mother(M, C) : - Person(C, _, M, _, _). where Person is defined as Person(name, gender, mother, father, spouse). It means, Mother(M, C) holds, if the predicate Person holds for C and M in the right positions, with anything else for the other parts.

Arithmetic • In particular, there is support for arithmetic and numbers: • Basics of

Arithmetic • In particular, there is support for arithmetic and numbers: • Basics of numbers: – – ? - 8 is 6+2. Yes – – ? - X is mod(7, 2). X=1 – – ? - 2 < 4. Yes

Arithmetic • • positive(N) : - N>0. non_zero(N) : - N<0 ; N>0. •

Arithmetic • • positive(N) : - N>0. non_zero(N) : - N<0 ; N>0. • • ? - X is sqrt(9), Y is 2 ** 4, Z is floor(3. 14). X = 3. 0 Y = 16. 0 Z = 3 • • minimum(X, Y, X) : - X<Y. minimum(X, Y, Y) : - X>=Y. ? -minimum(3, 8, A). A = 3 (unification) It’s a bit like: void minimum(int x, int y, int & z) { if (x < y) z = x; else z = y; }

There are more details about Prolog but we will leave it at that Good

There are more details about Prolog but we will leave it at that Good Luck