How to use Pilog in PicoLisp

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

In the last seven posts we introduced the main concepts and syntax of Pilog. However, all the queries were only done in interactive mode from the REPL. So how do we use it from PicoLisp?
The interface from PicoLisp to Pilog is via the functions goal (prepare a query from PicoLisp data) and prove (return an association list of successful bindings), and the application level functions pilog and solve. It is often used for database queries.
The opposite case - calling PicoLisp functions from Pilog - is done with the caret symbol ^ in the CAR of a Pilog clause and a PicoLisp programm body in the CDDR, and then unified with the result in the CADR. We will see now how this can look like.
The factorial function of an integer Nis defined as N * N-1 * N-2 * ... * 1. Let's see how we can define it in Pilog. The factorial function can be implemented recursively (in this post we discussed the PicoLisp implementation).
The base case is for N=0. In this case, the result is 1. With this the calculation can stop, so we can use the cut operator T after this clause.
(be factorial (0 1) T)
For the other cases we can define it recursively. Let's state a rule (be factorial (@N @X) with three conditions:
@N-1 to @A,@A to bind @B to a value,@X as @N * @B.(be factorial (@N @X)
(^ @A (dec @N))
(factorial @A @B)
(^ @X (* @N @B)) )
(dec @N) and ( * @N @B) are PicoLisp functions.
We can call a PicoLisp function using a list with three ore more elements: The first element is the caret symbol ^, the second (CADR) is the variable, and the third (CDDR) is the program to be evaluated).
Let's test it in the REPL:
: (be factorial (0 1) T)
-> factorial
: (be factorial (@N @X)
(^ @A (dec @N))
(factorial @A @B)
(^ @X (* @N @B)) )
-> factorial
: (? (factorial 5 @X))
@X=120
-> NIL
It works!
Now let's use it from within PicoLisp. We can call Pilog predicates with the solve function:
(solve 'lst [. prg]) -> lstEvaluates a Pilog query and, returns the list of result sets. If
prgis given, it is executed for each result set, with all Pilog variables bound to their matching values, and returns a list of the results.
(println
(car
(solve '((factorial 5 @X)) @X) ) )
Let's test it:
$ pil factorial.l
120
If we execute factorial.l, it prints 120.
You can find the factorial.l file here.
Next, we will look at a couple of pilog examples from the Rosetta Code:
After that we will introduce Pilog as query language for the PicoLisp database.
https://software-lab.de/doc/ref.html#pilog