
A “Hello World” Server in Prolog - eatonphil
http://www.swi-prolog.org/howto/http/HelloText.html
======
jdp23
Great to see ongoing interest in Prolog. I did my Master's thesis in Prolog
back in the "Fifth Generation" days [1] ... obviously the hype was waaaaay
overstated but it still significantly influenced my thinking. I really think
of it as one of those languages like Lisp or ML that opens your mind even if
you don't wind up using it in your day-to-day job.

Plus, I thought it was entertaining to see this on the front page at the same
time as "Hello, declarative world" :)

[1] [https://en.wikipedia.org/wiki/Fifth-
generation_programming_l...](https://en.wikipedia.org/wiki/Fifth-
generation_programming_language)

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

~~~
samuell
And I did mine more recently (2010) [1] :) (Mostly semantic web stuff ...
showing how it can far outperform Java based SPARQL implementations for some
stuff).

It was a real joy to work with semantic web data in SWI-prolog! Prolog lets
you raise your level of abstraction so quickly, as defining prolog rules
basically is a form of creating "named queries", something that is not
possible e.g. in SPARQL (the canonical semweb query language).

I can't understand why this feature of prolog is not implemented more widely
in more recent systems. The only example I know of that does, is Datomic [2],
by Rich Hickey.

[1] [http://www.diva-
portal.org/smash/record.jsf?pid=diva2%3A3988...](http://www.diva-
portal.org/smash/record.jsf?pid=diva2%3A398839&dswid=-8767)

[2] [http://www.datomic.com](http://www.datomic.com)

~~~
iso8859-1
How exactly can I use Prolog in Datomic? I only saw Datalog.

~~~
samuell
Yeah, I meant that it uses the ideas of "named queries" from prolog, which is
included in the datalog IIUC.

------
chollida1
We're a pretty heavy user of Prolog for several systems. We use it for doing
end of day allocations to prime brokers to determine the optimum allocations,
basically a constraint solver.

I had it in the trading system for some time but I had to pull it out as I
couldn't find a way to make its runtime deterministic enough. Most of the time
it would be great and then on occasion I'd get 100 millisecond pauses:( I
could never figure out if it was me doing something, the C bindings we were
using or if Prolog just has a mind of its own:)

If anyone has any pointers on how to reason about the run time efficiency and
performance of your prolog code I'd be willing to buy you a coffee!

[http://www.amazon.com/Programming-Artificial-Intelligence-
In...](http://www.amazon.com/Programming-Artificial-Intelligence-
International-Computer/dp/0321417461)

Awesome book, can usually be found in any used university book store!

~~~
tom_mellior
> on occasion I'd get 100 millisecond pauses

As a first guess, this sounds like garbage collection pauses. Details of what
to do about them depend on the particular implementation.

In SWI you can use the profiler to see if the garbage collector is in fact
being called when you don't expect it. Then: (a) change the default (veeery
low) garbage collector settings to use more memory before collection is
triggered; (b) make sure you're not leaving unnecessary choice points around
(listed as "redo" by the profiler, fix with correctly placed green cuts); (c)
call garbage_collect/0 yourself at well-defined points when you're currently
not doing low-latency stuff; (d) possibly (but I have no personal experience
with this) turn off the garbage collector completely for certain sections of
the code.

~~~
userbinator
_change the default (veeery low) garbage collector settings to use more memory
before collection is triggered_

I'm not familiar with Prolog GC but in other systems, making GC less frequent
just tends to cause each collection to take even longer, as more objects have
been created between each pass.

~~~
tom_mellior
That is true as a general trend, but in my experience with SWI-Prolog the
effect is not linear, and frequent small collections do cost more (in MY
applications). For one thing, if you have long-lived data used throughout the
program's whole lifetime, they will be visited on every collection without
ever being freed. For another thing, if you have enough memory to fit all the
data ever allocated by the program, you may not need to collect at all! Also,
as mentioned in my previous comment, depending on the application there may be
program parts where it is OK to trigger even a long-ish collection manually,
while you want other parts to be fast and collection-free.

So, to reiterate: You are right in broad terms, but it all depends very much
on the details of the application.

------
mindcrime
If anybody is interested in Prolog, you might find this old blog post of mine
useful. I cataloged a number of pretty useful online (freely available, legal)
Prolog learning resources:

[http://fogbeam.blogspot.com/2013/05/prolog-im-going-to-
learn...](http://fogbeam.blogspot.com/2013/05/prolog-im-going-to-learn-
prolog.html)

Sadly I've only just now _finally_ gotten around to investing some time in to
trying to learn Prolog myself. I got as far as creating myself a Github
repo[1] and writing "Hello, World" and figuring out how to run Prolog programs
at least. It's not much, but ya gotta start somewhere.

[1]:
[https://github.com/mindcrime/LearningProlog](https://github.com/mindcrime/LearningProlog)

~~~
nitrogen
I'm glad to know I'm not the only person who creates "learning_xyz"
repositories that may or may not progress very far, though I've yet to share
any of mine online.

~~~
mindcrime
I only recently started sharing mine. I decided there was no real reason not
to, and possibly some advantages in doing so. For one, it lets anyone looking
at my GitHub profile know what kinds of things I'm into, beyond my "primary"
projects. And it opens up the possibility of collaborating with friends and
acquaintances (or, as far as that goes, total strangers) on some interesting
stuff. I also find it motivational, especially when I work on increasing
length of my "streak" of activity on GitHub.

~~~
nitrogen
Easiest way to have a long streak: run daily backups that push to GitHub via
cron ;-)

~~~
mindcrime
Yeah, but that kinda defeats the point. What I've been trying to do is focus
on making at least one commit a day that at least fixes something trivial,
like replacing a "println" statement with a "log.debug" or "log.info" or
whatever (in some Grails code). This way I'm both slowly and steadily
improving the code, and I get to push my streak.

Still, my long streak is only 14 days so far. :-(

------
Kutta
I had a Prolog class in university and also fiddled with it a bit
extracurricularry. My biggest gripe was that (aside from being untyped and
having poor support for higher-order programming, but that's probably remedied
in more modern logic programming languages such as Mercury) for more complex
use cases and computations the system degrades into functional programming
with poor syntax: logical relations become deterministic functions with
designated inputs and output, at which point I would be just better off with
FP + backtracking (for example, the list monad in Haskell).

------
penetrarthur
I like how posts about Prolog get populular very fast, but have little to no
comments. People have heard that this language is special but have never
actually used it in production, so they hope that someone will shed the light
on it. Unfortunatelly it usually ends with a copypasted list of 15
projects(including Watson) where Prolog is used and thats it. Maybe someone
can share his/her production level experience with it this time...

~~~
hencq
I've never actually used Prolog, but like you say, it always seems like a
really cool language. That said, I always wondered why it needs to be a
language instead of just a library in another language. It seems there are
some types of problems that are really well suited to Prolog, but I don't
think it necessarily applies to all problems. So why not use another language
and then bring in the relational piece as a library? Granted, I think that's
what Kanren does in Scheme, but it doesn't really seem to be all that common.

Also, maybe I misunderstand, but doesn't using Prolog limit you to the
unification algorithm used? If you wanted to build something like the
Cassowary constraint solver in Prolog would it be really easy or would you
still need to implement the algorithm from scratch?

~~~
dragonwriter
> That said, I always wondered why it needs to be a language instead of just a
> library in another language.

It can be -- and often is. Depending on the language, the syntax may not be as
convenient as using Prolog directly.

------
cronjobber
This reminded me of O'Keefe's Prolog Well-formed Pages, one of the cleanest
templating approaches.

[http://www.cs.otago.ac.nz/staffpriv/ok/pwp.pl](http://www.cs.otago.ac.nz/staffpriv/ok/pwp.pl)

------
webnrrd2k
A suggestion to anyone who's interested in logic languages, like prolog: Give
The Reasoned Schemer by Daniel P. Friedman a try. It's an easy, fun book, with
surprising depth if you give it some effort and thought. .

------
pbowyer
When I used Prolog at university, we built really cool constraint solvers
using hardcoded predicates, but is it possible to create the predicates
dynamically? To be useful, if a user has a GUI where they can define rules,
they've got to be loaded into the program somehow... hopefully not through
code generation?

------
mark_l_watson
I think one of Swi-Prolog's great advantages is the large library, especially
tools for the semantic web. I have not had any consulting work using Prolog in
at least 8 years and don't do much for-fun Prolog programming either. I used
to find Prolog especially good for hacking together quick prototypes.

------
0xFFC
I am OS guy . so I don't have so much experience in other than C. Yesterday I
was watching youtube video about prolog , and I amazed by Logic Programming
concept.Maybe for most of you it is ordinary concept , but for it was
wonderful and I have plan to continue it in my spare time.

------
debacle
Anyone have a mirror?

~~~
eatonphil
Is it down for you?

[https://web.archive.org/web/20150913080629/http://www.swi-
pr...](https://web.archive.org/web/20150913080629/http://www.swi-
prolog.org/howto/http/HelloText.html)

------
timothycrosley
For comparison:

Here's the same thing in modern Python:
[https://news.ycombinator.com/item?id=10221707](https://news.ycombinator.com/item?id=10221707)

