Live data from Hacker News

The Power of Prolog

metalevel.at

121–130 of 162 posts

Re: The Power of Prolog

#121
post #47

One of the coolest things I've seen is to use Prolog with CLP(FD) to solve the 7-11 problem. The problem basically says the sum of the prices of four items is $7.11, and the product is $7.11 too (no rounding); find the prices of these four items. This can be solved in two lines of code that gives the (unique) solution in a second. Not even my expensive Mathematica can do this! ?- use_module(library(clpfd)). true. ?-…

This Python code does a full search and finds the unique solution instantly:

    p = 711
    q = 711000000
    for a in range(1, 1 + p // 4):
      if q % a == 0:
        for b in range(a, 1 + (p - a) // 3):
          if q % (a * b) == 0:
            for c in range(b, 1 + (p - a - b) // 2):
              d = p - a - b - c
              if a * b * c * d == q:
                print (a, b, c, d)
Does Prolog do the same pruning? Alternatively, it could be dumber (omit some of the pruning that my code does) or smarter (start by factoring 711000000 or something).

Re: The Power of Prolog

#122
post #47

One of the coolest things I've seen is to use Prolog with CLP(FD) to solve the 7-11 problem. The problem basically says the sum of the prices of four items is $7.11, and the product is $7.11 too (no rounding); find the prices of these four items. This can be solved in two lines of code that gives the (unique) solution in a second. Not even my expensive Mathematica can do this! ?- use_module(library(clpfd)). true. ?-…

This Python code does a full search and finds the unique solution instantly: p = 711 q = 711000000 for a in range(1, 1 + p // 4): if q % a == 0: for b in range(a, 1 + (p - a) // 3): if q % (a * b) == 0: for c in range(b, 1 + (p - a - b) // 2): d = p - a - b - c if a * b * c * d == q: print (a, b, c, d) Does Prolog do the same pruning? Alternatively, it could be dumber (omit some of the pruning that my code does) or s…

You could write the same solution in prolog. The difference between yours and the original solution is the latter follows the original program specification and is easier to compare to it. That is, it defines what the problem is, not how to solve it.

Re: The Power of Prolog

#123
post #17
post #9

One of my favorite languages, pity that is has had even harder time than Lisp getting mainstream acceptance.

I once asked my Prolog instructor why this all takes so long. His reply: "How well are you prepared for the next ISO meeting?" At that point, I realized that if you prepare yourself in earnest for, say, the standardization process, or even to accomplish any concrete improvement in Prolog systems (such as better indexing, more advanced constraints, faster garbage collection etc.), then there is so much to do that ther…

it sounds like ISO standardisation was a mistake?

Re: The Power of Prolog

#124
post #4
post #3

Earlier quoted context omitted.

Very nice book. Do you know what companies are using prolog commercially?

Thank you for the kind words! Regarding companies that use Prolog, here are a few examples: A third of all airline traffic is handled by systems that run SICStus Prolog: https://www.sics.se/projects/sicstus-prolog-leading-prolog-t... A large portion of the New Zealand stock exchange is powered by Prolog: https://dtai.cs.kuleuven.be/CHR/files/Elston_SecuritEase.pdf Oracle uses Prolog in the JVM specification: https://…

>A third of all airline traffic is handled by systems that run SICStus Prolog

But are those systems responsible for all the I.T. problems that are plaguing the industry?[1] (I've been under the impression the issues were primarily with COBOL/mainframe systems, but wasn't aware Prolog was so widely used by airlines).

[1] https://www.google.com/search?hl=en&q=airline%20computer%20f...

Re: The Power of Prolog

#125
post #47

One of the coolest things I've seen is to use Prolog with CLP(FD) to solve the 7-11 problem. The problem basically says the sum of the prices of four items is $7.11, and the product is $7.11 too (no rounding); find the prices of these four items. This can be solved in two lines of code that gives the (unique) solution in a second. Not even my expensive Mathematica can do this! ?- use_module(library(clpfd)). true. ?-…

This Python code does a full search and finds the unique solution instantly: p = 711 q = 711000000 for a in range(1, 1 + p // 4): if q % a == 0: for b in range(a, 1 + (p - a) // 3): if q % (a * b) == 0: for c in range(b, 1 + (p - a - b) // 2): d = p - a - b - c if a * b * c * d == q: print (a, b, c, d) Does Prolog do the same pruning? Alternatively, it could be dumber (omit some of the pruning that my code does) or s…

Prolog doesn't do that automatically, but you can write the same thing in Prolog too:

    xkcd_ugly(A, B, C, D) :-
        P = 711,
        Q = 711000000,
        Max_A is 1 + P // 4,
        between(1, Max_A, A),
        Q mod A =:= 0,
        Max_B is 1 + (P - A) // 3,
        between(A, Max_B, B),
        Q mod (A * B) =:= 0,
        Max_C is 1 + (P - A - B) // 2,
        between(B, Max_C, C),
        D is P - A - B - C,
        A * B * C * D =:= Q.
In SWI-Prolog, I get the following timings:

    ?- time(xkcd_ugly(A, B, C, D)).
    % 383,992 inferences, 0.056 CPU in 0.056 seconds (100% CPU, 6825328 Lips)
    A = 120,
    B = 125,
    C = 150,
    D = 316 ;
    % 3,274 inferences, 0.003 CPU in 0.003 seconds (100% CPU, 1229464 Lips)
    false.
That is to say, it found the first solution in 0.056 seconds CPU time, then took an additional 0.003 seconds to prove that there are no further solutions. (This is the minimum of a handful of runs, there is quite a bit of variation.)

Quick and very dirty benchmarks on the same machine run your Python code in about 0.02 seconds. The CLP(FD) code above takes about 0.1 seconds but it's a lot clearer.

Re: The Power of Prolog

#126
I recall at college in 1989 our lecturer had brought in a chap who'd done an entire airline baggage management suite written in prolog (turbo prolog, to be more accurate). The demo we saw was absolutely gorgeous -- real time updates on-screen to changes in baggage locations elsewhere (in the system). This was on i386-equivalent machines, before anyone gets overly nonchalant. He explained that he'd been up against two competing tenders - one in COBOL, the other in C - both higher priced and longer delivery times.

Nonetheless, I still never really properly grokked prolog, as much as I tried. I feel my brain had already been wired away from declarative languages. Perhaps a poor cop-out.

Re: The Power of Prolog

#127
post #115

Prolog: just say no. (To explain the joke, one of the main problems with Prolog is that solutions which cannot be unified just emit a "no" with no further explanation, making large Prolog setups hard to debug)

I think the usual (and funnier) version of that joke is:

    Q: How many Prolog programmers does it take to change a lightbulb?
    A: no

Re: The Power of Prolog

#130

I hated this language (and I'm saying it knowingly, despite the fact that this is most definitely a Prolog loving group). I found its working model difficult for me and the ROI quite low. I wanted to make my critique a bit constructive though and, before spitting out something of my own, I decided to look if there's already anything available in this regard. I think Andre Vellino puts it quite well (or at least bette…

Quote from the link: "The most confusing thing about Prolog is that, whatever algorithm you implement with it must be on top of the built-in ones, namely depth-first search, and unification (and only using recursion rather than iteration). "

When I discovered that during my studies I decided that Prolog is not for me. First it looks neat for toy problems and then you're fighting the solver until you give up.

Post reply on HN