csci 2325 logic programming in prolog
play

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


  1. 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 Background u Propositional and predicate logic u Example: There is no greatest prime number. 1 ¡

  2. 12/9/14 ¡ Horn clause u Special type of predicate logic u p 1 Λ p 2 Λ ... Λ p n => h u To show h, first show p1, then p2, ..., pn u Notation: h ç p 1 , p 2 , ..., p n u Equivalent to: (p 1 Λ p 2 Λ ... Λ p n ) V h u Equivalent to: p 1 V ... V p n 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 , L), X ≠ Y u talksWith(X, Y) ç speaks(X, L), speaks(Y speaks(Alice, French) speaks(Bob, French) Instantiation u Therefore, talksWith(Alice, Bob) 2 ¡

  3. 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 ¡

  4. 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 ¡

  5. 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 ¡

  6. 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 ¡

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

  8. 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 ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend