prolog prolog: programming in logic prolog programs consist of clauses and rules. declarative...

14
Prolog • Prolog: Programming in Logic • Prolog programs consist of clauses and rules. • Declarative programming – Emphasis is on data and relations between objects. – No familiar procedural controls. – Built-in inference engine.

Post on 21-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Prolog Prolog: Programming in Logic Prolog programs consist of clauses and rules. Declarative programming –Emphasis is on data and relations between objects

Prolog

• Prolog: Programming in Logic• Prolog programs consist of clauses and

rules.• Declarative programming

– Emphasis is on data and relations between objects.

– No familiar procedural controls.– Built-in inference engine.

Page 2: Prolog Prolog: Programming in Logic Prolog programs consist of clauses and rules. Declarative programming –Emphasis is on data and relations between objects

Where is declarative programming useful?

• Data-driven programming tasks– Verification– Natural Language– Expert Systems– Relational Databases– Diagnostic Systems

Page 3: Prolog Prolog: Programming in Logic Prolog programs consist of clauses and rules. Declarative programming –Emphasis is on data and relations between objects

Logical Operators

A B A^B

0 0 0

0 1 0

1 0 0

1 1 1

AND: Conjunction

A B AvB

0 0 0

0 1 1

1 0 1

1 1 1

OR: Disjunction

NOT: Inversion

A !A

0 1

1 0

Page 4: Prolog Prolog: Programming in Logic Prolog programs consist of clauses and rules. Declarative programming –Emphasis is on data and relations between objects

Logical Operators

A B A->B

0 0 1

0 1 1

1 0 0

1 1 1

Implication

A->B is equivalent to !AvB

A B A<->B

0 0 1

0 1 0

1 0 0

1 1 1

Equivalence

A<->B is equivalent to:A->B ^ B->A(A ^ B) v (!A ^ !B)

Page 5: Prolog Prolog: Programming in Logic Prolog programs consist of clauses and rules. Declarative programming –Emphasis is on data and relations between objects

Propositional Calculus

• Represent facts as symbols.– Homer_likes_beer.– Lisa_likes_tofu.– !(Bart_likes_school)– Bart_likes_school -> Bart_does_homework.– Lisa_likes_school ^ Lisa_likes_music

• Easy, but overly verbose.

• No reuse.

Page 6: Prolog Prolog: Programming in Logic Prolog programs consist of clauses and rules. Declarative programming –Emphasis is on data and relations between objects

Predicate Calculus• A predicate is a function that maps from

zero or more objects to {T,F}.– Likes(Homer, beer)– !Likes(Bart, school)– Likes(Bart, school) -> Does(Bart, homework).– Happy(Lisa).– Gives(Moe, Homer, beer) -> Happy(Homer).

• Predicates provides a syntactic shorthand for propositions.

• No extra representational power.

Page 7: Prolog Prolog: Programming in Logic Prolog programs consist of clauses and rules. Declarative programming –Emphasis is on data and relations between objects

Inference Rules

• AND-elimination:– If A^B is true, then A is true.– likes(Homer,beer) ^ likes(Homer, food)->likes(Homer,beer)

• OR-introduction:– If A is true, then AvB is true.– likes(Homer,food)->likes(Homer,food)v likes(Homer,work).

• Modus Ponens:– If A->B is true, and A is true, then B is true.– likes(Lisa,school)->does(Lisa,homework) ^

likes(Lisa,school)-> Does(Lisa,homework)

Page 8: Prolog Prolog: Programming in Logic Prolog programs consist of clauses and rules. Declarative programming –Emphasis is on data and relations between objects

Quantification

• We would like to be able to express facts such as:– “Everyone who likes school does their

homework.”– “No one who likes beef also likes tofu.”– “Someone likes beef.”

• This requires us to be able to quantify over variables in the domain.

Page 9: Prolog Prolog: Programming in Logic Prolog programs consist of clauses and rules. Declarative programming –Emphasis is on data and relations between objects

First-Order Logic

• First order logic allows quantification over objects.

• Universal quantification: “For all x”x likes(x,school) -> does(x, homework).

“Everyone who likes school does their homework.”

• Existential quantification: “There exists an x”x likes(x,tofu).

“There exists someone who likes tofu.”

Page 10: Prolog Prolog: Programming in Logic Prolog programs consist of clauses and rules. Declarative programming –Emphasis is on data and relations between objects

Computational Issues• Full FOL is too computationally difficult to

work with.– Nested quantifiers are semi-decidable.xyz likes(x,y) ^ likes(y,z).“Everyone likes a person who likes everyone.”– Implications with an AND in the consequent

are exponentially hard.x likes(x, school) -> does(x,homework) ^

attends(x,class)“Everyone who likes school does their homework

and attends class.”

Page 11: Prolog Prolog: Programming in Logic Prolog programs consist of clauses and rules. Declarative programming –Emphasis is on data and relations between objects

Horn clauses

• Horn clauses are implications of the form:x ^ y ^ z -> a

• Only a single term in the consequent.

• Horn clause inference is computationally tractable.

• Prolog uses Horn clauses.

Page 12: Prolog Prolog: Programming in Logic Prolog programs consist of clauses and rules. Declarative programming –Emphasis is on data and relations between objects

Knowledge Representation in Prolog

• Facts (unit clauses):– likes(homer, beer).

• Constants are lower case.

• Names of relations are lower case.

• Conjunctions:– likes(homer,beer).– likes(homer,food).

• Statement is represented as separate Prolog clauses.

<- ends with a period

Page 13: Prolog Prolog: Programming in Logic Prolog programs consist of clauses and rules. Declarative programming –Emphasis is on data and relations between objects

Knowledge Representation in Prolog

• Rules– does(lisa,homework) :- likes(lisa,school).– Equivalent to:

• likes(lisa,school) ->does(lisa,homework)

– Read as: “Lisa does homework if she likes school.” or …

– “To prove that Lisa does homework, prove that she likes school.”

Page 14: Prolog Prolog: Programming in Logic Prolog programs consist of clauses and rules. Declarative programming –Emphasis is on data and relations between objects

Variables

• Variables are all upper-case.

• Universal quantification is achieved by using variables in rules.– does(X,homework) :- likes(X,school).

• “Everyone who likes school does their homework.”

• “To prove that someone does their homework, prove that they like school.”