
The Power of Prolog - fogus
https://www.metalevel.at/prolog
======
triska
Thank you very much for your interest, I greatly appreciate it!

I hope you are all doing reasonably well. Please take care!

This book was most recently discussed here in May 2018:

[https://news.ycombinator.com/item?id=17121028](https://news.ycombinator.com/item?id=17121028)

Since then, I have added a new chapter, _Logical Foundations of Prolog_ :

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

Also, I have made several other additions and improvements. You can see most
of the changes since the last discussion in a public git repository:

[https://github.com/triska/the-power-of-
prolog/compare/8a94ed...](https://github.com/triska/the-power-of-
prolog/compare/8a94ed3d0ab73d31d6400ab18b9b341ed56fbad1...master)

Currently, I am working on several videos that will eventually form the core
of the book. Here are a few previews:

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

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

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

These videos are all work in progress, and they may be replaced by better
versions at any time. Hence, if possible, please use the links above to refer
to them: They will always point to the latest versions.

Alternatively, please use the following overview page that shows all videos:

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

Also, I have published a comprehensive journal paper about my CLP(B) system,
i.e., a _SAT solver_ with some nice algebraic properties, seamlessly
integrated into Prolog as a specialized form of unification:

[https://www.metalevel.at/boolean.pdf](https://www.metalevel.at/boolean.pdf)

For Prolog application programmers and system implementors, the paper's
appendices may be especially interesting. They formalize a few important
concepts that are also a major theme in the book.

As of October 2019, the CLP(B) system is also available in Mark Thom's Scryer
Prolog. Scryer is a Rust-based Prolog implementation that is freely available,
conforms to the Prolog ISO standard, represents strings efficiently as lists
of characters, and includes important features for implementing Prolog-based
constraint solvers:

[https://github.com/mthom/scryer-prolog](https://github.com/mthom/scryer-
prolog)

As of a few days ago, Scryer Prolog also ships with my implementation of
CLP(ℤ), Constraint Logic Programming over _integers_. This is a very useful
declarative paradigm for solving combinatorial tasks, in some ways superior to
SAT solving because it allows more convenient modeling, easier experimentation
with different formulations, and reasoning at a higher conceptual level. The
chapter on declarative integer arithmetic contains more information, and
further pointers:

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

For illustration, here is an example page where you can solve _timetabling
instances_ with this approach:

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

I welcome all comments and suggestions about the book, these videos, and
Prolog in general. Also, I would like to take this opportunity to thank all
readers for your thoughtful comments and endorsements. Your feedback and
encouragement are making this work especially worthwhile.

Enjoy!

~~~
mthom
Some coming features of Scryer Prolog for those interested:

\- Automatic detection and compilation of partial strings

\- Streams, including sockets

\- Garbage collection in anticipation of very fast yet logically pure I/O

\- Improvements to the instruction dispatch loop (many opportunities for
enhancement there, probably a good place to start for a beginning contributor)

In the past few months, we've added delimited continuations, tabling, partial
strings, and Markus' CLP(B), CLP(ℤ) and format libraries. For a hobbyist
project I'd say we're moving at a fairly quick pace!

Longer term, we're interested in:

\- JIT compilation to native code (Cranelift seems a good candidate?)

\- Low-level integration with Common Lisp environments

I'd love to have system-level contributors, although library contributions are
always very welcome!

~~~
felixyz
> very fast yet logically pure I/O

Sounds interesting, do you have a link or something else to share regarding
your conception of logically pure I/O?

~~~
Avshalom
[https://www.swi-prolog.org/pldoc/man?section=pio](https://www.swi-
prolog.org/pldoc/man?section=pio)

[https://github.com/SWI-Prolog/swipl-
devel/blob/master/librar...](https://github.com/SWI-Prolog/swipl-
devel/blob/master/library/pure_input.pl)

this (I assume) is the library(pio) mentioned in the github issue

~~~
falsissime
From the interface (mostly yes, that, is apart from the impure extensions
added later, and that chars are used and not codes) it is the same. But from
the implementation behind it´s different. In particular from the space
requirements. A text of n characters requires 3 * 8 * n bytes in SWI, but n +
2 * 8 bytes in Scryer. So there is a factor 24 in space requirements (on
64bit).

Also, there is now ample room for input that SWI once almost offered.

------
smabie
Is there a reason why anyone would choose prolog today instead of a library?
Like racklog [[https://docs.racket-lang.org/racklog/](https://docs.racket-
lang.org/racklog/)]. All the benefits of prolog, with none of the cost of
using a new language and integrating it into your project.

~~~
felixyz
I think you have a point, but not sure your example illustrates it very well -
is Racket really any less niche than Prolog? On the contrary, I would say.

But regardless, if one needs some of the basic features of Prolog and a good
library exists for your language/environment of choice (core.logic for Clojure
is probably one of the most solid ones at the moment), then I think it's good
common sense not to add one more language to your system (assuming this is in
a production context, of course). But modern Prolog systems have a lot more
functionality than the unification and backtracking semantics that at the core
of Prolog. Among them are the constraint solving capabilities that The Power
of Prolog describe, and tabling in some form, which allows for efficient
execution of programs that in early Prolog systems would lead to non-
terminating search.

Not only are these features available, but in many cases they are tightly
integrated with the language, optimized and fine-tuned over many years. So the
answer is similar to if someone asks "Why choose Erlang when there are actor-
based concurrency libraries in so many languages?" (or any number of similar
questions): it might not make sense for the simpler use cases, but you won't
get the full experience unless you take the plunge.

~~~
smabie
I was using racklog as an example of a library you would use if your app was
programmed in Racket; I wasn't suggesting that you would decide to use racklog
in a C project or something over prolog.

------
dang
Earlier versions were discussed in 2018:
[https://news.ycombinator.com/item?id=17121028](https://news.ycombinator.com/item?id=17121028)

and 2017:
[https://news.ycombinator.com/item?id=14045987](https://news.ycombinator.com/item?id=14045987)

(Links are for the curious. Reposts are ok after a year:
[https://news.ycombinator.com/newsfaq.html](https://news.ycombinator.com/newsfaq.html))

------
29athrowaway
Prolog reminds me of Cyc.

"Cyc failed to understand a story about a person named Fred shaving in the
morning... Its inference engine detected an inconsistency in the story: it
knew people do not have electrical parts, but because Fred was holding an
electric razor, it believed the entity 'FredWhileShaving' contained electrical
parts. It therefore asked whether Fred was still a person while shaving."

From "Deep Learning" by Ian Goodfellow, Yoshua Bengio, Aaron Courville.

~~~
imglorp
I think that's a valid point. Cyc consisted of a set of frames/predicates with
real world relations and concepts. And then the logic part on top would try to
find inconsistencies, generate new predicates, and of course some kind of
unification to answer questions over predicates.

They worked on it in isolation for decades, building new frames laboriously by
hand, and made a few corporate sales I guess, but withdrew OpenCyc and that's
about the last we heard from them. Major bummer.

I feel like an open source business model would have allowed the public to use
and extend the frames, propelling Cyc to mainstream, while Cycorp would
consult and advocate and curate.

------
haolez
I've found this channel a few months ago and my wife thinks I'm crazy when I'm
watching it. "This looks like work, but harder!!". Awesome content! Thanks for
posting it :)

~~~
triska
Thank you so much, and you are welcome! That's an awesome slogan!

Some things are hard, yet have no pay-off or are even detrimental. On the
other hand, things that pay off are often proportionally hard.

It is not may goal to make it hard, in fact I am doing everything I can to
make it as easy as possible. I am very interested in didactic approaches, and
always welcome feedback! I would like to make it worth the effort for viewers,
and — beyond that — exceed the required effort in value.

~~~
haolez
You do a very good job making it approachable. I'll make a PoC this year using
Prolog in a specific application regarding contract generation (LawTech). It's
surely thanks to your insights!

Now that I know your handle, I'll ping you when I have some news :)

~~~
triska
Thank you a lot, I would greatly appreciate this!

There is significant interest in applying logical reasoning and logic
programming in the context of legislation and application of laws, in fact
especially in Austria, for several reasons both historic and current, and also
throughout Europe, for instance to implement cross-border use cases that are
mandated by the Single Digital Gateway Regulation (SDG).

As one contact point, see for example the _Vienna Legal Hackers_ :

[http://vie-legalhackers.at/en/home-en/](http://vie-legalhackers.at/en/home-
en/)

A few weeks ago, I gave a presentation about _Logic in the Public Sector_ in
the form of a RuleML webinar, maybe these slides are interesting for your use
case, or in future projects in LegalTech:

[http://ruleml.org/talks/MarkusTriska-
LogicInThePublicSector-...](http://ruleml.org/talks/MarkusTriska-
LogicInThePublicSector-RuleMLWebinar-2020-03-25.pdf)

See also ruleml.org for more information about RuleML, and further potential
opportunities for cooperation!

One thing I can say for certain, after discussing this topic with lawyers who
are interested in Prolog: In Vienna, opportunities for applying Prolog in
concrete projects in LegalTech abound. If you want to use Prolog in a job in
the legal sector and are reasonably skilled in the language, you can start
working immediately, for instance in the context of EU projects.

------
davidgl
See also Mercury, FP logic programming like the love child of Prolog and
Haskell
[https://en.wikipedia.org/wiki/Mercury_(programming_language)](https://en.wikipedia.org/wiki/Mercury_\(programming_language\))

~~~
jacobush
PicoLisp also has a prolog in it.

------
collyw
Prolog look like it would be the correct tool for the job for a certain class
of problems, I wish I had time to learn it. I always wonder why it doesn't get
more attention.

~~~
sacado2
Nowadays, ontology-management systems like Protégé and SPARQL can more or less
solve all problems prolog can solve, while providing higher-level formalisms
and with the guarantee your query will terminate.

~~~
ben-schaaf
Doesn't guaranteed termination just mean it isn't Turing complete and thus by
definition can't solve all problems prolog can solve?

~~~
sacado2
Absolutely. But in practice, I can't think of a problem that coulnd't be
solved with OWL (or some other formalism like answer set programming) and
where prolog would be the best solution (rather than more "traditional"
programming languages). There are certainly a few, but not enough IMO to draw
a lot of attention to prolog.

------
downerending
I absolutely loved Prolog and CLP(R) back in the day.

The one thing I never got past was its difficulty in dealing with large,
mutable sets of data. So, for example, given an array of a million or billion
elements that are being rapidly modified, even the most trivial sorts of
backtracking become infeasable. Maybe there's some elegant way to do it, but I
never got it.

(And difference lists need to die.)

~~~
tabtab
I've always had difficulty debugging functional and formula based logic, and
others have said similar. Imperative is just easier to "fractal-ly dissect" to
me. Debugging seems to be a reoccurring theme of many "high abstraction"
techniques that perhaps keep them out of the mainstream. I known many
disagree, and this claim often triggers heated debates.

------
photon_lines
If anyone loves both Prolog and Python, I wrote a mini-prolog interpreter in
Python just over a year ago which you can check out here:
[https://github.com/photonlines/Python-Prolog-
Interpreter](https://github.com/photonlines/Python-Prolog-Interpreter)

~~~
mattsouth
snap, but mine is in javascript and references a couple of predecessor
javascript mini-prolog interpreters:
[https://github.com/mattsouth/dprolog](https://github.com/mattsouth/dprolog).
I still need to read that WAM book. But I tell that to myself everytime it
comes up here.

------
jlos
Just a conjecture, but I think the main drawback of Prolog is its hard to see
it having popular support. Recursion, logic programming, and constraint
satisfaction are challenging paradigms which doesn't lend well to doing
something useful quickly, as opposed to something like Rails.

Im just finishing a class using Prolog and CLP(FD) and I've really enjoyed
using the language. And while lots of things in programming aren't easy at
first, I'm just saying its hard to see wide adoption of prolog for that
reason.

------
vpribish
has anyone here ever used prolog in production - like real world, making
money, production? How about in real world research where it produced no-
kidding actual value? From what I can tell it is only useful these days as a
academic example of an alternative type of language. Is there something else
that has carried the torch forward?

~~~
hyeomans
Yarn
[https://yarnpkg.com/features/constraints](https://yarnpkg.com/features/constraints)

------
kitd
OT, but I was entertained by the fact that, as soon as I went to the page, the
"Recursion" link was purple.

------
christmm
prolog should be used as a library DSL, embedded, within programs written in
other languages

