title: Haskell evaluating properties of a data type only once when first accessed?
body: <p>In imperative/object oriented programming with mutable state, it would be very common and useful to declare a structure such as the following:</p>
  
  <pre><code>struct RigidBody {
    float m_mass;
    float m_inverseMass;
    Mat3 m_localInverseInertiaTensor;
    Mat3 m_globalInverseInertiaTensor;
  
    Vec3 m_globalCentroid;
    Vec3 m_localCentroid;
  
    Vec3 m_position;
    Mat3 m_orientation;
    Vec3 m_linearVelocity;
    Vec3 m_angularVelocity;
  };
  </code></pre>
  
  <p>Source: <a href="http://allenchou.net/2013/12/game-physics-motion-dynamics-implementations/" rel="nofollow noreferrer">http://allenchou.net/2013/12/game-physics-motion-dynamics-implementations/</a></p>
  
  <p>There are many properties here that are able to be computed directly from others, such as <code>m_inverseMass</code> from <code>m_mass</code>. In a stateless programming language like Haskell, getting derived values is easy enough:</p>
  
  <pre><code>data RigidBody = RigidBody {mass :: Float}
  
  inverseMass :: RigidBody -&gt; Float
  inverseMass body = 1 / mass body
  </code></pre>
  
  <p>But this computes the <code>inverseMass</code> every time we need it, which can get expensive especially in domains where performance is critical, like physics simulation. I've considered memoization, but I wasn't sure if this is a good way of expressing this lazy evaluation of dependent properties, as it seemed to be a complicated solution. How would I store derivative values without having to recompute them?</p>