←back to thread

171 points voat | 4 comments | | HN request time: 0.277s | source
1. avodonosov ◴[] No.42160024[source]
> Composite(a * b) distinct :- ...

Wait, does Logica factorize the number passed to this predicate when unifying the number with a * b?

So when we call Composite (100) it automatically tries all a's and b's who give 100 when m7ltiplied

I'd be curious to see the SQL it transpiles to.

replies(1): >>42162549 #
2. puzzledobserver ◴[] No.42162549[source]
As someone who is intimately familiar with Datalog, but have not read much about Logica:

The way I read these rules is not from left-to-right but from right-to-left. In this case, it would say: Pick two numbers a > 1 and b > 1, their product a*b is a composite number. The solver starts with the facts that are immediately evident, and repeatedly apply these rules until no more conclusions are left to be drawn.

"But there are infinitely many composite numbers," you'll object. To which I will point out the limit of numbers <= 30 in the line above. So the fixpoint is achieved in bounded time.

Datalog is usually defined using what is called set semantics. In other words, tuples are either derivable or not. A cursory inspection of the page seems to indicate that Logica works over bags / multisets. The distinct keyword in the rule seems to have something to do with this, but I am not entirely sure.

This reading of Datalog rules is commonly called bottom-up evaluation. Assuming a finite universe, bottom-up and top-down evaluation are equivalent, although one approach might be computationally more expensive, as you point out.

In contrast to this, Prolog enforces a top-down evaluation approach, though the actual mechanics of evaluation are somewhat more complicated.

replies(2): >>42169032 #>>42169077 #
3. avodonosov ◴[] No.42169032[source]
Bottom-up? Ok, I see. From the tutorial it seems Logica reifies every predicate into an actual table (except for what they call "infinite predicates")

I found a way to look at the SQL it generates without installing anything:

Execute the first two cells in the online tutorial collab (the Install and Import). Then replace the 3rd cell content with the following and execute it:

    %%logica Composite

    @Engine("sqlite"); # don't try to authorise and use BigQuery

    # 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);

Look at the SQL tab in the results.
4. avodonosov ◴[] No.42169077[source]
Where does your intimate knowledge of Datalog comes from? Do you use Datalog regularly (maybe with Datomic)? Or you just studied how it is implemenced out of curiosity?