Logical Programming in PicoLisp: Learning Pilog!

Logical Programming in PicoLisp: Learning Pilog!


4 min read

Welcome to the "Programming in Pilog" series!

I guess it's quite likely that you have never heard of Pilog before. Short definition: By using Pilog, we can do logical programming within PicoLisp.

In this post, we will explain what logical programming actually is and where it is used within the PicoLisp world.

What is Pilog?

Pilog is short for Picolisp Prolog, and is a Prolog engine for PicoLisp.

As you might know, Prolog was the first logical programming language and is still one of the most important ones. Logical programming can be very powerful in certain application fields, for example for writing queries to a database. It enables us to use simple and expressive statements to perform complex tasks.

First of all, let's try to understand where we can find logical programming languages within the world of programming paradigms.

A quick overview over programming paradigms

On the highest level, there are two types of programming paradigms, i. e. ways to classify a programming style. On the one side we have imperative languages, and on the other side declarative languages. From a very high-level classification, it could look like this:


Note that most languages are not restricted to a single programming paradigm. However due to grammar and implementation, some are more suitable to write code in certain paradigms than in others.

Generally, the philosophy of an imperative language is that the programmer tells the computer how to accomplish a task, while a declarative language describes what should be accomplished.

Most modern programming language (like C, Java, Python) are based on imperative paradigms. The imperative approach is easier to learn because it is closer to what is happening inside the computer, and because you have a step-by-step way to describe a problem.

The downside is that it often takes much more steps to solve a task - compare the PicoLisp solution of the Knapsack Problem to the Java one!

The increased verbosity can make the code harder to read, scale and maintain.

The Declarative Programming Paradigm

On the other side, we have declarative programming with its two main branches: Functional programming and logical programming.

As this blog is called "Functional Programming in PicoLisp", you should have noticed by now that PicoLisp is well suited to write functional code, although also imperative paradigms like object-orientation are supported.

Unfortunately we didn't bring a clear definition about what functional programming actually is yet. Clearly it's centered around functions, but you can do more with it than just calculations in a mathematical sense. This is certainly worth its own post and I'm working on it! As a short working definition, let's stick with Eric Normand from Lispcast.com:

Functional programming is a programming style that makes a clear distinction between actions, calculations and data.

(If this does not convince you, I recommend to check out his podcast "Thoughts on functional programming" or wait for my blogpost on that topic ๐Ÿ˜‹).

Logical Programming

Now let's take a look at the other main branch of declarative programming: Logical Programming. As the name already tells us, it is a paradigm that is based on formal logic, with its first and still most important representative Prolog.

The main idea behind logical programming is to feed a set of things you know to your system (the "Knowledge Base"), and then to test any hypothesis based on that knowledge base.

Internally, this is accomplished by backtracking strategies (recursion). On the other hand, familiar things that we know from imperative programming won't work in Prolog, like for example loops. This means basically that it requires a completely different mindset to write a Prolog program than to write, say, a Java Script program.

If you already know Prolog, Pilog will be easy for you - it is basically the same language, it just looks a little different. However, besides to what Prolog can offer, we can also call PicoLisp functions from within Pilog, which can be very useful.

If you don't know Prolog yet, this series will be very interesting for you. Logial programming is really a completely new thing and requires a different kind of thinking. I think it's fun!

Next steps

First of all, we will take a quick (historical) look into Prolog, as it is the inspiration for Pilog. Then we will first cover the basic syntax and concepts of Pilog. After that we will show how to use it within PicoLisp, for example to solve classical logical algorithms (like the "Zebra riddle"), and for database queries.


Icons made by Becris from www.flaticon.com