Namespaces and Localisation
The rules for name resolution have been generalised for namespaces.
In flat APL, the interpreter searches the state indicator to resolve names referenced by a defined function or operator. If the name does not appear in the state indicator, then the workspace-global name is assumed.
With namespaces, a defined function or operator is evaluated in its 'home' namespace. When a name is referenced, the interpreter searches only those lines of the state indicator which belong to the home namespace. If the name does not appear in any of these lines, the home namespace-global value is assumed.
For example, if #.FN1
calls XX.FN2
calls #.FN3
calls XX.FN4
, then:
-
FN1
:- is evaluated in
#
- can see its own dynamic local names
- can see global names in
#
- is evaluated in
-
FN2
:- is evaluated in
XX
- can see its own dynamic local names
- can see global names in
XX
- is evaluated in
-
FN3
:- is evaluated in
#
- can see its own dynamic local names
- can see dynamic local names in
FN1
- can see global names in
#
- is evaluated in
-
FN4
:- is evaluated in
XX
- can see its own dynamic local names
- can see dynamic local names in
FN2
- can see global names in
XX
- is evaluated in
The following picture illustrates how APL looks down the stack to find names:
│ │ │
│ a+b+c │ │ [8] h references a, b and c
│ │ │ │ │ │
│ ∇h;a │ │ │ │ [7] h localises a
│ │ │ │ │
│ │ │ │ │ [6] g calls X.h
│ │ │ │ │
│ │ │ │ a+b+c │ [5] g references a, b and c
│ │ │ │ │ │ │ │
│ │ │ │ ∇g;a;│ c │ [4] g localises a and c
│ │ │ │ │ │
│ │ │ │ │ │ [3] f calls Y.g
↑ │ │ │ │ │ │
s │ a+b+c │ │ │ [2] f references a, b and c
t │ │ │ │ │ │ │
a │ ∇f;a;b │ │ │ │ [1] f localises a and b
c │ │ │ │ │
k │ a b c │ a b c │ [0] global names a, b and c
└X──────────┴Y──────────┘ in namspaces X and Y
The above diagram represents the SI stack, growing upwards from two namespaces X
and Y
, which each have three global names a
, b
and c
.
- Function
f
inX
localises namesa
andb
. -
Function
f
references namesa
,b
andc
.The interpreter looks down the stack and finds local names∇ f;a;b [1] a+b+c [2] Y.g
a
andb
inf
's header andc
in namespaceX
. -
Function
f
calls functiong
in namespaceY
. - Function
g
inY
localises namesa
andc
. -
Function
g
references namesa
,b
andc
.The interpreter looks down the stack and finds local names∇ g;a;c [1] a+b+c [2] X.h
a
andc
ing
's header andb
in namespacesY
. -
Function
g
calls functionh
in namespaceX
. - Function
h
inX
localises namea
. - Function
h
references namesa
,b
andc
.The interpreter looks down the stack and finds local name∇ h;a [1] a+b+c
a
inh
's header;b
inf
's header; andc
in namespaceX
.