Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

Like this presentation? Why not share!

2,257 views

Published on

PROLOG: Recursion And Lists In Prolog

Published in:
Technology

No Downloads

Total views

2,257

On SlideShare

0

From Embeds

0

Number of Embeds

55

Shares

0

Downloads

0

Comments

0

Likes

2

No embeds

No notes for slide

- 1. Recursion and Lists in Prolog<br />
- 2. OVERVIEW<br />Recursive definitions<br />Clause ordering, goal ordering, and termination.<br />Lists<br />Members<br />Recursing Down Lists<br />
- 3. Recursive definitions<br />A predicate is recursively defined if one or more rules in its definition refers to itself.<br />Ex: is_digesting(X,Y) :- just_ate(X,Y).<br />is_digesting(X,Y) :-<br />just_ate(X,Z),<br />is_digesting(Z,Y).<br />just_ate(mosquito,blood(john)).<br />just_ate(frog,mosquito).<br />just_ate(stork,frog).<br /> It's just a knowledge base containing two facts and<br />two rules. <br />But the definition of the is_digesting/2 predicate is recursive.<br />
- 4. Another way of writing numerals, which is sometimes used in mathematical logic is to makes use of just four symbols: 0, succ, and the left and right brackets. <br />This style of numeral is defined by the following inductive definition:<br />1. 0 is a numeral.<br />2. If X is a numeral, then so is succ(X).<br />This definition in prolog program is written as:<br />numeral(0).<br />numeral(succ(X)) :- numeral(X).<br />So, on posing the query numeral(succ(succ(succ(0)))).<br /><ul><li>We get Yes.</li></li></ul><li>Now on running the following query<br />numeral(X).<br />we can have the following dialogue with Prolog:<br />X = 0 ;<br />X = succ(0) ;<br />X = succ(succ(0)) ;<br />X = succ(succ(succ(0))) ;<br />X = succ(succ(succ(succ(0)))) ;<br />X = succ(succ(succ(succ(succ(0))))) ;<br />X = succ(succ(succ(succ(succ(succ(0)))))) ;<br />X = succ(succ(succ(succ(succ(succ(succ(0))))))) ;<br />X = succ(succ(succ(succ(succ(succ(succ(succ(0)))))))) ;<br />X = succ(succ(succ(succ(succ(succ(succ(succ(succ(0))))))))) ;<br />X = succ(succ(succ(succ(succ(succ(succ(succ(succ(succ(0))))))))))<br />Yes<br />It's backtracking through the recursive definition, and actually building numerals using matching.<br />
- 5. Clause ordering, goal ordering, and termination<br />Consider the following ex:<br />child(martha,charlotte).<br />child(charlotte,caroline).<br />child(caroline,laura).<br />child(laura,rose).<br />descend(X,Y) :- child(X,Y).<br />descend(X,Y) :- child(X,Z),<br />descend(Z,Y).<br />We'll make two changes to it, and call the result descend2.pl:<br />child(martha,charlotte).<br />child(charlotte,caroline).<br />child(caroline,laura).<br />child(laura,rose).<br />descend(X,Y) :- descend(Z,Y),<br />child(X,Z).<br />descend(X,Y) :- child(X,Y).<br />
- 6. we have merely reversed the order of the two rules, and reversed the order of the two goals in the recursive clause.<br /> So, viewed as a purely logical definition, nothing has changed.<br />But the procedural meaning has changed dramatically. For example, if you pose the query<br />descend(martha,rose).<br />you will get an error message (`out of local stack', or something similar).<br />Because descend1.pl and descend2.pl are Prolog knowledge bases with the same declarative meaning but different procedural meanings: from a purely logical perspective they are identical, but they behave very differently.<br />
- 7. The declarative and procedural meanings of a Prolog program can differ, when writing Prolog programs you need to bear both aspects in mind.<br />When you need to think about how Prolog will actually evaluate queries. The following questions must be considered:<br />Are the rule orderings sensible? How will the program actually run?<br />
- 8. Lists<br />lists, an important recursive data structure widely used in computational linguistics.<br />It is a finite sequence of elements. Here are some examples of lists in Prolog:<br />[mia, vincent, jules, yolanda]<br />[mia, robber(honey_bunny), X, 2, mia]<br />[]<br />[mia, [vincent, jules], [butch, girlfriend(butch)]]<br />[[], dead(zed), [2, [b, chopper]], [], Z, [2, [b, chopper]]]<br />
- 9. Lists<br />We can specify lists in Prolog by enclosing the elements of the list in square brackets(that is, the symbols [ and ]). The elements are separated by commas.<br />All sorts of Prolog objects can be elements of a list and the same item may occur more than once in the same list.<br />The empty list(as its name suggests) is the list that contains no elements.<br />lists can contain other lists as elements.<br />Any non-empty list can be thought of as consisting of two<br />parts: the head and the tail. <br />The head is simply the first item in the list; the tail is everything else.<br />
- 10. Members<br />Member is a fundamental Prolog tool for manipulating lists, and to introduce the idea of recursing down lists.<br />consider a program that, when given as inputs an arbitrary object X and a list L, tells us whether or not X belongs to L. The program that does this is usually called member. Here it is:<br />member(X,[X|T]).<br />member(X,[H|T]) :- member(X,T).<br />one fact (namely member(X,[X|T])) and one rule (namely member(X,[H|T]) :- member(X,T)). But note that the rule is recursive (after all, the functor member occurs in both the rule's head and tail)<br />
- 11. Members<br />Suppose we posed the following query:<br />? -member(yolanda,[yolanda,trudy,vincent,jules]).<br />Prolog will immediately answer `Yes'. Because it can unify yolanda with both occurrences of X in the first clause (the fact) in the definition of member/2, so it succeeds immediately.<br />
- 12. Recursing Down Lists<br />Member works by recursively working down a list, doing something to the head, and then recursively doing the same thing to the tail. <br />Recursing down a list (or indeed, several lists) in this way is extremely common in Prolog.<br />When working with lists, we often want to compare one list with another, or to copy bits of one list into another, or to translate the contents of one list into another, or something similar.<br />Ex: Let's suppose we need a predicate a2b/2 that takes two lists as arguments, and succeeds if the first argument is a list of as, and the second argument is a list of bs of exactly the same length.<br />If we pose the following query<br />a2b([a,a,a,a],[b,b,b,b]).<br />we want Prolog to say `yes'. <br />
- 13. Recursing Down Lists<br />If we pose the query<br />a2b([a,a,a,a],[b,b,b]) or the query<br />a2b([a,c,a,a],[b,b,5,4]).<br />we want Prolog to say `no'.<br />For longer lists, think recursively. So when should a2b/2 decide that two non-empty lists are a list of as and a list of bs of exactly the same length?<br />
- 14. Simple: when the head of the first list is an a, and the head of the second list is a b, and a2b/2 decides that the two tails are lists of as and bs of exactly the same length! <br />This immediately gives us the following rule:<br />a2b([a|Ta],[b|Tb]) :- a2b(Ta,Tb).<br />The a2b/2 predicate should succeed if its first argument is a list with head a, its second argument is a list with head b, and a2b/2 succeeds on the two tails.<br />Now, this definition make good sense declaratively.<br />
- 15. Visit more self help tutorials<br />Pick a tutorial of your choice and browse through it at your own pace.<br />The tutorials section is free, self-guiding and will not involve any additional support.<br />Visit us at www.dataminingtools.net<br />

No public clipboards found for this slide

×
### Save the most important slides with Clipping

Clipping is a handy way to collect and organize the most important slides from a presentation. You can keep your great finds in clipboards organized around topics.

Be the first to comment