Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Are there any particularly excellent examples of prolog implemented as a library you could point us to?


As an example of a use case, "Gerrit Code Review"[1] is written in Java and uses prolog for the submit rules.[2]

I haven't looked into the implementation. But taking a brief glance now, it looks interesting. They appear to be translating Prolog to Java via a WAM representation[3]. The compiler (prolog-cafe) is written in prolog and bootstrapped into Java via swi-prolog.

I don't know why compilation is necessary, it seems like an interpreter would be fast enough for that use case, but I'd love to take it apart and see how it works.

[1]: https://www.gerritcodereview.com/ [2]: https://gerrit-documentation.storage.googleapis.com/Document... [3]: https://gerrit.googlesource.com/prolog-cafe/+/refs/heads/mas...


Not exactly prolog, but logic programming implemented as a library/DSL: https://minikanren.org/


If it's a DSL then it's "writing a new language" and you're just calling from a native API, no?


Embedded DSLs are libraries. They are the epitome of the advice to write a library. It just so happens that the library abstractions could map to a language if you wanted to add parsing and all of the other machinery.


Clojure's core.logic is based on minikanren, so that fits the bill.


My time to shine! https://news.ycombinator.com/item?id=45902088

References were Racket with the Racklog library¹. There's also Datalog² and MiniKanren, picat, flix. There were tons of good comments there which you should check out, but PySwip seemed like "the right thing" when I was looking at it: https://github.com/yuce/pyswip/

...documentation is extremely sparse, and assumes you already know prolog, but here's a slightly better example of kindof the utility of it:

https://eugeneasahara.com/2024/08/12/playing-with-prolog-pro...

...ie:

    # ya don't really care how this works
    prolog.consult("diabetes_risk.pl")

    # ...but you can query into it!
    query = "at_risk_for_diabetes(Person)"
    results = list(prolog.query(query))
...the point being there's sometimes some sort of "logic calculation that you wish could be some sort of regex", and I always think of prolog as "regexes for logic".

One time I wished I could use prolog was trying to figure the best match between video file, format, bitrate, browser, playback plugin... or if you've seen https://pcpartpicker.com/list/ ...being able to "just" encode all the constraints, and say something like:

   valid_config = consult("rules.pl")
      + consult("parts_data.pl")
      + python.choice_so_far(...)

   rules.pl: only_one_cpu, total_watts < power_supply(watts)
   parts_data.pl: cpu_xyz: ...; power_supply_abc: watts=1000
   choices: cpu(xyz), power_supply(abc), ...
...this is a terribly syntactically incorrect example, but you could imagine that this would be horrific code to maintain in python (and sqrt(horrific) to maintain in prolog), but _that's_ the benefit! You can take a well-defined portion and kindof sqrt(...) the maintenance cost, at the expense of 1.5x'ing the number of programming languages you need to expect people to know.


not prolog but logical programming in python: https://sites.google.com/site/pydatalog/


not OP, but for clojure : https://github.com/bobschrag/clolog




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: