logoalt Hacker News

bwestergardlast Wednesday at 2:23 PM6 repliesview on HN

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


Replies

dunhamlast Wednesday at 3:06 PM

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...

khaled_ismaeellast Wednesday at 2:27 PM

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

show 2 replies
ramses0last Wednesday at 2:42 PM

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.
exe34last Wednesday at 2:29 PM

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