HackerTrans
TopNewTrendsCommentsPastAskShowJobs

evgskv

no profile record

comments

evgskv
·hace 2 años·discuss
Ah, yes, you're right! Please do:

  # ...
  Q(your_column) :- example_db.YourTable(your_column:);
You can query multiple columns of course. Feel free to start threads in Discussions of the repo with whatever questions you have!
evgskv
·hace 2 años·discuss
In my opinion any analytical query is easier to read in logic language than in SQL. But it's most obvious for recrusive querries. E.g. distance in graph defined by predicate (aka table) G written in Logica looke like:

  D(a, b) Min= 1 :- G(a, b);       # Connected by an edge => distance 1.
  D(a, c) Min= D(a, b) + D(b, c);  # Triangle inequality.
 
It will be much harder to read with SQL CTE. It can also computed over weighted graphs, which is impossible or extremely hard with SQL.

In practice you rarely need recursive querries, so gap between Logica and SQL isn't as large as it is here, but Logica is easier to read (in my opinion) for similar reasons.
evgskv
·hace 2 años·discuss
To connect to a database file use:

  @AttachDatabase("db_prefix", "your_file.db");
  # Then you can query from it:
  Q(..r) :- db_prefix.YourTable(..r);
evgskv
·hace 2 años·discuss
There is a standard method of optimization - breaking predicate into smaller ones and saving intermediates into database.

Typically program runs efficiently, but when optimization is needed - you can do it by breaking up the predicate.
evgskv
·hace 2 años·discuss
Yeah, we need better tutorials.

To use SQLite use @Engine("sqlite") imperative. And you can then connect to you database file with @AttachDatabase imperative.

For example if you have example.db file with Fruit table which has col0 column, then you can count fruits with program:

@Engine("sqlite"); @AttachDatabase("example", "example.db");

CountFruit(fruit) += 1 :- Fruit(fruit);

Then run CountFruit predicate.
evgskv
·hace 2 años·discuss
yes.
evgskv
·hace 2 años·discuss
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