System Labels: Difference between revisions

From NARS2000
Jump to navigationJump to search
No edit summary
No edit summary
 
(10 intermediate revisions by the same user not shown)
Line 1: Line 1:
<p>Normally, execution of a user-defined function/operator starts execution at line one.  However, in certain contexts, execution may start at a different line depending upon the context and the presence of a <b>System Label</b>.  These special labels start with a quad symbol so as to present no confusion with normal labels.  The following table lists the system labels defined so far and the special context in which they are used.</p>
<p>Normally, execution of an [[Anonymous_Functions/Operators/Hyperators|Anonymous Function/Operator/Hyperator]] (AFOH) or User-Defined Function/Operator/Hyperator (UDFOH) starts execution at line one.  However, in certain contexts, execution may start at a different place depending upon the context and the presence of a <b>System Label</b>.  These special labels start with a quad symbol so as to distinguish them from normal labels.  The following table lists the system labels defined so far and the special context in which they are used.</p>


<table border="1" cellpadding="5" cellspacing="0" rules="none" summary="">
<table border="1" cellpadding="5" cellspacing="0" rules="none" summary="">
Line 6: Line 6:
     <table border="0" cellpadding="5" cellspacing="0" summary="">
     <table border="0" cellpadding="5" cellspacing="0" summary="">
     <tr>
     <tr>
       <td valign="top"><apll>⎕PROTOTYPE:</apll></td>
       <td valign="top"><apll>⎕ID:</apll></td>
       <td></td>
       <td></td>
       <td></td>
       <td></td>
       <td>is called where a prototype element is needed as in <apll>foo¨R</apll> where <apll>R</apll> is empty.</td>
       <td>is called when an identity element is needed as in <apll>foo/R</apll> where <apll>R</apll> is empty.</td>
     </tr>
     </tr>
    <tr>
    <td valign="top"><apll>⎕INV:</apll></td>
    <td></td>
    <td></td>
    <td>is called when an inverse function is needed as in <apll>foo⍣¯1 R</apll>.</td>
  </tr>
  <tr>
      <td valign="top"><apll>⎕MS:</apll></td>
      <td></td>
      <td></td>
      <td>is called when an AFOH/UDFOH is invoked by the [[Multisets|Multiset Operator]] as in <apll>foo⍦R</apll>.</td>
    </tr>
     <tr>
     <tr>
       <td valign="top"><apll>⎕IDENTITY:</apll></td>
       <td valign="top"><apll>⎕PRO:</apll></td>
       <td></td>
       <td></td>
       <td></td>
       <td></td>
       <td>is called where an identity element is needed as in <apll>foo/R</apll> where <apll>R</apll> is empty.</td>
       <td>is called when a prototype element is needed as in <apll>foo¨R</apll> where <apll>R</apll> is empty, or <apll>L foo¨R</apll> where one of both of <apll>L</apll> or <apll>R</apll> is empty and the other is conformable.</td>
     </tr>
     </tr>
   </table>
   </table>
Line 22: Line 37:
</table>
</table>
<br />
<br />
<p>For example,</p>
<p>For example, as a UDFOH</p>
 
<apll><pre>
    ∇ Z←{L} foo R
[1]  Z←L,R ⋄ →0
[2]  ⎕ID :Z←'Identity' ,(⎕NC 'L'),(⎕NC 'R') ⋄ →0
[3]  ⎕INV:Z←'Inverse'  ,(⎕NC 'L'),(⎕NC 'R') ⋄ →0
[4]  ⎕MS :Z←'Multiset' ,(⎕NC 'L'),(⎕NC 'R') ⋄ →0
[5]  ⎕PRO:Z←'Prototype',(⎕NC 'L'),(⎕NC 'R') ⋄ →0
    ∇
</pre></apll>
 
or equivalently as an AFOH
 
<apll><pre>
      foo←{
<span style="color:blue;">➥</span>0:⍺←0 ⋄ ⍺,⍵
<span style="color:blue;">➥</span>⋄ ⎕ID :'Identity' ,(⎕NC '⍺'),⎕NC '⍵'
<span style="color:blue;">➥</span>⋄ ⎕INV:'Inverse'  ,(⎕NC '⍺'),⎕NC '⍵'
<span style="color:blue;">➥</span>⋄ ⎕MS :'Multiset' ,(⎕NC '⍺'),⎕NC '⍵'
<span style="color:blue;">➥</span>⋄ ⎕PRO:'Prototype',(⎕NC '⍺'),⎕NC '⍵'}
      )BOX ON
Was OFF
      foo/⍬
┌──────────────┐
│┌8───────────┐│
││Identity 0 2││
│└────────────┘2
└∊─────────────┘
      {⍺+÷⍵ ⋄ ⎕ID:∞}/⍬
      {⍺+÷⍵ ⋄ ⎕ID:∞}\7⍴1
1 2 1.5 1.666666667 1.6 1.625 1.615384615
 
      foo⍣¯1 1
┌9───────────┐
│ Inverse 0 2│
└+───────────┘
 
      foo⍦ 1
Multiset 0 2
      2 foo⍦ 1
Multiset 2 2
      1 foo¨⍬
┌0──────────────┐
│┌11───────────┐│
││          0 0││
│└─────────────┘2
└∊──────────────┘
      foo/¨0⍴⊂⍬
NONCE ERROR
      foo/¨0⍴⊂⍬
        ∧
</pre></apll>


<apll>&nbsp;&nbsp;&nbsp;&nbsp;∇ Z←L foo R<br />
* <p>In the Identity element case, the AFOH/UDFOH is called with the Reduction function's right argument '''prototype''' as the right argument to the AFOH/UDFOH; the left argument is undefined.  For example, in <apll>foo/3 0⍴⊂⍳4</apll>, the (right) argument passed to the <apll>⎕ID:</apll> entry point is <apll>0 0 0 0</apll>.  The return value from the AFOH/UDFOH is used as the common item in the result.  Thus, the <apll>⎕ID</apll> entry point is called only once even though the result may have multiple copies of the return value.</p>
[1]&nbsp;&nbsp;&nbsp;Z←L,R ⋄ →0<br />
[2]&nbsp;&nbsp;&nbsp;⎕PROTOTYPE:Z←'Prototype' ⋄ →0<br />
[3]&nbsp;&nbsp;&nbsp;⎕IDENTITY:Z←'Identity'<br />
&nbsp;&nbsp;&nbsp;&nbsp;∇<br /></apll>


<apll>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;⎕fmt foo/⍳0<br />
* <p>In the Inverse case, the AFOH/UDFOH is called with the same argument(s) as the Inverse-derived function.</p>
┌──────────┐<br />
│┌8───────┐│<br />
││Identity││<br />
│└────────┘2<br />
└∊─────────┘<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;⎕fmt 1 foo¨⍬<br />
┌0──────────┐<br />
│┌9────────┐│<br />
││Prototype││<br />
│└─────────┘2<br />
└∊──────────┘<br /></apll>


<p>In the Identity element case for '''reduction''', the function is called with the reduction function's right argument's prototype as the right argument to the user-defined function/operator; the left argument is undefined.  For example, in <apll>foo/3 0⍴⊂⍳4</apll>, the (right) argument passed to the function is <apll>0 0 0 0</apll>.</p>
* <p>In the Multiset case, the AFOH/UDFOH is called with the same argument(s) as the Multiset-derived function.</p>


<p>In the Identity element case for '''inner product''', the function is called with the inner product function's left argument's prototype as the left argument and the right argument's prototype as the right argument to the user-defined function/operator.  For example, in <apll>(3 0⍴⊂⍳4)foo.+0 2⍴⊂⍳2</apll>, the left argument passed to the function is <apll>0 0 0 0</apll> and the right argument is <apll>0 0</apll>.</p>
* <p>In the Prototype case, the AFOH/UDFOH is called with arguments that are the '''prototypes''' of the respective arguments; in the monadic case, the left argument is undefined.</p>


<p>In both of the above cases, the <apll>⎕IDENTITY</apll> entry point is called only once even though the result may have multiple copies of the identity element.</p>
* <p>For the moment, if two or more system labels would be called at the same time (such as the last example above, which needs both a Prototype and Identity element), a <apll>NONCE ERROR</apll> is signaled.  Eventually, this case will be handled by placing all appropriate System Labels on the same line as in <apll>⎕ID:⎕PRO:</apll>.</p>


<p>In the Prototype case, the user-defined function/operator is called with arguments that are the prototypes of the respective arguments.</p>
* This idea was taken from the paper by David A. Rabenhorst, "[http://portal.acm.org/citation.cfm?id=801228 APL function variants and system labels]", ACM SIGAPL APL Quote Quad, APL83, Volume 13,  Issue 3  (March 1983), pp. 281-284.

Latest revision as of 19:53, 18 September 2022

Normally, execution of an Anonymous Function/Operator/Hyperator (AFOH) or User-Defined Function/Operator/Hyperator (UDFOH) starts execution at line one. However, in certain contexts, execution may start at a different place depending upon the context and the presence of a System Label. These special labels start with a quad symbol so as to distinguish them from normal labels. The following table lists the system labels defined so far and the special context in which they are used.

⎕ID: is called when an identity element is needed as in foo/R where R is empty.
⎕INV: is called when an inverse function is needed as in foo⍣¯1 R.
⎕MS: is called when an AFOH/UDFOH is invoked by the Multiset Operator as in foo⍦R.
⎕PRO: is called when a prototype element is needed as in foo¨R where R is empty, or L foo¨R where one of both of L or R is empty and the other is conformable.


For example, as a UDFOH

    ∇ Z←{L} foo R
[1]   Z←L,R ⋄ →0
[2]   ⎕ID :Z←'Identity' ,(⎕NC 'L'),(⎕NC 'R') ⋄ →0
[3]   ⎕INV:Z←'Inverse'  ,(⎕NC 'L'),(⎕NC 'R') ⋄ →0
[4]   ⎕MS :Z←'Multiset' ,(⎕NC 'L'),(⎕NC 'R') ⋄ →0
[5]   ⎕PRO:Z←'Prototype',(⎕NC 'L'),(⎕NC 'R') ⋄ →0
    ∇

or equivalently as an AFOH

      foo←{
0:⍺←0 ⋄ ⍺,⍵
⋄ ⎕ID :'Identity' ,(⎕NC '⍺'),⎕NC '⍵'
⋄ ⎕INV:'Inverse'  ,(⎕NC '⍺'),⎕NC '⍵'
⋄ ⎕MS :'Multiset' ,(⎕NC '⍺'),⎕NC '⍵'
⋄ ⎕PRO:'Prototype',(⎕NC '⍺'),⎕NC '⍵'}
      )BOX ON
Was OFF
      foo/⍬
┌──────────────┐
│┌8───────────┐│
││Identity 0 2││
│└────────────┘2
└∊─────────────┘
      {⍺+÷⍵ ⋄ ⎕ID:∞}/⍬
∞
      {⍺+÷⍵ ⋄ ⎕ID:∞}\7⍴1
1 2 1.5 1.666666667 1.6 1.625 1.615384615

      foo⍣¯1 1
┌9───────────┐
│ Inverse 0 2│
└+───────────┘

      foo⍦ 1
Multiset 0 2
      2 foo⍦ 1
Multiset 2 2
      1 foo¨⍬
┌0──────────────┐
│┌11───────────┐│
││          0 0││
│└─────────────┘2
└∊──────────────┘
      foo/¨0⍴⊂⍬
NONCE ERROR
      foo/¨0⍴⊂⍬
         ∧
  • In the Identity element case, the AFOH/UDFOH is called with the Reduction function's right argument prototype as the right argument to the AFOH/UDFOH; the left argument is undefined. For example, in foo/3 0⍴⊂⍳4, the (right) argument passed to the ⎕ID: entry point is 0 0 0 0. The return value from the AFOH/UDFOH is used as the common item in the result. Thus, the ⎕ID entry point is called only once even though the result may have multiple copies of the return value.

  • In the Inverse case, the AFOH/UDFOH is called with the same argument(s) as the Inverse-derived function.

  • In the Multiset case, the AFOH/UDFOH is called with the same argument(s) as the Multiset-derived function.

  • In the Prototype case, the AFOH/UDFOH is called with arguments that are the prototypes of the respective arguments; in the monadic case, the left argument is undefined.

  • For the moment, if two or more system labels would be called at the same time (such as the last example above, which needs both a Prototype and Identity element), a NONCE ERROR is signaled. Eventually, this case will be handled by placing all appropriate System Labels on the same line as in ⎕ID:⎕PRO:.