Skip to main content

Write simple fact for the statement using PROLOG

 

Write simple fact for the statement using PROLOG.

A Prolog program consists of a number of clauses. Each clause is either a fact or a rule. After a Prolog program is loaded (or consulted) in a Prolog interpreter, users can submit goals or queries, and the Prolog intepreter will give results (answers) according to the facts and rules.

Facts 

A fact must start with a predicate (which is an atom) and end with a fullstop. The predicate may be followed by one or more arguments which are enclosed by parentheses. The arguments can be atoms (in this case, these atoms are treated as constants), numbers, variables or lists. Arguments are separated by commas.

If we consider the arguments in a fact to be objects, then the predicate of the fact describes a property of the objects.

In a Prolog program, a presence of a fact indicates a statement that is true. An absence of a fact indicates a statement that is not true. See the following example:

Program 2: A Prolog program with facts only

sunny.
father(john, peter).
father(john, mary).
mother(susan, peter).

Goals/Queries and their results (in red):

?- sunny.    /* The response is yes because the fact "sunny." is present. */

yes

?- rainy.    /* There is an error because there is no predicate "rainy". */

Erroneous result.

?- father(john, mary).

yes

?- mother(susan, mary). /* This cannot be deduced. */

no

?- father(john, susan). /* This cannot be deduced. */

no

Rules 

A rule can be viewed as an extension of a fact with added conditions that also have to be satisfied for it to be true. It consists of two parts. The first part is similar to a fact (a predicate with arguments). The second part consists of other clauses (facts or rules which are separated by commas) which must all be true for the rule itself to be true. These two parts are separated by ":-". You may interpret this operator as "if" in English.

See the following example:

Program 3: A program describes the relationships of the members in a family

father(jack, susan).                             /* Fact  1 */
father(jack, ray).                               /* Fact  2 */
father(david, liza).                             /* Fact  3 */
father(david, john).                             /* Fact  4 */
father(john, peter).                             /* Fact  5 */
father(john, mary).                              /* Fact  6 */
mother(karen, susan).                            /* Fact  7 */
mother(karen, ray).                              /* Fact  8 */
mother(amy, liza).                               /* Fact  9 */
mother(amy, john).                               /* Fact 10 */
mother(susan, peter).                            /* Fact 11 */
mother(susan, mary).                             /* Fact 12 */

parent(X, Y) :- father(X, Y).                    /* Rule  1 */
parent(X, Y) :- mother(X, Y).                    /* Rule  2 */
grandfather(X, Y) :- father(X, Z), parent(Z, Y). /* Rule  3 */
grandmother(X, Y) :- mother(X, Z), parent(Z, Y). /* Rule  4 */
grandparent(X, Y) :- parent(X, Z), parent(Z, Y). /* Rule  5 */
yeye(X, Y) :- father(X, Z), father(Z, Y).        /* Rule  6 */
mama(X, Y) :- mother(X, Z), father(Z, Y).        /* Rule  7 */
gunggung(X, Y) :- father(X, Z), mother(Z, Y).    /* Rule  8 */
popo(X, Y) :- mother(X, Z), mother(Z, Y).        /* Rule  9 */

Take Rule 3 as an example. It means that "granfather(X, Y)" is true if both "father(X, Z)" and "parent(Z, X)" are true. The comma between the two conditions can be considered as a logical-AND operator.

You may see that both Rules 1 and 2 start with "parent(X, Y)". When will "parent(X, Y)" be true? The answer is any one of these two rules is true. This means that "parent(X, Y)" is true when "father(X, Y)" is true, or "mother(X, Y)" is true.

As you can see from Rules 3 to 5, predicates that only appear in rules but not facts (in this case, parent) can also form conditions of other rules.

 

Comments

Popular posts from this blog

Write a code simulating ARP /RARP protocols

   Write a code simulating ARP /RARP protocols . Aim:        To write a java program for simulating ARP/RARP protocols ALGORITHM: server 1. Create a server socket and bind it to port. 2. Listen for new connection and when a connection arrives, accept it. 3. Send server ‟ s date and time to the client. 4. Read client ‟ s IP address sent by the client. 5. Display the client details. 6. Repeat steps 2-5 until the server is terminated. 7. Close all streams. 8. Close the server socket. 9. Stop. Client 1. Create a client socket and connect it to the server ‟ s port number. 2. Retrieve its own IP address using built-in function. 3. Send its address to the server. 4. Display the date & time sent by the server. 5. Close the input and output streams. 6. Close the client socket. 7. Stop. Program Program for Address Resolutuion Protocol (ARP) using TCP Client: import java.io.*; import java.net.*; impor...

Importants Links of Leetcode

  Here are all the important discussion posts which contain brief descriptions about a topic with its problem list. Recursion:- A.   https://leetcode.com/tag/recursion/discuss/1733447/Become-Master-In-Recursion Binary Tree and Binary Search Tree:- A.   https://leetcode.com/tag/binary-tree/discuss/1212004/Binary-Trees-study-guide B.   https://leetcode.com/tag/binary-tree/discuss/1820334/Become-Master-in-Tree C.   https://leetcode.com/tag/binary-tree/discuss/1094690/Views-and-Traversal-of-binary-tree-or-Important-topics-or-Must-Read-%3A- ) Dynamic Programming:- A.   https://leetcode.com/discuss/study-guide/458695/Dynamic-Programming-Patterns B.   https://leetcode.com/tag/dynamic-programming/discuss/1437879/Dynamic-Programming-Patterns C.   https://leetcode.com/tag/dynamic-programming/discuss/662866/DP-for-Beginners-Problems-or-Patterns-or-Sample-Solutions D.   https://leetcode.com/tag/dynamic-programming/discuss/1050391/Must-do-Dynamic-programm...

The Future of Web Development: Why Next.js is Going Viral

  Are you ready to level up your web development game? Look no further than Next.js, the latest sensation in the world of web development that's taking the industry by storm. With its powerful features, flexibility, and performance optimizations, Next.js is becoming the go-to framework for building modern web applications. In this blog post, we'll explore why Next.js is going viral and how it's changing the landscape of web development. Supercharged Performance: One of the main reasons why Next.js is making waves is its superior performance capabilities. With built-in optimizations like automatic static site generation (SSG) and server-side rendering (SSR), Next.js allows you to create blazing-fast web applications that load quickly and deliver seamless user experiences. This is especially important in today's fast-paced digital world, where users demand instant gratification and have little patience for slow-loading websites. Flexibility and Scalability: Next.js provid...