A small introduction to logic programming

A small introduction to logic programming

4 min read
Tags:

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

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

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:

Using Recursion

Variable immutability and unification

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

On the Prolog interpreter write the following:

Unification

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

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:

Related resources

How to Send Emails Using an API

Key Takeaways This post will provide a complete walkthrough for integrating an email API focused…

How to build a CRM in 3 sprints with Nylas

What is a CRM? CRM stands for Customer Relationship Management, and it’s basically a way…

How to create an appointment scheduler in your React app

Learn how to create an appointment scheduler in your React app using Nylas Scheduler. Streamline bookings and UX with step-by-step guidance.