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 

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 

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

Create a socket for HTTP for web page upload and download

Create a socket for HTTP for web page upload and download. Aim: To write a java program for socket for HTTP for web page upload and download . Algorithm 1.Start the program. 2.Get the frame size from the user 3.To create the frame based on the user request. 4.To send frames to server from the client side. 5.If your frames reach the server it will send ACK signal to client otherwise it will send NACK signal to client. 6.Stop the program Program : Client import javax.swing.*; import java.net.*; import java.awt.image.*; import javax.imageio.*; import java.io.*; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class Client{ public static void main(String args[]) throws Exception{ Socket soc; BufferedImage img = null; soc=new Socket("localhost",4000); System.out.println("Client is running. ");  try { System.out.println("Reading image from disk. "); im...

Write an HTML program to design an entry form of student details and send it to store at database server like SQL, Oracle or MS Access

Write an HTML program to design an entry form of student details and send it to store at database server like SQL, Oracle or MS Access <!DOCTYPE html> <html> <head> <title>PHP insertion</title> <link href="css/insert.css" rel="stylesheet"> </head> <body> <div class="maindiv"> <!--HTML Form --> <div class="form_div"> <div class="title"> <h2>Insert Data In Database Using PHP.</h2> </div> <form action="insert.php" method="post"> <!-- Method can be set as POST for hiding values in URL--> <h2>Form</h2> <label>Name:</label> <input class="input" name="name" type="text" value=""> <label>Email:</label> <input class="input" name="email" type="text" value=""> <label>Contact:</label> <inp...

Write a code simulating PING and TRACEROUTE commands

  Write a code simulating PING command     Aim:   To Write the java program for simulating ping command   Algorithm Step 1: start the program. Step 2: Include necessary package in java. Step 3: To create a process object p to implement the ping command. Step 4: declare one BufferedReader stream class object. Step 5: Get thedetails of the server          5.1: length of the IP address.          5.2: time required to get the details.          5.3: send packets , receive packets and lost packets.          5.4: minimum ,maximum and average times. Step 6: print the results. Step 7:Stop the program.   Program:   import java.io.*; import java.net.*; class pingserver { public static void main(String args[]) { try { String str; System.out.print(" Enter...