Logic Programming: Inferring Queries from a Logic Program, Study notes of Discrete Structures and Graph Theory

Logic programming, a form of declarative programming where the goal is to infer new facts from a set of given clauses. The basics of logic programming, including the representation of queries, the process of sld resolution, and techniques for computing functions. Examples are provided using prolog, a popular logic programming language.

Typology: Study notes

Pre 2010

Uploaded on 08/18/2009

koofers-user-0lf
koofers-user-0lf 🇺🇸

10 documents

1 / 25

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Discrete Structures II
CS 251
Lecture 13
(Sections 9.2, 10.1)
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download Logic Programming: Inferring Queries from a Logic Program and more Study notes Discrete Structures and Graph Theory in PDF only on Docsity!

Discrete Structures II

CS 251

Lecture 13

(Sections 9.2, 10.1)

1

Section 9.2 Logic Programming

A logic program is a set of clauses with the restriction that there is exactly one

positive literal in each clause. Such clauses are often called definite clauses.

Example.

A  B 1  B 2 (One positive literal and two negative literals) A  B 1  B 2

Head Body

“A is true if B 1 and B 2 are true”

2

Query/Goal

A query or goal is a question that asks whether the program infers something. The something is a sequence of one or more atoms and the question is whether there is a substitution that can be applied to the atoms so that the resulting atoms are inferred by the program.

Example. Suppose we have the following little logic program. p(a, b). p(b, d). g(x, y) ← p(x, z), p(z, y).

Let g(a, w) be a query. It asks whether a has a grandchild. If we let θ = {w/d}, then g(a, w)θ = g(a, d), which says a has a grandchild d. This follows from the two program facts p(a, b) and p(b, d) and the definition of g. So g(a, d) is inferred by the program.

4

Formal representation of a query

From a formal viewpoint (i.e., first-order predicate calculus) a query is the existential closure of a conjunction of one or more atoms.

Example. The query g(a, w) is formally represented by ∃w g(a, w). We saw that the example program infers g(a, w)θ = g(a, d) for θ = {w/d}. But EG can be applied to g(a,d) to infer ∃w g(a, w). Therefore the program infers ∃w g(a, w).

Example. The query g(a, w), p(u, w) is formally represented by ∃w ∃u (g(a, w)∧ p(u,w)). If we let θ = {w/d, u/b}, then (g(a, w) ∧ p(u, w))θ = g(a, d) ∧ p(b, d), which follows from the two example program facts p(a, b) and p(b, d) and the definition of g. So g(a, d) ∧ p(b, d) is inferred by the program. Now apply EG twice to infer ∃w ∃u (g(a, w) ∧ p(u, w)). Therefore the program infers ∃w ∃u (g(a, w) ∧ p(u, w)).

5

SLD Resolution

SLD-resolution is a form of resolution used to execute logic programs. SLD

means selective linear resolution. Select the leftmost atom in the goal clause

to “cancel”. For the second clause, pick a program clause whose head unifies

with the atom selected from the goal clause.

To resolve the goal  B 1 ,….., Bk with the clause A  A 1 , …, An perform the

following steps:

  1. Unify B 1 and A and obtain the most general unifier .
  2. Replace B1 by the body atoms A 1 ,…., An.
  3. Apply  to the result to obtain the resolvant

 (A 1 ,…., An, B 1 ,….., Bk)

7

Example

Given the following little logic program and query, where the clauses are also

listed in first-order form.

Logic Programming Syntax First-Order Clauses

p(a, b). p(a, b).

p(b, d). p(b, d).

g(x, y) ← p(x, z), p(z, y). g(x, y) ∨ ¬ p(x, z) ∨ ¬ p(z, y).

The query:

← g(a, w), p(u, w). ¬ g(a, w) ∨ ¬ p(u, w).

Computation Trees

A computation tree for a query represents all possible ways to satisfy the

query. i.e., it represents all possible refutations by resolution.

The root is the query and a child of a node is a resolvant of the node with a

program clause.

The mgus are listed along each branch of the tree. Computed answers are

listed below the leaf of each successful refutation.

10

Example

Example. Given the following logic program and query:

p(a, b) p(c, b) p(b, d) g(x, y) ← p(x, z), p(z, y)

Query: ← g(w, d)

The computation tree for

the query is pictured.

 g(w, d) {x/w, y/d}  p(w, z), p(z, d) {w/a, z/b} {w/b, z/d} {w/c, z/b}  p(b, d)  p(b, d)  p(d, d) { } { } failure

Success Success w = a w=c

11

Example: The factorial function

f(x)= x. The recursive definition of f is : f(0) = 1 f(x) = x * f(x-1) Let fact be the predicate used to compute f. fact(0,1) fact(x, xf(x-1)) Let y=f(x-1) then we have fact(0,1) fact(x, xy) where, y=f(x-1) Create a predicate fact(0,1) fact(x, xy) where, fact(x-1, y) The desired logic program has the twp clauses: fact(0,1) fact(x, xy) fact(x-1, y)

13

10.1:What is Algebra?

To most people the word algebra means high school algebra. But there are many kinds of algebra. An algebra consists of one or more sets, called carriers, and one or more operations on the sets. The collection of operations is often called the signature of the algebra. Denote an algebra by listing the carriers, then a semicolon, then the operators in the signature. Examples. 〈 N ; +〉 〈 N ; 0, +〉 〈 Q ; +, ·, 0, 1〉 To solve problems with an algebra means to be able to apply properties of the operations. In the algebra 〈 Q ; +, ·, 0, 1〉, the following properties hold: x + 0 = x, x·1 = x, x + y = y + x, x · y=y · x, x· (y + z) = x · y + x · z, and so on.

Properties of binary operations

Let  be a binary operation on S.

  1.  is commutative if x  y = y  x for all x, y ∈ S.
  2.  is associative if (x  y)  z = x  (y  z) for all x, y ∈ S.
  3. An element e ∈ S is an identity for  if x  e = e  x = x for all x ∈ S.
  4. An element z ∈ S is a zero for  if x  z = z  x = z for all x ∈ S.
  5. If x, y ∈ S, and e is an identity, then y is an inverse of x if x  y = y  x = e.

Binary operation tables

If  is a binary operation on a finite set S, then we can represent  in table form, where the entry in row x and column y is the value x  y.

Example. Let S = {0, 1, 2, 3} and let x  y = (x + y) mod 4. Then the operation table for  is pictured.

For the table pictured in the previous example, state whether  satisfies any of the following properties: commutative, associative, identity, zero, inverses. Answer. Is commutative; is associative; has identity 0; no zero; has inverses 1  3 = 0, 2  2 = 0, and 0  0 = 0.

Using properties of operations

Algebras are useful when we apply properties of the operations to solve problems.

Example. Suppose we want to prove the following statement about integers:

x + x = x implies x = 0. Here is an “equational proof” with reasons in parentheses.

Proof:

x = x + 0 (0 is identity for +)

= x + (x + – x) (–x is the inverse of x with respect to +)

= (x + x) + – x (+ is associative)

= x + – x (hypothesis)

= 0 (–x is the inverse of x with respect to +). QED.

Notice in the example that we used several properties of the algebra 〈 Z ; 0, +〉.

Namely, that + has an identity, + is associative, and an inverse exists for every integer.

Example

Given the algebra 〈 Z ; 0, +〉. Prove the cancellation law:

x + z = y + z implies x = y.

Answer.

x = x + 0 (0 is identity for +)

= x + (z + – z) (–z is the inverse of z with respect to +)

= (x + z) + – z (+ is associative)

= (y + z) + – z (hypothesis)

= y + (z + – z) (+ is associative)

= y + 0 (–z is the inverse of z with respect to +)

= y (0 is identity for +). QED.