9
Page 1 Logic Programming and Prolog Presenter: John Xu Date: Nov 09, 2004

Page 1 Nov 09, 2004CAS 701, McMaster Logic Programming and Prolog Presenter: John Xu Date: Nov 09, 2004

Embed Size (px)

Citation preview

Page 1: Page 1 Nov 09, 2004CAS 701, McMaster Logic Programming and Prolog Presenter: John Xu Date: Nov 09, 2004

Page 1

Logic Programming and Prolog

Presenter: John Xu

Date: Nov 09, 2004

Page 2: Page 1 Nov 09, 2004CAS 701, McMaster Logic Programming and Prolog Presenter: John Xu Date: Nov 09, 2004

Page 2

Logic Programming – Example

Suppose we have the following sentences

1) A graduate student of CAS must take the core course CAS 701

2) A student can not obtain a degree without passing the required core course

I am a graduate student of CAS and want to obtain a degree, should I pass CAS 701?

Page 3: Page 1 Nov 09, 2004CAS 701, McMaster Logic Programming and Prolog Presenter: John Xu Date: Nov 09, 2004

Page 3

Logic Programming – Example

Structured solution:

S: Being a graduate student of CAS

T: Taking core course CAS 701

O: Obtaining a degree

P: Passing required core course

Σ: S => T, ¬P => ¬O, S Λ O

Logic consequence: T Λ P

Page 4: Page 1 Nov 09, 2004CAS 701, McMaster Logic Programming and Prolog Presenter: John Xu Date: Nov 09, 2004

Page 4

Logic Programming – Definition

• Logic programming is a declarative approach of writing computer programs based on Logic

• Prolog, which stands for PROgramming in LOGic, is the most widely available logic programming language

Page 5: Page 1 Nov 09, 2004CAS 701, McMaster Logic Programming and Prolog Presenter: John Xu Date: Nov 09, 2004

Page 5

/* Filename: ancestor.pro This program creates a small set of facts and rules on

who is the ancestor of whom. The user types the desired goal interactively when the program is run.*/

/* ancestor(A, B) means A is an ancestor of B */ancestor(bob, susan).ancestor(A, X) :- parent(A, X).ancestor(A, X) :- parent(A, C), ancestor(C, X).

/* parent(P, C) means P is a parent of C */parent(fred, sally).parent(tina, sally).parent(sally, diane).parent(sam, bill).

Page 6: Page 1 Nov 09, 2004CAS 701, McMaster Logic Programming and Prolog Presenter: John Xu Date: Nov 09, 2004

Page 6

Logic Programming – Prolog Query

Queries:

• ancestor(fred, sally).

Yes

• ancestor(fred, diane).

Yes

• ancestor(fred, bill).

No

Page 7: Page 1 Nov 09, 2004CAS 701, McMaster Logic Programming and Prolog Presenter: John Xu Date: Nov 09, 2004

Page 7

Logic Programming – Implementation

SLD-Resolution (inference mechanism)

• Unification: a procedure that takes two atomic formulas as input, and either shows how they can be instantiated to identical atoms or, reports a failure

• An SLD-derivation of G0 (using P and <) is a finite or infinite sequence of goals: g0->g1->g2…->gn …

• SLD-refutation: a finite derivation of the goal g0• Failed derivation: a derivation of the goal g0 with the last

element that is not empty and cannot be resolved by any clause of the program

Page 8: Page 1 Nov 09, 2004CAS 701, McMaster Logic Programming and Prolog Presenter: John Xu Date: Nov 09, 2004

Page 8

Logic Programming – Implementation

• Soundness of SLD-resolution• Completeness of SLD-resolution• Cut: Pruning the SLD-tree

Page 9: Page 1 Nov 09, 2004CAS 701, McMaster Logic Programming and Prolog Presenter: John Xu Date: Nov 09, 2004

Page 9

Logic Programming – Reference

Book:• Ralph P. Grimaldi, DISCRETE AND COMBINATORIAL

MATHEMATICS, 5TH ED, Pearson Education, 2004 • Ulf Nilsson and Jan Ma luszy_nski, LOGIC, PROGRAMMING AND

PROLOG, 2ND ED, Ulf Nilsson and Jan Ma luszy_nski, 2000

Web:• http://cis.stvincent.edu/carlsond/prolog.html• http://ktiml.mff.cuni.cz/~bartak/prolog/index.html• http://www.cs.auckland.ac.nz/~j-hamer/07.363/prolog-for-se.html• http://www.swi-prolog.org/