Mastering Facts in Prolog: Definitions and Implementation

21 Oct 2023 Balmiki Mandal 0 Prolog Programming language

Defining and Using Facts in Prolog Programming Language

What are facts in Prolog?

A fact in Prolog is a declarative statement about the problem domain. It is a predicate expression that is assumed to be always true. Facts are represented in Prolog as a predicate followed by a list of arguments, enclosed in parentheses and ending with a period.

For example, the following are facts about a fictitious family:

Prolog
father_of(joe, paul).
father_of(joe, mary).
mother_of(jane, paul).
mother_of(jane, mary).

These facts state that Joe is the father of Paul and Mary, and Jane is the mother of Paul and Mary.

How to use facts in Prolog

Facts can be used in Prolog to answer questions about the problem domain. For example, we can ask Prolog whether Joe is the father of Paul:

Prolog
?- father_of(joe, paul).

Prolog will respond with "yes", indicating that the query is true.

We can also use facts to ask more complex questions. For example, we can ask Prolog whether Jane has any grandchildren:

Prolog
?- grandchild(jane, X).

Prolog will respond with "X = paul" and "X = mary", indicating that Jane has two grandchildren: Paul and Mary.

Example

The following Prolog program shows how to use facts to answer questions about a family tree:

Prolog
father_of(joe, paul).
father_of(joe, mary).
mother_of(jane, paul).
mother_of(jane, mary).

% Query: Does Joe have any children?
?- father_of(joe, X).
% Yes, Joe has two children: Paul and Mary.
X = paul.
X = mary.

% Query: Does Jane have any grandchildren?
?- grandchild(jane, X).
% Yes, Jane has two grandchildren: Paul and Mary.
X = paul.
X = mary.

Conclusion

 

Facts are an important part of Prolog programming. They allow us to represent knowledge about the problem domain in a declarative way. Facts can be used to answer questions about the problem domain, and to infer new knowledge.

Enroll Now: 

Prolog Programming language Scratch to advance 2023 - 2024] "Start Supercharging Your Productivity!"

Contact Us:

  • For any inquiries, please email us at [info@electro4u.net].
  • Follow us on insta  [ electro4u_offical_ ] for updates and tips.

 

 

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.