1 / 23

CENG 242 Prolog Recitation

CENG 242 Prolog Recitation. Bahar Pamuk. Outline. Introduction Syntax a Clauses, Programs and Queries List Manipulation Operators Backtracking, Cuts References. Introduction. Declarative programming language

gower
Download Presentation

CENG 242 Prolog Recitation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CENG 242Prolog Recitation Bahar Pamuk

  2. Outline • Introduction • Syntaxa • Clauses, Programs and Queries • List Manipulation • Operators • Backtracking, Cuts • References

  3. Introduction • Declarative programming language • Instead of specifying how to achieev a goal, we specify what the situation (facts, rules) and the goal(query) are and let Prolog to derive the solution.

  4. Introduction • Facts • mother(“A”, “B”). • sister(“B”, “C”). • Rules • is_bigger(X, Y) :- bigger(X, Y). • is_bigger(X, Y) :- bigger(X,Z), is_bigger(Z, Y)

  5. Syntax • Terms • Atoms • a, ab123, z_999, a_b_c, 'an atom sample' • Numbers • Variables • A, Z_1_2, _123, _ • Compound Terms • mother(ali, X), f(g(X, _))

  6. Clauses, Programs & Queries • Clauses • Fact: A certain instance of a relation is true. • Rule: The goal expressed by its head is true, if we show that all of the expressions in the rule's body are true. • Program: Sequence of clauses • Queries: Ask prolog interpreter whether all its predicates are provably true. • is_bigger(elephant, donkey). • is elephant bigger than donkey. • small(X), green(X), slimy(X). • is there any X that small(X), green(X) and slimy(X) are all true.

  7. Built-in Predicates • ?-atom(elephant) Yes ?- compound(f(elephant)). Yes • = (equality) X=Y • consult('myprogram.pl'). • ?-write('hello world'), n1. hello world Yes ?-X = elephant, write(X), n1. elephant X=elephant Yes

  8. Matching Queries • ?- is_bigger(X, dog) = is_bigger(elephant, dog). X=elephant Yes • ?- p(X, 2, 2) = p(1, Y, X). No • ?- p(_, 2, 2) = p(1, Y, _) Y=2 Yes • ?- f(a, g(X, Y)) = f(X, Z), Z= g(W, h(X)). • X=a • Y=h(a) • Z=g(W, h(a)) • W=a • Yes • ?- X=my_functor(Y). • X=my_functor(_G177) • Y=_G177 • Yes

  9. Goal Execution • All man are mortal. Socrates is a man. Hence Socrates is mortal. • mortal(X):-man(X) • man(socrates) • ?- mortal(socrates). • Yes • mortal(socrates) is the initial goal. • mortal(X) is the first possible fact to match. X=socrates • Variable inst. Is extended to the body of the rule. • man(socrates) • man(socrates) is our new goal • It is matched to the fact man(socrates). • Current goal succeeds, so initial goal succeeds.

  10. List Manipulation • [elephant, horse, dog, cat] • [elephant, [], X, parent(X, tom), [a, b, c], f(22)] • [a,b,c] = • .(a, .(b, .(c, []))) • Head: first element • Tail: Rest of the list • ?- [1,2,3,4] = [Head | Tail] • Head = 1 • Tail=[2,3,4] • Yes

  11. List Manipulation • ?-concat_lists([1,2], [3,4,5], X). • X=[1,2,3,4,5] • concat_lists([],List, List). • concat_lists([Elem | List1], List2, [Elem | List3]) :- concat_lists(List1, List2, List3). • ?- concat_lists(X, Y, [1,2,3]). • X=[] • Y=[1,2,3]; • X=[1] • Y=[2,3]; • X=[1,2] • Y=[3]; • X=[1,2,3] • Y=[]

  12. Built-in Predicates • length([1,2,3], X). • X=3 • length(List, 3). • List=[_G123, _G234, _G456] • member(Elem, List) • append(List1, List2, List3) • last(Elem, List) • reverse(List1, List2) % reverse order of List1 • select(List1, Elem, List2) % select elements of List1 after Elem

  13. Arithmetic Expressions • 2+3 = 5 % they do not match • X is 2+3, X=5. • X=5 • 5 is 2+3. • Yes • 2+3 is 5. % is evaluates rhs and matches it to left. • No • max, min, abs, //, **... for rhs of is operator

  14. Relations • Compare two evaluated arithmetic expressions. • Whenever an arithmetic expression is used, the arguments are evaluated (no need to use is operator.) • <, >, <=, >=, =:=, =\= • =:= arithmetically equal, • = pattern matching • 2 ** 3 =:= 3 + 5 Yes • 2 ** 3 =8 3 + 5 No

  15. Operators • Every operator is associated with an integer denoting its precedence. The lower the precedence the stronger the operator is binding. • The precedence of a term is defined as 0 unless its principal functor is an operator. • 3+5 -> 500 • 3 * 3 + 5 * 2 -> 400 • elephant -> 0 • (3+5) -> 0

  16. Backtracking, Cuts & Negation • During proof search, Prolog keeps track of choice points; situations where there is more than 1 match. • When the chosen path is a failure/the user asks for alternative solutions, the systems jumps back to the last choice point and try the next alternative. • permutation ([], []). • permutation ( List, [Elem | Perm]) :- • select(List, Elem, Rest), • permutation (Rest, Perm).

  17. Backtracking • ?- permutation( [1,2,3], X). • X = [1,2,3]; • X = [1,3,2]; • X = [2,1,3]; • X = [2,3,1]; • X = [3,1,2]; • X = [3,2,1]; • No

  18. Backtracking • remove_dup ([], []). • remove_dup ([Head | Tail], Result) :- member(Head, Tail), remove_dup(Tail, Result). • remove_dup ([Head | Tail], [Head| Result]) :- remove_dup(Tail, Result). • ?- remove_dup ([a, b, b, c, a], X). • X = [b,c,a]; • X = [b, b,c,a]; • X = [a,b,c,a]; • X = [a,b,b,c,a]; • No.

  19. Derivation Tree remove_dup ([a, b, b, c, a], X) member(a,[ b,b,c,a]) remove_dup([b,b,c,a], Result) member(b, [b,c,a]) remove_dup([b,c,a], Result) member(b, [c,a]) remove_dup([b|c,a], [b|Result]):- remove_dup([c,a], Result) Result=[b,c,a] member(c, [a]) remove_dup([c|a], [c|Result]):-remove_dup([a], Result) Result=[c,a] Result=[a] member(a, []) remove_dup([a|], [a|Result]):-remove_dup([], Result) Result=[]

  20. Derivation Tree remove_dup ([a, b, b, c, a], X) member(a,[ b,b,c,a]) remove_dup([b,b,c,a], Result) Result=[b,b,c,a] member(b, [b,c,a]) remove_dup([b,c,a], Result) remove_dup([b|b,c,a], [b|Result]):- remove_dup([b,c,a], Result) Result=[b,c,a] member(b, [c,a]) remove_dup([b|c,a], [b|Result]):- remove_dup([c,a], Result) member(c, [a]) remove_dup([c|a], [c|Result]):-remove_dup([a], Result) member(a, []) remove_dup([a|], [a|Result]):-remove_dup([], Result)

  21. Cuts • It is possible to cut out backtracking points and preventing unwanted alternative solutions. • ! is a predicate and can be placed anywhere inside rule body. Execution of ! will always succeed • remove_dup ([Head|Tail], Result) :- member(Head, Tail), !,remove_dup(Tail, Result). • Without cut, alternative solution search will continue. But when cut is passed this isn't possible anymore. • remove_dup([a,b,b,c,a], X). • X = [b,c,a]; • No.

  22. Negation • In order to give a positive answer to a query Prolog has to construct a proof to show that the set of facts and rules implies that query. • Yes means the query is provably true. • No doesn’t mean that the query is necessarily false, just not provably true. • Closed world assumption: Negating everything that is not explicitly in the program.

  23. References • Endriss, U, Lecture Notes, An Introduction to Prolog Programming, King's College, London

More Related