top | item 40996641

(no title)

evgskv | 1 year ago

I think of Prolog as a general purpose logic programming language and Datalog to be logic programming more focused on data analysis. Data analysis is a very large area, so boundary might get blurry at times.

If your data is in a relational database consider Logica - a Datalog family language that compiles to SQL and runs naturally on SQLite, Postgres, DuckDB and Google BigQuery.

Easy to install, easy to play with in CoLab or any other Jupyter notebook.

Works for data analysis (aggregation, filtering etc) that is commonly associated with SQL, as well as recursive logical querries commonly associalted with Logic programming per-se.

Here is what it looks like for a data-analysis-ish query of finding popular baby names over time:

# Count babies per year.

NameCountByYear(name:, year:) += number :- BabyNames(name:, year:, number:);

# For each year pick the most popular.

TopNameByYear(year) ArgMax= name -> NameCountByYear(name:, year:);

# Accumulate most popular name into a table, dropping the year.

PopularName(name: TopNameByYear());

The classic grand-parent rule looks as usual:

Grandparent(a, c) :- Parent(a, b), Parent(b, c);

Here is a recursive program for finidng distances in a directed graph:

D(a, b) Min= 1 :- Edge(a, b);

D(a, b) Min= D(a, x) + D(x, b);

Links to CoLabs:

Grandparent, ancestor: https://colab.research.google.com/drive/1lujnnUOXsF6VrC9__jV...

Distance in graph:

https://colab.research.google.com/drive/1sOCODHqN0ruxZSx_L-V...

Github repo: https://github.com/EvgSkv/logica

discuss

order

No comments yet.