Comment by fracus

Comment by fracus 4 days ago

8 replies

I think it would be really impactful to start with a problem and describe how logic programming solves that problem better than the other paradigms.

sirwhinesalot 3 days ago

I mention the intuition in passing (you have an object graph with complex bidirectional and derived relationships). Any example that would truly show the benefit would be too big to show on a blog post. Treat it like the world's smartest database, that's the key.

Another example would be something like an Entity Component System. The moment it starts getting complex (i.e., you have fancy queries and joins), then you're actually implementing a really shitty relational programming engine, and you might as well just implement Datalog instead at that point and reap the benefits.

Other kinds of search problems are probably better tackled by constraint programming instead.

cjonas 4 days ago

The only production experience I have with logic programming is OPA Rego for writing security policies (not sure it's a "pure" logic language but feels like the primary paradigm).

I found it pretty interesting for that use case, although the learning curve isn't trivial for traditional devs.

https://www.openpolicyagent.org/

trealira 4 days ago

I've been reading a bit about it, and it seems easier to make goal-driven backwards chaining AI from it, like the block world example. You could in theory use that for something like a video game AI (like GOAP, Goal-Oriented Action Planning, which is based on STRIPS). Whenever I read about GOAP though, they seem to have used a graphical editor to declaratively input rules rather than a logic programming language.

Note that I'm not an expert in any of this, I've just been reading about this kind of AI recently. I haven't actually done this myself.

dkjaudyeqooe 4 days ago

Generally speaking, the advantage of logic programming is that it's (more) declarative: you describe the problem and it derives a solution.

  • taeric 4 days ago

    Ish? Is only really true if what you are programming can be seen as a search for the completion of a statement?

    For an easy example to consider, what would the logical program look like that described any common fractal? https://rosettacode.org/wiki/Koch_curve#Prolog shows that... it is not necessarily a win for this idea.

    For the general task asked in the OP here, I would hope you could find an example in rosettacode that shows prolog gets a good implementation. Unfortunately, I get the impression some folks prefer code golf for these more so than they do "makes the problem obvious."

    • rabbits77 4 days ago

      I’d argue that is not the most ideal Prolog solution. More like it’s simply a recursive implementation of an imperative solution.

      For fractals you’ll want to be able to recognize and generate the structures. It’s a great use case for Definite Clause Grammars (DCGs). A perfect example of this would be Triska’s Dragon Curve implementation. https://www.youtube.com/watch?v=DMdiPC1ZckI

      • taeric 4 days ago

        I would agree. I was actually hoping to be proven wrong with that example. I have yet to see anything other than a constraint program that looks easier to understand in logic programming, sadly.

        • taeric 3 days ago

          Adding late to this, as I didn't get to actually look at this video yesterday.

          I would still agree that you can do better than the examples I'm finding, but I am not entirely clear why/how the dragon curve is honestly any better here? The prolog, notably, does not draw the curve. Just being used to generate the sequence of characters that describes it. But... that is already trivial in normal code.

          Actually drawing it will get you something like this: https://rosettacode.org/wiki/Dragon_curve#Prolog. Contrast that with the Logo version and you can see how the paradigm of the programming language can make a giant difference in how the code solution looks.