Scoped global variables in Prolog
It just so happens that I needed global variables in some Prolog code. In fact, I needed more carefully scoped global variables. SWI-Prolog's global variables are really global. (Well, they are thread-scoped, for what it matters.) This is not good, if you need a lot of global variables and maybe even in different parts of an application. An uninspiring approach would be to fabricate global variable names in a manner that they are scoped internally by some name prefix. It was more fun to achieve scope by means of actually using one truly global variable to provide many scoped variables. Here is a demo: ?- logvars:get(myscope, a, X). true. ?- logvars:get(myscope, a, Y). true. ?- logvars:get(myscope, a, X), logvars:get(myscope, a, Y). X = Y . ?- logvars:get(myscope, a, X), logvars:get(myscope, b, Y). true . Here is the code: https://github.com/softlang/yas/blob/master/lib/Prolog/logvars.pro Inlined below: % (C) 2016 Ralf Laemmel :- module...