Logical Programming in PicoLisp: Learning Pilog!

Search for a command to run...

No comments yet. Be the first to comment.
In this series, we will introduce Pilog, a declarative language which is built on top of PicoLisp. It has the semantics of Prolog, but uses the syntax of Lisp.
Before we actually start off with Pilog, I thnk it will be good to take a few minutes to talk about Prolog, which is the basis of Pilog. A brief history of PROLOG The Prolog programming language appeared in 1972. It was developed and implemented in ...
In our previous post, we introduced how to utilize Discrete Event Simulation (DES) in PicoLisp. Now, let's explore another application: a railroad model simulation that includes tracks and trains. The visualization is ASCII-based, so feel free to unl...

The Dining Philosophers

In this post, we will introduce the concept of co-routines and how they are handled in PicoLisp. This will be the foundation for the subsequent posts on Discrete Event Simulation and finally a couple of examples, including an ASCII model railway simu...

One of the many nice things of PicoLisp is that it comes with a built-in database. This makes it easy to collect and store our own data, for example via a mobile app. But not all data is best represented in tables and lists: sometimes a colorful litt...

In the last two posts, we have built a little app that shows the current location and displays the nearest ice cream shops: https://picolisp-explored.com/an-openstreetmap-app-written-in-picolisp-pt-1 https://picolisp-explored.com/an-openstreetmap-ap...

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.
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.
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.
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 😋).
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!
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.
https://www.ionos.com/digitalguide/websites/web-development/imperative-programming/
Icons made by Becris from www.flaticon.com