
The Prolog Story (2010) - peter_d_sherman
http://kylecordes.com/2010/the-prolog-story
======
mark_l_watson
I have used Lisp languages far more often than Prolog, but I do have my own
Prolog success story: I had just used ExperLisp on the Macintosh to write a
little app ExperOPS5 for the company who wrote and marketed ExperLisp and
ExperProlog. After this I was given an internal research grant to write a
complete simulation environment in ExperLisp as part of trying to win a large
contract. Given familiarity with the tools I was using, a good prototype took
about 100 hours to write. I had time left over and talked with my management
about doing a parallel implementation in ExperProlog. Using a declarative
approach, I had a parallel system, with interactive UI and graphics done in
about 40 hours - and I had a learning curve with ExperProlog.

I have also used SwiProlog and occasionally Amzi Prolog also since then.
SwiProlog’s semantic Web library was the basis for all my early work and
experience with semantic Web and linked data. I definitely suggest trying
Prolog on a few small projects and add it to your tool set.

~~~
Eli_P
Have you been feeling dire expressing your code while having only (Subject,
Predicate, Object) first order logic? What kind of requests your app could
manage and how large was DB (how many triplets)?

------
nickdrozd
Summary: They needed to do some complex queries on data in a SQL database,
queries that were clearly better suited for Prolog. So they 1) queried for the
relevant data from SQL, 2) formatted the data into the form of Prolog terms
and dumped it into a file, 3) fired up the Prolog interpreter and loaded the
data along with a small amount of Prolog query code, 4) ran the Prolog
queries, 5) formatted the results into CSV and dumped it into a file, and
finally 6) imported the results back into SQL. While it has some hacky
elements, this approach took just one day to implement, as opposed to the
projected months it would have taken to do it without Prolog.

If I understand it right, Prolog is being used kind of like a more powerful
version of Awk / Perl. _The Power of Prolog_ [1] describes a somewhat similar
use of Prolog: storing server logs as Prolog terms and then running complex
queries directly on the log files.

[1]
[https://www.metalevel.at/prolog/business](https://www.metalevel.at/prolog/business)

~~~
deckar01
> Prolog is being used kind of like a more powerful version of Awk / Perl

I have limited experience with Prolog, but it is not just a quick and dirty
scripting language (how I view Perl). They were able to build the system so
fast, because they only needed to translate the customer's requirements into
Prolog declarations, and the Prolog engine is powerful enough to solve the
constraints. In my experience, imperative code rarely has a 1-to-1
relationship with the requirements like this.

~~~
nickdrozd
Maybe it would be clearer if I said Prolog was being used like a "much" more
powerful version of Awk / Perl. Consider the server log case I mentioned. A
question you might ask of a server log is: _Are there any client requests that
were not served?_ That query is an easy one-liner. You can go a little further
and say: _Give me the IPs of all the client requests that were not served_.
That query is also easy, maybe just another line or two. It would be a lot
harder to get that information with Awk etc, but the spirit is the same: you
have a bunch of data, and you want to extract some particular information, and
you want to do so without a lot of pomp.

------
rurban
I recently used prolog for a complicated binary reverse-engineering task: bit
packing. Likewise prolog is perfect for compilation.

[https://savannah.gnu.org/forum/forum.php?forum_id=9203](https://savannah.gnu.org/forum/forum.php?forum_id=9203)

In fact it's a better prolog, picat, which also allows pretty straightforward
statements, like loops, and has solver support.

[http://picat-lang.org/](http://picat-lang.org/)

~~~
mark_l_watson
I agree with the other commenter’s Wow! Picat also supports satisfiability
apps like MiniZinc does. Thanks for posting that link!

~~~
rurban
Initially I choose MiniZinc, but then was turned off by its baroque syntax.
Straight prolog syntax and picat syntax was much easier in the end.

writing prolog is mostly about writing in the most natural and readable syntax
possible. using haskell-like types put me off.

------
rntz
I would like to know what Prolog implementation the author used. In
particular, the article claims:

1\. > An initial analysis found that we would need to implement a complex
depth/breadth search algorithm either in the client application or in SQL.

2\. The Prolog runtime would efficiently solve this problem given rules that
naively described it.

I am skeptical, as this is emphatically not my experience with Prolog. In my
experience, for any problem requiring a complex search strategy, Prolog is not
your friend. Most Prologs do something approximating a naive depth-first
search with unification, and on complex search problems this approach rapidly
blows up. This can _sometimes_ be fixed by:

1\. Making careful use of cut (`!`) or other non-declarative operators to
change the search order.

2\. Using tabling, essentially an advanced form of memoization. But only some
Prologs support tabling, and it's useful only for some sorts of problem.

However, the author mentions none of these. Are they glossing over something,
or is my (probably more limited) Prolog experience uncharacteristic? Are there
"smart" Prologs out there I'm unaware of?

~~~
kylecordes
(I am the author.)

The implementation I used was SWI-Prolog. I had tinkered with a couple of
others prior to that in school (sorry, I don't remember more details), but
this was free/open, and up to the job at hand without requiring an acquisition
process from the customer of the system.

About the search strategy; yes I had to use a few cuts to get acceptable
efficiency. Still, this was much easier than re-creating the same thing in SQL
or imperative application code. The particular details, I don't remember well;
sadly I've had little occasion since 2010 to use Prolog.

~~~
peter_d_sherman
Hi Kyle, Peter Sherman here. (I posted this HN article). You and I had an
email dialogue around 2012, and of course I remember you from your Delphi days
(BDE Alternatives Guide, etc.) and the old Joel On Software forum. Anyway, the
other day I was reading about CoQ and it's use for formal verification, which
turns out to solve problems in various different diverse domains, and I said
to myself, "Hey, this sounds a whole heck of a lot like Prolog, plus a type
system", and so I was attempting to compare and contrast the two systems when
I thought back to your great video detailing the customer problem you had and
how you used Prolog to solve it. So, that's why the link appears on HN now,
that's why the lag time -- to answer your other question. Hey, if you've got a
spare minute or two, you should shoot me an email, peter.d.sherman AT gmail
DOT com. I might have an interesting business proposition for you...

------
hardwaresofton
Prolog looks like the exact right language for at least some part of Netflix's
OPA[0] -- I wonder why they didn't use it or why it wasn't a good fit (if
someone considered it).

I often want to reach for prolog when I face a problem like this, but I just
don't know enough about how it degrades/breaks and of course don't want to use
it to do any of the rest of the program stuff (web server, DB access), etc.

[0]: [http://www.openpolicyagent.org/](http://www.openpolicyagent.org/)

~~~
tsandall
(OPA co-founder here.)

The semantics of OPA's policy language are based on Datalog, a non-Turing
complete subset of Prolog. This means that all policy queries in OPA are
guaranteed to terminate (which makes it a good fit for problems like
authorization.)

Beyond regular Datalog, OPA adds first-class support for querying
complex/nested data structures like JSON.

As a side note, OPA was not developed at Netflix, but they were one of the
early adopters and continue to use it today.

~~~
hardwaresofton
Thanks so much for the answer -- I thoroughly enjoyed the OPA talks I've
seen[0][1]. I apologize for mistaking OPA as a netflix product, I think one of
the first times I saw it was as it was being used by Netflix so I assumed it
was one of their F/OSS projects or built by someone there.

Did you guys build your own engine? I took a quick look at the repo but don't
see anything that looks like a datalog library in your glide package list.

Last but not least, thanks for making and open sourcing such an awesome tool!
Will definitely be passing the word on about Styra[2], I had no idea there was
a whole company/more efforts behind OPA. I plan on using OPA in a bunch of
upcoming projects -- it looks like a fantastic, stable addition to the toolbox
of people looking to build robust programs/services.

[0]:
[https://www.youtube.com/watch?v=XEHeexPpgrA](https://www.youtube.com/watch?v=XEHeexPpgrA)

[1]:
[https://www.youtube.com/watch?v=4mBJSIhs2xQ](https://www.youtube.com/watch?v=4mBJSIhs2xQ)

[2]: [https://www.styra.com/](https://www.styra.com/)

~~~
tsandall
> Did you guys build your own engine? I took a quick look at the repo but
> don't see anything that looks like a datalog library in your glide package
> list.

Yes, the language implementation (parser/compiler/evaluator) is implemented
from scratch.

> Last but not least, thanks for making and open sourcing such an awesome
> tool!

Thanks for the kind words! If you have questions or need help, feel free to
file issues on Github or ask questions on Slack.

------
okket
Previous discussion from 2010:
[https://news.ycombinator.com/item?id=1363680](https://news.ycombinator.com/item?id=1363680)
(60 comments)

------
xvilka
There is a modern book about Prolog - Power of Prolog [1].

[1] [https://www.metalevel.at/prolog](https://www.metalevel.at/prolog)

------
Subi
Love seeing a good Prolog story surface. I was blown away by how productive it
was to use back in university along with XPCE for UI. Became the first
language I expertised in and it has paid off in terms of opening up a lot of
other avenues like being able to easily learn Erlang and Elixir.

There isn't always a good reason to use it for many situations nowadays but it
the way you deal with graphs and trees and such is fantastic. Though fun fact
Allegrograph has a Prolog rules interface

------
kylecordes
Hey, this is me!

This is some serious latency between recording and writing a thing, and is
appearing on hacker news. I will go through here and try to answer various
questions etc.

------
carapace
I recently went through what I'm calling a "conversion experience" with
Prolog. I was writing a compiler and a link to Warren's 1980 paper "Logic
Programming and Compiler Writing" went by here on HN (
[https://news.ycombinator.com/item?id=17674859](https://news.ycombinator.com/item?id=17674859)
.) After a brief learning curve I now have a much more powerful compiler in
about 1/5th of the code.

There are a few problems that _don 't_ fit well with Prolog, but not many. For
everything else, if you're not using Prolog you're probably working too hard.

Consider that achieving feature parity with 1/5th the code means 1/5th the
bugs, right off the top.

But often Prolog code is more useful than some equivalent imperative code, for
example, a Sudoku relation defined in Prolog serves to solve puzzles, but it
can also generate new puzzles and verify partial puzzles (count the solutions
and assert that there's only one.) [https://swish.swi-
prolog.org/p/Boring%20Sudoku.swinb](https://swish.swi-
prolog.org/p/Boring%20Sudoku.swinb)

Prolog is also _old_.

I keep thinking, "What about FOO?", only to find that FOO has been explored
years ago by a group of researchers, and often there is working code off-the-
shelf and ready to go to solve whatever problem.

Anyhow, TL;DR: For goodness' sake please check out Prolog. It's like time-
traveling into the future.

------
gumby
> I used Prolog in a production system a few years ago,

Nice pun!

