A Small Introduction to Logic Programming

Have you ever wondered what Logic Programming is all about? Let us show you using simple examples in Prolog.

A Small Introduction to Logic Programming

Logic programming is a paradigm where everything is expressed in facts and rules.

While the use of mathematical logic to represent and execute computer programs was specified on Lambda Calculus, developed by Alonzo Church in the 1930s, the formal proposal to use a clausal form of logic was made by Cordell Green. There aren’t many known logic programming languages out there, and Prolog is considered the first logic programming language.

There are other not well known logic programming languages like Mercury (Which is actually Functional/Logic), Mozart and Datalog (Part of Racket).

Artificial Intelligence is the field where Logic Programming is most used.

What makes logic programming so special?

Facts → Something true about the domain problem. (Haskell is a functional language)

Rules → Inferences about facts in the domain. (All functional languages are hard to learn)

Queries → Questions about the domain. (Is Haskell hard to learn?)

Unification → Basically means that two objects have the same value.

What do we need?

In this post, we’re going to explore some key fundamentals of logic programming using examples in the most known and used programming language.

Prolog (1952 / Logic) → If we’re on a Mac, the best way is to use homebrew and type on the terminal window:

$ brew install swi-prolog

For other systems, you can refer to the download page.

Facts, Rules and Queries

We’re going to create a program where we define who likes an ice cream flavour (Fact), we’re going to specify that if someone likes a flavour, it means that the person eats that flavour (Rules) and finally, when calling our application, we’re going to ask whether a person likes a flavour and which flavour is liked by a person (Query).

For this, we will create a file called ice_cream.pl (Yep, the same extension as Perl):

% Facts

% Rules
% If someone likes a flavor, then that
% someone eats that flavor
eats(X,Y) :- likes(X,Y).

On the same folder, go to the terminal and type:

$ swipl

Once we’re in the Prolog interpreter, we need to type:

?- [ice_cream].

To load our application and start doing queries.

Our first Logic example. How likes Ice Cream?

We can see that it’s true that Blag likes Caramel. And we can confirm that Ram eats Vanilla, because Ram likes Vanilla.


Recursion is important in logic programming as well, especially because there are no loops.

Create file called recursion.pl and type the following code:

% H -> Head -> First element of list
% T -> Tail -> Rest of list
list([H|T]) :- write(H), nl, 

This will print each of the elements of the list passed as parameter:

Using Recursion

Variable Immutability and Unification

Once assigned, a variable cannot change its value. Unification means equality.

On the Prolog interpreter write the following:


Finding a Path

Let’s say that we have the following graph:

Path Graph

And we want to find the paths going from 1 to 6, 1 to 5 or 4 to 6. Let’s create a program called path.pl:

% Define the connections between nodes

% Receive start and end points.
% We reach the end point
path(X, Y, [X,Y]) :- edge(X, Y).
% We haven't reached the end point
path(X, Y, [X|Path]) :-
% Look for the next node
    edge(X, Z),
% Call the function again with the
% next end point and stop if end
% point is reached    
    path(Z, Y, Path), !.

We want to go from point A to point B. We use recursion to navigate through the nodes and find the best path.

Finding the shortest logic path

And that’s it for a small introduction to logic programming.

To see the Livestream, go to Introduction to Logic Programming | Coding with Nylas | Episode 29.

If you like Functional Programming, you can check A Small Introduction to Functional Programming and its accompanying Livestream Introduction to Functional Programming | Coding with Nylas | Episode 27.

A Prolog and Logic programming joke
In old Prolog incarnations, Yes. was used instead of true.

Don’t miss the action, watch our LiveStream Coding with Nylas:

You May Also Like

Nylas_Gartner Cool Vendor
Nylas recognized as a Cool Vendor in the Gartner 2023 Cool Vendors in Composable Customer Engagement Platforms
How to manage your Contacts using Reflex (Pynecone)
2023 Compliance Audits
Nylas’ 2023 compliance audits: A benchmark in trust and security

Subscribe for our updates

Please enter your email address and receive the latest updates.