Swift: Google’s Bet on Differentiable Programming
Can&#x27;t you do this in Rust by writing a #[derivative] proc macro without having to modify the language?<p><pre><code>    #[derivative]
    fn cube(x: f32) -&gt; f32 { x * x * x }
    &#x2F;&#x2F; expands to 
    &#x2F;&#x2F; impl Derivative for cube {
    &#x2F;&#x2F;    type Args = (f32,);
    &#x2F;&#x2F;    type Return = f32;
    &#x2F;&#x2F;    fn derivative(x: Self::Args) -&gt; Self::Return {
    &#x2F;&#x2F;        3 * x * x 
    &#x2F;&#x2F;    }
    &#x2F;&#x2F; }

    let cubeGrad = gradient(cube);
    assert_eq!(cubeGrad(2), 12);
</code></pre>
where `gradient` is just a normal Rust function:<p><pre><code>    fn gradient&lt;F: Derivative&gt;(_: F) -&gt; impl Fn(F::Args) -&gt; F::Return {
        |x: F::Args|  Derivative::derivative(x) 
    }
</code></pre>
My original attempt was accidentally more powerful, in that it allowed `cube` to be a stateful function like a closure.<p>To implement this, the only thing one needs to do in the `#[derivative]` macro is:<p>* parse the function into an AST<p>* fold the AST using symbolic differentiation<p>* quasi quote the new AST<p>Swift can probably differentiate functions in other translation units because it keeps the bytecode of all functions around. A proc-macro based approach wouldn&#x27;t be able to achieve that, at least, not in general, but a Rust plugin could, since plugins can drive compilation to, e.g., compile whatever function is being derived, and differentiate its bytecode instead of Rust, and if the function happens to be in a different translation unit, access their byte code.