Top
Best
New

Posted by azhenley 14 hours ago

My Gripes with Prolog(buttondown.com)
125 points | 72 comments
upghost 10 hours ago|
Author: "Not my favorite language"

Prolog: "Mistakes were made"

As an avid Prolog fan, I would have to agree with a lot of Mr. Wayne's comments! There are some things about the language that are now part of the ISO standard that are a bit unergonomic.

On the other hand, you don't have to write Prolog like that! The only shame is that there are 10x more examples (at least) of bad Prolog on the internet than good Prolog.

If you want to see some really beautiful stuff, check out Power of Prolog[1] (which Mr. Wayne courteously links to in his article!)

If you are really wondering why Prolog, the thing about it that makes it special among all languages is metainterpretation. No, seriously, would strongly recommend you check it out[2]

This is all that it takes to write a metainterpreter in Prolog:

  mi1(true).
  mi1((A,B)) :-
          mi1(A),
          mi1(B).
  mi1(Goal) :-
          Goal \= true,
          Goal \= (_,_),
          clause(Goal, Body),
          mi1(Body).
Writing your own Prolog-like language in Prolog is nearly as fundamental as for-loops in other language.

[1] https://www.youtube.com/@ThePowerOfProlog

https://www.metalevel.at/prolog

[2] https://www.youtube.com/watch?v=nmBkU-l1zyc

https://www.metalevel.at/acomip/

schmuhblaster 7 hours ago||
I also have a strange obsession with Prolog and Markus Triska's article on meta-interpreters heavily inspired me to write a Prolog-based agent framework with a meta-interpreter at its core [0].

I have to admit that writing Prolog sometimes makes me want to bash my my head against the wall, but sometimes the resulting code has a particular kind of beauty that's hard to explain. Anyways, Opus 4.5 is really good at Prolog, so my head feels much better now :-)

[0] http://github.com/deepclause/deepclause-desktop

tannhaeuser 1 hour ago|||
> Opus 4.5 is really good at Prolog

Anything you'd like to share? I did some research within the realm of classic robotic-like planning ([1]) and the results were impressive with local LLMs already a year ago, to the point that obtaining textual descriptions for complex enough problems became the bottleneck, suggesting that prompting is of limited use when you could describe the problem in Prolog concisely and directly already, given Prolog's NLP roots and one-to-one mapping of simple English sentences. Hence that report isn't updated to GLM 4.7, Claude whatever, or other "frontier" models yet.

[1]: https://quantumprolog.sgml.net/llm-demo/part1.html

schmuhblaster 1 hour ago||
Opus 4.5 helped me implement a basic coding agent in a DSL built on top of Prolog: https://deepclause.substack.com/p/implementing-a-vibed-llm-c.... It worked surprisingly well. With a bit of context it was able to (almost) one-shot about 500 lines of code. With older models, I felt that they "never really got it".
kamaal 5 hours ago|||
>>I have to admit that writing Prolog sometimes makes me want to bash my my head against the wall

I think much of the frustration with older tech like this comes from the fact that these things were mostly written(and rewritten till perfection) on paper first and only the near-end program was input into a computer with a keyboard.

Modern ways of carving out a program with 'Successive Approximations' with a keyboard and monitor until you get to something to work is mostly a recent phenomenon. Most of us are used to working like this. Which quite honestly is mostly trial and error. The frustration is understandable because you are basically throwing darts, most of the times in the dark.

I knew a programmer from the 1980s who(built medical electronics equipment) would tell me how even writing C worked back then. It was mostly writing a lot, on paper. You had to prove things on paper first.

pixl97 11 minutes ago|||
I'm assuming they were written on paper because they were commonly punched into paper at some stage after that. We tend to be more careful with non erasable media.
schmuhblaster 4 hours ago||||
>> I think much of the frustration with older tech like this comes from the fact that these things were mostly written(and rewritten till perfection) on paper first and only the near-end program was input into a computer with a keyboard.

I very much agree with this, especially since Prolog's execution model doesn't seem to go that well with the "successive approximations" method.

bjourne 1 hour ago|||
But I wonder if that characterization is actually flattering for Prolog? I can't think of any situation, skill, technology, paradigm, or production process for which "doing it right the first time" beats iterative refinement.
pixl97 10 minutes ago||
Being that prolog is from the 70s, I would guess you're a bit more careful with punch cards.
goku12 7 hours ago||
This is the sort of comment I'm on HN for. Information, especially links to appropriate resources, that only a true practitioner can offer.
gpvos 4 hours ago||
Indeed. Favorited it. My Prolog is too rusty to understand it all, but even just skimming the metainterpretation article was enlightening.
Nora23 4 hours ago||
Same here. The metainterpretation stuff is fascinating but dense.
xelxebar 11 hours ago||
Here's a nicely-designed tiling window manager, implemented in SWI-Prolog:

https://github.com/Seeker04/plwm

It actually has quite good UX affordances. More than that, however, I find the code imminently hackable, even as someone with very little Prolog experience. Reading through the plwm code really demystified the apparent gap between toy and practical Prolog for me. Heck, even the SWI-Prolog codbase itself is quite approachable!

I'm also mildly surprised at some of OG's gripes. A while back, I ran through Triska's The Power of Prolog[0], which crisply grounds Prolog's mental model and introduces standard conventions. In particular, it covers desugaring syntax into normal predicates, e.g. -/2 as pairs, [,]/2 as special syntax for ./2 cons cells, etc. Apparently, I just serendipitously stumbled into good pedagogical resources!

I'd be interested in ways that people utilize logical programming concepts and techniques into non-LP languages.

[0]:https://www.metalevel.at/prolog

usgroup 4 hours ago||
I think this article is problematic because Prolog is truly a different paradigm which requires time to understand. Laments about no strings, no functions and "x is confusing" read like expectations of a different paradigm.

Prolog is also unusual in a sense that it is essential to understand what the interpreter does with your code in order to be able to write it well. For vanilla Prolog, that's not so hard. However, when constraint programming and other extensions are added, that becomes much harder to do.

pjmlp 1 hour ago||
When I did my degree in Software Engineering, logic programming (with Tarsky's World), and Programming with Prolog were required classes.

There were only two prevalent attitudes, some of us really loved FP (me included), others hated it and could hardly wait to get it done.

Somehow there was a similar overlap with those of us that enjoyed going out of mainstream languages, and those that rather stay with Pascal and C.

aeonik 2 hours ago|||
Datalog has the same capabilities as prolog but allows strings right?

My understanding is that they have very different evaluation strategies, bottom up vs top down. But with laziness and pruning you can still achieve the same goals in datalog with more ergonomics, right?

I think every language should have a prolog or datalog implementation, kind of like regex.

rramadass 17 minutes ago||
> I think this article is problematic because Prolog is truly a different paradigm which requires time to understand.

> Prolog is also unusual in a sense that it is essential to understand what the interpreter does with your code in order to be able to write it well.

100% this!

Coming from procedural/OO paradigms i did not understand how to think about Prolog until i read Robert Kowalski's paper Predicate Logic as a Programming Language - https://www.researchgate.net/publication/221330242_Predicate...

I still have a long way to go but at least i am on the right track.

tannhaeuser 5 hours ago||
> No standardized strings

> ISO "strings" are just atoms or lists of single-character atoms (or lists of integer character codes) [...]. Code written with strings in SWI-Prolog will not work in [other] Prolog.

That's because SWI isn't following ISO (and even moving away from ISO in other places eg. [1]).

ISO Prolog strings are lists of character codes period. It's just that there are convenient string manipulation-like predicates operating on atom names such as sub_atom, atom_concat, atom_length, etc ([2]). You'd use atom_codes to converse between atoms/strings or use appropriate list predicates.

[1]: https://www.reddit.com/r/prolog/comments/1089peh/can_someone...

[2]: https://quantumprolog.sgml.net/docs/libreference.html#string...

YeGoblynQueenne 6 minutes ago|
That's where ISO clashes with the de-facto standard of its most popular implementation, that is also the best maintained. Too bad for ISO.

... we've disagreed about this before though :)

infotainment 14 hours ago||
I always felt like Prolog's ability to execute programs was entirely accidental.

To me, it feels like a data description language that someone discovered could be tricked into performing computation.

YeGoblynQueenne 1 minute ago||
[delayed]
hwayne 14 hours ago|||
Check out datalog! https://learn-some.com/ The tutorial there uses Clojure syntax but Datalog normally uses a Prolog syntax.
akritid 12 hours ago||
This datalog implementation uses prolog syntax, can even run the queries in prolog to contrast the model: https://des.sourceforge.io/
jjgreen 14 hours ago||
... a bit like life ...
flopsamjetsam 11 hours ago||
Conway's Life? Or DNA?
mlajtos 5 hours ago||
yes
xlii 6 hours ago||
Someone bashing on my pet language? Cracks knuckles

Just kidding. Some of those are stylistic choices I don't have gripes but can understand the criticism. There is however one thing about "Non-cuts are confusing" I'd like to clarify:

In this example:

    foo(A, B) :-
      \+ (A = B),
      A = 1,
      B = 2.
It's very obvious why it fails and it has nothing to do with non-cut. Let's say A can be apple and B can be orange and now you're asking Prolog to compare apples to oranges! ;)

In short one has to "hint" Prolog what A and B can be so then it can "figure out" whethever comparison can be made and what is its result. Assuming there exist is_number(X) clause that can instantiate X as a number following would work just fine:

    foo(A, B) :-
      is_number(A),
      is_number(B),
      \+ (A = B),
      A = 1,
      B = 2.
(note that this would be stupid and very slow clause. Instantiation in such clauses like is_number(X) usually starts with some defined bounds. For A = 10000, B = 10001 and lower bound of 1 pessimistic case this clause would require 100M checks!
YeGoblynQueenne 44 minutes ago|
I think that should be nonvar(A), nonvar(B) because the reason the unification succeeds and \+(A = B) fails is because A and B are variables (when called as foo(A,B). What confuses the author is unification, as far as I can tell.

But, really, that's just not good style. It's bound to fail at some point. It's supposed to be a simple example, but it ends up not being simple at all because the author is confused about what's it supposed to behave like.

drob518 1 hour ago||
Prolog is pretty unique. I’ve run into similar frustrations when I have used it as well. I think you have to drop all your preconceived notions and past programming experience when you use Prolog and just accept it as it is. Prolog was developed to run in very small machines and so the resolution algorithm operates in a strict order. This means clause order sometimes matters. Overall, I’ve found it useful to always keep in mind that I’m unifying trees of terms, not so much programming in a pure logical language.
wodenokoto 4 hours ago||
As someone who is interested in learning more abut these kinds of tools, where does one start? Prolog? datalog? MiniKranren? And now the TFA also introduces Picat.

And once you've settled on one of these, which learning resource should one go with?

rramadass 35 minutes ago||
see https://news.ycombinator.com/item?id=45915699 first.

Then checkout the books recommended by user "YeGoblynQueenne" who knows this domain pretty well.

cess11 3 hours ago||
If you just want to dip in, grab https://www.scryer.pl/ and do some exercises from https://www.metalevel.at/prolog.

Scryer is a good start because it's ISO. Datalog is kind of a subset, MiniKanren is somewhat related but not Prolog, and Picat is kind of Prolog with an imperative language within it.

floxy 12 hours ago||
I guess we are supposed to pile on, so I'll add that the author should read "The Art of Prolog" (Sterling & Shapiro) and then "The Craft of Prolog" (O'Keefe).
YeGoblynQueenne 11 hours ago|
And also "Prolog Programming for AI" by Bratko and "Programming in Prolog" by Clocksin and Mellish.

Although these days I'd recommend anyone interested in Prolog starts in at the deep end with "Foundations of Logic Programming" by George W. Lloyd, because I've learned the hard way that teaching Prolog as a mere programming language, without explaining the whole logic programming thing, fails.

bw86 2 hours ago|
findall instead of bagof can also help for these cases.

    | ?- findall(A, (tree(A, N), branch(N)), As).

    As = [n,n1]

    yes
See https://lpn.swi-prolog.org/lpnpage.php?pagetype=html&pageid=...
More comments...