tutorial: logic programming

91
Tutorial: logic programming Markus Krötzsch Institute AIFB, Universität Karlsruhe [email protected] Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 1 / 37

Upload: others

Post on 12-Sep-2021

15 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Tutorial: logic programming

Tutorial: logic programming

Markus Krötzsch

Institute AIFB, Universität [email protected]

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 1 / 37

Page 2: Tutorial: logic programming

Outline

1 Basics

2 Propositional logic programs

3 Definite logic programs

4 Normal programs

5 F-Logic

6 Summary

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 2 / 37

Page 3: Tutorial: logic programming

Outline

1 Basics

2 Propositional logic programs

3 Definite logic programs

4 Normal programs

5 F-Logic

6 Summary

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 3 / 37

Page 4: Tutorial: logic programming

Rules

A (logic programming) rule is a formula of the form

B1 ∧ B2 ∧ . . . ∧ Bn︸ ︷︷ ︸

Body

→ H1 ∨ H2 ∨ . . . ∨ Hm︸ ︷︷ ︸

Head

Bi and Hj are usually literals, i.e. negated or non-negated logicalatoms

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 4 / 37

Page 5: Tutorial: logic programming

Rules

A (logic programming) rule is a formula of the form

B1 ∧ B2 ∧ . . . ∧ Bn︸ ︷︷ ︸

Body

→ H1 ∨ H2 ∨ . . . ∨ Hm︸ ︷︷ ︸

Head

Bi and Hj are usually literals, i.e. negated or non-negated logicalatoms

Disjunctions in the body are not needed:

A ∨ B → C ⇒A → CB → C

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 4 / 37

Page 6: Tutorial: logic programming

Rules

A (logic programming) rule is a formula of the form

B1 ∧ B2 ∧ . . . ∧ Bn︸ ︷︷ ︸

Body

→ H1 ∨ H2 ∨ . . . ∨ Hm︸ ︷︷ ︸

Head

Bi and Hj are usually literals, i.e. negated or non-negated logicalatoms

Disjunctions in the body are not needed:

A ∨ B → C ⇒A → CB → C

Conjunctions in the head are not needed:

A → B ∧ C ⇒A → BA → C

{ Lloyd-Topor translations

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 4 / 37

Page 7: Tutorial: logic programming

Clauses

In classical logic, implications are disjunctions:

B → H ≡ ¬B ∨ H

For a rule, we obtain:

B1 ∧ B2 ∧ . . . ∧ Bn → H1 ∨ H2 ∨ . . . ∨ Hm

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 5 / 37

Page 8: Tutorial: logic programming

Clauses

In classical logic, implications are disjunctions:

B → H ≡ ¬B ∨ H

For a rule, we obtain:

B1 ∧ B2 ∧ . . . ∧ Bn → H1 ∨ H2 ∨ . . . ∨ Hm

≡ ¬(B1 ∧ B2 ∧ . . . ∧ Bn) ∨ H1 ∨ H2 ∨ . . . ∨ Hm

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 5 / 37

Page 9: Tutorial: logic programming

Clauses

In classical logic, implications are disjunctions:

B → H ≡ ¬B ∨ H

For a rule, we obtain:

B1 ∧ B2 ∧ . . . ∧ Bn → H1 ∨ H2 ∨ . . . ∨ Hm

≡ ¬(B1 ∧ B2 ∧ . . . ∧ Bn) ∨ H1 ∨ H2 ∨ . . . ∨ Hm

≡ ¬B1 ∨ ¬B2 ∨ . . . ∨ ¬Bn ∨ H1 ∨ H2 ∨ . . . ∨ Hm

Disjunctions of literals are called clauses

{ rules as clauses

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 5 / 37

Page 10: Tutorial: logic programming

Outline

1 Basics

2 Propositional logic programs

3 Definite logic programs

4 Normal programs

5 F-Logic

6 Summary

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 6 / 37

Page 11: Tutorial: logic programming

Propositional logic programs

A propositional logic program . . .

is a finite set of rules which are

propositional: only propositional atoms,

definite: exactly one atom in the head.

General form: B1 ∧ B2 ∧ . . . ∧ Bn → H

Common syntax: H D B1,B2, . . . ,Bn

Fact: rule with empty body ({ body vacuously true)

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 7 / 37

Page 12: Tutorial: logic programming

Example

abc D ae D b, de D b, ce D a, b, e

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 8 / 37

Page 13: Tutorial: logic programming

Semantics of propositional programs

Standard semantics of propositional logic:

An interpretation I . . .

is a mapping ·I from formulae to {t , f }, such that

(A ∧ B)I = t iff AI = t and BI = t ,

(A ∨ B)I = t iff AI = t or BI = t ,

¬AI = t iff AI = f .

{We can view interpretations as sets of (true) atoms

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 9 / 37

Page 14: Tutorial: logic programming

Semantics of propositional programs

Standard semantics of propositional logic:

An interpretation I . . .

is a mapping ·I from formulae to {t , f }, such that

(A ∧ B)I = t iff AI = t and BI = t ,

(A ∨ B)I = t iff AI = t or BI = t ,

¬AI = t iff AI = f .

{We can view interpretations as sets of (true) atoms

Take a set of formulae F (e.g. a propositional program).

I is a model of F if FI = t for all F ∈ F .

A formula G is a consequence of F if GI = t for all models I of F .

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 9 / 37

Page 15: Tutorial: logic programming

Bottom-up computation

How can we compute the above semantics?

Observation: every rule has exactly one consequence

Idea: compute all consequences bottom-up, step by step

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 10 / 37

Page 16: Tutorial: logic programming

Bottom-up computation

How can we compute the above semantics?

Observation: every rule has exactly one consequence

Idea: compute all consequences bottom-up, step by step

The TP operator

Consider a program P. The function TP maps interpretations tointerpretations:

TP(I) = {H | P contains a rule H D B1, . . . ,Bn such that Bi ∈ I}

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 10 / 37

Page 17: Tutorial: logic programming

Bottom-up computation

How can we compute the above semantics?

Observation: every rule has exactly one consequence

Idea: compute all consequences bottom-up, step by step

The TP operator

Consider a program P. The function TP maps interpretations tointerpretations:

TP(I) = {H | P contains a rule H D B1, . . . ,Bn such that Bi ∈ I}

Step-wise computation:

T0P = ∅ Ti+1

P = TP(TiP)

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 10 / 37

Page 18: Tutorial: logic programming

Bottom-up computation (2)

Example: P =a c D a e D b, cb e D b, d e D a, b, e

{ T0P = ∅

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 11 / 37

Page 19: Tutorial: logic programming

Bottom-up computation (2)

Example: P =a c D a e D b, cb e D b, d e D a, b, e

{ T0P = ∅, T1

P = {a, b}

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 11 / 37

Page 20: Tutorial: logic programming

Bottom-up computation (2)

Example: P =a c D a e D b, cb e D b, d e D a, b, e

{ T0P = ∅, T1

P = {a, b}, T2P = {a, b , c}

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 11 / 37

Page 21: Tutorial: logic programming

Bottom-up computation (2)

Example: P =a c D a e D b, cb e D b, d e D a, b, e

{ T0P = ∅, T1

P = {a, b}, T2P = {a, b , c}, T3

P = {a, b , c, e}

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 11 / 37

Page 22: Tutorial: logic programming

Bottom-up computation (2)

Example: P =a c D a e D b, cb e D b, d e D a, b, e

{ T0P = ∅, T1

P = {a, b}, T2P = {a, b , c}, T3

P = {a, b , c, e}{ T4

P = {a, b , c, e} = T5P = T6

P = . . .

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 11 / 37

Page 23: Tutorial: logic programming

Bottom-up computation (2)

Example: P =a c D a e D b, cb e D b, d e D a, b, e

{ T0P = ∅, T1

P = {a, b}, T2P = {a, b , c}, T3

P = {a, b , c, e}{ T4

P = {a, b , c, e} = T5P = T6

P = . . .

I is a fixed point of TP if TP(I) = I

T∞P =⋃

i≥0 TiP is always the least fixed point and the least model

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 11 / 37

Page 24: Tutorial: logic programming

Goal-directed computation

Computing all consequences is not efficient.

How can we directly check whether some consequence holds?

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 12 / 37

Page 25: Tutorial: logic programming

Goal-directed computation

Computing all consequences is not efficient.

How can we directly check whether some consequence holds?

Reasoning by contradiction

F is a consequence of P iff ¬F ∧ P has no models (i.e. is unsatisfiable).

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 12 / 37

Page 26: Tutorial: logic programming

Goal-directed computation

Computing all consequences is not efficient.

How can we directly check whether some consequence holds?

Reasoning by contradiction

F is a consequence of P iff ¬F ∧ P has no models (i.e. is unsatisfiable).

Strategy:

Question: does Q1 ∧ Q2 ∧ . . . ∧Qn follow from P?

Step 1: negate query ¬(Q1 ∧ . . . ∧ Qn)

Step 2: check whether P ∧ ¬(Q1 ∧ . . . ∧Qn) is satisfiable

{ A goal is a formula of the form ¬(Q1 ∧ . . . ∧ Qn){ Goals can be written as D Q1, . . . ,Qn

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 12 / 37

Page 27: Tutorial: logic programming

Resolution

How can we check unsatisfiability?{ Check whether a contradictory statements follows!

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 13 / 37

Page 28: Tutorial: logic programming

Resolution

How can we check unsatisfiability?{ Check whether a contradictory statements follows!

What is a contradictory statement?

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 13 / 37

Page 29: Tutorial: logic programming

Resolution

How can we check unsatisfiability?{ Check whether a contradictory statements follows!

What is a contradictory statement?Recall: an implication is a disjunctionRecall: the empty implication is always false

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 13 / 37

Page 30: Tutorial: logic programming

Resolution

How can we check unsatisfiability?{ Check whether a contradictory statements follows!

What is a contradictory statement?Recall: an implication is a disjunctionRecall: the empty implication is always false{ the one and only contradictory rule is “→”

How to compute logic consequences of a program?

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 13 / 37

Page 31: Tutorial: logic programming

Resolution

How can we check unsatisfiability?{ Check whether a contradictory statements follows!

What is a contradictory statement?Recall: an implication is a disjunctionRecall: the empty implication is always false{ the one and only contradictory rule is “→”

How to compute logic consequences of a program? { Resolution

The resolution rule:A → B B → C

A → C

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 13 / 37

Page 32: Tutorial: logic programming

Resolution

How can we check unsatisfiability?{ Check whether a contradictory statements follows!

What is a contradictory statement?Recall: an implication is a disjunctionRecall: the empty implication is always false{ the one and only contradictory rule is “→”

How to compute logic consequences of a program? { Resolution

The resolution rule:A → B B → C

A → C

More generally:

A1 ∧ . . . ∧ An → Bi B1 ∧ . . . ∧ Bi ∧ . . . ∧ Bm → CA1 ∧ . . . ∧ An ∧ B1 ∧ . . . ∧ Bi−1 ∧ Bi+1 ∧ . . . ∧ Bm → C

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 13 / 37

Page 33: Tutorial: logic programming

SLD-resolution

Fact: only resolutions steps with a goal as a main premise areneeded:

A1 ∧ . . . ∧ An → Bi B1 ∧ . . . ∧ Bi ∧ . . . ∧ Bm →

A1 ∧ . . . ∧ An ∧ B1 ∧ . . . ∧ Bi−1 ∧ Bi+1 ∧ . . . ∧ Bm →

{ linear resolution: apply rules only to the latest goal

Fact: selection of Bi is “don’t care” nondeterministic{ any selectionrule can be chosen

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 14 / 37

Page 34: Tutorial: logic programming

SLD-resolution

Fact: only resolutions steps with a goal as a main premise areneeded:

A1 ∧ . . . ∧ An → Bi B1 ∧ . . . ∧ Bi ∧ . . . ∧ Bm →

A1 ∧ . . . ∧ An ∧ B1 ∧ . . . ∧ Bi−1 ∧ Bi+1 ∧ . . . ∧ Bm →

{ linear resolution: apply rules only to the latest goal

Fact: selection of Bi is “don’t care” nondeterministic{ any selectionrule can be chosen

SLD-resolutionSelection-driven, Linear resolution for Definite clauses (a “folkacronym”).

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 14 / 37

Page 35: Tutorial: logic programming

SLD-trees and backtracking

Choice of program rule to resolve upon is “don’t known” nondeterministic{ backtracking needed

(1) a (3) c D a (5) e D b, c(2) b (4) e D b, d (6) e D a, b, e

D e

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 15 / 37

Page 36: Tutorial: logic programming

SLD-trees and backtracking

Choice of program rule to resolve upon is “don’t known” nondeterministic{ backtracking needed

(1) a (3) c D a (5) e D b, c(2) b (4) e D b, d (6) e D a, b, e

D e(4)

yyssssssssss

D b , d

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 15 / 37

Page 37: Tutorial: logic programming

SLD-trees and backtracking

Choice of program rule to resolve upon is “don’t known” nondeterministic{ backtracking needed

(1) a (3) c D a (5) e D b, c(2) b (4) e D b, d (6) e D a, b, e

D e(4)

yyssssssssss

D b , d

(2)

��

D d

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 15 / 37

Page 38: Tutorial: logic programming

SLD-trees and backtracking

Choice of program rule to resolve upon is “don’t known” nondeterministic{ backtracking needed

(1) a (3) c D a (5) e D b, c(2) b (4) e D b, d (6) e D a, b, e

D e(4)

yyssssss

ssss

(5)��

D b , d

(2)

��

D b , c

D d

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 15 / 37

Page 39: Tutorial: logic programming

SLD-trees and backtracking

Choice of program rule to resolve upon is “don’t known” nondeterministic{ backtracking needed

(1) a (3) c D a (5) e D b, c(2) b (4) e D b, d (6) e D a, b, e

D e(4)

yyssssss

ssss

(5)��

D b , d

(2)

��

D b , c

(2)

��

D d D c

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 15 / 37

Page 40: Tutorial: logic programming

SLD-trees and backtracking

Choice of program rule to resolve upon is “don’t known” nondeterministic{ backtracking needed

(1) a (3) c D a (5) e D b, c(2) b (4) e D b, d (6) e D a, b, e

D e(4)

yyssssss

ssss

(5)��

D b , d

(2)

��

D b , c

(2)

��

D d D c

(3)

��

D a

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 15 / 37

Page 41: Tutorial: logic programming

SLD-trees and backtracking

Choice of program rule to resolve upon is “don’t known” nondeterministic{ backtracking needed

(1) a (3) c D a (5) e D b, c(2) b (4) e D b, d (6) e D a, b, e

D e(4)

yyssssss

ssss

(5)��

D b , d

(2)

��

D b , c

(2)

��

D d D c

(3)

��

D a

(1)��

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 15 / 37

Page 42: Tutorial: logic programming

SLD-trees and backtracking

Choice of program rule to resolve upon is “don’t known” nondeterministic{ backtracking needed

(1) a (3) c D a (5) e D b, c(2) b (4) e D b, d (6) e D a, b, e

D e(4)

yyssssss

ssss

(5)��

(6)

&&MMMMMMMMMM

D b , d

(2)

��

D b , c

(2)

��

D a, b , e

D d D c

(3)

��

D a

(1)��

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 15 / 37

Page 43: Tutorial: logic programming

SLD-trees and backtracking

Choice of program rule to resolve upon is “don’t known” nondeterministic{ backtracking needed

(1) a (3) c D a (5) e D b, c(2) b (4) e D b, d (6) e D a, b, e

D e(4)

yyssssss

ssss

(5)��

(6)

&&MMMMMMMMMM

D b , d

(2)

��

D b , c

(2)

��

D a, b , e

(1)��

D d D c

(3)

��

D b , e

(2)

��

D a

(1)��

D e

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 15 / 37

Page 44: Tutorial: logic programming

SLD-trees and backtracking

Choice of program rule to resolve upon is “don’t known” nondeterministic{ backtracking needed

(1) a (3) c D a (5) e D b, c(2) b (4) e D b, d (6) e D a, b, e

D e(4)

yyssssss

ssss

(5)��

(6)

&&MMMMMMMMMM

D b , d

(2)

��

D b , c

(2)

��

D a, b , e

(1)��

D d D c

(3)

��

D b , e

(2)

��

D a

(1)��

D e

��� . . .

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 15 / 37

Page 45: Tutorial: logic programming

Complexity of propositional programs

Computation with propositional logic programs can be done in P (which iseasy to see from the computation of TP).

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 16 / 37

Page 46: Tutorial: logic programming

Outline

1 Basics

2 Propositional logic programs

3 Definite logic programs

4 Normal programs

5 F-Logic

6 Summary

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 17 / 37

Page 47: Tutorial: logic programming

Definite logic programs

Instead of propositional atoms, we have a first order language:predicate symbols with some arity, e.g. Rconstants, e.g. a, bfunction symbols with some arity, e.g. f , gvariables, e.g. X , Y

A term is any well-formed expression built from constants, functions,and variables. Terms without variables are called ground.

An atom is an expression R(t1, . . . , tn) with R an n-ary predicate andt1, . . . , tn terms

A definite rule is a rule of the form

H D B1, . . . ,Bm

with H,B1, . . . ,Bm atoms, where m might be 0.

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 18 / 37

Page 48: Tutorial: logic programming

What do variables mean?

Variables are universally quantified on rule-level.

Example: uncle(X ,Z) D brother(X ,Y), parent(Y ,Z)

Rules with variables can be read as “macros” for rules obtained byuniformly replacing all variables with ground terms

More generally: variables can be uniformly replaced by any term{substitution

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 19 / 37

Page 49: Tutorial: logic programming

Example

number(0)number(s(X)) D number(X)

add(0,Y ,Y)add(s(X),Y ,Z) D add(X , s(Y),Z)

mul(0,Y , 0)mul(s(X),Y ,Z) D mul(X ,Y ,Z ′), add(Y ,Z ′,Z)

Example conclusions:number(s(s(s(0)))), add(s(s((0)), s(0), s(s(s(0)))), . . .

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 20 / 37

Page 50: Tutorial: logic programming

Example

number(0)number(s(X)) D number(X)

add(0,Y ,Y)add(s(X),Y ,Z) D add(X , s(Y),Z)

mul(0,Y , 0)mul(s(X),Y ,Z) D mul(X ,Y ,Z ′), add(Y ,Z ′,Z)

Example conclusions:number(s(s(s(0)))), add(s(s((0)), s(0), s(s(s(0)))), . . .

{ Definite programs can encode infinite domains (they can also enforcethem, though not in the above example).

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 20 / 37

Page 51: Tutorial: logic programming

Semantics

Definite logic program as abbreviation for infinite propositionalprogram

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 21 / 37

Page 52: Tutorial: logic programming

Semantics

Definite logic program as abbreviation for infinite propositionalprogram

Example:number(s(X)) D number(X)

{

number(s(0)) D number(0)number(s(s(0))) D number(s(0))

number(s(s(s(0)))) D number(s(s(0))). . .

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 21 / 37

Page 53: Tutorial: logic programming

Semantics

Definite logic program as abbreviation for infinite propositionalprogram

Example:number(s(X)) D number(X)

{

number(s(0)) D number(0)number(s(s(0))) D number(s(0))

number(s(s(s(0)))) D number(s(s(0))). . .

ground atoms instead of propositional atoms

interpretations as sets of (true) ground atoms{ Herbrand interpretation

TP operator defined as before (on the infinite set of ground rules)

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 21 / 37

Page 54: Tutorial: logic programming

Goal-directed computation

How can we practically compute consequences?

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 22 / 37

Page 55: Tutorial: logic programming

Goal-directed computation

How can we practically compute consequences?{ Compute (partially) ground rules “on demand”{ Two literals represent overlapping sets of ground atoms if they canbe unified.

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 22 / 37

Page 56: Tutorial: logic programming

Goal-directed computation

How can we practically compute consequences?{ Compute (partially) ground rules “on demand”{ Two literals represent overlapping sets of ground atoms if they canbe unified.

Example: R(X , f(a)) and R(g(Z),W) are unifiable

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 22 / 37

Page 57: Tutorial: logic programming

Goal-directed computation

How can we practically compute consequences?{ Compute (partially) ground rules “on demand”{ Two literals represent overlapping sets of ground atoms if they canbe unified.

Example: R(X , f(a)) and R(g(Z),W) are unifiable{ R(g(Z), f(a)) represents all ground instances both agree on{ substitution σ = {X 7→ g(Z),W 7→ f(a)}

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 22 / 37

Page 58: Tutorial: logic programming

Goal-directed computation

How can we practically compute consequences?{ Compute (partially) ground rules “on demand”{ Two literals represent overlapping sets of ground atoms if they canbe unified.

Example: R(X , f(a)) and R(g(Z),W) are unifiable{ R(g(Z), f(a)) represents all ground instances both agree on{ substitution σ = {X 7→ g(Z),W 7→ f(a)}

Resolution on non-ground rules:

A → B B ′ → C(A → C)σ

if Bσ = B ′σ

propositional SLD-resolution can be lifted

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 22 / 37

Page 59: Tutorial: logic programming

Example: SLD resolution for definite programs

(1) add(0,Y ,Y)(2) add(s(X),Y ,Z) D add(X , s(Y),Z)

Goal: D add(s(0), s(0),W)

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 23 / 37

Page 60: Tutorial: logic programming

Example: SLD resolution for definite programs

(1) add(0,Y ,Y)(2) add(s(X),Y ,Z) D add(X , s(Y),Z)

Goal: D add(s(0), s(0),W)

D add(s(0), s(0),W)

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 23 / 37

Page 61: Tutorial: logic programming

Example: SLD resolution for definite programs

(1) add(0,Y ,Y)(2) add(s(X),Y ,Z) D add(X , s(Y),Z)

Goal: D add(s(0), s(0),W)

D add(s(0), s(0),W) + (2) σ1 = {X1 7→ 0,Y1 7→ s(0),Z1 7→ W }

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 23 / 37

Page 62: Tutorial: logic programming

Example: SLD resolution for definite programs

(1) add(0,Y ,Y)(2) add(s(X),Y ,Z) D add(X , s(Y),Z)

Goal: D add(s(0), s(0),W)

D add(s(0), s(0),W) + (2) σ1 = {X1 7→ 0,Y1 7→ s(0),Z1 7→ W }

D add(0, s(s(0)),W)

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 23 / 37

Page 63: Tutorial: logic programming

Example: SLD resolution for definite programs

(1) add(0,Y ,Y)(2) add(s(X),Y ,Z) D add(X , s(Y),Z)

Goal: D add(s(0), s(0),W)

D add(s(0), s(0),W) + (2) σ1 = {X1 7→ 0,Y1 7→ s(0),Z1 7→ W }

D add(0, s(s(0)),W) + (1) σ2 = {Y2 7→ s(s(0)),W 7→ s(s(0))}

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 23 / 37

Page 64: Tutorial: logic programming

Example: SLD resolution for definite programs

(1) add(0,Y ,Y)(2) add(s(X),Y ,Z) D add(X , s(Y),Z)

Goal: D add(s(0), s(0),W)

D add(s(0), s(0),W) + (2) σ1 = {X1 7→ 0,Y1 7→ s(0),Z1 7→ W }

D add(0, s(s(0)),W) + (1) σ2 = {Y2 7→ s(s(0)),W 7→ s(s(0))}

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 23 / 37

Page 65: Tutorial: logic programming

Example: SLD resolution for definite programs

(1) add(0,Y ,Y)(2) add(s(X),Y ,Z) D add(X , s(Y),Z)

Goal: D add(s(0), s(0),W)

D add(s(0), s(0),W) + (2) σ1 = {X1 7→ 0,Y1 7→ s(0),Z1 7→ W }

D add(0, s(s(0)),W) + (1) σ2 = {Y2 7→ s(s(0)),W 7→ s(s(0))}

Result: Wσ1σ2 = s(s(0))

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 23 / 37

Page 66: Tutorial: logic programming

How hard is this computation?

Extend the earlier example:pow(X , 0, s(0))

pow(X , s(Y),Z) D pow(X ,Y ,Z ′),mul(X ,Z ′,Z)

Goal: D pow(X , s(s(s(N))),XP), pow(Y , s(s(s(N))),YP),pow(Z , s(s(s(N))),ZP), add(XP,YP,ZP)

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 24 / 37

Page 67: Tutorial: logic programming

How hard is this computation?

Extend the earlier example:pow(X , 0, s(0))

pow(X , s(Y),Z) D pow(X ,Y ,Z ′),mul(X ,Z ′,Z)

Goal: D pow(X , s(s(s(N))),XP), pow(Y , s(s(s(N))),YP),pow(Z , s(s(s(N))),ZP), add(XP,YP,ZP)

In words:“Which natural numbers satisfy the equation xn + yn = zn, for n > 2?”

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 24 / 37

Page 68: Tutorial: logic programming

How hard is this computation?

Extend the earlier example:pow(X , 0, s(0))

pow(X , s(Y),Z) D pow(X ,Y ,Z ′),mul(X ,Z ′,Z)

Goal: D pow(X , s(s(s(N))),XP), pow(Y , s(s(s(N))),YP),pow(Z , s(s(s(N))),ZP), add(XP,YP,ZP)

In words:“Which natural numbers satisfy the equation xn + yn = zn, for n > 2?”

Uh oh . . .

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 24 / 37

Page 69: Tutorial: logic programming

How hard is this computation?

Extend the earlier example:pow(X , 0, s(0))

pow(X , s(Y),Z) D pow(X ,Y ,Z ′),mul(X ,Z ′,Z)

Goal: D pow(X , s(s(s(N))),XP), pow(Y , s(s(s(N))),YP),pow(Z , s(s(s(N))),ZP), add(XP,YP,ZP)

In words:“Which natural numbers satisfy the equation xn + yn = zn, for n > 2?”

Uh oh . . .

Definite logic programming is indeed undecidable.

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 24 / 37

Page 70: Tutorial: logic programming

How hard is this computation?

Extend the earlier example:pow(X , 0, s(0))

pow(X , s(Y),Z) D pow(X ,Y ,Z ′),mul(X ,Z ′,Z)

Goal: D pow(X , s(s(s(N))),XP), pow(Y , s(s(s(N))),YP),pow(Z , s(s(s(N))),ZP), add(XP,YP,ZP)

In words:“Which natural numbers satisfy the equation xn + yn = zn, for n > 2?”

Uh oh . . .

Definite logic programming is indeed undecidable.But it is semi-decidable: all positive conclusions eventually appear

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 24 / 37

Page 71: Tutorial: logic programming

Decidable cases

Non-recursive definite programs

Termination guaranteed for logic programs without recursive rules.

Computational complexity: NET (just like OWL DL)

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 25 / 37

Page 72: Tutorial: logic programming

Decidable cases

Non-recursive definite programs

Termination guaranteed for logic programs without recursive rules.

Computational complexity: NET (just like OWL DL)

Datalog

A datalog progam is a definite program without function symbols.

Datalog is decidable

Computational complexity: ET (just like OWL Lite)even with just a single rule and no facts (“sirup”)

Data complexity: P{ useful for deductive databases

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 25 / 37

Page 73: Tutorial: logic programming

Outline

1 Basics

2 Propositional logic programs

3 Definite logic programs

4 Normal programs

5 F-Logic

6 Summary

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 26 / 37

Page 74: Tutorial: logic programming

Logical programs with negation

Normal logic programs: first-order programs with negation in the body

typically non-monotonic negation

Example:bird(tweety)

bird(tux)penguin(tux)

fly(X) D bird(X), not penguin(X)

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 27 / 37

Page 75: Tutorial: logic programming

Negation as failure

How to compute with non-monotonic negation?

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 28 / 37

Page 76: Tutorial: logic programming

Negation as failure

How to compute with non-monotonic negation?

Negation as failure:

negated statements are true if it their positive version cannot be derived{ start a new proof tree when encountering negated statements{ SLDNF-resolution (SLD + Negation as Failure)

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 28 / 37

Page 77: Tutorial: logic programming

Negation as failure

How to compute with non-monotonic negation?

Negation as failure:

negated statements are true if it their positive version cannot be derived{ start a new proof tree when encountering negated statements{ SLDNF-resolution (SLD + Negation as Failure)

operational, not declarative

implemented in Prolog (usually based on incompleteSLD-implementation, making the system unsound when usingnegation)

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 28 / 37

Page 78: Tutorial: logic programming

Semantic issues

What does the following mean?p D not qq D not p

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 29 / 37

Page 79: Tutorial: logic programming

Semantic issues

What does the following mean?p D not qq D not p

Classical semantics: p ∨ q

Negation as failure: loop proof search

Intuition(?): neither p nor q “really” hold

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 29 / 37

Page 80: Tutorial: logic programming

Semantic issues

What does the following mean?p D not qq D not p

Classical semantics: p ∨ q

Negation as failure: loop proof search

Intuition(?): neither p nor q “really” hold

{ Introduction of three-valued semantics: true, false, unknown

Example: Well-founded semantics

Many different non-monotonic semantics, usually agree on morewell-behaved programs

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 29 / 37

Page 81: Tutorial: logic programming

Expressive power

How powerful are non-monotonic semantics?

Recall: in definite programs, we could ask whether there is acounterexample to Fermat’s Last Theorem

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 30 / 37

Page 82: Tutorial: logic programming

Expressive power

How powerful are non-monotonic semantics?

Recall: in definite programs, we could ask whether there is acounterexample to Fermat’s Last Theorem

In normal logic programs, we can also ask whether there is no suchcounterexample{ Non-monotonic semantics usually striclty more powerful thanmonotonic variants

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 30 / 37

Page 83: Tutorial: logic programming

Expressive power

How powerful are non-monotonic semantics?

Recall: in definite programs, we could ask whether there is acounterexample to Fermat’s Last Theorem

In normal logic programs, we can also ask whether there is no suchcounterexample{ Non-monotonic semantics usually striclty more powerful thanmonotonic variants

Well-founded semantics: not semidecidable (captures Π11-fragment of

second order logic)

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 30 / 37

Page 84: Tutorial: logic programming

Outline

1 Basics

2 Propositional logic programs

3 Definite logic programs

4 Normal programs

5 F-Logic

6 Summary

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 31 / 37

Page 85: Tutorial: logic programming

F-Logic

“Object oriented” logic programming

different definitions of semantics: classical (first-order) and LP(non-monotonic)

no normative defintions for syntax or semantics

Implementation in ONtoobroker and Flora: assumes well-foundedsemantics (and computes unspecified parts of it){ systems cannot return all answers or all true answers, even ininfinite time

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 32 / 37

Page 86: Tutorial: logic programming

F-Logic example syntax

/* Atoms */

iokaste:woman.

laios:man.

/* Molecules */

oedipus:man[father->laios; mother->iokaste].

polyneikes:man[father->oedipus; mother->iokaste;

sister->>{antigone, ismene};

brother->>eteokles:man]

/* Rules */

FORALL X,Y X[son->>Y] <- Y:man[father->X].

FORALL X,Y X[son->>Y] <- Y:man[mother->X].

/* Queries */

FORALL X,Y <- X:man[sohn->>Y[mother->iokaste]].

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 33 / 37

Page 87: Tutorial: logic programming

F-Logic semantics

“Object oriented” notation is translated into facts:

a:C 7→ isa_(a,C)

A::B 7→ sub_(A,B)

a[B->c] 7→ att_(a,B,c)

a[B-»c] 7→ setatt_(a,B,c)

A[B=>C] 7→ atttype_(A,B,C)

A[B=»C] 7→ setatttype_(A,B,C)

Properties of auxilliary predicates such as sub_ described by axioms.

Further syntactic features treated with Lloyd-Topor translations

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 34 / 37

Page 88: Tutorial: logic programming

F-Logic semantics

“Object oriented” notation is translated into facts:

a:C 7→ isa_(a,C)

A::B 7→ sub_(A,B)

a[B->c] 7→ att_(a,B,c)

a[B-»c] 7→ setatt_(a,B,c)

A[B=>C] 7→ atttype_(A,B,C)

A[B=»C] 7→ setatttype_(A,B,C)

Properties of auxilliary predicates such as sub_ described by axioms.

Further syntactic features treated with Lloyd-Topor translations

Warning! The above is based on incomplete descriptions in Angele andLausen (2004), which merely sketch the semantics. I know of nonormative public spec of the current implementation in Ontobroker or Flora(which does not say that such documents do not exist).

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 34 / 37

Page 89: Tutorial: logic programming

Outline

1 Basics

2 Propositional logic programs

3 Definite logic programs

4 Normal programs

5 F-Logic

6 Summary

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 35 / 37

Page 90: Tutorial: logic programming

Summary

Complexity overview:Formalism Complexity Var.? Func.? Neg.?

Propositional programs P — — —Datalog ET-complete × — —Definite programs r.e.-complete × × —— without recursion NET-complete × × —Well-founded semantics Π1

1-complete × × ×

Prolog is based on definite programs

F-Logic is based on well-founded semantics + F-Logic

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 36 / 37

Page 91: Tutorial: logic programming

References

J.W. Lloyd. Foundations of Logic Programming. Springer, 1987.

E. Dantsin, T. Eiter, G. Gottlob, and A. Voronkov. Complexity andexpressive power of logic programming. ACM ComputingSurveys, 33(3): 374–425, 2001.

G. A. Ringwood. SLD: a folk acronym? ACM SIGPLAN Notices,24(5): 71–75, 1989.

J. Angele and G. Lausen. Ontologies in F-logic. In S. Staab and R.Studer, editors, Handbook on Ontologies, pages 29–50. Springer,2004.

Markus Krötzsch (AIFB Karlsruhe) Logic programming Oct 24 2006 37 / 37