Prolog Recursive Rules

 19 July 16:48   This area is about using rules recursively.

    Consider the afterward program.

    


     parent(david, john).

     parent(jim, david).

     parent(steve, jim).

     parent(nathan, steve).

     grandparent(A, B) :- parent(A, X), parent(X, B).

    


    It shows a macho band in a ancestors tree, and defines with a aphorism if anyone is someones grandparent (if hes a ancestor of that bodies parent). What if we capital to ascertain if anyone is an antecedent of anyone else? That is, a parent, grandparent (a ancestor of a parent) grandparent, abundant grandparent (a ancestor of a ancestor of a parent) and so on. If we would address rules for all these situations we could ascertain ancestor(A,B) for this accurate bloodline:

    


     ancestor(A,B) :- parent(A, B).

     ancestor(A,B) :- parent(A, X), parent(X, B).

     ancestor(A,B) :- parent(A, X), parent(X, Y), parent(Y, B).

     ancestor(A,B) :- parent(A, X), parent(X, Y), parent(Y, Z), parent(Z,B).

    


    Clearly this is not the way to go. Its not elegant, a lot of plan and alotof importantly, it still doesnt ascertain antecedent properly. This may plan for a bloodline with four generations, but if we were to add a fifth ancestor to the program, wed charge a new band for antecedent as well. What we charge is a analogue for antecedent that works for any band of parents, no amount how long.

    To accomplish this we charge to anticipate about the analogue of an ancestor. In the first place, being P is being Cs ancestor, if P is Cs parent. Furthermore, we can say that being A is being Cs antecedent if being A is a ancestor of an antecedent of C. This doesnt complete actual useful, back we use the chat antecedent in its own definition, but acclimated calm with the first statement, it becomes complete definition.

    For instance, if we were to ask ourselves if Steve is an antecedent of John, we would attending at the defintions. Steve is absolutely no ancestor of John, so that doesnt apply. Is Steve the ancestor of an antecedent of John? Steve is a ancestor of Jim, so the catechism becomes, is Jim an antecedent of John? Jim is a ancestor of David, so is David an antecedent of John? Actuality we can use the first definition, because David is a ancestor of John.

    In prolog, the analogue looks like this:

    


     ancestor(A, B) :- parent(A, B).

     ancestor(A, B) :- parent(A, X), ancestor(X, B).

    


    The additional aphorism is recursive; it uses antecedent to ascertain ancestor. The first aphorism is alleged the stop assert as it stops the assert calling itself.

    When faced with the concern ?- ancestor(steve,john). prolog will first try band one of the definition, but wont be able to arrange parent(steve, john) with any band in the database. It will then try the additional band and appear up with the sub-goals parent(steve, X) and ancestor(X, B). It can arrange parent(steve, X) with parent(steve, jim), so prolog is larboard with the sub-goal ancestor(jim, B). It will abide this until its larboard with the subgoal ancestor(david, john) which, using the first band of the definition, is true, because parent(david, john) is true.

    Prolog is actual adequate with using recursive rules. It can actual calmly acquisition all ancestors of John:

    


     ?- ancestor(X, john).

     X = david ;

     X = jim ;

     X = steve ;

     X = nathan ;

     No

    


    Or all humans that Nathan is an antecedent of:

    


     ?- ancestor(nathan, X).

     X = steve ;

     X = jim ;

     X = david ;

     X = john ;

     No

    


    Using recursive rules is not after its dangers, it can advance to all kinds of loops, which will usually couldcause assemblage overflows, acceptation prolog has run out of memory. The afterward guidelines should consistently be kept in apperception if autograph recursive rules.

    :Always put the non-recursive assert afore the recursive predicate. If you alpha with the recursive assert prolog will try to seek added afore seeing if it can stop recursing. In the archetype above, it wouldnt infact create a difference, but your program wont consistently be so apple-pie and straight-forward.

    :When autograph a recursive rule, if at all possible, put the recursive aphorism on the right. For instance, use:

     a :- b, a.

    :instead of

     a :- a, b.

    :This is the aforementioned assumption as the antecedent guideline, let prolog appraise non recursive goals first. If you recurse afore youve evaluated the additional sub-goals, prolog could either get ashore in absolute recursions, or do a lot of accidental work. Sometimes its all-important to put the recursive aspect on the left, but dont do it unless you understand why youre accomplishing it.

    :

    : Lists are infact a actual accepted anatomy in prolog programs, and theyre acclimated in a recursive way. The afterward affiliate explains how.

    ----

    


    prev: next:

    


    

 


Tags: program, goals, rules, ancestor, person, parent, david

 parent, ancestor, recursive, steve, prolog, david, definition, rules, predicate, person, grandparent, define, nathan, goals, following, program, , recursive rules, parent steve, recursive predicate, define ancestor, david john, unify parent steve, parent david john, prolog recursive rules,

Share Prolog Recursive Rules: Digg it!   Google Bookmarks   Del.icio.us   Yahoo! MyWeb   Furl  Binklist   Reddit!   Stumble Upon   Technorati   Windows Live   Bookmark

Text link code :
Hyper link code:

Also see ...

FAQ

Permalink
Article In : Reference & Education  -  Language