<div dir="ltr"><div style>Just to be clear. The below does work in TR and idx is of type Index. However when one embeds the (in-range 3 5) directly in the for/vector idx is not of type Index. No reason why it shouldn't however.</div>
<div><br></div><div>(: v (Vectorof Symbol))</div><div>(define v '#(a b c d e))</div><div><br></div><div>(define: s : (Sequenceof Index) (in-range 3 5))<br></div><div><br></div><div>;; Efficient!!!</div><div>;; s : (Sequenceof Index) </div>
<div>;; See below for 's' construction.</div><div>(for/vector: : (Vectorof Symbol) </div><div> ([idx : Index s])</div><div> (vector-ref v idx))</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">
On Wed, Feb 27, 2013 at 4:34 PM, Ray Racine <span dir="ltr"><<a href="mailto:ray.racine@gmail.com" target="_blank">ray.racine@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr"><div>The below is just an observation. </div><div><br></div><div>Historically in TR I've always struggled with Index vs Fixnum vs Natural for the vector indexing type when iterating over a vector (or large sub-ranges or even larger vectors). Either the Optimizer complains that an index? check was not elided in a vector-ref OR you end up doing something along the lines of (assert (add1 idx) index?) while looping your index variable passing over the Vector.</div>
<div><br></div><div>But I've just noticed that one can construct TR recognized (Sequenceof Index). </div><div><br></div><div>So in theory, TR + Compiler should be able to elide a number of checks, such as (assert idx index?), use `unsafe-vector-ref', even elide in loop bounds checks if the vector is supplied to the 'for'. This assumes the (Sequenceof Index) is an efficient generator. i.e., (in-range 3 10000000) is not allocating 10000000 somethings. ( I don't know what it "really" is happening overall, just what I think is possible. Sufficiently smart compiler yada yada.)</div>
<div><br></div><div>;;;;;;;;;;;;;;;;</div><div>;; Example</div><div>;;;;;;;;;;;;;;;;</div><div><br></div><div><div>(: v (Vectorof Symbol))</div><div>(define v '#(a b c d e))</div></div>
<div><br></div><div>;; Efficient!!!</div><div>;; s : (Sequenceof Index) </div><div>;; See below for 's' construction.</div><div><div>(for/vector: : (Vectorof Symbol) </div><div> ([idx : Index s])</div>
<div> (vector-ref v idx))</div><div><br></div><div>However if one tries to inline the (in-range ...) construction, TR appears to over-generalized and infers (Sequenceof Positive-Fixnum) in lieu of the more compiler optimizable specific (Sequenceof Index).</div>
<div><br></div><div>;; Not optimal as idx is not recognized as restricted to type Index.</div></div><div><div>for/vector: : (Vectorof Symbol) </div><div> ([idx : Index (in-range 3 5)])</div><div> (vector-ref v idx))</div>
</div><div><br></div><div>;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;</div><div>;; Creating a (Sequenceof Index)</div><div>;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;</div><div><br>
</div><div><div>#lang typed/racket</div><div>(require racket/sequence)</div><div><br></div><div>;; Index bounds checking is occurring on construction!!</div><div>;; (define: s : (Sequenceof Index) (in-range 3 890235782389053905))</div>
<div>;; stdin::311: Type Checker: Expected (Sequenceof Index), but got (Sequenceof Nonnegative-Integer)</div><div>;; in: (in-range 3 890235782389053905)</div><div>;; ...</div><div><br></div><div>(define: s : (Sequenceof Index) (in-range 3 100000000))</div>
<div>(define-values (s-more? s-next?)(sequence-generate s))</div><div><br></div><div>;; > more?</div><div>;; - : (-> Boolean)</div><div>;; #<procedure:sequence-more?></div><div>;; > next?</div><div>;; - : (-> Index)</div>
<div>;; #<procedure:sequence-next></div><div><br></div><div>(: v (Vectorof Symbol))</div><div>(define v '#(a b c d e))</div><div><br></div><div>(when (s-more?)</div><div> (vector-ref v (s-next?))) ;; should be eliding index? check.</div>
<div><br></div></div></div>
</blockquote></div><br></div>