\

Prolog Coding Horror

139 points - yesterday at 9:15 PM

Source
  • schmuhblaster

    today at 2:29 AM

    As someone who has developed a somewhat weird obsession with Prolog, I can highly recommend Markus Triska's other articles on Prolog. His article on meta-interpreters [0] was particularly inspiring for me.

    [0] https://www.complang.tuwien.ac.at/ulrich/prolog_misc/acomip....

    • MikeNotThePope

      today at 6:19 AM

      I haven’t used Prolog, but I have a little experience with Erlang and a lot with Elixir. As I understand it, the early versions of Erlang were inspired by Prolog.

      For those with familiarity with both Prolog and Erlang, can you comment on the similarities and differences between? Is/was Erlang basically Prolog with OTP bolted on?

        • xlii

          today at 7:16 AM

          Super simple example

              % Prolog
              next_light(green, yellow).
              next_light(yellow, red).
              next_light(red, green).
          
              % Erlang
              next_light(green)  -> yellow;
              next_light(yellow) -> red;
              next_light(red)    -> green.
          
          Notable differences:

          - `;` in Erlang indicates multi clause function

          - In Prolog next_light is database, in Erlang it's just a function

          Question: What's the light before green?

              % Erlang
              % Returns actual value
              prev_light_search() -> 
              [State || State <- [green, yellow, red], next_light(State) == green].
          
              % Prolog (?- means that it's in query mode)
              ?- next_light(Light, green).
          
          
          So syntax IS similar though the thing is that Prolog is more like binding and quering database and Erlang is executing function.

          In a nutshell Erlang is more like: "when I have X, then I can calculate Y" and Prolog like "If I want Y, what's the X".

          • Antibabelic

            today at 7:00 AM

            Erlang has very little in common with Prolog, which is a language in an entirely different paradigm (logic programming).

            Early versions of Erlang were implemented in Prolog, which is why Erlang's syntax looks a whole lot like Prolog's, but beyond that they're not very similar.

            • kunley

              today at 7:59 AM

              What Joe Armstrong et al took from Prolog is mainly the syntax.

              • jb1991

                today at 7:10 AM

                These two languages are completely different paradigms.

            • appil

              yesterday at 10:13 PM

              What do people use Prolog for in the real world? I learned about it on a university course and it seems so esoteric compared to other things on the course. Like something invented just for computer scientists to enjoy.

                • crustycoder

                  today at 9:17 AM

                  I've just rolled out an internal SWI Prolog app that is similar to one linked to elsewhere in the thread [1]. We have a large Cloud estate with 10s of thousands of resources in it. Detecting unused or misconfigured resources manually isn't practical, and there are significant cost savings to be had by cleaning things up. The Prolog app reads in JSON resource snapshots, creates an in-memory database of facts from it and then applies rules to detect issues. Most of the rules are simple and the ones for detecting unused resources (3 LOC) or resources that reference other non-existent resources (7 LOC) are entirely generic. There's also link metadata that models the possibility of links existing between resource types, even if they aren't always there in practice

                  There's TUI that allows querying of issues and the resource hierarchy. Issues can also be output as JSON which is fed into a LLM to produce cleanup actions and management reporting.

                  The Prolog app is very fast, considering what it's doing, largely because it makes heavy use of tabling so once an issue has been detected it's not recomputed when queries are made.

                  [1] https://web.archive.org/web/20190525163234/https://dev.to/da...

                  • xlii

                    today at 7:28 AM

                    My last use case: Testing Scenario Generator.

                    I have a application that has actions and actions can happen pretty much in any order. By default all scenarios should be an error except for ones that are in the boundary of logical steps, e.g.

                       login  -> ...[!logout]... -> logout
                       login  -> ...[!logout]... -> dashboard
                       upload -> ...             -> view_file
                    
                    Of course you can immediately see problem with this scope - what happens when non-logged-in user tries to upload or user logouts after upload etc.

                    So I have ~50 actions, ~30 constraints which generate >200 scenarios which then are transformed into test suite.

                    Yet in short: Prolog is useful everywhere it's simple to express a rule but not that easy to implement it.

                    • DanielHB

                      today at 8:31 AM

                      The JS package manager Yarn had an experimental feature to define dependency constraints using prolog, it made for very concise way to represent the logic.

                      https://v3.yarnpkg.com/features/constraints

                      It never got released for good though. I actually had need of such feature for a project but I thought that using an exoteric programming language and an experimental feature was a bit much. I ended up setting up those constraints as a CI check hand-made script and the code was surprisingly large (~300 lines), but not that hard to understand.

                      • _flux

                        today at 7:36 AM

                        Many years ago Maemo (the mobile OS from Nokia) had the profile manager (day mode/night mode etc) written in Prolog. To me it seems like it's a very appropriate application for it.

                        • bmitch3020

                          yesterday at 11:38 PM

                          20+ years ago, it was the backend for the business rules engine that processed various logging and monitoring events. The concept was interesting, the performance was terrible, and businesses mostly didn't want to touch it. After I setup clients with a generic set of rules that worked on Prolog facts, most all of my clients were happy to limit their changes to only those fact files.

                          • fleur-de-lotus

                            today at 6:17 AM

                            Not used. Quote about prolog: The elegant solution is not efficient. The efficient solution is not elegant.

                              • Antibabelic

                                today at 7:48 AM

                                Prolog is an elegant abstraction. One of the points of abstractions is that they let us concentrate our optimization efforts in one place. Prolog benefits from many decades of research into how to make it work fast. When your problem does require a Prolog-shaped solution the most sensible thing to do is to use a highly optimized Prolog system instead of reinventing a naive algorithm yourself. Your "inelegant" solution will not be faster.

                                (This is also the problem with "I'll just quickly implement a Prolog-like DSL when I need it". Sometimes not a bad idea, but you have to be realistic. Your "lightweight" Prolog will be worse in every way compared to serious Prolog implementations).

                                • taneq

                                  today at 7:31 AM

                                  I feel like an inefficient solution is inelegant by definition.

                                    • Smaug123

                                      today at 8:06 AM

                                      Nah - for example, AIXI is so inefficient that it's literally uncomputable, but it is beautiful.

                                      • zingar

                                        today at 8:09 AM

                                        That sounds intuitively right but breaks down when you ask “inefficient at what?”. Are you efficient with CPU cycles or efficient with human working memory?

                                        • Antibabelic

                                          today at 7:40 AM

                                          Richard O'Keefe in The Craft of Prolog: "Elegance is not optional".

                                  • radomir_cernoch

                                    yesterday at 10:33 PM

                                    Some applications were discussed in https://news.ycombinator.com/item?id=40994552

                                    • marhee

                                      today at 7:24 AM

                                      Prolog is for logic what inverse kinematics is for robotics.

                                      Where in imperative programming languages you supply arguments to a function and get a result in Prolog you can give the result of a function call and get all argument sets that lead to that result.

                                      But you are right that in production systems you would seldom see Prolog, for reason that you can easily LLM.

                                      • christophilus

                                        yesterday at 10:30 PM

                                        Dunno about Prolog, but Datomic uses datalog for its query language, and it’s excellent. Datalog is a subset of Prolog.

                                          • ted_dunning

                                            yesterday at 11:12 PM

                                            Datalog may appear to be a subset, but it is quite distinct semantically.

                                            • raffael_de

                                              yesterday at 11:13 PM

                                              What is Datalog used for nowadays?

                                            • gobdovan

                                              today at 1:17 AM

                                              Datalog is not a subset of Prolog. It looks that way because both are based on Horn clause logic, while Prolog is more expressive.

                                              This loops Prolog, but terminates in Datalog:

                                              p :- p.

                                              p.

                                              ?- p.

                                              This is because the underlying mechanism is completely different. Datalog is like SQL with recursion, you start with known facts and repeatedly applies rules to derive all consequences until nothing new appears. In Prolog, you start from the query and works backward through rules until it either finds a proof or fails.

                                              So, Datalog treats Horn clauses as database constraints/inference rules while Prolog treats Horn clauses as a search program. They use the same mathematical substrate, but completely different computational models.

                                          • AdieuToLogic

                                            today at 12:57 AM

                                            > What do people use Prolog for in the real world?

                                            Here[0] is an example of using Ruby and Prolog to solve a real-world AWS management problem.

                                            0 - https://web.archive.org/web/20190525163234/https://dev.to/da...

                                              • jimbokun

                                                today at 3:36 AM

                                                That is brilliant and simple and shows that when Graph Databases were a big thing they probably should have used Prolog as a front end.

                                            • fodkodrasz

                                              today at 7:14 AM

                                              Gerrit had an embedded prolog engine, and I recall branch protection rules / PR workflows were configured using progolog.

                                              • segmondy

                                                today at 12:14 AM

                                                Everything, you heard the joke about those who don't know Lisp end up reinventing it, well, the same can be said for Prolog.

                                              • bandrami

                                                today at 5:54 AM

                                                There was a neat helicopter landing game written in prolog way back in the day

                                                • yesterday at 10:34 PM

                                                  • metaketa

                                                    today at 6:43 AM

                                                    TerminusDb

                                                  • ratew

                                                    today at 8:16 AM

                                                    [dead]

                                                • mmastrac

                                                  yesterday at 11:59 PM

                                                  If you want to understand prolog, you must understand the four-port model:

                                                  https://grack.com/writing/school/enel553/report/prolog.html

                                                    • AdieuToLogic

                                                      today at 12:25 AM

                                                      And to understand the four-port model is to understand solution-space navigation and pruning.

                                                        • cwillu

                                                          today at 12:29 AM

                                                          It's why smartphones lost all their ports: forbidden knowledge must not be leaked to the public.

                                                            • AdieuToLogic

                                                              today at 1:00 AM

                                                              > ... forbidden knowledge must not be leaked to the public.

                                                              Understanding is a personal achievement and has nothing to do with "forbidden knowledge" when the source of said knowledge is both quoted above and freely available.

                                                                • cwillu

                                                                  today at 4:42 AM

                                                                  I fear that the joke didn't land with you.

                                                                    • alecthomas

                                                                      today at 7:45 AM

                                                                      I found it very amusing :)

                                                  • rtpg

                                                    yesterday at 10:56 PM

                                                    There's something quite illuminating with this first "horror", where they basically say "it's OK to report wrong answers, because you can check the answers".

                                                    I don't think I've ever felt like it's OK for my program to provide a list of answers where some are right and some are wrong, but reading this... and generally believing in P != NP.... maybe that's a decent way of looking at some stuff!

                                                      • Zarathustra30

                                                        yesterday at 11:55 PM

                                                        I've actually run into this in the wild, with regards to sales forecasting. A program we were using returned zero if the error bars on a forecast were over 100%. For example, selling somewhere between 1 and 7 units, but averaging 3.

                                                        Returning 3 was "wrong", but infinitely more correct than retuning 0.

                                                        • cwillu

                                                          today at 12:31 AM

                                                          iirc, shor's algorithm for factoring relies on this.

                                                          • DonHopkins

                                                            yesterday at 11:17 PM

                                                            Sometimes the Biorhythm program on my Apple ][ failed to produce correct answers. But it sure was great for impressing cool hippie chicks.

                                                            https://www.youtube.com/watch?v=jYoY1cwAd90

                                                            • hedora

                                                              yesterday at 11:32 PM

                                                              The article server is offline, but I assume they found out that prolog rule evaluation depends on the order the rules are presented in the program.

                                                              If so, the language they thought they were using (and that they should actually use) is datalog, not prolog.

                                                              Datalog has declarative semantics: All facts that are derivable from the base database and the rules will be derived by the interpreter, and it will not add extra hallucinated facts. If that's not true, it's a bug in the runtime, not in the language.

                                                                • cbarrick

                                                                  today at 12:51 AM

                                                                  www.metalevel.at is run by Prolog legend Markus Triska, author of CLP(FD)/CLP(Z).

                                                                  So it's not that they "discovered" anything about Prolog; they already knew the language inside out.

                                                                  This article explains how to appropriately use Prolog declaratively and with full generality.

                                                          • crustycoder

                                                            today at 8:37 AM

                                                            I get what he's saying but I think it's overstated. I'd categorise his list as "Things to be careful with" not "Coding horrors". For example, "The primary means to make your programs defective in this way is to use predicates like assertz/1 and retract/1" is an unqualified statement that makes it sound like you should never ever use them, and that's not the case. I have a real-life Prolog app that applies rules to facts read from JSON data files. I could do that two ways:

                                                            1) Read the JSON with Prolog (there's a library) and assertz() the facts from that, building an immutable database in the first phase before applying the rules in the second phase.

                                                            2) Externally transform the JSON into Prolog facts, load that into the app on startup and apply the same rules to it.

                                                            I agree that mutating the database in the second phase is probably a bad idea, but that's not the same as saying "assertz() always bad". I'd read his site before it appeared on HN and whilst there a lot of very good stuff on it, some of it reminds me of FP purist edicts - fine if you want to go that way and it's appropriate to your problem, but that isn't always going to be the case. That was the basis of my earlier (downvoted) "Mostly overblown" comment.

                                                            But nice to see Prolog mentioned at all on HN :-)

                                                            • alecthomas

                                                              today at 7:36 AM

                                                              I've never seen Prolog used at all in the wild, but OPA (and its ancestor, Datalog) are fairly common.

                                                              • yesterday at 10:34 PM

                                                                • today at 5:26 AM

                                                                  • txhwind

                                                                    today at 3:09 AM

                                                                    now we may have a more powerful "Prolog" - LLM Agent, though not precise and correct somtimes.

                                                                      • xlii

                                                                        today at 7:33 AM

                                                                        It reminds me of an old joke:

                                                                        Radio Yerevan: A listener asks: "Is it true that in Moscow, on Red Square, they are giving away cars?"

                                                                        Our answer: "Yes, it is true. Except it isn't in Moscow, but in Leningrad. And it isn't on Red Square, but on Palace Square. And they aren't cars, but bicycles. And they aren't giving them away, they are stealing them."

                                                                    • crustycoder

                                                                      yesterday at 10:10 PM

                                                                      Mostly overblown.