% Simulated memory % Prolog variables are temporary local variables that can be lost % during backtracking. % The Prolog data base is global but does not associate a unique % value with each variable. % This file shows how to emulate a global random access memory. % ram(x, v) is a clause when and only when x is a variable and v its % current value. :-dynamic(ram/2). % allow ram to change. print_ram:-ram(X, VX), write('RAM['), write(X), write(']='), write(VX), nl,fail. print_ram:- write('-------------'), nl. initial_ram:-abolish(ram,2 ), !, assert(ram(nothing, empty)). % Now define semantics of assignment statements used with our RAM. % Notice these do not evaluate the value being stored. set(X,V):- remove_old(X), assert(ram(X, V)),!. remove_old(X):- (ram(X,Old), retract(ram(X,Old)); true),!. % The following two definitions will work with functions.plg to % allow variables in RAM to be used in functional expressions :-consult('functions.plg'). function(X,V):-ram(X,V),!. let(X,E):-EV es E, remove_old(X), assert(ram(X,EV)),!. :-write('intial_ram, print_ram, set(X,V), let(X,Expr) loaded'),nl,nl.