CSCI-2325 Logic Programming in Prolog Reading: Ch 15 of - - PDF document

csci 2325 logic programming in prolog
SMART_READER_LITE
LIVE PREVIEW

CSCI-2325 Logic Programming in Prolog Reading: Ch 15 of - - PDF document

12/9/14 CSCI-2325 Logic Programming in Prolog Reading: Ch 15 of Tucker-Noonan Mohammad T . Irfan Logic Programming u Declarative/rule-based u Objective is specified, not algorithm u Solution is specified using logic u


slide-1
SLIDE 1

12/9/14 ¡ 1 ¡

CSCI-2325 Logic Programming in Prolog

Reading: Ch 15 of Tucker-Noonan

Mohammad T . Irfan

Logic Programming

u Declarative/rule-based u Objective is specified, not algorithm

u Solution is specified using logic

u Background

u Propositional and predicate logic u Example: There is no greatest prime number.

slide-2
SLIDE 2

12/9/14 ¡ 2 ¡

Horn clause

u Special type of predicate logic u p1 Λ p2 Λ ... Λ pn => h u To show h, first show p1, then p2, ..., pn u Notation: h ç p1, p2, ..., pn

u Equivalent to: (p1 Λ p2 Λ ... Λ pn) V h u Equivalent to: p1 V ... V pn V h

u Not all predicates can be written as Horn

clause

u Every literate person can read or write

Resolution

u Making inference from several clauses u Example (Wikipedia)

u All Greeks are Europeans.

Homer is a Greek. Therefore, Homer is a European.

u

Therefore, u Another example

u talksWith(X, Y) ç speaks(X, L), speaks(Y

, L), X ≠ Y

speaks(Alice, French) speaks(Bob, French)

u Therefore, talksWith(Alice, Bob)

Instantiation

slide-3
SLIDE 3

12/9/14 ¡ 3 ¡

Unification

u How to do the instantiations? u One solution: recursively find all possible

instantiations for which resolutions can be made

Prolog

Colmerauer, Rousseau, Kowalski (1970)

slide-4
SLIDE 4

12/9/14 ¡ 4 ¡

Resources

u Installation

u http://www.swi-prolog.org/Download.html

u Learning

u John Fisher’s problem-centric tutorial u http://www.csupomona.edu/~jrfisher/www/

prolog_tutorial/contents.html

Syntax

u Terms

u Constants

u ‘Alice’, english, zebra

u Variables

u Person, Language

u Structures

u Predicate (with possible arguments) u speaks(Person, Language)

u Rules

u Horn clauses: term :- term_1, term_2, ..., term_n

u Facts

u Horn clauses with empty head/goal

slide-5
SLIDE 5

12/9/14 ¡ 5 ¡

Example: “talk.pl”

u Program in talk.pl file speaks(alice, french). speaks(bob, french). speaks(clive, english). speaks(doug, french). talksWith(X,Y) :- speaks(X, L), speaks(Y, L), X \= Y. u consult(talk). u talksWith(alice, bob). u talksWith(alice, clive). u talksWith(P

, bob).

u listing(speaks).

Closed-world assumption

u Just because Prolog cannot prove something,

it will say it’s false

u true/fail system, not true/false

slide-6
SLIDE 6

12/9/14 ¡ 6 ¡

Example: family tree

grandparent(G,X) :- parent(P,X), parent(G,P). sibling(X,Y) :- parent(P, X), parent(P, Y), X \= Y. parent('Alice', 'Bob'). parent('Clive', 'Bob'). parent('Doug', 'Alice'). parent('Clive', 'Eric'). u grandparent(G, ‘Bob’). u sibling(S, ‘Bob’).

Trace

factorial(0, 1). factorial(N, Result) :- N > 0, M is N-1, factorial(M, SubResult), Result is N * SubResult.

u trace. %Enter trace mode. To exit: notrace u factorial(4, X).

Force instantiation

slide-7
SLIDE 7

12/9/14 ¡ 7 ¡

Eight queens problem

valid(TrialRow, TrialDiag1, TrialDiag2, RowList, Diag1List, Diag2List) :- not(member(TrialRow, RowList)), not(member(TrialDiag1, Diag1List)), not(member(TrialDiag2, Diag2List)). getDiag(Row, Col, Diag1, Diag2) :- Diag1 is Row + Col, Diag2 is Row - Col. place(N, Row, Col, RowList, Diag1List, Diag2List, Row) :- Row < N, getDiag(Row, Col, Diag1, Diag2), valid(Row, Diag1, Diag2, RowList, Diag1List, Diag2List). place(N, Row, Col, RowList, Diag1List, Diag2List, Answer) :- NextRow is Row + 1, NextRow < N, place(N, NextRow, Col, RowList, Diag1List, Diag2List, Answer).

slide-8
SLIDE 8

12/9/14 ¡ 8 ¡

solve(N, Col, RowList, _, _, RowList) :- Col >= N. solve(N, Col, RowList, Diag1List, Diag2List, Answer) :- Col < N, place(N, 0, Col, RowList, Diag1List, Diag2List, Row), getDiag(Row, Col, Diag1, Diag2), NextCol is Col + 1, solve(N, NextCol, [Row | RowList], [Diag1 | Diag1List], [Diag2 | Diag2List], Answer). queens(N, Answer) :- solve(N, 0, [], [], [], Answer).