Lập trình hướng đối tượng - Chapter 15: Logic programming
We can identify the variables B, C, F, M, and S with the five persons,
and the structure floors(Floors) as a function whose argument is the list to be solved.
Here’s the first constraint:
member(floor(baker, B), Floors), B\=5
which says that Baker doesn't live on the 5th floor.
The other four constraints are coded similarly, leading to the following program:
19 trang |
Chia sẻ: huyhoang44 | Lượt xem: 607 | Lượt tải: 0
Bạn đang xem nội dung tài liệu Lập trình hướng đối tượng - Chapter 15: Logic programming, để tải tài liệu về máy bạn click vào nút DOWNLOAD ở trên
Programming Languages2nd editionTucker and NoonanChapter 15Logic ProgrammingQ: How many legs does a dog have if you call its tail a leg?A: Four. Calling a tail a leg doesn’t make it one. Abraham LincolnContents15.1 Logic and Horn Clauses15.2 Logic Programming in Prolog15.2.1 Prolog Program Elements15.2.2 Practical Aspects of Prolog15.3 Prolog Examples15.3.1 Symbolic Differentiation15.3.2 Solving Word Puzzles15.3.3 Natural Language Processing15.3.4 Semantics of Clite15.3 5 Eight Queens Problem15.2.2 Practical Aspects of PrologTracingThe CutNegationThe is, not, and Other OperatorsThe Assert FunctionTracingTo see the dynamics of a function call, the trace function can be used. E.g., if we want to trace a call to the following function:factorial(0, 1).factorial(N, Result) :- N > 0, M is N - 1, factorial(M, SubRes), Result is N * SubRes.we can activate trace and then call the function:?- trace(factorial/2).?- factorial(4, X). Note: the argument to trace must include the function’s arity.Tracing Output?- factorial(4, X).Call: ( 7) factorial(4, _G173)Call: ( 8) factorial(3, _L131)Call: ( 9) factorial(2, _L144)Call: ( 10) factorial(1, _L157)Call: ( 11) factorial(0, _L170)Exit: ( 11) factorial(0, 1)Exit: ( 10) factorial(1, 1)Exit: ( 9) factorial(2, 2)Exit: ( 8) factorial(3, 6)Exit: ( 7) factorial(4, 24)X = 24These are temporary variablesThese are levels in the search treeThe CutThe cut is an operator (!) inserted on the right-hand side of a rule. semantics: the cut forces those subgoals not to be retried if the right-hand side succeeds once.E.g (bubble sort):bsort(L, S) :- append(U, [A, B | V], L), B 0, M is N - 1, factorial(M, SubRes), Result is N * SubRes.Here, the variables M and Result are instantiated This is like an assignment to a local variable in C-like languages. Other OperatorsProlog provides the operators + - * / ^ = >= = C, member(floor(smith, S), Floors), not(adjacent(S, F)), not(adjacent(F, C)), print_floors(Floors).Auxiliary functionsFloor adjacency: adjacent(X, Y) :- X =:= Y+1. adjacent(X, Y) :- X =:= Y-1.Note: =:= tests for numerical equality. Displaying the results: print_floors([A | B]) :- write(A), nl, print_floors(B). print_floors([]).Note: write is a Prolog function and nl stands for “new line.”Solving the puzzle is done with the query: ?- building(X).which finds an instantiation for X that satisfies all the constraints.
Các file đính kèm theo tài liệu này:
- ch15b_446.ppt