Self Contained Namespaces

General APL language issues

Self Contained Namespaces

Postby paulmansour on Fri May 12, 2017 4:35 pm

If you are like me, you use a lot of unnamed namespaces as a datatype, just to pass around variables as a single neat package. If these namespaces contain references to other namespaces that are not children, you won't be able to serialize it (save it in a component file, etc) in the upcoming V16. This is change from V15, which would serialize it but then reading it back in, create all the sub-namespaces as real children. So, in order to save such a namespace in v16, we have to ensure that it is self contained. I think this does the trick:

      SelfContain←{
⍝ ⍵ ←→ Namespace
⍝ ←→ ⍵ self contained
n←⍵.⎕NL ¯9
0=≢n:⍵
v←⍵.⎕NS¨⍵⍎¨n
⍵⊣∇¨n ⍵.{⍎⍺,'←⍵'}¨v
}


Not that it matters too much for my purposes, but it appears this is not a tail call at the end, I thought due to the
      ⍵⊣
at the beginning of the last line. However, when I remove this, it still does not appear to be a tail call. Does the "each" get in the way of that?

Is there a way to make this a tail call?
paulmansour
 
Posts: 420
Joined: Fri Oct 03, 2008 4:14 pm

Re: Self Contained Namespaces

Postby paulmansour on Fri May 12, 2017 4:42 pm

On a related note, I have meaning to ask what the consensus is on injecting names and values into a namespace. I tend to use something like:

      n ⍵.{⍎⍺,'←⍵'}¨v


I've never liked this really. Are there any better alternatives? Would a new primitive or language feature make sense? I find this happens all the time in my code, and while I have cover functions for it, it is not a satisfying solution.
paulmansour
 
Posts: 420
Joined: Fri Oct 03, 2008 4:14 pm

Re: Self Contained Namespaces

Postby JohnS|Dyalog on Fri May 12, 2017 9:28 pm

"... does not appear to be a tail call. Does the "each" get in the way of that?"

Yes it does. Currently ∇ ¨ is not a tail call but I have logged an issue [14567] to investigate whether it would be possible.
JohnS|Dyalog
 

Re: Self Contained Namespaces

Postby Phil Last on Sat May 13, 2017 5:41 pm

paulmansour wrote:Would a new primitive or language feature make sense?
I've always wondered at the utilty of dyadic ⍎ given the fact that
      ns ⍎ string ←→ ns.⍎ string
surely
      names ⍎ values ←→ names {⍎⍺,'←⍵'} values
and
      names ns.⍎ values ←→ names ns.{⍎⍺,'←⍵'} values
would be much better uses for the dyad.
User avatar
Phil Last
 
Posts: 628
Joined: Thu Jun 18, 2009 6:29 pm
Location: Wessex

Re: Self Contained Namespaces

Postby Phil Last on Sat May 13, 2017 5:41 pm

JohnS|Dyalog wrote:"Currently ∇ ¨ is not a tail call but I have logged an issue [14567] to investigate whether it would be possible.

Some time in this or the previous century I asked a similar question of ∇ / but I can't remember if there was an answer.
User avatar
Phil Last
 
Posts: 628
Joined: Thu Jun 18, 2009 6:29 pm
Location: Wessex

Re: Self Contained Namespaces

Postby paulmansour on Sat May 13, 2017 7:24 pm

Phil, I like it!

At first glance it looks like your suggestion for dyadic execute would not conflict with the current definition and thus could be an enhancement.
paulmansour
 
Posts: 420
Joined: Fri Oct 03, 2008 4:14 pm

Re: Self Contained Namespaces

Postby JohnS|Dyalog on Sun May 14, 2017 7:05 am

Phil Last wrote:
JohnS|Dyalog wrote:"Currently ∇ ¨ is not a tail call but I have logged an issue [14567] to investigate whether it would be possible.

Some time in this or the previous century I asked a similar question of ∇ / but I can't remember if there was an answer.

If ∇ ¨ could be made a tail call, the same technique would apply to all primitive operator expressions, including reduction and, for example, ∇ ⍨ /.

But what about ⊃∇ / and ↑∇ /, which, it seems to me, appear in the majority of such reductions?

The disclose/mix would need either to be recognised at ⎕FX time as part of an idiom, or passed dynamically as a disclose/mix_when_you're_finished flag to the reduction. The latter technique is more general as it allows tail-reductions with any operand function in the same capsule, not just "self" ∇.
JohnS|Dyalog
 

Re: Self Contained Namespaces

Postby paulmansour on Sun May 14, 2017 10:58 am

I wonder if the new @ operator could do the job of injecting values v with names n into space s:

      (v@n)s


It seems to fit.

If the result was the space itself, which seems only natural (and what else would it be?), then we could create and populate a space in one go:

      s←(v@n)⎕ns ''


which I don't think we can do now.

Now all I need is a primitive to replace the offending ⎕NS ''!
Last edited by paulmansour on Sun May 14, 2017 11:44 am, edited 3 times in total.
paulmansour
 
Posts: 420
Joined: Fri Oct 03, 2008 4:14 pm

Re: Self Contained Namespaces

Postby paulmansour on Sun May 14, 2017 11:23 am

JohnS|Dyalog wrote:"... does not appear to be a tail call. Does the "each" get in the way of that?"

Yes it does. Currently ∇ ¨ is not a tail call but I have logged an issue [14567] to investigate whether it would be possible.


Thanks John.

Even if this were a tail call, is there a way I can get rid of the leading ⍵⊣ in my particular case above, and still get the result I want, which is the original right arg, and and not the result of ∇¨?
paulmansour
 
Posts: 420
Joined: Fri Oct 03, 2008 4:14 pm

Re: Self Contained Namespaces

Postby JohnS|Dyalog on Sun May 14, 2017 11:41 am

Even if this were a tail call, is there a way I can get rid of the leading ⍵⊣ in my particular case above, and still get the result I want, which is the original right arg, and and not the result of ∇¨?

If ∇ ¨ were a tail call, the only way that springs to mind is to pass back an ignored result from the recursion and call SelfContain via a (⊢⊣ ...) fork. Note line[0] in:

      SelfContain ← ⊢⊣{
⍝ ⍵ ←→ Namespace
⍝ ←→ ⍵ self contained
n←⍵.⎕NL ¯9
0=≢n:42 ⍝ (result ignored by above ⊢⊣ fork)
v←⍵.⎕NS¨⍵⍎¨n
∇¨n ⍵.{⍎⍺,'←⍵'}¨v ⍝ look, no ⍵⊣
}

Can someone think of a better way?
JohnS|Dyalog
 

Next

Return to Language

Who is online

Users browsing this forum: Bing [Bot] and 1 guest