In this section, we will focus on creating the knowledge base for problem solving. This KB is the fundamental part of Logic Programming and contains facts, rules and questions/queries.
Facts represent an explicit relationship between objects, and properties. They are unconditionally true in nature. For example,
| Tom is a dog | Ria loves to eat icecream | Jia stood first. |
| Stars twinkle | Sun is hot | Pratyusha plays cricket |
To write facts in Prolog, we must remember the following points:
- Names of properties/relationships begin with lower case letters.
- Name of the relationship is written as the first term.
- Objects appear as comma-separated arguments within parentheses.
- A fact must be ended with a period “.”.
- Objects begin with lower case letters. They can also start with digits, and can be strings of characters enclosed in quotes, e.g., color(hair, ‘black’).
- Phone_no(anuj, 1234567890). is a predicate or clause.
The syntax for defining facts is,

For example,

Rules define an implicit relationship between objects. It considers facts to be conditionally true. That is, when an associated condition is true, then the predicate is also true. For example,
- Mina is happy if she sings.
- Krish is hungry if he is eating stale food.
- Parul and Disha are friends if both of them go for dancing classes.
- Will go to play if it is a holiday and friend is free.
In case of a rule, if the right-hand side is true, then the left-hand side is also true. Technically, it is represented with the symbol (:- ) and is pronounced as “If”, or “is implied by”. This is also known as neck symbol. In a rule, the LHS of this symbol is called the Head. Correspondingly, RHS is called Body.
To represent conjunction in a rule, we use comma (,) and to represent disjunction, semicolon is used.
Syntax of writing a rule is,

| If we have a rule as, P:- Q;R. Then, it can be written as, P:- Q. P:- R. | If a rule is, P:- Q, R; S, T, U. Then, it can also be written as, P:- Q, R. P:- S, T, U. |

Queries are some questions on the relationships between objects and object properties. Logic programming language finds answers to these questions and returns them. For example, as per our facts and rules, some queries can be,
| Is tom a dog? | Does riya loves to eat iceccream? |
| Is mina happy? | Will Manav play guitar? |
Creating Knowledge Base 1 Containing Facts in Prolog
Open a file in any text editor (like WordPad, NotePad, etc.) and write the facts given below in it. Save this file with any name and .pl extension. We have saved this file as kb1.pl in the SampleDocs folder.
student(ben).
student(kiara).
student(madhav).
studies_AI(kiara).
Now, in the GNU-Prolog, we will ask queries and get a reply as shown in the figure given below. Note that every time we start GNU-Prolog, we have to change directory to set it where our files are stored. Then we have to compile our knowledge base before executing the queries.
Names of persons, filenames, objects and relations are all written in lowercase letters, because in Prolog, a string starting with uppercase letter indicates a variable.

Credit: Copyright (C) 1999-2021 Daniel Diaz
Creating Knowledge Base 2 Containing Facts and Rules in Prolog
Open a new file in text editor and write the following facts and rules in it. Save this file as kb2.pl in SampleDocs folder.



Leave a Reply