Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dr. Yasser Nada Fall 2010/2011 Lecture 5

Similar presentations


Presentation on theme: "Dr. Yasser Nada Fall 2010/2011 Lecture 5"— Presentation transcript:

1 Dr. Yasser Nada Fall 2010/2011 Lecture 5
Logic Programming PROLOG Dr. Yasser Nada Fall 2010/2011 Lecture 5 Logic Programming

2 Reversibility of Prolog Programs
Consider this program: square(1,1). square(2,4). square(3,9). square(4,16). square(5,25). square(6,36). ?- square(2,X). X=4 ? ? square(X,25). X=5 ? ? PROLOG Logic Programming

3 Reversibility of Prolog Programs
Unlike many other programming languages: No need to write new procedure for finding each argument. Program square/2 is reversible. PROLOG Logic Programming

4 PROLOG Evaluation in Prolog Difference between: Y is 1+2. and Y = 1+2.
In Y is 1+2, the term 1+2 is evaluated to 3 and assigned to Y. In Y=1+2, the term 1+2 is unevaluated and assigned to Y. PROLOG Logic Programming

5 List Construction Example
successor(X,Y) :- Y is X+1. is/2 is a two argument predicate. It is in the form: is(Y,X+1). is/2 is not reversible. ?- successor(3,Y). Y=4 ? ?- successor(X,4). *** Error: uninstantiated variable in arithmetic expression ?- successor(3,5). no ?- PROLOG Logic Programming

6 PROLOG Calling Patterns Mode successor(+,?). Mode is(?,+).
+: input argument. -: output argument. ?: input or output (+ or -). Mode successor(+,?). Mode is(?,+). PROLOG Logic Programming

7 PROLOG List Processing Test for existence. Test All elements.
Return a result – having processed one element. Return a result – having processed all elements. PROLOG Logic Programming

8 PROLOG Test for Existence
Determine one object with a desired property among a list of objects. list_existence_test(Info, [H|T]) :- element_has_property(Info, H). list_existence_test(Info,[H|T]) :- list_existence_test(Info, T). PROLOG Logic Programming

9 Test for Existence Example
Test if a list is a multi-list. nested_list([H|T]) :- iflist(H). nested_list([_H|T]) :- nested_list(T). iflist([]). iflist([H|T]). ?- nested_list([a, [b], c, [d], []]). yes. ?- nested_list([a,b,c]). ?- no PROLOG Logic Programming

10 Test for Existence Example
?- nested_list([a, [b], c, [d], []), write(y), fail. yyyno ?- If we want to display one y, then we need to use a cut (explained later). Mode nested_list(+). PROLOG Logic Programming

11 Test for Existence Example
member(E, [E|T]). member(E,[H|T]) :- member(E,T). Equivalent to: member(E,[H|T]) :- E=H. element_has_property is E=H. Program fits in this pattern when used with mode member(+,+). PROLOG Logic Programming

12 Test for Existence Example
an_integer([H|T]) :- integer(H). an_integer([H|T]) :- an_integer(T). ?- an_integer([a, fred, 5, yes]). Yes ?- an_integer([a,b,c]). No ?- an_integer([]). ?- PROLOG Logic Programming

13 PROLOG Test All Elements
The elements of a list all satisfy some properties. test_all_have_property(Info,[]). test_all_have_property(Info,[Head|Tail]):- element_has_property(Info,Head), test_all_have_property(Info,Tail). PROLOG Logic Programming

14 Test All Elements Example
testing that a list of elements consists of digits only digits([0,1,2,3,4,5,6,7,8,9]). all_digits([]). all_digits([Head|Tail]):- digits(D), member(Head,D), all_digits(Tail). member(E,[E|T]). membet(E,[H|T]) :- member(E,T). PROLOG Logic Programming

15 Test All Elements Example
Ex 6.1 (2b): no_consonant([]). no_consonant([H|T]) :- member(H, [a,e,o,i,u]), no_consonant(T). ?- no_consonant([a,b,e]). no ?- no_consonant([a,e,i]). yes ?- PROLOG Logic Programming

16 Return a Result —Having Processed One Element
Returning an element that satisfies a property. PROLOG return_after_event(Info,[H|T],Result):- property(Info,H), result(Info,H,T,Result). return_after_event(Info,[Head|Tail],Ans):- return_after_event(Info,Tail,Ans). Logic Programming

17 Return a Result —Having Processed One Element
everything_after_a([H|T],R) :- H = a, R=T. everything_after_a(T,R). ?- everything_after_a([d,a,s,a,f],R). R=[s,a,f] ? ; R=[f] ? ; no ?- This can be written as: everything_after_a([a|T],T). PROLOG Logic Programming

18 Return a Result —Having Processed All Elements
Transform each element from a list into a new element in a new list. process_all(Info,[],[]). process_all(Info,[H1|T1],[H2|T2]):- process_one(Info,H1,H2), process_all(Info,T1,T2). PROLOG Logic Programming

19 Return a Result —Having Processed All Elements
triple([],[]). triple([H1|T1],[H2|T2]):- H2 is 3*H1, triple(T1,T2). ?- triple([1,2,3,4],R). R=[3,6,9,12] ? ?- PROLOG Logic Programming

20 Return a Result —Having Processed All Elements
Another way to write predicate triple is: triple([],Y,Y). triple([H1|T1],L,R):- H2 is 3*H1, triple(T1,[H2|L],R). ?- triple([1,2,3,4],[],R). R=[3,6,9,12] ? ?- PROLOG Logic Programming

21 Homework Do exercise 6.1 (1b), (2a), 3,4 page in the book. PROLOG Logic Programming

22 Questions PROLOG Logic Programming 22


Download ppt "Dr. Yasser Nada Fall 2010/2011 Lecture 5"

Similar presentations


Ads by Google