tannhaeuser 6 hours ago

If you like this stuff, consider how to "implement" JSON in Prolog:

    :- op(400, yfx, :).
    ?- JSON = {
      a: 1, b: "a string",
      c: { d: "a compound" }
    },
    JSON = { a: X, b: Y, c: { d: Z }}
Basically, you don't ;) all we did here is declaring ':' as an infix operator. The next lines bind/unify a term to the var JSON, and the last line is again unification in action (basically destructuring on steroids), binding the vars X, Y, and Z to the respective value 1, "a string", and "a compound" from the previously bound JSON var.

You can paste this snippet into [1] and execute it right in your browser to check that indeed X, Y, and Z are bound as described if you wish.

Not that it matters that much. Prolog is really for indeterministically searching a large/infinite space such as in planning (and yes it can be used for theorem proving and program verification as well). It's always fun to see newcomers trying to implement pointless low-level stuff such as list primitives when coming from a functional programming background or even getters/setters.

[1]: https://quantumprolog.sgml.net/browser-demo/browser-demo.htm...

  • xlii 22 minutes ago

    > Prolog is for indeterministically…

    One side note here. Yes you can do indeterministic search but it’s not a property in Prolog. In fact, Prolog execution is completely deterministic without use of randomization.

    But I guess that in some domains (e.g. optimization) people would rather shuffle numbers than do a boring 1…1000000000 loops.

xonix 5 hours ago

Long time ago I was obsessed with Prolog. I found that if you bend it enough you could implement imperative code (a-la JavaScript) evaluation, still being valid Prolog:

https://github.com/xonixx/prolog-experiments/blob/main/Prolo...

This was super funny, but obviously absolutely useless

  • convolvatron 5 hours ago

    Why do you say that? Imagine you wrote a language that looked procedural but was actually relational underneath? We could get rid of so much glue and use unification when it made sense and ignore it otherwise. That’s a great idea

    • winwang 4 hours ago

      Sounds similar to Haskell "do" notation where it can look procedural, but backed by FP principles and tools.

      (Something something monads are sequentialness)

tombert 7 hours ago

I really need to learn Prolog. It looks interesting and powerful, but it's different enough form most languages that I'd need to actually sit down and learn it and how to write useful stuff with it.

At a superficial level, it looks a bit like Z3, which I do have some experience with, but I suspect that there's a lot of intricacies to Prolog that don't apply to Z3.

  • teruakohatu 7 hours ago

    Prolog feels like magic, and it well worth learning. It is a general purpose programming language rather than a very specific purpose tool like Z3.

    Sadly non-'pure' logical programming is required to get real-world performance (eg. cuts that prune the search tree) which kind of feel like, or is, a leaky abstraction that breaks the magic.

    I have had good success in solving logical problems by asking o1 to produce prolog programs.

    • xlii 12 minutes ago

      > Sadly non-'pure' logical programming is required to get real-world performance (eg. cuts that prune the search tree) which kind of feel like, or is, a leaky abstraction that breaks the magic.

      They aren’t required but are convenient. Excuse brutalizing syntax and formatting but in Prolog you can do:

        A(X) :- between(1,3,X).
        B(X) :- between(1,1000,X).
        ?- A(X), B(X).
        ?- B(X), A(X).
      
      And get either 9 visits or 3000. It’s possible to design a program without cuts but (in my opinion) it’s very hard and doesn’t bring any benefits outside of street cred.

      Also cuts and pruning long time ago felt like bloated terms (when I didn’t get Prolog) whereas it’s a simple break equivalent in imperative languages loops.

    • coliveira 3 hours ago

      If you learn modern prolog techniques, cuts have become less and less necessary.

  • quesomaster9000 7 hours ago

    Z3 is entirely different IMHO, once you get into solvers the question becomes not just 'is this satisfied' but 'what is the minimum sequence of steps necessary to arrive at the result'.

    I have relied heavily on both Z3 and Alloy for ad-hoc jobs, and Prolog doesn't even come close to the inference power, and that's along-side Macsyma and Sage.

    • rscho 5 hours ago

      The big advantage of prolog vs solvers is the logic programming aspect. If you can express parts of your program in a logically pure way, Prolog code will be much more concise than the Z3 equivalent, although probably slower. Another advantage is that Prolog outputs and reads Prolog terms, which makes it very good for metaprogramming. It's like lisp: incredible if you're solo prototyping, not so much if you're part of a team of corporate cogs.

    • Karrot_Kream 6 hours ago

      What problem domain are you working in? I find usage of solvers and Prolog to be very domain specific.

    • gatlin 6 hours ago

      Have you tried sCASP?

elteto 6 hours ago

> I call it, “C Plus Prolog”, or “C+P” for short.

Missed the chance to call it CPP.

  • LordShredda 21 minutes ago

    Would confuse it with the Canadian pension plan :^)

coliveira 3 hours ago

The main advantage of Prolog is that it forces you to think in different ways to solve problems. The main weakness of Prolog is that it forces you to think in different ways to solve problems. Especially because most existing libraries are written in procedural style, so they don't work well with prolog. In the end you'll have some difficulty to integrate with existing code.

fithisux 28 minutes ago

Mercury is not mentioned.

rhelz 7 hours ago

This is the most creative idea I've seen in a long time.

It is also the smoothest way of "declaring an imperative" I've ever seen.

It is also the most amazing use of the parser which is built-in to prolog (no, not the DCG's, the other parser, the railroad/operator precedence parser).

I wish it had been discovered around 1981. The world might be very different.