A small experiment I wanted to do for some time. A Mini Erlang interpreter in ISO Prolog. The interpreter does only use pure ISO predicates and no extras. Practically it would need good tail recursion, since the interpreter loop is a tail recursive predicate.

Good tail recursion gives rise to what is also known as "perpetual processes". The programming pattern is:

p(State1) :-
n(State1,State2), p(State2).

The challenge in the above pattern is a good carbage collection. Best would be if most of the State1 gets reclaimed and the interpreter continues with State2. In a structure sharing interpreter State2 might have been built from State1, and reclamation can be difficult.

Ulrich Neumerkel once published a paper with a couple of corresponding test case. We did not yet run them, but there was some progress in release 0.9.2, and release 0.9.3 will deliver some tools to better watch the memory.

Now how does the Erlang loop look like? One major difference to a normal Prolog quary answer loop is the fact that variables bindings persists from query to query. This can be archived by the following pure ISO code:

% loop(+Assoc)
loop(M) :-
write('> '),
flush_output,
read_term(I,[variable_names(N)]),
merge_names(N,M,L),
eval(I,R),
write(R),
nl,
loop(L).

As the screenshot shows, variables are really carried over from query to query. In the example the variable X is bound in query #2 and then used again in query #3. No difference can be seen for the example between the behaviour of the real Erlang interpreter and our Mini Erlang interpreter written in ISO Prolog.

Precise Garbage Collection in Prolog
Jan Wielemaker and Ulrich Neumerkel
http://www.swi-prolog.org/download/publications/lifegc.pdf

Full Source Code of the Mini Erlang Interpreter
http://www.xlog.ch/jekejeke/perpetual/erlang.p
2
Add a comment...