Presentation is loading. Please wait.

Presentation is loading. Please wait.

Prolog 3 Tests and Backtracking 1. Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t.

Similar presentations


Presentation on theme: "Prolog 3 Tests and Backtracking 1. Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t."— Presentation transcript:

1 Prolog 3 Tests and Backtracking 1

2 Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t need to be written Comparisons:, = =, =:= (equals), =\= (not equals) = Infix operators: go between two terms. =</2 is used 5 =< 7. (infix) =<(5,7). (prefix)  all infix operators can also be prefixed Equality is different from unification =/2 checks if two terms unify =:=/2 compares the arithmetic value of two expressions ?- X=Y. ?- X=:=Y. ?- X=Y. ?- X=:=Y. ?-X=4,Y=3, X+2 =:= Y+3. yes ?-X=4,Y=3, X+2 =:= Y+3. yes 2

3 Examples of operator properties PositionOperator SyntaxNormal Syntax Prefix:-2-(2) Infix:5+17+(17,5) Postfix:N!!(N) Associativity: left, right, none. X+Y+Z is parsed as (X+Y)+Z X+Y+Z is parsed as (X+Y)+Z because addition is left-associative. Precedence: an integer. X+Y*Z is parsed as X+(Y*Z) because multiplication has higher precedence. These are all the same as the normal rules of arithmetic.

4 Arithmetic Operators (2) Arithmetic Operators : +, -, *, / = Infix operators but can also be used as prefix.  Need to use is/2 to access result of the arithmetic expression otherwise it is treated as a term: |?- X = 5+4.|?- X is 5+4. X = 5+4 ? X = 9 ? yes yes (Can X unify with 5+4?) (What is the result of 5+4?) Mathematical precedence is preserved : /, *, before +,- Can make compound sums using round brackets  Impose new precedence  Inner-most brackets first 4 | ?- X is 5+4*2. X = 13 ? yes | ?- X is (5+4)*2. X = 18 ? yes

5 Tests within clauses These operators can be used within the body of a clause:  To manipulate values, sum(X,Y,Sum):- Sum is X+Y.  To distinguish between clauses of a predicate definition bigger(N,M):- N < M, write(‘The bigger number is ‘), write(M). bigger(N,M):- N > M, write(‘The bigger number is ‘), write(N). bigger(N,M):- N =:= M, write(‘Numbers are the same‘). 5

6 Backtracking |?- bigger(5,4). bigger(N,M):- bigger(N,M):- N < M, write(‘The bigger number is ‘),write(M).bigger(N,M):- N > M, write(‘The bigger number is ‘),write(N).bigger(N,M):- N =:= M, write(‘Numbers are the same‘). 6

7 7 Backtracking |?- bigger(5,4). bigger(5,4):- 5 < 4,  fails write(‘The bigger number is ‘),write(M). bigger(N,M):- N > M, write(‘The bigger number is ‘),write(N). bigger(N,M):- N =:= M, write(‘Numbers are the same‘). Backtrack

8 8 |?- bigger(5,4). bigger(N,M):- N < M, write(‘The bigger number is ‘), write(M). bigger(5,4):- 5 > 4, write(‘The bigger number is ‘), write(N). bigger(N,M):- N =:= M, write(‘Numbers are the same‘). Backtracking

9 9 |?- bigger(5,4). bigger(N,M):- N < M, write(‘The bigger number is ‘), write(M). bigger(5,4):- 5 > 4,  succeeds, go on with body. write(‘The bigger number is ‘), write(5). The bigger number is 5 yes |?- Reaches full-stop = clause succeeds Backtracking

10 10 |?- bigger(5,5).  If our query only matches the final clause bigger(N,M):- N < M, write(‘The bigger number is ‘), write(M). bigger(N,M):- N > M, write(‘The bigger number is ‘), write(N). bigger(5,5):- 5 =:= 5, write(‘Numbers are the same‘). Numbers are the same yes  Is already known as the first two clauses failed. Backtracking

11 Reporting Answers |?- bigger(5,4).  Question is asked The bigger number is 5  Answer is written to terminal yes  Succeeds but answer is lost This is fine for checking what the code is doing but not for using the proof. |?- bigger(6,4), bigger(Answer,5). Instantiation error! To report back answers we need to  put an uninstantiated variable in the query,  instantiate the answer to that variable when the query succeeds,  pass the variable all the way back to the query. 11

12 Passing Back Answers To report back answers we need to To report back answers we need to 1. put an uninstantiated variable in the query, | ?- bigger(6,4,Answer),bigger(Answer,5,New_answ er). 2. instantiate the answer to that variable when the query succeeds, 3. pass the variable all the way back to the query. 12 1 3 2 bigger(X,Y,Answer):- X>Y. bigger(X,Y,Answer):- X=<Y., Answer = X., Answer = Y.

13 13 Head Unification To report back answers we need to 1.put an uninstantiated variable in the query, ?- bigger(6,4,Answer),bigger(Answer,5,New_answer). Or, do steps 2 and 3 in one step by naming the variable in the head of the clause the same as the correct answer. = head unification 1 2 bigger(X,Y,X):- X>Y. bigger(X,Y,Y):- X=<Y. 3

14 Satisfying Subgoals Most rules contain calls to other predicates in their body. These are known as Subgoals. These subgoals can match:  facts,  other rules, or  the same rule = a recursive call 14 1) drinks(alan,orange). 2) likes(alan,coffee). 3) likes(heather,coffee). 4) likes(Person,Drink):- drinks(Person,Drink).  a different subgoal 5) likes(Person,Somebody):- likes(Person,Drink),  recursive subgoals likes(Somebody,Drink). 

15 Unification Two terms unify if substitutions can be made for any variables in the terms so that the terms are made identical. If no such substitution exists, the terms do not unify. The Unification Algorithm proceeds by recursive descent of the two terms.  Constants unify if they are identical  Variables unify with any term, including other variables  Compound terms unify if their functors and components unify.

16 Examples The terms f(X, a(b,c)) and f(d, a(Z, c)) unify. Zc a d f bc a X f The terms are made equal if d is substituted for X, and b is substituted for Z. We also say X is instantiated to d and Z is instantiated to b, or X/d, Z/b.

17 Examples The terms f(X, a(b,c)) and f(Z, a(Z, c)) unify. Zc a Z f bc a X f Note that Z co-refers within the term. Here, X/b, Z/b.

18 Examples The terms f(c, a(b,c)) and f(Z, a(Z, c)) do not unify. Zc a Z f bc a c f No matter how hard you try, these two terms cannot be made identical by substituting terms for variables.

19 Exercise Do terms g(Z, f(A, 17, B), A+B, 17) and g(C, f(D, D, E), C, E) unify? AB +f g Z 17 AB Cf g C E DED

20 Exercise First write in the co-referring variables. AB +f g Z 17 AB Cf g C E DED

21 Exercise AB +f g Z AB Cf g C E DED Z/C, C/Z Now proceed by recursive descent We go top-down, left- to-right, but the order does not matter as long as it is systematic and complete.

22 Exercise AB +f g Z 17 AB Cf g C E DED Z/C, C/Z, A/D, D/A

23 Exercise AB +f g Z 17 AB Cf g C E DED Z/C, C/Z, A/17, D/17

24 Exercise AB +f g Z 17 AB Cf g C E DED Z/C, C/Z, A/17, D/17, B/E, E/B

25 Exercise AB +f g Z 17 AB Cf g C E DED Z/17+B, C/17+B, A/17, D/17, B/E, E/B

26 Exercise AB +f g Z 17 AB Cf g C E DED Z/17+17, C/17+17, A/17, D/17, B/17, E/17

27 Exercise – Alternative Method Z/C AB +f g Z 17 AB Cf g C E DED

28 Exercise – Alternative Method Z/C AB +f g C 17 AB Cf g C E DED

29 Exercise – Alternative Method A/D, Z/C AB +f g C 17 AB Cf g C E DED

30 Exercise – Alternative Method D/17, A/D, Z/C DB +f g C 17 DB Cf g C E DED

31 Exercise – Alternative Method D/17, A/17, Z/C 17B +f g C B Cf g C E E

32 Exercise – Alternative Method B/E, D/17, A/17, Z/C 17B +f g C B Cf g C E E

33 Exercise – Alternative Method B/E, D/17, A/17, Z/C 17E +f g C E Cf g C E E

34 Exercise – Alternative Method C/17+E, B/E, D/17, A/17, Z/C 17E +f g C E Cf g C E E

35 Exercise – Alternative Method C/17+E, B/E, D/17, A/17, Z/17+E 17E +f g + E +f g + E E E E E

36 Exercise – Alternative Method E/17, C/17+E, B/E, D/17, A/17, Z/C 17E +f g + E +f g + E E E E E

37 Exercise – Alternative Method E/17, C/17+17, B/17, D/17, A/17, Z/C 17 +f g + +f g +

38 Structures and Lists 38

39 39 The central ideas of Prolog SUCCESS/FAILURE –any computation can “succeed'' or “fail'', and this is used as a ‘test‘ mechanism. MATCHING –any two data items can be compared for similarity, and values can be bound to variables in order to allow a match to succeed. SEARCHING –the whole activity of the Prolog system is to search through various options to find a combination that succeeds. Main search tools are backtracking and recursion BACKTRACKING –when the system fails during its search, it returns to previous choices to see if making a different choice would allow success.

40 Prolog Data Objects (Terms) 40 Simple objects Structured Objects Constants IntegersAtoms Symbols Strings Signs VariablesStructuresLists a bob l8r_2day ‘a’ ‘Bob’ ‘L8r 2day’ ==> … -6 987 X A_var _Var date(4,10,04) person(bob,48) [] [a,b,g] [[a],[b]] [bit(a,d),a,’Bob’]

41 Structures To create a single data element from a collection of related terms we use a structure. A structure is constructed from a functor (a constant symbol) and one of more components. somerelationship (a,b,c,[1,2,3]) The components can be of any type: atoms, integers, variables, or structures. As functors are treated as data objects just like constants they can be unified with variables 41 functor

42 Structure unification 2 structures will unify if  the functors are the same,  they have the same number of components,  and all the components unify. | ?- person(Nm,london,Age) = person(bob,london,48). Nm = bob, Age = 48? yes | ?- person(Someone,_,45) = person(harry,dundee,45). Someone = harry ? yes (A plain underscore ‘_’ is not bound to any value. By using it you are telling Prolog to ignore this argument and not report it.) 42

43 Structure unification (2) A structure may also have another structure as a component. |?-addr(flat(4),street(‘Home Str.’),postcode(eh8_9lw)) = addr(flat(Z),Yy,postcode(Xxx)). Z = 4, Yy = street(‘Home Str.’), Xxx = eh8_9lw ? yes Unification of nested structures works recursively:  first it unifies the entire structure,  then it tries to unify the nested structures. 43 Remember to close brackets! Reported variables are ordered according to number of characters in the variable name.

44 Structures = facts? The syntax of structures and facts is identical but:  Structures are not facts as they are not stored in the database as being true (followed by a period ‘.’);  Structures are generally just used to group data;  Functors do not have to match predicate names. However predicates can be stored as structures command(X):- X. | ?- X = write(‘Passing a command’), command(X). Passing a command X = write('Passing a command') ? yes 44 By instantiating a variable with a structure which is also a predicate you can pass commands.

45 Lists A collection of ordered data. Has zero or more elements enclosed by square brackets (‘[ ]’) and separated by commas (‘,’). [a]  a list with one element []  an empty list 1 2 3 1 2 3 1 2 1 2 [34,tom,[2,3]]  a list with 3 elements where the 3 rd element is a list of 2 elements. Like any object, a list can be unified with a variable |?- [Any, list, ‘of elements’] = X. X = [Any, list, ‘of elements’]? yes 45

46 List Unification Two lists unify if they are the same length and all their elements unify. |?-[a,B,c,D]=[A,b,C,d]. |?-[(a+X),(Y+b)]=[(W+c),(d+b)]. A = a, W = a, B = b, X = c, C = c, Y = d? D = d ? yes yes |?- [[X,a]]=[b,Y]. |?-[[a],[B,c],[]]=[X,[b,c],Y]. no B = b, X = [a], X = [a], Y = [] ? Y = [] ? yes yes 46 Length 1 Length 2

47 Definition of a List Lists are recursively defined structures. “An empty list, [], is a list. A structure of the form [X, …] is a list if X is a term and […] is a list, possibly empty.” This recursiveness is made explicit by the bar notation  [Head|Tail] (‘|’ = bottom left PC keyboard character) Head must unify with a single term. Tail unifies with a list of any length, including an empty list, [].  the bar notation turns everything after the Head into a list and unifies it with Tail. 47

48 Head and Tail |?-[a,b,c,d]=[Head|Tail]. |?-[a,b,c,d]=[X|[Y|Z]]. Head = a, X = a, Tail = [b,c,d]? Y = b, yes Z = [c,d]; yes yes |?-[a] = [H|T]. |?-[a,b,c]=[W|[X|[Y|Z]]]. H = a, W = a, T = []; X = b, yes Y = c, Z = []? yes Z = []? yes |?-[] = [H|T]. |?-[a|[b|[c|[]]]]= List. no List = [a,b,c]? yes yes 48

49 Summary Prolog’s proof strategy can be represented using AND/OR trees. Tree representations allow us trace Prolog’s search for multiple matches to a query. They also highlight the strengths and weaknesses of recursion (e.g. economical code vs. infinite looping). Recursive data structures can be represented as structures or lists. Structures can be unified with variables then used as commands. Lists can store ordered data and allow its sequential processing through recursion. 49

50 50 Thank You!


Download ppt "Prolog 3 Tests and Backtracking 1. Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t."

Similar presentations


Ads by Google