How many of you realize that SWI-Prolog is an excellent, genuinely excellent language?

The beauty of SWI-Prolog - hotloading code

Just along the lines of what has been posted, recently I had a long running process (hours and hours) and I needed to correct a small issue. I couldn’t stop the process. Well…SWI-Prolog came to the rescue: it is able to hot load code on the fly!

Here is a little example.

:- use_module(library(prolog_server)).
:- prolog_server(4333,[]).

longrunning :-
    flag(counter,C,C+1),
	format('~w blippy blops so far~n',[C]),
	sleep(3),
	longrunning.

Run this in one terminal window:

26 ?- longrunning.
...
232306 blippy blops so far
232307 blippy blops so far
232308 blippy blops so far
232309 blippy blops so far
232310 blippy blops so far
232311 blippy blops so far

Now, those numbers are getting hard to read, wouldn’t it be nice if I could just fix the output, printing easier to read numbers, without stopping the process?

Well, SWI-Prolog does it!
In your favorite editor fix the format/2 statement changing this:

	format('~w blippy blops so far~n',[C]),

to this:

	format('~D blippy blops so far~n',[C]),

Save the file, and now we can hotload the code! In another terminal, run this:

$ telnet localhost 4333   % yes, ssh is supported with the libssh addon
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Welcome to the SWI-Prolog server on thread client@localhost.localdomain

1 ?- make.
...

Now look at the output in the first terminal :grin: :

26 ?- longrunning.
...
232306 blippy blops so far
232307 blippy blops so far
232308 blippy blops so far
232309 blippy blops so far
232310 blippy blops so far
232311 blippy blops so far
232312 blippy blops so far
232313 blippy blops so far
232,314 blippy blops so far      % right here the code was hotloaded
232,315 blippy blops so far
232,316 blippy blops so far
232,317 blippy blops so far
232,318 blippy blops so far
232,319 blippy blops so far

I have only been able to do this with erlang in the past.
Certainly, SWI-Prolog is a gem.


EDIT

By @EricGT

This reply and its replies were moved to a new topic.

3 Likes