CSCI-2325 Logic Programming in Prolog Reading: Ch 15 of Tucker-Noonan Mohammad T

CSCI-2325 Logic Programming in Prolog Reading: Ch 15 of Tucker-Noonan Mohammad T

12/9/15 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 12/9/15 Background: Proposition u Proposition u A statement that is either true or false u "It's raining." u Operations on propositions: and, or, not, (implies) u Example u It's raining è The sky is cloudy u The sky is cloudy è It's raining Background: Predicate u Predicate u A boolean-valued function u Variables u Quantifiers (for all, for some) u Example predicate: There's no largest prime. u ∀p [ Prime(p) → ∃q ( q>p ∧ Prime(q) ) ] 2 12/9/15 Horn clause u Special type of predicate 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 u Literate(x) è Read(x) V Write(x) 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) Instantiation u Therefore, talksWith(Alice, Bob) 3 12/9/15 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) 4 12/9/15 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 5 12/9/15 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(clive, alice). u listing(speaks). Multiple solutions u talksWith(alice, P). u Multiple solutions for P (press space bar to continue; enter to terminate) u Alternative (without pressing space bar) u talksWith(alice, X), write(X), nl, fail. u write(X) prints the value of X u nl is new line u fail forcibly makes the evaluation false and tricks prolog into trying a different solution! u Need only one solution? cut (!) u talksWith(alice, X), !. 6 12/9/15 Closed-world assumption u Just because Prolog cannot prove something, it will say it’s false u true/fail system, not true/false u Example: speaks(alice, french). u Alice only speaks French, no other language. 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’). 7 12/9/15 Recursion factorial(0, 1). Force factorial(N, Result) :- instantiation N > 0, M is N-1, factorial(M, SubResult), Result is N * SubResult. u factorial(4, X). 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. 8 12/9/15 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). 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). 9 .

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    9 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us