Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Toy implementation of imperative variables and manual scoping
%commands to the interpreter are submitted from stdin input ('show input' box below) %'halt.' will be automatically appended to stdin input. %swi-prolog 7.6.4 %(basic) emulation of imperative local variables (with manual scoping) % In the following "scope" means a scope manually created with new_scope, or the global scope. % set(Symbol,Value) binds the value Value to the name Symbol in the current scope, and returns true. % e.g. set(abc,12) binds the value 12 to the name abc % set(f(X,a),5) binds the value 5 to all terms in the form f(_,a) % set_global(Symbol,Value) binds the value Value to the name Symbol in the global scope. % fetch(Symbol,Value) checks if Symbol is bound to Value, in the topmost scope in which it is bound. % If Symbol isn't bound in any scope visible from the curret scope, ir returns false. % new_scope/0 creates a new scope. Always returns true. % end_scope/0 ends the topmost scope. Returns false iff it is called in the global scope. :- dynamic get_var/3. set_var(Scope,Name,Value) :- asserta( (get_var(Scope,Name1,Value1) :- Name1=Name, !, Value1=Value) ). :- dynamic next_scope_id/1. :- dynamic active_scope/1. next_scope_id(0) :- !. active_scope([]) :- !. new_scope :- next_scope_id(Id), NextId is Id+1, asserta(next_scope_id(NextId) :- !), active_scope(L), asserta(active_scope([Id|L]) :- !), !. end_scope :- active_scope(S), S=[_|T], asserta(active_scope(T) :- !), !. set(Var,Value) :- active_scope(S), set_var(S,Var,Value). set_global(Var,Value) :- set_var([],Var,Value). fetch_in_scope(S,Var,Value) :- (get_var(S,Var,Value), !); S=[_|T], fetch_in_scope(T,Var,Value). fetch(Var,Value) :- active_scope(S), fetch_in_scope(S,Var,Value). %% EXAMPLE %% main :- set(x,"in outer scope"), new_scope, set(x,"in inner scope"), fetch(x,Y), write(Y), nl, % ==> prints "in inner scope" end_scope, fetch(x,X), write(X), nl. % ==> prints "in outer scope" :- main.
run
|
edit
|
history
|
help
1
00 Huhu-Dea
Luis Fernando Jiménez De Jesús_Ejercicio2_IngenieriaDeConocimiento_Examen
a
Base conocimiento SUPER QUERYS
BD.3
Codificador de arreglos y objetos
Gramatica JSON V1.1
ARBOL B. BALANCES - 4Mercados Indrustias
is_sort
Alcala Ornelas Carlos Alberto #1