Prolog Basics Explained with Pokémon

unplannedobsolescence.com

169 points by birdculture 2 days ago


jodrellblank - 6 hours ago

> "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

triska - 7 hours ago

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.

macintux - 9 hours ago

It continues to be immensely surprising to me that Joe Armstrong was able to write the initial Erlang implementation in Prolog. I wish I’d asked him about getting a copy of the source code.

lagrange77 - 8 hours ago

When i was in uni, the course teaching Prolog and Lisp was called "Artificial Intelligence for Engineers".

Joker_vD - 4 hours ago

> 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.
Modified3019 - 10 hours ago

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.

stellartux - 5 hours ago

If this is your article, you have a typo in learns_priority/3, "move_priority #> 0" should be "P #> 0".

Almondsetat - 9 hours ago

Are there public tournaments of games like Pokemon where contestants have to compete with eachother using a specific class of algorithms (e.g., logic programming, neural nets, linear programming, etc.)?

fl1pper - 4 hours ago

I'm not familiar with Pokemon universe :( Can somebody please explain Pokemon using Prolog?

admeliora01 - 6 hours ago

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.

zombot - 8 hours ago

Are there pokémon with backtracking and unification traits? Those could do real Prolog!

SilentM68 - 7 hours ago

That's very helpful & easy to follow.

Do you have an Odin tutorial that's as easy to digest?

Sol