Javatpoint Logo
Javatpoint Logo

Two Factorial Definitions

It is used to find out the factorial functions. The stating of these definitions is as follows:

In prolog, this program has two clauses. The first clause has no body, and it is a unit clause. The second clause of prolog has a body. On the right-hand side of '-', the body of the second clause exists. The symbol '-' will be read as "if". The body consists of literals and that literals can be separated using the commas ','. The symbol '.' will be read as "and". If the clause is a unit clause, then the head of the class will be the whole class. Otherwise, the part of the clause which appears to the left of the colon in ':-' will be the head of the class. The first clause (unit clause) is used to show that "factorial 0 is 1". The second clause is used to show that "factorial of N is F if N>0, N1 is N-1, the factorial of N1 is F1 and F is N*F1".

In prolog, if we find the factorial of 3, it will respond with a value of W.

Now we are going to construct the following clause tree for factorial(3, W). The clause tree has an instance of variables, but it does not have any free variables. In the original program, each branch can be determined by a clause under a node. Using the instance of head of clause, the node of the tree can be determined. In the clause tree, the children of the node can be determined by the body literals of the clause.

Two Factorial Definitions

After evaluation, we can say that all of the arithmetic leaves are true. In the above tree, the lowest link corresponds to the very first clause of the factorial program. The first clause of the program will be written as

?- true is the goal of a prolog that always succeeds.

At the root of the tree, the clause tree in the program provides the meaning of the program for the goal. The factorial(3, 6) has a clause tree, and all of the leaves of factorial(3,6) are true that's why factorial(3, 6) is a consequence of the prolog program. The literal factorial(7, 3) has no clause tree and has all true leaves that's why factorial(7, 3) is not a consequence of the prolog program. For the literal factorial(7, 3), the meaning of the program shows that it is false.

The clause trees of the program are also known as AND-trees. If the subtrees are rooted at literals, the root to be a consequence of the program and all the subtrees will also be consequences of the program. Now we know that clause trees give us a meaning.

Now we are going to differentiate between the prolog derivation tree and program clause tree. In prolog, the derivation tree is based on the variable-binding mechanism. It is also based on the order that is considered by subgoals. The clause trees of the program are static in nature. The clause trees are drawn for a program. The clause tree is simply a declarative reading of the program.

How variables are bound in prolog is shown by the execution of prolog trace to satisfy goals. The on and off of the prolog tracer is shown in the following sample:

To find the Prolog goal 'factorial(3, X)', the following animation tree describes another look at the derivation tree. Click on the button 'Step' to start the animation.

In this section, two factorial definitions referred by this title. Here, we show the same predicate name but with three variables.

Specify the following goal for this version.

In the definition, accumulating parameter is shown by the second parameter.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA