Introduction to the PicoLisp Database
Tags: picolisp, lisp, database, functional
Published: 2022-03-09 10:06:20

We have now covered the basics of frontend- and backend development in PicoLisp. To get the full stack, we still need one more thing: the database!


--------------------------

### Data Types in PicoLisp - revisited

 First of all let's clarify some terms that are important to understand the theory and documentation.

We go one step back to the [Concepts and Data Types of PicoLisp](https://picolisp-blog.hashnode.dev/concepts-and-data-types). There we said that there are three data types: Lists, Numbers and Symbols.

What we didn't mention yet was that the **symbols** can be distinguished into four types: ``NIL``, internal, transient and external. All four symbol types are on the same hierarchy level:

```
                       cell
                        |
            +-----------+-----------+
            |           |           |
         Number       Symbol       Pair
                        |
                        |
   +--------+-----------+-----------+
   |        |           |           |
  NIL   Internal    Transient    External
```

----------------------------------------

Let's discuss the four types quickly.

- ``NIL`` is a special symbol which exists exactly once and is used for various purposes (we have seen many of them already) - like representing an empty list or a boolean "false". 
- **Internal Symbols** are basically all symbols that we have seen so far: variable names, function definitions... There cannot be two different symbols with the same name because they are indexed by their name.
- **Transient Symbols** are only interned for a certain time and released after that, which means that they cannot be accessed by their name. For example: anonymous objects without names, text strings...
- And finally **External Symbols**: External symbols reside in an external resource (usually a database file) and are loaded into memory - and written back to the file - dynamically. They are kept in memory ("cached") as long as they are accessible.

----------------------------

### Characteristics of external symbols
 
External symbol names are surrounded by braces (``{}``) and have their location information (disk file and block offset) directly coded into the symbol names. The symbol name cannot be directly controlled by the programmer, and there cannot be two identical symbol names at the same time. 

Like the other data types, also external symbols have a characteristic tag bit so that the interpreter recognizes them.

-----------------------------

External symbols are on the same hierarchy level als internal symbols which means they are **first-class data types**: They can be treated just like any other object we have covered so far (passed around, returned from functions and so on). Unlike internal symbols, the external symbols still exist after the parent process is stopped. This is called **persistence**.

-----------------------------

### The PicoLisp Database

Enough for the theory - so how is that related to the PicoLisp database? 

Besides that PicoLisp has external symbols as first-class data types, it also has a built-in framework that allows a close mapping of the application data structure to the database.

The PicoLisp database framework supports several database paradigms:

- **Object-Oriented**: The PicoLisp database directly supports object-oriented programming, which means that the application objects can be easily persisted.
- **Document-based**: Each record can have its own specific schema, similarly to the folder-structure on a PC.
- **Graph-based**: The records can be directly interlinked to define the relationships between the entities.

Usually records (i. e. symbols) in the database are objects derived from the pre-defined class ``+Entity``. Entities can have relationships that define the model, like for example:

- scalar (like "is a string", "number", "date"...),
- object-to-object (uni-, bidirectional or hooks),
- containers for other entities (like lists),
- various indexing possibilities (unique keys, full-text...),
- Blobs (binary large objects).

We will see many examples of this in the next posts. It will be helpful to know the basics of [OOP in PicoLisp](https://picolisp-blog.hashnode.dev/picolisp-explored-object-oriented-programming-part-1) as we will see many of the OOP syntax and principles there.

-----------------

### Why should I use the PicoLisp database?

*Source: [The PicoLisp database vs. ORMs](https://picolisp.com/wiki/?pilvsorm).*

The application layer and database layer in PicoLisp are on the same layer, which means there is no "translation system" in between. This has several advantages:

- **Higher productivity**: The "object" or "table" definition needs to be done only once (instead of several times, like in the databasse schema, application class, business class...),
- **Less code, less space for bugs**,
- **Simpler mental model**,
- **Performance increase** since the data is cached on the application layer
- **full [ACID support](https://en.wikipedia.org/wiki/ACID)** (atomicity, consistency, isolation, durability).

----------------------------

In the next couple posts, we will discuss show the syntax and structure and work through a small example. We will also need to take a look into Prolog and its PicoLisp version "pilog". 

After that we will return to the Web Application tutorial to complete the To-Do App to a full-stack example with user authentification and more.


-----------------------------

### Sources
- [The Picolisp database vs. ORMS](https://picolisp.com/wiki/?pilvsorm)  
- https://software-lab.de/doc/ref.html#dbase  
- https://www.mongodb.com/databases/what-is-an-object-oriented-database