title: Caching internals with a partially applied function
body: <p>Imagine such a function:</p>
  
  <pre><code>bar :: Foo -&gt; A -&gt; B -&gt; C -&gt; IO ()
  </code></pre>
  
  <p>That function performs some <code>IO</code> stuff using a <code>Foo</code> and other values. The <code>Foo</code> value has to be passed to <code>bar</code>, and can be retrieved from <code>IO</code> via this:</p>
  
  <pre><code>foo :: X -&gt; IO Foo
  </code></pre>
  
  <p>Now, <code>A</code>, <code>B</code>, <code>C</code> and <code>X</code> are all plain pure values. I’d rather like such a <code>bar</code> function:</p>
  
  <pre><code>bar :: X -&gt; A -&gt; B -&gt; C -&gt; IO ()
  </code></pre>
  
  <p>And <code>Foo</code> would be generated in the <code>bar</code> function using the <code>X</code> value. If I do that :</p>
  
  <pre><code>let f = bar myX
  </code></pre>
  
  <p><code>f :: A -&gt; B -&gt; C -&gt; IO ()</code>. If I call that function several times, the <code>X</code> value remains the same because of partial application, but since it’s an <code>IO</code> effect, it will be generated each time. Is there a <em>native</em>, <em>built-in-ghc</em> way to perform some kind of caching so that the <code>Foo</code> value is generated once – for the generated closure? I guess it’s all boxing-related, but I never figured out how to do that without using dirty <code>IORef</code>, expanding the parameters of <code>bar</code>, which is ugly.</p>