logoalt Hacker News

Prolog Basics Explained with Pokémon

102 pointsby birdculturelast Friday at 11:02 AM16 commentsview on HN

Comments

jodrellblanktoday at 3:10 PM

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

triskatoday at 1:40 PM

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.

lagrange77today at 1:20 PM

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

show 1 reply
macintuxtoday at 12:03 PM

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.

Modified3019today at 11:02 AM

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.

show 2 replies
admeliora01today at 3:01 PM

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.

Almondsetattoday at 11:43 AM

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.)?

zombottoday at 12:38 PM

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

SilentM68today at 1:32 PM

That's very helpful & easy to follow.

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

Sol