successor arithmetics - What are the steps that Prolog follows with the definition of natural numbers? -


I am learning how to program in prologue and I got the next program that defines natural numbers and their amount: / p>

  yoga (succ (x), y, succ (jade)): - zodiac (x, y, z). Amount (0, X, X)? - Yoga (succ (succ (0)), succ (succ (succ (0)), answer). Answer = succ (succ (succ (succ (succ (succ (succ (0)))   

(found)

The problem is that I struggled with execution I do not know what this does for the flow of this program. How can Prolog find the value of the answer?

It helps to understand how Prolog works to detect a current masterpiece, or when someone makes a new design when you make a query like : sum (succ (succ (0)), succ (succ (succ (0)), answer).

Prolog facts and rules sum (_ , _, _) ( sum / 3 ) and select first a match in which the rules are in place:

  (1) Yoga (succ (x), y, succ (z)): - zodiac (x, y, z). (2) zodiac (0, x, x).   

If you look at the query, then it clearly matches the pattern of rule # 1. Remember that in Prologue, a variable can be started for any type of term, and A Are integrated variable names (immediately) for the same price. When Prong unites the "head" of # 1 with the query, it does by uniting the variable as follows:

  x = succ (0) y = succ (succ (succ ( 0 (A) Answer = Suk (Z)   

Note that the answer value succ (Z) even if Z has not been compelled (a solid value has been assigned) So far, we now follow the rule, which will query yoga (x, y, z) which That would be the query:

  sum (succ (0), succ (succ (succ (0)), z) | one SZZ   

The prolol will now start at the top because it is a new query for sum / 3 like the first time, it rules with the following triggers Matches # 1:

  X = 0 Y = succ (succ (succ (0))) (B) Z = succ (Z ')   

I'm using it at Z to differentiate it from sum (succ (0)), Succ ( above) succ (succ (0)), z , because it is different from the one used in the head for sum (..., su) (su) (z)) < / code>. It looks like there is a function in C, which is declared as int f (x) {return 2 * x; } and you call it from somewhere other local variables x , that name is used in two different places and two different - represents different variables).

Then we can follow the next recursive query, sum (x, y, z ') again, that becomes:

  Sum (0, succ (succ (succ (0)), z ') | | | XYZ '  

This recurring query does not match the rule # 1 because the first argument, 0 , does not match succ (X) . However, this rule matches the # 2:

  0 = 0x = succ (succ (succ (0))) (c) x = z '  

now X = succ (succ (succ (0))) hence Z '= succ (succ (succ (0))) . Since there is no other question within this rule, it is finally successful from the place where the inquiry was done. Return it back to (B):

  Z = succ (Z ') = succ (succ (succ (succ (0))))   < p> and return it to (a) back:  
  answer = succ (Z) = succ (succ (succ (succ (0)))))   

And there you have it. Using @ Code> trace, you can see these steps in Prol Interpreter and immediately follow the variable.

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -