• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
PROLOG: Recursion And Lists In Prolog

PROLOG: Recursion And Lists In Prolog



PROLOG: Recursion And Lists In Prolog

PROLOG: Recursion And Lists In Prolog



Total Views
Views on SlideShare
Embed Views



4 Embeds 54

http://www.dataminingtools.net 41
http://dataminingtools.net 10
http://www.slideshare.net 2
http://translate.googleusercontent.com 1



Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

    PROLOG: Recursion And Lists In Prolog PROLOG: Recursion And Lists In Prolog Presentation Transcript

    • Recursion and Lists in Prolog
      Recursive definitions
      Clause ordering, goal ordering, and termination.
      Recursing Down Lists
    • Recursive definitions
      A predicate is recursively defined if one or more rules in its definition refers to itself.
      Ex: is_digesting(X,Y) :- just_ate(X,Y).
      is_digesting(X,Y) :-
       It's just a knowledge base containing two facts and
      two rules.
      But the definition of the is_digesting/2 predicate is recursive.
    • 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.
      This style of numeral is defined by the following inductive definition:
      1. 0 is a numeral.
      2. If X is a numeral, then so is succ(X).
      This definition in prolog program is written as:
      numeral(succ(X)) :- numeral(X).
      So, on posing the query numeral(succ(succ(succ(0)))).
      • We get Yes.
    • Now on running the following query
      we can have the following dialogue with Prolog:
      X = 0 ;
      X = succ(0) ;
      X = succ(succ(0)) ;
      X = succ(succ(succ(0))) ;
      X = succ(succ(succ(succ(0)))) ;
      X = succ(succ(succ(succ(succ(0))))) ;
      X = succ(succ(succ(succ(succ(succ(0)))))) ;
      X = succ(succ(succ(succ(succ(succ(succ(0))))))) ;
      X = succ(succ(succ(succ(succ(succ(succ(succ(0)))))))) ;
      X = succ(succ(succ(succ(succ(succ(succ(succ(succ(0))))))))) ;
      X = succ(succ(succ(succ(succ(succ(succ(succ(succ(succ(0))))))))))
      It's backtracking through the recursive definition, and actually building numerals using matching.
    • Clause ordering, goal ordering, and termination
      Consider the following ex:
      descend(X,Y) :- child(X,Y).
      descend(X,Y) :- child(X,Z),
      We'll make two changes to it, and call the result descend2.pl:
      descend(X,Y) :- descend(Z,Y),
      descend(X,Y) :- child(X,Y).
    • we have merely reversed the order of the two rules, and reversed the order of the two goals in the recursive clause.
      So, viewed as a purely logical definition, nothing has changed.
      But the procedural meaning has changed dramatically. For example, if you pose the query
      you will get an error message (`out of local stack', or something similar).
      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.
    • The declarative and procedural meanings of a Prolog program can differ, when writing Prolog programs you need to bear both aspects in mind.
      When you need to think about how Prolog will actually evaluate queries. The following questions must be considered:
      Are the rule orderings sensible? How will the program actually run?
    • Lists
      lists, an important recursive data structure widely used in computational linguistics.
      It is a finite sequence of elements. Here are some examples of lists in Prolog:
      [mia, vincent, jules, yolanda]
      [mia, robber(honey_bunny), X, 2, mia]
      [mia, [vincent, jules], [butch, girlfriend(butch)]]
      [[], dead(zed), [2, [b, chopper]], [], Z, [2, [b, chopper]]]
    • Lists
      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.
      All sorts of Prolog objects can be elements of a list and the same item may occur more than once in the same list.
      The empty list(as its name suggests) is the list that contains no elements.
      lists can contain other lists as elements.
      Any non-empty list can be thought of as consisting of two
      parts: the head and the tail.
      The head is simply the first item in the list; the tail is everything else.
    • Members
      Member is a fundamental Prolog tool for manipulating lists, and to introduce the idea of recursing down lists.
      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:
      member(X,[H|T]) :- member(X,T).
      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)
    • Members
      Suppose we posed the following query:
      ? -member(yolanda,[yolanda,trudy,vincent,jules]).
      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.
    • Recursing Down Lists
      Member works by recursively working down a list, doing something to the head, and then recursively doing the same thing to the tail.
      Recursing down a list (or indeed, several lists) in this way is extremely common in Prolog.
      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.
      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.
      If we pose the following query
      we want Prolog to say `yes'.
    • Recursing Down Lists
      If we pose the query
      a2b([a,a,a,a],[b,b,b]) or the query
      we want Prolog to say `no'.
      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?
    • 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!
      This immediately gives us the following rule:
      a2b([a|Ta],[b|Tb]) :- a2b(Ta,Tb).
      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.
      Now, this definition make good sense declaratively.
    • Visit more self help tutorials
      Pick a tutorial of your choice and browse through it at your own pace.
      The tutorials section is free, self-guiding and will not involve any additional support.
      Visit us at www.dataminingtools.net