# 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. 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)

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`

## 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
likes(blag,caramel).
likes(ash,chocolate).
likes(ram,vanilla).

% 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`

`?- [ice_cream].`

To load our application and start doing queries.

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

## Recursion

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([]).
list([H|T]) :- write(H), nl,
list(T).```

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

## Variable Immutability and Unification

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

## Finding a Path

Let’s say that we have the following 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
edge(1,2).
edge(2,3).
edge(2,4).
edge(2,6).
edge(4,3).
edge(4,5).
edge(5,6).

% 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.

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.

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

Tags:

## You May Also Like ##### 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) 