<<

12/9/14

CSCI-2325 Programming in Reading: Ch 15 of Tucker-Noonan Mohammad T. Irfan

Logic Programming u Declarative/rule-based u Objective is specified, not

u Solution is specified using logic u Background

u Propositional and predicate logic

u Example: There is no greatest prime number.

1 12/9/14

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 u Every literate person can read or write

Resolution u Making inference from several clauses u Example () 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) Instantiation u Therefore, talksWith(Alice, Bob)

2 12/9/14

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)

3 12/9/14

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

4 12/9/14

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

5 12/9/14

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). Force factorial(N, Result) :- instantiation 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).

6 12/9/14

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).

7 12/9/14

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).

8