Find: Difference between revisions

Jump to navigation Jump to search
3,194 bytes removed ,  14:02, 23 August 2022
→‎Empty left argument: Cut "overlay model" discussion: see talk page
(→‎Empty left argument: Cut "overlay model" discussion: see talk page)
Line 52: Line 52:
* [[APLX]] never finds any empty arrays.
* [[APLX]] never finds any empty arrays.
* [[APL+]] finds empty arrays everywhere, even where they would extend beyond the edges of the right argument.
* [[APL+]] finds empty arrays everywhere, even where they would extend beyond the edges of the right argument.
=== Discussion ===
In 2021, internal discussions about the correctness of the implemented primitive for empty left arguments happened at [[Dyalog Ltd]].<ref>[[Dyalog Ltd]]. Internal emails. ''more ⍷ follies
'', 15–19 Feb; ''ancient bug in ⍷ with empty left argument'', 7–8 Apr, and 26 May 2021.</ref>


In February, [[Roger Hui]] posed that the primitive had a bug in that it was finding empty subarrays of the wrong type, while defined in terms of [[match]] (<source lang=apl inline>≡</source>), which does distinguish between empty arrays of unequal type. [[Adám Brudzewsky]] devised an alternative mental model to describe Find's behaviour where, rather than checking if the left argument could be ''extracted'' from the right argument by peeling off outer elements, one could check if the left argument could be ''overlaid'' on the right argument, without the right argument changing. He devised two almost identical models, to emphasise the difference between the extraction model and the overlay model:
The prototype is never used, in contrast to [[Match]], which in many APLs compares prototypes of empty arrays. The behavior may come from the use of the [[inner product]] <source lang=apl inline>.=</source> in early [[Array_model#Flat_array_theory|flat]] APLs where [[Match]] is not a primitive; this function naturally checks elements and not the prototype. [[Adin Falkoff]] presented code for Find-like functions using <source lang=apl inline>∧.=</source> at [[APL79]].<ref>[[Adin Falkoff|Falkoff, Adin]]. [https://dl.acm.org/doi/10.1145/390009.804448 A note on pattern matching: Where do you find the match to an empty array?] at [[APL79]].</ref>
<source lang=apl>
ee←{ ⍝ extraction model
    ⎕IO←0
    ra←≢sa←⍴⍺ ⋄ rw←≢sw←⍴⍵
    rm←ra⌈rw
    rm>ra:⍵ ∇⍨⍺⍴⍨sa,⍨1⍴⍨rm-ra
    sa∨.>rm↑sw,¯1:sw⍴0
    _Extract_←{ ⍝ does extracting ⍺⍺ from ⍵⍵ change ⍺⍺?
        ⍺⍺≡⍺↑⍵↓⍵⍵
    }
    sw↑sa∘(⍺ _Extract_ ⍵)¨(-⍨∘×⍨sa)↓⍳sw
}
eo←{ ⍝ overlay model
    ⎕IO←0
    ra←≢sa←⍴⍺ ⋄ rw←≢sw←⍴⍵
    rm←ra⌈rw
    rm>ra:⍵ ∇⍨⍺⍴⍨sa,⍨1⍴⍨rm-ra
    sa∨.>rm↑sw,¯1:sw⍴0
    _Overlay_←{ ⍝ does overlaying ⍺⍺ on ⍵⍵ change ⍵⍵?
        ⍵⍵≡⍺⍺@((⍳⍺)+⊂⍵)⊢⍵⍵
    }
    sw↑sa∘(⍺ _Overlay_ ⍵)¨(-⍨∘×⍨sa)↓⍳sw
}
</source>
[[Morten Kromberg]] speculated that the behaviour stemmed from early [[Array_model#Flat_array_theory|flat]] APL where [[Match]] didn't exist. Instead, common practice was to use [[And]]-[[reduce|reduction]] (often written as the [[inner product]] <source lang=apl inline>∧.=</source>) over element-wise [[equal to|equality]], which ignores type mismatches because the comparison of two empty arrays (a [[scalar function]] application) itself is empty, thus making the reduction yield the [[identity element]] of And, which is true (<source lang=apl inline>1</source>).
 
In April, Hui wrote that he ''disagree[d] strongly with the "alternative APL and mental model"'' which Brudzewsky had devised, because it ''among other things [meant he] can not give a good accounting of it.  Also that all the descriptions (APL or non-APL) of string search/find that [he had] seen do not use that mental model.''.
 
Kromberg agreed with Hui that Brudzewsky's model was ''strained at best'' and ''clearly a modern construction based on a more complete understanding of <source lang=apl inline>≡</source> and prototypes, than a possible explanation for what the implementors where thinking when they did this work.'' He reiterated his theory about And-reductons over equality in a moving window, thus posing that the current behaviour can be seen as correct.
 
In May, Brudzewsky found support for Kromberg's theory, based on that exact usage in a conference proceeding,<ref>[[Adin Falkoff|Falkoff, Adin]]. [A note on pattern matching: Where do you find the match to an empty array?] [[APL79]]. doi:[https://doi.org/10.1145/800136.804470 10.1145/800136.804470].</ref> finding that redefining <source lang=apl inline>≡</source> accordingly as <source lang=apl inline>{(⍺≡⍥⍴⍵)∧(∧/⍺≡¨⍥,⍵)}</source> would make [[#Model|Hui's <source lang=apl inline>ebar</source> model]] align with the behaviour of the primitive as implemented. Hui promised to write an appendix to his earlier forum post ''at an appropriate time'', but passed away before being able to do so.


== See also ==
== See also ==

Navigation menu