Introduction to Prolog: A Programming Language for AI

Prolog is a programming language that is well-suited for developing logic-based artificial intelligence applications.

Written by Charles Calapini
Published on Nov. 01, 2023
Robotic hand writing prolog on computer
Image: Shutterstock / Built In
Brand Studio Logo

Prolog is a programming language that’s well-suited for developing logic-based artificial intelligence applications.

What Is Prolog?

Prolog is a declarative programming language designed for developing logic-based AI applications. Developers can set rules and facts around a problem, and then Prolog’s interpreter will use that information to automatically infer solutions.

It’s a declarative programming language, meaning that it allows the programmer to specify the rules and facts about a problem domain, and then the Prolog interpreter will use these rules and facts to automatically infer solutions to problems.

 

prolog screenshot
Prolog programming language screenshot. | Screenshot: Charles Calapini

How Is Prolog Different From Other Programming Languages?

One of the key features of Prolog is its ability to handle uncertain or incomplete information. In Prolog, a programmer can specify a set of rules and facts that are known to be true, but they can also specify rules and facts that might be true or false.

The Prolog interpreter will then use those rules and facts to automatically reason about the problem domain and find solutions that are most likely to be correct, given the available information.

More on AI3 Reasons AI Should Be Open Source

 

How to Use Prolog

One way to use Prolog is to define a set of rules that describe the relationships between different objects or concepts in your problem domain. For example, you might define rules that specify that certain objects are bigger than others, or that some objects are the same color. Then, you can use Prolog to ask questions about these objects and their relationships, and the interpreter will use your rules to deduce the answers.

To use Prolog, you will need to have a Prolog interpreter installed on your computer. There are several different Prolog interpreters available, including SWI-Prolog, GNU Prolog and B-Prolog. Once you’ve installed an interpreter, you can start writing Prolog programs using a text editor and then run them using the interpreter.

Prolog is a powerful and flexible programming language that’s well-suited for developing logic-based artificial intelligence applications. It allows the programmer to specify a set of rules and facts about a problem domain, and then use those rules and facts to automatically infer solutions to problems.

 

Prolog Program Basics to Know

In Prolog, programs are made up of two main components: facts and rules. Facts are statements that are assumed to be true, such as “John is a man” or “the capital of France is Paris.” Rules are logical statements that describe the relationships between different facts, such as “If John is a man and Mary is a woman, then John is not Mary.”

Prolog programs are written using a syntax that is similar to natural language. For example, a simple Prolog program might look like this:

man(john).
woman(mary).
capital_of(france, paris).

not(X,Y) :- man(X), woman(Y).

In this example, the first three lines are facts, while the fourth line is a rule. The rule uses the not/2 predicate to state that if X is a man and Y is a woman, then X is not Y.

 

Prolog Tips to Know

There is no single “syntax” for Prolog, as the language allows for a wide range of different programming styles and approaches. However, here are some basic elements of Prolog syntax that are commonly used:

  • Facts are statements that are assumed to be true. In Prolog, facts are written using a predicate name followed by a list of arguments enclosed in parentheses. For example: man(john).
  • Rules are logical statements that describe the relationships between different facts. In Prolog, rules are written using the predicate name followed by a list of arguments enclosed in parentheses, followed by a colon and a hyphen (:-) and the body of the rule. For example: not(X,Y) :- man(X), woman(Y).
  • Variables are used to represent values that can change or be determined by the interpreter. In Prolog, variables are written using a name that begins with an uppercase letter. For example: X.
  • Queries are used to ask the interpreter to find solutions to problems based on the rules and facts in the program. In Prolog, queries are written using the same syntax as facts followed by a question mark (?). For example: not(john, mary)?

 

How Do Facts Work in Prolog?

In Prolog, facts are statements that are assumed to be true. They are used to provide the interpreter with information about the problem domain, and the interpreter will use this information to automatically infer solutions to problems.

Facts are written using a predicate name followed by a list of arguments enclosed in parentheses. For example:

man(john).
woman(mary).
capital_of(france, paris).

In this example, the first line states that john is a man, the second line states that mary is a woman, and the third line states that paris is the capital of france.

Prolog facts can have any number of arguments, and the arguments can be variables or constants. For example, the following fact has two arguments, one of which is a variable:

parent_of(X,Y) :- man(X), woman(Y).

In this case, the fact states that if X is a man and Y is a woman, then X is the parent of Y.

Once you have defined your facts in a Prolog program, you can use them to automatically infer solutions to problems. For example, you could ask the interpreter to find the capital of France by using the following query:

capital_of(france, X)?

In this query, the interpreter will use the capital_of/2 fact that you defined earlier to determine that the capital of France is Paris, and it will return the value paris as the solution.

More on AIHow to Develop A Large Language Model (LLM) Application

 

How Do Rules Work in Prolog?

In Prolog, rules are logical statements that describe the relationships between different facts. They are used to specify the conditions that must be met in order for a certain fact to be true.

Rules are written using the predicate name followed by a list of arguments enclosed in parentheses followed by a colon and a hyphen (:-) and the body of the rule. For example:

not(X,Y) :- man(X), woman(Y).

In this example, the rule uses the not/2 predicate to state that if X is a man and Y is a woman, then X is not Y. The body of the rule is made up of two facts: man(X) and woman(Y).

Prolog rules can have any number of arguments, and the arguments can be variables or constants. For example, the following rule has three arguments, two of which are variables:

bigger_than(X,Y,Z) :- size(X,Xsize), size(Y,Ysize), Xsize > Ysize.

In this case, the rule states that if X and Y are objects with sizes Xsize and Ysize, respectively, and Xsize is greater than Ysize, then X is bigger than Y.

Once you have defined your rules in a Prolog program, you can use them to automatically infer solutions to problems. For example, you could ask the interpreter to find out which object is bigger than another object by using the following query:

bigger_than(X,Y,Z)?

In this query, the interpreter will use the bigger_than/3 rule that you defined earlier to determine which object is bigger than the other, and it will return the appropriate value for Z as the solution.

 

How Do Variables Work in Prolog?

In Prolog, variables are used to represent values that can change or be determined by the interpreter. They are written using a name that begins with an uppercase letter, such as X or Y.

Variables can be used in both facts and rules to represent values that are not known at the time the program is written. For example, the following fact uses a variable to represent the capital of a country:

capital_of(Country, Capital).

In this case, the fact states that the Capital of a given Country is unknown, and the interpreter will use other facts and rules to determine the value of Capital when a query is made.

 

How Do Queries Work in Prolog?

Queries are used to ask the interpreter to find solutions to problems based on the rules and facts in the program. In Prolog, queries are written using the same syntax as facts, followed by a question mark (?). For example:

capital_of(france, X)?

In this query, the interpreter will use the capital_of/2 fact that was defined earlier to determine that the capital of France is Paris, and it will return the value paris for the variable X as the solution.

You can use queries to ask the interpreter to find solutions to a wide range of problems, based on the rules and facts that you have defined in your Prolog program. The interpreter will use these rules and facts to automatically reason about the problem domain and find solutions that are most likely to be correct, given the available information.

 

Prolog Program Example

Here is a simple Prolog program that defines a set of rules and facts about a problem domain, and then uses those rules and facts to answer a few queries:

% Facts
man(john).
woman(mary).
capital_of(france, paris).
% Rule
not(X,Y) :- man(X), woman(Y).
% Query 1
not(john, mary)?
% Query 2
capital_of(france, X)?

In this example, the program defines three facts: man(john), woman(mary), and capital_of(france, paris). These facts state that John is a man, Mary is a woman and Paris is the capital of France.

The program also defines a rule using the not/2 predicate. This rule states that if X is a man and Y is a woman, then X is not Y.

Finally, the program includes two queries. The first query, not(john, mary)?, asks the interpreter to determine whether John is not Mary, based on the not/2 rule and the man/1 and woman/1 facts. The interpreter will use these rules and facts to deduce that John is not Mary, and it will return true as the solution to the query.

The second query, capital_of(france, X)?, asks the interpreter to determine the capital of France. The interpreter will use the capital_of/2 fact to determine that the capital of France is Paris, and it will return the value paris for the variable X as the solution.

Overall, this Prolog program demonstrates how to define rules and facts about a problem domain, and how to use those rules and facts to automatically infer solutions to problems.

 

Example AI Application of Prolog

One possible example of an AI application in Prolog is a simple diagnostic tool for medical conditions. In this application, the Prolog program would define a set of rules and facts about different medical conditions and their symptoms, and then use those rules and facts to diagnose a patient’s condition based on their reported symptoms.

Here is a simple example of a Prolog program that could be used for this purpose:

% Facts
has_symptom(flu, fever).
has_symptom(flu, headache).
has_symptom(flu, body_aches).
has_symptom(flu, cough).
has_symptom(flu, sore_throat).
has_symptom(flu, runny_nose).
has_symptom(allergy, sneezing).
has_symptom(allergy, watery_eyes).
has_symptom(allergy, runny_nose).
has_symptom(allergy, itchy_eyes).
has_symptom(cold, sneezing).
has_symptom(cold, watery_eyes).
has_symptom(cold, runny_nose).
has_symptom(cold, cough).
has_symptom(cold, sore_throat).
% Rule
has_condition(X,C) :- has_symptom(C,X).
% Query
has_condition(sneezing, X)?

In this example, the program defines a set of facts that describe the symptoms of three different medical conditions: the flu, allergies and the common cold. The program also defines a rule using the has_condition/2 predicate, which states that if a patient has a certain symptom, then they have the medical condition that is associated with that symptom.

Finally, the program includes a query that asks the interpreter to determine which medical condition a patient has based on their reported symptoms. In this case, the query specifies that the patient has the symptom of sneezing, and it asks the interpreter to determine which medical condition the patient has. The interpreter will use the has_condition/2 rule and the has_symptom/2 facts to deduce that the patient has either the flu, allergies or the common cold, and it will return one of these conditions as the solution to the query.

This simple Prolog program demonstrates how the language can be used to develop an AI application that can diagnose medical conditions based on symptoms. Of course, in a real-world application, the program would need to be much more comprehensive and sophisticated, with a larger set of rules and facts and the ability to handle a wider range of symptoms and conditions.

A tutorial on Prolog programming language. | Video: Derek Banas

More on AIWhat Is Neuromorphic Computing?

 

Advantages of Using Prolog

In conclusion, Prolog is a powerful and flexible language for developing AI applications. If you are interested in exploring the capabilities of Prolog for yourself, consider downloading a Prolog interpreter and experimenting with writing your own programs. 

You may be surprised by the insights and solutions that can be automatically inferred from a set of simple rules and facts. Give Prolog a try and see what it can do for you.

Explore Job Matches.