Prototype: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
Miraheze>Adám Brudzewsky
m (Adám Brudzewsky moved page Array prototype to Prototype over redirect: Merge with primitive function)
Miraheze>Adám Brudzewsky
No edit summary
Line 1: Line 1:
In the [[nested array model]], an array's '''prototype''' is an array which gives information about the type and structure of its [[Element|elements]]. It is derived from the first element of the array in [[ravel order]], or, if the array is [[Empty array|empty]], from information stored alongside the array (usually this information is just the prototype itself). An array's prototype is used as a [[fill element]] for operations involving that array.
{{Built-ins|Prototype|∊|⊤}} is a [[monadic function]] which in the [[nested array model]] gives information about the type and structure of its argument array's [[element]]s. The result is derived from the first element of the array in [[ravel order]], or, if the array is [[Empty array|empty]], from information stored alongside the array (usually this information is just the prototype itself). An array's prototype is used as a [[fill element]] for operations involving that array, and to determine some [[identity element]]s.
 
== Support==
Few dialects have Prototype as a [[primitive function]]. [[Dyalog APL]] uses <source lang=apl inline>∊</source> (as in ''prototypical '''e'''lement''), but only when [[migration level]] is 0, while [[NARS2000]] uses [[monadic]] <source lang=apl inline>⊤</source> (resembling the Latin letter '''T''' for '''''T'''ype'').
 
However, an arrays prototype can always be determined by enclosing the array in an empty array, and then coercing our a fill value. Since prototypes are used as fill elements, the resulting array will be the prototype of the original array:
<source lang=apl>
      ⊃ 0 ⍴ ⊂ 1 2 'ab'
┌─┬─┬──┐
│0│0│  │
└─┴─┴──┘
</source>


APLs with a [[flat array model]] do not typically store prototype information, instead using only the array's type ([[character]], [[numeric]], or [[Box|boxed]]) to determine its [[Fill element|fills]].
APLs with a [[flat array model]] do not typically store prototype information, instead using only the array's type ([[character]], [[numeric]], or [[Box|boxed]]) to determine its [[Fill element|fills]].
Line 57: Line 68:
{{Works in|[[Dyalog APL]]}}
{{Works in|[[Dyalog APL]]}}
{{APL programming language}}
{{APL programming language}}
{{APL built-ins}}

Revision as of 12:41, 13 November 2019

Prototype (, ) is a monadic function which in the nested array model gives information about the type and structure of its argument array's elements. The result is derived from the first element of the array in ravel order, or, if the array is empty, from information stored alongside the array (usually this information is just the prototype itself). An array's prototype is used as a fill element for operations involving that array, and to determine some identity elements.

Support

Few dialects have Prototype as a primitive function. Dyalog APL uses (as in prototypical element), but only when migration level is 0, while NARS2000 uses monadic (resembling the Latin letter T for Type).

However, an arrays prototype can always be determined by enclosing the array in an empty array, and then coercing our a fill value. Since prototypes are used as fill elements, the resulting array will be the prototype of the original array:

      ⊃ 0 ⍴ ⊂ 1 2 'ab'
┌─┬─┬──┐
│0│0│  │
└─┴─┴──┘

APLs with a flat array model do not typically store prototype information, instead using only the array's type (character, numeric, or boxed) to determine its fills.

Examples

The prototype is used when an array is expanded or when empty elements are inserted in some other way. Take and Expand on a character array produce an array with spaces, because ' ' is the prototype of a character array:

      ¯12 ↑ 'whitespace'
  whitespace
      (5 1 5/1 ¯2 1) \ 'whitespace'
white  space

The Rank operator combines its left operand's result cells by extending them to a common shape like Mix does. Numeric vectors of different lengths will be expanded with 0, the prototype for a simple numeric array.

      (⍳⍤0) 2 3 4
1 2 0 0
1 2 3 0
1 2 3 4
Works in: Dyalog APL

An array's prototype is based on its first element, so the array a below has a simple numeric prototype, as does any prefix of a. This property even applies to empty arrays: the expression 0 ↑ a results in an empty array with the same prototype as a.

      ⊢a ← 5 'c' ¯2
5 c ¯2
      4 ↑ a
5 c ¯2 0
      4 ↑ 2 ↑ a
5 c 0 0
      4 ↑ 0 ↑ a
0 0 0 0
Works in: Dyalog APL

The prototype of an array may be nested. Here, we inspect the prototype of an array containing both character and numeric data. In the prototype, all characters are converted to spaces and all numbers to zeros. Thus the prototype retains type and structure information but not specific values.

      ⊢x ← ⊂'de'(3 4 5)   ⍝ A complicated scalar array
┌──────────┐
│┌──┬─────┐│
││de│3 4 5││
│└──┴─────┘│
└──────────┘
      0 1 \ x             ⍝ Its fill element is nested
┌──────────┬──────────┐
│┌──┬─────┐│┌──┬─────┐│
││  │0 0 0│││de│3 4 5││
│└──┴─────┘│└──┴─────┘│
└──────────┴──────────┘
      e ← 0 ↑ x           ⍝ An empty array based on x
      ⊃e                  ⍝ Disclosing gets the prototype
┌──┬─────┐
│  │0 0 0│
└──┴─────┘
Works in: Dyalog APL

Template:APL programming language

APL built-ins [edit]
Primitives (Timeline) Functions
Scalar
Monadic ConjugateNegateSignumReciprocalMagnitudeExponentialNatural LogarithmFloorCeilingFactorialNotPi TimesRollTypeImaginarySquare Root
Dyadic AddSubtractTimesDivideResiduePowerLogarithmMinimumMaximumBinomialComparison functionsBoolean functions (And, Or, Nand, Nor) ∙ GCDLCMCircularComplexRoot
Non-Scalar
Structural ShapeReshapeTallyDepthRavelEnlistTableCatenateReverseRotateTransposeRazeMixSplitEncloseNestCut (K)PairLinkPartitioned EnclosePartition
Selection FirstPickTakeDropUniqueIdentityStopSelectReplicateExpandSet functions (IntersectionUnionWithout) ∙ Bracket indexingIndexCartesian ProductSort
Selector Index generatorGradeIndex OfInterval IndexIndicesDealPrefix and suffix vectors
Computational MatchNot MatchMembershipFindNub SieveEncodeDecodeMatrix InverseMatrix DivideFormatExecuteMaterialiseRange
Operators Monadic EachCommuteConstantReplicateExpandReduceWindowed ReduceScanOuter ProductKeyI-BeamSpawnFunction axis
Dyadic BindCompositions (Compose, Reverse Compose, Beside, Withe, Atop, Over) ∙ Inner ProductDeterminantPowerAtUnderRankDepthVariantStencilCutDirect definition (operator)
Quad names Index originComparison toleranceMigration levelAtomic vector