Real World Programming in SWI-Prolog

luu | 112 points

The golden language at my current place of work is Python. But we have some pretty gnarly business logic in some places (decision tree of average height 10 and branching factor of maybe 3).

To better understand how all the pieces fit together, I took on the challenge of writing the decision tree out in SWI-Prolog over the weekend, since I figured it would be the most “expressive” way to capture the logic.

I’d taken a PL class in college, and used prolog for a couple weeks therein. But this was the first time I’d used it in years. The first 30 minutes were a struggle, as I tried to remember how predicates worked, and tried to translate my mental model into a series of iteratively applied predicates. But after a couple hours of very satisfying top-down programming, I’d written pretty much the whole thing.

And it worked!

My grand theory is that we’d be able to get compliance, legal, and finance (main stakeholders of the program), to interface with some exposed subset of the predicates, to tweak to their hearts content.

This program could be the Golden source of truth that all implementations would have to live up to. Maybe, we could even hook in python programs to do I/O (fetch inputs from the databases, write decisions to the database), and use this prolog program directly to drive decisions in a python program.

It’s all a fantasy at this point, but it’s a fun one. The idea’s lingered since that weekend, many months ago.

Has anybody pulled off anything similar? Replacing a complex component of a bigger program with a prolog core?

gen220 | 4 years ago

I heartily recommend "The Search Space" podcast to anyone interested in learning more about logic programming. The host is very good at introducing concepts in an easily understandable manner, and at chaining them in the right order so that you don't feel overwhelmed. I'm a complete noob, plus not a native english speaker, yet I didn't feel the need the rewind things at any point while listening to it.

https://thesearch.space/

prrls | 4 years ago

I have many Prolog books and by far the most practical or "real world" of them is "Prolog Programming in Depth" by Covington/Nute/Vellino, which the lead author has made freely available as a PDF:

http://www.covingtoninnovations.com/books/PPID.pdf

It doesn't shy away from explicitly discussing how one goes about expressing various procedural/imperative constructs in a declarative manner.

daviddaviddavid | 4 years ago

I love Prolog -- the problem I find is that it makes hard stuff easy, but things that should be easy, hard - most of which comes from abuse of backtracking. When you realize that default Prolog SLD resolution is just a depth-first search and that every predicate you define really has a implicit "foreach" in front of it, it's easier to wrap your brain around. To get efficiency however, you need to embrace logical vars and use them as "holes" in data-structures (such as diff-lists) that will be filled in later, which does require some restructuring of your thinking sometimes.

thelazydogsback | 4 years ago

A buddy worked at the University of Amsterdam (closely associated with swi of swi-prolog fame) and worked on garp, qualitative modeling software.

Their codebase was tens of thousands of lines of code, most of it was pretty readable.

Do not make a gui in prolog though, you will regret it. It was pretty hard to troubleshoot and maintain at scale. The paradigm does not lend itself to that kind of thing.

Prolog is pretty cool for search and symbolic ai though. It was awesome as a first language in uni.

Okkef | 4 years ago

From my limited experience, prolog is perfect for describing the shape of the valid state space for almost any domain. But its default execution strategy -- just depth-first search -- doesn't enable you to efficiently navigate that state space from diverse perspectives.

infogulch | 4 years ago

Is there a publicly available code repository with a program written in Prolog that does some non-trivial task? #3 looks the closest, but I'm looking for an example of something complete that people are using.

protomyth | 4 years ago

As a total counterpoint, here is a decidedly non-real-world application of Prolog: writing theorem provers in Prolog.

https://link.springer.com/book/10.1007/978-1-4684-0357-2

Tainnor | 4 years ago