Skip to main content

Logica language.

Project description

Logica: language of Big Data

Logica is an open source declarative logic programming language for data manipulation. Logica is a successor to Yedalog, a language created at Google earlier.

Why?

Logica is for engineers, data scientists and other specialists who want to use logic programming syntax when writing queries and pipelines for databases and datawarehouses. Logica programs run on BigQuery, Postgres and SQLite.

Logica compiles to SQL and gives you access to the power of SQL ecosystem with the convenience of logic programming syntax.

This is useful because SQL enginers are magnitudes more powerful than state of the art native logic programming engines. For example, BigQuery is a distributed datawarehouse and thus logic programs written in Logica can be easily parallelized onto thousands of servers. Postgres and SQLite are among most popular databases, they are capable of processing substantial volumes of data right on your machine.

We encourage you to try Logica, especially if

  • you already use logic programming and need more computational power, or
  • you already have data in BigQuery, PostgreSQL or SQLite, or
  • you want to learn logic programming and apply it to processing of Big Data.

Support for more SQL dialects and engines is coming in the future.

I have not heard of logic programming. What is it?

Logic programming is a declarative programming paradigm where the program is written as a set of logical statements.

Logic programming was developed in academia from the late 60s. Prolog and Datalog are the most prominent examples of logic programming languages. Logica is a language of the Datalog family.

Datalog and relational databases start from the same idea: think of data as relations and think of data manipulation as a sequence of operations over these relations. But Datalog and SQL differ in how these operations are described. Datalog is inspired by the mathematical syntax of the first order propositional logic and SQL follows the syntax of natural language.

SQL was based on the natural language to give access to databases to the people without formal training in computer programming or mathematics. This convenience may become costly when the logic that you want to express is non trivial. There are many examples of hard-to-read SQL queries that correspond to simple logic programs.

How does Logica work?

Logica compiles the logic program into a SQL expression, so it can be executed on BigQuery, the state of the art SQL engine.

Among database theoreticians Datalog and SQL are known to be equivalent. And indeed the conversion from Datalog to SQL and back is often straightforward. However there are a few nuances, for example how to treat disjunction and negation. In Logica we tried to make choices that make understanding of the resulting SQL structure as easy as possible, thus empowering user to write programs that are executed efficiently.

Why is it called Logica?

Logica stands for Logic with aggregation.

How to learn?

Learn basics of Logica with the CoLab tutorial located at tutorial folder. See examples of using Logica in examples folder.

Tutorial and examples show how to access Logica from CoLab. You can also install Logica command line tool.

Prerequisites

To run Logica programs on BigQuery you will need a Google Cloud Project. Once you have a project you can run Logica programs in CoLab providing your project id.

To run Logica locally you need Python3.

To initiate Logica predicates execution from the command line you will need bq, a BigQuery command line tool. For that you need to install Google Cloud SDK.

Installation

Google Cloud Project is the only thing you need to run Logica in Colab, see Hello World example.

You can install Logica command with pip as follows.

# Install.
python3 -m pip install logica
# Run:
# To see usage message.
python3 -m logica
# To print SQL for HelloWorld program.
python3 -m logica - print Greet <<<'Greet(greeting: "Hello world!")'

If your PATH includes Python's bin folder then you will also be able to run it simply as

logica - print Greet <<<'Greet(greeting: "Hello world!")'

Alternatively, you can clone GitHub repository:

git clone https://github.com/evgskv/logica
cd logica
./logica - print Greet <<<'Greet(greeting: "Hello world!")'

Code samples

Here a couple examples of how Logica code looks like.

Prime numbers

Find prime numbers less than 30.

Program primes.l:

# Define numbers 1 to 30.
Number(x + 1) :- x in Range(30);

# Defining composite numbers.
Composite(a * b) distinct :- Number(a), Number(b), a > 1, b > 1;

# Defining primes as "not composite".
Prime(n) distinct :- Number(n), n > 1, ~Composite(n);

Running primes.l

$ logica primes.l run Prime
+-------+
| prime |
+-------+
|     2 |
|     3 |
|     5 |
|     7 |
|    11 |
|    13 |
|    17 |
|    19 |
|    23 |
|    29 |
+-------+

Cities with largest beer variety

Let's use beer variety dataset from plotly.

Let us find top 5 states with largest variety of beers. In each state we will pick city with the largest variety in the state.

Program beer.l:

@Engine("duckdb");

@Ground(Beer);
Beer(..r) :- 
    `('https://github.com/plotly/datasets/blob/master/beers.csv?raw=true')`(..r);

BeersInState(state) += 1 :- Beer(state:);
BeersInCity(state, city) += 1 :- Beer(state:, city:);

ArgMax5(x) = ArgMaxK(x, 5);
BestCityForBeer(state:, city:,
                city_beers: BeersInCity(state, city),
                state_beers: BeersInState(state)) :-
    state in ArgMax5{s -> BeersInState(s)},
    city = ArgMax{c -> BeersInCity(state, c)};

Running beer.l:

# logica beer.l run BestCityForBeer
+-------+--------------+------------+-------------+
| state | city         | city_beers | state_beers |
+-------+--------------+------------+-------------+
| IN    | Indianapolis | 43         | 139         |
| CO    | Boulder      | 41         | 265         |
| CA    | San Diego    | 42         | 183         |
| TX    | Austin       | 25         | 130         |
| MI    | Grand Rapids | 66         | 162         |
+-------+--------------+------------+-------------+

Feedback

Feel free to create github issues for bugs and feature requests.

You questions and comments are welcome at our github discussions!


Unless otherwise noted, the Logica source files are distributed under the Apache 2.0 license found in the LICENSE file.

This is not an officially supported Google product.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

logica-1.3.14159265358.tar.gz (130.0 kB view details)

Uploaded Source

Built Distribution

logica-1.3.14159265358-py3-none-any.whl (175.7 kB view details)

Uploaded Python 3

File details

Details for the file logica-1.3.14159265358.tar.gz.

File metadata

  • Download URL: logica-1.3.14159265358.tar.gz
  • Upload date:
  • Size: 130.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.6

File hashes

Hashes for logica-1.3.14159265358.tar.gz
Algorithm Hash digest
SHA256 042de98f2e6375eff6316bd1da831b962d02e06b2c57b85d210f361e97364403
MD5 96f013691da29958da2591390f2510ba
BLAKE2b-256 a3e15f34fe12ee23ca0d0a7bc6534c82ca5f65955f0919be14be4b28858be297

See more details on using hashes here.

File details

Details for the file logica-1.3.14159265358-py3-none-any.whl.

File metadata

File hashes

Hashes for logica-1.3.14159265358-py3-none-any.whl
Algorithm Hash digest
SHA256 e5ac40ab6f25fd99ea7e797df4d8d0c97f2e70cf055a0d3bb7d99467a6371264
MD5 2e03ff476c32746d18c16a578b9b3442
BLAKE2b-256 f66ea2f2cfa39e57b94a70a87a7498460241f3a30f255a692ac90daeb6adb3bc

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page