How to locate and extract the logic behind an invoked function or method in Rust?

62 Views Asked by At

I am trying to build an auto-differentiation procedural macro in Rust. Currently, the macro can successfully evaluate the partial derivative of a given expression when all of the logic is contained within the bounds of the macro. However, when attempting to evaluate a function or method call to logic that has been previously defined the system runs into an issue as these items fail to contain all of the information required to successfully compute the partial derivative.

Questions

  • Is it possible to create a function that can locate and extract the body of a function or implemented method call using the rust compiler or some other similar method?

After doing some research, it seems the rustdoc crate has an experimental feature (scrape_examples) which essentially finds all of the relevant function calls found in any examples. I am considering creating a modified version of this to fit my needs, however, would prefer to find a less invasive and more streamlined approach to solving the issue.

Working Examples

use acme::prelude::autodiff;

fn main() {
    let x = 2_f64;
    assert_eq!(autodiff!(x: 1.0 / (1.0 + (-x).exp())), sigmoid_prime(x));
    assert_eq!(
        autodiff!(x: | x: f64 | 1.0 / (1.0 + (-x).exp())),
        sigmoid_prime(x)
    );
    assert_eq!(
        autodiff!(x: fn sigmoid(x: f64) -> f64 { 1_f64 / (1_f64 + (-x).exp()) }),
        sigmoid_prime(x)
    );
}

Objective

use acme::prelude::{autodiff, sigmoid};

fn main() {
    let x = 2_f64;
    assert_ne!(autodiff!(x: sigmoid(x)), sigmoid_prime(x));

}
0

There are 0 best solutions below