Prolog Basics Explained with Pokémon

42 points by cceckman a day ago on lobsters | 12 comments

vrolfs | 16 hours ago

I would be curious to hear about anybody using Prolog productively (at work or for personal use). I have only ever seen toy examples like this one.

jamesnvc | 14 hours ago

I like Prolog quite a bit (I’m the author of what I believe is the most popular language server implementation for Prolog) and use of for lots of little scripting things. I technically have at least one thing in production in Prolog, an internal analytics dashboard; in the past, I had the backend server for an iOS app written in Prolog too (which led me to write an HTTP/2 client library for the language, so I could send APNS notifications without external services).

apetros | 10 hours ago

I don't know if this will be a satisfying answer to your question, but since writing this blog post I find myself reaching for Prolog in cases that are entirely orthogonal to any kind of code I used to write before. There are definitely people in the Prolog community who like using it for web servers or whatnot, but for me personally it's more about unlocking a different skill tree. For instance, I've become better at identifying where custom parsers or DSLs might be a good fit for a problem.

After writing this blog post, I used the knowledge gained to re-implement a useful subset of the IRS Fact Graph (its tax logic engine). Prolog is an amazing tool for that job, because it surfaces and forces resolution of poorly-documented corners in ways that an imperative implementation simply can't. Even though I haven't quite finished the "execution" part, the parsing is complete enough that I was able to write up a pretty good first-draft of the documentation. It's missing one major feature (date arithmetic), but once that's in I will write it up.

With DCGs, Prolog is fabulous at parsing complex structured text. Where before my brain would go to Python or JS after something got too complicated for awk, now Prolog is a great fit when structure and discipline are required. There's something satisfyingly old-school about writing complex codebases that fit in one file (but still not APL-like in terseness).

I'll also throw out there that while my example is trivial, the Pokemon case is not, and most of my trivial example is possible because there exists an extremely thorough implementation of the hilariously complex battle mechanics. I am intrigued by—and have taken tentative steps towards implementing—a Prolog rules engine that can do some of what the existing tooling does, the benefit being that it could surface possibilities through DFS more easily and maintainably than imperative code.

quasi_qua_quasi | an hour ago

One of my favorite things about Pokemon Showdown is that they have the battle mechanics for each generation. This includes stuff like "a fencepost error means that in Gen 1, every move has a 1/256 chance to miss". IIRC they even have a message for that so you don't think it's a bug in Showdown. It's an amazing project.

I've used it occasionally for program analysis. A decent number of tools use Datalog to store information about programs and allow users to write queries over them, and I'll occasionally use ones that provides actual SWI-Prolog instead so you can do backtracking or functions.

aarroyoc | 6 hours ago

My blog is generated with Prolog, here's the source code. The docs of Scryer Prolog are also generated with a Prolog program that I called DocLog. I also did some libraries that these programs use. SWI Prolog also dogfoods SWI in its website, the Web IDE called Swish, the ClioPatria server,...

Screwtape | 23 hours ago

I have used SQLite as a kind of "type-safe spreadsheet" to keep information organised, even without any kind of CRUD interface built on top of it. It's not always the prettiest language, though. I spent some time looking into Datalog, but most of the implementations seem to be something you'd embed in a larger program, rather than something you could use for recording handy information like in this article.

Maybe Prolog is actually the tool to use?

aarroyoc | 16 hours ago

You have Mangle and Datomic as Datalog external databases, which may fit your bills but I don't have any experience. Prolog is a superset of Datalog and can do everything Datalog does and more. But sometimes the "more" is problematic as now you have a Turing complete language, which may not finish, which may be a bit harder to reason with, which could be be used in insecure ways, etc

Datalog often has better performance too because you can take shortcuts based on its limitations.

Lastly in Prolog data equals code (it's homoiconic) but that means that Create/Update/Delete operations are very much modifying the source code. This can be done dynamically in fact, but don't expect a nice flow, it still needs some manual sync between files and the loaded program .

patrickdlogan | 8 hours ago

Production-ready Prolog implementations such as SWI can and should be used to access databases just as with any other programming language. Also SWI has access to the file system and can be used to implement a database such as ClioPatria, a triple store with support for queries as well as updates.

aarroyoc | 6 hours ago

Yes, of course. What I was trying to say is that the the native way to do "inserts" in Prolog is with the assertz/1 predicate, but it only modifies the runtime data, it doesn't sync with the filesystem (there's library(persistency) in SWI that tries to add that behavior). Of course you can directly manipulate the file system as with any other general purpose programming language, that's what I referred as manual. If you compare it with SQL, it's too low level.

daveliepmann | 14 hours ago

Also Datalevin

I would love to get deeper into Prolog and use it for instruction set queries.

but modelling logic with quantities and bit-level viewed integers is quite hard