Skip to main content
freelanceshack.com

Back to all posts

How to Make Fact(Any) In Prolog?

Published on
5 min read
How to Make Fact(Any) In Prolog? image

Best Prolog Programming Books to Buy in October 2025

1 Programming in Prolog: Using The Iso Standard

Programming in Prolog: Using The Iso Standard

  • AFFORDABLE PRICES: GREAT SAVINGS ON QUALITY USED BOOKS!
  • GOOD QUALITY: THOROUGHLY CHECKED FOR READABILITY AND WEAR.
  • ECO-FRIENDLY: SUPPORT SUSTAINABLE READING BY CHOOSING USED BOOKS!
BUY & SAVE
$71.24 $74.99
Save 5%
Programming in Prolog: Using The Iso Standard
2 Logic Programming with Prolog

Logic Programming with Prolog

BUY & SAVE
$45.61 $49.99
Save 9%
Logic Programming with Prolog
3 Clause and Effect: Prolog Programming for the Working Programmer

Clause and Effect: Prolog Programming for the Working Programmer

  • QUALITY ASSURANCE: EACH BOOK IS THOROUGHLY CHECKED FOR GOOD CONDITION.
  • ECO-FRIENDLY CHOICE: SAVE MONEY AND THE ENVIRONMENT WITH USED BOOKS!
  • UNIQUE FINDS: DISCOVER RARE TITLES YOU WON’T FIND IN NEW EDITIONS!
BUY & SAVE
$80.06 $84.99
Save 6%
Clause and Effect: Prolog Programming for the Working Programmer
4 Prolog Programming for Artificial Intelligence

Prolog Programming for Artificial Intelligence

BUY & SAVE
$56.73 $79.80
Save 29%
Prolog Programming for Artificial Intelligence
5 Learn Prolog Now! (Texts in Computing, Vol. 7)

Learn Prolog Now! (Texts in Computing, Vol. 7)

  • AFFORDABLE PRICES MAKE QUALITY READING ACCESSIBLE FOR EVERYONE.
  • THOROUGHLY CHECKED FOR QUALITY; GREAT VALUE FOR GREAT STORIES!
  • ECO-FRIENDLY CHOICE: REDUCE WASTE BY BUYING PRE-LOVED BOOKS.
BUY & SAVE
$22.21 $31.00
Save 28%
Learn Prolog Now! (Texts in Computing, Vol. 7)
6 Micro-Prolog: Programming in Logic

Micro-Prolog: Programming in Logic

BUY & SAVE
$247.19
Micro-Prolog: Programming in Logic
7 Prolog: The Standard: Reference Manual

Prolog: The Standard: Reference Manual

  • EXCELLENT VALUE WITH AFFORDABLE PRICES ON QUALITY USED BOOKS.
  • THOROUGHLY INSPECTED FOR GOOD CONDITION AND READY TO READ!
  • ENVIRONMENTALLY FRIENDLY CHOICE BY PROMOTING BOOK REUSE.
BUY & SAVE
$64.34 $119.99
Save 46%
Prolog: The Standard: Reference Manual
8 The Practice of Prolog (Logic Programming)

The Practice of Prolog (Logic Programming)

BUY & SAVE
$35.00
The Practice of Prolog (Logic Programming)
+
ONE MORE?

In Prolog, facts are represented as statements that describe relationships or properties between objects. To create a fact in Prolog, you would typically use the following syntax:

predicate_name(argument1, argument2, ..., argumentN).

Here, predicate_name is the name of the fact, and argument1, argument2, ..., argumentN are the arguments that the fact takes. Each fact should end with a period to signify the end of the statement.

For example, if you wanted to create a fact that states "john is a man", you would write it as follows:

is_man(john).

This fact indicates that the atom john is a member of the category man. This fact could then be queried or used in other Prolog clauses to infer further relationships or properties.

How to manipulate facts in Prolog?

In Prolog, facts are typically stored as clauses in a predicate definition. To manipulate facts in Prolog, you can use various built-in predicates such as assertz/1, asserta/1, retract/1, and retractall/1. Here are some examples of how you can manipulate facts in Prolog:

  1. Adding a new fact: You can add a new fact to the knowledge base using the assertz/1 predicate. For example, to add the fact "likes(john, pizza)" to the knowledge base, you can write:

assertz(likes(john, pizza)).

  1. Removing a fact: You can remove a fact from the knowledge base using the retract/1 predicate. For example, to remove the fact "likes(john, pizza)" from the knowledge base, you can write:

retract(likes(john, pizza)).

  1. Removing all facts: You can remove all facts for a given predicate using the retractall/1 predicate. For example, to remove all facts for the predicate likes/2, you can write:

retractall(likes(_, _)).

  1. Checking if a fact is true: You can check if a fact is true using pattern matching with the facts in the knowledge base. For example, to check if the fact "likes(john, pizza)" is true, you can write:

likes(john, pizza).

These are some basic ways to manipulate facts in Prolog. By using these built-in predicates and pattern matching, you can add, remove, and check the validity of facts in your Prolog programs.

How to remove a fact from a Prolog database?

To remove a fact from a Prolog database, you can use the retract predicate. Here's an example of how to remove a fact from a Prolog database:

% Define the fact to be removed likes(john, pizza).

% Check if the fact exists in the database ?- likes(john, pizza). true.

% Remove the fact from the database retract(likes(john, pizza)).

% Check if the fact has been removed ?- likes(john, pizza). false.

In this example, the retract predicate is used to remove the fact likes(john, pizza) from the Prolog database. After removing the fact, querying likes(john, pizza) returns false, indicating that the fact has been successfully removed.

How to represent a fact in Prolog?

In Prolog, facts are represented using predicates.

A fact is a statement that is true in a particular interpretation or model of the Prolog program. Facts are defined using a predicate with a specific arity (number of arguments).

For example, if we want to represent the fact "John is a man", we can define it in Prolog as follows:

man(john).

This fact can be interpreted as "john is a man" in the context of the program.

To represent multiple facts, you can define them as separate predicates. For example, if we want to represent the fact "Mary is a woman", we can define it like this:

woman(mary).

These facts can be queried and used in Prolog rules to derive new information or make decisions.

How to organize facts to improve code readability in Prolog?

There are a few strategies you can use to organize facts in Prolog to improve code readability:

  1. Group related facts together: Organize facts that are related to each other into logical groups. For example, you could group facts related to different types of objects, or facts related to different properties of a single object.
  2. Use comments: Add comments to your code to explain the purpose of different groups of facts, or to provide additional context for specific facts. This can help other developers understand the code more easily.
  3. Use descriptive predicate names: Choose descriptive names for your predicates that make it clear what each fact represents. This can help other developers quickly understand the purpose of each fact without having to read the details.
  4. Order facts logically: Arrange facts in a logical order, such as alphabetically or by priority. This can help make it easier to find specific facts when reading the code.
  5. Use indentation and spacing: Use indentation and spacing to visually separate different groups of facts, making it easier to see the structure of your code at a glance.

By following these strategies, you can make your Prolog code more organized and easier to read, which can help improve the overall readability and maintainability of your codebase.