
Learn X in Y minutes Where X=Prolog - rbanffy
https://learnxinyminutes.com/docs/prolog/
======
YeGoblynQueenne
This is horrible! Prolog is nothing like the language described in this
"tutorial". The author has completely made up a bunch of programming language
semantics that have nothing to do with Prolog.

Just for the most blatant example of made-up semantics: unification is not
"essentially a combination of assignment and equality".

Unification is not assignment: Prolog predicates are immutable. You can't
assign to an immutable data structure.

Unification is not equality, either. For example, in Prolog 2 = 1+1 fails
because "2" does not unify with "1+1" (and not because "unification can't do
arithmetic"; it's because unification is not equality).

Unification is an algorithm to decide whether two Horn clauses can be
_resolved_ with each other. Resolution is the inference rule used in Prolog's
automated theorem proving. This is it:

    
    
       ¬p ∨ q, p
      ----------
          q
    

The horizontal line is read as "entails". p and q are predicates. The rule
says that if we can show that p is false or q is true and that p is true, then
we can show that q is true. If ¬p and p unify, they can be resolved and q can
be shown to be true.

Resolution is used in Prolog to prove a query by refutation. Queries are the
goals that the author calls "commands". These are not commands: they are
predicates, just like "facts" and "rules". Prolog tries to disprove a goal by
finding a clause with a head literal that unifies with the query (only "facts"
and "rules" have heads, whereas goals only have bodies). If it can, it then
recursively proves each literal in the body of the clause, by finding a clause
with a head that unifies with the literal.

Unfortunately, none of this will make sense if you try to understand Prolog in
terms of assignment and equality tests, and so on. Reading this article and
trying to "learn Prolog in Y minutes" will only serve to confuse any further
attempts to understand and learn the language.

~~~
bollu
Why horn clauses, specifically? By the way, are you aware of resources that
explain Prolog the way you did (in terms of logic?) the ones that I do know
either explain how to _implement_ prolog (eg. the WAM), or provide a "high-
level" explanation of unification --- missing out the details that you've just
pointed out. I'd love for an authoritative reference!

Another question: do you know of an easy way to _implement_ prolog? The
problem with the WAM is that it is incredibly hard to debug: I had begun
implementing it
([https://github.com/bollu/warren](https://github.com/bollu/warren)) but then
gave up due to getting boggled in large heap data structures that weren't
doing what I wanted, and debugging this manually got far too painful :(

~~~
YeGoblynQueenne
>> Why horn clauses, specifically?

That's a very good question. My (sloppy) answer is that satisfiability of sets
of clauses is undecidable for arbitrary clausal languages, but a variant of
the resolution rule, SLDNF-Resolution, is refutation-complete for sets of Horn
clauses and sound (and complete) for sets of definite clauses. Definite
clauses are a subset of Horn clauses having exactly one positive literal (i.e.
facts and rules in Prolog). Horn clauses are clauses with at most one positive
literal.

Given a set of definite clauses and a Horn goal clause, it is possible to
prove by refutation that the goal is true in the context of the program, in a
finite number of steps. This is how the Prolog interactive mode works: the
user enters a goal clause as a "query" and Prolog tries to refute the query by
SLDNF-resolution.

SLDNF-resolution is "SLD resolution with Negatation as (finite) Failure". "SLD
resolution" stands for Selective, Linear Definite resolution (it's a long
story). Negation as (finite) failure (NAF) accepts that a statement is true if
it cannot be proven false under a closed-world assumption (that all we know
that is true, is all that we can assume is true). NAF means that Prolog
programs can include clauses with negated literals, which is very handy
because one often wants to say things like "X is an Evil Demon Bear from Mars
if X has green fur and purple-glowing eyes _unless_ X plays the hurdy-gurdy".

These are some of the basic assumptions of Prolog: SLDNF-resolution, definite
clauses and negation-as-failure under a closed-world assumption. The goal is
to have a package that, under some conditions, can prove a goal given a logic
program. Given that, it's possible to write a logic theorem and have it
execute like a program, by proving a goal.

In terms of learning resources- if you have a bit of a logic background I
think that "Programming in Prolog" by Clocksin and Mellish does a good job of
going over both the declarative and imperative reading of Prolog (but it's
been more than 10 years since I first read it so I might be wrong).

I haven't even tried implementing Prolog. Too much hard work and it's already
been done by others :) So no idea how to do it. But it's probably the easiest
way to get a good set of deep insights in the language and I should probably
plonk my arse down and do it, myself.

------
punnerud
I have been thinking of Prolog as SQL, but the “goals” are your data
generator/table and the constraints are everything in there WHERE-clause. Made
it simpler to reason about.

Learning Erlang was also easier when I figured out that it is (was?) build on
Prolog, with its analogy to SQL.

~~~
FreeFull
There is a subset of Prolog called Datalog, explicitly designed for use in
databases. Datalog queries are guaranteed to terminate, and unlike Prolog it
is fully declarative (order of declarations doesn't matter).

~~~
slifin
Datalog is implemented in many Clojure(Script) databases if you'd like to try
it out, Datahike is my favourite for dev/learning use but Crux, Datomic,
Datascript and others exist

------
e12e
Does anyone know if there's a datalog-like embeddable language that is
suitable for simple integer constraints (the sum of a, b, c, and d must be e;
e=10.,b>2.)?

For prolog, there's apparently
[https://github.com/triska/clpz/](https://github.com/triska/clpz/)

But I really just want some simple constraints-oriented form validation (you
have 100 points, please feel free to by x's for 1 point each, y's for a cost
of 5 points. You don't have to spend all points - with feedback of how many
points are left as more x and y are bought. But for more variables, where
there are clear rules that can be modeled naturally as constraints rather than
with state and callbacks on change).

Ed: specifically for forms, there's [https://developer.mozilla.org/en-
US/docs/Web/Guide/HTML/HTML...](https://developer.mozilla.org/en-
US/docs/Web/Guide/HTML/HTML5/Constraint_validation) \- but it feels like it
gets messy with interdependent fields.

~~~
schoen
You might like a Satisfiability Modulo Theories (SMT) solver such as Z3.

[https://github.com/Z3Prover/z3](https://github.com/Z3Prover/z3)

It has bindings for other languages and is able to make some rather
sophisticated arithmetical and logical deductions in an often pretty efficient
way. I've used it successfully for parts of puzzle competitions and CTFs (and
I'm really only beginning to scratch the surface of what it can do).

You can read more about the options in that area at

[https://en.wikipedia.org/wiki/Satisfiability_modulo_theories](https://en.wikipedia.org/wiki/Satisfiability_modulo_theories)

which also links to the related tools in

[https://en.wikipedia.org/wiki/Answer_set_programming](https://en.wikipedia.org/wiki/Answer_set_programming)

[https://en.wikipedia.org/wiki/Constraint_programming](https://en.wikipedia.org/wiki/Constraint_programming)

These systems can feel pretty magical and are getting more powerful all the
time. (Of course there are cases where there is a better algorithm that you
could find, perhaps a much more efficient one, and using these solvers will be
asymptotically or worst-case slower than it had to be given more human
insight.)

Some of these could be overkill for your application, and Z3 doesn't have a
Javascript binding, so I guess it's not as helpful for web forms.

~~~
kqr
Do you have any suggestions on how to get started? I remember reading "The
unreasonable effectiveness of SAT solvers" or whatever it was called which
showed that SAT solvers are not always the most efficient performers, but they
can take you within a factor 10 in much less time. I remember thinking, "Oh,
but what about SMT solvers, then?" But not getting anywhere with that.

~~~
overlords
SAT, SMT, constraint programming lie on a continuum. SMT is SAT plus machinery
from constraint programming. I'm not an expert on this, just info from this
video. According to him constraint programming will win in the long run
because of constraint programming has larger structures which can be exploited
with global constraints (SAT is only a flat sequence).

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

------
sansnomme
Most of the Prolog books are thick academic textbooks, for a more modern take
with YouTube videos, take a look at
[https://www.metalevel.at/prolog](https://www.metalevel.at/prolog)

~~~
JadeNB
For textbooks, The Art of Prolog is highly accessible
([https://mitpress.mit.edu/books/art-
prolog](https://mitpress.mit.edu/books/art-prolog)), and The Craft of Prolog
([https://mitpress.mit.edu/books/craft-
prolog](https://mitpress.mit.edu/books/craft-prolog)) is not really
appropriate for self instruction but is simply a delight to read (and you'll
learn a lot of really, perhaps sometimes excessively, clever Prolog by reading
it).

------
fizixer
I'm told Prolog is the old way.

miniKanren (by William Byrd et. al.) is trending for many years though I'd
like to know what's fundamentally new about it.

The fact that Byrd later worked on constraint'ed miniKanren (cKanren) means
miniKanren is not as powerful?

His recent talk about using it with Barliman frontend was pretty cool [1]

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

~~~
7thaccount
I always thought minikanren was just a toy Prolog implemented in Lisp and then
other languages.

~~~
hyperpape
Definitely not the intent: [http://minikanren.org/minikanren-and-
prolog.html](http://minikanren.org/minikanren-and-prolog.html).

(I also doubt it's the result, though I've not done enough with either
language to comment too much).

~~~
7thaccount
I read the article and small/minimal...etc isn't that much different than a
toy in some respects, but see your point of intent. I've seen a few
implementations and didn't get the feeling of "production worthiness".

------
7thaccount
I think the Adventures in Prolog book is the best way to learn where you code
up a text adventure game. It made things click much better than any tutorial
for me.

In general though I really like "learn x in y" as a resource.

I like the concept of Prolog too, but to me it makes very hard things easier
and easy things hard. So it doesn't make sense in many situations to use.

------
DonHopkins
I always wanted to write a compiler whose front-end consumes Prolog and back-
end emits PostScript, and call it "PrologueToPostscript".

prologue: a separate introductory section of a literary, dramatic, or musical
work.

postscript: an additional remark at the end of a letter, after the signature
and introduced by ‘PS’.

------
piadodjanho
What are the differences from Prolog for a constraint programing language such
MiniZinc?

~~~
yaantc
MiniZinc is a purely declarative problem description language. It's a way to
describe a problem once, and feed it to different solvers easily. You can
provide hints for the search, but there is no more control on the solving
process.

Prolog is a "full" language. You can program any search strategy you want with
it.

I'd recommend starting with MiniZinc first, as it's easier. If it's not enough
and you need to customize the search strategy then once can move to Prolog,
particularly a modern one with constraint support.

~~~
piadodjanho
Any suggestion on what Prolog to use?

~~~
yaantc
Eclipse CLP (no relation to the IDE ;) has good built-in support and can also
interface to many external solvers. It's open source.

[https://eclipseclp.org/](https://eclipseclp.org/)

------
balthasar
What is y?

~~~
sansnomme
No.

~~~
DonHopkins
How many Prolog programmers does it take to change a lightbulb?

false.

[https://www.j-paine.org/dobbs/prolog_lightbulb.html](https://www.j-paine.org/dobbs/prolog_lightbulb.html)

------
xena
What is a good web framework or library for Prolog?

