As an aside, I created a stateful REPL a while ago for CHR development purposes. It doesn’t capture and reuse variables, but it does accumulate CHR objects. With a bit of effort I could probably make a top level for prolog that behaves like:
?- X = 5.
X=5.
?- double(X, Y).
Y=10.
?- X = 6.
fail.
?-
Not actually top level, but more like break mode. I guess the trouble comes when you want to reuse names, I guess you could have a directive to clear a variable or all variables from the working memory. Specifically, the REPL loses the names so you can reuse them. Losing the actual variable objects and their content is up to the GC.