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).                            

?- logvars:get(myscope, a, Y).

?- 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:


Inlined below:

% (C) 2016 Ralf Laemmel
:- module(logvars, []).

get(+S, +N, -V): Retrieve the value V of the global variable named N
and "scoped" by the global variable S. The variable N is
"automatically" created with a fresh logical variable V as initial

get(S, N, V) :-
    ( nb_current(S, _) ->
          nb_setval(S, _) ),
    nb_getval(S, (Ns, Vs)),
    varnth0(Pos, Ns, N),
    nth0(Pos, Vs, V).   

varnth0(-Pos, ?Ns, +N): given a list Ns of atoms with a variable tail,
find the name N in Ns or, if not present, unify the first variable
position of Ns with N, and return the position as Pos. This is a
helper for get/3.

varnth0(Pos1, [N1|Ns], N2) :-
    ( N1 == N2 ->
   Pos1 = 0;
          varnth0(Pos2, Ns, N2),
          Pos1 is Pos2 + 1 ).
varnth0(0, [N1|_], N2) :-
    N1 = N2.


Megamodels of Coupled Transformations

While having fun at UvA in A'dam by means of teaching Haskell in a pre-master summer school, I also manage to walk across the street to see folks at CWI. Will be giving a presentation on coupled transformations and megamodeling.

Title: Megamodels of Coupled Transformations

Abstract: Many software engineering contexts involve a collection of coupled artifacts, i.e., changing one artifact may challenge consistency between artifacts of the collection. A coupled software transformation (CX) is meant to transform one or more artifacts of such a collection while preserving consistency. There are many forms of coupling—depending on technological space and application domain and solution approach. We axiomatize and illustrate important forms of coupling within the Prolog-based software language repository YAS (Yet Another SLR (Software Language Repository)) while relying on a higher-level predicate logic-based megamodeling language LAL for axiomatization and a lower-level megamodeling language Ueber for build management and testing.

Date and Time: 19 August, 11:00am

Related links:


An updated update on the software language book

Deleting this post as it superseded by newer updates.