
Prolog Under the Hood: An Honest Look (1992) - networked
https://www.amzi.com/articles/prolog_under_the_hood.htm
======
YeGoblynQueenne
>> Although Prolog's original intent was to allow programmers to specify
programs in a syntax close to logic, that is not how Prolog works. In fact, a
conceptual understanding of logic is not that useful for understanding Prolog.

Look, I get what the article is saying, but the fact of the matter is that
Prolog has to run on real-world computers and those are not purely logical.
They have working memories and storage, they have processors that take in
instructions one-by-one and so on. It's all nice and well to say that you want
a purely declarative language but, well, we just don't have the hardware for
that sort of thing, at least not yet anyway (and not in the foreseeable
future, given that nobody is really working on that- not anymore, not after
the Fifth Generation Computer programme tanked).

So compromises have to be made. They have to be made at every step of the way.
Compromises like the selection rule for resolvents (this is what makes the
order of clauses in your program matter, when it doesn't in pure logic).
Compromises like having extra-logical predicates, like write/1 and get/1 (the
latter gets the next byte from the current input- what is a "byte" in logical
terms? It isn't). Compromises like the cut, !/0 and even that stupid fail/0,
that give you some control over the interpreter's program flow, which in and
of itself is something completely outside of logic.

But, this is how you get a language that is 90% of the way to the purely
declartive, logic programming ideal. You gotta be, whatsitcalled, pragmatic,
innit.

I also don't agree that teaching the operational semantics of the language
(the four-port model) is the best way to reveal the beauty of the language.
I've been coding in Prolog since around 2008, and I was very comfortable with
that model, but my understanding is still incomplete. I've recently read
"Foundations of Inductive Logic Programming" that has a big intro to logic and
logic programming with endless proofs about subsumption and the soundness of
resolution and so on, and it's like, I was blind in one eye and could only see
half the world. You gotta have a very solid grasp of both the theory and the
practice to code well in Prolog.

... and there's your problem, right there. The investment you have to make to
really master the language is huge and very few people have the resources for
it.

~~~
AlexCoventry
> The investment you have to make to really master the language is huge

What's the payoff?

~~~
YeGoblynQueenne
The payoff is that you master a skill that is hard to master, but I suspect
you're asking more about what are the rewards in practical terms.

For me, the extra effort I put into learning Prolog has actually paid off in
career advacement. First, because several potential employers noticed that I
listed Prolog in my CV as one of the programming languages I know (a CV is
where one is _required_ to brag about what they know, right?) and definitely
considered it a factor in their decision to at least interview me, or hire me
(I know because they told me). I could of course just list Prolog in my CV
without knowing any, but I could have been found out, and I'm a wuss so I'd
never do that.

Second, because my interest in Prolog led to an interest in AI, because a
majority of Prolog textbooks are also (Good, Old-Fashioned) AI textbooks and
reading them to learn Prolog led me to learn about AI, and eventually take a
Master's course in, well, actually, machine learning and data science, right
around the time when it was exploding in popularity.

And third, because my interest in Prolog combined with my Master's in machine
learning led me to apply for a PhD at one of the highest-rated technology
research universities in the UK (and in the world- Imperial College), to study
Inductive Logic Programming, which is basically machine-learning Prolog
programs from Prolog programs, under the supervision of the guy who,
literally, wrote the book.

Prolog has taken me places- places I'd never have been without it. Lucrative
places, and prestigious places. Anyway you look at it, for me at least, it was
a worthy investment in time and effort.

~~~
AlexCoventry
Thanks. Do you know of anyone using modern ML techniques to propose smarter
search strategies?

~~~
YeGoblynQueenne
Sorry, I haven't heard of anyone doing something like that. What do you have
in mind?

~~~
AlexCoventry
Something like reinforcement learning on neural-net parameters, to quickly
find solutions to a set of constraints.

~~~
YeGoblynQueenne
Hey, sorry, I missed this. No, I don't know about anything like that, but
constraint solving are not my cup of tea and I don't know the biblio. There
may be something like that around. I'd start by looking for papers on
constraint solving in IJCAI or AAAI where you're likely to find some, then
follow their references. You might unearth something like that.

~~~
AlexCoventry
No worries. Thanks for the response.

------
carapace
> After a brief exposure, however, many programmers fail to appreciate its
> full breadth and never use it again.

Yeah. That was me. A friend of mine tried to clue me in to Prolog about twenty
years ago and I didn't "get it".

Recently I revisited it and realized that about 1/3 of my working career was
wasted by not using Prolog.

My _strong_ suggestion to any programmers reading this: learn and use Prolog.

~~~
sgt101
20 years ago : you are right. Although be warned, Prolog has some killer
issues, principally the way that the knowledge base and search system works
means that you will end up stirring fiddle factors (cut) into your logic
program and at scale the brain boggling complexity of what is going on can
lead to complete, sudden and irretrievable project failure.

As in : it doesn't work, no one knows why, no one can fix it and no one has a
plan that doesn't involve starting from the beginning and doing it
differently.

consider two programs

Good prolog :

on(a,b).

on(b,c).

above(X,Y) :- on(X,Y).

above(X,Y) :- on(X,Z), above(Z,Y).

->above(a,c).

You'll get TRUE (yay) a is above c ! GOOOOOOOD prolog.

but now :

on(a,b). on(b,c).

above(X,Y) :- above(X,Z), on(Z,Y).

above(X,Y) :- on(X,Y).

-> above(a,c).

ERROR: Out of local stack

    
    
       Exception: (1,675,220) above(a, _4808) ? 
    

BAD BAD BAD BAD prolog. Go to your room!

ERROR: Out of local stack Exception: (1,675,220) above(a, _4808) ?

Prolog gets stuck expanding the first search until it can't carry on. One line
is out of order in the program and you are dead, and the problem is that you
will not find that line until that question is asked, and when you find it (in
a real example) it really is the devils own job to work out where the
expansion is coming and how to fix it.

But there you go - search based logic resolution, brought to you by 1970's
computer architecture (small main store) and solvers. Note, not only is "it
doesn't bloody work" an issue, but also (probably much worse) "it keeps
hanging for five minutes". This inconsistency and problem is hard to replicate
and very, very hard to debug. It's caused by an occasional big search
happening in the knowledge base, but a big search that doesn't end up
exploding the system, it's often accompanied by shifty developer behaviour
involving parameters and special cases. In a real project it's a nightmare.

Now : NOOOOOOO! Instead use Problog or something similar and reason using
Answer Sets. For your first steps an answer set system looks very like Prolog,
but does not suffer from the issues above (as far as I can see). There has not
been (so far) a big charge to use this in prod (with Prolog and KBS's there
was, it failed, because above) because everyone is playing with 40 layer 200
billion parameter neural networks and wondering why proving they can beat a
tiny part of one benchmark or mostly steer a car without killing many people
is not translating into application success. And this is a good thing because
I don't think that the tech is quite there yet and the community needs five
years and about half a dozen big "let's make this practical" applied research
projects (btw. keen on partners?) to actually make this manageable for people
with deadlines and investors.

But - it's going to help you more than Prolog.

~~~
YeGoblynQueenne
>> This inconsistency and problem is hard to replicate and very, very hard to
debug.

My experience is the opposite. Because Prolog is an interpreted langauge, and
because it tends to not need the global dependencies other languages have
(e.g. no variables declared outside a predicate's closure etc) it's very easy
and convenient to program in a "bottom-up" approach.

Which means, in plain English, that everytime you add a new predicate to your
program, you can test it immediately on the command line and see whether it
"goes infinite" or behaves in other ways strangely. It's kind of like test-
driven programming; let's say, REPL-driven programming.

And let's not forget that you can get in an infinite-loop situation with any
language. I have fond memories of working at a C# shop, where one line of code
in a shared lib brought down the entire stack of web apps deployed by the
company, because someone hadn't tested a terminating condition in an iterative
loop. The post-mortem went a bit strange and it seems (I wasn't present) that
upper management, who didn't know much about programming, advised that
iterative constructs like for-loops etc should be henceforth avoided like the
plague and _recursion_ used instead. Obviously, nobody even tried to do that.

In another incident, in the same shop, one of the designers caused the in-
house, jango-like, templating language, to go into an infinite recursion.
Apparently, the jango-like language (called "mango") was Turing complete.
Whaddaya know.

Playing with Turing machines is dangerous. They tend to get tied up in loops.
You should know that, understand why, and code as to avoid it _in any
language_ , not just Prolog.

~~~
sgt101
Yup - if you have control over your code base and can keep it totally
consistent you have a fighting chance, but as I say - some shifty person
introduces a problem and all goes very very wrong. The big demonstration is
that it's really rarely used in industry. Which is a big shame as if we could
sort out the programming in the large then I believe big value is on the
table. The new generation of reasoners are a good start!

~~~
zephyrfalcon
If "some shifty person introduces a problem", then that's bad news for any
code base in any language. Nothing specific to Prolog.

> The big demonstration is that it's really rarely used in industry.

By that reasoning, software written in Javascript must be extremely reliable
and easy to debug. >.>

------
tralarpa
(The last time I worked on a Prolog program was 20 years ago. It had only
around 4000 lines, so take the following with a grain of salt.)

For me, a work day in Prolog looked like this: Write your idea down in 1 hour
and then spend the remaining 7 hours figuring out where to place the cuts :)

~~~
marmaduke
Sometimes I think my Python code has 7 lines of validation or plumbing for
each line of algorithm.

~~~
jgalt212
not far from the truth, but I guess it comes down to where to you place the
technical debt (up front with statically typed langues), or after you have
something working (dynamic languages). I favor the later because of the low
cost of experimentation, but you do need get around to hardening the system,
and that part has real costs.

~~~
taeric
While i agree with the technical debt here, I think you slightly misplace it.
You take debt by using the libraries available on python. Same as you take
loans from others that have capital. The currency here is their knowledge.

That is, the dynamic/static divide is a distraction. Easy to talk towards, but
likely nowhere near as important as other factors.

------
kirillzubovsky
Writing Prolog was one of my favorite things in college. It's not the language
that appealed to me, but the underlying ability to express and validate logic.
It's so clean and delightful. I do wish more people would get introduced to
logic this way, it's a very powerful instrument for everyday life.

------
sifoobar
I think the real problem is that it's a lot more convenient to add logic
capabilities to whatever language than to squeeze the entire solution into
Prolog's limited view of the world. I know there are extensions and whatnots
to build database driven GUI's and web apps in Prolog, but everything I've
seen looks like one step to the side and two backwards. Possible does not
imply useful. If it was such a great idea, plenty of people would already be
doing it by now.

~~~
ghosthamlet
I think you are Partly right, but Logic programming is very useful and elegant
for some thing, like this static code analyzer for Clojure built by
core.logic: [https://github.com/jonase/kibit](https://github.com/jonase/kibit)

