calc :- init, prompt(_, '> '),
       repeat, read(X), do(X), X == exit, !.

init :- retractall(value(_)), assert(value(0)).

do(set(V)) :- retract(value(_)), assert(value(V)).

do(print) :- value(V), writeln(V).

do(exit).

do(double) :- value(V0), V is V0 * 2, do(set(V)).
do(halve) :- value(V0), V is V0 / 2, do(set(V)).

do(add(X)) :- value(V0), V is V0 + X, do(set(V)).

%do(sub(X0)) :- X is -X0, do(add(X)).

do(sub(X0)) :- do(add(-X0)). % works!

calc0 :- prompt(_, '> '), repeat, read(T), format('Read ~w~n', T), T = exit, !.

