1:- module(gvar_syntax,[
    2 ('.')/2, 
    3 was_gvar/1,
    4 is_gvar/2]).

gvar_syntax - Global Variable Syntax

Author: Douglas R. Miles E-mail: logicmoo@gmail.com WWW: http://www.logicmoo.org Copyright (C): 2017

This program is free software; you can redistribute it and/or modify it.

   18:- use_module(library(dicts)).   19
   20
   21:- '$set_source_module'('$dicts').   22:- clause('.'(Dict, Func, Value),BODY),nb_setval(gvar_syntax, (':-'('.'(Dict, Func, Value),BODY))).   23:- redefine_system_predicate('system':'.'(_Dict, _Func, _Value)).   24:- 'system':abolish('$dicts':'.'/3).   25:- if(\+ current_prolog_flag(gvar_syntax,non_extendable)).   26:- dynamic('$dicts':'.'/3).   27:- multifile('$dicts':'.'/3).   28:- module_transparent('$dicts':'.'/3).   29:- endif.   30:- nb_getval(gvar_syntax,WAS),
   31 compile_aux_clauses([(('$dicts':'.'(Self,Func,Value):- 
   32   gvar_syntax:is_gvar(Self,Name),!,gvar_syntax:gvar_interp(Name,Func,Value))),WAS]).   33:- if(current_prolog_flag(gvar_syntax,non_extendable)).   34:- compile_predicates(['$dicts':'.'(_Dict, _Func, _Value)]).   35:- endif.   36:- system:import('$dicts':'.'/3).   37:- system:lock_predicate('$dicts':'.'/3).   38:- '$set_source_module'(gvar_syntax).   39
   40:- if(\+ current_prolog_flag(gvar_callable_syntax,false)).   41:- multifile((.)/2).   42:- dynamic((.)/2).   43:- module_transparent((.)/2).   44:- Head=..['.',Self, Func], assert((( Head :- debugging(gvar_syntax,gvar_callable_syntax(Self, Func)), '$dicts':'.'(Self, Func,_)))).   45:- endif.
 is_gvar(+Self, -Name) is det
Tests to see if Self is $(Name) or was_gvar($Name).
   53is_gvar(Self,Name):- 
   54    ground(Self),
   55    (Self='$'(Name);
   56     Self=was_gvar('$'(Name))),!.
   57/*
   58is_gvar(Self,Name):- 
   59    compound(Self),
   60    ((Self='$'(Name);
   61     (Self=was_gvar(DSelf),compound(DSelf),DSelf='$'(Name)))),!
   62*/
Wrapper that is callable
   69was_gvar(_).
 gvar_interp(+GVar, +Func, ?Value) is det
Get/Set GVar or call the previous Dict interpretor
   78% checked above gvar_interp(Name, Var, Value):- var(Var),!,make_dot(Name, Var,Value).
   79gvar_interp(Name, current(),Value):- !, nb_current(Name,Value).
   80gvar_interp(Name, get(),Value):- !,nb_getval(Name,Value).
   81gvar_interp(Name, value, Value):- !, gvar_unify(Name,Value).
   82
   83% the trick here is an undone linkval
   84gvar_interp(Name, let, Value):- !, b_setval(Name,Value),nb_linkval(Name,Value).
   85gvar_interp(Name, set, Value):- !, gvar_put(Name,Value).
   86gvar_interp(Name, set(Value),was_gvar($Name)):-!, gvar_interp(Name, set, Value).
   87gvar_interp(Name, let(Value),was_gvar($Name)):-!, gvar_interp(Name, let, Value).
   88gvar_interp(Name, clear(),was_gvar($Name)):-!, nb_delete(Name).
   89gvar_interp(Name, Missed,Value) :-  make_dot(Name,Missed,Value).
   90
   91
   92make_dot(Name, Missed,Value):- Value =.. ['.',$(Name),Missed].
   93
   94% gvar_unify(Name,Value):- nb_current(Name,Ref),!,freeze(Value,Value=Ref).
   95gvar_unify(Name,Value):- nb_current(Name,Was),!,Value=Was.
   96gvar_unify(Name,Value):- nb_linkval(Name,Value),freeze(Value,gvar_put(Name, Value)).
   97
   98
   99% Sets a copy and then unifies the value to the copy
  100gvar_put(Name,Value):- 
  101   nb_setval(Name,Value), % after duplicate_term
  102   nb_current(Name,Value). %  we still want the same variables (if possible)