FWIW, the Scala folks have done something along these lines, Value Classes, originally aka Inline Classes.<div><br><div><a href="https://docs.google.com/document/d/10TQKgMiJTbVtkdRG53wsLYwWM2MkhtmdV25-NZvLLMA/edit?hl=en_US&amp;pli=1">https://docs.google.com/document/d/10TQKgMiJTbVtkdRG53wsLYwWM2MkhtmdV25-NZvLLMA/edit?hl=en_US&amp;pli=1</a><br>
<br><br><div class="gmail_quote">On Mon, Aug 27, 2012 at 12:58 PM, Neil Toronto <span dir="ltr">&lt;<a href="mailto:neil.toronto@gmail.com" target="_blank">neil.toronto@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im">On 08/27/2012 09:11 AM, Vincent St-Amour wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
TR&#39;s complex number optimizations eliminate repeated boxing and unboxing<br>
in chains of operations that each consume and produce complex numbers.<br>
<br>
Similar optimizations for structs would eliminate structs for<br>
intermediate results in chains of operations that each consume and<br>
produce that same (single) kind of struct.<br>
<br>
If your code has such chains of operations, then these optimizations<br>
could apply.<br>
<br>
Do you have code that you think would benefit that I could look at?<br>
</blockquote>
<br></div>
Not yet. I&#39;ve given up on a union of different probability representation types for now.<br>
<br>
For arrays, I could. An array is just a function with a finite, rectangular domain (a &quot;shape&quot;). Think of the type like this:<br>
<br>
  (struct: (A) array ([shape : (Vectorof Index)]<br>
                      [proc : ((Vectorof Index) -&gt; A)]))<br>
<br>
(This is a bit simplified.) So<br>
<br>
  (array-exp (array-abs arr))<br>
<br>
doesn&#39;t actually compute (log (abs x)) for any `x&#39;, it just creates an array that would if you asked it for an element. IOW, it&#39;s equivalent to<br>
<br>
  (array-map (compose exp abs) arr)<br>
<br>
which itself is equivalent to<br>
<br>
  (array (array-shape arr) (compose exp abs (array-proc arr)))<br>
<br>
If I made functions like `array-exp&#39; and `array-map&#39; into macros, and if array structs were unboxed, Racket&#39;s optimizer could inline the compositions. I imagine this eventual lovely outcome:<br>
<br>
  (let ([proc  (array-proc arr)])<br>
    (array (array-shape arr)<br>
           (lambda (x) (unsafe-flexp (unsafe-flabs (proc x))))))<br>
<br>
There are also FlArray and FCArray, which are subtypes of Array that store their elements (unboxed in flvectors and fcvectors). FlArray-specific operations are currently about 5x faster than Array&#39;s. But Array&#39;s ops don&#39;t allocate stores for intermediate results, so with the right optimizations, Array&#39;s could be the faster ones.<br>

<br>
If you want to have a look right now, checkout<br>
<br>
  <a href="https://github.com/ntoronto/racket.git" target="_blank">https://github.com/ntoronto/<u></u>racket.git</a><br>
<br>
and try this program:<br>
<br>
#lang typed/racket<br>
<br>
(require math/array)<br>
<br>
(define arr (array [[1.2 4.0] [3.2 -1.8]]))<br>
(inline-array-map exp (inline-array-map abs arr))<div class="HOEnZb"><div class="h5"><br>
<br>
<br>
Neil ⊥<br>
<br>
_________________________<br>
 Racket Developers list:<br>
 <a href="http://lists.racket-lang.org/dev" target="_blank">http://lists.racket-lang.org/<u></u>dev</a><br>
</div></div></blockquote></div><br></div></div>