Live data from Hacker News

Prolog Basics Explained with Pokémon

unplannedobsolescence.com

11–20 of 64 posts

Re: Prolog Basics Explained with Pokémon

#12
Very nice!

In the Scryer Prolog discussions, Alex has shared a few ideas and considerations for possible improvements to the Prolog code, including the use of metaprogramming to automatically generate more general relations:

https://github.com/mthom/scryer-prolog/discussions/3221

I hope for an interesting followup article!

Another very interesting Prolog program by Alex is factgraph.pl:

https://github.com/alexpetros/factgraph.pl

It's a Prolog implementation of the IRS Fact Graph, an application of Law as Code.

Re: Prolog Basics Explained with Pokémon

#13
post #5

Earlier quoted context omitted.

All examples shown in the article can be ran with Datalog too (with stratified negation and arithmetic comparison), which has a clearer execution model and looks almost identical to Prolog. Prolog underneath is doing backtracking, while Datalog is finding a least fixed point of derived relations where iterating on data won't produce more relations, and is akind to SQL (but usually stronger because of recursion).

Importantly, Datalog is not Turing-complete though.

You can get Turing completeness by wrapping your datalog query in a while loop, so that's not particularly restrictive.

Re: Prolog Basics Explained with Pokémon

#14

Was initially nonplussed, but toward the end I realized the choice of pokemon for an example actually works out well for showing how prologue can solve problems. I’m now a bit curious about trying it out somewhere.

Prolog is actually a perfect fit for all kinds of adventure, role playing, strategy, and classic board/card games, with clauses representing game rules and facts representing the game state and universe in the most natural way.

Simple general-purpose opponents can be coded using just recursive backtracking search, while more advanced ones (supporting moves that need to destructively change state) can still be conveniently modelled by reifying facts and thereby enable backtracking over assert/retract-like Prolog DB modifications, as used in discrete combinatorial planners [1].

[1]: https://quantumprolog.sgml.net/container-planning-demo/part1...

Re: Prolog Basics Explained with Pokémon

#15
Love this use case, makes me want to implement something similar for Magic the Gathering. I love using scryfall, but I think a more cli first approach with descriptive rules would suffice much better for brewing in eternal formats like Commander with ever growing card pools. I mostly work off of keyword search.

Re: Prolog Basics Explained with Pokémon

#16

Earlier quoted context omitted.

Importantly, Datalog is not Turing-complete though.

You can get Turing completeness by wrapping your datalog query in a while loop, so that's not particularly restrictive.

In the case of Datalog, it not being Turing-complete is usually seen as a feature rather than restrictive.

Re: Prolog Basics Explained with Pokémon

#17
> "Don't be bothered with by the fact that the solutions end with "or false" here. It's a function of how the search algorithms work; the solver looked for more solutions, then failed. I'll admit, I don't totally understand why it only sometimes does this, but it's expected."

I think this is explained in The Power of Prolog[1] that the answers coming from Prolog are not printing text to a terminal, they are valid Prolog terms(/data/code). That's why the result uses the same `;` for OR as code does. Answer (x ; y ; false) is "query can be answered by x or y or no other answer found". (This would let you do meta-programming, reasoning about the results and rewriting the results in a LISPy data-as-code way, if you were more advanced than I am).

Prolog systems do optimisations to jump to the correct answer without searching, if they can, (e.g. database style indexing on the facts and rules) and in those cases there is no code left to search after showing the first answer, no need to prompt the user "should I search for more answers in the remaining code?", and so no need for an output "false" to say "I finished searching and found no more solutions".

[1] https://www.metalevel.at/prolog

Re: Prolog Basics Explained with Pokémon

#19

Was initially nonplussed, but toward the end I realized the choice of pokemon for an example actually works out well for showing how prologue can solve problems. I’m now a bit curious about trying it out somewhere.

Nonplussed like initially surprised? It does not mean bored or nonchalant which many people seem to think, probably due to the non- prefix.

Re: Prolog Basics Explained with Pokémon

#20
> Then query it like so:

    SELECT DISTINCT pokemon, special_attack
    FROM pokemon as p
    WHERE
      p.special_attack > 120
      AND EXISTS (
        SELECT 1
        FROM pokemon_moves as pm
        WHERE p.pokemon_name = pm.pokemon_name AND move = 'freezedry'
      )
      AND EXISTS (
        SELECT 1
        FROM pokemon_types as pt
        WHERE p.pokemon_name = pt.pokemon_name AND type = 'ice'
      );
Hmm. I wonder if this

    SELECT DISTINCT pokemon, special_attack
    FROM pokemon as p
      NATURAL JOIN pokemon_moves as pm
      NATURAL JOIN pokemon_types as pt
    WHERE
      p.special_attack > 120 AND
      pm.move = 'freezedry' AND
      pt.type = 'ice'
    ;
would work instead.
Post reply on HN